Completed
Push — feature/mailchimp-widget ( 9069ca...c9a9c5 )
by
unknown
09:19 queued 02:24
created

Jetpack_MailChimp_Subscriber_Popup_Widget::form()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 214

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 1
dl 0
loc 214
rs 8
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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'] = 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
				$hidden_style = '';
96
				if ( $popup_mode && ! $is_amp_request ) {
97
					$hidden_style = 'style=display:none;';
98
99
					wp_enqueue_script(
100
						'jetpack-mailchimp-popup',
101
						Assets::get_file_url_for_environment(
102
							'_inc/build/widgets/mailchimp/js/popup.min.js',
103
							'modules/widgets/mailchimp/js/popup.js'
104
						),
105
						array( 'jquery' ),
106
						'20200615',
107
						true
108
					);
109
110
					wp_localize_script(
111
						'jetpack-mailchimp-popup',
112
						'jetpackMailchimpPopup',
113
						array(
114
							'delay' => $instance['delay'],
115
						)
116
					);
117
118
					wp_enqueue_style(
119
						'jetpack-mailchimp-popup-style',
120
						Assets::get_file_url_for_environment(
121
							'_inc/build/widgets/mailchimp/css/popup.min.css',
122
							'modules/widgets/mailchimp/css/popup.css'
123
						),
124
						array(),
125
						'20200615'
126
					);
127
				}
128
129
				echo sprintf(
130
					'<div class="%s" %s >%s</div>',
131
					'jetpack_mailchimp_widget_form',
132
					esc_html( $hidden_style ),
133
					// Use the same ouput as the mailchimp block.
134
					// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
135
					render_block( $instance )
136
				);
137
			}
138
139
			/** This action is documented in modules/widgets/gravatar-profile.php */
140
			do_action( 'jetpack_stats_extra', 'widget_view', 'mailchimp_subscriber_popup' );
141
		}
142
143
144
		/**
145
		 * Deals with the settings when they are saved by the admin.
146
		 *
147
		 * @param array $new_instance New configuration values.
148
		 * @param array $old_instance Old configuration values.
149
		 *
150
		 * @return array
151
		 */
152
		public function update( $new_instance, $old_instance ) {
153
			$instance = array();
154
155
			if ( empty( $new_instance['code'] ) || ( ! empty( $new_instance['new_form'] ) && 'on' === $new_instance['new_form'] ) ) {
156
				$instance['code'] = '';
157
			}
158
159
			$instance['emailPlaceholder']            = sanitize_text_field( $new_instance['emailPlaceholder'] );
160
			$instance['processingLabel']             = sanitize_text_field( $new_instance['processingLabel'] );
161
			$instance['successLabel']                = sanitize_text_field( $new_instance['successLabel'] );
162
			$instance['errorLabel']                  = sanitize_text_field( $new_instance['errorLabel'] );
163
			$instance['interests']                   = sanitize_text_field( $new_instance['interests'] );
164
			$instance['signupFieldTag']              = sanitize_text_field( $new_instance['signupFieldTag'] );
165
			$instance['signupFieldValue']            = sanitize_text_field( $new_instance['signupFieldValue'] );
166
			$instance['customBackgroundButtonColor'] = sanitize_text_field( $new_instance['customBackgroundButtonColor'] );
167
			$instance['customTextButtonColor']       = sanitize_text_field( $new_instance['customTextButtonColor'] );
168
			$instance['css_class']                   = sanitize_text_field( $new_instance['css_class'] );
169
			$instance['popupMode']                   = ! empty( $new_instance['popupMode'] ) && 'on' === $new_instance['popupMode'] ? 'on' : 'off';
170
			$instance['delay']                       = sanitize_text_field( $new_instance['delay'] );
171
172
			return $instance;
173
		}
174
175
		/**
176
		 * Enqueue the scripts for the widget.
177
		 *
178
		 * @return void
179
		 */
180
		public function enqueue_admin_scripts() {
181
			global $pagenow;
182
183 View Code Duplication
			if ( 'widgets.php' === $pagenow ) {
184
				wp_enqueue_script(
185
					'jetpack-mailchimp-admin',
186
					Assets::get_file_url_for_environment(
187
						'_inc/build/widgets/mailchimp/js/admin.min.js',
188
						'modules/widgets/mailchimp/js/admin.js'
189
					),
190
					array( 'jquery', 'wp-color-picker' ),
191
					'20200607',
192
					true
193
				);
194
195
				wp_enqueue_style( 'wp-color-picker' );
196
			}
197
		}
