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