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