PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 11.4.1
Jetpack – WP Security, Backup, Speed, & Growth v11.4.1
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 11.4.1, at modules/subscriptions/views.php

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