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