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

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