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

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

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