PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 9.2
Jetpack – WP Security, Backup, Speed, & Growth v9.2
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
← All changes | modules/subscriptions.php +147 -291 12.3.29.2 View file →
@@ -1,5 +1,5 @@
1 -<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName)
1 +<?php
2 2 /**
3 3 * Module Name: Subscriptions
4 4 * Module Description: Let visitors subscribe to new posts and comments via email
5 5 * Sort Order: 9
@@ -5,9 +5,8 @@
5 5 * Sort Order: 9
6 6 * Recommendation Order: 8
7 7 * First Introduced: 1.2
8 8 * Requires Connection: Yes
9 - * Requires User Connection: Yes
10 9 * Auto Activate: No
11 10 * Module Tags: Social
12 11 * Feature: Engagement
13 12 * Additional Search Queries: subscriptions, subscription, email, follow, followers, subscribers, signup
@@ -12,17 +11,12 @@
12 11 * Feature: Engagement
13 12 * Additional Search Queries: subscriptions, subscription, email, follow, followers, subscribers, signup
14 13 */
15 14
16 -// phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files.
17 -
18 15 use Automattic\Jetpack\Connection\XMLRPC_Async_Call;
19 16
20 17 add_action( 'jetpack_modules_loaded', 'jetpack_subscriptions_load' );
21 18
22 -/**
23 - * Loads the Subscriptions module.
24 - */
25 19 function jetpack_subscriptions_load() {
26 20 Jetpack::enable_module_configurable( __FILE__ );
27 21 }
28 22
@@ -48,49 +42,32 @@
48 42
49 43 return $data;
50 44 }
51 45
52 -/**
53 - * Main class file for the Subscriptions module.
54 - */
55 46 class Jetpack_Subscriptions {
56 - /**
57 - * Whether Jetpack has been instantiated or not.
58 - *
59 - * @var bool
60 - */
61 47 public $jetpack = false;
62 48
63 - /**
64 - * Hash of the siteurl option.
65 - *
66 - * @var string
67 - */
68 49 public static $hash;
69 50
70 51 /**
71 52 * Singleton
72 - *
73 53 * @static
74 54 */
75 - public static function init() {
55 + static function init() {
76 56 static $instance = false;
77 57
78 - if ( ! $instance ) {
79 - $instance = new Jetpack_Subscriptions();
58 + if ( !$instance ) {
59 + $instance = new Jetpack_Subscriptions;
80 60 }
81 61
82 62 return $instance;
83 63 }
84 64
85 - /**
86 - * Jetpack_Subscriptions constructor.
87 - */
88 - public function __construct() {
65 + function __construct() {
89 66 $this->jetpack = Jetpack::init();
90 67
91 68 // Don't use COOKIEHASH as it could be shared across installs && is non-unique in multisite.
92 - // @see: https://twitter.com/nacin/status/378246957451333632 .
69 + // @see: https://twitter.com/nacin/status/378246957451333632
93 70 self::$hash = md5( get_option( 'siteurl' ) );
94 71
95 72 add_filter( 'jetpack_xmlrpc_methods', array( $this, 'xmlrpc_methods' ) );
96 73
@@ -95,23 +72,22 @@
95 72 add_filter( 'jetpack_xmlrpc_methods', array( $this, 'xmlrpc_methods' ) );
96 73
97 74 // @todo remove sync from subscriptions and move elsewhere...
98 75
99 - // Add Configuration Page.
76 + // Add Configuration Page
100 77 add_action( 'admin_init', array( $this, 'configure' ) );
101 78
102 - // Catch subscription widget submits.
103 - if ( isset( $_REQUEST['jetpack_subscriptions_widget'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce checked in widget_submit() for logged in users.
79 + // Catch subscription widget submits
80 + if ( isset( $_REQUEST['jetpack_subscriptions_widget'] ) )
104 81 add_action( 'template_redirect', array( $this, 'widget_submit' ) );
105 - }
106 82
107 - // Set up the comment subscription checkboxes.
83 + // Set up the comment subscription checkboxes
108 84 add_filter( 'comment_form_submit_field', array( $this, 'comment_subscribe_init' ), 10, 2 );
109 85
110 86 // Catch comment posts and check for subscriptions.
111 87 add_action( 'comment_post', array( $this, 'comment_subscribe_submit' ), 50, 2 );
112 88
113 - // Adds post meta checkbox in the post submit metabox.
89 + // Adds post meta checkbox in the post submit metabox
114 90 add_action( 'post_submitbox_misc_actions', array( $this, 'subscription_post_page_metabox' ) );
115 91
116 92 add_action( 'transition_post_status', array( $this, 'maybe_send_subscription_email' ), 10, 3 );
117 93
@@ -119,13 +95,9 @@
119 95
120 96 add_filter( 'post_updated_messages', array( $this, 'update_published_message' ), 18, 1 );
121 97
122 98 // Set "social_notifications_subscribe" option during the first-time activation.
123 - add_action( 'jetpack_activate_module_subscriptions', array( $this, 'set_social_notifications_subscribe' ) );
124 -
125 - // Hide subscription messaging in Publish panel for posts that were published in the past
126 - add_action( 'init', array( $this, 'register_post_meta' ), 20 );
127 - add_action( 'transition_post_status', array( $this, 'maybe_set_first_published_status' ), 10, 3 );
99 + add_action( 'jetpack_activate_module_subscriptions', array( $this, 'set_social_notifications_subscribe' ) );
128 100 }
129 101
130 102 /**
131 103 * Jetpack_Subscriptions::xmlrpc_methods()
@@ -130,12 +102,11 @@
130 102 /**
131 103 * Jetpack_Subscriptions::xmlrpc_methods()
132 104 *
133 105 * Register subscriptions methods with the Jetpack XML-RPC server.
134 - *
135 - * @param array $methods Methods being registered.
106 + * @param array $methods
136 107 */
137 - public function xmlrpc_methods( $methods ) {
108 + function xmlrpc_methods( $methods ) {
138 109 return array_merge(
139 110 $methods,
140 111 array(
141 112 'jetpack.subscriptions.subscribe' => array( $this, 'subscribe' ),
@@ -142,13 +113,13 @@
142 113 )
143 114 );
144 115 }
145 116
146 - /**
117 + /*
147 118 * Disable Subscribe on Single Post
148 119 * Register post meta
149 120 */
150 - public function subscription_post_page_metabox() {
121 + function subscription_post_page_metabox() {
151 122 if (
152 123 /**
153 124 * Filter whether or not to show the per-post subscription option.
154 125 *
@@ -157,9 +128,10 @@
157 128 * @since 3.7.0
158 129 *
159 130 * @param bool true = show checkbox option on all new posts | false = hide the option.
160 131 */
161 - ! apply_filters( 'jetpack_allow_per_post_subscriptions', false ) ) {
132 + ! apply_filters( 'jetpack_allow_per_post_subscriptions', false ) )
133 + {
162 134 return;
163 135 }
164 136
165 137 if ( has_filter( 'jetpack_subscriptions_exclude_these_categories' ) || has_filter( 'jetpack_subscriptions_include_only_these_categories' ) ) {
@@ -168,19 +140,18 @@
168 140
169 141 global $post;
170 142 $disable_subscribe_value = get_post_meta( $post->ID, '_jetpack_dont_email_post_to_subs', true );
171 143 // only show checkbox if post hasn't been published and is a 'post' post type.
172 - if ( get_post_status( $post->ID ) !== 'publish' && get_post_type( $post->ID ) === 'post' ) :
173 - // Nonce it.
144 + if ( get_post_status( $post->ID ) !== 'publish' && get_post_type( $post->ID ) == 'post' ) :
145 + // Nonce it
174 146 wp_nonce_field( 'disable_subscribe', 'disable_subscribe_nonce' );
175 147 ?>
176 148 <div class="misc-pub-section">
177 - <label for="_jetpack_dont_email_post_to_subs"><?php esc_html_e( 'Jetpack Subscriptions:', 'jetpack' ); ?></label><br>
149 + <label for="_jetpack_dont_email_post_to_subs"><?php _e( 'Jetpack Subscriptions:', 'jetpack' ); ?></label><br>
178 150 <input type="checkbox" name="_jetpack_dont_email_post_to_subs" id="jetpack-per-post-subscribe" value="1" <?php checked( $disable_subscribe_value, 1, true ); ?> />
179 - <?php esc_html_e( 'Don&#8217;t send this to subscribers', 'jetpack' ); ?>
151 + <?php _e( 'Don&#8217;t send this to subscribers', 'jetpack' ); ?>
180 152 </div>
181 - <?php
182 - endif;
153 + <?php endif;
183 154 }
184 155
185 156 /**
186 157 * Checks whether or not the post should be emailed to subscribers
@@ -191,38 +162,32 @@
191 162 * - Existence of the per-post checkbox option
192 163 *
193 164 * Only one of these can be used at any given time.
194 165 *
195 - * @param string $new_status Tthe "new" post status of the transition when saved.
196 - * @param string $old_status The "old" post status of the transition when saved.
197 - * @param object $post obj The post object.
166 + * @param $new_status string - the "new" post status of the transition when saved
167 + * @param $old_status string - the "old" post status of the transition when saved
168 + * @param $post obj - The post object
198 169 */
199 - public function maybe_send_subscription_email( $new_status, $old_status, $post ) {
170 + function maybe_send_subscription_email( $new_status, $old_status, $post ) {
200 171
201 172 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
202 173 return;
203 174 }
204 175
205 - // Make sure that the checkbox is preseved.
206 - 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.
176 + // Make sure that the checkbox is preseved
177 + if ( ! empty( $_POST['disable_subscribe_nonce'] ) && wp_verify_nonce( $_POST['disable_subscribe_nonce'], 'disable_subscribe' ) ) {
207 178 $set_checkbox = isset( $_POST['_jetpack_dont_email_post_to_subs'] ) ? 1 : 0;
208 179 update_post_meta( $post->ID, '_jetpack_dont_email_post_to_subs', $set_checkbox );
209 180 }
210 181 }
211 182
212 - /**
213 - * Message used when publishing a post.
214 - *
215 - * @param array $messages Message array for a post.
216 - */
217 - public function update_published_message( $messages ) {
183 + function update_published_message( $messages ) {
218 184 global $post;
219 185 if ( ! $this->should_email_post_to_subscribers( $post ) ) {
220 186 return $messages;
221 187 }
222 188
223 - $view_post_link_html = sprintf(
224 - ' <a href="%1$s">%2$s</a>',
189 + $view_post_link_html = sprintf( ' <a href="%1$s">%2$s</a>',
225 190 esc_url( get_permalink( $post ) ),
226 191 __( 'View post', 'jetpack' )
227 192 );
228 193
@@ -228,17 +193,12 @@
228 193
229 194 $messages['post'][6] = sprintf(
230 195 /* translators: Message shown after a post is published */
231 196 esc_html__( 'Post published and sending emails to subscribers.', 'jetpack' )
232 - ) . $view_post_link_html;
197 + ) . $view_post_link_html;
233 198 return $messages;
234 199 }
235 200
236 - /**
237 - * Determine if a post should notifiy subscribers via email.
238 - *
239 - * @param object $post The post.
240 - */
241 201 public function should_email_post_to_subscribers( $post ) {
242 202 $should_email = true;
243 203 if ( get_post_meta( $post->ID, '_jetpack_dont_email_post_to_subs', true ) ) {
244 204 return false;
@@ -243,10 +203,10 @@
243 203 if ( get_post_meta( $post->ID, '_jetpack_dont_email_post_to_subs', true ) ) {
244 204 return false;
245 205 }
246 206
247 - // Only posts are currently supported.
248 - if ( 'post' !== $post->post_type ) {
207 + // Only posts are currently supported
208 + if ( $post->post_type !== 'post' ) {
249 209 return false;
250 210 }
251 211
252 212 // Private posts are not sent to subscribers.
@@ -266,9 +226,9 @@
266 226 * @param array $args Array of category slugs or ID's.
267 227 */
268 228 $excluded_categories = apply_filters( 'jetpack_subscriptions_exclude_these_categories', array() );
269 229
270 - // Never email posts from these categories.
230 + // Never email posts from these categories
271 231 if ( ! empty( $excluded_categories ) && in_category( $excluded_categories, $post->ID ) ) {
272 232 $should_email = false;
273 233 }
274 234
@@ -284,9 +244,9 @@
284 244 * @param array $args Array of category slugs or ID's.
285 245 */
286 246 $only_these_categories = apply_filters( 'jetpack_subscriptions_exclude_all_categories_except', array() );
287 247
288 - // Only emails posts from these categories.
248 + // Only emails posts from these categories
289 249 if ( ! empty( $only_these_categories ) && ! in_category( $only_these_categories, $post->ID ) ) {
290 250 $should_email = false;
291 251 }
292 252
@@ -292,15 +252,9 @@
292 252
293 253 return $should_email;
294 254 }
295 255
296 - /**
297 - * Retrieve which flags should be added to a particular post.
298 - *
299 - * @param array $flags Flags to be added.
300 - * @param object $post A post object.
301 - */
302 - public function set_post_flags( $flags, $post ) {
256 + function set_post_flags( $flags, $post ) {
303 257 $flags['send_subscription'] = $this->should_email_post_to_subscribers( $post );
304 258 return $flags;
305 259 }
306 260
@@ -308,10 +262,10 @@
308 262 * Jetpack_Subscriptions::configure()
309 263 *
310 264 * Jetpack Subscriptions configuration screen.
311 265 */
312 - public function configure() {
313 - // Create the section.
266 + function configure() {
267 + // Create the section
314 268 add_settings_section(
315 269 'jetpack_subscriptions',
316 270 __( 'Jetpack Subscriptions Settings', 'jetpack' ),
317 271 array( $this, 'subscriptions_settings_section' ),
@@ -317,9 +271,9 @@
317 271 array( $this, 'subscriptions_settings_section' ),
318 272 'discussion'
319 273 );
320 274
321 - /** Subscribe to Posts */
275 + /** Subscribe to Posts ***************************************************/
322 276
323 277 add_settings_field(
324 278 'jetpack_subscriptions_post_subscribe',
325 279 __( 'Follow Blog', 'jetpack' ),
@@ -332,9 +286,9 @@
332 286 'discussion',
333 287 'stb_enabled'
334 288 );
335 289
336 - /** Subscribe to Comments */
290 + /** Subscribe to Comments ******************************************************/
337 291
338 292 add_settings_field(
339 293 'jetpack_subscriptions_comment_subscribe',
340 294 __( 'Follow Comments', 'jetpack' ),
@@ -347,9 +301,9 @@
347 301 'discussion',
348 302 'stc_enabled'
349 303 );
350 304
351 - /** Email me whenever: Someone follows my blog */
305 + /** Email me whenever: Someone follows my blog ***************************************************/
352 306 /* @since 8.1 */
353 307
354 308 add_settings_section(
355 309 'notifications_section',
@@ -371,9 +325,9 @@
371 325 'social_notifications_subscribe',
372 326 array( $this, 'social_notifications_subscribe_validate' )
373 327 );
374 328
375 - /** Subscription Messaging Options */
329 + /** Subscription Messaging Options ******************************************************/
376 330
377 331 register_setting(
378 332 'reading',
379 333 'subscription_options',
@@ -404,62 +358,47 @@
404 358 );
405 359 }
406 360
407 361 /**
408 - * Discussions setting section blurb.
362 + * Discussions setting section blurb
363 + *
409 364 */
410 - public function subscriptions_settings_section() {
411 - ?>
412 - <p id="jetpack-subscriptions-settings"><?php esc_html_e( 'Change whether your visitors can subscribe to your posts or comments or both.', 'jetpack' ); ?></p>
365 + function subscriptions_settings_section() {
366 + ?>
367 + <p id="jetpack-subscriptions-settings"><?php _e( 'Change whether your visitors can subscribe to your posts or comments or both.', 'jetpack' ); ?></p>
413 368
414 - <?php
369 + <?php
415 370 }
416 371
417 372 /**
418 - * Post Subscriptions Toggle.
373 + * Post Subscriptions Toggle
374 + *
419 375 */
420 - public function subscription_post_subscribe_setting() {
376 + function subscription_post_subscribe_setting() {
421 377
422 - $stb_enabled = get_option( 'stb_enabled', 1 );
423 - ?>
378 + $stb_enabled = get_option( 'stb_enabled', 1 ); ?>
424 379
425 380 <p class="description">
426 381 <input type="checkbox" name="stb_enabled" id="jetpack-post-subscribe" value="1" <?php checked( $stb_enabled, 1 ); ?> />
427 - <?php
428 - echo wp_kses(
429 - __(
430 - "Show a <em>'follow blog'</em> option in the comment form",
431 - 'jetpack'
432 - ),
433 - array( 'em' => array() )
434 - );
435 - ?>
382 + <?php _e( "Show a <em>'follow blog'</em> option in the comment form", 'jetpack' ); ?>
436 383 </p>
437 - <?php
384 + <?php
438 385 }
439 386
440 387 /**
441 - * Comments Subscriptions Toggle.
388 + * Comments Subscriptions Toggle
389 + *
442 390 */
443 - public function subscription_comment_subscribe_setting() {
391 + function subscription_comment_subscribe_setting() {
444 392
445 - $stc_enabled = get_option( 'stc_enabled', 1 );
446 - ?>
393 + $stc_enabled = get_option( 'stc_enabled', 1 ); ?>
447 394
448 395 <p class="description">
449 396 <input type="checkbox" name="stc_enabled" id="jetpack-comment-subscribe" value="1" <?php checked( $stc_enabled, 1 ); ?> />
450 - <?php
451 - echo wp_kses(
452 - __(
453 - "Show a <em>'follow comments'</em> option in the comment form",
454 - 'jetpack'
455 - ),
456 - array( 'em' => array() )
457 - );
458 - ?>
397 + <?php _e( "Show a <em>'follow comments'</em> option in the comment form", 'jetpack' ); ?>
459 398 </p>
460 399
461 - <?php
400 + <?php
462 401 }
463 402
464 403 /**
465 404 * Someone follows my blog section
@@ -466,9 +405,9 @@
466 405 *
467 406 * @since 8.1
468 407 */
469 408 public function social_notifications_subscribe_section() {
470 - // Atypical usage here. We emit jquery to move subscribe notification checkbox to be with the rest of the email notification settings.
409 + // Atypical usage here. We emit jquery to move subscribe notification checkbox to be with the rest of the email notification settings
471 410 ?>
472 411 <script type="text/javascript">
473 412 jQuery( function( $ ) {
474 413 var table = $( '#social_notifications_subscribe' ).parents( 'table:first' ),
@@ -523,79 +462,53 @@
523 462 // Otherwise we return 'on'.
524 463 return 'on';
525 464 }
526 465
527 - /**
528 - * Validate settings for the Subscriptions module.
529 - *
530 - * @param array $settings Settings to be validated.
531 - */
532 - public function validate_settings( $settings ) {
466 + function validate_settings( $settings ) {
533 467 global $allowedposttags;
534 468
535 469 $default = $this->get_default_settings();
536 470
537 - // Blog Follow.
471 + // Blog Follow
538 472 $settings['invitation'] = trim( wp_kses( $settings['invitation'], $allowedposttags ) );
539 - if ( empty( $settings['invitation'] ) ) {
473 + if ( empty( $settings['invitation'] ) )
540 474 $settings['invitation'] = $default['invitation'];
541 - }
542 475
543 - // Comments Follow (single post).
476 + // Comments Follow (single post)
544 477 $settings['comment_follow'] = trim( wp_kses( $settings['comment_follow'], $allowedposttags ) );
545 - if ( empty( $settings['comment_follow'] ) ) {
478 + if ( empty( $settings['comment_follow'] ) )
546 479 $settings['comment_follow'] = $default['comment_follow'];
547 - }
548 480
549 481 return $settings;
550 482 }
551 483
552 - /**
553 - * HTML output helper for Reading section.
554 - */
555 484 public function reading_section() {
556 485 echo '<p id="follower-settings">';
557 - esc_html_e( 'These settings change emails sent from your blog to followers.', 'jetpack' );
486 + _e( 'These settings change emails sent from your blog to followers.', 'jetpack' );
558 487 echo '</p>';
559 488 }
560 489
561 - /**
562 - * HTML output helper for Invitation section.
563 - */
564 490 public function setting_invitation() {
565 491 $settings = $this->get_settings();
566 492 echo '<textarea name="subscription_options[invitation]" class="large-text" cols="50" rows="5">' . esc_textarea( $settings['invitation'] ) . '</textarea>';
567 - echo '<p><span class="description">' . esc_html__( 'Introduction text sent when someone follows your blog. (Site and confirmation details will be automatically added for you.)', 'jetpack' ) . '</span></p>';
493 + echo '<p><span class="description">'.__( 'Introduction text sent when someone follows your blog. (Site and confirmation details will be automatically added for you.)', 'jetpack' ).'</span></p>';
568 494 }
569 495
570 - /**
571 - * HTML output helper for Comment Follow section.
572 - */
573 496 public function setting_comment_follow() {
574 497 $settings = $this->get_settings();
575 498 echo '<textarea name="subscription_options[comment_follow]" class="large-text" cols="50" rows="5">' . esc_textarea( $settings['comment_follow'] ) . '</textarea>';
576 - echo '<p><span class="description">' . esc_html__( 'Introduction text sent when someone follows a post on your blog. (Site and confirmation details will be automatically added for you.)', 'jetpack' ) . '</span></p>';
499 + echo '<p><span class="description">'.__( 'Introduction text sent when someone follows a post on your blog. (Site and confirmation details will be automatically added for you.)', 'jetpack' ).'</span></p>';
577 500 }
578 501
579 - /**
580 - * Get default settings for the Subscriptions module.
581 - */
582 - public function get_default_settings() {
583 - $site_url = get_home_url();
584 - $display_url = preg_replace( '(^https?://)', '', untrailingslashit( $site_url ) );
585 -
502 + function get_default_settings() {
586 503 return array(
587 - /* translators: Both %1$s and %2$s is site address */
588 - 'invitation' => sprintf( __( "Howdy,\nYou recently subscribed to <a href='%1\$s'>%2\$s</a> and we need to verify the email you provided. Once you confirm below, you'll be able to receive and read new posts.\n\nIf you believe this is an error, ignore this message and nothing more will happen.", 'jetpack' ), $site_url, $display_url ),
589 - 'comment_follow' => __( "Howdy.\n\nYou recently followed one of my posts. This means you will receive an email when new comments are posted.\n\nTo activate, click confirm below. If you believe this is an error, ignore this message and we'll never bother you again.", 'jetpack' ),
504 + 'invitation' => __( "Howdy.\n\nYou recently followed this blog's posts. This means you will receive each new post by email.\n\nTo activate, click confirm below. If you believe this is an error, ignore this message and we'll never bother you again.", 'jetpack' ),
505 + 'comment_follow' => __( "Howdy.\n\nYou recently followed one of my posts. This means you will receive an email when new comments are posted.\n\nTo activate, click confirm below. If you believe this is an error, ignore this message and we'll never bother you again.", 'jetpack' )
590 506 );
591 507 }
592 508
593 - /**
594 - * Reeturn merged `subscription_options` option with module default settings.
595 - */
596 - public function get_settings() {
597 - return wp_parse_args( (array) get_option( 'subscription_options' ), $this->get_default_settings() );
509 + function get_settings() {
510 + return wp_parse_args( (array) get_option( 'subscription_options', array() ), $this->get_default_settings() );
598 511 }
599 512
600 513 /**
601 514 * Jetpack_Subscriptions::subscribe()
@@ -601,30 +514,29 @@
601 514 * Jetpack_Subscriptions::subscribe()
602 515 *
603 516 * Send a synchronous XML-RPC subscribe to blog posts or subscribe to post comments request.
604 517 *
605 - * @param string $email being subscribed.
606 - * @param array $post_ids (optional) defaults to 0 for blog posts only: array of post IDs to subscribe to blog's posts.
518 + * @param string $email
519 + * @param array $post_ids (optional) defaults to 0 for blog posts only: array of post IDs to subscribe to blog's posts
607 520 * @param bool $async (optional) Should the subscription be performed asynchronously? Defaults to true.
608 - * @param array $extra_data Additional data passed to the `jetpack.subscribeToSite` call.
609 521 *
610 522 * @return true|WP_Error true on success
611 - * invalid_email : not a valid email address
612 - * invalid_post_id : not a valid post ID
613 - * unknown_post_id : unknown post
614 - * not_subscribed : strange error. Jetpack servers at WordPress.com could subscribe the email.
615 - * disabled : Site owner has disabled subscriptions.
616 - * active : Already subscribed.
617 - * pending : Tried to subscribe before but the confirmation link is never clicked. No confirmation email is sent.
618 - * unknown : strange error. Jetpack servers at WordPress.com returned something malformed.
619 - * unknown_status : strange error. Jetpack servers at WordPress.com returned something I didn't understand.
523 + * invalid_email : not a valid email address
524 + * invalid_post_id : not a valid post ID
525 + * unknown_post_id : unknown post
526 + * not_subscribed : strange error. Jetpack servers at WordPress.com could subscribe the email.
527 + * disabled : Site owner has disabled subscriptions.
528 + * active : Already subscribed.
529 + * pending : Tried to subscribe before but the confirmation link is never clicked. No confirmation email is sent.
530 + * unknown : strange error. Jetpack servers at WordPress.com returned something malformed.
531 + * unknown_status : strange error. Jetpack servers at WordPress.com returned something I didn't understand.
620 532 */
621 - public function subscribe( $email, $post_ids = 0, $async = true, $extra_data = array() ) {
622 - if ( ! is_email( $email ) ) {
533 + function subscribe( $email, $post_ids = 0, $async = true, $extra_data = array() ) {
534 + if ( !is_email( $email ) ) {
623 535 return new WP_Error( 'invalid_email' );
624 536 }
625 537
626 - if ( ! $async ) {
538 + if ( !$async ) {
627 539 $xml = new Jetpack_IXR_ClientMulticall();
628 540 }
629 541
630 542 foreach ( (array) $post_ids as $post_id ) {
@@ -630,9 +542,9 @@
630 542 foreach ( (array) $post_ids as $post_id ) {
631 543 $post_id = (int) $post_id;
632 544 if ( $post_id < 0 ) {
633 545 return new WP_Error( 'invalid_post_id' );
634 - } elseif ( $post_id && ! get_post( $post_id ) ) {
546 + } else if ( $post_id && !$post = get_post( $post_id ) ) {
635 547 return new WP_Error( 'unknown_post_id' );
636 548 }
637 549
638 550 if ( $async ) {
@@ -637,9 +549,9 @@
637 549
638 550 if ( $async ) {
639 551 XMLRPC_Async_Call::add_call( 'jetpack.subscribeToSite', 0, $email, $post_id, serialize( $extra_data ) ); //phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
640 552 } else {
641 - $xml->addCall( 'jetpack.subscribeToSite', $email, $post_id, serialize( $extra_data ) ); //phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
553 + $xml->addCall( 'jetpack.subscribeToSite', $email, $post_id, serialize( $extra_data ) );
642 554 }
643 555 }
644 556
645 557 if ( $async ) {
@@ -645,9 +557,9 @@
645 557 if ( $async ) {
646 558 return;
647 559 }
648 560
649 - // Call.
561 + // Call
650 562 $xml->query();
651 563
652 564 if ( $xml->isError() ) {
653 565 return $xml->get_jetpack_error();
@@ -661,9 +573,9 @@
661 573 $r[] = $xml->get_jetpack_error( $response['faultCode'], $response['faultString'] );
662 574 continue;
663 575 }
664 576
665 - if ( ! is_array( $response[0] ) || empty( $response[0]['status'] ) ) {
577 + if ( !is_array( $response[0] ) || empty( $response[0]['status'] ) ) {
666 578 $r[] = new WP_Error( 'unknown' );
667 579 continue;
668 580 }
669 581
@@ -696,36 +608,35 @@
696 608 * Jetpack_Subscriptions::widget_submit()
697 609 *
698 610 * When a user submits their email via the blog subscription widget, check the details and call the subsribe() method.
699 611 */
700 - public function widget_submit() {
612 + function widget_submit() {
701 613 // Check the nonce.
702 614 if ( is_user_logged_in() ) {
703 615 check_admin_referer( 'blogsub_subscribe_' . get_current_blog_id() );
704 616 }
705 617
706 - if ( empty( $_REQUEST['email'] ) || ! is_string( $_REQUEST['email'] ) ) {
618 + if ( empty( $_REQUEST['email'] ) || ! is_string( $_REQUEST['email'] ) )
707 619 return false;
708 - }
709 620
710 621 $redirect_fragment = false;
711 622 if ( isset( $_REQUEST['redirect_fragment'] ) ) {
712 - $redirect_fragment = preg_replace( '/[^a-z0-9_-]/i', '', $_REQUEST['redirect_fragment'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- This is manually unslashing and sanitizing.
623 + $redirect_fragment = preg_replace( '/[^a-z0-9_-]/i', '', $_REQUEST['redirect_fragment'] );
713 624 }
714 - if ( ! $redirect_fragment || ! is_string( $redirect_fragment ) ) {
625 + if ( !$redirect_fragment || ! is_string( $redirect_fragment ) ) {
715 626 $redirect_fragment = 'subscribe-blog';
716 627 }
717 628
718 - $subscribe = self::subscribe(
719 - isset( $_REQUEST['email'] ) ? wp_unslash( $_REQUEST['email'] ) : null, // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Validated inside self::subscribe().
720 - 0,
721 - false,
722 - array(
723 - 'source' => 'widget',
724 - 'widget-in-use' => is_active_widget( false, false, 'blog_subscription', true ) ? 'yes' : 'no',
725 - 'comment_status' => '',
726 - 'server_data' => jetpack_subscriptions_cherry_pick_server_data(),
727 - )
629 + $subscribe = Jetpack_Subscriptions::subscribe(
630 + $_REQUEST['email'],
631 + 0,
632 + false,
633 + array(
634 + 'source' => 'widget',
635 + 'widget-in-use' => is_active_widget( false, false, 'blog_subscription', true ) ? 'yes' : 'no',
636 + 'comment_status' => '',
637 + 'server_data' => jetpack_subscriptions_cherry_pick_server_data(),
638 + )
728 639 );
729 640
730 641 if ( is_wp_error( $subscribe ) ) {
731 642 $error = $subscribe->get_error_code();
@@ -785,10 +696,11 @@
785 696 *
786 697 * Set up and add the comment subscription checkbox to the comment form.
787 698 *
788 699 * @param string $submit_button HTML markup for the submit field.
700 + * @param array $args Arguments passed to `comment_form()`.
789 701 */
790 - public function comment_subscribe_init( $submit_button ) {
702 + function comment_subscribe_init( $submit_button, $args ) {
791 703 global $post;
792 704
793 705 $comments_checked = '';
794 706 $blog_checked = '';
@@ -801,20 +713,20 @@
801 713 if ( isset( $_COOKIE[ 'jetpack_blog_subscribe_' . self::$hash ] ) ) {
802 714 $blog_checked = ' checked="checked"';
803 715 }
804 716
805 - // Some themes call this function, don't show the checkbox again.
717 + // Some themes call this function, don't show the checkbox again
806 718 remove_action( 'comment_form', 'subscription_comment_form' );
807 719
808 - // Check if Mark Jaquith's Subscribe to Comments plugin is active - if so, suppress Jetpack checkbox.
720 + // Check if Mark Jaquith's Subscribe to Comments plugin is active - if so, suppress Jetpack checkbox
809 721
810 722 $str = '';
811 723
812 - if ( false === has_filter( 'comment_form', 'show_subscription_checkbox' ) && 1 === (int) get_option( 'stc_enabled', 1 ) && empty( $post->post_password ) && 'post' === get_post_type() ) {
813 - // Subscribe to comments checkbox.
814 - $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 . ' /> ';
724 + if ( FALSE === has_filter( 'comment_form', 'show_subscription_checkbox' ) && 1 == get_option( 'stc_enabled', 1 ) && empty( $post->post_password ) && 'post' == get_post_type() ) {
725 + // Subscribe to comments checkbox
726 + $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 . ' /> ';
815 727 $comment_sub_text = __( 'Notify me of follow-up comments by email.', 'jetpack' );
816 - $str .= '<label class="subscribe-label" id="subscribe-label" for="subscribe_comments">' . esc_html(
728 + $str .= '<label class="subscribe-label" id="subscribe-label" for="subscribe_comments">' . esc_html(
817 729 /**
818 730 * Filter the Subscribe to comments text appearing below the comment form.
819 731 *
820 732 * @module subscriptions
@@ -827,13 +739,13 @@
827 739 ) . '</label>';
828 740 $str .= '</p>';
829 741 }
830 742
831 - if ( 1 === (int) get_option( 'stb_enabled', 1 ) ) {
832 - // Subscribe to blog checkbox.
833 - $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 . ' /> ';
743 + if ( 1 == get_option( 'stb_enabled', 1 ) ) {
744 + // Subscribe to blog checkbox
745 + $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 . ' /> ';
834 746 $blog_sub_text = __( 'Notify me of new posts by email.', 'jetpack' );
835 - $str .= '<label class="subscribe-label" id="subscribe-blog-label" for="subscribe_blog">' . esc_html(
747 + $str .= '<label class="subscribe-label" id="subscribe-blog-label" for="subscribe_blog">' . esc_html(
836 748 /**
837 749 * Filter the Subscribe to blog text appearing below the comment form.
838 750 *
839 751 * @module subscriptions
@@ -864,49 +776,40 @@
864 776 /**
865 777 * Jetpack_Subscriptions::comment_subscribe_init()
866 778 *
867 779 * When a user checks the comment subscribe box and submits a comment, subscribe them to the comment thread.
868 - *
869 - * @param int|string $comment_id Comment thread being subscribed to.
870 - * @param string $approved Comment status.
871 780 */
872 - public function comment_subscribe_submit( $comment_id, $approved ) {
781 + function comment_subscribe_submit( $comment_id, $approved ) {
873 782 if ( 'spam' === $approved ) {
874 783 return;
875 784 }
876 785
877 786 $comment = get_comment( $comment_id );
878 - if ( ! $comment ) {
879 - return;
880 - }
881 787
882 - // Set cookies for this post/comment.
883 - $this->set_cookies( isset( $_REQUEST['subscribe_comments'] ), $comment->comment_post_ID, isset( $_REQUEST['subscribe_blog'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
788 + // Set cookies for this post/comment
789 + $this->set_cookies( isset( $_REQUEST['subscribe_comments'] ), $comment->comment_post_ID, isset( $_REQUEST['subscribe_blog'] ) );
884 790
885 - if ( ! isset( $_REQUEST['subscribe_comments'] ) && ! isset( $_REQUEST['subscribe_blog'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
791 + if ( !isset( $_REQUEST['subscribe_comments'] ) && !isset( $_REQUEST['subscribe_blog'] ) )
886 792 return;
887 - }
888 793
889 794 $post_ids = array();
890 795
891 - if ( isset( $_REQUEST['subscribe_comments'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
796 + if ( isset( $_REQUEST['subscribe_comments'] ) )
892 797 $post_ids[] = $comment->comment_post_ID;
893 - }
894 798
895 - if ( isset( $_REQUEST['subscribe_blog'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
799 + if ( isset( $_REQUEST['subscribe_blog'] ) )
896 800 $post_ids[] = 0;
897 - }
898 801
899 - $result = self::subscribe(
900 - $comment->comment_author_email,
901 - $post_ids,
902 - true,
903 - array(
904 - 'source' => 'comment-form',
905 - 'widget-in-use' => is_active_widget( false, false, 'blog_subscription', true ) ? 'yes' : 'no',
906 - 'comment_status' => $approved,
907 - 'server_data' => jetpack_subscriptions_cherry_pick_server_data(),
908 - )
802 + $result = Jetpack_Subscriptions::subscribe(
803 + $comment->comment_author_email,
804 + $post_ids,
805 + true,
806 + array(
807 + 'source' => 'comment-form',
808 + 'widget-in-use' => is_active_widget( false, false, 'blog_subscription', true ) ? 'yes' : 'no',
809 + 'comment_status' => $approved,
810 + 'server_data' => jetpack_subscriptions_cherry_pick_server_data(),
811 + )
909 812 );
910 813
911 814 /**
912 815 * Fires on each comment subscription form submission.
@@ -926,16 +829,16 @@
926 829 *
927 830 * Set a cookie to save state on the comment and post subscription checkboxes.
928 831 *
929 832 * @param bool $subscribe_to_post Whether the user chose to subscribe to subsequent comments on this post.
930 - * @param int $post_id If $subscribe_to_post is true, the post ID they've subscribed to.
833 + * @param int $post_id If $subscribe_to_post is true, the post ID they've subscribed to.
931 834 * @param bool $subscribe_to_blog Whether the user chose to subscribe to all new posts on the blog.
932 835 */
933 - public function set_cookies( $subscribe_to_post = false, $post_id = null, $subscribe_to_blog = false ) {
836 + function set_cookies( $subscribe_to_post = false, $post_id = null, $subscribe_to_blog = false ) {
934 837 $post_id = (int) $post_id;
935 838
936 839 /** This filter is already documented in core/wp-includes/comment-functions.php */
937 - $cookie_lifetime = apply_filters( 'comment_cookie_lifetime', 30000000 );
840 + $cookie_lifetime = apply_filters( 'comment_cookie_lifetime', 30000000 );
938 841
939 842 /**
940 843 * Filter the Jetpack Comment cookie path.
941 844 *
@@ -944,9 +847,9 @@
944 847 * @since 2.5.0
945 848 *
946 849 * @param string COOKIEPATH Cookie path.
947 850 */
948 - $cookie_path = apply_filters( 'jetpack_comment_cookie_path', COOKIEPATH );
851 + $cookie_path = apply_filters( 'jetpack_comment_cookie_path', COOKIEPATH );
949 852
950 853 /**
951 854 * Filter the Jetpack Comment cookie domain.
952 855 *
@@ -955,20 +858,20 @@
955 858 * @since 2.5.0
956 859 *
957 860 * @param string COOKIE_DOMAIN Cookie domain.
958 861 */
959 - $cookie_domain = apply_filters( 'jetpack_comment_cookie_domain', COOKIE_DOMAIN );
862 + $cookie_domain = apply_filters( 'jetpack_comment_cookie_domain', COOKIE_DOMAIN );
960 863
961 864 if ( $subscribe_to_post && $post_id >= 0 ) {
962 - setcookie( 'jetpack_comments_subscribe_' . self::$hash . '_' . $post_id, 1, time() + $cookie_lifetime, $cookie_path, $cookie_domain, is_ssl(), true );
865 + setcookie( 'jetpack_comments_subscribe_' . self::$hash . '_' . $post_id, 1, time() + $cookie_lifetime, $cookie_path, $cookie_domain );
963 866 } else {
964 - setcookie( 'jetpack_comments_subscribe_' . self::$hash . '_' . $post_id, '', time() - 3600, $cookie_path, $cookie_domain, is_ssl(), true );
867 + setcookie( 'jetpack_comments_subscribe_' . self::$hash . '_' . $post_id, '', time() - 3600, $cookie_path, $cookie_domain );
965 868 }
966 869
967 870 if ( $subscribe_to_blog ) {
968 - setcookie( 'jetpack_blog_subscribe_' . self::$hash, 1, time() + $cookie_lifetime, $cookie_path, $cookie_domain, is_ssl(), true );
871 + setcookie( 'jetpack_blog_subscribe_' . self::$hash, 1, time() + $cookie_lifetime, $cookie_path, $cookie_domain );
969 872 } else {
970 - setcookie( 'jetpack_blog_subscribe_' . self::$hash, '', time() - 3600, $cookie_path, $cookie_domain, is_ssl(), true );
873 + setcookie( 'jetpack_blog_subscribe_' . self::$hash, '', time() - 3600, $cookie_path, $cookie_domain );
971 874 }
972 875 }
973 876
974 877 /**
@@ -975,64 +878,17 @@
975 878 * Set the social_notifications_subscribe option to `off` when the Subscriptions module is activated in the first time.
976 879 *
977 880 * @since 8.1
978 881 *
979 - * @return void
882 + * @return null
980 883 */
981 - public function set_social_notifications_subscribe() {
884 + function set_social_notifications_subscribe() {
982 885 if ( false === get_option( 'social_notifications_subscribe' ) ) {
983 886 add_option( 'social_notifications_subscribe', 'off' );
984 887 }
985 888 }
986 889
987 - /**
988 - * Save a flag when a post was ever published.
989 - *
990 - * It saves the post meta when the post was published and becomes a draft.
991 - * Then this meta is used to hide subscription messaging in Publish panel.
992 - *
993 - * @param string $new_status Tthe "new" post status of the transition when saved.
994 - * @param string $old_status The "old" post status of the transition when saved.
995 - * @param object $post obj The post object.
996 - */
997 - public function maybe_set_first_published_status( $new_status, $old_status, $post ) {
998 - $was_post_ever_published = get_post_meta( $post->ID, '_jetpack_post_was_ever_published', true );
999 - if ( ! $was_post_ever_published && 'publish' === $old_status && 'draft' === $new_status ) {
1000 - update_post_meta( $post->ID, '_jetpack_post_was_ever_published', true );
1001 - }
1002 - }
1003 -
1004 - /**
1005 - * Checks if the current user can publish posts.
1006 - *
1007 - * @return bool
1008 - */
1009 - public function first_published_status_meta_auth_callback() {
1010 - if ( current_user_can( 'publish_posts' ) ) {
1011 - return true;
1012 - }
1013 - return false;
1014 - }
1015 -
1016 - /**
1017 - * Registers the 'post_was_ever_published' post meta for use in the REST API.
1018 - */
1019 - public function register_post_meta() {
1020 - $jetpack_post_was_ever_published = array(
1021 - 'type' => 'boolean',
1022 - 'description' => __( 'Whether the post was ever published.', 'jetpack' ),
1023 - 'single' => true,
1024 - 'default' => false,
1025 - 'show_in_rest' => array(
1026 - 'name' => 'jetpack_post_was_ever_published',
1027 - ),
1028 - 'auth_callback' => array( $this, 'first_published_status_meta_auth_callback' ),
1029 - );
1030 -
1031 - register_meta( 'post', '_jetpack_post_was_ever_published', $jetpack_post_was_ever_published );
1032 - }
1033 -
1034 890 }
1035 891
1036 892 Jetpack_Subscriptions::init();
1037 893
1038 -require __DIR__ . '/subscriptions/views.php';
894 +include dirname( __FILE__ ) . '/subscriptions/views.php';