Completed
Push — renovate/slack-web-api-5.x ( f1014a...4f2b74 )
by
unknown
30:35 queued 23:31
created

Jetpack_Subscriptions_Widget::enqueue_style()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 9
rs 9.9666
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
	static $allowed_html_tags_for_submit_button = array( 'br' => array() );
9
	/**
10
	 * Use this variable when printing the message after submitting an email in subscription widgets
11
	 *
12
	 * @var array what tags are allowed
13
	 */
14
	public static $allowed_html_tags_for_message = array(
15
		'a'  => array(
16
			'href'   => array(),
17
			'title'  => array(),
18
			'rel'    => array(),
19
			'target' => array(),
20
		),
21
		'br' => array(),
22
	);
23
24
	function __construct() {
25
		$widget_ops = array(
26
			'classname'                   => 'widget_blog_subscription jetpack_subscription_widget',
27
			'description'                 => __( 'Add an email signup form to allow people to subscribe to your blog.', 'jetpack' ),
28
			'customize_selective_refresh' => true,
29
		);
30
31
		$name = self::is_jetpack() ?
32
			/** This filter is documented in modules/widgets/facebook-likebox.php */
33
			apply_filters( 'jetpack_widget_name', __( 'Blog Subscriptions', 'jetpack' ) ) :
34
			__( 'Follow Blog', 'jetpack' );
35
36
		parent::__construct(
37
			'blog_subscription',
38
			$name,
39
			$widget_ops
40
		);
41
42 View Code Duplication
		if ( self::is_jetpack() &&
43
		     (
44
			     is_active_widget( false, false, $this->id_base ) ||
45
			     is_active_widget( false, false, 'monster' ) ||
46
			     is_customize_preview()
47
		     )
48
		) {
49
			add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
50
		}
51
	}
52
53
	/**
54
	 * Enqueue the form's CSS.
55
	 *
56
	 * @since 4.5.0
57
	 */
58
	function enqueue_style() {
59
		wp_register_style(
60
			'jetpack-subscriptions',
61
			plugins_url( 'subscriptions.css', __FILE__ ),
62
			array(),
63
			JETPACK__VERSION
64
		);
65
		wp_enqueue_style( 'jetpack-subscriptions' );
66
	}
67
68
	/**
69
	 * Renders a full widget either within the context of WordPress widget, or in response to a shortcode.
70
	 *
71
	 * @param array $args Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'.
72
	 * @param array $instance The settings for the particular instance of the widget.
73
	 */
74
	function widget( $args, $instance ) {
75
		if ( self::is_jetpack() &&
76
		     /** This filter is documented in modules/contact-form/grunion-contact-form.php */
77
		     false === apply_filters( 'jetpack_auto_fill_logged_in_user', false )
78
		) {
79
			$subscribe_email = '';
80
		} else {
81
			$current_user = wp_get_current_user();
82
			if ( ! empty( $current_user->user_email ) ) {
83
				$subscribe_email = esc_attr( $current_user->user_email );
84
			} else {
85
				$subscribe_email = '';
86
			}
87
		}
88
89
		$stats_action = self::is_jetpack() ? 'jetpack_subscriptions' : 'follow_blog';
90
		/** This action is documented in modules/widgets/gravatar-profile.php */
91
		do_action( 'jetpack_stats_extra', 'widget_view', $stats_action );
92
93
		$after_widget  = isset( $args['after_widget'] ) ? $args['after_widget'] : '';
94
		$before_widget = isset( $args['before_widget'] ) ? $args['before_widget'] : '';
95
		$instance      = wp_parse_args( (array) $instance, $this->defaults() );
96
97
		echo $before_widget;
98
99
		Jetpack_Subscriptions_Widget::$instance_count ++;
100
101
		self::render_widget_title( $args, $instance );
102
103
		self::render_widget_status_messages( $instance );
104
105
		if ( self::is_current_user_subscribed() ) {
106
			self::render_widget_already_subscribed( $instance );
107
		} else {
108
			self::render_widget_subscription_form( $args, $instance, $subscribe_email );
109
		}
110
111
		echo "\n" . $after_widget;
112
	}
113
114
	/**
115
	 * Prints the widget's title. If show_only_email_and_button is true, we will not show a title.
116
	 *
117
	 * @param array $args Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'.
118
	 * @param array $instance The settings for the particular instance of the widget.
119
	 */
