Completed
Push — fix/sync-package-callables ( 493293...c74f87 )
by
unknown
204:35 queued 193:37
created

)   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 1
dl 0
loc 19
rs 9.3222
c 0
b 0
f 0
1
<?php
2
3
class Jetpack_Subscriptions_Widget extends WP_Widget {
4
	static $instance_count = 0;
5
	/**
6
	 * @var array When printing the submit button, what tags are allowed
7
	 */
8
	public static $allowed_html_tags_for_submit_button = array(
9
		'br'     => array(),
10
		's'      => array(),
11
		'strong' => array(),
12
		'em'     => array(),
13
	);
14
15
	/**
16
	 * Use this variable when printing the message after submitting an email in subscription widgets
17
	 *
18
	 * @var array what tags are allowed
19
	 */
20
	public static $allowed_html_tags_for_message = array(
21
		'a'  => array(
22
			'href'   => array(),
23
			'title'  => array(),
24
			'rel'    => array(),
25
			'target' => array(),
26
		),
27
		'br' => array(),
28
	);
29
30
	function __construct() {
31
		$widget_ops = array(
32
			'classname'                   => 'widget_blog_subscription jetpack_subscription_widget',
33
			'description'                 => __( 'Add an email signup form to allow people to subscribe to your blog.', 'jetpack' ),
34
			'customize_selective_refresh' => true,
35
		);
36
37
		$name = self::is_jetpack() ?
38
			/** This filter is documented in modules/widgets/facebook-likebox.php */
39
			apply_filters( 'jetpack_widget_name', __( 'Blog Subscriptions', 'jetpack' ) ) :
40
			__( 'Follow Blog', 'jetpack' );
41
42
		parent::__construct(
43
			'blog_subscription',
44
			$name,
45
			$widget_ops
46
		);
47
48
		if ( self::is_jetpack() &&
49
		     (
50
			     is_active_widget( false, false, $this->id_base ) ||
51
			     is_active_widget( false, false, 'monster' ) ||
52
			     is_customize_preview()
53
		     )
54
		) {
55
			add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
56
		}
57
	}
58
59
	/**
60
	 * Enqueue the form's CSS.
61
	 *
62
	 * @since 4.5.0
63
	 */
64
	function enqueue_style() {
65
		wp_register_style(
66
			'jetpack-subscriptions',
67
			plugins_url( 'subscriptions.css', __FILE__ ),
68
			array(),
69
			JETPACK__VERSION
70
		);
71
		wp_enqueue_style( 'jetpack-subscriptions' );
72
	}
73
74
	/**
75
	 * Renders a full widget either within the context of WordPress widget, or in response to a shortcode.
76
	 *
77
	 * @param array $args Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'.
78
	 * @param array $instance The settings for the particular instance of the widget.
79
	 */
80
	function widget( $args, $instance ) {
81
		if ( self::is_jetpack() &&
82
		     /** This filter is documented in modules/contact-form/grunion-contact-form.php */
83
		     false === apply_filters( 'jetpack_auto_fill_logged_in_user', false )
84
		) {
85
			$subscribe_email = '';
86
		} else {
87
			$current_user = wp_get_current_user();
88
			if ( ! empty( $current_user->user_email ) ) {
89
				$subscribe_email = esc_attr( $current_user->user_email );
90
			} else {
91
				$subscribe_email = '';
92
			}
93
		}
94
95
		$stats_action = self::is_jetpack() ? 'jetpack_subscriptions' : 'follow_blog';
96
		/** This action is documented in modules/widgets/gravatar-profile.php */
97
		do_action( 'jetpack_stats_extra', 'widget_view', $stats_action );
98
99
		$after_widget  = isset( $args['after_widget'] ) ? $args['after_widget'] : '';
100
		$before_widget = isset( $args['before_widget'] ) ? $args['before_widget'] : '';
101
		$instance      = wp_parse_args( (array) $instance, $this->defaults() );
0 ignored issues
show
Documentation introduced by
$this->defaults() is of type array, 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...
102
103
		echo $before_widget;
104
105
		Jetpack_Subscriptions_Widget::$instance_count ++;
106
107
		self::render_widget_title( $args, $instance );
108
109
		self::render_widget_status_messages( $instance );
110
111
		self::render_widget_subscription_form( $args, $instance, $subscribe_email );
112
113
		echo "\n" . $after_widget;
114
	}
115
116
	/**
117
	 * Prints the widget's title. If show_only_email_and_button is true, we will not show a title.
118
	 *
119
	 * @param array $args Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'.
120
	 * @param array $instance The settings for the particular instance of the widget.
121
	 */
122
	static function render_widget_title( $args, $instance ) {
123
		$show_only_email_and_button = $instance['show_only_email_and_button'];
124
		$before_title               = isset( $args['before_title'] ) ? $args['before_title'] : '';
125
		$after_title                = isset( $args['after_title'] ) ? $args['after_title'] : '';
126
		if ( self::is_wpcom() && ! $show_only_email_and_button ) {
127
			if ( self::is_current_user_subscribed() ) {
128 View Code Duplication
				if ( ! empty( $instance['title_following'] ) ) {
129
					echo $before_title . '<label for="subscribe-field' . ( Jetpack_Subscriptions_Widget::$instance_count > 1 ? '-' . Jetpack_Subscriptions_Widget::$instance_count : '' ) . '">' . esc_attr( $instance['title_following'] ) . '</label>' . $after_title . "\n";
130
				}
131 View Code Duplication
			} else {
132
				if ( ! empty( $instance['title'] ) ) {
133
					echo $before_title . '<label for="subscribe-field' . ( Jetpack_Subscriptions_Widget::$instance_count > 1 ? '-' . Jetpack_Subscriptions_Widget::$instance_count : '' ) . '">' . esc_attr( $instance['title'] ) . '</label>' . $after_title . "\n";
134
				}
135
			}
136
		}
137
138
		if ( self::is_jetpack() && empty( $instance['show_only_email_and_button'] ) ) {
139
			echo $args['before_title'] . esc_attr( $instance['title'] ) . $args['after_title'] . "\n";
140
		}
141
	}
142
143
	/**
144
	 * Prints the subscription block's status messages after someone has attempted to subscribe.
145
	 * Either a success message or an error message.
146
	 *
147
	 * @param array $instance The settings for the particular instance of the widget.
148
	 */
149
	static function render_widget_status_messages( $instance ) {
150
		if ( self::is_jetpack() && isset( $_GET['subscribe'] ) ) {
151
			$success_message   = isset( $instance['success_message'] ) ? stripslashes( $instance['success_message'] ) : '';
152
			$subscribers_total = self::fetch_subscriber_count();
153
			switch ( $_GET['subscribe'] ) :
154
				case 'invalid_email' : ?>
155
                    <p class="error"><?php esc_html_e( 'The email you entered was invalid. Please check and try again.', 'jetpack' ); ?></p>
156
					<?php break;
157
				case 'opted_out' : ?>
158
                    <p class="error"><?php printf( __( 'The email address has opted out of subscription emails. <br /> You can manage your preferences at <a href="%1$s" title="%2$s" target="_blank">subscribe.wordpress.com</a>', 'jetpack' ),
159
							'https://subscribe.wordpress.com/',
160
							__( 'Manage your email preferences.', 'jetpack' )
161
						); ?></p>
162
					<?php break;
163
				case 'already' : ?>
164
                    <p class="error"><?php printf( __( 'You have already subscribed to this site. Please check your inbox. <br /> You can manage your preferences at <a href="%1$s" title="%2$s" target="_blank">subscribe.wordpress.com</a>', 'jetpack' ),
165
							'https://subscribe.wordpress.com/',
166
							__( 'Manage your email preferences.', 'jetpack' )
167
						); ?></p>
168
					<?php break;
169 View Code Duplication
				case 'many_pending_subs':
170
					?>
171
					<p class="error">
172
						<?php
173
						printf(
174
							wp_kses(
175
								/* translators: 1: Link to Subscription Management page https://subscribe.wordpress.com/, 2: Description of this link */
176
								__( 'You already have several pending email subscriptions. <br /> Approve or delete a few subscriptions at <a href="%1$s" title="%2$s" target="_blank" rel="noopener noreferrer">subscribe.wordpress.com</a> before continuing.', 'jetpack' ),
177
								self::$allowed_html_tags_for_message
178
							),
179
							'https://subscribe.wordpress.com/',
180
							esc_attr__( 'Manage your email preferences.', 'jetpack' )
181
						);
182
						?>
183
					</p>
184
					<?php break;
185 View Code Duplication
				case 'pending':
186
					?>
187
					<p class="error">
188
						<?php
189
						printf(
190
							wp_kses(
191
								/* translators: 1: Link to Subscription Management page https://subscribe.wordpress.com/, 2: Description of this link */
192
								__( 'You subscribed this site before but you have not clicked the confirmation link yet. Please check your inbox. <br /> Otherwise, you can manage your preferences at <a href="%1$s" title="%2$s" target="_blank" rel="noopener noreferrer">subscribe.wordpress.com</a>.', 'jetpack' ),
193
								self::$allowed_html_tags_for_message
194
							),
195
							'https://subscribe.wordpress.com/',
196
							esc_attr__( 'Manage your email preferences.', 'jetpack' )
197
						);
198
						?>
199
					</p>
200
					<?php
201
					break;
202
				case 'success' : ?>
203
                    <div class="success"><?php echo wpautop( str_replace( '[total-subscribers]', number_format_i18n( $subscribers_total['value'] ), $success_message ) ); ?></div>
204
					<?php break;
205
				default : ?>
206
                    <p class="error"><?php esc_html_e( 'There was an error when subscribing. Please try again.', 'jetpack' ); ?></p>
207
					<?php break;
208
			endswitch;
209
		}
210
211
		if ( self::is_wpcom() && self::wpcom_has_status_message() ) {
212
			global $themecolors;
213
			switch ( $_GET['blogsub'] ) {
214 View Code Duplication
				case 'confirming':
215
					echo "<div style='background-color: #{$themecolors['bg']}; border: 1px solid #{$themecolors['border']}; color: #{$themecolors['text']}; padding-left: 5px; padding-right: 5px; margin-bottom: 10px;'>";
216
					_e( 'Thanks for subscribing! You&rsquo;ll get an email with a link to confirm your subscription. If you don&rsquo;t get it, please <a href="https://en.support.wordpress.com/contact/">contact us</a>.' );
217
					echo "</div>";
218
					break;
219 View Code Duplication
				case 'blocked':
220
					echo "<div style='background-color: #{$themecolors['bg']}; border: 1px solid #{$themecolors['border']}; color: #{$themecolors['text']}; padding-left: 5px; padding-right: 5px; margin-bottom: 10px;'>";
221
					_e( 'Subscriptions have been blocked for this email address.' );
222
					echo "</div>";
223
					break;
224 View Code Duplication
				case 'flooded':
225
					echo "<div style='background-color: #{$themecolors['bg']}; border: 1px solid #{$themecolors['border']}; color: #{$themecolors['text']}; padding-left: 5px; padding-right: 5px; margin-bottom: 10px;'>";
226
					_e( 'You already have several pending email subscriptions. Approve or delete a few through your <a href="https://subscribe.wordpress.com/">Subscription Manager</a> before attempting to subscribe to more blogs.' );
227
					echo "</div>";
228
					break;
229
				case 'spammed':
230
					echo "<div style='background-color: #{$themecolors['bg']}; border: 1px solid #{$themecolors['border']}; color: #{$themecolors['text']}; padding-left: 5px; padding-right: 5px; margin-bottom: 10px;'>";
231
					echo wp_kses_post( sprintf( __( 'Because there are many pending subscriptions for this email address, we have blocked the subscription. Please <a href="%s">activate or delete</a> pending subscriptions before attempting to subscribe.' ), 'https://subscribe.wordpress.com/' ) );
232
					echo "</div>";
233
					break;
234 View Code Duplication
				case 'subscribed':
235
					echo "<div style='background-color: #{$themecolors['bg']}; border: 1px solid #{$themecolors['border']}; color: #{$themecolors['text']}; padding-left: 5px; padding-right: 5px; margin-bottom: 10px;'>";
236
					_e( 'You&rsquo;re already subscribed to this site.' );
237
					echo "</div>";
238
					break;
239 View Code Duplication
				case 'pending':
240
					echo "<div style='background-color: #{$themecolors['bg']}; border: 1px solid #{$themecolors['border']}; color: #{$themecolors['text']}; padding-left: 5px; padding-right: 5px; margin-bottom: 10px;'>";
241
					_e( 'You have a pending subscription already; we just sent you another email. Click the link or <a href="https://en.support.wordpress.com/contact/">contact us</a> if you don&rsquo;t receive it.' );
242
					echo "</div>";
243
					break;
244 View Code Duplication
				case 'confirmed':
245
					echo "<div style='background-color: #{$themecolors['bg']}; border: 1px solid #{$themecolors['border']}; color: #{$themecolors['text']}; padding-left: 5px; padding-right: 5px; margin-bottom: 10px;'>";
246
					_e( 'Congrats, you&rsquo;re subscribed! You&rsquo;ll get an email with the details of your subscription and an unsubscribe link.' );
247
					echo "</div>";
248
					break;
249
			}
250
		}
251
	}
252
253
	/**
254
	 * Renders a form allowing folks to subscribe to the blog.
255
	 *
256
	 * @param array $args Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'.
257
	 * @param array $instance The settings for the particular instance of the widget.
258
	 * @param string $subscribe_email The email to use to prefill the form.
259
	 */
260
	static function render_widget_subscription_form( $args, $instance, $subscribe_email ) {
261
		$show_only_email_and_button = $instance['show_only_email_and_button'];
262
		$show_subscribers_total     = (bool) $instance['show_subscribers_total'];
263
		$subscribe_text             = empty( $instance['show_only_email_and_button'] ) ?
264
			stripslashes( $instance['subscribe_text'] ) :
265
			false;
266
		$referer                    = ( is_ssl() ? 'https' : 'http' ) . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
267
		$source                     = 'widget';
268
		$widget_id                  = esc_attr( ! empty( $args['widget_id'] ) ? esc_attr( $args['widget_id'] ) : mt_rand( 450, 550 ) );
269
		$subscribe_button           = ! empty( $instance['submit_button_text'] ) ? $instance['submit_button_text'] : $instance['subscribe_button'];
270
		$subscribers_total          = self::fetch_subscriber_count();
271
		$subscribe_placeholder      = isset( $instance['subscribe_placeholder'] ) ? stripslashes( $instance['subscribe_placeholder'] ) : '';
272
		$submit_button_classes      = isset( $instance['submit_button_classes'] ) ? $instance['submit_button_classes'] : '';
273
		$submit_button_styles       = isset( $instance['submit_button_styles'] ) ? $instance['submit_button_styles'] : '';
274
		$email_field_classes        = isset( $instance['email_field_classes'] ) ? $instance['email_field_classes'] : '';
275
		$email_field_styles         = isset( $instance['email_field_styles'] ) ? $instance['email_field_styles'] : '';
276
277
		if ( self::is_wpcom() && ! self::wpcom_has_status_message() ) {
278
			global $current_blog;
279
280
			$url     = defined( 'SUBSCRIBE_BLOG_URL' ) ? SUBSCRIBE_BLOG_URL : '';
281
			$form_id = 'subscribe-blog' . self::$instance_count > 1
282
				? '-' . self::$instance_count
283
				: '';
284
			?>
285
			<form
286
				action="<?php echo esc_url( $url ); ?>"
287
				method="post"
288
				accept-charset="utf-8"
289
				id="<?php echo esc_attr( $form_id ); ?>"
290
			>
291
				<?php
292
				if ( ! $show_only_email_and_button ) {
293
					// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
294
					echo wpautop( $subscribe_text );
295
				}
296
				if ( $show_subscribers_total && $subscribers_total ) {
297
					?>
298
					<div class="jetpack-subscribe-count">
299
						<p>
300
						<?php
301
						/* translators: %s: number of folks following the blog */
302
						echo esc_html( sprintf( _n( 'Join %s other follower', 'Join %s other followers', $subscribers_total, 'jetpack' ), number_format_i18n( $subscribers_total ) ) );
303
						?>
304
						</p>
305
					</div>
306
					<?php
307
				}
308
				$email_field_id  = 'subscribe-field';
309
				$email_field_id .= self::$instance_count > 1
310
					? '-' . self::$instance_count
311
					: '';
312
				$label_field_id  = $email_field_id . '-label';
313
				?>
314
				<p id="subscribe-email">
315
					<label
316
						id="<?php echo esc_attr( $label_field_id ); ?>"
317
						for="<?php echo esc_attr( $email_field_id ); ?>"
318
						class="screen-reader-text"
319
					>
320
						<?php echo esc_html__( 'Email Address:', 'jetpack' ); ?>
321
					</label>
322
323
					<?php
324
					printf(
325
						'<input
326
							type="text"
327
							name="email"
328
							%1$s
329
							style="%2$s"
330
							placeholder="%3$s"
331
							value=""
332
							id="%4$s"
333
						/>',
334
						( ! empty( $email_field_classes )
335
							? 'class="' . esc_attr( $email_field_classes ) . '"'
336
							: ''
337
						),
338
						( ! empty( $email_field_styles )
339
							? esc_attr( $email_field_styles )
340
							: 'width: 95%; padding: 1px 10px'
341
						),
342
						esc_attr__( 'Enter your email address', 'jetpack' ),
343
						esc_attr( $email_field_id )
344
					);
345
					?>
346
				</p>
347
348
				<p id="subscribe-submit">
349
                    <input type="hidden" name="action" value="subscribe"/>
350
                    <input type="hidden" name="blog_id" value="<?php echo (int) $current_blog->blog_id; ?>"/>
351
                    <input type="hidden" name="source" value="<?php echo esc_url( $referer ); ?>"/>
352
                    <input type="hidden" name="sub-type" value="<?php echo esc_attr( $source ); ?>"/>
353
                    <input type="hidden" name="redirect_fragment" value="<?php echo esc_attr( $widget_id ); ?>"/>
354
					<?php wp_nonce_field( 'blogsub_subscribe_' . $current_blog->blog_id, '_wpnonce', false ); ?>
355
                    <button type="submit"
356
	                    <?php if ( ! empty( $submit_button_classes ) ) { ?>
357
	                        class="<?php echo esc_attr( $submit_button_classes ); ?>"
358
	                    <?php }; ?>
359
		                <?php if ( ! empty( $submit_button_styles ) ) { ?>
360
			                style="<?php echo esc_attr( $submit_button_styles ); ?>"
361
		                <?php }; ?>
362
	                >
363
	                    <?php
364
	                    echo wp_kses(
365
		                    $subscribe_button,
366
		                    self::$allowed_html_tags_for_submit_button
367
	                    );
368
	                    ?>
369
                    </button>
370
                </p>
371
            </form>
372
			<?php
373
		}
374
375
		if ( self::is_jetpack() ) {
376
			/**
377
			 * Filter the subscription form's ID prefix.
378
			 *
379
			 * @module subscriptions
380
			 *
381
			 * @since 2.7.0
382
			 *
383
			 * @param string subscribe-field Subscription form field prefix.
384
			 * @param int $widget_id Widget ID.
385
			 */
386
			$subscribe_field_id = apply_filters( 'subscribe_field_id', 'subscribe-field', $widget_id );
0 ignored issues
show
Unused Code introduced by
The call to apply_filters() has too many arguments starting with $widget_id.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
387
			?>
388
            <form action="#" method="post" accept-charset="utf-8" id="subscribe-blog-<?php echo $widget_id; ?>">
389
				<?php
390
				if ( $subscribe_text && ( ! isset ( $_GET['subscribe'] ) || 'success' != $_GET['subscribe'] ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $subscribe_text of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
391
					?>
392
                    <div id="subscribe-text"><?php echo wpautop( str_replace( '[total-subscribers]', number_format_i18n( $subscribers_total['value'] ), $subscribe_text ) ); ?></div><?php
393
				}
394
395
				if ( $show_subscribers_total && 0 < $subscribers_total['value'] ) {
396
					?>
397
					<div class="jetpack-subscribe-count">
398
						<p>
399
						<?php
400
						/* translators: %s: number of folks following the blog */
401
						echo esc_html( sprintf( _n( 'Join %s other subscriber', 'Join %s other subscribers', $subscribers_total['value'], 'jetpack' ), number_format_i18n( $subscribers_total['value'] ) ) );
402
						?>
403
						</p>
404
					</div>
405
					<?php
406
				}
407
				if ( ! isset ( $_GET['subscribe'] ) || 'success' != $_GET['subscribe'] ) { ?>
408
                    <p id="subscribe-email">
409
                        <label id="jetpack-subscribe-label"
410
                               class="screen-reader-text"
411
                               for="<?php echo esc_attr( $subscribe_field_id ) . '-' . esc_attr( $widget_id ); ?>">
412
							<?php echo ! empty( $subscribe_placeholder ) ? esc_html( $subscribe_placeholder ) : esc_html__( 'Email Address:', 'jetpack' ); ?>
413
                        </label>
414
                        <input type="email" name="email" required="required"
415
                        	<?php if ( ! empty( $email_field_classes ) ) { ?>
416
	                            class="<?php echo esc_attr( $email_field_classes ); ?> required"
417
                            <?php }; ?>
418
		                    <?php if ( ! empty( $email_field_styles ) ) { ?>
419
			                    style="<?php echo esc_attr( $email_field_styles ); ?>"
420
		                    <?php }; ?>
421
                            value="<?php echo esc_attr( $subscribe_email ); ?>"
422
                            id="<?php echo esc_attr( $subscribe_field_id ) . '-' . esc_attr( $widget_id ); ?>"
423
                            placeholder="<?php echo esc_attr( $subscribe_placeholder ); ?>"
424
                        />
425
                    </p>
426
427
                    <p id="subscribe-submit">
428
                        <input type="hidden" name="action" value="subscribe"/>
429
                        <input type="hidden" name="source" value="<?php echo esc_url( $referer ); ?>"/>
430
                        <input type="hidden" name="sub-type" value="<?php echo esc_attr( $source ); ?>"/>
431
                        <input type="hidden" name="redirect_fragment" value="<?php echo $widget_id; ?>"/>
432
						<?php
433
						if ( is_user_logged_in() ) {
434
							wp_nonce_field( 'blogsub_subscribe_' . get_current_blog_id(), '_wpnonce', false );
435
						}
436
						?>
437
                        <button type="submit"
438
	                        <?php if ( ! empty( $submit_button_classes ) ) { ?>
439
	                            class="<?php echo esc_attr( $submit_button_classes ); ?>"
440
                            <?php }; ?>
441
		                    <?php if ( ! empty( $submit_button_styles ) ) { ?>
442
			                    style="<?php echo esc_attr( $submit_button_styles ); ?>"
443
		                    <?php }; ?>
444
	                        name="jetpack_subscriptions_widget"
445
	                    >
446
	                        <?php
447
	                        echo wp_kses(
448
		                        $subscribe_button,
449
		                        self::$allowed_html_tags_for_submit_button
450
	                        ); ?>
451
                        </button>
452
                    </p>
453
				<?php } ?>
454
            </form>
455
		<?php }
456
	}
457
458
	/**
459
	 * Determines if the current user is subscribed to the blog.
460
	 *
461
	 * @return bool Is the person already subscribed.
462
	 */
463
	static function is_current_user_subscribed() {
464
		$subscribed = isset( $_GET['subscribe'] ) && 'success' == $_GET['subscribe'];
465
466
		if ( self::is_wpcom() && class_exists( 'Blog_Subscription' ) && class_exists( 'Blog_Subscriber' ) ) {
467
			$subscribed = is_user_logged_in() && Blog_Subscription::is_subscribed( new Blog_Subscriber() );
468
		}
469
470
		return $subscribed;
471
	}
472
473
	/**
474
	 * Is this script running in the wordpress.com environment?
475
	 *
476
	 * @return bool
477
	 */
478
	static function is_wpcom() {
479
		return defined( 'IS_WPCOM' ) && IS_WPCOM;
480
	}
481
482
	/**
483
	 * Is this script running in a self-hosted environment?
484
	 *
485
	 * @return bool
486
	 */
487
	static function is_jetpack() {
488
		return ! self::is_wpcom();
489
	}
490
491
	/**
492
	 * Used to determine if there is a valid status slug within the wordpress.com environment.
493
	 *
494
	 * @return bool
495
	 */
496
	static function wpcom_has_status_message() {
497
		return isset( $_GET['blogsub'] ) &&
498
		       in_array(
499
			       $_GET['blogsub'],
500
			       array(
501
				       'confirming',
502
				       'blocked',
503
				       'flooded',
504
				       'spammed',
505
				       'subscribed',
506
				       'pending',
507
				       'confirmed',
508
			       )
509
		       );
510
	}
511
512
	/**
513
	 * Determine the amount of folks currently subscribed to the blog.
514
	 *
515
	 * @return int|array
516
	 */
517
	static function fetch_subscriber_count() {
518
		$subs_count = 0;
519
520
		if ( self::is_jetpack() ) {
521
			$subs_count = get_transient( 'wpcom_subscribers_total' );
522
			if ( false === $subs_count || 'failed' == $subs_count['status'] ) {
523
				$xml = new Jetpack_IXR_Client();
524
525
				$xml->query( 'jetpack.fetchSubscriberCount' );
526
527
				if ( $xml->isError() ) { // if we get an error from .com, set the status to failed so that we will try again next time the data is requested
528
					$subs_count = array(
529
						'status'  => 'failed',
530
						'code'    => $xml->getErrorCode(),
531
						'message' => $xml->getErrorMessage(),
532
						'value'   => ( isset( $subs_count['value'] ) ) ? $subs_count['value'] : 0,
533
					);
534
				} else {
535
					$subs_count = array(
536
						'status' => 'success',
537
						'value'  => $xml->getResponse(),
538
					);
539
				}
540
541
				set_transient( 'wpcom_subscribers_total', $subs_count, 3600 ); // try to cache the result for at least 1 hour
542
			}
543
		}
544
545
		if ( self::is_wpcom() && function_exists( 'wpcom_reach_total_for_blog' ) ) {
546
			$subs_count = wpcom_reach_total_for_blog();
547
		}
548
549
		return $subs_count;
550
	}
551
552
	/**
553
	 * Updates a particular instance of a widget when someone saves it in wp-admin.
554
	 *
555
	 * @param array $new_instance
556
	 * @param array $old_instance
557
	 *
558
	 * @return array
559
	 */
560
	function update( $new_instance, $old_instance ) {
561
		$instance = $old_instance;
562
563
		if ( self::is_jetpack() ) {
564
			$instance['title']                 = wp_kses( stripslashes( $new_instance['title'] ), array() );
565
			$instance['subscribe_placeholder'] = wp_kses( stripslashes( $new_instance['subscribe_placeholder'] ), array() );
566
			$instance['subscribe_button']      = wp_kses( stripslashes( $new_instance['subscribe_button'] ), array() );
567
			$instance['success_message']       = wp_kses( stripslashes( $new_instance['success_message'] ), array() );
568
		}
569
570
		if ( self::is_wpcom() ) {
571
			$instance['title']               = strip_tags( stripslashes( $new_instance['title'] ) );
572
			$instance['title_following']     = strip_tags( stripslashes( $new_instance['title_following'] ) );
573
			$instance['subscribe_logged_in'] = wp_filter_post_kses( stripslashes( $new_instance['subscribe_logged_in'] ) );
574
			$instance['subscribe_button']    = strip_tags( stripslashes( $new_instance['subscribe_button'] ) );
575
		}
576
577
		$instance['show_subscribers_total']     = isset( $new_instance['show_subscribers_total'] ) && $new_instance['show_subscribers_total'];
578
		$instance['show_only_email_and_button'] = isset( $new_instance['show_only_email_and_button'] ) && $new_instance['show_only_email_and_button'];
579
		$instance['subscribe_text']             = wp_filter_post_kses( stripslashes( $new_instance['subscribe_text'] ) );
580
581
		return $instance;
582
	}
583
584
	/**
585
	 * The default args for rendering a subscription form.
586
	 *
587
	 * @return array
588
	 */
589
	static function defaults() {
590
		$defaults = array(
591
			'show_subscribers_total'     => true,
592
			'show_only_email_and_button' => false
593
		);
594
595
		$defaults['title']                 = esc_html__( 'Subscribe to Blog via Email', 'jetpack' );
596
		$defaults['subscribe_text']        = esc_html__( 'Enter your email address to subscribe to this blog and receive notifications of new posts by email.', 'jetpack' );
597
		$defaults['subscribe_placeholder'] = esc_html__( 'Email Address', 'jetpack' );
598
		$defaults['subscribe_button']      = esc_html__( 'Subscribe', 'jetpack' );
599
		$defaults['success_message']       = esc_html__( "Success! An email was just sent to confirm your subscription. Please find the email now and click 'Confirm Follow' to start subscribing.", 'jetpack' );
600
601
		return $defaults;
602
	}
603
604
	/**
605
	 * Renders the widget's options form in wp-admin.
606
	 *
607
	 * @param array $instance
608
	 */
609
	function form( $instance ) {
610
		$instance               = wp_parse_args( (array) $instance, $this->defaults() );
0 ignored issues
show
Documentation introduced by
$this->defaults() is of type array, 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...
611
		$show_subscribers_total = checked( $instance['show_subscribers_total'], true, false );
612
613
614
		if ( self::is_wpcom() ) {
615
			$title               = esc_attr( stripslashes( $instance['title'] ) );
616
			$title_following     = esc_attr( stripslashes( $instance['title_following'] ) );
617
			$subscribe_text      = esc_attr( stripslashes( $instance['subscribe_text'] ) );
618
			$subscribe_logged_in = esc_attr( stripslashes( $instance['subscribe_logged_in'] ) );
619
			$subscribe_button    = esc_attr( stripslashes( $instance['subscribe_button'] ) );
620
			$subscribers_total   = self::fetch_subscriber_count();
621
		}
622
623
		if ( self::is_jetpack() ) {
624
			$title                 = stripslashes( $instance['title'] );
625
			$subscribe_text        = stripslashes( $instance['subscribe_text'] );
626
			$subscribe_placeholder = stripslashes( $instance['subscribe_placeholder'] );
627
			$subscribe_button      = stripslashes( $instance['subscribe_button'] );
628
			$success_message       = stripslashes( $instance['success_message'] );
629
			$subs_fetch            = self::fetch_subscriber_count();
630
			if ( 'failed' == $subs_fetch['status'] ) {
631
				printf( '<div class="error inline"><p>%s: %s</p></div>', esc_html( $subs_fetch['code'] ), esc_html( $subs_fetch['message'] ) );
632
			}
633
			$subscribers_total = number_format_i18n( $subs_fetch['value'] );
634
		}
635
636
		if ( self::is_wpcom() ) : ?>
637
            <p>
638
                <label for="<?php echo $this->get_field_id( 'title' ); ?>">
639
					<?php _e( 'Widget title for non-followers:' ); ?>
640
                    <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>"
641
                           name="<?php echo $this->get_field_name( 'title' ); ?>" type="text"
642
                           value="<?php echo $title; ?>"/>
0 ignored issues
show
Bug introduced by
The variable $title does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
643
                </label>
644
            </p>
645
            <p>
646
                <label for="<?php echo $this->get_field_id( 'title_following' ); ?>">
647
					<?php _e( 'Widget title for followers:' ); ?>
648
                    <input class="widefat" id="<?php echo $this->get_field_id( 'title_following' ); ?>"
649
                           name="<?php echo $this->get_field_name( 'title_following' ); ?>" type="text"
650
                           value="<?php echo $title_following; ?>"/>
0 ignored issues
show
Bug introduced by
The variable $title_following does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
651
                </label>
652
            </p>
653
            <p>
654
                <label for="<?php echo $this->get_field_id( 'subscribe_logged_in' ); ?>">
655
					<?php _e( 'Optional text to display to logged in WordPress.com users:' ); ?>
656
                    <textarea style="width: 95%" id="<?php echo $this->get_field_id( 'subscribe_logged_in' ); ?>"
657
                              name="<?php echo $this->get_field_name( 'subscribe_logged_in' ); ?>"
658
                              type="text"><?php echo $subscribe_logged_in; ?></textarea>
0 ignored issues
show
Bug introduced by
The variable $subscribe_logged_in does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
659
                </label>
660
            </p>
661
            <p>
662
                <label for="<?php echo $this->get_field_id( 'subscribe_text' ); ?>">
663
					<?php _e( 'Optional text to display to non-WordPress.com users:' ); ?>
664
                    <textarea style="width: 95%" id="<?php echo $this->get_field_id( 'subscribe_text' ); ?>"
665
                              name="<?php echo $this->get_field_name( 'subscribe_text' ); ?>"
666
                              type="text"><?php echo $subscribe_text; ?></textarea>
0 ignored issues
show
Bug introduced by
The variable $subscribe_text does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
667
                </label>
668
            </p>
669
            <p>
670
                <label for="<?php echo $this->get_field_id( 'subscribe_button' ); ?>">
671
					<?php _e( 'Follow Button Text:' ); ?>
672
                    <input class="widefat" id="<?php echo $this->get_field_id( 'subscribe_button' ); ?>"
673
                           name="<?php echo $this->get_field_name( 'subscribe_button' ); ?>" type="text"
674
                           value="<?php echo $subscribe_button; ?>"/>
0 ignored issues
show
Bug introduced by
The variable $subscribe_button does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
675
                </label>
676
            </p>
677
            <p>
678
                <label for="<?php echo $this->get_field_id( 'show_subscribers_total' ); ?>">
679
                    <input type="checkbox" id="<?php echo $this->get_field_id( 'show_subscribers_total' ); ?>"
680
                           name="<?php echo $this->get_field_name( 'show_subscribers_total' ); ?>"
681
                           value="1"<?php echo $show_subscribers_total; ?> />
682
					<?php echo esc_html( sprintf( _n( 'Show total number of followers? (%s follower)', 'Show total number of followers? (%s followers)', $subscribers_total ), number_format_i18n( $subscribers_total ) ) ); ?>
0 ignored issues
show
Bug introduced by
The variable $subscribers_total does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
683
                </label>
684
            </p>
685
		<?php endif;
686
687
		if ( self::is_jetpack() ) : ?>
688
            <p>
689
                <label for="<?php echo $this->get_field_id( 'title' ); ?>">
690
					<?php _e( 'Widget title:', 'jetpack' ); ?>
691
                    <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>"
692
                           name="<?php echo $this->get_field_name( 'title' ); ?>" type="text"
693
                           value="<?php echo esc_attr( $title ); ?>"/>
694
                </label>
695
            </p>
696
            <p>
697
                <label for="<?php echo $this->get_field_id( 'subscribe_text' ); ?>">
698
					<?php _e( 'Optional text to display to your readers:', 'jetpack' ); ?>
699
                    <textarea class="widefat" id="<?php echo $this->get_field_id( 'subscribe_text' ); ?>"
700
                              name="<?php echo $this->get_field_name( 'subscribe_text' ); ?>"
701
                              rows="3"><?php echo esc_html( $subscribe_text ); ?></textarea>
702
                </label>
703
            </p>
704
            <p>
705
                <label for="<?php echo $this->get_field_id( 'subscribe_placeholder' ); ?>">
706
					<?php esc_html_e( 'Subscribe Placeholder:', 'jetpack' ); ?>
707
                    <input class="widefat" id="<?php echo $this->get_field_id( 'subscribe_placeholder' ); ?>"
708
                           name="<?php echo $this->get_field_name( 'subscribe_placeholder' ); ?>" type="text"
709
                           value="<?php echo esc_attr( $subscribe_placeholder ); ?>"/>
0 ignored issues
show
Bug introduced by
The variable $subscribe_placeholder does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
710
                </label>
711
            </p>
712
            <p>
713
                <label for="<?php echo $this->get_field_id( 'subscribe_button' ); ?>">
714
					<?php _e( 'Subscribe Button:', 'jetpack' ); ?>
715
                    <input class="widefat" id="<?php echo $this->get_field_id( 'subscribe_button' ); ?>"
716
                           name="<?php echo $this->get_field_name( 'subscribe_button' ); ?>" type="text"
717
                           value="<?php echo esc_attr( $subscribe_button ); ?>"/>
718
                </label>
719
            </p>
720
            <p>
721
                <label for="<?php echo $this->get_field_id( 'success_message' ); ?>">
722
					<?php _e( 'Success Message Text:', 'jetpack' ); ?>
723
                    <textarea class="widefat" id="<?php echo $this->get_field_id( 'success_message' ); ?>"
724
                              name="<?php echo $this->get_field_name( 'success_message' ); ?>"
725
                              rows="5"><?php echo esc_html( $success_message ); ?></textarea>
0 ignored issues
show
Bug introduced by
The variable $success_message does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
726
                </label>
727
            </p>
728
            <p>
729
                <label for="<?php echo $this->get_field_id( 'show_subscribers_total' ); ?>">
730
                    <input type="checkbox" id="<?php echo $this->get_field_id( 'show_subscribers_total' ); ?>"
731
                           name="<?php echo $this->get_field_name( 'show_subscribers_total' ); ?>"
732
                           value="1"<?php echo $show_subscribers_total; ?> />
733
					<?php echo esc_html( sprintf( _n( 'Show total number of subscribers? (%s subscriber)', 'Show total number of subscribers? (%s subscribers)', $subscribers_total, 'jetpack' ), $subscribers_total ) ); ?>
734
                </label>
735
            </p>
736
		<?php endif;
737
	}
738
}
739
740
if ( defined( 'IS_WPCOM' ) && IS_WPCOM && function_exists( 'class_alias' ) ) {
741
	class_alias( 'Jetpack_Subscriptions_Widget', 'Blog_Subscription_Widget' );
742
}
743
744
function get_jetpack_blog_subscriptions_widget_classname() {
745
	return ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ?
746
		'Blog_Subscription_Widget' :
747
		'Jetpack_Subscriptions_Widget';
748
}
749
750
function jetpack_do_subscription_form( $instance ) {
751
	if ( empty( $instance ) || ! is_array( $instance ) ) {
752
		$instance = array();
753
	}
754
755
	if ( empty( $instance['show_subscribers_total'] ) || 'false' === $instance['show_subscribers_total'] ) {
756
		$instance['show_subscribers_total'] = false;
757
	} else {
758
		$instance['show_subscribers_total'] = true;
759
	}
760
761
	$show_only_email_and_button             = isset( $instance['show_only_email_and_button'] ) ? $instance['show_only_email_and_button'] : false;
762
	$submit_button_text                     = isset( $instance['submit_button_text'] ) ? $instance['submit_button_text'] : '';
763
764
	// Build up a string with the submit button's classes and styles and set it on the instance
765
	$submit_button_classes = isset( $instance['submit_button_classes'] ) ? $instance['submit_button_classes'] : '';
766
	$email_field_classes   = isset( $instance['email_field_classes'] ) ? $instance['email_field_classes'] : '';
767
	$style                 = '';
0 ignored issues
show
Unused Code introduced by
$style is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
768
	$submit_button_styles  = '';
769
	$email_field_styles    = '';
770
771 View Code Duplication
	if ( isset( $instance['custom_background_button_color'] ) && 'undefined' !== $instance['custom_background_button_color'] ) {
772
		$submit_button_styles .= 'background: ' . $instance['custom_background_button_color'] . '; ';
773
	}
774 View Code Duplication
	if ( isset( $instance['custom_text_button_color'] ) && 'undefined' !== $instance['custom_text_button_color'] ) {
775
		$submit_button_styles .= 'color: ' . $instance['custom_text_button_color'] . '; ';
776
	}
777
778 View Code Duplication
	if ( isset( $instance['custom_font_size'] ) && 'undefined' !== $instance['custom_font_size'] ) {
779
		$style                 = 'font-size: ' . $instance['custom_font_size'] . 'px; ';
780
		$submit_button_styles .= $style;
781
		$email_field_styles   .= $style;
782
	}
783
	if ( isset( $instance['custom_padding'] ) && 'undefined' !== $instance['custom_padding'] ) {
784
		$style = 'padding: ' .
785
			$instance['custom_padding'] . 'px ' .
786
			round( $instance['custom_padding'] * 1.5 ) . 'px ' .
787
			$instance['custom_padding'] . 'px ' .
788
			round( $instance['custom_padding'] * 1.5 ) . 'px; ';
789
790
		$submit_button_styles .= $style;
791
		$email_field_styles   .= $style;
792
	}
793
794
	$button_spacing = 0;
795
	if ( ! empty( $instance['custom_spacing'] ) ) {
796
		$button_spacing = $instance['custom_spacing'];
797
	}
798
	if ( isset( $instance['button_on_newline'] ) && 'true' === $instance['button_on_newline'] ) {
799
		$submit_button_styles .= 'margin-top: ' . $button_spacing . 'px; ';
800
	} else {
801
		$submit_button_styles .= 'margin-left: ' . $button_spacing . 'px; ';
802
	}
803
804 View Code Duplication
	if ( isset( $instance['custom_border_radius'] ) && 'undefined' !== $instance['custom_border_radius'] ) {
805
		$style                 = 'border-radius: ' . $instance['custom_border_radius'] . 'px; ';
806
		$submit_button_styles .= $style;
807
		$email_field_styles   .= $style;
808
	}
809 View Code Duplication
	if ( isset( $instance['custom_border_weight'] ) && 'undefined' !== $instance['custom_border_weight'] ) {
810
		$style                 = 'border-width: ' . $instance['custom_border_weight'] . 'px; ';
811
		$submit_button_styles .= $style;
812
		$email_field_styles   .= $style;
813
	}
814 View Code Duplication
	if ( isset( $instance['custom_border_color'] ) && 'undefined' !== $instance['custom_border_color'] ) {
815
		$style =
816
			'border-color: ' . $instance['custom_border_color'] . '; ' .
817
			'border-style: solid;';
818
819
		$submit_button_styles .= $style;
820
		$email_field_styles   .= $style;
821
	}
822
823
	$instance = shortcode_atts(
824
		Jetpack_Subscriptions_Widget::defaults(),
825
		$instance,
826
		'jetpack_subscription_form'
827
	);
828
829
	// These must come after the call to shortcode_atts()
830
	$instance['submit_button_text']         = $submit_button_text;
831
	$instance['show_only_email_and_button'] = $show_only_email_and_button;
832
	if ( ! empty( $submit_button_classes ) ) {
833
		$instance['submit_button_classes'] = $submit_button_classes;
834
	}
835
	if ( ! empty( $email_field_classes ) ) {
836
		$instance['email_field_classes'] = $email_field_classes;
837
	}
838
839
	if ( ! empty( $submit_button_styles ) ) {
840
		$instance['submit_button_styles'] = trim( $submit_button_styles );
841
	}
842
	if ( ! empty( $email_field_styles ) ) {
843
		$instance['email_field_styles'] = trim( $email_field_styles );
844
	}
845
846
	$args = array(
847
		'before_widget' => '<div class="jetpack_subscription_widget">',
848
	);
849
	ob_start();
850
	the_widget( get_jetpack_blog_subscriptions_widget_classname(), $instance, $args );
851
	$output = ob_get_clean();
852
853
	return $output;
854
}
855
856
add_shortcode( 'jetpack_subscription_form', 'jetpack_do_subscription_form' );
857
add_shortcode( 'blog_subscription_form', 'jetpack_do_subscription_form' );
858
859
function jetpack_blog_subscriptions_init() {
860
	register_widget( get_jetpack_blog_subscriptions_widget_classname() );
861
}
862
863
add_action( 'widgets_init', 'jetpack_blog_subscriptions_init' );
864