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

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