Completed
Push — try/wp-client-i18n ( 5ca50e...f7e161 )
by Jeremy
26:36 queued 16:52
created

views.php ➔ jetpack_do_subscription_form()   F

Complexity

Conditions 12
Paths 512

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
nc 512
nop 1
dl 0
loc 48
rs 3.4777
c 0
b 0
f 0

How to fix   Complexity   

Long Method

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

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

Commonly applied refactorings include:

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