Completed
Push — feature/mailchimp-widget ( 4f8c7b...578459 )
by
unknown
08:20
created

update()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 8
nop 2
dl 0
loc 22
rs 8.9457
c 0
b 0
f 0
1
<?php
2
3
use Automattic\Jetpack\Assets;
4
5
use Jetpack_AMP_Support;
6
7
use function Automattic\Jetpack\Extensions\Mailchimp\load_assets as render_block;
8
9
if ( ! class_exists( 'Jetpack_MailChimp_Subscriber_Popup_Widget' ) ) {
10
11
	if ( ! class_exists( 'MailChimp_Subscriber_Popup' ) ) {
12
		include_once JETPACK__PLUGIN_DIR . 'modules/shortcodes/mailchimp.php';
13
	}
14
15
	//register MailChimp Subscriber Popup widget
16
	function jetpack_mailchimp_subscriber_popup_widget_init() {
17
		register_widget( 'Jetpack_MailChimp_Subscriber_Popup_Widget' );
18
	}
19
20
	add_action( 'widgets_init', 'jetpack_mailchimp_subscriber_popup_widget_init' );
21
22
	/**
23
	 * Add a MailChimp subscription form.
24
	 */
25
	class Jetpack_MailChimp_Subscriber_Popup_Widget extends WP_Widget {
26
27
		/**
28
		 * Array contaning the section and fields of the widget form.
29
		 *
30
		 * @var array
31
		 */
32
		protected $form_sections;
33
34
		/**
35
		 * Array contaning the data for the placeholder view.
36
		 *
37
		 * @var array
38
		 */
39
		protected $placeholder_data;
40
41
		/**
42
		 * Constructor
43
		 */
44
		public function __construct() {
45
			parent::__construct(
46
				'widget_mailchimp_subscriber_popup',
47
				/** This filter is documented in modules/widgets/facebook-likebox.php */
48
				apply_filters( 'jetpack_widget_name', __( 'MailChimp Subscriber Popup', 'jetpack' ) ),
49
				array(
50
					'classname'                   => 'widget_mailchimp_subscriber_popup',
51
					'description'                 => __( 'Allows displaying a popup subscription form to visitors.', 'jetpack' ),
52
					'customize_selective_refresh' => true,
53
				)
54
			);
55
56
			add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
57
		}
58
59
		/**
60
		 * Outputs the HTML for this widget.
61
		 *
62
		 * @param array $args     An array of standard parameters for widgets in this theme.
63
		 * @param array $instance An array of settings for this widget instance.
64
		 *
65
		 * @return void Echoes it's output
66
		 **/
67
		public function widget( $args, $instance ) {
68
			$instance = wp_parse_args(
69
				$instance,
70
				array(
0 ignored issues
show
Documentation introduced by
array('code' => '', 'css... => '', 'delay' => '0') is of type array<string,string,{"co...ing","delay":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71
					'code'      => '',
72
					'cssClass'  => '',
73
					'popupMode' => '',
74
					'delay'     => '0',
75
				)
76
			);
77
78
			if ( ! empty( $instance['code'] ) ) {
79
				// Regular expresion that will match maichimp shortcode.
80
				$regex = '(\[mailchimp_subscriber_popup[^\]]+\])';
81
82
				// Check if the shortcode exists.
83
				preg_match( $regex, $instance['code'], $matches );
84
85
				// Process the shortcode only, if exists.
86
				if ( ! empty( $matches[0] ) ) {
87
					echo do_shortcode( $matches[0] );
88
				}
89
			} else {
90
				$instance['interests'] = empty( $instance['interests'] ) ? array() : explode( '_', $instance['interests'] );
91
92
				$is_amp_request = class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request();
93
94
				$popup_mode = 'on' === $instance['popupMode'];
95
				// Use the same outpu as the block.
96
				$output       = render_block( $instance );
97
				$form_classes = 'jetpack-mailchimp-widget-form';
98
99
				if ( ! empty( $instance['cssClass'] ) ) {
100
					$form_classes .= ' ' . $instance['cssClass'];
101
				}
102
103
				if ( $popup_mode ) {
104
105
					if ( $is_amp_request ) {
106
107
						wp_enqueue_style(
108
							'jetpack-mailchimp-popup-style',
109
							Assets::get_file_url_for_environment(
110
								'_inc/build/widgets/mailchimp/css/popup-amp.min.css',
111
								'modules/widgets/mailchimp/css/popup-amp.css'
112
							),
113
							array(),
114
							'20200618'
115
						);
116
117
						$output = sprintf(
118
							'<amp-user-notification class="%2$s-wrapper" id="%2$s" layout="nodisplay">
119
								<div class="%1$s">
120
									<button class="%2$s-close" on="tap:%2$s.dismiss"></button>
121
									%3$s
122
								</div>
123
							</amp-user-notification>',
124
							$form_classes,
125
							'jetpack-mailchimp-widget',
126
							$output
127
						);
128
					} else {
129
130
						wp_enqueue_script(
131
							'jetpack-mailchimp-popup',
132
							Assets::get_file_url_for_environment(
133
								'_inc/build/widgets/mailchimp/js/popup.min.js',
134
								'modules/widgets/mailchimp/js/popup.js'
135
							),
136
							array( 'jquery' ),
137
							'20200615',
138
							true
139
						);
140
141
						wp_localize_script(
142
							'jetpack-mailchimp-popup',
143
							'jetpackMailchimpPopup',
144
							array(
145
								'delay' => $instance['delay'],
146
							)
147
						);
148
149
						wp_enqueue_style(
150
							'jetpack-mailchimp-popup-style',
151
							Assets::get_file_url_for_environment(
152
								'_inc/build/widgets/mailchimp/css/popup.min.css',
153
								'modules/widgets/mailchimp/css/popup.css'
154
							),
155
							array(),
156
							'20200615'
157
						);
158
159
						$output = sprintf(
160
							'<div class="%s" style="display:none;">%s</div>',
161
							$form_classes,
162
							$output
163
						);
164
					}
165
				}
166
				// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
167
				echo $output;
168
			}
169
170
			/** This action is documented in modules/widgets/gravatar-profile.php */
171
			do_action( 'jetpack_stats_extra', 'widget_view', 'mailchimp_subscriber_popup' );
172
		}
173
174
175
		/**
176
		 * Deals with the settings when they are saved by the admin.
177
		 *
178
		 * @param array $new_instance New configuration values.
179
		 * @param array $old_instance Old configuration values.
180
		 *
181
		 * @return array
182
		 */
183
		public function update( $new_instance, $old_instance ) {
184
			$instance = array();
185
186
			if ( empty( $new_instance['code'] ) || ( ! empty( $new_instance['new_form'] ) && 'on' === $new_instance['new_form'] ) ) {
187
				$instance['code'] = '';
188
			}
189
190
			$instance['emailPlaceholder']            = sanitize_text_field( $new_instance['emailPlaceholder'] );
191
			$instance['processingLabel']             = sanitize_text_field( $new_instance['processingLabel'] );
192
			$instance['successLabel']                = sanitize_text_field( $new_instance['successLabel'] );
193
			$instance['errorLabel']                  = sanitize_text_field( $new_instance['errorLabel'] );
194
			$instance['interests']                   = sanitize_text_field( $new_instance['interests'] );
195
			$instance['signupFieldTag']              = sanitize_text_field( $new_instance['signupFieldTag'] );
196
			$instance['signupFieldValue']            = sanitize_text_field( $new_instance['signupFieldValue'] );
197
			$instance['customBackgroundButtonColor'] = sanitize_text_field( $new_instance['customBackgroundButtonColor'] );
198
			$instance['customTextButtonColor']       = sanitize_text_field( $new_instance['customTextButtonColor'] );
199
			$instance['css_class']                   = sanitize_text_field( $new_instance['css_class'] );
200
			$instance['popupMode']                   = ! empty( $new_instance['popupMode'] ) && 'on' === $new_instance['popupMode'] ? 'on' : 'off';
201
			$instance['delay']                       = sanitize_text_field( $new_instance['delay'] );
202
203
			return $instance;
204
		}
205
206
		/**
207
		 * Enqueue the scripts for the widget.
208
		 *
209
		 * @return void
210
		 */
211
		public function enqueue_admin_scripts() {
212
			global $pagenow;
213
214
			if ( 'widgets.php' === $pagenow ) {
215
				wp_enqueue_script(
216
					'jetpack-mailchimp-admin-js',
217
					Assets::get_file_url_for_environment(
218
						'_inc/build/widgets/mailchimp/js/admin.min.js',
219
						'modules/widgets/mailchimp/js/admin.js'
220
					),
221
					array( 'jquery', 'wp-color-picker' ),
222
					'20200607',
223
					true
224
				);
225
226
				wp_enqueue_style(
227
					'jetpack-mailchimp-admin-style',
228
					Assets::get_file_url_for_environment(
229
						'_inc/build/widgets/mailchimp/css/admin.min.css',
230
						'modules/widgets/mailchimp/css/admin.css'
231
					),
232
					array(),
233
					'20200615'
234
				);
235
236
				wp_enqueue_style( 'wp-color-picker' );
237
			}
238
		}
239
240
241
		/**
242
		 * Displays the form for this widget on the Widgets page of the WP Admin area.
243
		 *
244
		 * @param array $instance Instance configuration.
245
		 *
246
		 * @return void
247
		 */
248
		public function form( $instance ) {
249
			$instance = wp_parse_args(
250
				$instance,
251
				array(
0 ignored issues
show
Documentation introduced by
array('code' => '', 'ema...' => '', 'delay' => '') is of type array<string,string,{"co...ing","delay":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
252
					'code'                        => '',
253
					'emailPlaceholder'            => '',
254
					'processingLabel'             => '',
255
					'successLabel'                => '',
256
					'errorLabel'                  => '',
257
					'interests'                   => '',
258
					'signupFieldTag'              => '',
259
					'signupFieldValue'            => '',
260
					'customBackgroundButtonColor' => '',
261
					'customTextButtonColor'       => '',
262
					'cssClass'                    => '',
263
					'popupMode'                   => '',
264
					'delay'                       => '',
265
				)
266
			);
267
268
			$this->form_sections = array(
269
				array(
270
					'title'  => __( 'Text Elements', 'jetpack' ),
271
					'fields' => array(
272
						array(
273
							'title'       => __( 'Email Placeholder', 'jetpack' ),
274
							'id'          => 'jetpack-mailchimp-email',
275
							'placeholder' => __( 'Enter your email', 'jetpack' ),
276
							'default'     => __( 'Enter your email', 'jetpack' ),
277
							'type'        => 'text',
278
							'name'        => esc_attr( $this->get_field_name( 'emailPlaceholder' ) ),
279
							'value'       => esc_html( $instance['emailPlaceholder'] ),
280
						),
281
					),
282
				),
283
284
				array(
285
					'title'  => __( 'Notifications', 'jetpack' ),
286
					'fields' => array(
287
						array(
288
							'title'       => __( 'Processing', 'jetpack' ),
289
							'id'          => 'jetpack-mailchimp-processing-label',
290
							'placeholder' => __( 'Processing', 'jetpack' ),
291
							'default'     => __( 'Processing', 'jetpack' ),
292
							'type'        => 'text',
293
							'name'        => esc_attr( $this->get_field_name( 'processingLabel' ) ),
294
							'value'       => esc_html( $instance['processingLabel'] ),
295
						),
296
297
						array(
298
							'title'       => __( 'Success text', 'jetpack' ),
299
							'id'          => 'jetpack-mailchimp-success-text',
300
							'placeholder' => __( 'Success! You are on the list.', 'jetpack' ),
301
							'default'     => __( 'Success! You are on the list.', 'jetpack' ),
302
							'type'        => 'text',
303
							'name'        => esc_attr( $this->get_field_name( 'successLabel' ) ),
304
							'value'       => esc_html( $instance['successLabel'] ),
305
						),
306
307
						array(
308
							'title'       => __( 'Error text', 'jetpack' ),
309
							'id'          => 'jetpack-mailchimp-error-label',
310
							'placeholder' => __( 'Whoops! There was an error and we could not process your subscription. Please reload the page and try again.', 'jetpack' ),
311
							'default'     => __( 'Whoops! There was an error and we could not process your subscription. Please reload the page and try again.', 'jetpack' ),
312
							'type'        => 'text',
313
							'name'        => esc_attr( $this->get_field_name( 'errorLabel' ) ),
314
							'value'       => esc_html( $instance['errorLabel'] ),
315
						),
316
					),
317
				),
318
319
				array(
320
					'title'         => __( 'Mailchimp Groups', 'jetpack' ),
321
					'fields'        => array(
322
						array(
323
							'type' => 'groups',
324
						),
325
					),
326
					'extra_content' => array(
327
						array(
328
							'text' => __( 'Learn about groups', 'jetpack' ),
329
							'link' => 'https://mailchimp.com/help/send-groups-audience/',
330
							'type' => 'link',
331
						),
332
					),
333
				),
334
335
				array(
336
					'title'         => __( 'Signup Location Tracking', 'jetpack' ),
337
					'fields'        => array(
338
						array(
339
							'title'       => __( 'Signup Field Tag', 'jetpack' ),
340
							'id'          => 'jetpack-mailchimp-signup-tag',
341
							'placeholder' => __( 'SIGNUP', 'jetpack' ),
342
							'default'     => '',
343
							'type'        => 'text',
344
							'name'        => esc_attr( $this->get_field_name( 'signupFieldTag' ) ),
345
							'value'       => esc_html( $instance['signupFieldTag'] ),
346
						),
347
348
						array(
349
							'title'       => __( 'Signup Field Value', 'jetpack' ),
350
							'id'          => 'jetpack-mailchimp-signup-value',
351
							'placeholder' => __( 'website', 'jetpack' ),
352
							'default'     => '',
353
							'type'        => 'text',
354
							'name'        => esc_attr( $this->get_field_name( 'signupFieldValue' ) ),
355
							'value'       => esc_html( $instance['signupFieldValue'] ),
356
						),
357
					),
358
					'extra_content' => array(
359
						array(
360
							'text' => __( 'Learn about signup location tracking(opens in a new tab)', 'jetpack' ),
361
							'link' => 'https://mailchimp.com/help/determine-webpage-signup-location/',
362
							'type' => 'link',
363
						),
364
					),
365
				),
366
367
				array(
368
					'title'         => __( 'Mailchimp Connection', 'jetpack' ),
369
					'extra_content' => array(
370
						array(
371
							'text' => __( 'Manage Connection', 'jetpack' ),
372
							'link' => 'connect_url',
373
							'type' => 'link',
374
						),
375
					),
376
				),
377
378
				array(
379
					'title'  => __( 'Button Color Settings', 'jetpack' ),
380
					'fields' => array(
381
						array(
382
							'id'      => 'jetpack-mailchimp-button-color',
383
							'type'    => 'color',
384
							'value'   => esc_html( $instance['customBackgroundButtonColor'] ),
385
							'default' => '',
386
							'name'    => esc_attr( $this->get_field_name( 'customBackgroundButtonColor' ) ),
387
							'label'   => __( 'Button Color', 'jetpack' ),
388
						),
389
390
						array(
391
							'id'      => 'jetpack-mailchimp-button-text-color',
392
							'type'    => 'color',
393
							'value'   => esc_html( $instance['customTextButtonColor'] ),
394
							'default' => '',
395
							'name'    => esc_attr( $this->get_field_name( 'customTextButtonColor' ) ),
396
							'label'   => __( 'Button Text Color', 'jetpack' ),
397
						),
398
					),
399
				),
400
401
				array(
402
					'title'  => __( 'Advanced', 'jetpack' ),
403
					'fields' => array(
404
						array(
405
							'title'       => __( 'Additional CSS class(es)', 'jetpack' ),
406
							'id'          => 'jetpack-mailchimp-css-class',
407
							'placeholder' => '',
408
							'default'     => '',
409
							'type'        => 'text',
410
							'name'        => esc_attr( $this->get_field_name( 'cssClass' ) ),
411
							'value'       => esc_html( $instance['cssClass'] ),
412
						),
413
414
						array(
415
							'title' => __( 'Activate popup mode', 'jetpack' ),
416
							'type'  => 'checkbox',
417
							'name'  => esc_attr( $this->get_field_name( 'popupMode' ) ),
418
							'value' => esc_html( $instance['popupMode'] ),
419
						),
420
421
						array(
422
							'title'       => __( 'Popup delay in miliseconds (only non-AMP)', 'jetpack' ),
423
							'type'        => 'number',
424
							'name'        => esc_attr( $this->get_field_name( 'delay' ) ),
425
							'value'       => esc_html( $instance['delay'] ),
426
							'default'     => '0',
427
							'placeholder' => '',
428
						),
429
					),
430
				),
431
			);
432
433
			$this->placeholder_data = array(
434
				'instructions'    => __( 'You need to connect your Mailchimp account and choose a list in order to start collecting Email subscribers.', 'jetpack' ),
435
				'setupButtonText' => __( 'Set up Mailchimp form', 'jetpack' ),
436
				'recheckText'     => __( 'Re-check Connection', 'jetpack' ),
437
			);
438
439
			if ( ! empty( $instance['code'] ) ) {
440
				?>
441
					<p class="mailchimp-code">
442
					<label for="<?php echo esc_attr( $this->get_field_id( 'code' ) ); ?>">
443
						<?php
444
							/* translators: %1$s is replaced mailchimp suppoert link */
445
							echo sprintf( __( 'Code: <a href="%s" target="_blank">( ? )</a>', 'jetpack' ), 'https://en.support.wordpress.com/mailchimp/' );
446
						?>
447
					</label>
448
					<textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'code' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'code' ) ); ?>" rows="3"><?php echo esc_textarea( $instance['code'] ); ?></textarea>
449
				</p>
450
				<p class="jetpack-mailchimp-new-form-wrapper">
451
					<input type="checkbox" id="jetpack-mailchimp-new-form" name="<?php echo esc_attr( $this->get_field_name( 'new_form' ) ); ?>" > <?php echo esc_html__( 'Check this if you want to use the new form for this widget (the code in the box above will be deleted)', 'jetpack' ); ?>
452
				</p>
453
				<?php
454
			}
455
456
			?>
457
			<div class="mailchimp-widget-jetpack-form-wrapper"></div>
458
			<script>
459
460
				var mailchimpAdmin = {
461
					formData: '<?php echo wp_json_encode( $this->form_sections ); ?>',
462
					placeholderData: '<?php echo wp_json_encode( $this->placeholder_data ); ?>',
463
					oldForm: <?php echo ! empty( $instance['code'] ) ? 'true' : 'false'; ?>,
464
					interests: '<?php echo esc_html( $instance['interests'] ); ?>',
465
					interestsFieldName: '<?php echo esc_attr( $this->get_field_name( 'interests' ) ); ?>'
466
				}
467
				jQuery( window ).trigger( 'jetpack_mailchimp_load_form' );
468
			</script>
469
			<?php
470
		}
471
472
	}
473
474
}
475