PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.2.2
Jetpack – WP Security, Backup, Speed, & Growth v7.2.2
16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 All 502 releases
jetpack / modules / widgets / simple-payments.php

simple-payments.php in Jetpack – WP Security, Backup, Speed, & Growth 7.2.2, at modules/widgets/simple-payments.php

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