Completed
Push — feature/mailchimp-widget ( 6e0de4...0fd824 )
by
unknown
07:26
created

Jetpack_MailChimp_Subscriber_Popup_Widget::form()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 152

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 152
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
if ( ! class_exists( 'Jetpack_MailChimp_Subscriber_Popup_Widget' ) ) {
6
7
	if ( ! class_exists( 'MailChimp_Subscriber_Popup' ) ) {
8
		include_once JETPACK__PLUGIN_DIR . 'modules/shortcodes/mailchimp.php';
9
	}
10
11
	//register MailChimp Subscriber Popup widget
12
	function jetpack_mailchimp_subscriber_popup_widget_init() {
13
		register_widget( 'Jetpack_MailChimp_Subscriber_Popup_Widget' );
14
	}
15
16
	add_action( 'widgets_init', 'jetpack_mailchimp_subscriber_popup_widget_init' );
17
18
	/**
19
	 * Add a MailChimp subscription form.
20
	 */
21
	class Jetpack_MailChimp_Subscriber_Popup_Widget extends WP_Widget {
22
23
		/**
24
		 * Array contaning the section and fields of the widget form.
25
		 *
26
		 * @var array
27
		 */
28
		protected $form_sections;
29
30
		/**
31
		 * Array contaning the data for the placeholder view.
32
		 *
33
		 * @var array
34
		 */
35
		protected $placeholder_data;
36
37
		/**
38
		 * Constructor
39
		 */
40
		public function __construct() {
41
			parent::__construct(
42
				'widget_mailchimp_subscriber_popup',
43
				/** This filter is documented in modules/widgets/facebook-likebox.php */
44
				apply_filters( 'jetpack_widget_name', __( 'MailChimp Subscriber Popup', 'jetpack' ) ),
45
				array(
46
					'classname'                   => 'widget_mailchimp_subscriber_popup',
47
					'description'                 => __( 'Allows displaying a popup subscription form to visitors.', 'jetpack' ),
48
					'customize_selective_refresh' => true,
49
				)
50
			);
51
52
			add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
53
		}
54
55
		/**
56
		 * Outputs the HTML for this widget.
57
		 *
58
		 * @param array $args     An array of standard parameters for widgets in this theme.
59
		 * @param array $instance An array of settings for this widget instance.
60
		 *
61
		 * @return void Echoes it's output
62
		 **/
63
		public function widget( $args, $instance ) {
64
			$instance = wp_parse_args( $instance, array( 'code' => '' ) );
0 ignored issues
show
Documentation introduced by
array('code' => '') is of type array<string,string,{"code":"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...
65
66
			// Regular expresion that will match maichimp shortcode.
67
			$regex = '(\[mailchimp_subscriber_popup[^\]]+\])';
68
69
			// Check if the shortcode exists.
70
			preg_match( $regex, $instance['code'], $matches );
71
72
			// Process the shortcode only, if exists.
73
			if ( ! empty( $matches[0] ) ) {
74
				echo do_shortcode( $matches[0] );
75
			}
76
77
			/** This action is documented in modules/widgets/gravatar-profile.php */
78
			do_action( 'jetpack_stats_extra', 'widget_view', 'mailchimp_subscriber_popup' );
79
		}
80
81
82
		/**
83
		 * Deals with the settings when they are saved by the admin.
84
		 *
85
		 * @param array $new_instance New configuration values.
86
		 * @param array $old_instance Old configuration values.
87
		 *
88
		 * @return array
89
		 */
90
		public function update( $new_instance, $old_instance ) {
91
			$instance         = array();
92
			$instance['code'] = MailChimp_Subscriber_Popup::reversal( $new_instance['code'] );
93
94
			return $instance;
95
		}
96
97
		/**
98
		 * Output the mailchimp form.
99
		 *
100
		 * @return void
101
		 */
102
		public function form_data() {
103
			?>
104
			<div class="mailchimp_form">
105
				<div class="section">
106
					<div class="section_toggler">
107
						<span class="section_title"><?php echo esc_html__( 'Text Elements', 'jetpack' ); ?></span>
108
					</div>
109
					<div class="section_content">
110
						<div class="field">
111
							<label for="jetpack_mailchimp_email"><?php echo esc_html__( 'Email Placeholder', 'jetpack' ); ?></label>
112
							<input type="text" placeholder="<?php echo esc_html__( 'Enter your email', 'jetpack' ); ?>" value="" id="jetpack_mailchimp_email">
113
						</div>
114
					</div>
115
				</div>
116
				<div class="section">
117
					<div class="section_toggler">
118
						<span class="section_title"><?php echo esc_html__( 'Notifications', 'jetpack' ); ?></span>
119
					</div>
120
					<div class="section_content">
121
						<div class="field">
122
							<label for="jetpack_mailchimp_processing_text"><?php echo esc_html__( 'Processing', 'jetpack' ); ?></label>
123
							<input type="text" placeholder="<?php echo esc_html__( 'Processing', 'jetpack' ); ?>" value="" id="jetpack_mailchimp_processing_text">
124
						</div>
125
						<div class="field">
126
							<label for="jetpack_mailchimp_success_text"><?php echo esc_html__( 'Success text', 'jetpack' ); ?></label>
127
							<input type="text" placeholder="<?php echo esc_html__( 'Success! You\'re on the list.', 'jetpack' ); ?>" value="" id="jetpack_mailchimp_success_text">
128
						</div>
129
						<div class="field">
130
							<label for="jetpack_mailchimp_error_text"><?php echo esc_html__( 'Error text', 'jetpack' ); ?></label>
131
							<input type="text" placeholder="<?php echo esc_html__( 'Whoops! There was an error and we couldn\'t process your subscription. Please reload the page and try again.', 'jetpack' ); ?>" value="" id="jetpack_mailchimp_error_text">
132
						</div>
133
					</div>
134
				</div>
135
				<div class="section">
136
					<div class="section_toggler">
137
						<span class="section_title"><?php echo esc_html__( 'Mailchimp Groups', 'jetpack' ); ?></span>
138
					</div>
139
					<div class="section_content">
140
						<a href=""><?php echo esc_html__( 'Learn about groups', 'jetpack' ); ?></a>
141
					</div>
142
				</div>
143
				<div class="section">
144
					<div class="section_toggler">
145
						<span class="section_title"><?php echo esc_html__( 'Signup Location Tracking', 'jetpack' ); ?></span>
146
					</div>
147
					<div class="section_content">
148
						<div class="field">
149
							<label for="jetpack_mailchimp_signup_tag"><?php echo esc_html__( 'Signup Field Tag', 'jetpack' ); ?></label>
150
							<input type="text" placeholder="<?php echo esc_html__( 'SIGNUP', 'jetpack' ); ?>" value="" id="jetpack_mailchimp_signup_tag">
151
						</div>
152
						<div class="field">
153
							<label for="jetpack_mailchimp_signup_value"><?php echo esc_html__( 'Signup Field Value', 'jetpack' ); ?></label>
154
							<input type="text" placeholder="<?php echo esc_html__( 'website', 'jetpack' ); ?>" value="" id="jetpack_mailchimp_signup_value">
155
						</div>
156
						<a href=""><?php echo esc_html__( 'Learn about signup location tracking(opens in a new tab)', 'jetpack' ); ?></a>
157
					</div>
158
				</div>
159
				<div class="section">
160
					<div class="section_toggler">
161
						<span class="section_title"><?php echo esc_html__( 'Mailchimp Groups', 'jetpack' ); ?></span>
162
					</div>
163
					<div class="section_content">
164
						<a href=""><?php echo esc_html__( 'Manage Connection', 'jetpack' ); ?></a>
165
					</div>
166
				</div>
167
			</div>
168
			<?php
169
		}
170
171
		/**
172
		 * Enqueue the scripts for the widget.
173
		 *
174
		 * @return void
175
		 */
176
		public function enqueue_admin_scripts() {
177
			global $pagenow;
178
179
			if ( 'widgets.php' === $pagenow ) {
180
				wp_enqueue_script(
181
					'mailchimp-admin',
182
					Assets::get_file_url_for_environment(
183
						'_inc/build/widgets/mailchimp/js/admin.min.js',
184
						'modules/widgets/mailchimp/js/admin.js'
185
					),
186
					array( 'jquery' ),
187
					'20200607',
188
					true
189
				);
190
			}
191
		}
192
193
194
		/**
195
		 * Displays the form for this widget on the Widgets page of the WP Admin area.
196
		 *
197
		 * @param array $instance Instance configuration.
198
		 *
199
		 * @return void
200
		 */
201
		public function form( $instance ) {
202
			$instance = wp_parse_args(
203
				$instance,
204
				array( 'code' => '' )
0 ignored issues
show
Documentation introduced by
array('code' => '') is of type array<string,string,{"code":"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...
205
			);
206
207
			$this->form_sections = array(
208
				array(
209
					'title'  => esc_html__( 'Text Elements', 'jetpack' ),
210
					'fields' => array(
211
						array(
212
							'title'       => esc_html__( 'Email Placeholder', 'jetpack' ),
213
							'id'          => 'jetpack_mailchimp_email',
214
							'placeholder' => esc_html__( 'Enter your email', 'jetpack' ),
215
							'type'        => 'text',
216
						),
217
					),
218
				),
219
220
				array(
221
					'title'  => esc_html__( 'Notifications', 'jetpack' ),
222
					'fields' => array(
223
						array(
224
							'title'       => esc_html__( 'Processing', 'jetpack' ),
225
							'id'          => 'jetpack_mailchimp_processing_text',
226
							'placeholder' => esc_html__( 'Processing', 'jetpack' ),
227
							'type'        => 'text',
228
						),
229
230
						array(
231
							'title'       => esc_html__( 'Success text', 'jetpack' ),
232
							'id'          => 'jetpack_mailchimp_success_text',
233
							'placeholder' => esc_html__( 'Success! You\'re on the list.', 'jetpack' ),
234
							'type'        => 'text',
235
						),
236
237
						array(
238
							'title'       => esc_html__( 'Error text', 'jetpack' ),
239
							'id'          => 'jetpack_mailchimp_error_text',
240
							'placeholder' => esc_html__( 'Whoops! There was an error and we couldn\'t process your subscription. Please reload the page and try again.', 'jetpack' ),
241
							'type'        => 'text',
242
						),
243
					),
244
				),
245
246
				array(
247
					'title'  => esc_html__( 'Mailchimp Groups', 'jetpack' ),
248
					'fields' => array(
249
						array(
250
							'type' => 'groups',
251
						),
252
					),
253
				),
254
255
				array(
256
					'title'         => esc_html__( 'Signup Location Tracking', 'jetpack' ),
257
					'fields'        => array(
258
						array(
259
							'title'       => esc_html__( 'Signup Field Tag', 'jetpack' ),
260
							'id'          => 'jetpack_mailchimp_signup_tag',
261
							'placeholder' => esc_html__( 'SIGNUP', 'jetpack' ),
262
							'type'        => 'text',
263
						),
264
265
						array(
266
							'title'       => esc_html__( 'Signup Field Value', 'jetpack' ),
267
							'id'          => 'jetpack_mailchimp_signup_value',
268
							'placeholder' => esc_html__( 'website', 'jetpack' ),
269
							'type'        => 'text',
270
						),
271
					),
272
					'extra_content' => array(
273
						'text' => esc_html__( 'Learn about signup location tracking(opens in a new tab)', 'jetpack' ),
274
						'link' => 'https://mailchimp.com/help/determine-webpage-signup-location/',
275
						'type' => 'link',
276
					),
277
				),
278
279
				array(
280
					'title'         => esc_html__( 'Mailchimp Groups', 'jetpack' ),
281
					'extra_content' => array(
282
						'text' => esc_html__( 'Manage Connection', 'jetpack' ),
283
						'link' => 'https://jetpack.com/redirect?source=calypso-marketing-connections&site=[site_url]&query=mailchimp',
284
						'type' => 'link',
285
					),
286
				),
287
288
				array(
289
					'title'  => esc_html__( 'Button Color Settings', 'jetpack' ),
290
					'fields' => array(
291
						array(
292
							'id'   => 'jetpack_mailchimp_button_color',
293
							'type' => 'color',
294
						),
295
296
						array(
297
							'id'   => 'jetpack_mailchimp_button_text_color',
298
							'type' => 'color',
299
						),
300
					),
301
				),
302
303
				array(
304
					'title'  => esc_html__( 'Advanced', 'jetpack' ),
305
					'fields' => array(
306
						array(
307
							'title'       => esc_html__( 'Additional CSS class(es)', 'jetpack' ),
308
							'id'          => 'jetpack_mailchimp_css_class',
309
							'placeholder' => '',
310
							'help_text'   => esc_html__( 'Separate multiple classes with spaces.', 'jetpack' ),
311
							'type'        => 'text',
312
						),
313
					),
314
				),
315
			);
316
317
			$this->placeholder_data = array(
318
				'instructions'    => esc_html__( 'You need to connect your Mailchimp account and choose a list in order to start collecting Email subscribers.', 'jetpack' ),
319
				'setupButtonText' => esc_html__( 'Set up Mailchimp form', 'jetpack' ),
320
				'recheckText'     => esc_html__( 'Re-check Connection', 'jetpack' ),
321
			);
322
323
			wp_localize_script(
324
				'mailchimp-admin',
325
				'mailchimpAdmin',
326
				array(
327
					'formSections'    => $this->form_sections,
328
					'placeholderData' => $this->placeholder_data,
329
				)
330
			);
331
332
			if ( empty( $instance['code'] ) ) {
333
				?>
334
					<div class="mailchimp_widget_jetpack_form_wrapper"></div>
335
				<?php
336
				return;
337
			}
338
339
			?>
340
341
			<p class="mailchimp_code">
342
				<label for="<?php echo esc_attr( $this->get_field_id( 'code' ) ); ?>">
343
					<?php printf( __( 'Code: <a href="%s" target="_blank">( ? )</a>', 'jetpack' ), 'https://en.support.wordpress.com/mailchimp/' ); ?>
344
				</label>
345
				<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>
346
			</p>
347
			<p>
348
				<input type="checkbox" id="jetpack_mailchimp_new_form" name="<?php echo esc_attr( $this->get_field_name( 'new_form' ) ); ?>" > <?php echo esc_html__( 'Check if you want to use the new form for this widget (the code in the box above will be deleted)', 'jetpack' ); ?>
349
			</p>
350
			<div class="mailchimp_widget_jetpack_form_wrapper"></div>
351
			<?php
352
		}
353
354
	}
355
356
}
357