198
199
200
		/**
201
		 * Displays the form for this widget on the Widgets page of the WP Admin area.
202
		 *
203
		 * @param array $instance Instance configuration.
204
		 *
205
		 * @return void
206
		 */
207
		public function form( $instance ) {
208
			$instance = wp_parse_args(
209
				$instance,
210
				array(
0 ignored issues
show
Documentation introduced by
array('code' => '', 'ema... '', 'css_class' => '') is of type array<string,string,{"co...,"css_class":"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...
211
					'code'              => '',
212
					'email_placeholder' => '',
213
					'processing_text'   => '',
214
					'success_text'      => '',
215
					'error_text'        => '',
216
					'groups'            => '',
217
					'signup_tag'        => '',
218
					'signup_value'      => '',
219
					'button_color'      => '',
220
					'text_color'        => '',
221
					'css_class'         => '',
222
				)
223
			);
224
225
			$this->form_sections = array(
226
				array(
227
					'title'  => __( 'Text Elements', 'jetpack' ),
228
					'fields' => array(
229
						array(
230
							'title'       => __( 'Email Placeholder', 'jetpack' ),
231
							'id'          => 'jetpack_mailchimp_email',
232
							'placeholder' => __( 'Enter your email', 'jetpack' ),
233
							'type'        => 'text',
234
							'name'        => esc_attr( $this->get_field_name( 'emailPlaceholder' ) ),
235
							'value'       => esc_html( $instance['emailPlaceholder'] ),
236
						),
237
					),
238
				),
239
240
				array(
241
					'title'  => __( 'Notifications', 'jetpack' ),
242
					'fields' => array(
243
						array(
244
							'title'       => __( 'Processing', 'jetpack' ),
245
							'id'          => 'jetpack_mailchimp_processing_label',
246
							'placeholder' => __( 'Processing', 'jetpack' ),
247
							'type'        => 'text',
248
							'name'        => esc_attr( $this->get_field_name( 'processingLabel' ) ),
249
							'value'       => esc_html( $instance['processingLabel'] ),
250
						),
251
252
						array(
253
							'title'       => __( 'Success text', 'jetpack' ),
254
							'id'          => 'jetpack_mailchimp_success_text',
255
							'placeholder' => __( 'Success! You are on the list.', 'jetpack' ),
256
							'type'        => 'text',
257
							'name'        => esc_attr( $this->get_field_name( 'successLabel' ) ),
258
							'value'       => esc_html( $instance['successLabel'] ),
259
						),
260
261
						array(
262
							'title'       => __( 'Error text', 'jetpack' ),
263
							'id'          => 'jetpack_mailchimp_error_label',
264
							'placeholder' => __( 'Whoops! There was an error and we could not process your subscription. Please reload the page and try again.', 'jetpack' ),
265
							'type'        => 'text',
266
							'name'        => esc_attr( $this->get_field_name( 'errorLabel' ) ),
267
							'value'       => esc_html( $instance['errorLabel'] ),
268
						),
269
					),
270
				),
271
272
				array(
273
					'title'         => __( 'Mailchimp Groups', 'jetpack' ),
274
					'fields'        => array(
275
						array(
276
							'type' => 'groups',
277
						),
278
					),
279
					'extra_content' => array(
280
						array(
281
							'text' => __( 'Learn about groups', 'jetpack' ),
282
							'link' => 'https://mailchimp.com/help/send-groups-audience/',
283
							'type' => 'link',
284
						),
285
					),
286
				),
287
288
				array(
289
					'title'         => __( 'Signup Location Tracking', 'jetpack' ),
290
					'fields'        => array(
291
						array(
292
							'title'       => __( 'Signup Field Tag', 'jetpack' ),
293
							'id'          => 'jetpack_mailchimp_signup_tag',
294
							'placeholder' => __( 'SIGNUP', 'jetpack' ),
295
							'type'        => 'text',
296
							'name'        => esc_attr( $this->get_field_name( 'signupFieldTag' ) ),
297
							'value'       => esc_html( $instance['signupFieldTag'] ),
298
						),
299
300
						array(
301
							'title'       => __( 'Signup Field Value', 'jetpack' ),
302
							'id'          => 'jetpack_mailchimp_signup_value',
303
							'placeholder' => __( 'website', 'jetpack' ),
304
							'type'        => 'text',
305
							'name'        => esc_attr( $this->get_field_name( 'signupFieldValue' ) ),
306
							'value'       => esc_html( $instance['signupFieldValue'] ),
307
						),
308
					),
309
					'extra_content' => array(
310
						array(
311
							'text' => __( 'Learn about signup location tracking(opens in a new tab)', 'jetpack' ),
312
							'link' => 'https://mailchimp.com/help/determine-webpage-signup-location/',
313
							'type' => 'link',
314
						),
315
					),
316
				),
317
318
				array(
319
					'title'         => __( 'Mailchimp Connection', 'jetpack' ),
320
					'extra_content' => array(
321
						array(
322
							'text' => __( 'Manage Connection', 'jetpack' ),
323
							'link' => 'connect_url',
324
							'type' => 'link',
325
						),
326
					),
327
				),
328
329
				array(
330
					'title'  => __( 'Button Color Settings', 'jetpack' ),
331
					'fields' => array(
332
						array(
333
							'id'      => 'jetpack_mailchimp_button_color',
334
							'type'    => 'color',
335
							'value'   => esc_html( $instance['customBackgroundButtonColor'] ),
336
							'default' => '#cd2653',
337
							'name'    => esc_attr( $this->get_field_name( 'customBackgroundButtonColor' ) ),
338
							'label'   => __( 'Button Color', 'jetpack' ),
339
						),
340
341
						array(
342
							'id'      => 'jetpack_mailchimp_button_text_color',
343
							'type'    => 'color',
344
							'value'   => esc_html( $instance['customTextButtonColor'] ),
345
							'default' => '#ffffff',
346
							'name'    => esc_attr( $this->get_field_name( 'customTextButtonColor' ) ),
347
							'label'   => __( 'Button Text Color', 'jetpack' ),
348
						),
349
					),
350
				),
351
352
				array(
353
					'title'  => __( 'Advanced', 'jetpack' ),
354
					'fields' => array(
355
						array(
356
							'title'       => __( 'Additional CSS class(es)', 'jetpack' ),
357
							'id'          => 'jetpack_mailchimp_css_class',
358
							'placeholder' => '',
359
							'help_text'   => __( 'Separate multiple classes with spaces.', 'jetpack' ),
360
							'type'        => 'text',
361
							'name'        => esc_attr( $this->get_field_name( 'cssClass' ) ),
362
							'value'       => esc_html( $instance['cssClass'] ),
363
						),
364
365
						array(
366
							'title' => __( 'Activate popup mode', 'jetpack' ),
367
							'type'  => 'checkbox',
368
							'name'  => esc_attr( $this->get_field_name( 'popupMode' ) ),
369
							'value' => esc_html( $instance['popupMode'] ),
370
						),
371
372
						array(
373
							'title'       => __( 'Popup delay (miliseconds)', 'jetpack' ),
374
							'type'        => 'number',
375
							'name'        => esc_attr( $this->get_field_name( 'delay' ) ),
376
							'value'       => esc_html( $instance['delay'] ),
377
							'placeholder' => '0',
378
						),
379
					),
380
				),
381
			);
382
383
			$this->placeholder_data = array(
384
				'instructions'    => __( 'You need to connect your Mailchimp account and choose a list in order to start collecting Email subscribers.', 'jetpack' ),
385
				'setupButtonText' => __( 'Set up Mailchimp form', 'jetpack' ),
386
				'recheckText'     => __( 'Re-check Connection', 'jetpack' ),
387
			);
388
389
			if ( ! empty( $instance['code'] ) ) {
390
				?>
391
					<p class="mailchimp_code">
392
					<label for="<?php echo esc_attr( $this->get_field_id( 'code' ) ); ?>">
393
						<?php
394
							/* translators: %1$s is replaced mailchimp suppoert link */
395
							echo sprintf( __( 'Code: <a href="%s" target="_blank">( ? )</a>', 'jetpack' ), 'https://en.support.wordpress.com/mailchimp/' );
396
						?>
397
					</label>
398
					<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>
399
				</p>
400
				<p class="jetpack_mailchimp_new_form_wrapper">
401
					<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' ); ?>
402
				</p>
403
				<?php
404
			}
405
406
			?>
407
			<div class="mailchimp_widget_jetpack_form_wrapper"></div>
408
			<script>
409
410
				var mailchimpAdmin = {
411
					formData: '<?php echo wp_json_encode( $this->form_sections ); ?>',
412
					placeholderData: '<?php echo wp_json_encode( $this->placeholder_data ); ?>',
413
					oldForm: <?php echo ! empty( $instance['code'] ) ? 'true' : 'false'; ?>,
414
					groups: '<?php echo esc_html( $instance['interests'] ); ?>',
415
					groupsFieldName: '<?php echo esc_attr( $this->get_field_name( 'interests' ) ); ?>'
416
				}
417
				jQuery( window ).trigger( 'jetpack_mailchimp_load_form' );
418
			</script>
419
			<?php
420
		}
421
422
	}
423
424
}
425