PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 8.2
Jetpack – WP Security, Backup, Speed, & Growth v8.2
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / subscriptions / views.php

views.php in Jetpack – WP Security, Backup, Speed, & Growth 8.2, at modules/subscriptions/views.php

801 lines 36.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 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 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 } 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 'pending':
184 ?>
185 <p class="error">
186 <?php
187 printf(
188 wp_kses(
189 /* translators: 1: Link to Subscription Management page https://subscribe.wordpress.com/, 2: Description of this link */
190 __( 'You subscribed this site before but you have not clicked the confirmation link yet. Please check your inbox. <br /> Otherwise, you can manage your preferences at <a href="%1$s" title="%2$s" target="_blank" rel="noopener noreferrer">subscribe.wordpress.com</a>.', 'jetpack' ),
191 self::$allowed_html_tags_for_message
192 ),
193 'https://subscribe.wordpress.com/',
194 esc_attr__( 'Manage your email preferences.', 'jetpack' )
195 );
196 ?>
197 </p>
198 <?php
199 break;
200 case 'success' : ?>
201 <div class="success"><?php echo wpautop( str_replace( '[total-subscribers]', number_format_i18n( $subscribers_total['value'] ), $success_message ) ); ?></div>
202 <?php break;
203 default : ?>
204 <p class="error"><?php esc_html_e( 'There was an error when subscribing. Please try again.', 'jetpack' ); ?></p>
205 <?php break;
206 endswitch;
207 }
208
209 if ( self::is_wpcom() && self::wpcom_has_status_message() ) {
210 global $themecolors;
211 switch ( $_GET['blogsub'] ) {
212 case 'confirming':
213 echo "<div style='background-color: #{$themecolors['bg']}; border: 1px solid #{$themecolors['border']}; color: #{$themecolors['text']}; padding-left: 5px; padding-right: 5px; margin-bottom: 10px;'>";
214 _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>.' );
215 echo "</div>";
216 break;
217 case 'blocked':
218 echo "<div style='background-color: #{$themecolors['bg']}; border: 1px solid #{$themecolors['border']}; color: #{$themecolors['text']}; padding-left: 5px; padding-right: 5px; margin-bottom: 10px;'>";
219 _e( 'Subscriptions have been blocked for this email address.' );
220 echo "</div>";
221 break;
222 case 'flooded':
223 echo "<div style='background-color: #{$themecolors['bg']}; border: 1px solid #{$themecolors['border']}; color: #{$themecolors['text']}; padding-left: 5px; padding-right: 5px; margin-bottom: 10px;'>";
224 _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.' );
225 echo "</div>";
226 break;
227 case 'spammed':
228 echo "<div style='background-color: #{$themecolors['bg']}; border: 1px solid #{$themecolors['border']}; color: #{$themecolors['text']}; padding-left: 5px; padding-right: 5px; margin-bottom: 10px;'>";
229 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/' ) );
230 echo "</div>";
231 break;
232 case 'subscribed':
233 echo "<div style='background-color: #{$themecolors['bg']}; border: 1px solid #{$themecolors['border']}; color: #{$themecolors['text']}; padding-left: 5px; padding-right: 5px; margin-bottom: 10px;'>";
234 _e( 'You&rsquo;re already subscribed to this site.' );
235 echo "</div>";
236 break;
237 case 'pending':
238 echo "<div style='background-color: #{$themecolors['bg']}; border: 1px solid #{$themecolors['border']}; color: #{$themecolors['text']}; padding-left: 5px; padding-right: 5px; margin-bottom: 10px;'>";
239 _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.' );
240 echo "</div>";
241 break;
242 case 'confirmed':
243 echo "<div style='background-color: #{$themecolors['bg']}; border: 1px solid #{$themecolors['border']}; color: #{$themecolors['text']}; padding-left: 5px; padding-right: 5px; margin-bottom: 10px;'>";
244 _e( 'Congrats, you&rsquo;re subscribed! You&rsquo;ll get an email with the details of your subscription and an unsubscribe link.' );
245 echo "</div>";
246 break;
247 }
248 }
249 }
250
251 /**
252 * Renders a message to folks who are already subscribed.
253 *
254 * @param array $instance The settings for the particular instance of the widget.
255 *
256 * @return void
257 */
258 static function render_widget_already_subscribed( $instance ) {
259 if ( self::is_wpcom() ) {
260 $subscribers_total = self::fetch_subscriber_count();
261 $edit_subs_url = 'https://wordpress.com/following/edit/';
262 if ( function_exists( 'localized_wpcom_url' ) ) {
263 $edit_subs_url = localized_wpcom_url( http() . '://wordpress.com/following/edit/', get_user_locale() );
264 }
265 $show_subscribers_total = (bool) $instance['show_subscribers_total'];
266 if ( $show_subscribers_total && $subscribers_total > 1 ) :
267 $subscribers_not_me = $subscribers_total - 1;
268 /* translators: %s: number of folks following the blog */
269 ?>
270 <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
271 else :
272 ?>
273 <p><?php printf( __( 'You are following this blog (<a href="%s">manage</a>).' ), $edit_subs_url ) ?></p><?php
274 endif;
275 }
276 }
277
278 /**
279 * Renders a form allowing folks to subscribe to the blog.
280 *
281 * @param array $args Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'.
282 * @param array $instance The settings for the particular instance of the widget.
283 * @param string $subscribe_email The email to use to prefill the form.
284 */
285 static function render_widget_subscription_form( $args, $instance, $subscribe_email ) {
286 $show_only_email_and_button = $instance['show_only_email_and_button'];
287 $subscribe_logged_in = isset( $instance['subscribe_logged_in'] ) ? stripslashes( $instance['subscribe_logged_in'] ) : '';
288 $show_subscribers_total = (bool) $instance['show_subscribers_total'];
289 $subscribe_text = empty( $instance['show_only_email_and_button'] ) ?
290 stripslashes( $instance['subscribe_text'] ) :
291 false;
292 $referer = ( is_ssl() ? 'https' : 'http' ) . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
293 $source = 'widget';
294 $widget_id = esc_attr( ! empty( $args['widget_id'] ) ? esc_attr( $args['widget_id'] ) : mt_rand( 450, 550 ) );
295 $subscribe_button = ! empty( $instance['submit_button_text'] ) ? $instance['submit_button_text'] : $instance['subscribe_button'];
296 $subscribers_total = self::fetch_subscriber_count();
297 $subscribe_placeholder = isset( $instance['subscribe_placeholder'] ) ? stripslashes( $instance['subscribe_placeholder'] ) : '';
298 $submit_button_classes = isset( $instance['submit_button_classes'] ) ? $instance['submit_button_classes'] : '';
299 $submit_button_styles = isset( $instance['submit_button_styles'] ) ? $instance['submit_button_styles'] : '';
300
301 if ( self::is_wpcom() && ! self::wpcom_has_status_message() ) {
302 global $current_blog;
303 $url = defined( 'SUBSCRIBE_BLOG_URL' ) ? SUBSCRIBE_BLOG_URL : '';
304 ?>
305 <form action="<?php echo $url; ?>" method="post" accept-charset="utf-8"
306 id="subscribe-blog<?php if ( Jetpack_Subscriptions_Widget::$instance_count > 1 ) {
307 echo '-' . Jetpack_Subscriptions_Widget::$instance_count;
308 } ?>">
309 <?php if ( is_user_logged_in() ) : ?>
310 <?php
311 if ( ! $show_only_email_and_button ) {
312 echo wpautop( $subscribe_logged_in );
313 }
314 if ( $show_subscribers_total && $subscribers_total ) {
315 /* translators: %s: number of folks following the blog */
316 echo wpautop( sprintf( _n( 'Join %s other follower', 'Join %s other followers', $subscribers_total ), number_format_i18n( $subscribers_total ) ) );
317 }
318 ?>
319 <?php else : ?>
320 <?php
321 if ( ! $show_only_email_and_button ) {
322 echo wpautop( $subscribe_text );
323 }
324 if ( $show_subscribers_total && $subscribers_total ) {
325 /* translators: %s: number of folks following the blog */
326 echo wpautop( sprintf( _n( 'Join %s other follower', 'Join %s other followers', $subscribers_total ), number_format_i18n( $subscribers_total ) ) );
327 }
328 ?>
329 <p><input type="text" name="email" style="width: 95%; padding: 1px 2px"
330 placeholder="<?php esc_attr_e( 'Enter your email address' ); ?>" value=""
331 id="subscribe-field<?php if ( Jetpack_Subscriptions_Widget::$instance_count > 1 ) {
332 echo '-' . Jetpack_Subscriptions_Widget::$instance_count;
333 } ?>"/></p>
334 <?php endif; ?>
335
336 <p>
337 <input type="hidden" name="action" value="subscribe"/>
338 <input type="hidden" name="blog_id" value="<?php echo (int) $current_blog->blog_id; ?>"/>
339 <input type="hidden" name="source" value="<?php echo esc_url( $referer ); ?>"/>
340 <input type="hidden" name="sub-type" value="<?php echo esc_attr( $source ); ?>"/>
341 <input type="hidden" name="redirect_fragment" value="<?php echo esc_attr( $widget_id ); ?>"/>
342 <?php wp_nonce_field( 'blogsub_subscribe_' . $current_blog->blog_id, '_wpnonce', false ); ?>
343 <button type="submit"
344 <?php if ( ! empty( $submit_button_classes ) ) { ?>
345 class="<?php echo esc_attr( $submit_button_classes ); ?>"
346 <?php }; ?>
347 <?php if ( ! empty( $submit_button_styles ) ) { ?>
348 style="<?php echo esc_attr( $submit_button_styles ); ?>"
349 <?php }; ?>
350 >
351 <?php
352 echo wp_kses(
353 $subscribe_button,
354 self::$allowed_html_tags_for_submit_button
355 );
356 ?>
357 </button>
358 </p>
359 </form>
360 <?php
361 }
362
363 if ( self::is_jetpack() ) {
364 /**
365 * Filter the subscription form's ID prefix.
366 *
367 * @module subscriptions
368 *
369 * @since 2.7.0
370 *
371 * @param string subscribe-field Subscription form field prefix.
372 * @param int $widget_id Widget ID.
373 */
374 $subscribe_field_id = apply_filters( 'subscribe_field_id', 'subscribe-field', $widget_id );
375 ?>
376 <form action="#" method="post" accept-charset="utf-8" id="subscribe-blog-<?php echo $widget_id; ?>">
377 <?php
378 if ( $subscribe_text && ( ! isset ( $_GET['subscribe'] ) || 'success' != $_GET['subscribe'] ) ) {
379 ?>
380 <div id="subscribe-text"><?php echo wpautop( str_replace( '[total-subscribers]', number_format_i18n( $subscribers_total['value'] ), $subscribe_text ) ); ?></div><?php
381 }
382
383 if ( $show_subscribers_total && 0 < $subscribers_total['value'] ) {
384 /* translators: %s: number of folks following the blog */
385 echo wpautop( sprintf( _n( 'Join %s other subscriber', 'Join %s other subscribers', $subscribers_total['value'], 'jetpack' ), number_format_i18n( $subscribers_total['value'] ) ) );
386 }
387 if ( ! isset ( $_GET['subscribe'] ) || 'success' != $_GET['subscribe'] ) { ?>
388 <p id="subscribe-email">
389 <label id="jetpack-subscribe-label"
390 class="screen-reader-text"
391 for="<?php echo esc_attr( $subscribe_field_id ) . '-' . esc_attr( $widget_id ); ?>">
392 <?php echo ! empty( $subscribe_placeholder ) ? esc_html( $subscribe_placeholder ) : esc_html__( 'Email Address:', 'jetpack' ); ?>
393 </label>
394 <input type="email" name="email" required="required" class="required"
395 value="<?php echo esc_attr( $subscribe_email ); ?>"
396 id="<?php echo esc_attr( $subscribe_field_id ) . '-' . esc_attr( $widget_id ); ?>"
397 placeholder="<?php echo esc_attr( $subscribe_placeholder ); ?>"/>
398 </p>
399
400 <p id="subscribe-submit">
401 <input type="hidden" name="action" value="subscribe"/>
402 <input type="hidden" name="source" value="<?php echo esc_url( $referer ); ?>"/>
403 <input type="hidden" name="sub-type" value="<?php echo esc_attr( $source ); ?>"/>
404 <input type="hidden" name="redirect_fragment" value="<?php echo $widget_id; ?>"/>
405 <?php
406 if ( is_user_logged_in() ) {
407 wp_nonce_field( 'blogsub_subscribe_' . get_current_blog_id(), '_wpnonce', false );
408 }
409 ?>
410 <button type="submit"
411 <?php if ( ! empty( $submit_button_classes ) ) { ?>
412 class="<?php echo esc_attr( $submit_button_classes ); ?>"
413 <?php }; ?>
414 <?php if ( ! empty( $submit_button_styles ) ) { ?>
415 style="<?php echo esc_attr( $submit_button_styles ); ?>"
416 <?php }; ?>
417 name="jetpack_subscriptions_widget"
418 >
419 <?php
420 echo wp_kses(
421 $subscribe_button,
422 self::$allowed_html_tags_for_submit_button
423 ); ?>
424 </button>
425 </p>
426 <?php } ?>
427 </form>
428 <?php }
429 }
430
431 /**
432 * Determines if the current user is subscribed to the blog.
433 *
434 * @return bool Is the person already subscribed.
435 */
436 static function is_current_user_subscribed() {
437 $subscribed = isset( $_GET['subscribe'] ) && 'success' == $_GET['subscribe'];
438
439 if ( self::is_wpcom() && class_exists( 'Blog_Subscription' ) && class_exists( 'Blog_Subscriber' ) ) {
440 $subscribed = is_user_logged_in() && Blog_Subscription::is_subscribed( new Blog_Subscriber() );
441 }
442
443 return $subscribed;
444 }
445
446 /**
447 * Is this script running in the wordpress.com environment?
448 *
449 * @return bool
450 */
451 static function is_wpcom() {
452 return defined( 'IS_WPCOM' ) && IS_WPCOM;
453 }
454
455 /**
456 * Is this script running in a self-hosted environment?
457 *
458 * @return bool
459 */
460 static function is_jetpack() {
461 return ! self::is_wpcom();
462 }
463
464 /**
465 * Used to determine if there is a valid status slug within the wordpress.com environment.
466 *
467 * @return bool
468 */
469 static function wpcom_has_status_message() {
470 return isset( $_GET['blogsub'] ) &&
471 in_array(
472 $_GET['blogsub'],
473 array(
474 'confirming',
475 'blocked',
476 'flooded',
477 'spammed',
478 'subscribed',
479 'pending',
480 'confirmed',
481 )
482 );
483 }
484
485 /**
486 * Determine the amount of folks currently subscribed to the blog.
487 *
488 * @return int|array
489 */
490 static function fetch_subscriber_count() {
491 $subs_count = 0;
492
493 if ( self::is_jetpack() ) {
494 $subs_count = get_transient( 'wpcom_subscribers_total' );
495 if ( false === $subs_count || 'failed' == $subs_count['status'] ) {
496 $xml = new Jetpack_IXR_Client( array( 'user_id' => JETPACK_MASTER_USER, ) );
497
498 $xml->query( 'jetpack.fetchSubscriberCount' );
499
500 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
501 $subs_count = array(
502 'status' => 'failed',
503 'code' => $xml->getErrorCode(),
504 'message' => $xml->getErrorMessage(),
505 'value' => ( isset( $subs_count['value'] ) ) ? $subs_count['value'] : 0,
506 );
507 } else {
508 $subs_count = array(
509 'status' => 'success',
510 'value' => $xml->getResponse(),
511 );
512 }
513
514 set_transient( 'wpcom_subscribers_total', $subs_count, 3600 ); // try to cache the result for at least 1 hour
515 }
516 }
517
518 if ( self::is_wpcom() && function_exists( 'wpcom_reach_total_for_blog' ) ) {
519 $subs_count = wpcom_reach_total_for_blog();
520 }
521
522 return $subs_count;
523 }
524
525 /**
526 * Updates a particular instance of a widget when someone saves it in wp-admin.
527 *
528 * @param array $new_instance
529 * @param array $old_instance
530 *
531 * @return array
532 */
533 function update( $new_instance, $old_instance ) {
534 $instance = $old_instance;
535
536 if ( self::is_jetpack() ) {
537 $instance['title'] = wp_kses( stripslashes( $new_instance['title'] ), array() );
538 $instance['subscribe_placeholder'] = wp_kses( stripslashes( $new_instance['subscribe_placeholder'] ), array() );
539 $instance['subscribe_button'] = wp_kses( stripslashes( $new_instance['subscribe_button'] ), array() );
540 $instance['success_message'] = wp_kses( stripslashes( $new_instance['success_message'] ), array() );
541 }
542
543 if ( self::is_wpcom() ) {
544 $instance['title'] = strip_tags( stripslashes( $new_instance['title'] ) );
545 $instance['title_following'] = strip_tags( stripslashes( $new_instance['title_following'] ) );
546 $instance['subscribe_logged_in'] = wp_filter_post_kses( stripslashes( $new_instance['subscribe_logged_in'] ) );
547 $instance['subscribe_button'] = strip_tags( stripslashes( $new_instance['subscribe_button'] ) );
548 }
549
550 $instance['show_subscribers_total'] = isset( $new_instance['show_subscribers_total'] ) && $new_instance['show_subscribers_total'];
551 $instance['show_only_email_and_button'] = isset( $new_instance['show_only_email_and_button'] ) && $new_instance['show_only_email_and_button'];
552 $instance['subscribe_text'] = wp_filter_post_kses( stripslashes( $new_instance['subscribe_text'] ) );
553
554 return $instance;
555 }
556
557 /**
558 * The default args for rendering a subscription form.
559 *
560 * @return array
561 */
562 static function defaults() {
563 $defaults = array(
564 'show_subscribers_total' => true,
565 'show_only_email_and_button' => false
566 );
567
568 if ( self::is_jetpack() ) {
569 $defaults['title'] = esc_html__( 'Subscribe to Blog via Email', 'jetpack' );
570 $defaults['subscribe_text'] = esc_html__( 'Enter your email address to subscribe to this blog and receive notifications of new posts by email.', 'jetpack' );
571 $defaults['subscribe_placeholder'] = esc_html__( 'Email Address', 'jetpack' );
572 $defaults['subscribe_button'] = esc_html__( 'Subscribe', 'jetpack' );
573 $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' );
574 }
575
576 if ( self::is_wpcom() ) {
577 $defaults['title'] = __( 'Follow Blog via Email' );
578 $defaults['title_following'] = __( 'You are following this blog' );
579 $defaults['subscribe_text'] = __( 'Enter your email address to follow this blog and receive notifications of new posts by email.' );
580 $defaults['subscribe_button'] = __( 'Follow' );
581 $defaults['subscribe_logged_in'] = __( 'Click to follow this blog and receive notifications of new posts by email.' );
582 }
583
584 return $defaults;
585 }
586
587 /**
588 * Renders the widget's options form in wp-admin.
589 *
590 * @param array $instance
591 */
592 function form( $instance ) {
593 $instance = wp_parse_args( (array) $instance, $this->defaults() );
594 $show_subscribers_total = checked( $instance['show_subscribers_total'], true, false );
595
596
597 if ( self::is_wpcom() ) {
598 $title = esc_attr( stripslashes( $instance['title'] ) );
599 $title_following = esc_attr( stripslashes( $instance['title_following'] ) );
600 $subscribe_text = esc_attr( stripslashes( $instance['subscribe_text'] ) );
601 $subscribe_logged_in = esc_attr( stripslashes( $instance['subscribe_logged_in'] ) );
602 $subscribe_button = esc_attr( stripslashes( $instance['subscribe_button'] ) );
603 $subscribers_total = self::fetch_subscriber_count();
604 }
605
606 if ( self::is_jetpack() ) {
607 $title = stripslashes( $instance['title'] );
608 $subscribe_text = stripslashes( $instance['subscribe_text'] );
609 $subscribe_placeholder = stripslashes( $instance['subscribe_placeholder'] );
610 $subscribe_button = stripslashes( $instance['subscribe_button'] );
611 $success_message = stripslashes( $instance['success_message'] );
612 $subs_fetch = self::fetch_subscriber_count();
613 if ( 'failed' == $subs_fetch['status'] ) {
614 printf( '<div class="error inline"><p>%s: %s</p></div>', esc_html( $subs_fetch['code'] ), esc_html( $subs_fetch['message'] ) );
615 }
616 $subscribers_total = number_format_i18n( $subs_fetch['value'] );
617 }
618
619 if ( self::is_wpcom() ) : ?>
620 <p>
621 <label for="<?php echo $this->get_field_id( 'title' ); ?>">
622 <?php _e( 'Widget title for non-followers:' ); ?>
623 <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>"
624 name="<?php echo $this->get_field_name( 'title' ); ?>" type="text"
625 value="<?php echo $title; ?>"/>
626 </label>
627 </p>
628 <p>
629 <label for="<?php echo $this->get_field_id( 'title_following' ); ?>">
630 <?php _e( 'Widget title for followers:' ); ?>
631 <input class="widefat" id="<?php echo $this->get_field_id( 'title_following' ); ?>"
632 name="<?php echo $this->get_field_name( 'title_following' ); ?>" type="text"
633 value="<?php echo $title_following; ?>"/>
634 </label>
635 </p>
636 <p>
637 <label for="<?php echo $this->get_field_id( 'subscribe_logged_in' ); ?>">
638 <?php _e( 'Optional text to display to logged in WordPress.com users:' ); ?>
639 <textarea style="width: 95%" id="<?php echo $this->get_field_id( 'subscribe_logged_in' ); ?>"
640 name="<?php echo $this->get_field_name( 'subscribe_logged_in' ); ?>"
641 type="text"><?php echo $subscribe_logged_in; ?></textarea>
642 </label>
643 </p>
644 <p>
645 <label for="<?php echo $this->get_field_id( 'subscribe_text' ); ?>">
646 <?php _e( 'Optional text to display to non-WordPress.com users:' ); ?>
647 <textarea style="width: 95%" id="<?php echo $this->get_field_id( 'subscribe_text' ); ?>"
648 name="<?php echo $this->get_field_name( 'subscribe_text' ); ?>"
649 type="text"><?php echo $subscribe_text; ?></textarea>
650 </label>
651 </p>
652 <p>
653 <label for="<?php echo $this->get_field_id( 'subscribe_button' ); ?>">
654 <?php _e( 'Follow Button Text:' ); ?>
655 <input class="widefat" id="<?php echo $this->get_field_id( 'subscribe_button' ); ?>"
656 name="<?php echo $this->get_field_name( 'subscribe_button' ); ?>" type="text"
657 value="<?php echo $subscribe_button; ?>"/>
658 </label>
659 </p>
660 <p>
661 <label for="<?php echo $this->get_field_id( 'show_subscribers_total' ); ?>">
662 <input type="checkbox" id="<?php echo $this->get_field_id( 'show_subscribers_total' ); ?>"
663 name="<?php echo $this->get_field_name( 'show_subscribers_total' ); ?>"
664 value="1"<?php echo $show_subscribers_total; ?> />
665 <?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 ) ) ); ?>
666 </label>
667 </p>
668 <?php endif;
669
670 if ( self::is_jetpack() ) : ?>
671 <p>
672 <label for="<?php echo $this->get_field_id( 'title' ); ?>">
673 <?php _e( 'Widget title:', 'jetpack' ); ?>
674 <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>"
675 name="<?php echo $this->get_field_name( 'title' ); ?>" type="text"
676 value="<?php echo esc_attr( $title ); ?>"/>
677 </label>
678 </p>
679 <p>
680 <label for="<?php echo $this->get_field_id( 'subscribe_text' ); ?>">
681 <?php _e( 'Optional text to display to your readers:', 'jetpack' ); ?>
682 <textarea class="widefat" id="<?php echo $this->get_field_id( 'subscribe_text' ); ?>"
683 name="<?php echo $this->get_field_name( 'subscribe_text' ); ?>"
684 rows="3"><?php echo esc_html( $subscribe_text ); ?></textarea>
685 </label>
686 </p>
687 <p>
688 <label for="<?php echo $this->get_field_id( 'subscribe_placeholder' ); ?>">
689 <?php esc_html_e( 'Subscribe Placeholder:', 'jetpack' ); ?>
690 <input class="widefat" id="<?php echo $this->get_field_id( 'subscribe_placeholder' ); ?>"
691 name="<?php echo $this->get_field_name( 'subscribe_placeholder' ); ?>" type="text"
692 value="<?php echo esc_attr( $subscribe_placeholder ); ?>"/>
693 </label>
694 </p>
695 <p>
696 <label for="<?php echo $this->get_field_id( 'subscribe_button' ); ?>">
697 <?php _e( 'Subscribe Button:', 'jetpack' ); ?>
698 <input class="widefat" id="<?php echo $this->get_field_id( 'subscribe_button' ); ?>"
699 name="<?php echo $this->get_field_name( 'subscribe_button' ); ?>" type="text"
700 value="<?php echo esc_attr( $subscribe_button ); ?>"/>
701 </label>
702 </p>
703 <p>
704 <label for="<?php echo $this->get_field_id( 'success_message' ); ?>">
705 <?php _e( 'Success Message Text:', 'jetpack' ); ?>
706 <textarea class="widefat" id="<?php echo $this->get_field_id( 'success_message' ); ?>"
707 name="<?php echo $this->get_field_name( 'success_message' ); ?>"
708 rows="5"><?php echo esc_html( $success_message ); ?></textarea>
709 </label>
710 </p>
711 <p>
712 <label for="<?php echo $this->get_field_id( 'show_subscribers_total' ); ?>">
713 <input type="checkbox" id="<?php echo $this->get_field_id( 'show_subscribers_total' ); ?>"
714 name="<?php echo $this->get_field_name( 'show_subscribers_total' ); ?>"
715 value="1"<?php echo $show_subscribers_total; ?> />
716 <?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 ) ); ?>
717 </label>
718 </p>
719 <?php endif;
720 }
721 }
722
723 if ( defined( 'IS_WPCOM' ) && IS_WPCOM && function_exists( 'class_alias' ) ) {
724 class_alias( 'Jetpack_Subscriptions_Widget', 'Blog_Subscription_Widget' );
725 }
726
727 function get_jetpack_blog_subscriptions_widget_classname() {
728 return ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ?
729 'Blog_Subscription_Widget' :
730 'Jetpack_Subscriptions_Widget';
731 }
732
733 function jetpack_do_subscription_form( $instance ) {
734 if ( empty( $instance ) || ! is_array( $instance ) ) {
735 $instance = array();
736 }
737
738 if ( empty( $instance['show_subscribers_total'] ) || 'false' === $instance['show_subscribers_total'] ) {
739 $instance['show_subscribers_total'] = false;
740 } else {
741 $instance['show_subscribers_total'] = true;
742 }
743
744 $show_only_email_and_button = isset( $instance['show_only_email_and_button'] ) ? $instance['show_only_email_and_button'] : false;
745 $submit_button_text = isset( $instance['submit_button_text'] ) ? $instance['submit_button_text'] : '';
746
747
748
749 // Build up a string with the submit button's classes and styles and set it on the instance
750 $submit_button_classes = isset( $instance['submit_button_classes'] ) ? $instance['submit_button_classes'] : '';
751 $submit_button_styles = '';
752 if ( isset( $instance['custom_background_button_color'] ) ) {
753 $submit_button_styles .= 'background-color: ' . $instance['custom_background_button_color'] . '; ';
754 }
755 if ( isset( $instance['custom_text_button_color'] ) ) {
756 $submit_button_styles .= 'color: ' . $instance['custom_text_button_color'] . ';';
757 }
758
759 $instance = shortcode_atts(
760 Jetpack_Subscriptions_Widget::defaults(),
761 $instance,
762 'jetpack_subscription_form'
763 );
764
765 // These must come after the call to shortcode_atts()
766 $instance['submit_button_text'] = $submit_button_text;
767 $instance['show_only_email_and_button'] = $show_only_email_and_button;
768 if ( ! empty( $submit_button_classes ) ) {
769 $instance['submit_button_classes'] = $submit_button_classes;
770 }
771 if ( ! empty ( $submit_button_styles ) ) {
772 $instance['submit_button_styles'] = $submit_button_styles;
773 }
774
775 $args = array(
776 'before_widget' => '<div class="jetpack_subscription_widget">',
777 );
778 ob_start();
779 the_widget( get_jetpack_blog_subscriptions_widget_classname(), $instance, $args );
780 $output = ob_get_clean();
781
782 return $output;
783 }
784
785 add_shortcode( 'jetpack_subscription_form', 'jetpack_do_subscription_form' );
786 add_shortcode( 'blog_subscription_form', 'jetpack_do_subscription_form' );
787
788 function jetpack_blog_subscriptions_init() {
789 register_widget( get_jetpack_blog_subscriptions_widget_classname() );
790 }
791
792 add_action( 'widgets_init', 'jetpack_blog_subscriptions_init' );
793
794 function jetpack_register_subscriptions_block() {
795 if ( class_exists( 'WP_Block_Type_Registry' ) && ! WP_Block_Type_Registry::get_instance()->is_registered( 'jetpack/subscriptions' ) ) {
796 jetpack_register_block( 'jetpack/subscriptions' );
797 }
798 }
799
800 add_action( 'init', 'jetpack_register_subscriptions_block' );
801