Completed
Push — add/simple-payments-metrics ( 1129db...a9661d )
by
unknown
15:24
created

Jetpack_Simple_Payments_Widget::form()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
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 Payment 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(
0 ignored issues
show
Unused Code introduced by
The property $supported_currency_list is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
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' => 'Kč',
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 Payment Button as a Widget.', 'jetpack' ),
55
					'customize_selective_refresh' => true,
56
				)
57
			);
58
59
			if ( is_customize_preview() ) {
60
				add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_styles_and_scripts' ) );
61
62
				add_filter( 'customize_refresh_nonces', array( $this, 'filter_nonces' ) );
63
				add_action( 'wp_ajax_customize-jetpack-simple-payments-button-save', array( $this, 'ajax_save_payment_button' ) );
64
				add_action( 'wp_ajax_customize-jetpack-simple-payments-button-delete', array( $this, 'ajax_delete_payment_button' ) );
65
			}
66
67
			if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) {
68
				add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
69
			}
70
		}
71
72
		/**
73
		 * Return an associative array of default values.
74
		 *
75
		 * These values are used in new widgets.
76
		 *
77
		 * @return array Default values for the widget options.
78
		 */
79
		private function defaults() {
80
			$current_user = wp_get_current_user();
81
			$default_product_id = $this->get_first_product_id();
82
83
			return array(
84
				'title' => '',
85
				'product_post_id' => $default_product_id,
86
				'form_action' => '',
87
				'form_product_id' => 0,
88
				'form_product_title' => '',
89
				'form_product_description' => '',
90
				'form_product_image_id' => 0,
91
				'form_product_image_src' => '',
92
				'form_product_currency' => '',
93
				'form_product_price' => '',
94
				'form_product_multiple' => '',
95
				'form_product_email' => $current_user->user_email,
96
			);
97
		}
98
99
		/**
100
		 * Adds a nonce for customizing menus.
101
		 *
102
		 * @param array $nonces Array of nonces.
103
		 * @return array $nonces Modified array of nonces.
104
		 */
105
		function filter_nonces( $nonces ) {
106
			$nonces['customize-jetpack-simple-payments'] = wp_create_nonce( 'customize-jetpack-simple-payments' );
107
			return $nonces;
108
		}
109
110
		function enqueue_style() {
111
			wp_enqueue_style( 'jetpack-simple-payments-widget-style', plugins_url( 'simple-payments/style.css', __FILE__ ), array(), '20180518' );
112
		}
113
114
		function admin_enqueue_styles_and_scripts(){
115
				wp_enqueue_style( 'jetpack-simple-payments-widget-customizer', plugins_url( 'simple-payments/customizer.css', __FILE__ ) );
116
117
				wp_enqueue_media();
118
				wp_enqueue_script( 'jetpack-simple-payments-widget-customizer', plugins_url( '/simple-payments/customizer.js', __FILE__ ), array( 'jquery' ), false, true );
119
				wp_localize_script( 'jetpack-simple-payments-widget-customizer', 'jpSimplePaymentsStrings', array(
120
					'deleteConfirmation' => __( 'Are you sure you want to delete this item? It will be disabled and removed from all locations where it currently appears.', 'jetpack' )
121
				) );
122
		}
