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