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

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