Geen omschrijving

simple-payments.php 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. <?php
  2. use Automattic\Jetpack\Tracking;
  3. /**
  4. * Disable direct access/execution to/of the widget code.
  5. */
  6. if ( ! defined( 'ABSPATH' ) ) {
  7. exit;
  8. }
  9. if ( ! class_exists( 'Jetpack_Simple_Payments_Widget' ) ) {
  10. /**
  11. * Pay with PayPal (aka Simple Payments)
  12. *
  13. * Display a Pay with PayPal button as a Widget.
  14. */
  15. class Jetpack_Simple_Payments_Widget extends WP_Widget {
  16. /**
  17. * Currencies should be supported by PayPal:
  18. * @link https://developer.paypal.com/docs/api/reference/currency-codes/
  19. *
  20. * List has to be in sync with list at the block's client side and API's backend side:
  21. * @link https://github.com/Automattic/jetpack/blob/31efa189ad223c0eb7ad085ac0650a23facf9ef5/extensions/blocks/simple-payments/constants.js#L9-L39
  22. * @link https://github.com/Automattic/jetpack/blob/31efa189ad223c0eb7ad085ac0650a23facf9ef5/modules/simple-payments/simple-payments.php#L386-L415
  23. *
  24. * Indian Rupee (INR) is listed here for backwards compatibility with previously added widgets.
  25. * It's not supported by Pay with PayPal because at the time of the creation of this file
  26. * because it's limited to in-country PayPal India accounts only.
  27. * Discussion: https://github.com/Automattic/wp-calypso/pull/28236
  28. */
  29. private static $supported_currency_list = array(
  30. 'USD' => '$',
  31. 'GBP' => '&#163;',
  32. 'JPY' => '&#165;',
  33. 'BRL' => 'R$',
  34. 'EUR' => '&#8364;',
  35. 'NZD' => 'NZ$',
  36. 'AUD' => 'A$',
  37. 'CAD' => 'C$',
  38. 'INR' => '₹',
  39. 'ILS' => '₪',
  40. 'RUB' => '₽',
  41. 'MXN' => 'MX$',
  42. 'SEK' => 'Skr',
  43. 'HUF' => 'Ft',
  44. 'CHF' => 'CHF',
  45. 'CZK' => 'Kč',
  46. 'DKK' => 'Dkr',
  47. 'HKD' => 'HK$',
  48. 'NOK' => 'Kr',
  49. 'PHP' => '₱',
  50. 'PLN' => 'PLN',
  51. 'SGD' => 'S$',
  52. 'TWD' => 'NT$',
  53. 'THB' => '฿',
  54. );
  55. /**
  56. * Constructor.
  57. */
  58. function __construct() {
  59. parent::__construct(
  60. 'jetpack_simple_payments_widget',
  61. /** This filter is documented in modules/widgets/facebook-likebox.php */
  62. apply_filters( 'jetpack_widget_name', __( 'Pay with PayPal', 'jetpack' ) ),
  63. array(
  64. 'classname' => 'jetpack-simple-payments',
  65. 'description' => __( 'Add a Pay with PayPal button as a Widget.', 'jetpack' ),
  66. 'customize_selective_refresh' => true,
  67. )
  68. );
  69. global $pagenow;
  70. if ( is_customize_preview() || 'widgets.php' === $pagenow ) {
  71. add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_styles' ) );
  72. }
  73. $jetpack_simple_payments = Jetpack_Simple_Payments::getInstance();
  74. if ( is_customize_preview() && $jetpack_simple_payments->is_enabled_jetpack_simple_payments() ) {
  75. add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
  76. add_filter( 'customize_refresh_nonces', array( $this, 'filter_nonces' ) );
  77. add_action( 'wp_ajax_customize-jetpack-simple-payments-buttons-get', array( $this, 'ajax_get_payment_buttons' ) );
  78. add_action( 'wp_ajax_customize-jetpack-simple-payments-button-save', array( $this, 'ajax_save_payment_button' ) );
  79. add_action( 'wp_ajax_customize-jetpack-simple-payments-button-delete', array( $this, 'ajax_delete_payment_button' ) );
  80. }
  81. if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) {
  82. add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
  83. }
  84. }
  85. /**
  86. * Return an associative array of default values.
  87. *
  88. * These values are used in new widgets.
  89. *
  90. * @return array Default values for the widget options.
  91. */
  92. private function defaults() {
  93. $current_user = wp_get_current_user();
  94. $default_product_id = $this->get_first_product_id();
  95. return array(
  96. 'title' => '',
  97. 'product_post_id' => $default_product_id,
  98. 'form_action' => '',
  99. 'form_product_id' => 0,
  100. 'form_product_title' => '',
  101. 'form_product_description' => '',
  102. 'form_product_image_id' => 0,
  103. 'form_product_image_src' => '',
  104. 'form_product_currency' => '',
  105. 'form_product_price' => '',
  106. 'form_product_multiple' => '',
  107. 'form_product_email' => $current_user->user_email,
  108. );
  109. }
  110. /**
  111. * Adds a nonce for customizing menus.
  112. *
  113. * @param array $nonces Array of nonces.
  114. * @return array $nonces Modified array of nonces.
  115. */
  116. function filter_nonces( $nonces ) {
  117. $nonces['customize-jetpack-simple-payments'] = wp_create_nonce( 'customize-jetpack-simple-payments' );
  118. return $nonces;
  119. }
  120. function enqueue_style() {
  121. wp_enqueue_style( 'jetpack-simple-payments-widget-style', plugins_url( 'simple-payments/style.css', __FILE__ ), array(), '20180518' );
  122. }
  123. function admin_enqueue_styles() {
  124. wp_enqueue_style( 'jetpack-simple-payments-widget-customizer', plugins_url( 'simple-payments/customizer.css', __FILE__ ) );
  125. }
  126. function admin_enqueue_scripts() {
  127. wp_enqueue_media();
  128. wp_enqueue_script( 'jetpack-simple-payments-widget-customizer', plugins_url( '/simple-payments/customizer.js', __FILE__ ), array( 'jquery' ), false, true );
  129. wp_localize_script(
  130. 'jetpack-simple-payments-widget-customizer', 'jpSimplePaymentsStrings', array(
  131. 'deleteConfirmation' => __( 'Are you sure you want to delete this item? It will be disabled and removed from all locations where it currently appears.', 'jetpack' ),
  132. )
  133. );
  134. }
  135. public function ajax_get_payment_buttons() {
  136. if ( ! check_ajax_referer( 'customize-jetpack-simple-payments', 'customize-jetpack-simple-payments-nonce', false ) ) {
  137. wp_send_json_error( 'bad_nonce', 400 );
  138. }
  139. if ( ! current_user_can( 'customize' ) ) {
  140. wp_send_json_error( 'customize_not_allowed', 403 );
  141. }
  142. $post_type_object = get_post_type_object( Jetpack_Simple_Payments::$post_type_product );
  143. if ( ! current_user_can( $post_type_object->cap->create_posts ) || ! current_user_can( $post_type_object->cap->publish_posts ) ) {
  144. wp_send_json_error( 'insufficient_post_permissions', 403 );
  145. }
  146. $product_posts = get_posts(
  147. array(
  148. 'numberposts' => 100,
  149. 'orderby' => 'date',
  150. 'post_type' => Jetpack_Simple_Payments::$post_type_product,
  151. 'post_status' => 'publish',
  152. )
  153. );
  154. $formatted_products = array_map( array( $this, 'format_product_post_for_ajax_reponse' ), $product_posts );
  155. wp_send_json_success( $formatted_products );
  156. }
  157. public function format_product_post_for_ajax_reponse( $product_post ) {
  158. return array(
  159. 'ID' => $product_post->ID,
  160. 'post_title' => $product_post->post_title,
  161. );
  162. }
  163. public function ajax_save_payment_button() {
  164. if ( ! check_ajax_referer( 'customize-jetpack-simple-payments', 'customize-jetpack-simple-payments-nonce', false ) ) {
  165. wp_send_json_error( 'bad_nonce', 400 );
  166. }
  167. if ( ! current_user_can( 'customize' ) ) {
  168. wp_send_json_error( 'customize_not_allowed', 403 );
  169. }
  170. $post_type_object = get_post_type_object( Jetpack_Simple_Payments::$post_type_product );
  171. if ( ! current_user_can( $post_type_object->cap->create_posts ) || ! current_user_can( $post_type_object->cap->publish_posts ) ) {
  172. wp_send_json_error( 'insufficient_post_permissions', 403 );
  173. }
  174. if ( empty( $_POST['params'] ) || ! is_array( $_POST['params'] ) ) {
  175. wp_send_json_error( 'missing_params', 400 );
  176. }
  177. $params = wp_unslash( $_POST['params'] );
  178. $errors = $this->validate_ajax_params( $params );
  179. if ( ! empty( $errors->errors ) ) {
  180. wp_send_json_error( $errors );
  181. }
  182. $product_post_id = isset( $params['product_post_id'] ) ? (int) $params['product_post_id'] : 0;
  183. $product_post = array(
  184. 'ID' => $product_post_id,
  185. 'post_type' => Jetpack_Simple_Payments::$post_type_product,
  186. 'post_status' => 'publish',
  187. 'post_title' => $params['post_title'],
  188. 'post_content' => $params['post_content'],
  189. '_thumbnail_id' => ! empty( $params['image_id'] ) ? $params['image_id'] : -1,
  190. 'meta_input' => array(
  191. 'spay_currency' => $params['currency'],
  192. 'spay_price' => $params['price'],
  193. 'spay_multiple' => isset( $params['multiple'] ) ? (int) $params['multiple'] : 0,
  194. 'spay_email' => is_email( $params['email'] ),
  195. ),
  196. );
  197. if ( empty( $product_post_id ) ) {
  198. $product_post_id = wp_insert_post( $product_post );
  199. } else {
  200. $product_post_id = wp_update_post( $product_post );
  201. }
  202. if ( ! $product_post_id || is_wp_error( $product_post_id ) ) {
  203. wp_send_json_error( $product_post_id );
  204. }
  205. $tracks_properties = array(
  206. 'id' => $product_post_id,
  207. 'currency' => $params['currency'],
  208. 'price' => $params['price'],
  209. );
  210. if ( 0 === $product_post['ID'] ) {
  211. $this->record_event( 'created', 'create', $tracks_properties );
  212. } else {
  213. $this->record_event( 'updated', 'update', $tracks_properties );
  214. }
  215. wp_send_json_success(
  216. array(
  217. 'product_post_id' => $product_post_id,
  218. 'product_post_title' => $params['post_title'],
  219. )
  220. );
  221. }
  222. public function ajax_delete_payment_button() {
  223. if ( ! check_ajax_referer( 'customize-jetpack-simple-payments', 'customize-jetpack-simple-payments-nonce', false ) ) {
  224. wp_send_json_error( 'bad_nonce', 400 );
  225. }
  226. if ( ! current_user_can( 'customize' ) ) {
  227. wp_send_json_error( 'customize_not_allowed', 403 );
  228. }
  229. if ( empty( $_POST['params'] ) || ! is_array( $_POST['params'] ) ) {
  230. wp_send_json_error( 'missing_params', 400 );
  231. }
  232. $params = wp_unslash( $_POST['params'] );
  233. $illegal_params = array_diff( array_keys( $params ), array( 'product_post_id' ) );
  234. if ( ! empty( $illegal_params ) ) {
  235. wp_send_json_error( 'illegal_params', 400 );
  236. }
  237. $product_id = (int) $params['product_post_id'];
  238. $product_post = get_post( $product_id );
  239. $return = array( 'status' => $product_post->post_status );
  240. wp_delete_post( $product_id, true );
  241. $status = get_post_status( $product_id );
  242. if ( false === $status ) {
  243. $return['status'] = 'deleted';
  244. }
  245. $this->record_event( 'deleted', 'delete', array( 'id' => $product_id ) );
  246. wp_send_json_success( $return );
  247. }
  248. /**
  249. * Returns the number of decimal places on string representing a price.
  250. *
  251. * @param string $number Price to check.
  252. * @return number number of decimal places.
  253. */
  254. private function get_decimal_places( $number ) {
  255. $parts = explode( '.', $number );
  256. if ( count( $parts ) > 2 ) {
  257. return null;
  258. }
  259. return isset( $parts[1] ) ? strlen( $parts[1] ) : 0;
  260. }
  261. public function validate_ajax_params( $params ) {
  262. $errors = new WP_Error();
  263. $illegal_params = array_diff( array_keys( $params ), array( 'product_post_id', 'post_title', 'post_content', 'image_id', 'currency', 'price', 'multiple', 'email' ) );
  264. if ( ! empty( $illegal_params ) ) {
  265. $errors->add( 'illegal_params', __( 'Invalid parameters.', 'jetpack' ) );
  266. }
  267. if ( empty( $params['post_title'] ) ) {
  268. $errors->add( 'post_title', __( "People need to know what they're paying for! Please add a brief title.", 'jetpack' ) );
  269. }
  270. if ( empty( $params['price'] ) || ! is_numeric( $params['price'] ) || (float) $params['price'] <= 0 ) {
  271. $errors->add( 'price', __( 'Everything comes with a price tag these days. Please add a your product price.', 'jetpack' ) );
  272. }
  273. // Japan's Yen is the only supported currency with a zero decimal precision.
  274. $precision = strtoupper( $params['currency'] ) === 'JPY' ? 0 : 2;
  275. $price_decimal_places = $this->get_decimal_places( $params['price'] );
  276. if ( is_null( $price_decimal_places ) || $price_decimal_places > $precision ) {
  277. $errors->add( 'price', __( 'Invalid price', 'jetpack' ) );
  278. }
  279. if ( empty( $params['email'] ) || ! is_email( $params['email'] ) ) {
  280. $errors->add( 'email', __( 'We want to make sure payments reach you, so please add an email address.', 'jetpack' ) );
  281. }
  282. return $errors;
  283. }
  284. function get_first_product_id() {
  285. $product_posts = get_posts(
  286. array(
  287. 'numberposts' => 1,
  288. 'orderby' => 'date',
  289. 'post_type' => Jetpack_Simple_Payments::$post_type_product,
  290. 'post_status' => 'publish',
  291. )
  292. );
  293. return ! empty( $product_posts ) ? $product_posts[0]->ID : null;
  294. }
  295. /**
  296. * Front-end display of widget.
  297. *
  298. * @see WP_Widget::widget()
  299. *
  300. * @param array $args Widget arguments.
  301. * @param array $instance Saved values from database.
  302. */
  303. function widget( $args, $instance ) {
  304. $instance = wp_parse_args( $instance, $this->defaults() );
  305. echo $args['before_widget'];
  306. /** This filter is documented in core/src/wp-includes/default-widgets.php */
  307. $title = apply_filters( 'widget_title', $instance['title'] );
  308. if ( ! empty( $title ) ) {
  309. echo $args['before_title'] . $title . $args['after_title'];
  310. }
  311. echo '<div class="jetpack-simple-payments-content">';
  312. if ( ! empty( $instance['form_action'] ) && in_array( $instance['form_action'], array( 'add', 'edit' ) ) && is_customize_preview() ) {
  313. require( dirname( __FILE__ ) . '/simple-payments/widget.php' );
  314. } else {
  315. $jsp = Jetpack_Simple_Payments::getInstance();
  316. $simple_payments_button = $jsp->parse_shortcode(
  317. array(
  318. 'id' => $instance['product_post_id'],
  319. )
  320. );
  321. if ( ! is_null( $simple_payments_button ) || is_customize_preview() ) {
  322. echo $simple_payments_button;
  323. }
  324. }
  325. echo '</div><!--simple-payments-->';
  326. echo $args['after_widget'];
  327. /** This action is already documented in modules/widgets/gravatar-profile.php */
  328. do_action( 'jetpack_stats_extra', 'widget_view', 'simple_payments' );
  329. }
  330. /**
  331. * Gets the latests field value from either the old instance or the new instance.
  332. *
  333. * @param array $mixed Array of values for the new form instance.
  334. * @param array $mixed Array of values for the old form instance.
  335. * @return mixed $mixed Field value.
  336. */
  337. private function get_latest_field_value( $new_instance, $old_instance, $field ) {
  338. return ! empty( $new_instance[ $field ] )
  339. ? sanitize_text_field( $new_instance[ $field ] )
  340. : $old_instance[ $field ];
  341. }
  342. /**
  343. * Gets the product fields from the product post. If no post found
  344. * it returns the default values.
  345. *
  346. * @param int Product Post ID.
  347. * @return array $fields Product Fields from the Product Post.
  348. */
  349. private function get_product_from_post( $product_post_id ) {
  350. $product_post = get_post( $product_post_id );
  351. $form_product_id = $product_post_id;
  352. if ( ! empty( $product_post ) ) {
  353. $form_product_image_id = get_post_thumbnail_id( $product_post_id );
  354. return array(
  355. 'form_product_id' => $form_product_id,
  356. 'form_product_title' => get_the_title( $product_post ),
  357. 'form_product_description' => $product_post->post_content,
  358. 'form_product_image_id' => $form_product_image_id,
  359. 'form_product_image_src' => wp_get_attachment_image_url( $form_product_image_id, 'thumbnail' ),
  360. 'form_product_currency' => get_post_meta( $product_post_id, 'spay_currency', true ),
  361. 'form_product_price' => get_post_meta( $product_post_id, 'spay_price', true ),
  362. 'form_product_multiple' => get_post_meta( $product_post_id, 'spay_multiple', true ) || '0',
  363. 'form_product_email' => get_post_meta( $product_post_id, 'spay_email', true ),
  364. );
  365. }
  366. return $this->defaults();
  367. }
  368. /**
  369. * Record a Track event and bump a MC stat.
  370. *
  371. * @param string $stat_name
  372. * @param string $event_action
  373. * @param array $event_properties
  374. */
  375. private function record_event( $stat_name, $event_action, $event_properties = array() ) {
  376. $current_user = wp_get_current_user();
  377. // `bumps_stats_extra` only exists on .com
  378. if ( function_exists( 'bump_stats_extras' ) ) {
  379. jetpack_require_lib( 'tracks/client' );
  380. tracks_record_event( $current_user, 'simple_payments_button_' . $event_action, $event_properties );
  381. /** This action is documented in modules/widgets/social-media-icons.php */
  382. do_action( 'jetpack_bump_stats_extra', 'jetpack-simple_payments', $stat_name );
  383. return;
  384. }
  385. $tracking = new Tracking();
  386. $tracking->tracks_record_event( $current_user, 'jetpack_wpa_simple_payments_button_' . $event_action, $event_properties );
  387. $jetpack = Jetpack::init();
  388. // $jetpack->stat automatically prepends the stat group with 'jetpack-'
  389. $jetpack->stat( 'simple_payments', $stat_name );
  390. $jetpack->do_stats( 'server_side' );
  391. }
  392. /**
  393. * Sanitize widget form values as they are saved.
  394. *
  395. * @see WP_Widget::update()
  396. *
  397. * @param array $new_instance Values just sent to be saved.
  398. * @param array $old_instance Previously saved values from database.
  399. *
  400. * @return array Updated safe values to be saved.
  401. */
  402. function update( $new_instance, $old_instance ) {
  403. $defaults = $this->defaults();
  404. //do not overrite `product_post_id` for `$new_instance` with the defaults
  405. $new_instance = wp_parse_args( $new_instance, array_diff_key( $defaults, array( 'product_post_id' => 0 ) ) );
  406. $old_instance = wp_parse_args( $old_instance, $defaults );
  407. $required_widget_props = array(
  408. 'title' => $this->get_latest_field_value( $new_instance, $old_instance, 'title' ),
  409. 'product_post_id' => $this->get_latest_field_value( $new_instance, $old_instance, 'product_post_id' ),
  410. 'form_action' => $this->get_latest_field_value( $new_instance, $old_instance, 'form_action' ),
  411. );
  412. if ( strcmp( $new_instance['form_action'], $old_instance['form_action'] ) !== 0 ) {
  413. if ( $new_instance['form_action'] == 'edit' ) {
  414. return array_merge( $this->get_product_from_post( (int) $old_instance['product_post_id'] ), $required_widget_props );
  415. }
  416. if ( $new_instance['form_action'] == 'clear' ) {
  417. return array_merge( $this->defaults(), $required_widget_props );
  418. }
  419. }
  420. $form_product_image_id = (int) $new_instance['form_product_image_id'];
  421. $form_product_email = ! empty( $new_instance['form_product_email'] )
  422. ? sanitize_text_field( $new_instance['form_product_email'] )
  423. : $defaults['form_product_email'];
  424. return array_merge(
  425. $required_widget_props, array(
  426. 'form_product_id' => (int) $new_instance['form_product_id'],
  427. 'form_product_title' => sanitize_text_field( $new_instance['form_product_title'] ),
  428. 'form_product_description' => sanitize_text_field( $new_instance['form_product_description'] ),
  429. 'form_product_image_id' => $form_product_image_id,
  430. 'form_product_image_src' => wp_get_attachment_image_url( $form_product_image_id, 'thumbnail' ),
  431. 'form_product_currency' => sanitize_text_field( $new_instance['form_product_currency'] ),
  432. 'form_product_price' => sanitize_text_field( $new_instance['form_product_price'] ),
  433. 'form_product_multiple' => sanitize_text_field( $new_instance['form_product_multiple'] ),
  434. 'form_product_email' => $form_product_email,
  435. )
  436. );
  437. }
  438. /**
  439. * Back-end widget form.
  440. *
  441. * @see WP_Widget::form()
  442. *
  443. * @param array $instance Previously saved values from database.
  444. */
  445. function form( $instance ) {
  446. $jetpack_simple_payments = Jetpack_Simple_Payments::getInstance();
  447. if ( ! method_exists( $jetpack_simple_payments, 'is_enabled_jetpack_simple_payments' ) ) {
  448. return;
  449. }
  450. if ( ! $jetpack_simple_payments->is_enabled_jetpack_simple_payments() ) {
  451. require dirname( __FILE__ ) . '/simple-payments/admin-warning.php';
  452. return;
  453. }
  454. $instance = wp_parse_args( $instance, $this->defaults() );
  455. $product_posts = get_posts(
  456. array(
  457. 'numberposts' => 100,
  458. 'orderby' => 'date',
  459. 'post_type' => Jetpack_Simple_Payments::$post_type_product,
  460. 'post_status' => 'publish',
  461. )
  462. );
  463. require dirname( __FILE__ ) . '/simple-payments/form.php';
  464. }
  465. }
  466. // Register Jetpack_Simple_Payments_Widget widget.
  467. function register_widget_jetpack_simple_payments() {
  468. if ( ! class_exists( 'Jetpack_Simple_Payments' ) ) {
  469. return;
  470. }
  471. $jetpack_simple_payments = Jetpack_Simple_Payments::getInstance();
  472. if ( ! $jetpack_simple_payments->is_enabled_jetpack_simple_payments() ) {
  473. return;
  474. }
  475. register_widget( 'Jetpack_Simple_Payments_Widget' );
  476. }
  477. add_action( 'widgets_init', 'register_widget_jetpack_simple_payments' );
  478. }