123
124
		public function ajax_save_payment_button() {
125
			if ( ! check_ajax_referer( 'customize-jetpack-simple-payments', 'customize-jetpack-simple-payments-nonce', false ) ) {
126
				wp_send_json_error( 'bad_nonce', 400 );
127
			}
128
129
			if ( ! current_user_can( 'customize' ) ) {
130
				wp_send_json_error( 'customize_not_allowed', 403 );
131
			}
132
133
			$post_type_object = get_post_type_object( Jetpack_Simple_Payments::$post_type_product );
0 ignored issues
show
Bug introduced by
The property post_type_product cannot be accessed from this context as it is declared private in class Jetpack_Simple_Payments.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
134
			if ( ! current_user_can( $post_type_object->cap->create_posts ) || ! current_user_can( $post_type_object->cap->publish_posts ) ) {
135
				wp_send_json_error( 'insufficient_post_permissions', 403 );
136
			}
137
138 View Code Duplication
			if ( empty( $_POST['params'] ) || ! is_array( $_POST['params'] ) ) {
139
				wp_send_json_error( 'missing_params', 400 );
140
			}
141
142
			$params = wp_unslash( $_POST['params'] );
143
			$errors = $this->validate_ajax_params( $params );
144
			if ( ! empty( $errors->errors ) ) {
145
				wp_send_json_error( $errors );
146
			}
147
148
			$product_post_id = isset( $params['product_post_id'] ) ? intval( $params['product_post_id'] ) : 0;
149
150
			$product_post = array(
151
				'ID' => $product_post_id,
152
				'post_type' => Jetpack_Simple_Payments::$post_type_product,
0 ignored issues
show
Bug introduced by
The property post_type_product cannot be accessed from this context as it is declared private in class Jetpack_Simple_Payments.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
153
				'post_status' => 'publish',
154
				'post_title' => $params['post_title'],
155
				'post_content' => $params['post_content'],
156
				'_thumbnail_id' => ! empty( $params['image_id'] ) ? $params['image_id'] : -1,
157
				'meta_input' => array(
158
					'spay_currency' => $params['currency'],
159
					'spay_price' => $params['price'],
160
					'spay_multiple' => isset( $params['multiple'] ) ? intval( $params['multiple'] ) : 0,
161
					'spay_email' => is_email( $params['email'] ),
162
				),
163
			);
164
165
			if ( empty( $product_post_id ) ) {
166
				$product_post_id = wp_insert_post( $product_post );
167
			} else {
168
				$product_post_id = wp_update_post( $product_post );
169
			}
170
171
			if ( ! $product_post_id || is_wp_error( $product_post_id ) ) {
172
				wp_send_json_error( $product_post_id );
173
			}
174
175
			$tracks_properties = array(
176
				'id'       => $product_post_id,
177
				'currency' => $params['currency'],
178
				'price'    => $params['price']
179
			);
180
			if ( 0 === $product_post['ID'] ) {
181
				$this->record_event( 'created', 'create', $tracks_properties );
182
			} else {
183
				$this->record_event( 'updated', 'update', $tracks_properties );
184
			}
185
186
			wp_send_json_success( [
187
				'product_post_id' => $product_post_id,
188
				'product_post_title' => $params['post_title'],
189
			] );
190
		}
191
192
		public function ajax_delete_payment_button() {
193
			if ( ! check_ajax_referer( 'customize-jetpack-simple-payments', 'customize-jetpack-simple-payments-nonce', false ) ) {
194
				wp_send_json_error( 'bad_nonce', 400 );
195
			}
196
197
			if ( ! current_user_can( 'customize' ) ) {
198
				wp_send_json_error( 'customize_not_allowed', 403 );
199
			}
200
201 View Code Duplication
			if ( empty( $_POST['params'] ) || ! is_array( $_POST['params'] ) ) {
202
				wp_send_json_error( 'missing_params', 400 );
203
			}
204
205
			$params = wp_unslash( $_POST['params'] );
206
			$illegal_params = array_diff( array_keys( $params ), array( 'product_post_id' ) );
207
			if ( ! empty( $illegal_params ) ) {
208
				wp_send_json_error( 'illegal_params', 400 );
209
			}
210
211
			$product_id = ( int ) $params['product_post_id'];
212
			$product_post = get_post( $product_id );
213
214
			$return = array( 'status' => $product_post->post_status );
215
216
			wp_delete_post( $product_id, true );
217
			$status = get_post_status( $product_id );
218
			if ( false === $status ) {
219
				$return['status'] = 'deleted';
220
			}
221
222
			$this->record_event( 'deleted', 'delete', array( 'id' => $product_id ) );
223
224
			wp_send_json_success( $return );
225
		}
226
227
		public function validate_ajax_params( $params ) {
228
			$errors = new WP_Error();
229
230
			$illegal_params = array_diff( array_keys( $params ), array( 'product_post_id', 'post_title', 'post_content', 'image_id', 'currency', 'price', 'multiple', 'email' ) );
231
			if ( ! empty( $illegal_params ) ) {
232
				$errors.add( 'illegal_params' );
233
			}
234
235
			if ( empty( $params['post_title'] ) ) {
236
				$errors->add( 'post_title', __( 'People need to know what they\'re paying for! Please add a brief title.' ) );
237
			}
238
239
			if ( empty( $params['price'] ) || intval( $params['price'] ) < 0 ) {
240
				$errors->add( 'price', __( 'Everything comes with a price tag these days. Please add a your product price.' ) );
241
			}
242
243
			if ( empty( $params['email'] ) || ! is_email( $params['email'] ) ) {
244
				$errors->add( 'email', __( 'We want to make sure payments reach you, so please add an email address.' ) );
245
			}
246
247
			return $errors;
248
		}
