PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 13.5
Jetpack – WP Security, Backup, Speed, & Growth v13.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
1,080 lines 32.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName)
2 /**
3 * Module Name: Newsletter
4 * Module Description: Let visitors subscribe to new posts and comments via email
5 * Sort Order: 9
6 * Recommendation Order: 8
7 * First Introduced: 1.2
8 * Requires Connection: Yes
9 * Requires User Connection: Yes
10 * Auto Activate: No
11 * Module Tags: Social
12 * Feature: Engagement
13 * Additional Search Queries: subscriptions, subscription, email, follow, followers, subscribers, signup, newsletter, creator
14 */
15
16 // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files.
17
18 use Automattic\Jetpack\Connection\Manager as Connection_Manager;
19 use Automattic\Jetpack\Connection\XMLRPC_Async_Call;
20 use Automattic\Jetpack\Redirect;
21 use Automattic\Jetpack\Status;
22 use Automattic\Jetpack\Status\Host;
23
24 add_action( 'jetpack_modules_loaded', 'jetpack_subscriptions_load' );
25
26 // Loads the User Content Link Redirection feature.
27 require_once __DIR__ . '/subscriptions/jetpack-user-content-link-redirection.php';
28
29 /**
30 * Loads the Subscriptions module.
31 */
32 function jetpack_subscriptions_load() {
33 Jetpack::enable_module_configurable( __FILE__ );
34 }
35
36 /**
37 * Cherry picks keys from `$_SERVER` array.
38 *
39 * @since 6.0.0
40 *
41 * @return array An array of server data.
42 */
43 function jetpack_subscriptions_cherry_pick_server_data() {
44 $data = array();
45
46 foreach ( $_SERVER as $key => $value ) {
47 if ( ! is_string( $value ) || str_starts_with( $key, 'HTTP_COOKIE' ) ) {
48 continue;
49 }
50
51 if ( str_starts_with( $key, 'HTTP_' ) || in_array( $key, array( 'REMOTE_ADDR', 'REQUEST_URI', 'DOCUMENT_URI' ), true ) ) {
52 $data[ $key ] = $value;
53 }
54 }
55
56 return $data;
57 }
58
59 /**
60 * Main class file for the Subscriptions module.
61 */
62 class Jetpack_Subscriptions {
63 /**
64 * Whether Jetpack has been instantiated or not.
65 *
66 * @var bool
67 */
68 public $jetpack = false;
69
70 /**
71 * Hash of the siteurl option.
72 *
73 * @var string
74 */
75 public static $hash;
76
77 /**
78 * Singleton
79 *
80 * @static
81 */
82 public static function init() {
83 static $instance = false;
84
85 if ( ! $instance ) {
86 $instance = new Jetpack_Subscriptions();
87 }
88
89 return $instance;
90 }
91
92 /**
93 * Jetpack_Subscriptions constructor.
94 */
95 public function __construct() {
96 $this->jetpack = Jetpack::init();
97
98 // Don't use COOKIEHASH as it could be shared across installs && is non-unique in multisite.
99 // @see: https://twitter.com/nacin/status/378246957451333632 .
100 self::$hash = md5( get_option( 'siteurl' ) );
101
102 add_filter( 'jetpack_xmlrpc_methods', array( $this, 'xmlrpc_methods' ) );
103
104 // @todo remove sync from subscriptions and move elsewhere...
105
106 // Add Configuration Page.
107 add_action( 'admin_init', array( $this, 'configure' ) );
108
109 // Catch subscription widget submits.
110 if ( isset( $_REQUEST['jetpack_subscriptions_widget'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce checked in widget_submit() for logged in users.
111 add_action( 'template_redirect', array( $this, 'widget_submit' ) );
112 }
113
114 // Set up the comment subscription checkboxes.
115 add_filter( 'comment_form_submit_field', array( $this, 'comment_subscribe_init' ), 10, 2 );
116
117 // Catch comment posts and check for subscriptions.
118 add_action( 'comment_post', array( $this, 'comment_subscribe_submit' ), 50, 2 );
119
120 // Adds post meta checkbox in the post submit metabox.
121 add_action( 'post_submitbox_misc_actions', array( $this, 'subscription_post_page_metabox' ) );
122
123 add_action( 'transition_post_status', array( $this, 'maybe_send_subscription_email' ), 10, 3 );
124
125 add_filter( 'jetpack_published_post_flags', array( $this, 'set_post_flags' ), 10, 2 );
126
127 add_filter( 'post_updated_messages', array( $this, 'update_published_message' ), 18, 1 );
128
129 // Set "social_notifications_subscribe" option during the first-time activation.
130 add_action( 'jetpack_activate_module_subscriptions', array( $this, 'set_social_notifications_subscribe' ) );
131
132 // Hide subscription messaging in Publish panel for posts that were published in the past
133 add_action( 'init', array( $this, 'register_post_meta' ), 20 );
134 add_action( 'transition_post_status', array( $this, 'maybe_set_first_published_status' ), 10, 3 );
135
136 // Add Subscribers menu to Jetpack navigation.
137 add_action( 'jetpack_admin_menu', array( $this, 'add_subscribers_menu' ) );
138
139 // Customize the configuration URL to lead to the Subscriptions settings.
140 add_filter(
141 'jetpack_module_configuration_url_subscriptions',
142 function () {
143 return Jetpack::admin_url( array( 'page' => 'jetpack#/newsletter' ) );
144 }
145 );
146
147 // Track categories created through the category editor page
148 add_action( 'wp_ajax_add-tag', array( $this, 'track_newsletter_category_creation' ), 1 );
149 }
150
151 /**
152 * Jetpack_Subscriptions::xmlrpc_methods()
153 *
154 * Register subscriptions methods with the Jetpack XML-RPC server.
155 *
156 * @param array $methods Methods being registered.
157 */
158 public function xmlrpc_methods( $methods ) {
159 return array_merge(
160 $methods,
161 array(
162 'jetpack.subscriptions.subscribe' => array( $this, 'subscribe' ),
163 )
164 );
165 }
166
167 /**
168 * Disable Subscribe on Single Post
169 * Register post meta
170 */
171 public function subscription_post_page_metabox() {
172 if (
173 /**
174 * Filter whether or not to show the per-post subscription option.
175 *
176 * @module subscriptions
177 *
178 * @since 3.7.0
179 *
180 * @param bool true = show checkbox option on all new posts | false = hide the option.
181 */
182 ! apply_filters( 'jetpack_allow_per_post_subscriptions', false ) ) {
183 return;
184 }
185
186 if ( has_filter( 'jetpack_subscriptions_exclude_these_categories' ) || has_filter( 'jetpack_subscriptions_include_only_these_categories' ) ) {
187 return;
188 }
189
190 global $post;
191 $disable_subscribe_value = get_post_meta( $post->ID, '_jetpack_dont_email_post_to_subs', true );
192 // only show checkbox if post hasn't been published and is a 'post' post type.
193 if ( get_post_status( $post->ID ) !== 'publish' && get_post_type( $post->ID ) === 'post' ) :
194 // Nonce it.
195 wp_nonce_field( 'disable_subscribe', 'disable_subscribe_nonce' );
196 ?>
197 <div class="misc-pub-section">
198 <label for="_jetpack_dont_email_post_to_subs"><?php esc_html_e( 'Jetpack Subscriptions:', 'jetpack' ); ?></label><br>
199 <input type="checkbox" name="_jetpack_dont_email_post_to_subs" id="jetpack-per-post-subscribe" value="1" <?php checked( $disable_subscribe_value, 1, true ); ?> />
200 <?php esc_html_e( 'Don&#8217;t send this to subscribers', 'jetpack' ); ?>
201 </div>
202 <?php
203 endif;
204 }
205
206 /**
207 * Checks whether or not the post should be emailed to subscribers
208 *
209 * It checks for the following things in order:
210 * - Usage of filter jetpack_subscriptions_exclude_these_categories
211 * - Usage of filter jetpack_subscriptions_include_only_these_categories
212 * - Existence of the per-post checkbox option
213 *
214 * Only one of these can be used at any given time.
215 *
216 * @param string $new_status Tthe "new" post status of the transition when saved.
217 * @param string $old_status The "old" post status of the transition when saved.
218 * @param object $post obj The post object.
219 */
220 public function maybe_send_subscription_email( $new_status, $old_status, $post ) {
221
222 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
223 return;
224 }
225
226 // Make sure that the checkbox is preseved.
227 if ( ! empty( $_POST['disable_subscribe_nonce'] ) && wp_verify_nonce( $_POST['disable_subscribe_nonce'], 'disable_subscribe' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- WP Core doesn't unslash or sanitize nonces either.
228 $set_checkbox = isset( $_POST['_jetpack_dont_email_post_to_subs'] ) ? 1 : 0;
229 update_post_meta( $post->ID, '_jetpack_dont_email_post_to_subs', $set_checkbox );
230 }
231 }
232
233 /**
234 * Message used when publishing a post.
235 *
236 * @param array $messages Message array for a post.
237 */
238 public function update_published_message( $messages ) {
239 global $post;
240 if ( ! $this->should_email_post_to_subscribers( $post ) ) {
241 return $messages;
242 }
243
244 $view_post_link_html = sprintf(
245 ' <a href="%1$s">%2$s</a>',
246 esc_url( get_permalink( $post ) ),
247 __( 'View post', 'jetpack' )
248 );
249
250 $messages['post'][6] = sprintf(
251 /* translators: Message shown after a post is published */
252 esc_html__( 'Post published and sending emails to subscribers.', 'jetpack' )
253 ) . $view_post_link_html;
254 return $messages;
255 }
256
257 /**
258 * Determine if a post should notifiy subscribers via email.
259 *
260 * @param object $post The post.
261 */
262 public function should_email_post_to_subscribers( $post ) {
263 $should_email = true;
264 if ( get_post_meta( $post->ID, '_jetpack_dont_email_post_to_subs', true ) ) {
265 return false;
266 }
267
268 // Only posts are currently supported.
269 if ( 'post' !== $post->post_type ) {
270 return false;
271 }
272
273 // Private posts are not sent to subscribers.
274 if ( 'private' === $post->post_status ) {
275 return false;
276 }
277
278 /**
279 * Array of categories that will never trigger subscription emails.
280 *
281 * Will not send subscription emails from any post from within these categories.
282 *
283 * @module subscriptions
284 *
285 * @since 3.7.0
286 *
287 * @param array $args Array of category slugs or ID's.
288 */
289 $excluded_categories = apply_filters( 'jetpack_subscriptions_exclude_these_categories', array() );
290
291 // Never email posts from these categories.
292 if ( ! empty( $excluded_categories ) && in_category( $excluded_categories, $post->ID ) ) {
293 $should_email = false;
294 }
295
296 /**
297 * ONLY send subscription emails for these categories
298 *
299 * Will ONLY send subscription emails to these categories.
300 *
301 * @module subscriptions
302 *
303 * @since 3.7.0
304 *
305 * @param array $args Array of category slugs or ID's.
306 */
307 $only_these_categories = apply_filters( 'jetpack_subscriptions_exclude_all_categories_except', array() );
308
309 // Only emails posts from these categories.
310 if ( ! empty( $only_these_categories ) && ! in_category( $only_these_categories, $post->ID ) ) {
311 $should_email = false;
312 }
313
314 return $should_email;
315 }
316
317 /**
318 * Retrieve which flags should be added to a particular post.
319 *
320 * @param array $flags Flags to be added.
321 * @param object $post A post object.
322 */
323 public function set_post_flags( $flags, $post ) {
324 $flags['send_subscription'] = $this->should_email_post_to_subscribers( $post );
325 return $flags;
326 }
327
328 /**
329 * Jetpack_Subscriptions::configure()
330 *
331 * Jetpack Subscriptions configuration screen.
332 */
333 public function configure() {
334 // Create the section.
335 add_settings_section(
336 'jetpack_subscriptions',
337 __( 'Jetpack Subscriptions Settings', 'jetpack' ),
338 array( $this, 'subscriptions_settings_section' ),
339 'discussion'
340 );
341
342 /** Subscribe to Posts */
343
344 add_settings_field(
345 'jetpack_subscriptions_post_subscribe',
346 __( 'Follow Blog', 'jetpack' ),
347 array( $this, 'subscription_post_subscribe_setting' ),
348 'discussion',
349 'jetpack_subscriptions'
350 );
351
352 register_setting(
353 'discussion',
354 'stb_enabled'
355 );
356
357 /** Subscribe to Comments */
358
359 add_settings_field(
360 'jetpack_subscriptions_comment_subscribe',
361 __( 'Follow Comments', 'jetpack' ),
362 array( $this, 'subscription_comment_subscribe_setting' ),
363 'discussion',
364 'jetpack_subscriptions'
365 );
366
367 register_setting(
368 'discussion',
369 'stc_enabled'
370 );
371
372 /** Enable Subscribe Modal */
373
374 add_settings_field(
375 'jetpack_subscriptions_comment_subscribe',
376 __( 'Enable Subscribe Modal', 'jetpack' ),
377 array( $this, 'subscribe_modal_setting' ),
378 'discussion',
379 'jetpack_subscriptions'
380 );
381
382 register_setting(
383 'discussion',
384 'sm_enabled'
385 );
386
387 /** Email me whenever: Someone subscribes to my blog */
388 /* @since 8.1 */
389
390 add_settings_section(
391 'notifications_section',
392 __( 'Someone subscribes to my blog', 'jetpack' ),
393 array( $this, 'social_notifications_subscribe_section' ),
394 'discussion'
395 );
396
397 add_settings_field(
398 'jetpack_subscriptions_social_notifications_subscribe',
399 __( 'Email me whenever', 'jetpack' ),
400 array( $this, 'social_notifications_subscribe_field' ),
401 'discussion',
402 'notifications_section'
403 );
404
405 register_setting(
406 'discussion',
407 'social_notifications_subscribe',
408 array( $this, 'social_notifications_subscribe_validate' )
409 );
410 }
411
412 /**
413 * Discussions setting section blurb.
414 */
415 public function subscriptions_settings_section() {
416 ?>
417 <p id="jetpack-subscriptions-settings"><?php esc_html_e( 'Change whether your visitors can subscribe to your posts or comments or both.', 'jetpack' ); ?></p>
418
419 <?php
420 }
421
422 /**
423 * Post Subscriptions Toggle.
424 */
425 public function subscription_post_subscribe_setting() {
426
427 $stb_enabled = get_option( 'stb_enabled', 1 );
428 ?>
429
430 <p class="description">
431 <input type="checkbox" name="stb_enabled" id="jetpack-post-subscribe" value="1" <?php checked( $stb_enabled, 1 ); ?> />
432 <?php
433 echo wp_kses(
434 __(
435 "Show a <em>'follow blog'</em> option in the comment form",
436 'jetpack'
437 ),
438 array( 'em' => array() )
439 );
440 ?>
441 </p>
442 <?php
443 }
444
445 /**
446 * Comments Subscriptions Toggle.
447 */
448 public function subscription_comment_subscribe_setting() {
449
450 $stc_enabled = get_option( 'stc_enabled', 1 );
451 ?>
452
453 <p class="description">
454 <input type="checkbox" name="stc_enabled" id="jetpack-comment-subscribe" value="1" <?php checked( $stc_enabled, 1 ); ?> />
455 <?php
456 echo wp_kses(
457 __(
458 "Show a <em>'follow comments'</em> option in the comment form",
459 'jetpack'
460 ),
461 array( 'em' => array() )
462 );
463 ?>
464 </p>
465
466 <?php
467 }
468
469 /**
470 * Subscribe Modal Toggle.
471 */
472 public function subscribe_modal_setting() {
473
474 $sm_enabled = get_option( 'sm_enabled', 1 );
475 ?>
476
477 <p class="description">
478 <input type="checkbox" name="sm_enabled" id="jetpack-subscribe-modal" value="1" <?php checked( $sm_enabled, 1 ); ?> />
479 <?php esc_html_e( 'Show a popup subscribe modal to readers.', 'jetpack' ); ?>
480 </p>
481
482 <?php
483 }
484
485 /**
486 * Someone subscribes to my blog section
487 *
488 * @since 8.1
489 */
490 public function social_notifications_subscribe_section() {
491 // Atypical usage here. We emit jquery to move subscribe notification checkbox to be with the rest of the email notification settings.
492 ?>
493 <script type="text/javascript">
494 jQuery( function( $ ) {
495 var table = $( '#social_notifications_subscribe' ).parents( 'table:first' ),
496 header = table.prevAll( 'h2:first' ),
497 newParent = $( '#moderation_notify' ).parent( 'label' ).parent();
498
499 if ( ! table.length || ! header.length || ! newParent.length ) {
500 return;
501 }
502
503 newParent.append( '<br/>' ).append( table.end().parent( 'label' ).siblings().andSelf() );
504 header.remove();
505 table.remove();
506 } );
507 </script>
508 <?php
509 }
510
511 /**
512 * Someone subscribes to my blog Toggle
513 *
514 * @since 8.1
515 */
516 public function social_notifications_subscribe_field() {
517 $checked = (int) ( 'on' === get_option( 'social_notifications_subscribe', 'on' ) );
518 ?>
519
520 <label>
521 <input type="checkbox" name="social_notifications_subscribe" id="social_notifications_subscribe" value="1" <?php checked( $checked ); ?> />
522 <?php
523 /* translators: this is a label for a setting that starts with "Email me whenever" */
524 esc_html_e( 'Someone subscribes to my blog', 'jetpack' );
525 ?>
526 </label>
527 <?php
528 }
529
530 /**
531 * Validate "Someone subscribes to my blog" option
532 *
533 * @since 8.1
534 *
535 * @param String $input the input string to be validated.
536 * @return string on|off
537 */
538 public function social_notifications_subscribe_validate( $input ) {
539 // If it's not set (was unchecked during form submission) or was set to off (during option update), return 'off'.
540 if ( ! $input || 'off' === $input ) {
541 return 'off';
542 }
543
544 // Otherwise we return 'on'.
545 return 'on';
546 }
547
548 /**
549 * Jetpack_Subscriptions::subscribe()
550 *
551 * Send a synchronous XML-RPC subscribe to blog posts or subscribe to post comments request.
552 *
553 * @param string $email being subscribed.
554 * @param array $post_ids (optional) defaults to 0 for blog posts only: array of post IDs to subscribe to blog's posts.
555 * @param bool $async (optional) Should the subscription be performed asynchronously? Defaults to true.
556 * @param array $extra_data Additional data passed to the `jetpack.subscribeToSite` call.
557 *
558 * @return true|WP_Error true on success
559 * invalid_email : not a valid email address
560 * invalid_post_id : not a valid post ID
561 * unknown_post_id : unknown post
562 * not_subscribed : strange error. Jetpack servers at WordPress.com could subscribe the email.
563 * disabled : Site owner has disabled subscriptions.
564 * active : Already subscribed.
565 * pending : Tried to subscribe before but the confirmation link is never clicked. No confirmation email is sent.
566 * unknown : strange error. Jetpack servers at WordPress.com returned something malformed.
567 * unknown_status : strange error. Jetpack servers at WordPress.com returned something I didn't understand.
568 */
569 public function subscribe( $email, $post_ids = 0, $async = true, $extra_data = array() ) {
570 if ( ! is_email( $email ) ) {
571 return new WP_Error( 'invalid_email' );
572 }
573
574 if ( ! $async ) {
575 $xml = new Jetpack_IXR_ClientMulticall();
576 }
577
578 foreach ( (array) $post_ids as $post_id ) {
579 $post_id = (int) $post_id;
580 if ( $post_id < 0 ) {
581 return new WP_Error( 'invalid_post_id' );
582 } elseif ( $post_id && ! get_post( $post_id ) ) {
583 return new WP_Error( 'unknown_post_id' );
584 }
585
586 if ( $async ) {
587 XMLRPC_Async_Call::add_call( 'jetpack.subscribeToSite', 0, $email, $post_id, serialize( $extra_data ) ); //phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
588 } else {
589 $xml->addCall( 'jetpack.subscribeToSite', $email, $post_id, serialize( $extra_data ) ); //phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
590 }
591 }
592
593 if ( $async ) {
594 return;
595 }
596
597 // Call.
598 $xml->query();
599
600 if ( $xml->isError() ) {
601 return $xml->get_jetpack_error();
602 }
603
604 $responses = $xml->getResponse();
605
606 $r = array();
607 foreach ( (array) $responses as $response ) {
608 if ( isset( $response['faultCode'] ) || isset( $response['faultString'] ) ) {
609 $r[] = $xml->get_jetpack_error( $response['faultCode'], $response['faultString'] );
610 continue;
611 }
612
613 if ( ! is_array( $response[0] ) || empty( $response[0]['status'] ) ) {
614 $r[] = new WP_Error( 'unknown' );
615 continue;
616 }
617
618 switch ( $response[0]['status'] ) {
619 case 'error':
620 $r[] = new WP_Error( 'not_subscribed' );
621 continue 2;
622 case 'disabled':
623 $r[] = new WP_Error( 'disabled' );
624 continue 2;
625 case 'active':
626 $r[] = new WP_Error( 'active' );
627 continue 2;
628 case 'confirming':
629 $r[] = true;
630 continue 2;
631 case 'pending':
632 $r[] = new WP_Error( 'pending' );
633 continue 2;
634 default:
635 $r[] = new WP_Error( 'unknown_status', (string) $response[0]['status'] );
636 continue 2;
637 }
638 }
639
640 return $r;
641 }
642
643 /**
644 * Jetpack_Subscriptions::widget_submit()
645 *
646 * When a user submits their email via the blog subscription widget, check the details and call the subsribe() method.
647 */
648 public function widget_submit() {
649 // Check the nonce.
650 if ( ! wp_verify_nonce( isset( $_REQUEST['_wpnonce'] ) ? sanitize_key( $_REQUEST['_wpnonce'] ) : '', 'blogsub_subscribe_' . \Jetpack_Options::get_option( 'id' ) ) ) {
651 return false;
652 }
653
654 if ( empty( $_REQUEST['email'] ) || ! is_string( $_REQUEST['email'] ) ) {
655 return false;
656 }
657
658 $redirect_fragment = false;
659 if ( isset( $_REQUEST['redirect_fragment'] ) ) {
660 $redirect_fragment = preg_replace( '/[^a-z0-9_-]/i', '', $_REQUEST['redirect_fragment'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- This is manually unslashing and sanitizing.
661 }
662 if ( ! $redirect_fragment || ! is_string( $redirect_fragment ) ) {
663 $redirect_fragment = 'subscribe-blog';
664 }
665
666 $subscribe = self::subscribe(
667 isset( $_REQUEST['email'] ) ? wp_unslash( $_REQUEST['email'] ) : null, // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Validated inside self::subscribe().
668 0,
669 false,
670 array(
671 'source' => 'widget',
672 'widget-in-use' => is_active_widget( false, false, 'blog_subscription', true ) ? 'yes' : 'no',
673 'comment_status' => '',
674 'server_data' => jetpack_subscriptions_cherry_pick_server_data(),
675 )
676 );
677
678 if ( is_wp_error( $subscribe ) ) {
679 $error = $subscribe->get_error_code();
680 } else {
681 $error = false;
682 foreach ( $subscribe as $response ) {
683 if ( is_wp_error( $response ) ) {
684 $error = $response->get_error_code();
685 break;
686 }
687 }
688 }
689
690 switch ( $error ) {
691 case false:
692 $result = 'success';
693 break;
694 case 'invalid_email':
695 $result = $error;
696 break;
697 case 'blocked_email':
698 $result = 'opted_out';
699 break;
700 case 'active':
701 $result = 'already';
702 break;
703 case 'flooded_email':
704 $result = 'many_pending_subs';
705 break;
706 case 'pending':
707 $result = 'pending';
708 break;
709 default:
710 $result = 'error';
711 break;
712 }
713
714 $redirect = add_query_arg( 'subscribe', $result );
715
716 /**
717 * Fires on each subscription form submission.
718 *
719 * @module subscriptions
720 *
721 * @since 3.7.0
722 *
723 * @param string $result Result of form submission: success, invalid_email, already, error.
724 */
725 do_action( 'jetpack_subscriptions_form_submission', $result );
726
727 wp_safe_redirect( "$redirect#$redirect_fragment" );
728 exit;
729 }
730
731 /**
732 * Jetpack_Subscriptions::comment_subscribe_init()
733 *
734 * Set up and add the comment subscription checkbox to the comment form.
735 *
736 * @param string $submit_button HTML markup for the submit field.
737 */
738 public function comment_subscribe_init( $submit_button ) {
739 global $post;
740
741 // Subscriptions are only available for posts so far.
742 if ( ! $post || 'post' !== $post->post_type ) {
743 return $submit_button;
744 }
745
746 $comments_checked = '';
747 $blog_checked = '';
748
749 // Check for a comment / blog submission and set a cookie to retain the setting and check the boxes.
750 if ( isset( $_COOKIE[ 'jetpack_comments_subscribe_' . self::$hash . '_' . $post->ID ] ) ) {
751 $comments_checked = ' checked="checked"';
752 }
753
754 if ( isset( $_COOKIE[ 'jetpack_blog_subscribe_' . self::$hash ] ) ) {
755 $blog_checked = ' checked="checked"';
756 }
757
758 // Some themes call this function, don't show the checkbox again.
759 remove_action( 'comment_form', 'subscription_comment_form' );
760
761 // Check if Mark Jaquith's Subscribe to Comments plugin is active - if so, suppress Jetpack checkbox.
762
763 $str = '';
764
765 if ( false === has_filter( 'comment_form', 'show_subscription_checkbox' ) && 1 === (int) get_option( 'stc_enabled', 1 ) && empty( $post->post_password ) && 'post' === get_post_type() ) {
766 // Subscribe to comments checkbox.
767 $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 . ' /> ';
768 $comment_sub_text = __( 'Notify me of follow-up comments by email.', 'jetpack' );
769 $str .= '<label class="subscribe-label" id="subscribe-label" for="subscribe_comments">' . esc_html(
770 /**
771 * Filter the Subscribe to comments text appearing below the comment form.
772 *
773 * @module subscriptions
774 *
775 * @since 3.4.0
776 *
777 * @param string $comment_sub_text Subscribe to comments text.
778 */
779 apply_filters( 'jetpack_subscribe_comment_label', $comment_sub_text )
780 ) . '</label>';
781 $str .= '</p>';
782 }
783
784 if ( 1 === (int) get_option( 'stb_enabled', 1 ) ) {
785 // Subscribe to blog checkbox.
786 $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 . ' /> ';
787 $blog_sub_text = __( 'Notify me of new posts by email.', 'jetpack' );
788 $str .= '<label class="subscribe-label" id="subscribe-blog-label" for="subscribe_blog">' . esc_html(
789 /**
790 * Filter the Subscribe to blog text appearing below the comment form.
791 *
792 * @module subscriptions
793 *
794 * @since 3.4.0
795 *
796 * @param string $comment_sub_text Subscribe to blog text.
797 */
798 apply_filters( 'jetpack_subscribe_blog_label', $blog_sub_text )
799 ) . '</label>';
800 $str .= '</p>';
801 }
802
803 /**
804 * Filter the output of the subscription options appearing below the comment form.
805 *
806 * @module subscriptions
807 *
808 * @since 1.2.0
809 *
810 * @param string $str Comment Subscription form HTML output.
811 */
812 $str = apply_filters( 'jetpack_comment_subscription_form', $str );
813
814 return $str . $submit_button;
815 }
816
817 /**
818 * Jetpack_Subscriptions::comment_subscribe_init()
819 *
820 * When a user checks the comment subscribe box and submits a comment, subscribe them to the comment thread.
821 *
822 * @param int|string $comment_id Comment thread being subscribed to.
823 * @param string $approved Comment status.
824 */
825 public function comment_subscribe_submit( $comment_id, $approved ) {
826 if ( 'spam' === $approved ) {
827 return;
828 }
829
830 $comment = get_comment( $comment_id );
831 if ( ! $comment ) {
832 return;
833 }
834
835 // Set cookies for this post/comment.
836 $this->set_cookies( isset( $_REQUEST['subscribe_comments'] ), $comment->comment_post_ID, isset( $_REQUEST['subscribe_blog'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
837
838 if ( ! isset( $_REQUEST['subscribe_comments'] ) && ! isset( $_REQUEST['subscribe_blog'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
839 return;
840 }
841
842 $post_ids = array();
843
844 if ( isset( $_REQUEST['subscribe_comments'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
845 $post_ids[] = $comment->comment_post_ID;
846 }
847
848 if ( isset( $_REQUEST['subscribe_blog'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
849 $post_ids[] = 0;
850 }
851
852 $result = self::subscribe(
853 $comment->comment_author_email,
854 $post_ids,
855 true,
856 array(
857 'source' => 'comment-form',
858 'widget-in-use' => is_active_widget( false, false, 'blog_subscription', true ) ? 'yes' : 'no',
859 'comment_status' => $approved,
860 'server_data' => jetpack_subscriptions_cherry_pick_server_data(),
861 )
862 );
863
864 /**
865 * Fires on each comment subscription form submission.
866 *
867 * @module subscriptions
868 *
869 * @since 5.5.0
870 *
871 * @param NULL|WP_Error $result Result of form submission: NULL on success, WP_Error otherwise.
872 * @param array $post_ids An array of post IDs that the user subscribed to, 0 means blog subscription.
873 */
874 do_action( 'jetpack_subscriptions_comment_form_submission', $result, $post_ids );
875 }
876
877 /**
878 * Jetpack_Subscriptions::set_cookies()
879 *
880 * Set a cookie to save state on the comment and post subscription checkboxes.
881 *
882 * @param bool $subscribe_to_post Whether the user chose to subscribe to subsequent comments on this post.
883 * @param int $post_id If $subscribe_to_post is true, the post ID they've subscribed to.
884 * @param bool $subscribe_to_blog Whether the user chose to subscribe to all new posts on the blog.
885 */
886 public function set_cookies( $subscribe_to_post = false, $post_id = null, $subscribe_to_blog = false ) {
887 $post_id = (int) $post_id;
888
889 /** This filter is already documented in core/wp-includes/comment-functions.php */
890 $cookie_lifetime = apply_filters( 'comment_cookie_lifetime', 30000000 );
891
892 /**
893 * Filter the Jetpack Comment cookie path.
894 *
895 * @module subscriptions
896 *
897 * @since 2.5.0
898 *
899 * @param string COOKIEPATH Cookie path.
900 */
901 $cookie_path = apply_filters( 'jetpack_comment_cookie_path', COOKIEPATH );
902
903 /**
904 * Filter the Jetpack Comment cookie domain.
905 *
906 * @module subscriptions
907 *
908 * @since 2.5.0
909 *
910 * @param string COOKIE_DOMAIN Cookie domain.
911 */
912 $cookie_domain = apply_filters( 'jetpack_comment_cookie_domain', COOKIE_DOMAIN );
913
914 if ( $subscribe_to_post && $post_id >= 0 ) {
915 setcookie( 'jetpack_comments_subscribe_' . self::$hash . '_' . $post_id, 1, time() + $cookie_lifetime, $cookie_path, $cookie_domain, is_ssl(), true );
916 } else {
917 setcookie( 'jetpack_comments_subscribe_' . self::$hash . '_' . $post_id, '', time() - 3600, $cookie_path, $cookie_domain, is_ssl(), true );
918 }
919
920 if ( $subscribe_to_blog ) {
921 setcookie( 'jetpack_blog_subscribe_' . self::$hash, 1, time() + $cookie_lifetime, $cookie_path, $cookie_domain, is_ssl(), true );
922 } else {
923 setcookie( 'jetpack_blog_subscribe_' . self::$hash, '', time() - 3600, $cookie_path, $cookie_domain, is_ssl(), true );
924 }
925 }
926
927 /**
928 * Set the social_notifications_subscribe option to `off` when the Subscriptions module is activated in the first time.
929 *
930 * @since 8.1
931 *
932 * @return void
933 */
934 public function set_social_notifications_subscribe() {
935 if ( false === get_option( 'social_notifications_subscribe' ) ) {
936 add_option( 'social_notifications_subscribe', 'off' );
937 }
938 }
939
940 /**
941 * Save a flag when a post was ever published.
942 *
943 * It saves the post meta when the post was published and becomes a draft.
944 * Then this meta is used to hide subscription messaging in Publish panel.
945 *
946 * @param string $new_status Tthe "new" post status of the transition when saved.
947 * @param string $old_status The "old" post status of the transition when saved.
948 * @param object $post obj The post object.
949 */
950 public function maybe_set_first_published_status( $new_status, $old_status, $post ) {
951 $was_post_ever_published = get_post_meta( $post->ID, '_jetpack_post_was_ever_published', true );
952 if ( ! $was_post_ever_published && 'publish' === $old_status && 'draft' === $new_status ) {
953 update_post_meta( $post->ID, '_jetpack_post_was_ever_published', true );
954 }
955 }
956
957 /**
958 * Checks if the current user can publish posts.
959 *
960 * @return bool
961 */
962 public function first_published_status_meta_auth_callback() {
963 /**
964 * Filter the capability to view if a post was ever published in the Subscription Module.
965 *
966 * @module subscriptions
967 *
968 * @since 13.4
969 *
970 * @param string $capability User capability needed to view if a post was ever published. Default to publish_posts.
971 */
972 $capability = apply_filters( 'jetpack_subscriptions_post_was_ever_published_capability', 'publish_posts' );
973 if ( current_user_can( $capability ) ) {
974 return true;
975 }
976 return false;
977 }
978
979 /**
980 * Registers the 'post_was_ever_published' post meta for use in the REST API.
981 */
982 public function register_post_meta() {
983 $jetpack_post_was_ever_published = array(
984 'type' => 'boolean',
985 'description' => __( 'Whether the post was ever published.', 'jetpack' ),
986 'single' => true,
987 'default' => false,
988 'show_in_rest' => array(
989 'name' => 'jetpack_post_was_ever_published',
990 ),
991 'auth_callback' => array( $this, 'first_published_status_meta_auth_callback' ),
992 );
993
994 register_meta( 'post', '_jetpack_post_was_ever_published', $jetpack_post_was_ever_published );
995 }
996
997 /**
998 * Create a Subscribers menu displayed on self-hosted sites.
999 *
1000 * - It is not displayed on WordPress.com sites.
1001 * - It directs you to Calypso to the existing Subscribers page.
1002 *
1003 * @return void
1004 */
1005 public function add_subscribers_menu() {
1006 /*
1007 * Do not display any menu on WoA and WordPress.com Simple sites (unless Classic wp-admin is enabled).
1008 * They already get a menu item under Users via nav-unification.
1009 */
1010 if ( ( new Host() )->is_wpcom_platform() && get_option( 'wpcom_admin_interface' ) !== 'wp-admin' ) {
1011 return;
1012 }
1013
1014 $status = new Status();
1015
1016 /*
1017 * Do not display if we're in Offline mode,
1018 * or if the user is not connected.
1019 */
1020 if (
1021 $status->is_offline_mode()
1022 || ! ( new Connection_Manager( 'jetpack' ) )->is_user_connected()
1023 ) {
1024 return;
1025 }
1026
1027 $blog_id = Connection_Manager::get_site_id( true );
1028
1029 $link = Redirect::get_url(
1030 'jetpack-menu-jetpack-manage-subscribers',
1031 array( 'site' => $blog_id ? $blog_id : $status->get_site_suffix() )
1032 );
1033
1034 add_submenu_page(
1035 'jetpack',
1036 esc_attr__( 'Subscribers', 'jetpack' ),
1037 __( 'Subscribers', 'jetpack' ) . ' <span class="dashicons dashicons-external"></span>',
1038 'manage_options',
1039 esc_url( $link ),
1040 null
1041 );
1042 }
1043
1044 /**
1045 * Record tracks event if categories is created when user enters
1046 * the edit category page through the newsletter settings page.
1047 *
1048 * @return void
1049 */
1050 public function track_newsletter_category_creation() {
1051
1052 // phpcs:disable WordPress.Security.NonceVerification.Missing
1053 if ( empty( $_POST['_wp_http_referer'] ) ) {
1054 return;
1055 }
1056
1057 if ( strpos( sanitize_url( wp_unslash( $_POST['_wp_http_referer'] ) ), 'referer=newsletter-categories' ) > -1 ) {
1058
1059 $parent = filter_var( empty( $_POST['parent'] ) ? 0 : wp_unslash( $_POST['parent'] ), FILTER_SANITIZE_NUMBER_INT );
1060
1061 $is_child_category = $parent > 0;
1062
1063 $tracking = new Automattic\Jetpack\Tracking();
1064 $tracking->tracks_record_event(
1065 wp_get_current_user(),
1066 'jetpack_newsletter_add_category',
1067 array(
1068 'is_child_category' => $is_child_category,
1069 )
1070 );
1071 }
1072 }
1073 }
1074
1075 Jetpack_Subscriptions::init();
1076
1077 require __DIR__ . '/subscriptions/views.php';
1078 require __DIR__ . '/subscriptions/subscribe-modal/class-jetpack-subscribe-modal.php';
1079 require __DIR__ . '/subscriptions/subscribe-overlay/class-jetpack-subscribe-overlay.php';
1080