120
	static function render_widget_title( $args, $instance ) {
121
		$show_only_email_and_button = $instance['show_only_email_and_button'];
122
		$before_title               = isset( $args['before_title'] ) ? $args['before_title'] : '';
123
		$after_title                = isset( $args['after_title'] ) ? $args['after_title'] : '';
124
		if ( self::is_wpcom() && ! $show_only_email_and_button ) {
125
			if ( self::is_current_user_subscribed() ) {
126 View Code Duplication
				if ( ! empty( $instance['title_following'] ) ) {
127
					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";
128
				}
129 View Code Duplication
			} else {
130
				if ( ! empty( $instance['title'] ) ) {
131
					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";
132
				}
133
			}
134
		}
135
136
		if ( self::is_jetpack() && empty( $instance['show_only_email_and_button'] ) ) {
137
			echo $args['before_title'] . esc_attr( $instance['title'] ) . $args['after_title'] . "\n";
138
		}
139
	}
140
141
	/**
142
	 * Prints the subscription block's status messages after someone has attempted to subscribe.
143
	 * Either a success message or an error message.
144
	 *
145
	 * @param array $instance The settings for the particular instance of the widget.
146
	 */
147
	static function render_widget_status_messages( $instance ) {
148
		if ( self::is_jetpack() && isset( $_GET['subscribe'] ) ) {
149
			$success_message   = isset( $instance['success_message'] ) ? stripslashes( $instance['success_message'] ) : '';
150
			$subscribers_total = self::fetch_subscriber_count();
151
			switch ( $_GET['subscribe'] ) :
152
				case 'invalid_email' : ?>
153
                    <p class="error"><?php esc_html_e( 'The email you entered was invalid. Please check and try again.', 'jetpack' ); ?></p>
154
					<?php break;
155
				case 'opted_out' : ?>
156
                    <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' ),
157
							'https://subscribe.wordpress.com/',
158
							__( 'Manage your email preferences.', 'jetpack' )
159
						); ?></p>
160
					<?php break;
161
				case 'already' : ?>
162
                    <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' ),
163
							'https://subscribe.wordpress.com/',
164
							__( 'Manage your email preferences.', 'jetpack' )
165
						); ?></p>
166
					<?php break;
167
				case 'many_pending_subs':
168
					?>
169
					<p class="error">
170
						<?php
171
						printf(
172
							wp_kses(
173
								/* translators: 1: Link to Subscription Management page https://subscribe.wordpress.com/, 2: Description of this link */
174
								__( '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' ),
175
								self::$allowed_html_tags_for_message
176
							),
177
							'https://subscribe.wordpress.com/',
178
							esc_attr__( 'Manage your email preferences.', 'jetpack' )
179
						);
180
						?>
181
					</p>
182
					<?php break;
183
				case 'success' : ?>
184
                    <div class="success"><?php echo wpautop( str_replace( '[total-subscribers]', number_format_i18n( $subscribers_total['value'] ), $success_message ) ); ?></div>
185
					<?php break;
186
				default : ?>
187
                    <p class="error"><?php esc_html_e( 'There was an error when subscribing. Please try again.', 'jetpack' ); ?></p>
188
					<?php break;
189
			endswitch;
190
		}
191
192
		if ( self::is_wpcom() && self::wpcom_has_status_message() ) {
193
			global $themecolors;
194
			switch ( $_GET['blogsub'] ) {
195 View Code Duplication
				case 'confirming':
196
					echo "<div style='background-color: #{$themecolors['bg']}; border: 1px solid #{$themecolors['border']}; color: #{$themecolors['text']}; padding-left: 5px; padding-right: 5px; margin-bottom: 10px;'>";
197
					_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>.' );
198
					echo "</div>";
199
					break;
200 View Code Duplication
				case 'blocked':
201
					echo "<div style='background-color: #{$themecolors['bg']}; border: 1px solid #{$themecolors['border']}; color: #{$themecolors['text']}; padding-left: 5px; padding-right: 5px; margin-bottom: 10px;'>";
202
					_e( 'Subscriptions have been blocked for this email address.' );
203
					echo "</div>";
204
					break;
205 View Code Duplication
				case 'flooded':
206
					echo "<div style='background-color: #{$themecolors['bg']}; border: 1px solid #{$themecolors['border']}; color: #{$themecolors['text']}; padding-left: 5px; padding-right: 5px; margin-bottom: 10px;'>";
207
					_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.' );
208
					echo "</div>";
209
					break;
210
				case 'spammed':
211
					echo "<div style='background-color: #{$themecolors['bg']}; border: 1px solid #{$themecolors['border']}; color: #{$themecolors['text']}; padding-left: 5px; padding-right: 5px; margin-bottom: 10px;'>";
212
					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/' ) );
213
					echo "</div>";
214
					break;
215 View Code Duplication
				case 'subscribed':
216
					echo "<div style='background-color: #{$themecolors['bg']}; border: 1px solid #{$themecolors['border']}; color: #{$themecolors['text']}; padding-left: 5px; padding-right: 5px; margin-bottom: 10px;'>";
217
					_e( 'You&rsquo;re already subscribed to this site.' );
218
					echo "</div>";
219
					break;
220 View Code Duplication
				case 'pending':
221
					echo "<div style='background-color: #{$themecolors['bg']}; border: 1px solid #{$themecolors['border']}; color: #{$themecolors['text']}; padding-left: 5px; padding-right: 5px; margin-bottom: 10px;'>";
222
					_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.' );
223
					echo "</div>";
224
					break;
225 View Code Duplication
				case 'confirmed':
226
					echo "<div style='background-color: #{$themecolors['bg']}; border: 1px solid #{$themecolors['border']}; color: #{$themecolors['text']}; padding-left: 5px; padding-right: 5px; margin-bottom: 10px;'>";
227
					_e( 'Congrats, you&rsquo;re subscribed! You&rsquo;ll get an email with the details of your subscription and an unsubscribe link.' );
228
					echo "</div>";
229
					break;
230
			}
231
		}
