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