Completed
Push — feature/mailchimp-widget ( ecaa76...6e0de4 )
by
unknown
08:05
created

__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 124

Duplication

Lines 0
Ratio 0 %

Importance

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