232
	}
233
234
	/**
235
	 * Renders a message to folks who are already subscribed.
236
	 *
237
	 * @param array $instance The settings for the particular instance of the widget.
238
	 *
239
	 * @return void
240
	 */
241
	static function render_widget_already_subscribed( $instance ) {
242
		if ( self::is_wpcom() ) {
243
			$subscribers_total = self::fetch_subscriber_count();
244
			$edit_subs_url     = 'https://wordpress.com/following/edit/';
245
			if ( function_exists( 'localized_wpcom_url' ) ) {
246
				$edit_subs_url = localized_wpcom_url( http() . '://wordpress.com/following/edit/', get_user_locale() );
247
			}
248
			$show_subscribers_total = (bool) $instance['show_subscribers_total'];
249
			if ( $show_subscribers_total && $subscribers_total > 1 ) :
250
				$subscribers_not_me = $subscribers_total - 1;
251
				/* translators: %s: number of folks following the blog */
252
				?>
253
                <p><?php printf( _n( 'You are following this blog, along with %s other amazing person (<a href="%s">manage</a>).', 'You are following this blog, along with %s other amazing people (<a href="%s">manage</a>).', $subscribers_not_me ), number_format_i18n( $subscribers_not_me ), $edit_subs_url ) ?></p><?php
254
			else :
255
				?>
256
                <p><?php printf( __( 'You are following this blog (<a href="%s">manage</a>).' ), $edit_subs_url ) ?></p><?php
257
			endif;
258
		}
259
	}
260
261
	/**
262
	 * Renders a form allowing folks to subscribe to the blog.
263
	 *
264
	 * @param array $args Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'.
265
	 * @param array $instance The settings for the particular instance of the widget.
266
	 * @param string $subscribe_email The email to use to prefill the form.
267
	 */
268
	static function render_widget_subscription_form( $args, $instance, $subscribe_email ) {
269
		$show_only_email_and_button = $instance['show_only_email_and_button'];
270
		$subscribe_logged_in        = isset( $instance['subscribe_logged_in'] ) ? stripslashes( $instance['subscribe_logged_in'] ) : '';
271
		$show_subscribers_total     = (bool) $instance['show_subscribers_total'];
272
		$subscribe_text             = empty( $instance['show_only_email_and_button'] ) ?
273
			stripslashes( $instance['subscribe_text'] ) :
274
			false;
275
		$referer                    = ( is_ssl() ? 'https' : 'http' ) . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
276
		$source                     = 'widget';
277
		$widget_id                  = esc_attr( ! empty( $args['widget_id'] ) ? esc_attr( $args['widget_id'] ) : mt_rand( 450, 550 ) );
278
		$subscribe_button           = ! empty( $instance['submit_button_text'] ) ? $instance['submit_button_text'] : $instance['subscribe_button'];
279
		$subscribers_total          = self::fetch_subscriber_count();
280
		$subscribe_placeholder      = isset( $instance['subscribe_placeholder'] ) ? stripslashes( $instance['subscribe_placeholder'] ) : '';
281
		$submit_button_classes      = isset( $instance['submit_button_classes'] ) ? $instance['submit_button_classes'] : '';
282
		$submit_button_styles       = isset( $instance['submit_button_styles'] ) ? $instance['submit_button_styles'] : '';
283
284
		if ( self::is_wpcom() && ! self::wpcom_has_status_message() ) {
285
			global $current_blog;
286
			$url = defined( 'SUBSCRIBE_BLOG_URL' ) ? SUBSCRIBE_BLOG_URL : '';
287
			?>
288
            <form action="<?php echo $url; ?>" method="post" accept-charset="utf-8"
289
                  id="subscribe-blog<?php if ( Jetpack_Subscriptions_Widget::$instance_count > 1 ) {
290
				      echo '-' . Jetpack_Subscriptions_Widget::$instance_count;
291
			      } ?>">
292
				<?php if ( is_user_logged_in() ) : ?>
293
					<?php
294
					if ( ! $show_only_email_and_button ) {
295
						echo wpautop( $subscribe_logged_in );
296
					}
297 View Code Duplication
					if ( $show_subscribers_total && $subscribers_total ) {
298
						/* translators: %s: number of folks following the blog */
299
						echo wpautop( sprintf( _n( 'Join %s other follower', 'Join %s other followers', $subscribers_total ), number_format_i18n( $subscribers_total ) ) );
300
					}
301
					?>
302
				<?php else : ?>
303
					<?php
304
					if ( ! $show_only_email_and_button ) {
305
						echo wpautop( $subscribe_text );
306
					}
307 View Code Duplication
					if ( $show_subscribers_total && $subscribers_total ) {
308
						/* translators: %s: number of folks following the blog */
309
						echo wpautop( sprintf( _n( 'Join %s other follower', 'Join %s other followers', $subscribers_total ), number_format_i18n( $subscribers_total ) ) );
310
					}
311
					?>
312
                    <p><input type="text" name="email" style="width: 95%; padding: 1px 2px"
313
                              placeholder="<?php esc_attr_e( 'Enter your email address' ); ?>" value=""
314
                              id="subscribe-field<?php if ( Jetpack_Subscriptions_Widget::$instance_count > 1 ) {
315
						          echo '-' . Jetpack_Subscriptions_Widget::$instance_count;
316
					          } ?>"/></p>
