PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 3.1.5
Jetpack – WP Security, Backup, Speed, & Growth v3.1.5
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.php

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

772 lines 26.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Module Name: Subscriptions
4 * Module Description: Allow users to subscribe to your posts and comments and receive notifications via email.
5 * Sort Order: 9
6 * First Introduced: 1.2
7 * Requires Connection: Yes
8 * Auto Activate: Yes
9 * Module Tags: Social
10 */
11
12 add_action( 'jetpack_modules_loaded', 'jetpack_subscriptions_load' );
13
14 Jetpack_Sync::sync_options(
15 __FILE__,
16 'home',
17 'blogname',
18 'siteurl',
19 'page_on_front',
20 'permalink_structure',
21 'category_base',
22 'rss_use_excerpt',
23 'subscription_options',
24 'stb_enabled',
25 'stc_enabled',
26 'tag_base'
27 );
28
29 Jetpack_Sync::sync_posts( __FILE__ );
30 Jetpack_Sync::sync_comments( __FILE__ );
31
32 function jetpack_subscriptions_load() {
33 Jetpack::enable_module_configurable( __FILE__ );
34 Jetpack::module_configuration_load( __FILE__, 'jetpack_subscriptions_configuration_load' );
35 }
36
37 function jetpack_subscriptions_configuration_load() {
38 wp_safe_redirect( admin_url( 'options-discussion.php#jetpack-subscriptions-settings' ) );
39 exit;
40 }
41
42 class Jetpack_Subscriptions {
43 var $jetpack = false;
44
45 public static $hash;
46
47 /**
48 * Singleton
49 * @static
50 */
51 static function init() {
52 static $instance = false;
53
54 if ( !$instance ) {
55 $instance = new Jetpack_Subscriptions;
56 }
57
58 return $instance;
59 }
60
61 function Jetpack_Subscriptions() {
62 $this->jetpack = Jetpack::init();
63
64 // Don't use COOKIEHASH as it could be shared across installs && is non-unique in multisite.
65 // @see: https://twitter.com/nacin/status/378246957451333632
66 self::$hash = md5( get_option( 'siteurl' ) );
67
68 add_filter( 'jetpack_xmlrpc_methods', array( $this, 'xmlrpc_methods' ) );
69
70 // @todo remove sync from subscriptions and move elsewhere...
71
72 // Add Configuration Page
73 add_action( 'admin_init', array( $this, 'configure' ) );
74
75 // Set up the subscription widget.
76 add_action( 'widgets_init', array( $this, 'widget_init' ) );
77
78 // Catch subscription widget submits
79 if ( isset( $_REQUEST['jetpack_subscriptions_widget'] ) )
80 add_action( 'template_redirect', array( $this, 'widget_submit' ) );
81
82 // Set up the comment subscription checkboxes
83 add_action( 'comment_form', array( $this, 'comment_subscribe_init' ) );
84
85 // Catch comment posts and check for subscriptions.
86 add_action( 'comment_post', array( $this, 'comment_subscribe_submit' ), 50, 2 );
87 }
88
89 function post_is_public( $the_post ) {
90 if ( !$post = get_post( $the_post ) ) {
91 return false;
92 }
93
94 if ( 'publish' === $post->post_status && strlen( (string) $post->post_password ) < 1 ) {
95 return apply_filters( 'jetpack_is_post_mailable', true );
96 }
97 }
98
99 /**
100 * Jetpack_Subscriptions::xmlrpc_methods()
101 *
102 * Register subscriptions methods with the Jetpack XML-RPC server.
103 * @param array $methods
104 */
105 function xmlrpc_methods( $methods ) {
106 return array_merge(
107 $methods,
108 array(
109 'jetpack.subscriptions.subscribe' => array( $this, 'subscribe' ),
110 )
111 );
112 }
113
114 /**
115 * Jetpack_Subscriptions::configure()
116 *
117 * Jetpack Subscriptions configuration screen.
118 */
119 function configure() {
120 // Create the section
121 add_settings_section(
122 'jetpack_subscriptions',
123 __( 'Jetpack Subscriptions Settings', 'jetpack' ),
124 array( $this, 'subscriptions_settings_section' ),
125 'discussion'
126 );
127
128 /** Subscribe to Posts ***************************************************/
129
130 add_settings_field(
131 'jetpack_subscriptions_post_subscribe',
132 __( 'Follow Blog', 'jetpack' ),
133 array( $this, 'subscription_post_subscribe_setting' ),
134 'discussion',
135 'jetpack_subscriptions'
136 );
137
138 register_setting(
139 'discussion',
140 'stb_enabled'
141 );
142
143 /** Subscribe to Comments ******************************************************/
144
145 add_settings_field(
146 'jetpack_subscriptions_comment_subscribe',
147 __( 'Follow Comments', 'jetpack' ),
148 array( $this, 'subscription_comment_subscribe_setting' ),
149 'discussion',
150 'jetpack_subscriptions'
151 );
152
153 register_setting(
154 'discussion',
155 'stc_enabled'
156 );
157
158 /** Subscription Messaging Options ******************************************************/
159
160 register_setting(
161 'reading',
162 'subscription_options',
163 array( $this, 'validate_settings' )
164 );
165
166 add_settings_section(
167 'email_settings',
168 __( 'Follower Settings', 'jetpack' ),
169 array( $this, 'reading_section' ),
170 'reading'
171 );
172
173 add_settings_field(
174 'invitation',
175 __( 'Blog follow email text', 'jetpack' ),
176 array( $this, 'setting_invitation' ),
177 'reading',
178 'email_settings'
179 );
180
181 add_settings_field(
182 'comment-follow',
183 __( 'Comment follow email text', 'jetpack' ),
184 array( $this, 'setting_comment_follow' ),
185 'reading',
186 'email_settings'
187 );
188 }
189
190 /**
191 * Discussions setting section blurb
192 *
193 */
194 function subscriptions_settings_section() {
195 ?>
196
197 <p id="jetpack-subscriptions-settings"><?php _e( 'Change whether your visitors can subscribe to your posts or comments or both.', 'jetpack' ); ?></p>
198
199 <?php
200 }
201
202 /**
203 * Post Subscriptions Toggle
204 *
205 */
206 function subscription_post_subscribe_setting() {
207
208 $stb_enabled = get_option( 'stb_enabled', 1 ); ?>
209
210 <p class="description">
211 <input type="checkbox" name="stb_enabled" id="jetpack-post-subscribe" value="1" <?php checked( $stb_enabled, 1 ); ?> />
212 <?php _e( "Show a <em>'follow blog'</em> option in the comment form", 'jetpack' ); ?>
213 </p>
214 <?php
215 }
216
217 /**
218 * Comments Subscriptions Toggle
219 *
220 */
221 function subscription_comment_subscribe_setting() {
222
223 $stc_enabled = get_option( 'stc_enabled', 1 ); ?>
224
225 <p class="description">
226 <input type="checkbox" name="stc_enabled" id="jetpack-comment-subscribe" value="1" <?php checked( $stc_enabled, 1 ); ?> />
227 <?php _e( "Show a <em>'follow comments'</em> option in the comment form", 'jetpack' ); ?>
228 </p>
229
230 <?php
231 }
232
233 function validate_settings( $settings ) {
234 global $allowedposttags;
235
236 $default = $this->get_default_settings();
237
238 // Blog Follow
239 $settings['invitation'] = trim( wp_kses( $settings['invitation'], $allowedposttags ) );
240 if ( empty( $settings['invitation'] ) )
241 $settings['invitation'] = $default['invitation'];
242
243 // Comments Follow (single post)
244 $settings['comment_follow'] = trim( wp_kses( $settings['comment_follow'], $allowedposttags ) );
245 if ( empty( $settings['comment_follow'] ) )
246 $settings['comment_follow'] = $default['comment_follow'];
247
248 return $settings;
249 }
250
251 public function reading_section() {
252 echo '<p id="follower-settings">';
253 _e( 'These settings change emails sent from your blog to followers.', 'jetpack' );
254 echo '</p>';
255 }
256
257 public function setting_invitation() {
258 $settings = $this->get_settings();
259 echo '<textarea name="subscription_options[invitation]" class="large-text" cols="50" rows="5">' . esc_textarea( $settings['invitation'] ) . '</textarea>';
260 echo '<p><span class="description">'.__( 'Introduction text sent when someone follows your blog. (Site and confirmation details will be automatically added for you.)', 'jetpack' ).'</span></p>';
261 }
262
263 public function setting_comment_follow() {
264 $settings = $this->get_settings();
265 echo '<textarea name="subscription_options[comment_follow]" class="large-text" cols="50" rows="5">' . esc_textarea( $settings['comment_follow'] ) . '</textarea>';
266 echo '<p><span class="description">'.__( 'Introduction text sent when someone follows a post on your blog. (Site and confirmation details will be automatically added for you.)', 'jetpack' ).'</span></p>';
267 }
268
269 function get_default_settings() {
270 return array(
271 'invitation' => __( "Howdy.\n\nYou recently followed this blog's posts. This means you will receive each new post by email.\n\nTo activate, click confirm below. If you believe this is an error, ignore this message and we'll never bother you again.", 'jetpack' ),
272 'comment_follow' => __( "Howdy.\n\nYou recently followed one of my posts. This means you will receive an email when new comments are posted.\n\nTo activate, click confirm below. If you believe this is an error, ignore this message and we'll never bother you again.", 'jetpack' )
273 );
274 }
275
276 function get_settings() {
277 return wp_parse_args( (array) get_option( 'subscription_options', array() ), $this->get_default_settings() );
278 }
279
280 /**
281 * Jetpack_Subscriptions::subscribe()
282 *
283 * Send a synchronous XML-RPC subscribe to blog posts or subscribe to post comments request.
284 *
285 * @param string $email
286 * @param array $post_ids (optional) defaults to 0 for blog posts only: array of post IDs to subscribe to blog's posts
287 * @param bool $async (optional) Should the subscription be performed asynchronously? Defaults to true.
288 *
289 * @return true|Jetpack_Error true on success
290 * invalid_email : not a valid email address
291 * invalid_post_id : not a valid post ID
292 * unknown_post_id : unknown post
293 * not_subscribed : strange error. Jetpack servers at WordPress.com could subscribe the email.
294 * disabled : Site owner has disabled subscriptions.
295 * active : Already subscribed.
296 * unknown : strange error. Jetpack servers at WordPress.com returned something malformed.
297 * unknown_status : strange error. Jetpack servers at WordPress.com returned something I didn't understand.
298 */
299 function subscribe( $email, $post_ids = 0, $async = true, $extra_data = array() ) {
300 if ( !is_email( $email ) ) {
301 return new Jetpack_Error( 'invalid_email' );
302 }
303
304 if ( !$async ) {
305 Jetpack::load_xml_rpc_client();
306 $xml = new Jetpack_IXR_ClientMulticall();
307 }
308
309 foreach ( (array) $post_ids as $post_id ) {
310 $post_id = (int) $post_id;
311 if ( $post_id < 0 ) {
312 return new Jetpack_Error( 'invalid_post_id' );
313 } else if ( $post_id && !$post = get_post( $post_id ) ) {
314 return new Jetpack_Error( 'unknown_post_id' );
315 }
316
317 if ( $async ) {
318 Jetpack::xmlrpc_async_call( 'jetpack.subscribeToSite', $email, $post_id, serialize( $extra_data ) );
319 } else {
320 $xml->addCall( 'jetpack.subscribeToSite', $email, $post_id, serialize( $extra_data ) );
321 }
322 }
323
324 if ( $async ) {
325 return;
326 }
327
328 // Call
329 $xml->query();
330
331 if ( $xml->isError() ) {
332 return $xml->get_jetpack_error();
333 }
334
335 $responses = $xml->getResponse();
336
337 $r = array();
338 foreach ( (array) $responses as $response ) {
339 if ( isset( $response['faultCode'] ) || isset( $response['faultString'] ) ) {
340 $r[] = $xml->get_jetpack_error( $response['faultCode'], $response['faultString'] );
341 continue;
342 }
343
344 if ( !is_array( $response[0] ) || empty( $response[0]['status'] ) ) {
345 $r[] = new Jetpack_Error( 'unknown' );
346 continue;
347 }
348
349 switch ( $response[0]['status'] ) {
350 case 'error' :
351 $r[] = new Jetpack_Error( 'not_subscribed' );
352 continue 2;
353 case 'disabled' :
354 $r[] = new Jetpack_Error( 'disabled' );
355 continue 2;
356 case 'active' :
357 $r[] = new Jetpack_Error( 'active' );
358 continue 2;
359 case 'pending' :
360 $r[] = true;
361 continue 2;
362 default :
363 $r[] = new Jetpack_Error( 'unknown_status', (string) $response[0]['status'] );
364 continue 2;
365 }
366 }
367
368 return $r;
369 }
370
371 /**
372 * Jetpack_Subscriptions::widget_init()
373 *
374 * Initialize and register the Jetpack Subscriptions widget.
375 */
376 function widget_init() {
377 register_widget( 'Jetpack_Subscriptions_Widget' );
378 }
379
380 /**
381 * Jetpack_Subscriptions::widget_submit()
382 *
383 * When a user submits their email via the blog subscription widget, check the details and call the subsribe() method.
384 */
385 function widget_submit() {
386 // Check the nonce.
387 if ( is_user_logged_in() ) {
388 check_admin_referer( 'blogsub_subscribe_' . get_current_blog_id() );
389 }
390
391 if ( empty( $_REQUEST['email'] ) )
392 return false;
393
394 $redirect_fragment = false;
395 if ( isset( $_REQUEST['redirect_fragment'] ) ) {
396 $redirect_fragment = preg_replace( '/[^a-z0-9_-]/i', '', $_REQUEST['redirect_fragment'] );
397 }
398 if ( !$redirect_fragment ) {
399 $redirect_fragment = 'subscribe-blog';
400 }
401
402 $subscribe = Jetpack_Subscriptions::subscribe(
403 $_REQUEST['email'],
404 0,
405 false,
406 array(
407 'source' => 'widget',
408 'widget-in-use' => is_active_widget( false, false, 'blog_subscription', true ) ? 'yes' : 'no',
409 'comment_status' => '',
410 'server_data' => $_SERVER,
411 )
412 );
413
414 if ( is_wp_error( $subscribe ) ) {
415 $error = $subscribe->get_error_code();
416 } else {
417 $error = false;
418 foreach ( $subscribe as $response ) {
419 if ( is_wp_error( $response ) ) {
420 $error = $response->get_error_code();
421 break;
422 }
423 }
424 }
425
426 if ( $error ) {
427 switch ( $error ) {
428 case 'invalid_email':
429 $redirect = add_query_arg( 'subscribe', 'invalid_email' );
430 break;
431 case 'active': case 'pending':
432 $redirect = add_query_arg( 'subscribe', 'already' );
433 break;
434 default:
435 $redirect = add_query_arg( 'subscribe', 'error' );
436 break;
437 }
438 } else {
439 $redirect = add_query_arg( 'subscribe', 'success' );
440 }
441
442 wp_safe_redirect( "$redirect#$redirect_fragment" );
443 exit;
444 }
445
446 /**
447 * Jetpack_Subscriptions::comment_subscribe_init()
448 *
449 * Set up and add the comment subscription checkbox to the comment form.
450 */
451 function comment_subscribe_init() {
452 global $post;
453
454 $comments_checked = '';
455 $blog_checked = '';
456
457 // Check for a comment / blog submission and set a cookie to retain the setting and check the boxes.
458 if ( isset( $_COOKIE[ 'jetpack_comments_subscribe_' . self::$hash ] ) && $_COOKIE[ 'jetpack_comments_subscribe_' . self::$hash ] == $post->ID )
459 $comments_checked = ' checked="checked"';
460
461 if ( isset( $_COOKIE[ 'jetpack_blog_subscribe_' . self::$hash ] ) )
462 $blog_checked = ' checked="checked"';
463
464 // Some themes call this function, don't show the checkbox again
465 remove_action( 'comment_form', 'subscription_comment_form' );
466
467 // Check if Mark Jaquith's Subscribe to Comments plugin is active - if so, suppress Jetpack checkbox
468
469 $str = '';
470
471 if ( FALSE === has_filter( 'comment_form', 'show_subscription_checkbox' ) && 1 == get_option( 'stc_enabled', 1 ) ) {
472 // Subscribe to comments checkbox
473 $str .= '<p class="comment-subscription-form"><input type="checkbox" name="subscribe_comments" id="subscribe_comments" value="subscribe" style="width: auto; -moz-appearance: checkbox; -webkit-appearance: checkbox;"' . $comments_checked . ' /> ';
474 $str .= '<label class="subscribe-label" id="subscribe-label" for="subscribe_comments">' . __( 'Notify me of follow-up comments by email.', 'jetpack' ) . '</label>';
475 $str .= '</p>';
476 }
477
478 if ( 1 == get_option( 'stb_enabled', 1 ) ) {
479 // Subscribe to blog checkbox
480 $str .= '<p class="comment-subscription-form"><input type="checkbox" name="subscribe_blog" id="subscribe_blog" value="subscribe" style="width: auto; -moz-appearance: checkbox; -webkit-appearance: checkbox;"' . $blog_checked . ' /> ';
481 $str .= '<label class="subscribe-label" id="subscribe-blog-label" for="subscribe_blog">' . __( 'Notify me of new posts by email.', 'jetpack' ) . '</label>';
482 $str .= '</p>';
483 }
484
485 echo apply_filters( 'jetpack_comment_subscription_form', $str );
486 }
487
488 /**
489 * Jetpack_Subscriptions::comment_subscribe_init()
490 *
491 * When a user checks the comment subscribe box and submits a comment, subscribe them to the comment thread.
492 */
493 function comment_subscribe_submit( $comment_id, $approved ) {
494 if ( 'spam' === $approved ) {
495 return;
496 }
497
498 // Set cookies for this post/comment
499 $this->set_cookies( isset( $_REQUEST['subscribe_comments'] ), isset( $_REQUEST['subscribe_blog'] ) );
500
501 if ( !isset( $_REQUEST['subscribe_comments'] ) && !isset( $_REQUEST['subscribe_blog'] ) )
502 return;
503
504 $comment = get_comment( $comment_id );
505 $post_ids = array();
506
507 if ( isset( $_REQUEST['subscribe_comments'] ) )
508 $post_ids[] = $comment->comment_post_ID;
509
510 if ( isset( $_REQUEST['subscribe_blog'] ) )
511 $post_ids[] = 0;
512
513 Jetpack_Subscriptions::subscribe(
514 $comment->comment_author_email,
515 $post_ids,
516 true,
517 array(
518 'source' => 'comment-form',
519 'widget-in-use' => is_active_widget( false, false, 'blog_subscription', true ) ? 'yes' : 'no',
520 'comment_status' => $approved,
521 'server_data' => $_SERVER,
522 )
523 );
524 }
525
526 /**
527 * Jetpack_Subscriptions::set_cookies()
528 *
529 * Set a cookie to save state on the comment and post subscription checkboxes.
530 */
531 function set_cookies( $comments = true, $posts = true ) {
532 global $post;
533
534 $cookie_lifetime = apply_filters( 'comment_cookie_lifetime', 30000000 );
535 $cookie_path = apply_filters( 'jetpack_comment_cookie_path', COOKIEPATH );
536 $cookie_domain = apply_filters( 'jetpack_comment_cookie_domain', COOKIE_DOMAIN );
537
538 if ( $comments )
539 setcookie( 'jetpack_comments_subscribe_' . self::$hash, $post->ID, time() + $cookie_lifetime, $cookie_path, $cookie_domain );
540 else
541 setcookie( 'jetpack_comments_subscribe_' . self::$hash, '', time() - 3600, $cookie_path, $cookie_domain );
542
543 if ( $posts )
544 setcookie( 'jetpack_blog_subscribe_' . self::$hash, 1, time() + $cookie_lifetime, $cookie_path, $cookie_domain );
545 else
546 setcookie( 'jetpack_blog_subscribe_' . self::$hash, '', time() - 3600, $cookie_path, $cookie_domain );
547 }
548 }
549
550 Jetpack_Subscriptions::init();
551
552
553 /***
554 * Blog Subscription Widget
555 */
556
557 class Jetpack_Subscriptions_Widget extends WP_Widget {
558 function Jetpack_Subscriptions_Widget() {
559 $widget_ops = array( 'classname' => 'jetpack_subscription_widget', 'description' => __( 'Add an email signup form to allow people to subscribe to your blog.', 'jetpack' ) );
560 $control_ops = array( 'width' => 300 );
561
562 $this->WP_Widget( 'blog_subscription', __( 'Blog Subscriptions (Jetpack)', 'jetpack' ), $widget_ops, $control_ops );
563
564 add_action( 'init', array( $this, 'maybe_add_style' ) );
565 }
566
567 function maybe_add_style() {
568 //if ( is_active_widget( false, false, $this->id_base, true ) ) {
569 wp_register_style( 'jetpack-subscriptions', plugins_url( 'subscriptions/subscriptions.css', __FILE__ ) );
570 wp_enqueue_style( 'jetpack-subscriptions' );
571 //}
572 }
573
574 function widget( $args, $instance ) {
575 global $current_user;
576
577
578
579 $source = 'widget';
580 $instance = wp_parse_args( (array) $instance, $this->defaults() );
581 $subscribe_text = isset( $instance['subscribe_text'] ) ? stripslashes( $instance['subscribe_text'] ) : '';
582 $subscribe_button = isset( $instance['subscribe_button'] ) ? stripslashes( $instance['subscribe_button'] ) : '';
583 $show_subscribers_total = (bool) $instance['show_subscribers_total'];
584 $subscribers_total = $this->fetch_subscriber_count();
585 $widget_id = esc_attr( !empty( $args['widget_id'] ) ? esc_attr( $args['widget_id'] ) : mt_rand( 450, 550 ) );
586
587 if ( ! is_array( $subscribers_total ) )
588 $show_subscribers_total = FALSE;
589
590 // Give the input element a unique ID
591 $subscribe_field_id = apply_filters( 'subscribe_field_id', 'subscribe-field', $widget_id );
592
593 echo $args['before_widget'];
594 echo $args['before_title'] . '<label for="' . esc_attr( $subscribe_field_id ) . '">' . esc_attr( $instance['title'] ) . '</label>' . $args['after_title'] . "\n";
595
596 $referer = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
597
598 // Check for subscription confirmation.
599 if ( isset( $_GET['subscribe'] ) && 'success' == $_GET['subscribe'] ) : ?>
600
601 <div class="success">
602 <p><?php esc_html_e( 'An email was just sent to confirm your subscription. Please find the email now and click activate to start subscribing.', 'jetpack' ); ?></p>
603 </div>
604
605 <?php endif;
606
607 // Display any errors
608 if ( isset( $_GET['subscribe'] ) ) :
609 switch ( $_GET['subscribe'] ) :
610 case 'invalid_email' : ?>
611 <p class="error"><?php esc_html_e( 'The email you entered was invalid, please check and try again.', 'jetpack' ); ?></p>
612 <?php break;
613 case 'already' : ?>
614 <p class="error"><?php esc_html_e( 'You have already subscribed to this site, please check your inbox.', 'jetpack' ); ?></p>
615 <?php break;
616 case 'success' :
617 echo wpautop( $subscribe_text );
618 break;
619 default : ?>
620 <p class="error"><?php esc_html_e( 'There was an error when subscribing, please try again.', 'jetpack' ) ?></p>
621 <?php break;
622 endswitch;
623 endif;
624
625 // Display a subscribe form ?>
626 <form action="#" method="post" accept-charset="utf-8" id="subscribe-blog-<?php echo $widget_id; ?>">
627 <?php
628 if ( ! isset ( $_GET['subscribe'] ) ) {
629 ?><p id="subscribe-text"><?php echo $subscribe_text ?></p><?php
630 }
631
632 if ( $show_subscribers_total && 0 < $subscribers_total['value'] ) {
633 echo wpautop( sprintf( _n( 'Join %s other subscriber', 'Join %s other subscribers', $subscribers_total['value'], 'jetpack' ), number_format_i18n( $subscribers_total['value'] ) ) );
634 }
635 ?>
636
637 <p id="subscribe-email"><input type="text" name="email" value="<?php echo !empty( $current_user->user_email ) ? esc_attr( $current_user->user_email ) : esc_html__( 'Email Address', 'jetpack' ); ?>" id="<?php echo esc_attr($subscribe_field_id) ?>" onclick="if ( this.value == '<?php esc_html_e( 'Email Address', 'jetpack' ) ?>' ) { this.value = ''; }" onblur="if ( this.value == '' ) { this.value = '<?php esc_html_e( 'Email Address', 'jetpack' ) ?>'; }" /></p>
638
639 <p id="subscribe-submit">
640 <input type="hidden" name="action" value="subscribe" />
641 <input type="hidden" name="source" value="<?php echo esc_url( $referer ); ?>" />
642 <input type="hidden" name="sub-type" value="<?php echo esc_attr( $source ); ?>" />
643 <input type="hidden" name="redirect_fragment" value="<?php echo $widget_id; ?>" />
644 <?php
645 if ( is_user_logged_in() ) {
646 wp_nonce_field( 'blogsub_subscribe_'. get_current_blog_id(), '_wpnonce', false );
647 }
648 ?>
649 <input type="submit" value="<?php echo esc_attr( $subscribe_button ); ?>" name="jetpack_subscriptions_widget" />
650 </p>
651 </form>
652
653 <?php
654
655 echo "\n" . $args['after_widget'];
656 }
657
658 function increment_subscriber_count( $current_subs_array = array() ) {
659 $current_subs_array['value']++;
660
661 set_transient( 'wpcom_subscribers_total', $current_subs_array, 3600 ); // try to cache the result for at least 1 hour
662
663 return $current_subs_array;
664 }
665
666 function fetch_subscriber_count() {
667 $subs_count = get_transient( 'wpcom_subscribers_total' );
668
669 if ( FALSE === $subs_count || 'failed' == $subs_count['status'] ) {
670 Jetpack:: load_xml_rpc_client();
671
672 $xml = new Jetpack_IXR_Client( array( 'user_id' => JETPACK_MASTER_USER, ) );
673
674 $xml->query( 'jetpack.fetchSubscriberCount' );
675
676 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
677 $subs_count = array(
678 'status' => 'failed',
679 'code' => $xml->getErrorCode(),
680 'message' => $xml->getErrorMessage(),
681 'value' => ( isset( $subs_count['value'] ) ) ? $subs_count['value'] : 0,
682 );
683 } else {
684 $subs_count = array(
685 'status' => 'success',
686 'value' => $xml->getResponse(),
687 );
688 }
689
690 set_transient( 'wpcom_subscribers_total', $subs_count, 3600 ); // try to cache the result for at least 1 hour
691 }
692
693 return $subs_count;
694 }
695
696 function update( $new_instance, $old_instance ) {
697 $instance = $old_instance;
698
699 $instance['title'] = wp_kses( stripslashes( $new_instance['title'] ), array() );
700 $instance['subscribe_text'] = wp_filter_post_kses( stripslashes( $new_instance['subscribe_text'] ) );
701 $instance['subscribe_logged_in'] = wp_filter_post_kses( stripslashes( $new_instance['subscribe_logged_in'] ) );
702 $instance['subscribe_button'] = wp_kses( stripslashes( $new_instance['subscribe_button'] ), array() );
703 $instance['show_subscribers_total'] = isset( $new_instance['show_subscribers_total'] ) && $new_instance['show_subscribers_total'];
704
705 return $instance;
706 }
707
708 public static function defaults() {
709 return array(
710 'title' => esc_html__( 'Subscribe to Blog via Email', 'jetpack' ),
711 'subscribe_text' => esc_html__( 'Enter your email address to subscribe to this blog and receive notifications of new posts by email.', 'jetpack' ),
712 'subscribe_button' => esc_html__( 'Subscribe', 'jetpack' ),
713 'subscribe_logged_in' => esc_html__( 'Click to subscribe to this blog and receive notifications of new posts by email.', 'jetpack' ),
714 'show_subscribers_total' => true,
715 );
716 }
717
718 function form( $instance ) {
719 $instance = wp_parse_args( (array) $instance, $this->defaults() );
720
721 $title = stripslashes( $instance['title'] );
722 $subscribe_text = stripslashes( $instance['subscribe_text'] );
723 $subscribe_button = stripslashes( $instance['subscribe_button'] );
724 $show_subscribers_total = checked( $instance['show_subscribers_total'], true, false );
725
726 $subs_fetch = $this->fetch_subscriber_count();
727
728 if ( 'failed' == $subs_fetch['status'] ) {
729 printf( '<div class="error inline"><p>' . __( '%s: %s', 'jetpack' ) . '</p></div>', esc_html( $subs_fetch['code'] ), esc_html( $subs_fetch['message'] ) );
730 }
731 $subscribers_total = number_format_i18n( $subs_fetch['value'] );
732
733 ?>
734 <p>
735 <label for="<?php echo $this->get_field_id( 'title' ); ?>">
736 <?php _e( 'Widget title:', 'jetpack' ); ?>
737 <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
738 </label>
739 </p>
740 <p>
741 <label for="<?php echo $this->get_field_id( 'subscribe_text' ); ?>">
742 <?php _e( 'Optional text to display to your readers:', 'jetpack' ); ?>
743 <textarea style="width: 95%" id="<?php echo $this->get_field_id( 'subscribe_text' ); ?>" name="<?php echo $this->get_field_name( 'subscribe_text' ); ?>" type="text"><?php echo esc_html( $subscribe_text ); ?></textarea>
744 </label>
745 </p>
746 <p>
747 <label for="<?php echo $this->get_field_id( 'subscribe_button' ); ?>">
748 <?php _e( 'Subscribe Button:', 'jetpack' ); ?>
749 <input class="widefat" id="<?php echo $this->get_field_id( 'subscribe_button' ); ?>" name="<?php echo $this->get_field_name( 'subscribe_button' ); ?>" type="text" value="<?php echo esc_attr( $subscribe_button ); ?>" />
750 </label>
751 </p>
752 <p>
753 <label for="<?php echo $this->get_field_id( 'show_subscribers_total' ); ?>">
754 <input type="checkbox" id="<?php echo $this->get_field_id( 'show_subscribers_total' ); ?>" name="<?php echo $this->get_field_name( 'show_subscribers_total' ); ?>" value="1"<?php echo $show_subscribers_total; ?> />
755 <?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 ) ); ?>
756 </label>
757 </p>
758 <?php
759 }
760 }
761
762 add_shortcode( 'jetpack_subscription_form', 'jetpack_do_subscription_form' );
763
764 function jetpack_do_subscription_form( $args ) {
765 $args['show_subscribers_total'] = empty( $args['show_subscribers_total'] ) ? false : true;
766 $args = shortcode_atts( Jetpack_Subscriptions_Widget::defaults(), $args );
767 ob_start();
768 the_widget( 'Jetpack_Subscriptions_Widget', $args );
769 $output = ob_get_clean();
770 return $output;
771 }
772