249
250
		function get_first_product_id() {
251
			$product_posts = get_posts( array(
252
				'numberposts' => 1,
253
				'orderby' => 'date',
254
				'post_type' => Jetpack_Simple_Payments::$post_type_product,
0 ignored issues
show
Bug introduced by
The property post_type_product cannot be accessed from this context as it is declared private in class Jetpack_Simple_Payments.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
255
				'post_status' => 'publish',
256
			 ) );
257
258
			return ! empty( $product_posts ) ? $product_posts[0]->ID : null;
259
		}
260
261
		/**
262
		 * Front-end display of widget.
263
		 *
264
		 * @see WP_Widget::widget()
265
		 *
266
		 * @param array $args     Widget arguments.
267
		 * @param array $instance Saved values from database.
268
		 */
269
		function widget( $args, $instance ) {
270
			$instance = wp_parse_args( $instance, $this->defaults() );
271
272
			echo $args['before_widget'];
273
274
			/** This filter is documented in core/src/wp-includes/default-widgets.php */
275
			$title = apply_filters( 'widget_title', $instance['title'] );
276
			if ( ! empty( $title ) ) {
277
				echo $args['before_title'] . $title . $args['after_title'];
278
			}
279
280
			echo '<div class="jetpack-simple-payments-content">';
281
282
			if ( ! empty( $instance['form_action'] ) && in_array( $instance['form_action'], array( 'add', 'edit' ) ) && is_customize_preview() ) {
283
				require( dirname( __FILE__ ) . '/simple-payments/widget.php' );
284
			} else {
285
				$jsp = Jetpack_Simple_Payments::getInstance();
286
				$simple_payments_button = $jsp->parse_shortcode( array(
287
					'id' => $instance['product_post_id'],
288
				) );
289
290
				if ( ! is_null( $simple_payments_button ) || is_customize_preview() ) {
291
					echo $simple_payments_button;
292
				}
293
			}
294
295
			echo '</div><!--simple-payments-->';
296
297
			echo $args['after_widget'];
298
299
			/** This action is already documented in modules/widgets/gravatar-profile.php */
300
			do_action( 'jetpack_stats_extra', 'widget_view', 'simple_payments' );
301
		}
302
303
		/**
304
		 * Gets the latests field value from either the old instance or the new instance.
305
		 *
306
		 * @param array $mixed Array of values for the new form instance.
0 ignored issues
show
Bug introduced by
There is no parameter named $mixed. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
307
		 * @param array $mixed Array of values for the old form instance.
0 ignored issues
show
Bug introduced by
There is no parameter named $mixed. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
308
		 * @return mixed $mixed Field value.
309
		 */
310
		private function get_latest_field_value( $new_instance, $old_instance, $field) {
311
			return ! empty( $new_instance[ $field ] )
312
				? sanitize_text_field( $new_instance[ $field ] )
313
				: $old_instance[ $field ];
314
		}
315
316
		/**
317
		 * Gets the product fields from the product post. If no post found
318
		 * it returns the default values.
319
		 *
320
		 * @param int Product Post ID.
321
		 * @return array $fields Product Fields from the Product Post.
322
		 */
323
		private function get_product_from_post( $product_post_id ) {
324
			$product_post = get_post( $product_post_id );
325
			$form_product_id = $product_post_id;
326
			if( ! empty( $product_post ) ) {
327
				$form_product_image_id = get_post_thumbnail_id( $product_post_id );
328
329
				return array(
330
					'form_product_id' => $form_product_id,
331
					'form_product_title' => get_the_title( $product_post ),
332
					'form_product_description' => $product_post->post_content,
333
					'form_product_image_id' => $form_product_image_id,
334
					'form_product_image_src' => wp_get_attachment_image_url( $form_product_image_id, 'thumbnail' ),
335
					'form_product_currency' => get_post_meta( $product_post_id, 'spay_currency', true ),
336
					'form_product_price' => get_post_meta( $product_post_id, 'spay_price', true ),
337
					'form_product_multiple' => get_post_meta( $product_post_id, 'spay_multiple', true ) || '0',
338
					'form_product_email' => get_post_meta( $product_post_id, 'spay_email', true ),
339
				);
340
			}
341
342
			return $this->defaults();
343
		}
344
345
		/**
346
		 * Record a Track event and bump a MC stat.
347
		 *
348
		 * @param string $stat_name
349
		 * @param string $event_action
350
		 * @param array $event_properties
351
		 */