317
				<?php endif; ?>
318
319
                <p>
320
                    <input type="hidden" name="action" value="subscribe"/>
321
                    <input type="hidden" name="blog_id" value="<?php echo (int) $current_blog->blog_id; ?>"/>
322
                    <input type="hidden" name="source" value="<?php echo esc_url( $referer ); ?>"/>
323
                    <input type="hidden" name="sub-type" value="<?php echo esc_attr( $source ); ?>"/>
324
                    <input type="hidden" name="redirect_fragment" value="<?php echo esc_attr( $widget_id ); ?>"/>
325
					<?php wp_nonce_field( 'blogsub_subscribe_' . $current_blog->blog_id, '_wpnonce', false ); ?>
326
                    <button type="submit"
327
	                    <?php if ( ! empty( $submit_button_classes ) ) { ?>
328
	                        class="<?php echo esc_attr( $submit_button_classes ); ?>"
329
	                    <?php }; ?>
330
		                <?php if ( ! empty( $submit_button_styles ) ) { ?>
331
			                style="<?php echo esc_attr( $submit_button_styles ); ?>"
332
		                <?php }; ?>
333
	                >
334
	                    <?php
335
	                    echo wp_kses(
336
		                    $subscribe_button,
337
		                    self::$allowed_html_tags_for_submit_button
338
	                    );
339
	                    ?>
340
                    </button>
341
                </p>
342
            </form>
343
			<?php
344
		}
345
346
		if ( self::is_jetpack() ) {
347
			/**
348
			 * Filter the subscription form's ID prefix.
349
			 *
350
			 * @module subscriptions
351
			 *
352
			 * @since 2.7.0
353
			 *
354
			 * @param string subscribe-field Subscription form field prefix.
355
			 * @param int $widget_id Widget ID.
356
			 */
357
			$subscribe_field_id = apply_filters( 'subscribe_field_id', 'subscribe-field', $widget_id );
358
			?>
359
            <form action="#" method="post" accept-charset="utf-8" id="subscribe-blog-<?php echo $widget_id; ?>">
360
				<?php
361
				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...
362
					?>
363
                    <div id="subscribe-text"><?php echo wpautop( str_replace( '[total-subscribers]', number_format_i18n( $subscribers_total['value'] ), $subscribe_text ) ); ?></div><?php
364
				}
365
366
				if ( $show_subscribers_total && 0 < $subscribers_total['value'] ) {
367
					/* translators: %s: number of folks following the blog */
368
					echo wpautop( sprintf( _n( 'Join %s other subscriber', 'Join %s other subscribers', $subscribers_total['value'], 'jetpack' ), number_format_i18n( $subscribers_total['value'] ) ) );
369
				}
