PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.1
Jetpack – WP Security, Backup, Speed, & Growth v7.1
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / subscriptions.php
subscriptions.php
789 lines 24.4 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 * Jumpstart Description: Give visitors two easy subscription options — while commenting, or via a separate email subscription widget you can display.
6 * Sort Order: 9
7 * Recommendation Order: 8
8 * First Introduced: 1.2
9 * Requires Connection: Yes
10 * Auto Activate: Yes
11 * Module Tags: Social
12 * Feature: Engagement, Jumpstart
13 * Additional Search Queries: subscriptions, subscription, email, follow, followers, subscribers, signup
14 */
15
16 add_action( 'jetpack_modules_loaded', 'jetpack_subscriptions_load' );
17
18 function jetpack_subscriptions_load() {
19 Jetpack::enable_module_configurable( __FILE__ );
20 Jetpack::module_configuration_load( __FILE__, 'jetpack_subscriptions_configuration_load' );
21 }
22
23 function jetpack_subscriptions_configuration_load() {
24 wp_safe_redirect( admin_url( 'options-discussion.php#jetpack-subscriptions-settings' ) );
25 exit;
26 }
27
28 /**
29 * Cherry picks keys from `$_SERVER` array.
30 *
31 * @since 6.0.0
32 *
33 * @return array An array of server data.
34 */
35 function jetpack_subscriptions_cherry_pick_server_data() {
36 $data = array();
37
38 foreach ( $_SERVER as $key => $value ) {
39 if ( ! is_string( $value ) || 0 === strpos( $key, 'HTTP_COOKIE' ) ) {
40 continue;
41 }
42
43 if ( 0 === strpos( $key, 'HTTP_' ) || in_array( $key, array( 'REMOTE_ADDR', 'REQUEST_URI', 'DOCUMENT_URI' ), true ) ) {
44 $data[ $key ] = $value;
45 }
46 }
47
48 return $data;
49 }
50
51 class Jetpack_Subscriptions {
52 public $jetpack = false;
53
54 public static $hash;
55
56 /**
57 * Singleton
58 * @static
59 */
60 static function init() {
61 static $instance = false;
62
63 if ( !$instance ) {
64 $instance = new Jetpack_Subscriptions;
65 }
66
67 return $instance;
68 }
69
70 function __construct() {
71 $this->jetpack = Jetpack::init();
72
73 // Don't use COOKIEHASH as it could be shared across installs && is non-unique in multisite.
74 // @see: https://twitter.com/nacin/status/378246957451333632
75 self::$hash = md5( get_option( 'siteurl' ) );
76
77 add_filter( 'jetpack_xmlrpc_methods', array( $this, 'xmlrpc_methods' ) );
78
79 // @todo remove sync from subscriptions and move elsewhere...
80
81 // Add Configuration Page
82 add_action( 'admin_init', array( $this, 'configure' ) );
83
84 // Catch subscription widget submits
85 if ( isset( $_REQUEST['jetpack_subscriptions_widget'] ) )
86 add_action( 'template_redirect', array( $this, 'widget_submit' ) );
87
88 // Set up the comment subscription checkboxes
89 add_filter( 'comment_form_submit_button', array( $this, 'comment_subscribe_init' ), 10, 2 );
90
91 // Catch comment posts and check for subscriptions.
92 add_action( 'comment_post', array( $this, 'comment_subscribe_submit' ), 50, 2 );
93
94 // Adds post meta checkbox in the post submit metabox
95 add_action( 'post_submitbox_misc_actions', array( $this, 'subscription_post_page_metabox' ) );
96
97 add_action( 'transition_post_status', array( $this, 'maybe_send_subscription_email' ), 10, 3 );
98
99 add_filter( 'jetpack_published_post_flags', array( $this, 'set_post_flags' ), 10, 2 );
100
101 add_filter( 'post_updated_messages', array( $this, 'update_published_message' ), 18, 1 );
102 }
103
104 /**
105 * Jetpack_Subscriptions::xmlrpc_methods()
106 *
107 * Register subscriptions methods with the Jetpack XML-RPC server.
108 * @param array $methods
109 */
110 function xmlrpc_methods( $methods ) {
111 return array_merge(
112 $methods,
113 array(
114 'jetpack.subscriptions.subscribe' => array( $this, 'subscribe' ),
115 )
116 );
117 }
118
119 /*
120 * Disable Subscribe on Single Post
121 * Register post meta
122 */
123 function subscription_post_page_metabox() {
124 if (
125 /**
126 * Filter whether or not to show the per-post subscription option.
127 *
128 * @module subscriptions
129 *
130 * @since 3.7.0
131 *
132 * @param bool true = show checkbox option on all new posts | false = hide the option.
133 */
134 ! apply_filters( 'jetpack_allow_per_post_subscriptions', false ) )
135 {
136 return;
137 }
138
139 if ( has_filter( 'jetpack_subscriptions_exclude_these_categories' ) || has_filter( 'jetpack_subscriptions_include_only_these_categories' ) ) {
140 return;
141 }
142
143 global $post;
144 $disable_subscribe_value = get_post_meta( $post->ID, '_jetpack_dont_email_post_to_subs', true );
145 // only show checkbox if post hasn't been published and is a 'post' post type.
146 if ( get_post_status( $post->ID ) !== 'publish' && get_post_type( $post->ID ) == 'post' ) :
147 // Nonce it
148 wp_nonce_field( 'disable_subscribe', 'disable_subscribe_nonce' );
149 ?>
150 <div class="misc-pub-section">
151 <label for="_jetpack_dont_email_post_to_subs"><?php _e( 'Jetpack Subscriptions:', 'jetpack' ); ?></label><br>
152 <input type="checkbox" name="_jetpack_dont_email_post_to_subs" id="jetpack-per-post-subscribe" value="1" <?php checked( $disable_subscribe_value, 1, true ); ?> />
153 <?php _e( 'Don&#8217;t send this to subscribers', 'jetpack' ); ?>
154 </div>
155 <?php endif;
156 }
157
158 /**
159 * Checks whether or not the post should be emailed to subscribers
160 *
161 * It checks for the following things in order:
162 * - Usage of filter jetpack_subscriptions_exclude_these_categories
163 * - Usage of filter jetpack_subscriptions_include_only_these_categories
164 * - Existence of the per-post checkbox option
165 *
166 * Only one of these can be used at any given time.
167 *
168 * @param $new_status string - the "new" post status of the transition when saved
169 * @param $old_status string - the "old" post status of the transition when saved
170 * @param $post obj - The post object
171 */
172 function maybe_send_subscription_email( $new_status, $old_status, $post ) {
173
174 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
175 return;
176 }
177
178 // Make sure that the checkbox is preseved
179 if ( ! empty( $_POST['disable_subscribe_nonce'] ) && wp_verify_nonce( $_POST['disable_subscribe_nonce'], 'disable_subscribe' ) ) {
180 $set_checkbox = isset( $_POST['_jetpack_dont_email_post_to_subs'] ) ? 1 : 0;
181 update_post_meta( $post->ID, '_jetpack_dont_email_post_to_subs', $set_checkbox );
182 }
183 }
184
185 function update_published_message( $messages ) {
186 global $post;
187 if ( ! $this->should_email_post_to_subscribers( $post ) ) {
188 return $messages;
189 }
190
191 $view_post_link_html = sprintf( ' <a href="%1$s">%2$s</a>',
192 esc_url( get_permalink( $post ) ),
193 __( 'View post', 'jetpack' )
194 );
195
196 $messages['post'][6] = sprintf(
197 /* translators: Message shown after a post is published */
198 esc_html__( 'Post published and sending emails to subscribers.', 'jetpack' )
199 ) . $view_post_link_html;
200 return $messages;
201 }
202
203 public function should_email_post_to_subscribers( $post ) {
204 $should_email = true;
205 if ( get_post_meta( $post->ID, '_jetpack_dont_email_post_to_subs', true ) ) {
206 return false;
207 }
208
209 // Only posts are currently supported
210 if ( $post->post_type !== 'post' ) {
211 return false;
212 }
213
214 // Private posts are not sent to subscribers.
215 if ( 'private' === $post->post_status ) {
216 return false;
217 }
218
219 /**
220 * Array of categories that will never trigger subscription emails.
221 *
222 * Will not send subscription emails from any post from within these categories.
223 *
224 * @module subscriptions
225 *
226 * @since 3.7.0
227 *
228 * @param array $args Array of category slugs or ID's.
229 */
230 $excluded_categories = apply_filters( 'jetpack_subscriptions_exclude_these_categories', array() );
231
232 // Never email posts from these categories
233 if ( ! empty( $excluded_categories ) && in_category( $excluded_categories, $post->ID ) ) {
234 $should_email = false;
235 }
236
237 /**
238 * ONLY send subscription emails for these categories
239 *
240 * Will ONLY send subscription emails to these categories.
241 *
242 * @module subscriptions
243 *
244 * @since 3.7.0
245 *
246 * @param array $args Array of category slugs or ID's.
247 */
248 $only_these_categories = apply_filters( 'jetpack_subscriptions_exclude_all_categories_except', array() );
249
250 // Only emails posts from these categories
251 if ( ! empty( $only_these_categories ) && ! in_category( $only_these_categories, $post->ID ) ) {
252 $should_email = false;
253 }
254
255 return $should_email;
256 }
257
258 function set_post_flags( $flags, $post ) {
259 $flags['send_subscription'] = $this->should_email_post_to_subscribers( $post );
260 return $flags;
261 }
262
263 /**
264 * Jetpack_Subscriptions::configure()
265 *
266 * Jetpack Subscriptions configuration screen.
267 */
268 function configure() {
269 // Create the section
270 add_settings_section(
271 'jetpack_subscriptions',
272 __( 'Jetpack Subscriptions Settings', 'jetpack' ),
273 array( $this, 'subscriptions_settings_section' ),
274 'discussion'
275 );
276
277 /** Subscribe to Posts ***************************************************/
278
279 add_settings_field(
280 'jetpack_subscriptions_post_subscribe',
281 __( 'Follow Blog', 'jetpack' ),
282 array( $this, 'subscription_post_subscribe_setting' ),
283 'discussion',
284 'jetpack_subscriptions'
285 );
286
287 register_setting(
288 'discussion',
289 'stb_enabled'
290 );
291
292 /** Subscribe to Comments ******************************************************/
293
294 add_settings_field(
295 'jetpack_subscriptions_comment_subscribe',
296 __( 'Follow Comments', 'jetpack' ),
297 array( $this, 'subscription_comment_subscribe_setting' ),
298 'discussion',
299 'jetpack_subscriptions'
300 );
301
302 register_setting(
303 'discussion',
304 'stc_enabled'
305 );
306
307 /** Subscription Messaging Options ******************************************************/
308
309 register_setting(
310 'reading',
311 'subscription_options',
312 array( $this, 'validate_settings' )
313 );
314
315 add_settings_section(
316 'email_settings',
317 __( 'Follower Settings', 'jetpack' ),
318 array( $this, 'reading_section' ),
319 'reading'
320 );
321
322 add_settings_field(
323 'invitation',
324 __( 'Blog follow email text', 'jetpack' ),
325 array( $this, 'setting_invitation' ),
326 'reading',
327 'email_settings'
328 );
329
330 add_settings_field(
331 'comment-follow',
332 __( 'Comment follow email text', 'jetpack' ),
333 array( $this, 'setting_comment_follow' ),
334 'reading',
335 'email_settings'
336 );
337 }
338
339 /**
340 * Discussions setting section blurb
341 *
342 */
343 function subscriptions_settings_section() {
344 ?>
345 <p id="jetpack-subscriptions-settings"><?php _e( 'Change whether your visitors can subscribe to your posts or comments or both.', 'jetpack' ); ?></p>
346
347 <?php
348 }
349
350 /**
351 * Post Subscriptions Toggle
352 *
353 */
354 function subscription_post_subscribe_setting() {
355
356 $stb_enabled = get_option( 'stb_enabled', 1 ); ?>
357
358 <p class="description">
359 <input type="checkbox" name="stb_enabled" id="jetpack-post-subscribe" value="1" <?php checked( $stb_enabled, 1 ); ?> />
360 <?php _e( "Show a <em>'follow blog'</em> option in the comment form", 'jetpack' ); ?>
361 </p>
362 <?php
363 }
364
365 /**
366 * Comments Subscriptions Toggle
367 *
368 */
369 function subscription_comment_subscribe_setting() {
370
371 $stc_enabled = get_option( 'stc_enabled', 1 ); ?>
372
373 <p class="description">
374 <input type="checkbox" name="stc_enabled" id="jetpack-comment-subscribe" value="1" <?php checked( $stc_enabled, 1 ); ?> />
375 <?php _e( "Show a <em>'follow comments'</em> option in the comment form", 'jetpack' ); ?>
376 </p>
377
378 <?php
379 }
380
381 function validate_settings( $settings ) {
382 global $allowedposttags;
383
384 $default = $this->get_default_settings();
385
386 // Blog Follow
387 $settings['invitation'] = trim( wp_kses( $settings['invitation'], $allowedposttags ) );
388 if ( empty( $settings['invitation'] ) )
389 $settings['invitation'] = $default['invitation'];
390
391 // Comments Follow (single post)
392 $settings['comment_follow'] = trim( wp_kses( $settings['comment_follow'], $allowedposttags ) );
393 if ( empty( $settings['comment_follow'] ) )
394 $settings['comment_follow'] = $default['comment_follow'];
395
396 return $settings;
397 }
398
399 public function reading_section() {
400 echo '<p id="follower-settings">';
401 _e( 'These settings change emails sent from your blog to followers.', 'jetpack' );
402 echo '</p>';
403 }
404
405 public function setting_invitation() {
406 $settings = $this->get_settings();
407 echo '<textarea name="subscription_options[invitation]" class="large-text" cols="50" rows="5">' . esc_textarea( $settings['invitation'] ) . '</textarea>';
408 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>';
409 }
410
411 public function setting_comment_follow() {
412 $settings = $this->get_settings();
413 echo '<textarea name="subscription_options[comment_follow]" class="large-text" cols="50" rows="5">' . esc_textarea( $settings['comment_follow'] ) . '</textarea>';
414 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>';
415 }
416
417 function get_default_settings() {
418 return array(
419 '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' ),
420 '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' )
421 );
422 }
423
424 function get_settings() {
425 return wp_parse_args( (array) get_option( 'subscription_options', array() ), $this->get_default_settings() );
426 }
427
428 /**
429 * Jetpack_Subscriptions::subscribe()
430 *
431 * Send a synchronous XML-RPC subscribe to blog posts or subscribe to post comments request.
432 *
433 * @param string $email
434 * @param array $post_ids (optional) defaults to 0 for blog posts only: array of post IDs to subscribe to blog's posts
435 * @param bool $async (optional) Should the subscription be performed asynchronously? Defaults to true.
436 *
437 * @return true|Jetpack_Error true on success
438 * invalid_email : not a valid email address
439 * invalid_post_id : not a valid post ID
440 * unknown_post_id : unknown post
441 * not_subscribed : strange error. Jetpack servers at WordPress.com could subscribe the email.
442 * disabled : Site owner has disabled subscriptions.
443 * active : Already subscribed.
444 * unknown : strange error. Jetpack servers at WordPress.com returned something malformed.
445 * unknown_status : strange error. Jetpack servers at WordPress.com returned something I didn't understand.
446 */
447 function subscribe( $email, $post_ids = 0, $async = true, $extra_data = array() ) {
448 if ( !is_email( $email ) ) {
449 return new Jetpack_Error( 'invalid_email' );
450 }
451
452 if ( !$async ) {
453 Jetpack::load_xml_rpc_client();
454 $xml = new Jetpack_IXR_ClientMulticall();
455 }
456
457 foreach ( (array) $post_ids as $post_id ) {
458 $post_id = (int) $post_id;
459 if ( $post_id < 0 ) {
460 return new Jetpack_Error( 'invalid_post_id' );
461 } else if ( $post_id && !$post = get_post( $post_id ) ) {
462 return new Jetpack_Error( 'unknown_post_id' );
463 }
464
465 if ( $async ) {
466 Jetpack::xmlrpc_async_call( 'jetpack.subscribeToSite', $email, $post_id, serialize( $extra_data ) );
467 } else {
468 $xml->addCall( 'jetpack.subscribeToSite', $email, $post_id, serialize( $extra_data ) );
469 }
470 }
471
472 if ( $async ) {
473 return;
474 }
475
476 // Call
477 $xml->query();
478
479 if ( $xml->isError() ) {
480 return $xml->get_jetpack_error();
481 }
482
483 $responses = $xml->getResponse();
484
485 $r = array();
486 foreach ( (array) $responses as $response ) {
487 if ( isset( $response['faultCode'] ) || isset( $response['faultString'] ) ) {
488 $r[] = $xml->get_jetpack_error( $response['faultCode'], $response['faultString'] );
489 continue;
490 }
491
492 if ( !is_array( $response[0] ) || empty( $response[0]['status'] ) ) {
493 $r[] = new Jetpack_Error( 'unknown' );
494 continue;
495 }
496
497 switch ( $response[0]['status'] ) {
498 case 'error' :
499 $r[] = new Jetpack_Error( 'not_subscribed' );
500 continue 2;
501 case 'disabled' :
502 $r[] = new Jetpack_Error( 'disabled' );
503 continue 2;
504 case 'active' :
505 $r[] = new Jetpack_Error( 'active' );
506 continue 2;
507 case 'pending' :
508 $r[] = true;
509 continue 2;
510 default :
511 $r[] = new Jetpack_Error( 'unknown_status', (string) $response[0]['status'] );
512 continue 2;
513 }
514 }
515
516 return $r;
517 }
518
519 /**
520 * Jetpack_Subscriptions::widget_submit()
521 *
522 * When a user submits their email via the blog subscription widget, check the details and call the subsribe() method.
523 */
524 function widget_submit() {
525 // Check the nonce.
526 if ( is_user_logged_in() ) {
527 check_admin_referer( 'blogsub_subscribe_' . get_current_blog_id() );
528 }
529
530 if ( empty( $_REQUEST['email'] ) )
531 return false;
532
533 $redirect_fragment = false;
534 if ( isset( $_REQUEST['redirect_fragment'] ) ) {
535 $redirect_fragment = preg_replace( '/[^a-z0-9_-]/i', '', $_REQUEST['redirect_fragment'] );
536 }
537 if ( !$redirect_fragment ) {
538 $redirect_fragment = 'subscribe-blog';
539 }
540
541 $subscribe = Jetpack_Subscriptions::subscribe(
542 $_REQUEST['email'],
543 0,
544 false,
545 array(
546 'source' => 'widget',
547 'widget-in-use' => is_active_widget( false, false, 'blog_subscription', true ) ? 'yes' : 'no',
548 'comment_status' => '',
549 'server_data' => jetpack_subscriptions_cherry_pick_server_data(),
550 )
551 );
552
553 if ( is_wp_error( $subscribe ) ) {
554 $error = $subscribe->get_error_code();
555 } else {
556 $error = false;
557 foreach ( $subscribe as $response ) {
558 if ( is_wp_error( $response ) ) {
559 $error = $response->get_error_code();
560 break;
561 }
562 }
563 }
564
565 switch ( $error ) {
566 case false:
567 $result = 'success';
568 break;
569 case 'invalid_email':
570 $result = $error;
571 break;
572 case 'blocked_email':
573 $result = 'opted_out';
574 break;
575 case 'active':
576 case 'pending':
577 $result = 'already';
578 break;
579 default:
580 $result = 'error';
581 break;
582 }
583
584 $redirect = add_query_arg( 'subscribe', $result );
585
586 /**
587 * Fires on each subscription form submission.
588 *
589 * @module subscriptions
590 *
591 * @since 3.7.0
592 *
593 * @param string $result Result of form submission: success, invalid_email, already, error.
594 */
595 do_action( 'jetpack_subscriptions_form_submission', $result );
596
597 wp_safe_redirect( "$redirect#$redirect_fragment" );
598 exit;
599 }
600
601 /**
602 * Jetpack_Subscriptions::comment_subscribe_init()
603 *
604 * Set up and add the comment subscription checkbox to the comment form.
605 *
606 * @param string $submit_button HTML markup for the submit button.
607 * @param array $args Arguments passed to `comment_form()`.
608 */
609 function comment_subscribe_init( $submit_button, $args ) {
610 global $post;
611
612 $comments_checked = '';
613 $blog_checked = '';
614
615 // Check for a comment / blog submission and set a cookie to retain the setting and check the boxes.
616 if ( isset( $_COOKIE[ 'jetpack_comments_subscribe_' . self::$hash . '_' . $post->ID ] ) ) {
617 $comments_checked = ' checked="checked"';
618 }
619
620 if ( isset( $_COOKIE[ 'jetpack_blog_subscribe_' . self::$hash ] ) ) {
621 $blog_checked = ' checked="checked"';
622 }
623
624 // Some themes call this function, don't show the checkbox again
625 remove_action( 'comment_form', 'subscription_comment_form' );
626
627 // Check if Mark Jaquith's Subscribe to Comments plugin is active - if so, suppress Jetpack checkbox
628
629 $str = '';
630
631 if ( FALSE === has_filter( 'comment_form', 'show_subscription_checkbox' ) && 1 == get_option( 'stc_enabled', 1 ) && empty( $post->post_password ) && 'post' == get_post_type() ) {
632 // Subscribe to comments checkbox
633 $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 . ' /> ';
634 $comment_sub_text = __( 'Notify me of follow-up comments by email.', 'jetpack' );
635 $str .= '<label class="subscribe-label" id="subscribe-label" for="subscribe_comments">' . esc_html(
636 /**
637 * Filter the Subscribe to comments text appearing below the comment form.
638 *
639 * @module subscriptions
640 *
641 * @since 3.4.0
642 *
643 * @param string $comment_sub_text Subscribe to comments text.
644 */
645 apply_filters( 'jetpack_subscribe_comment_label', $comment_sub_text )
646 ) . '</label>';
647 $str .= '</p>';
648 }
649
650 if ( 1 == get_option( 'stb_enabled', 1 ) ) {
651 // Subscribe to blog checkbox
652 $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 . ' /> ';
653 $blog_sub_text = __( 'Notify me of new posts by email.', 'jetpack' );
654 $str .= '<label class="subscribe-label" id="subscribe-blog-label" for="subscribe_blog">' . esc_html(
655 /**
656 * Filter the Subscribe to blog text appearing below the comment form.
657 *
658 * @module subscriptions
659 *
660 * @since 3.4.0
661 *
662 * @param string $comment_sub_text Subscribe to blog text.
663 */
664 apply_filters( 'jetpack_subscribe_blog_label', $blog_sub_text )
665 ) . '</label>';
666 $str .= '</p>';
667 }
668
669 /**
670 * Filter the output of the subscription options appearing below the comment form.
671 *
672 * @module subscriptions
673 *
674 * @since 1.2.0
675 *
676 * @param string $str Comment Subscription form HTML output.
677 */
678 $str = apply_filters( 'jetpack_comment_subscription_form', $str );
679
680 return $str . $submit_button;
681 }
682
683 /**
684 * Jetpack_Subscriptions::comment_subscribe_init()
685 *
686 * When a user checks the comment subscribe box and submits a comment, subscribe them to the comment thread.
687 */
688 function comment_subscribe_submit( $comment_id, $approved ) {
689 if ( 'spam' === $approved ) {
690 return;
691 }
692
693 $comment = get_comment( $comment_id );
694
695 // Set cookies for this post/comment
696 $this->set_cookies( isset( $_REQUEST['subscribe_comments'] ), $comment->comment_post_ID, isset( $_REQUEST['subscribe_blog'] ) );
697
698 if ( !isset( $_REQUEST['subscribe_comments'] ) && !isset( $_REQUEST['subscribe_blog'] ) )
699 return;
700
701 $post_ids = array();
702
703 if ( isset( $_REQUEST['subscribe_comments'] ) )
704 $post_ids[] = $comment->comment_post_ID;
705
706 if ( isset( $_REQUEST['subscribe_blog'] ) )
707 $post_ids[] = 0;
708
709 $result = Jetpack_Subscriptions::subscribe(
710 $comment->comment_author_email,
711 $post_ids,
712 true,
713 array(
714 'source' => 'comment-form',
715 'widget-in-use' => is_active_widget( false, false, 'blog_subscription', true ) ? 'yes' : 'no',
716 'comment_status' => $approved,
717 'server_data' => jetpack_subscriptions_cherry_pick_server_data(),
718 )
719 );
720
721 /**
722 * Fires on each comment subscription form submission.
723 *
724 * @module subscriptions
725 *
726 * @since 5.5.0
727 *
728 * @param NULL|WP_Error $result Result of form submission: NULL on success, WP_Error otherwise.
729 * @param Array $post_ids An array of post IDs that the user subscribed to, 0 means blog subscription.
730 */
731 do_action( 'jetpack_subscriptions_comment_form_submission', $result, $post_ids );
732 }
733
734 /**
735 * Jetpack_Subscriptions::set_cookies()
736 *
737 * Set a cookie to save state on the comment and post subscription checkboxes.
738 *
739 * @param bool $subscribe_to_post Whether the user chose to subscribe to subsequent comments on this post.
740 * @param int $post_id If $subscribe_to_post is true, the post ID they've subscribed to.
741 * @param bool $subscribe_to_blog Whether the user chose to subscribe to all new posts on the blog.
742 */
743 function set_cookies( $subscribe_to_post = false, $post_id = null, $subscribe_to_blog = false ) {
744 $post_id = intval( $post_id );
745
746 /** This filter is already documented in core/wp-includes/comment-functions.php */
747 $cookie_lifetime = apply_filters( 'comment_cookie_lifetime', 30000000 );
748
749 /**
750 * Filter the Jetpack Comment cookie path.
751 *
752 * @module subscriptions
753 *
754 * @since 2.5.0
755 *
756 * @param string COOKIEPATH Cookie path.
757 */
758 $cookie_path = apply_filters( 'jetpack_comment_cookie_path', COOKIEPATH );
759
760 /**
761 * Filter the Jetpack Comment cookie domain.
762 *
763 * @module subscriptions
764 *
765 * @since 2.5.0
766 *
767 * @param string COOKIE_DOMAIN Cookie domain.
768 */
769 $cookie_domain = apply_filters( 'jetpack_comment_cookie_domain', COOKIE_DOMAIN );
770
771 if ( $subscribe_to_post && $post_id >= 0 ) {
772 setcookie( 'jetpack_comments_subscribe_' . self::$hash . '_' . $post_id, 1, time() + $cookie_lifetime, $cookie_path, $cookie_domain );
773 } else {
774 setcookie( 'jetpack_comments_subscribe_' . self::$hash . '_' . $post_id, '', time() - 3600, $cookie_path, $cookie_domain );
775 }
776
777 if ( $subscribe_to_blog ) {
778 setcookie( 'jetpack_blog_subscribe_' . self::$hash, 1, time() + $cookie_lifetime, $cookie_path, $cookie_domain );
779 } else {
780 setcookie( 'jetpack_blog_subscribe_' . self::$hash, '', time() - 3600, $cookie_path, $cookie_domain );
781 }
782 }
783
784 }
785
786 Jetpack_Subscriptions::init();
787
788 include dirname( __FILE__ ) . '/subscriptions/views.php';
789