PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 2.4.7
Jetpack – WP Security, Backup, Speed, & Growth v2.4.7
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 2.4.7, at modules/subscriptions.php

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