370
				if ( ! isset ( $_GET['subscribe'] ) || 'success' != $_GET['subscribe'] ) { ?>
371
                    <p id="subscribe-email">
372
                        <label id="jetpack-subscribe-label"
373
                               class="screen-reader-text"
374
                               for="<?php echo esc_attr( $subscribe_field_id ) . '-' . esc_attr( $widget_id ); ?>">
375
							<?php echo ! empty( $subscribe_placeholder ) ? esc_html( $subscribe_placeholder ) : esc_html__( 'Email Address:', 'jetpack' ); ?>
376
                        </label>
377
                        <input type="email" name="email" required="required" class="required"
378
                               value="<?php echo esc_attr( $subscribe_email ); ?>"
379
                               id="<?php echo esc_attr( $subscribe_field_id ) . '-' . esc_attr( $widget_id ); ?>"
380
                               placeholder="<?php echo esc_attr( $subscribe_placeholder ); ?>"/>
381
                    </p>
382
383
                    <p id="subscribe-submit">
384
                        <input type="hidden" name="action" value="subscribe"/>
385
                        <input type="hidden" name="source" value="<?php echo esc_url( $referer ); ?>"/>
386
                        <input type="hidden" name="sub-type" value="<?php echo esc_attr( $source ); ?>"/>
387
                        <input type="hidden" name="redirect_fragment" value="<?php echo $widget_id; ?>"/>
388
						<?php
389
						if ( is_user_logged_in() ) {
390
							wp_nonce_field( 'blogsub_subscribe_' . get_current_blog_id(), '_wpnonce', false );
391
						}
392
						?>
393
                        <button type="submit"
394
	                        <?php if ( ! empty( $submit_button_classes ) ) { ?>
395
	                            class="<?php echo esc_attr( $submit_button_classes ); ?>"
396
                            <?php }; ?>
397
		                    <?php if ( ! empty( $submit_button_styles ) ) { ?>
398
			                    style="<?php echo esc_attr( $submit_button_styles ); ?>"
399
		                    <?php }; ?>
400
	                        name="jetpack_subscriptions_widget"
401
	                    >
402
	                        <?php
403
	                        echo wp_kses(
404
		                        $subscribe_button,
405
		                        self::$allowed_html_tags_for_submit_button
406
	                        ); ?>
407
                        </button>
408
                    </p>
409
				<?php } ?>
410
            </form>
411
		<?php }
412
	}
413
414
	/**
415
	 * Determines if the current user is subscribed to the blog.
416
	 *
417
	 * @return bool Is the person already subscribed.
418
	 */
419
	static function is_current_user_subscribed() {
420
		$subscribed = isset( $_GET['subscribe'] ) && 'success' == $_GET['subscribe'];
421
422
		if ( self::is_wpcom() && class_exists( 'Blog_Subscription' ) && class_exists( 'Blog_Subscriber' ) ) {
423
			$subscribed = is_user_logged_in() && Blog_Subscription::is_subscribed( new Blog_Subscriber() );
424
		}
425
426
		return $subscribed;
427
	}
428
429
	/**
430
	 * Is this script running in the wordpress.com environment?
431
	 *
432
	 * @return bool
433
	 */
434
	static function is_wpcom() {
435
		return defined( 'IS_WPCOM' ) && IS_WPCOM;
436
	}
437
438
	/**
439
	 * Is this script running in a self-hosted environment?
440
	 *
441
	 * @return bool
442
	 */
443
	static function is_jetpack() {
444
		return ! self::is_wpcom();
445
	}
446
447
	/**
448
	 * Used to determine if there is a valid status slug within the wordpress.com environment.
449
	 *
450
	 * @return bool
451
	 */
452
	static function wpcom_has_status_message() {
453
		return isset( $_GET['blogsub'] ) &&
454
		       in_array(
455
			       $_GET['blogsub'],
456
			       array(
457
				       'confirming',
458
				       'blocked',
459
				       'flooded',
460
				       'spammed',
461
				       'subscribed',
462
				       'pending',
463
				       'confirmed',
464
			       )
465
		       );
466
	}
467
468
	/**
469
	 * Determine the amount of folks currently subscribed to the blog.
470
	 *
471
	 * @return int|array
472
	 */
473
	static function fetch_subscriber_count() {
474
		$subs_count = 0;
475
476
		if ( self::is_jetpack() ) {
477
			$subs_count = get_transient( 'wpcom_subscribers_total' );
478
			if ( false === $subs_count || 'failed' == $subs_count['status'] ) {
479
				$xml = new Jetpack_IXR_Client( array( 'user_id' => JETPACK_MASTER_USER, ) );
480
481
				$xml->query( 'jetpack.fetchSubscriberCount' );
482
483
				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
484
					$subs_count = array(
485
						'status'  => 'failed',
486
						'code'    => $xml->getErrorCode(),
487
						'message' => $xml->getErrorMessage(),
488
						'value'   => ( isset( $subs_count['value'] ) ) ? $subs_count['value'] : 0,
489
					);
490
				} else {
491
					$subs_count = array(
492
						'status' => 'success',
493
						'value'  => $xml->getResponse(),
494
					);
495
				}
496
497
				set_transient( 'wpcom_subscribers_total', $subs_count, 3600 ); // try to cache the result for at least 1 hour
498
			}
499
		}
500
501
		if ( self::is_wpcom() && function_exists( 'wpcom_reach_total_for_blog' ) ) {
502
			$subs_count = wpcom_reach_total_for_blog();
503
		}
