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

1,000 lines 39.7 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 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
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" style="border: 1px solid%2$s; padding-left: 5px; padding-right: 5px; margin-bottom: 10px;">%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 = ( is_ssl() ? 'https' : 'http' ) . '://' . $_SERVER['HTTP_HOST'] . $_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 <form
358 action="<?php echo esc_url( $url ); ?>"
359 method="post"
360 accept-charset="utf-8"
361 id="<?php echo esc_attr( $form_id ); ?>"
362 >
363 <?php
364 if ( ! $show_only_email_and_button ) {
365 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
366 echo wpautop( $subscribe_text );
367 }
368 if ( $show_subscribers_total && $subscribers_total ) {
369 ?>
370 <div class="jetpack-subscribe-count">
371 <p>
372 <?php
373 /* translators: %s: number of folks following the blog */
374 echo esc_html( sprintf( _n( 'Join %s other follower', 'Join %s other followers', $subscribers_total, 'jetpack' ), number_format_i18n( $subscribers_total ) ) );
375 ?>
376 </p>
377 </div>
378 <?php
379 }
380 $email_field_id = 'subscribe-field';
381 $email_field_id .= self::$instance_count > 1
382 ? '-' . self::$instance_count
383 : '';
384 $label_field_id = $email_field_id . '-label';
385 ?>
386 <p id="subscribe-email">
387 <label
388 id="<?php echo esc_attr( $label_field_id ); ?>"
389 for="<?php echo esc_attr( $email_field_id ); ?>"
390 class="screen-reader-text"
391 >
392 <?php echo esc_html__( 'Email Address:', 'jetpack' ); ?>
393 </label>
394
395 <?php
396 printf(
397 '<input
398 type="email"
399 name="email"
400 %1$s
401 style="%2$s"
402 placeholder="%3$s"
403 value=""
404 id="%4$s"
405 />',
406 ( ! empty( $email_field_classes )
407 ? 'class="' . esc_attr( $email_field_classes ) . '"'
408 : ''
409 ),
410 ( ! empty( $email_field_styles )
411 ? esc_attr( $email_field_styles )
412 : 'width: 95%; padding: 1px 10px'
413 ),
414 esc_attr__( 'Enter your email address', 'jetpack' ),
415 esc_attr( $email_field_id )
416 );
417 ?>
418 </p>
419
420 <p id="subscribe-submit"
421 <?php if ( ! empty( $submit_button_wrapper_styles ) ) { ?>
422 style="<?php echo esc_attr( $submit_button_wrapper_styles ); ?>"
423 <?php } ?>
424 >
425 <input type="hidden" name="action" value="subscribe"/>
426 <input type="hidden" name="blog_id" value="<?php echo (int) $current_blog->blog_id; ?>"/>
427 <input type="hidden" name="source" value="<?php echo esc_url( $referer ); ?>"/>
428 <input type="hidden" name="sub-type" value="<?php echo esc_attr( $source ); ?>"/>
429 <input type="hidden" name="redirect_fragment" value="<?php echo esc_attr( $form_id ); ?>"/>
430 <?php wp_nonce_field( 'blogsub_subscribe_' . $current_blog->blog_id, '_wpnonce', false ); ?>
431 <button type="submit"
432 <?php if ( ! empty( $submit_button_classes ) ) { ?>
433 class="<?php echo esc_attr( $submit_button_classes ); ?>"
434 <?php } ?>
435 <?php if ( ! empty( $submit_button_styles ) ) { ?>
436 style="<?php echo esc_attr( $submit_button_styles ); ?>"
437 <?php } ?>
438 >
439 <?php
440 echo wp_kses(
441 $subscribe_button,
442 self::$allowed_html_tags_for_submit_button
443 );
444 ?>
445 </button>
446 </p>
447 </form>
448 <?php
449 }
450
451 if ( self::is_jetpack() ) {
452 /**
453 * Filter the subscription form's ID prefix.
454 *
455 * @module subscriptions
456 *
457 * @since 2.7.0
458 *
459 * @param string subscribe-field Subscription form field prefix.
460 * @param int $widget_id Widget ID.
461 */
462 $subscribe_field_id = apply_filters( 'subscribe_field_id', 'subscribe-field', $widget_id );
463
464 $form_id = self::get_redirect_fragment( $widget_id );
465 ?>
466 <form action="#" method="post" accept-charset="utf-8" id="<?php echo esc_attr( $form_id ); ?>">
467 <?php
468 if ( $subscribe_text && ( ! isset( $_GET['subscribe'] ) || 'success' !== $_GET['subscribe'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Non-sensitive informational output.
469 ?>
470 <div id="subscribe-text"><?php echo wp_kses( wpautop( str_replace( '[total-subscribers]', number_format_i18n( $subscribers_total['value'] ), $subscribe_text ) ), 'post' ); ?></div>
471 <?php
472 }
473
474 if ( $show_subscribers_total && 0 < $subscribers_total['value'] ) {
475 ?>
476 <div class="jetpack-subscribe-count">
477 <p>
478 <?php
479 /* translators: %s: number of folks following the blog */
480 echo esc_html( sprintf( _n( 'Join %s other subscriber', 'Join %s other subscribers', $subscribers_total['value'], 'jetpack' ), number_format_i18n( $subscribers_total['value'] ) ) );
481 ?>
482 </p>
483 </div>
484 <?php
485 }
486 if ( ! isset( $_GET['subscribe'] ) || 'success' !== $_GET['subscribe'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Display of unsubmitted form.
487 ?>
488 <p id="subscribe-email">
489 <label id="jetpack-subscribe-label"
490 class="screen-reader-text"
491 for="<?php echo esc_attr( $subscribe_field_id . '-' . $widget_id ); ?>">
492 <?php echo ! empty( $subscribe_placeholder ) ? esc_html( $subscribe_placeholder ) : esc_html__( 'Email Address:', 'jetpack' ); ?>
493 </label>
494 <input type="email" name="email" required="required"
495 <?php if ( ! empty( $email_field_classes ) ) { ?>
496 class="<?php echo esc_attr( $email_field_classes ); ?> required"
497 <?php } ?>
498 <?php if ( ! empty( $email_field_styles ) ) { ?>
499 style="<?php echo esc_attr( $email_field_styles ); ?>"
500 <?php } ?>
501 value="<?php echo esc_attr( $subscribe_email ); ?>"
502 id="<?php echo esc_attr( $subscribe_field_id . '-' . $widget_id ); ?>"
503 placeholder="<?php echo esc_attr( $subscribe_placeholder ); ?>"
504 />
505 </p>
506
507 <p id="subscribe-submit"
508 <?php if ( ! empty( $submit_button_wrapper_styles ) ) { ?>
509 style="<?php echo esc_attr( $submit_button_wrapper_styles ); ?>"
510 <?php } ?>
511 >
512 <input type="hidden" name="action" value="subscribe"/>
513 <input type="hidden" name="source" value="<?php echo esc_url( $referer ); ?>"/>
514 <input type="hidden" name="sub-type" value="<?php echo esc_attr( $source ); ?>"/>
515 <input type="hidden" name="redirect_fragment" value="<?php echo esc_attr( $form_id ); ?>"/>
516 <?php
517 if ( is_user_logged_in() ) {
518 wp_nonce_field( 'blogsub_subscribe_' . get_current_blog_id(), '_wpnonce', false );
519 }
520 ?>
521 <button type="submit"
522 <?php if ( ! empty( $submit_button_classes ) ) { ?>
523 class="<?php echo esc_attr( $submit_button_classes ); ?>"
524 <?php } ?>
525 <?php if ( ! empty( $submit_button_styles ) ) { ?>
526 style="<?php echo esc_attr( $submit_button_styles ); ?>"
527 <?php } ?>
528 name="jetpack_subscriptions_widget"
529 >
530 <?php
531 echo wp_kses(
532 $subscribe_button,
533 self::$allowed_html_tags_for_submit_button
534 );
535 ?>
536 </button>
537 </p>
538 <?php } ?>
539 </form>
540 <?php
541 }
542 }
543
544 /**
545 * Determines if the current user is subscribed to the blog.
546 *
547 * @return bool Is the person already subscribed.
548 */
549 public static function is_current_user_subscribed() {
550 $subscribed = isset( $_GET['subscribe'] ) && 'success' === $_GET['subscribe']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
551
552 if ( self::is_wpcom() && class_exists( 'Blog_Subscription' ) && class_exists( 'Blog_Subscriber' ) ) {
553 $subscribed = is_user_logged_in() && Blog_Subscription::is_subscribed( new Blog_Subscriber() );
554 }
555
556 return $subscribed;
557 }
558
559 /**
560 * Is this script running in the wordpress.com environment?
561 *
562 * @return bool
563 */
564 public static function is_wpcom() {
565 return defined( 'IS_WPCOM' ) && IS_WPCOM;
566 }
567
568 /**
569 * Is this script running in a self-hosted environment?
570 *
571 * @return bool
572 */
573 public static function is_jetpack() {
574 return ! self::is_wpcom();
575 }
576
577 /**
578 * Used to determine if there is a valid status slug within the wordpress.com environment.
579 *
580 * @return bool
581 */
582 public static function wpcom_has_status_message() {
583 return isset( $_GET['blogsub'] ) && // phpcs:ignore WordPress.Security.NonceVerification.Recommended
584 in_array(
585 $_GET['blogsub'], // phpcs:ignore WordPress.Security.NonceVerification.Recommended
586 array(
587 'confirming',
588 'blocked',
589 'flooded',
590 'spammed',
591 'subscribed',
592 'pending',
593 'confirmed',
594 ),
595 true
596 );
597 }
598
599 /**
600 * Determine the amount of folks currently subscribed to the blog.
601 *
602 * @return int|array
603 */
604 public static function fetch_subscriber_count() {
605 $subs_count = 0;
606
607 if ( self::is_jetpack() ) {
608 $subs_count = get_transient( 'wpcom_subscribers_total' );
609 if ( false === $subs_count || 'failed' === $subs_count['status'] ) {
610 $xml = new Jetpack_IXR_Client();
611
612 $xml->query( 'jetpack.fetchSubscriberCount' );
613
614 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.
615 $subs_count = array(
616 'status' => 'failed',
617 'code' => $xml->getErrorCode(),
618 'message' => $xml->getErrorMessage(),
619 'value' => ( isset( $subs_count['value'] ) ) ? $subs_count['value'] : 0,
620 );
621 } else {
622 $subs_count = array(
623 'status' => 'success',
624 'value' => $xml->getResponse(),
625 );
626 }
627
628 set_transient( 'wpcom_subscribers_total', $subs_count, 3600 ); // Try to cache the result for at least 1 hour.
629 }
630 }
631
632 if ( self::is_wpcom() && function_exists( 'wpcom_reach_total_for_blog' ) ) {
633 $subs_count = wpcom_reach_total_for_blog();
634 }
635
636 return $subs_count;
637 }
638
639 /**
640 * Updates a particular instance of a widget when someone saves it in wp-admin.
641 *
642 * @param array $new_instance New widget instance settings.
643 * @param array $old_instance Old widget instance settings.
644 *
645 * @return array
646 */
647 public function update( $new_instance, $old_instance ) {
648 $instance = $old_instance;
649
650 if ( self::is_jetpack() ) {
651 $instance['title'] = wp_kses( stripslashes( $new_instance['title'] ), array() );
652 $instance['subscribe_placeholder'] = wp_kses( stripslashes( $new_instance['subscribe_placeholder'] ), array() );
653 $instance['subscribe_button'] = wp_kses( stripslashes( $new_instance['subscribe_button'] ), array() );
654 $instance['success_message'] = wp_kses( stripslashes( $new_instance['success_message'] ), array() );
655 }
656
657 if ( self::is_wpcom() ) {
658 $instance['title'] = wp_strip_all_tags( stripslashes( $new_instance['title'] ) );
659 $instance['title_following'] = wp_strip_all_tags( stripslashes( $new_instance['title_following'] ) );
660 $instance['subscribe_logged_in'] = wp_filter_post_kses( stripslashes( $new_instance['subscribe_logged_in'] ) );
661 $instance['subscribe_button'] = wp_strip_all_tags( stripslashes( $new_instance['subscribe_button'] ) );
662 }
663
664 $instance['show_subscribers_total'] = isset( $new_instance['show_subscribers_total'] ) && $new_instance['show_subscribers_total'];
665 $instance['show_only_email_and_button'] = isset( $new_instance['show_only_email_and_button'] ) && $new_instance['show_only_email_and_button'];
666 $instance['subscribe_text'] = wp_filter_post_kses( stripslashes( $new_instance['subscribe_text'] ) );
667
668 return $instance;
669 }
670
671 /**
672 * The default args for rendering a subscription form.
673 *
674 * @return array
675 */
676 public static function defaults() {
677 $defaults = array(
678 'show_subscribers_total' => true,
679 'show_only_email_and_button' => false,
680 );
681
682 $defaults['title'] = esc_html__( 'Subscribe to Blog via Email', 'jetpack' );
683 $defaults['subscribe_text'] = esc_html__( 'Enter your email address to subscribe to this blog and receive notifications of new posts by email.', 'jetpack' );
684 $defaults['subscribe_placeholder'] = esc_html__( 'Email Address', 'jetpack' );
685 $defaults['subscribe_button'] = esc_html__( 'Subscribe', 'jetpack' );
686 $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' );
687
688 return $defaults;
689 }
690
691 /**
692 * Renders the widget's options form in wp-admin.
693 *
694 * @param array $instance Widget instance.
695 */
696 public function form( $instance ) {
697 $instance = wp_parse_args( (array) $instance, $this->defaults() );
698 $show_subscribers_total = checked( $instance['show_subscribers_total'], true, false );
699
700 if ( self::is_wpcom() ) {
701 $title = esc_attr( stripslashes( $instance['title'] ) );
702 $title_following = esc_attr( stripslashes( $instance['title_following'] ) );
703 $subscribe_text = esc_attr( stripslashes( $instance['subscribe_text'] ) );
704 $subscribe_logged_in = esc_attr( stripslashes( $instance['subscribe_logged_in'] ) );
705 $subscribe_button = esc_attr( stripslashes( $instance['subscribe_button'] ) );
706 $subscribers_total = self::fetch_subscriber_count();
707 }
708
709 if ( self::is_jetpack() ) {
710 $title = stripslashes( $instance['title'] );
711 $subscribe_text = stripslashes( $instance['subscribe_text'] );
712 $subscribe_placeholder = stripslashes( $instance['subscribe_placeholder'] );
713 $subscribe_button = stripslashes( $instance['subscribe_button'] );
714 $success_message = stripslashes( $instance['success_message'] );
715 $subs_fetch = self::fetch_subscriber_count();
716 if ( 'failed' === $subs_fetch['status'] ) {
717 printf( '<div class="error inline"><p>%s: %s</p></div>', esc_html( $subs_fetch['code'] ), esc_html( $subs_fetch['message'] ) );
718 }
719 $subscribers_total = number_format_i18n( $subs_fetch['value'] );
720 }
721
722 if ( self::is_wpcom() ) :
723 ?>
724 <p>
725 <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
726 <?php esc_html_e( 'Widget title for non-followers:', 'jetpack' ); ?>
727 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
728 name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text"
729 value="<?php echo esc_attr( $title ); ?>"/>
730 </label>
731 </p>
732 <p>
733 <label for="<?php echo esc_attr( $this->get_field_id( 'title_following' ) ); ?>">
734 <?php esc_html_e( 'Widget title for followers:', 'jetpack' ); ?>
735 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title_following' ) ); ?>"
736 name="<?php echo esc_attr( $this->get_field_name( 'title_following' ) ); ?>" type="text"
737 value="<?php echo esc_attr( $title_following ); ?>"/>
738 </label>
739 </p>
740 <p>
741 <label for="<?php echo esc_attr( $this->get_field_id( 'subscribe_logged_in' ) ); ?>">
742 <?php esc_html_e( 'Optional text to display to logged in WordPress.com users:', 'jetpack' ); ?>
743 <textarea style="width: 95%" id="<?php echo esc_attr( $this->get_field_id( 'subscribe_logged_in' ) ); ?>"
744 name="<?php echo esc_attr( $this->get_field_name( 'subscribe_logged_in' ) ); ?>"
745 type="text"><?php echo esc_html( $subscribe_logged_in ); ?></textarea>
746 </label>
747 </p>
748 <p>
749 <label for="<?php echo esc_attr( $this->get_field_id( 'subscribe_text' ) ); ?>">
750 <?php esc_html_e( 'Optional text to display to non-WordPress.com users:', 'jetpack' ); ?>
751 <textarea style="width: 95%" id="<?php echo esc_attr( $this->get_field_id( 'subscribe_text' ) ); ?>"
752 name="<?php echo esc_attr( $this->get_field_name( 'subscribe_text' ) ); ?>"
753 type="text"><?php echo esc_html( $subscribe_text ); ?></textarea>
754 </label>
755 </p>
756 <p>
757 <label for="<?php echo esc_attr( $this->get_field_id( 'subscribe_button' ) ); ?>">
758 <?php esc_html_e( 'Follow Button Text:', 'jetpack' ); ?>
759 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'subscribe_button' ) ); ?>"
760 name="<?php echo esc_attr( $this->get_field_name( 'subscribe_button' ) ); ?>" type="text"
761 value="<?php echo esc_attr( $subscribe_button ); ?>"/>
762 </label>
763 </p>
764 <p>
765 <label for="<?php echo esc_attr( $this->get_field_id( 'show_subscribers_total' ) ); ?>">
766 <input type="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'show_subscribers_total' ) ); ?>"
767 name="<?php echo esc_attr( $this->get_field_name( 'show_subscribers_total' ) ); ?>"
768 value="1"<?php echo esc_attr( $show_subscribers_total ); ?> />
769 <?php
770 /* translators: %s: Number of followers. */
771 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 ) ) );
772 ?>
773 </label>
774 </p>
775 <?php
776 endif;
777
778 if ( self::is_jetpack() ) :
779 ?>
780 <p>
781 <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
782 <?php esc_html_e( 'Widget title:', 'jetpack' ); ?>
783 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
784 name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text"
785 value="<?php echo esc_attr( $title ); ?>"/>
786 </label>
787 </p>
788 <p>
789 <label for="<?php echo esc_attr( $this->get_field_id( 'subscribe_text' ) ); ?>">
790 <?php esc_html_e( 'Optional text to display to your readers:', 'jetpack' ); ?>
791 <textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'subscribe_text' ) ); ?>"
792 name="<?php echo esc_attr( $this->get_field_name( 'subscribe_text' ) ); ?>"
793 rows="3"><?php echo esc_html( $subscribe_text ); ?></textarea>
794 </label>
795 </p>
796 <p>
797 <label for="<?php echo esc_attr( $this->get_field_id( 'subscribe_placeholder' ) ); ?>">
798 <?php esc_html_e( 'Subscribe Placeholder:', 'jetpack' ); ?>
799 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'subscribe_placeholder' ) ); ?>"
800 name="<?php echo esc_attr( $this->get_field_name( 'subscribe_placeholder' ) ); ?>" type="text"
801 value="<?php echo esc_attr( $subscribe_placeholder ); ?>"/>
802 </label>
803 </p>
804 <p>
805 <label for="<?php echo esc_attr( $this->get_field_id( 'subscribe_button' ) ); ?>">
806 <?php esc_html_e( 'Subscribe Button:', 'jetpack' ); ?>
807 <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'subscribe_button' ) ); ?>"
808 name="<?php echo esc_attr( $this->get_field_name( 'subscribe_button' ) ); ?>" type="text"
809 value="<?php echo esc_attr( $subscribe_button ); ?>"/>
810 </label>
811 </p>
812 <p>
813 <label for="<?php echo esc_attr( $this->get_field_id( 'success_message' ) ); ?>">
814 <?php esc_html_e( 'Success Message Text:', 'jetpack' ); ?>
815 <textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'success_message' ) ); ?>"
816 name="<?php echo esc_attr( $this->get_field_name( 'success_message' ) ); ?>"
817 rows="5"><?php echo esc_html( $success_message ); ?></textarea>
818 </label>
819 </p>
820 <p>
821 <label for="<?php echo esc_attr( $this->get_field_id( 'show_subscribers_total' ) ); ?>">
822 <input type="checkbox" id="<?php echo esc_attr( $this->get_field_id( 'show_subscribers_total' ) ); ?>"
823 name="<?php echo esc_attr( $this->get_field_name( 'show_subscribers_total' ) ); ?>"
824 value="1"<?php echo esc_attr( $show_subscribers_total ); ?> />
825 <?php
826 /* translators: %s: Number of subscribers. */
827 echo esc_html( sprintf( _n( 'Show total number of subscribers? (%s subscriber)', 'Show total number of subscribers? (%s subscribers)', $subscribers_total, 'jetpack' ), $subscribers_total ) );
828 ?>
829 </label>
830 </p>
831 <?php
832 endif;
833 }
834 }
835
836 if ( defined( 'IS_WPCOM' ) && IS_WPCOM && function_exists( 'class_alias' ) ) {
837 class_alias( 'Jetpack_Subscriptions_Widget', 'Blog_Subscription_Widget' );
838 }
839
840 /**
841 * Classname / shortcode tag to use for the Subscriptions widget.
842 *
843 * @return string
844 */
845 function get_jetpack_blog_subscriptions_widget_classname() {
846 return ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ?
847 'Blog_Subscription_Widget' :
848 'Jetpack_Subscriptions_Widget';
849 }
850
851 /**
852 * Subscriptions widget form HTML output.
853 *
854 * @param array $instance Widget instance data.
855 */
856 function jetpack_do_subscription_form( $instance ) {
857 if ( empty( $instance ) || ! is_array( $instance ) ) {
858 $instance = array();
859 }
860
861 if ( empty( $instance['show_subscribers_total'] ) || 'false' === $instance['show_subscribers_total'] ) {
862 $instance['show_subscribers_total'] = false;
863 } else {
864 $instance['show_subscribers_total'] = true;
865 }
866
867 $show_only_email_and_button = isset( $instance['show_only_email_and_button'] ) ? $instance['show_only_email_and_button'] : false;
868 $submit_button_text = isset( $instance['submit_button_text'] ) ? $instance['submit_button_text'] : '';
869
870 // Build up a string with the submit button's classes and styles and set it on the instance.
871 $submit_button_classes = isset( $instance['submit_button_classes'] ) ? $instance['submit_button_classes'] : '';
872 $email_field_classes = isset( $instance['email_field_classes'] ) ? $instance['email_field_classes'] : '';
873 $style = '';
874 $submit_button_styles = '';
875 $submit_button_wrapper_styles = '';
876 $email_field_styles = '';
877 $success_message = '';
878
879 if ( isset( $instance['custom_background_button_color'] ) && 'undefined' !== $instance['custom_background_button_color'] ) {
880 $submit_button_styles .= 'background: ' . $instance['custom_background_button_color'] . '; ';
881 }
882 if ( isset( $instance['custom_text_button_color'] ) && 'undefined' !== $instance['custom_text_button_color'] ) {
883 $submit_button_styles .= 'color: ' . $instance['custom_text_button_color'] . '; ';
884 }
885 if ( isset( $instance['custom_button_width'] ) && 'undefined' !== $instance['custom_button_width'] ) {
886 $submit_button_wrapper_styles .= 'width: ' . $instance['custom_button_width'] . '; ';
887 $submit_button_wrapper_styles .= 'max-width: 100%; ';
888
889 // Account for custom margins on inline forms.
890 if (
891 ! empty( $instance['custom_spacing'] ) &&
892 ! ( isset( $instance['button_on_newline'] ) && 'true' === $instance['button_on_newline'] )
893 ) {
894 $submit_button_styles .= 'width: calc(100% - ' . $instance['custom_spacing'] . 'px); ';
895 } else {
896 $submit_button_styles .= 'width: 100%; ';
897 }
898 }
899
900 if ( isset( $instance['custom_font_size'] ) && 'undefined' !== $instance['custom_font_size'] ) {
901 $style = 'font-size: ' . $instance['custom_font_size'];
902 $style .= is_numeric( $instance['custom_font_size'] ) ? 'px; ' : '; '; // Handle deprecated numeric font size values.
903
904 $submit_button_styles .= $style;
905 $email_field_styles .= $style;
906 }
907 if ( isset( $instance['custom_padding'] ) && 'undefined' !== $instance['custom_padding'] ) {
908 $style = 'padding: ' .
909 $instance['custom_padding'] . 'px ' .
910 round( $instance['custom_padding'] * 1.5 ) . 'px ' .
911 $instance['custom_padding'] . 'px ' .
912 round( $instance['custom_padding'] * 1.5 ) . 'px; ';
913
914 $submit_button_styles .= $style;
915 $email_field_styles .= $style;
916 }
917
918 $button_spacing = 0;
919 if ( ! empty( $instance['custom_spacing'] ) ) {
920 $button_spacing = $instance['custom_spacing'];
921 }
922 if ( isset( $instance['button_on_newline'] ) && 'true' === $instance['button_on_newline'] ) {
923 $submit_button_styles .= 'margin-top: ' . $button_spacing . 'px; ';
924 } else {
925 $submit_button_styles .= 'margin-left: ' . $button_spacing . 'px; ';
926 }
927
928 if ( isset( $instance['custom_border_radius'] ) && 'undefined' !== $instance['custom_border_radius'] ) {
929 $style = 'border-radius: ' . $instance['custom_border_radius'] . 'px; ';
930 $submit_button_styles .= $style;
931 $email_field_styles .= $style;
932 }
933 if ( isset( $instance['custom_border_weight'] ) && 'undefined' !== $instance['custom_border_weight'] ) {
934 $style = 'border-width: ' . $instance['custom_border_weight'] . 'px; ';
935 $submit_button_styles .= $style;
936 $email_field_styles .= $style;
937 }
938 if ( isset( $instance['custom_border_color'] ) && 'undefined' !== $instance['custom_border_color'] ) {
939 $style =
940 'border-color: ' . $instance['custom_border_color'] . '; ' .
941 'border-style: solid;';
942
943 $submit_button_styles .= $style;
944 $email_field_styles .= $style;
945 }
946 if ( isset( $instance['success_message'] ) && 'undefined' !== $instance['success_message'] ) {
947 $success_message = wp_kses( stripslashes( $instance['success_message'] ), array() );
948 }
949
950 $instance = shortcode_atts(
951 Jetpack_Subscriptions_Widget::defaults(),
952 $instance,
953 'jetpack_subscription_form'
954 );
955
956 // These must come after the call to shortcode_atts().
957 $instance['submit_button_text'] = $submit_button_text;
958 $instance['show_only_email_and_button'] = $show_only_email_and_button;
959 if ( ! empty( $submit_button_classes ) ) {
960 $instance['submit_button_classes'] = $submit_button_classes;
961 }
962 if ( ! empty( $email_field_classes ) ) {
963 $instance['email_field_classes'] = $email_field_classes;
964 }
965
966 if ( ! empty( $submit_button_styles ) ) {
967 $instance['submit_button_styles'] = trim( $submit_button_styles );
968 }
969 if ( ! empty( $submit_button_wrapper_styles ) ) {
970 $instance['submit_button_wrapper_styles'] = trim( $submit_button_wrapper_styles );
971 }
972 if ( ! empty( $email_field_styles ) ) {
973 $instance['email_field_styles'] = trim( $email_field_styles );
974 }
975 if ( ! empty( $success_message ) ) {
976 $instance['success_message'] = trim( $success_message );
977 }
978
979 $args = array(
980 'before_widget' => '<div class="jetpack_subscription_widget">',
981 );
982 ob_start();
983 the_widget( get_jetpack_blog_subscriptions_widget_classname(), $instance, $args );
984 $output = ob_get_clean();
985
986 return $output;
987 }
988
989 add_shortcode( 'jetpack_subscription_form', 'jetpack_do_subscription_form' );
990 add_shortcode( 'blog_subscription_form', 'jetpack_do_subscription_form' );
991
992 /**
993 * Register the Subscriptions widget.
994 */
995 function jetpack_blog_subscriptions_init() {
996 register_widget( get_jetpack_blog_subscriptions_widget_classname() );
997 }
998
999 add_action( 'widgets_init', 'jetpack_blog_subscriptions_init' );
1000