PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.2.1
Jetpack – WP Security, Backup, Speed, & Growth v7.2.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 in Jetpack – WP Security, Backup, Speed, & Growth 7.2.1, at modules/subscriptions.php

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