504
505
		return $subs_count;
506
	}
507
508
	/**
509
	 * Updates a particular instance of a widget when someone saves it in wp-admin.
510
	 *
511
	 * @param array $new_instance
512
	 * @param array $old_instance
513
	 *
514
	 * @return array
515
	 */
516
	function update( $new_instance, $old_instance ) {
517
		$instance = $old_instance;
518
519
		if ( self::is_jetpack() ) {
520
			$instance['title']                 = wp_kses( stripslashes( $new_instance['title'] ), array() );
521
			$instance['subscribe_placeholder'] = wp_kses( stripslashes( $new_instance['subscribe_placeholder'] ), array() );
522
			$instance['subscribe_button']      = wp_kses( stripslashes( $new_instance['subscribe_button'] ), array() );
523
			$instance['success_message']       = wp_kses( stripslashes( $new_instance['success_message'] ), array() );
524
		}
525
526
		if ( self::is_wpcom() ) {
527
			$instance['title']               = strip_tags( stripslashes( $new_instance['title'] ) );
528
			$instance['title_following']     = strip_tags( stripslashes( $new_instance['title_following'] ) );
529
			$instance['subscribe_logged_in'] = wp_filter_post_kses( stripslashes( $new_instance['subscribe_logged_in'] ) );
530
			$instance['subscribe_button']    = strip_tags( stripslashes( $new_instance['subscribe_button'] ) );
531
		}
532
533
		$instance['show_subscribers_total']     = isset( $new_instance['show_subscribers_total'] ) && $new_instance['show_subscribers_total'];
534
		$instance['show_only_email_and_button'] = isset( $new_instance['show_only_email_and_button'] ) && $new_instance['show_only_email_and_button'];
535
		$instance['subscribe_text']             = wp_filter_post_kses( stripslashes( $new_instance['subscribe_text'] ) );
536
537
		return $instance;
538
	}
539
540
	/**
541
	 * The default args for rendering a subscription form.
542
	 *
543
	 * @return array
544
	 */
545
	static function defaults() {
546
		$defaults = array(
547
			'show_subscribers_total'     => true,
548
			'show_only_email_and_button' => false
549
		);
550
551
		if ( self::is_jetpack() ) {
552
			$defaults['title']                 = esc_html__( 'Subscribe to Blog via Email', 'jetpack' );
553
			$defaults['subscribe_text']        = esc_html__( 'Enter your email address to subscribe to this blog and receive notifications of new posts by email.', 'jetpack' );
554
			$defaults['subscribe_placeholder'] = esc_html__( 'Email Address', 'jetpack' );
555
			$defaults['subscribe_button']      = esc_html__( 'Subscribe', 'jetpack' );
556
			$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' );
557
		}
558
559
		if ( self::is_wpcom() ) {
560
			$defaults['title']               = __( 'Follow Blog via Email' );
561
			$defaults['title_following']     = __( 'You are following this blog' );
562
			$defaults['subscribe_text']      = __( 'Enter your email address to follow this blog and receive notifications of new posts by email.' );
563
			$defaults['subscribe_button']    = __( 'Follow' );
564
			$defaults['subscribe_logged_in'] = __( 'Click to follow this blog and receive notifications of new posts by email.' );
565
		}
566
567
		return $defaults;
568
	}
569
570
	/**
571
	 * Renders the widget's options form in wp-admin.
572
	 *
573
	 * @param array $instance
574
	 */