352
		private function record_event( $stat_name, $event_action, $event_properties = array() ) {
353
			$current_user = wp_get_current_user();
354
355
			// `bumps_stats_extra` only exists on .com
356
			if ( function_exists( 'bump_stats_extras' ) ) {
357
				require_lib( 'tracks/client' );
358
				tracks_record_event( $current_user, 'simple_payments_button_' . $event_action, $event_properties );
359
				/** This action is documented in modules/widgets/social-media-icons.php */
360
				do_action( 'jetpack_bump_stats_extra', 'jetpack-simple_payments', $stat_name );
361
				return;
362
			}
363
364
			jetpack_tracks_record_event( $current_user, 'jetpack_wpa_simple_payments_button_' . $event_action, $event_properties );
365
			$jetpack = Jetpack::init();
366
			// $jetpack->stat automatically prepends the stat group with 'jetpack-'
367
			$jetpack->stat( 'simple_payments', $stat_name ) ;
368
			$jetpack->do_stats( 'server_side' );
369
		}
370
371
		/**
372
		 * Sanitize widget form values as they are saved.
373
		 *
374
		 * @see WP_Widget::update()
375
		 *
376
		 * @param array $new_instance Values just sent to be saved.
377
		 * @param array $old_instance Previously saved values from database.
378
		 *
379
		 * @return array Updated safe values to be saved.
380
		 */
381
		function update( $new_instance, $old_instance ) {
382
			$defaults = $this->defaults();
383
			//do not overrite `product_post_id` for `$new_instance` with the defaults
384
			$new_instance = wp_parse_args( $new_instance, array_diff_key( $defaults, array( 'product_post_id' => 0 ) ) );
385
			$old_instance = wp_parse_args( $old_instance, $defaults );
386
387
			$required_widget_props = array(
388
				'title' => $this->get_latest_field_value( $new_instance, $old_instance, 'title' ),
389
				'product_post_id' => $this->get_latest_field_value( $new_instance, $old_instance, 'product_post_id' ),
390
				'form_action' => $this->get_latest_field_value( $new_instance, $old_instance, 'form_action' ),
391
			);
392
393
			if ( strcmp( $new_instance['form_action'], $old_instance['form_action'] ) !== 0 ) {
394
				if ( $new_instance['form_action'] == 'edit' ) {
395
					return array_merge( $this->get_product_from_post( ( int ) $old_instance['product_post_id'] ), $required_widget_props );
396
				}
397
398
				if ( $new_instance['form_action'] == 'clear' ) {
399
					return array_merge( $this->defaults(), $required_widget_props );
400
				}
401
			}
402
403
			$form_product_image_id = (int) $new_instance['form_product_image_id'];
404
405
			$form_product_email = ! empty( $new_instance['form_product_email'] )
406
				? sanitize_text_field( $new_instance['form_product_email'] )
407
				: $this->defaults()['form_product_email'];
408
409
			return array_merge( $required_widget_props, array(
410
				'form_product_id' => ( int ) $new_instance['form_product_id'],
411
				'form_product_title' => sanitize_text_field( $new_instance['form_product_title'] ),
412
				'form_product_description' => sanitize_text_field( $new_instance['form_product_description'] ),
413
				'form_product_image_id' => $form_product_image_id,
414
				'form_product_image_src' => wp_get_attachment_image_url( $form_product_image_id, 'thumbnail' ),
415
				'form_product_currency' => sanitize_text_field( $new_instance['form_product_currency'] ),
416
				'form_product_price' => sanitize_text_field( $new_instance['form_product_price'] ),
417
				'form_product_multiple' => sanitize_text_field( $new_instance['form_product_multiple'] ),
418
				'form_product_email' => $form_product_email,
419
			) );
420
		}
421
422
		/**
423
		 * Back-end widget form.
424
		 *
425
		 * @see WP_Widget::form()
426
		 *
427
		 * @param array $instance Previously saved values from database.
428
		 */
429
		function form( $instance ) {
430
			$instance = wp_parse_args( $instance, $this->defaults() );
431
432
			$product_posts = get_posts( array(
433
				'numberposts' => 100,
434
				'orderby' => 'date',
435
				'post_type' => Jetpack_Simple_Payments::$post_type_product,
0 ignored issues
show
Bug introduced by
The property post_type_product cannot be accessed from this context as it is declared private in class Jetpack_Simple_Payments.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
436
				'post_status' => 'publish',
437
			 ) );
438
439
			require( dirname( __FILE__ ) . '/simple-payments/form.php' );
440
		}
441
	}
442
443
	// Register Jetpack_Simple_Payments_Widget widget.
444
	function register_widget_jetpack_simple_payments() {
445
		if ( ! Jetpack::is_active() ) {
446
			return;
447
		}
448
449
		register_widget( 'Jetpack_Simple_Payments_Widget' );
450
	}
451
	add_action( 'widgets_init', 'register_widget_jetpack_simple_payments' );
452
}
453