575
	function form( $instance ) {
576
		$instance               = wp_parse_args( (array) $instance, $this->defaults() );
577
		$show_subscribers_total = checked( $instance['show_subscribers_total'], true, false );
578
579
580
		if ( self::is_wpcom() ) {
581
			$title               = esc_attr( stripslashes( $instance['title'] ) );
582
			$title_following     = esc_attr( stripslashes( $instance['title_following'] ) );
583
			$subscribe_text      = esc_attr( stripslashes( $instance['subscribe_text'] ) );
584
			$subscribe_logged_in = esc_attr( stripslashes( $instance['subscribe_logged_in'] ) );
585
			$subscribe_button    = esc_attr( stripslashes( $instance['subscribe_button'] ) );
586
			$subscribers_total   = self::fetch_subscriber_count();
587
		}
588
589
		if ( self::is_jetpack() ) {
590
			$title                 = stripslashes( $instance['title'] );
591
			$subscribe_text        = stripslashes( $instance['subscribe_text'] );
592
			$subscribe_placeholder = stripslashes( $instance['subscribe_placeholder'] );
593
			$subscribe_button      = stripslashes( $instance['subscribe_button'] );
594
			$success_message       = stripslashes( $instance['success_message'] );
595
			$subs_fetch            = self::fetch_subscriber_count();
596
			if ( 'failed' == $subs_fetch['status'] ) {
597
				printf( '<div class="error inline"><p>%s: %s</p></div>', esc_html( $subs_fetch['code'] ), esc_html( $subs_fetch['message'] ) );
598
			}
599
			$subscribers_total = number_format_i18n( $subs_fetch['value'] );
600
		}
601
602
		if ( self::is_wpcom() ) : ?>
603
            <p>
604
                <label for="<?php echo $this->get_field_id( 'title' ); ?>">
605
					<?php _e( 'Widget title for non-followers:' ); ?>
606
                    <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>"
607
                           name="<?php echo $this->get_field_name( 'title' ); ?>" type="text"
608
                           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...
609
                </label>
610
            </p>
611
            <p>
612
                <label for="<?php echo $this->get_field_id( 'title_following' ); ?>">
613
					<?php _e( 'Widget title for followers:' ); ?>
614
                    <input class="widefat" id="<?php echo $this->get_field_id( 'title_following' ); ?>"
615
                           name="<?php echo $this->get_field_name( 'title_following' ); ?>" type="text"
616
                           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...
617
                </label>
618
            </p>
619
            <p>
620
                <label for="<?php echo $this->get_field_id( 'subscribe_logged_in' ); ?>">
621
					<?php _e( 'Optional text to display to logged in WordPress.com users:' ); ?>
622
                    <textarea style="width: 95%" id="<?php echo $this->get_field_id( 'subscribe_logged_in' ); ?>"
623
                              name="<?php echo $this->get_field_name( 'subscribe_logged_in' ); ?>"
624
                              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...
625
                </label>
626
            </p>
627
            <p>
628
                <label for="<?php echo $this->get_field_id( 'subscribe_text' ); ?>">
629
					<?php _e( 'Optional text to display to non-WordPress.com users:' ); ?>
630
                    <textarea style="width: 95%" id="<?php echo $this->get_field_id( 'subscribe_text' ); ?>"
631
                              name="<?php echo $this->get_field_name( 'subscribe_text' ); ?>"
632
                              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...
633
                </label>
634
            </p>
635
            <p>
636
                <label for="<?php echo $this->get_field_id( 'subscribe_button' ); ?>">
637
					<?php _e( 'Follow Button Text:' ); ?>
638
                    <input class="widefat" id="<?php echo $this->get_field_id( 'subscribe_button' ); ?>"
639
                           name="<?php echo $this->get_field_name( 'subscribe_button' ); ?>" type="text"
640
                           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...
641
                </label>
642
            </p>
643
            <p>
644
                <label for="<?php echo $this->get_field_id( 'show_subscribers_total' ); ?>">
645
                    <input type="checkbox" id="<?php echo $this->get_field_id( 'show_subscribers_total' ); ?>"
646
                           name="<?php echo $this->get_field_name( 'show_subscribers_total' ); ?>"
647
                           value="1"<?php echo $show_subscribers_total; ?> />
648
					<?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...
649
                </label>
650
            </p>
651
		<?php endif;
652
653
		if ( self::is_jetpack() ) : ?>
654
            <p>
655
                <label for="<?php echo $this->get_field_id( 'title' ); ?>">
656
					<?php _e( 'Widget title:', 'jetpack' ); ?>
657
                    <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>"
658
                           name="<?php echo $this->get_field_name( 'title' ); ?>" type="text"
659
                           value="<?php echo esc_attr( $title ); ?>"/>
660
                </label>
661
            </p>
662
            <p>
663
                <label for="<?php echo $this->get_field_id( 'subscribe_text' ); ?>">
664
					<?php _e( 'Optional text to display to your readers:', 'jetpack' ); ?>
665
                    <textarea class="widefat" id="<?php echo $this->get_field_id( 'subscribe_text' ); ?>"
666
                              name="<?php echo $this->get_field_name( 'subscribe_text' ); ?>"
667
                              rows="3"><?php echo esc_html( $subscribe_text ); ?></textarea>
668
                </label>
669
            </p>
670
            <p>
671
                <label for="<?php echo $this->get_field_id( 'subscribe_placeholder' ); ?>">
672
					<?php esc_html_e( 'Subscribe Placeholder:', 'jetpack' ); ?>
673
                    <input class="widefat" id="<?php echo $this->get_field_id( 'subscribe_placeholder' ); ?>"
674
                           name="<?php echo $this->get_field_name( 'subscribe_placeholder' ); ?>" type="text"
675
                           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...
676
                </label>
677
            </p>
678
            <p>
679
                <label for="<?php echo $this->get_field_id( 'subscribe_button' ); ?>">
680
					<?php _e( 'Subscribe Button:', 'jetpack' ); ?>
681
                    <input class="widefat" id="<?php echo $this->get_field_id( 'subscribe_button' ); ?>"
682
                           name="<?php echo $this->get_field_name( 'subscribe_button' ); ?>" type="text"
683
                           value="<?php echo esc_attr( $subscribe_button ); ?>"/>
684
                </label>
685
            </p>
686
            <p>
687
                <label for="<?php echo $this->get_field_id( 'success_message' ); ?>">
688
					<?php _e( 'Success Message Text:', 'jetpack' ); ?>
689
                    <textarea class="widefat" id="<?php echo $this->get_field_id( 'success_message' ); ?>"
690
                              name="<?php echo $this->get_field_name( 'success_message' ); ?>"
691
                              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...
692
                </label>
693
            </p>
694
            <p>
695
                <label for="<?php echo $this->get_field_id( 'show_subscribers_total' ); ?>">
696
                    <input type="checkbox" id="<?php echo $this->get_field_id( 'show_subscribers_total' ); ?>"
697
                           name="<?php echo $this->get_field_name( 'show_subscribers_total' ); ?>"
698
                           value="1"<?php echo $show_subscribers_total; ?> />
699
					<?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 ) ); ?>
700
                </label>
701
            </p>
702
		<?php endif;
703
	}
704
}
705
706
if ( defined( 'IS_WPCOM' ) && IS_WPCOM && function_exists( 'class_alias' ) ) {
707
	class_alias( 'Jetpack_Subscriptions_Widget', 'Blog_Subscription_Widget' );
708
}
709
710
function get_jetpack_blog_subscriptions_widget_classname() {
711
	return ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ?
712
		'Blog_Subscription_Widget' :
713
		'Jetpack_Subscriptions_Widget';
714
}
715
716
function jetpack_do_subscription_form( $instance ) {
717
	if ( empty( $instance ) || ! is_array( $instance ) ) {
718
		$instance = array();
719
	}
720
721
	if ( empty( $instance['show_subscribers_total'] ) || 'false' === $instance['show_subscribers_total'] ) {
722
		$instance['show_subscribers_total'] = false;
723
	} else {
724
		$instance['show_subscribers_total'] = true;
725
	}
726
727
	$show_only_email_and_button             = isset( $instance['show_only_email_and_button'] ) ? $instance['show_only_email_and_button'] : false;
728
	$submit_button_text                     = isset( $instance['submit_button_text'] ) ? $instance['submit_button_text'] : '';
729
730
731
732
	// Build up a string with the submit button's classes and styles and set it on the instance
733
	$submit_button_classes = isset( $instance['submit_button_classes'] ) ? $instance['submit_button_classes'] : '';
734
	$submit_button_styles = '';
735
	if ( isset( $instance['custom_background_button_color'] ) ) {
736
		$submit_button_styles .= 'background-color: ' . $instance['custom_background_button_color'] . '; ';
737
	}
738
	if ( isset( $instance['custom_text_button_color'] ) ) {
739
		$submit_button_styles .= 'color: ' . $instance['custom_text_button_color'] . ';';
740
	}
741
742
	$instance = shortcode_atts(
743
		Jetpack_Subscriptions_Widget::defaults(),
744
		$instance,
745
		'jetpack_subscription_form'
746
	);
747
748
	// These must come after the call to shortcode_atts()
749
	$instance['submit_button_text']         = $submit_button_text;
750
	$instance['show_only_email_and_button'] = $show_only_email_and_button;
751
	if ( ! empty( $submit_button_classes ) ) {
752
		$instance['submit_button_classes'] = $submit_button_classes;
753
	}
754
	if ( ! empty ( $submit_button_styles ) ) {
755
		$instance['submit_button_styles'] = $submit_button_styles;
756
	}
757
758
	$args = array(
759
		'before_widget' => '<div class="jetpack_subscription_widget">',
760
	);
761
	ob_start();
762
	the_widget( get_jetpack_blog_subscriptions_widget_classname(), $instance, $args );
763
	$output = ob_get_clean();
764
765
	return $output;
766
}
767
768
add_shortcode( 'jetpack_subscription_form', 'jetpack_do_subscription_form' );
769
add_shortcode( 'blog_subscription_form', 'jetpack_do_subscription_form' );
770
771
function jetpack_blog_subscriptions_init() {
772
	register_widget( get_jetpack_blog_subscriptions_widget_classname() );
773
}
774
775
add_action( 'widgets_init', 'jetpack_blog_subscriptions_init' );
776
777
function jetpack_register_subscriptions_block() {
778
	if ( class_exists( 'WP_Block_Type_Registry' ) && ! WP_Block_Type_Registry::get_instance()->is_registered( 'jetpack/subscriptions' ) ) {
779
		jetpack_register_block( 'jetpack/subscriptions' );
780
	}
781
}
782
783
add_action( 'init', 'jetpack_register_subscriptions_block' );
784