PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.7
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.7
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.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 All 506 releases
← All changes | extensions/blocks/subscriptions/subscriptions.php +529 -226 12.8.3 → 16.3-a.7 View file →
@@ -7,20 +7,24 @@
7 7
8 8 namespace Automattic\Jetpack\Extensions\Subscriptions;
9 9
10 10 use Automattic\Jetpack\Blocks;
11 -use Automattic\Jetpack\Connection\Manager as Connection_Manager;
11 +use Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Abstract_Token_Subscription_Service;
12 12 use Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Jetpack_Token_Subscription_Service;
13 -use Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Token_Subscription_Service;
14 -use Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\WPCOM_Token_Subscription_Service;
15 -use Automattic\Jetpack\Status;
13 +use Automattic\Jetpack\Modules;
16 14 use Automattic\Jetpack\Status\Host;
17 -use Jetpack;
15 +use Automattic\Jetpack\Status\Request;
18 16 use Jetpack_Gutenberg;
19 17 use Jetpack_Memberships;
20 18 use Jetpack_Subscriptions_Widget;
21 19
20 +if ( ! defined( 'ABSPATH' ) ) {
21 + exit( 0 );
22 +}
23 +
24 +require_once __DIR__ . '/class-jetpack-subscription-site.php';
22 25 require_once __DIR__ . '/constants.php';
26 +require_once JETPACK__PLUGIN_DIR . 'extensions/blocks/premium-content/_inc/subscription-service/include.php';
23 27
24 28 /**
25 29 * These block defaults should match ./constants.js
26 30 */
@@ -28,8 +32,9 @@
28 32 const DEFAULT_BORDER_WEIGHT_VALUE = 1;
29 33 const DEFAULT_FONTSIZE_VALUE = '16px';
30 34 const DEFAULT_PADDING_VALUE = 15;
31 35 const DEFAULT_SPACING_VALUE = 10;
36 +const DEFAULT_BUTTON_WIDTH = 'auto';
32 37
33 38 /**
34 39 * Registers the block for use in Gutenberg
35 40 * This is done via an action so that we can disable
@@ -43,17 +48,24 @@
43 48 \WPForTeams\is_wpforteams_site( get_current_blog_id() ) ) {
44 49 return;
45 50 }
46 51
47 - if (
48 - ( defined( 'IS_WPCOM' ) && IS_WPCOM )
49 - || ( ( new Connection_Manager( 'jetpack' ) )->has_connected_owner() && ! ( new Status() )->is_offline_mode() )
50 - ) {
52 + /*
53 + * Do not proceed if the newsletter feature (Subscriptions module) is not enabled
54 + */
55 + if ( ! ( new Modules() )->is_active( 'subscriptions' ) ) {
56 + return;
57 + }
58 +
59 + require_once JETPACK__PLUGIN_DIR . '/modules/memberships/class-jetpack-memberships.php';
60 +
61 + if ( \Jetpack_Memberships::should_enable_monetize_blocks_in_editor() ) {
51 62 Blocks::jetpack_register_block(
52 - BLOCK_NAME,
63 + __DIR__,
53 64 array(
54 - 'render_callback' => __NAMESPACE__ . '\render_block',
55 - 'supports' => array(
65 + 'render_callback' => __NAMESPACE__ . '\render_block',
66 + 'render_email_callback' => __NAMESPACE__ . '\render_email',
67 + 'supports' => array(
56 68 'spacing' => array(
57 69 'margin' => true,
58 70 'padding' => true,
59 71 ),
@@ -62,31 +74,40 @@
62 74 )
63 75 );
64 76 }
65 77
66 - /*
67 - * If the Subscriptions module is not active,
68 - * do not make any further changes on the site.
69 - */
70 - if ( ! Jetpack::is_module_active( 'subscriptions' ) ) {
71 - return;
72 - }
78 + register_post_meta(
79 + 'post',
80 + META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS,
81 + array(
82 + 'show_in_rest' => true,
83 + 'single' => true,
84 + 'type' => 'string',
85 + // The REST schema already rejects non-strings, but non-REST writers
86 + // (importers, XML-RPC, WP-CLI, direct update_post_meta, sync) only pass
87 + // through sanitize_meta(). Coerce anything that isn't a string to ''
88 + // ("everybody") so a corrupt value can't be persisted and later fatal the
89 + // strict string-typed access checks.
90 + 'sanitize_callback' => function ( $value ) {
91 + return is_string( $value ) ? $value : '';
92 + },
93 + 'auth_callback' => function () {
94 + return wp_get_current_user()->has_cap( 'edit_posts' );
95 + },
96 + )
97 + );
73 98
74 - /**
75 - * Do not proceed if the newsletter feature is not enabled
76 - * or if the 'Jetpack_Memberships' class does not exists.
77 - */
78 - if ( ! class_exists( '\Jetpack_Memberships' ) ) {
79 - return;
80 - }
99 + // The meta is a "don't send" flag, so invert the blog option (which defaults to true = "send").
100 + $dont_email_default = ! get_option( 'wpcom_newsletter_send_default', true );
81 101
82 102 register_post_meta(
83 103 'post',
84 - META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS,
104 + META_NAME_FOR_POST_DONT_EMAIL_TO_SUBS,
85 105 array(
106 + 'default' => $dont_email_default,
86 107 'show_in_rest' => true,
87 108 'single' => true,
88 - 'type' => 'string',
109 + 'type' => 'boolean',
89 110 'auth_callback' => function () {
90 111 return wp_get_current_user()->has_cap( 'edit_posts' );
91 112 },
92 113 )
@@ -104,13 +125,35 @@
104 125 },
105 126 )
106 127 );
107 128
129 + register_post_meta(
130 + 'post',
131 + META_NAME_CONTAINS_PAYWALLED_CONTENT,
132 + array(
133 + 'show_in_rest' => true,
134 + 'single' => true,
135 + 'type' => 'boolean',
136 + 'auth_callback' => function () {
137 + return wp_get_current_user()->has_cap( 'edit_posts' );
138 + },
139 + )
140 + );
141 +
108 142 // This ensures Jetpack will sync this post meta to WPCOM.
109 143 add_filter(
110 144 'jetpack_sync_post_meta_whitelist',
111 145 function ( $allowed_meta ) {
112 - return array_merge( $allowed_meta, array( META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS ) );
146 + return array_merge(
147 + $allowed_meta,
148 + array(
149 + META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS,
150 + META_NAME_FOR_POST_DONT_EMAIL_TO_SUBS,
151 + META_NAME_CONTAINS_PAYWALLED_CONTENT,
152 + META_NAME_FOR_POST_TIER_ID_SETTINGS,
153 + META_NAME_CONTAINS_PAID_CONTENT,
154 + )
155 + );
113 156 }
114 157 );
115 158
116 159 // Hide the content – Priority 8 makes it run before do_blocks gets called for the content
@@ -122,14 +165,41 @@
122 165
123 166 // Hide existing comments
124 167 add_filter( 'get_comment', __NAMESPACE__ . '\maybe_gate_existing_comments' );
125 168
126 - // Gate the excerpt for a post
127 - add_filter( 'get_the_excerpt', __NAMESPACE__ . '\jetpack_filter_excerpt_for_newsletter', 10, 2 );
169 + // Add a 'Newsletter' column to the Edit posts page
170 + // We only display the "Newsletter" column if we have configured the paid newsletter plan
171 + if ( defined( 'WP_ADMIN' ) && WP_ADMIN && Jetpack_Memberships::has_configured_plans_jetpack_recurring_payments( 'newsletter' ) ) {
172 + add_action( 'manage_post_posts_columns', __NAMESPACE__ . '\register_newsletter_access_column' );
173 + add_action( 'manage_post_posts_custom_column', __NAMESPACE__ . '\render_newsletter_access_rows', 10, 2 );
174 + add_action( 'admin_head', __NAMESPACE__ . '\newsletter_access_column_styles' );
175 + }
128 176
129 - // Add a 'Newsletter access' column to the Edit posts page
130 - add_action( 'manage_post_posts_columns', __NAMESPACE__ . '\register_newsletter_access_column' );
131 - add_action( 'manage_post_posts_custom_column', __NAMESPACE__ . '\render_newsletter_access_rows', 10, 2 );
177 + add_action( 'init', __NAMESPACE__ . '\maybe_prevent_super_cache_caching' );
178 +
179 + add_action( 'wp_after_insert_post', __NAMESPACE__ . '\add_paywalled_content_post_meta', 99, 2 );
180 +
181 + add_filter(
182 + 'jetpack_options_whitelist',
183 + function ( $options ) {
184 + $options[] = 'jetpack_subscriptions_subscribe_post_end_enabled';
185 + $options[] = 'jetpack_subscriptions_subscribe_navigation_enabled';
186 +
187 + return $options;
188 + }
189 + );
190 +
191 + // If called via REST API, we need to register later in the lifecycle
192 + if ( ( new Host() )->is_wpcom_platform() && ! Request::is_frontend() ) {
193 + add_action(
194 + 'restapi_theme_init',
195 + function () {
196 + Jetpack_Subscription_Site::init()->handle_subscribe_block_placements();
197 + }
198 + );
199 + } else {
200 + Jetpack_Subscription_Site::init()->handle_subscribe_block_placements();
201 + }
132 202 }
133 203 add_action( 'init', __NAMESPACE__ . '\register_block', 9 );
134 204
135 205 /**
@@ -147,15 +217,10 @@
147 217 * @param array $columns An array of column names.
148 218 * @return array An array of column names.
149 219 */
150 220 function register_newsletter_access_column( $columns ) {
151 - if ( ! Jetpack_Memberships::has_configured_plans_jetpack_recurring_payments( 'newsletter' ) ) {
152 - // We only display the "NL access" column if we have published one paid-newsletter
153 - return $columns;
154 - }
155 -
156 221 $position = array_search( 'title', array_keys( $columns ), true );
157 - $new_column = array( NEWSLETTER_COLUMN_ID => '<span>' . __( 'Newsletter', 'jetpack' ) . '</span>' );
222 + $new_column = array( NEWSLETTER_COLUMN_ID => __( 'Newsletter', 'jetpack' ) );
158 223 return array_merge(
159 224 array_slice( $columns, 0, $position + 1, true ),
160 225 $new_column,
161 226 array_slice( $columns, $position, null, true )
@@ -162,8 +227,38 @@
162 227 );
163 228 }
164 229
165 230 /**
231 + * Add a meta to prevent publication on firehose, ES AI or Reader
232 + *
233 + * @param int $post_id Post id being saved.
234 + * @param \WP_Post $post Post being saved.
235 + * @return void
236 + */
237 +function add_paywalled_content_post_meta( int $post_id, \WP_Post $post ) {
238 + if ( $post->post_type !== 'post' ) {
239 + return;
240 + }
241 +
242 + $access_level = get_post_meta( $post_id, META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS, true );
243 +
244 + // A Paywall block gates the post whatever the stored access level; see Jetpack_Memberships::get_post_access_level().
245 + $is_paywalled = has_block( \Automattic\Jetpack\Extensions\Paywall\BLOCK_NAME, $post );
246 + switch ( $access_level ) {
247 + case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS_ALL_TIERS:
248 + case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS:
249 + case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_SUBSCRIBERS:
250 + $is_paywalled = true;
251 + }
252 + if ( $is_paywalled ) {
253 + update_post_meta( $post_id, META_NAME_CONTAINS_PAYWALLED_CONTENT, $is_paywalled );
254 + }
255 + if ( ! $is_paywalled ) {
256 + delete_post_meta( $post_id, META_NAME_CONTAINS_PAYWALLED_CONTENT );
257 + }
258 +}
259 +
260 +/**
166 261 * Displays the newsletter access level.
167 262 *
168 263 * @param string $column_id The ID of the column to display.
169 264 * @param int $post_id The current post ID.
@@ -172,21 +267,21 @@
172 267 if ( NEWSLETTER_COLUMN_ID !== $column_id ) {
173 268 return;
174 269 }
175 270
176 - $access_level = get_post_meta( $post_id, META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS, true );
271 + $access_level = Jetpack_Memberships::get_post_access_level( $post_id );
177 272
178 273 switch ( $access_level ) {
179 - case Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS_ALL_TIERS:
274 + case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS_ALL_TIERS:
180 275 echo esc_html__( 'Paid Subscribers (all plans)', 'jetpack' );
181 276 break;
182 - case Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS:
277 + case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS:
183 278 echo esc_html__( 'Paid Subscribers', 'jetpack' );
184 279 break;
185 - case Token_Subscription_Service::POST_ACCESS_LEVEL_SUBSCRIBERS:
280 + case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_SUBSCRIBERS:
186 281 echo esc_html__( 'Subscribers', 'jetpack' );
187 282 break;
188 - case Token_Subscription_Service::POST_ACCESS_LEVEL_EVERYBODY:
283 + case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_EVERYBODY:
189 284 echo esc_html__( 'Everybody', 'jetpack' );
190 285 break;
191 286 default:
192 287 echo '';
@@ -193,11 +288,18 @@
193 288 }
194 289 }
195 290
196 291 /**
197 - * Determine the amount of folks currently subscribed to the blog, splitted out in email_subscribers & social_followers & paid_subscribers
292 + * Adds the Newsletter column styles
293 + */
294 +function newsletter_access_column_styles() {
295 + echo '<style id="jetpack-newsletter-newsletter-access-column"> table.fixed .column-newsletter_access { width: 10%; } </style>';
296 +}
297 +
298 +/**
299 + * Determine the amount of folks currently subscribed to the blog, splitted out in total_subscribers, email_subscribers, social_followers & paid_subscribers.
198 300 *
199 - * @return array containing ['value' => ['email_subscribers' => 0, 'paid_subscribers' => 0, 'social_followers' => 0]]
301 + * @return array containing ['value' => ['total_subscribers' => 0, 'email_subscribers' => 0, 'paid_subscribers' => 0, 'social_followers' => 0]]
200 302 */
201 303 function fetch_subscriber_counts() {
202 304 $subs_count = 0;
203 305 if ( is_wpcom() ) {
@@ -215,9 +317,10 @@
215 317 $subs_count = array(
216 318 'status' => 'failed',
217 319 'code' => $xml->getErrorCode(),
218 320 'message' => $xml->getErrorMessage(),
219 - 'value' => ( isset( $subs_count['value'] ) ) ? $subs_count['value'] : array(
321 + 'value' => $subs_count['value'] ?? array(
322 + 'total_subscribers' => 0,
220 323 'email_subscribers' => 0,
221 324 'social_followers' => 0,
222 325 'paid_subscribers' => 0,
223 326 ),
@@ -243,11 +346,11 @@
243 346 function get_subscriber_count( $include_social_followers ) {
244 347 $counts = fetch_subscriber_counts();
245 348
246 349 if ( $include_social_followers ) {
247 - $subscriber_count = $counts['value']['email_subscribers'] + $counts['value']['social_followers'];
350 + $subscriber_count = $counts['value']['total_subscribers'] + $counts['value']['social_followers'];
248 351 } else {
249 - $subscriber_count = $counts['value']['email_subscribers'];
352 + $subscriber_count = $counts['value']['total_subscribers'];
250 353 }
251 354 return $subscriber_count;
252 355 }
253 356
@@ -350,8 +453,24 @@
350 453 );
351 454 }
352 455
353 456 /**
457 + * Checks if block style is "button only"
458 + *
459 + * @param string $class_name Block attribute className; multiple names are spearated by space.
460 + *
461 + * @return bool
462 + */
463 +function is_button_only_style( $class_name ) {
464 + if ( empty( $class_name ) ) {
465 + return false;
466 + }
467 +
468 + $class_names = explode( ' ', $class_name );
469 + return in_array( 'is-style-button', $class_names, true );
470 +}
471 +
472 +/**
354 473 * Uses block attributes to generate an array containing the styles for various block elements.
355 474 * Based on Jetpack_Subscriptions_Widget::do_subscription_form() which the block was originally using.
356 475 *
357 476 * @param array $attributes Array containing the block attributes.
@@ -358,8 +477,10 @@
358 477 *
359 478 * @return array
360 479 */
361 480 function get_element_styles_from_attributes( $attributes ) {
481 + $is_button_only_style = is_button_only_style( get_attribute( $attributes, 'className', '' ) );
482 +
362 483 $button_background_style = ! has_attribute( $attributes, 'buttonBackgroundColor' ) && has_attribute( $attributes, 'customButtonGradient' )
363 484 ? get_attribute( $attributes, 'customButtonGradient' )
364 485 : get_attribute( $attributes, 'customButtonBackgroundColor' );
365 486
@@ -375,15 +496,15 @@
375 496 $submit_button_styles .= sprintf( 'color: %s;', get_attribute( $attributes, 'customTextColor' ) );
376 497 }
377 498
378 499 if ( has_attribute( $attributes, 'buttonWidth' ) ) {
379 - $submit_button_wrapper_styles .= sprintf( 'width: %s;', get_attribute( $attributes, 'buttonWidth' ) );
500 + $submit_button_wrapper_styles .= sprintf( 'width: %s;', get_attribute( $attributes, 'buttonWidth', DEFAULT_BUTTON_WIDTH ) );
380 501 $submit_button_wrapper_styles .= 'max-width: 100%;';
381 502
382 503 // Account for custom margins on inline forms.
383 504 $submit_button_styles .= true === get_attribute( $attributes, 'buttonOnNewLine' )
384 - ? sprintf( 'width: calc(100%% - %dpx);', get_attribute( $attributes, 'spacing', DEFAULT_SPACING_VALUE ) )
385 - : 'width: 100%;';
505 + ? 'width: 100%;'
506 + : sprintf( 'width: calc(100%% - %dpx);', get_attribute( $attributes, 'spacing', DEFAULT_SPACING_VALUE ) );
386 507 }
387 508
388 509 $font_size = get_attribute( $attributes, 'customFontSize', DEFAULT_FONTSIZE_VALUE );
389 510 $style = sprintf( 'font-size: %s%s;', $font_size, is_numeric( $font_size ) ? 'px' : '' );
@@ -396,14 +517,16 @@
396 517
397 518 $submit_button_styles .= $style;
398 519 $email_field_styles .= $style;
399 520
400 - $button_spacing = get_attribute( $attributes, 'spacing', DEFAULT_SPACING_VALUE );
401 - if ( true === get_attribute( $attributes, 'buttonOnNewLine' ) ) {
402 - $submit_button_styles .= sprintf( 'margin-top: %dpx;', $button_spacing );
403 - } else {
404 - $submit_button_styles .= 'margin: 0px; '; // Reset Safari's 2px default margin for buttons affecting input and button union
405 - $submit_button_styles .= sprintf( 'margin-left: %dpx;', $button_spacing );
521 + if ( ! $is_button_only_style ) {
522 + $button_spacing = get_attribute( $attributes, 'spacing', DEFAULT_SPACING_VALUE );
523 + if ( true === get_attribute( $attributes, 'buttonOnNewLine' ) ) {
524 + $submit_button_styles .= sprintf( 'margin-top: %dpx;', $button_spacing );
525 + } else {
526 + $submit_button_styles .= 'margin: 0; '; // Reset Safari's 2px default margin for buttons affecting input and button union
527 + $submit_button_styles .= sprintf( 'margin-left: %dpx;', $button_spacing );
528 + }
406 529 }
407 530
408 531 if ( has_attribute( $attributes, 'borderColor' ) ) {
409 532 $style = sprintf( 'border-color: %s;', get_attribute( $attributes, 'borderColor', '' ) );
@@ -425,9 +548,9 @@
425 548 $submit_button_styles .= $style;
426 549 $email_field_styles .= $style;
427 550 }
428 551
429 - if ( ! jetpack_is_frontend() ) {
552 + if ( ! Request::is_frontend() ) {
430 553 $background_color_style = get_attribute_color( 'buttonBackgroundColor', $attributes, '#113AF5' /* default lettre theme color */ );
431 554 $text_color_style = get_attribute_color( 'textColor', $attributes, '#FFFFFF' );
432 555 $submit_button_styles .= sprintf( ' background-color: %s; color: %s;', $background_color_style, $text_color_style );
433 556 }
@@ -512,8 +635,15 @@
512 635 return null;
513 636 }
514 637
515 638 /**
639 + * Is the Jetpack_Memberships class loaded.
640 + */
641 +function is_jetpack_memberships_loaded(): bool {
642 + return class_exists( '\Jetpack_Memberships' );
643 +}
644 +
645 +/**
516 646 * Subscriptions block render callback.
517 647 *
518 648 * @param array $attributes Array containing the block attributes.
519 649 *
@@ -520,28 +650,32 @@
520 650 * @return string
521 651 */
522 652 function render_block( $attributes ) {
523 653 // If the Subscriptions module is not active, don't render the block.
524 - if ( ! Jetpack::is_module_active( 'subscriptions' ) ) {
654 + if ( ! ( new Modules() )->is_active( 'subscriptions' ) ) {
525 655 return '';
526 656 }
527 657
528 - if ( class_exists( '\Jetpack_Memberships' ) ) {
529 - // We only want the sites that have newsletter feature enabled to be graced by this JavaScript and thickbox.
530 - Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME, array( 'thickbox' ) );
531 - if ( ! wp_style_is( 'enqueued' ) ) {
532 - wp_enqueue_style( 'thickbox' );
533 - }
658 + if ( is_jetpack_memberships_loaded() ) {
659 + // We only want the sites that have newsletter feature enabled to be graced by this JavaScript.
660 + Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
534 661 } else {
535 662 Jetpack_Gutenberg::load_styles_as_required( FEATURE_NAME );
536 663 }
537 664
665 + if ( ! class_exists( 'Jetpack_Subscriptions_Widget' ) ) {
666 + return '';
667 + }
668 +
669 + // Prefill the email field with the current user's email if they are logged in via Memberships premium content token
538 670 $subscribe_email = Jetpack_Memberships::get_current_user_email();
539 671
540 - /** This filter is documented in modules/contact-form/grunion-contact-form.php */
541 - if ( is_wpcom() || false !== apply_filters( 'jetpack_auto_fill_logged_in_user', false ) ) {
542 - $current_user = wp_get_current_user();
543 - $subscribe_email = ! empty( $current_user->user_email ) ? $current_user->user_email : '';
672 + // If no email, then prefill the email field with the current user's email if they are logged in
673 + if ( empty( $subscribe_email ) ) {
674 + $current_user = wp_get_current_user();
675 + if ( ! empty( $current_user->user_email ) ) {
676 + $subscribe_email = $current_user->user_email;
677 + }
544 678 }
545 679
546 680 // The block is using the Jetpack_Subscriptions_Widget backend, hence the need to increase the instance count.
547 681 ++Jetpack_Subscriptions_Widget::$instance_count;
@@ -548,38 +682,52 @@
548 682
549 683 $classes = get_element_class_names_from_attributes( $attributes );
550 684 $styles = get_element_styles_from_attributes( $attributes );
551 685
686 + // The default value was previously "true" in block.json. We don't want to rely setting "default" in block.json to falsy,
687 + // because it would change the setting for previously saved blocks. Block editor doesn't store default values in attributes at all.
688 + // Hence users without this set will still get social counts included in the subscriber counter.
689 + // Lowering the subscriber count on their behalf with code change would be controversial.
690 + // We want to disencourage including social count as it's misleading.
552 691 $include_social_followers = isset( $attributes['includeSocialFollowers'] ) ? (bool) get_attribute( $attributes, 'includeSocialFollowers' ) : true;
553 - $is_paid_subscriber = get_attribute( $attributes, 'isPaidSubscriber', false );
554 692
555 693 $data = array(
556 - 'widget_id' => Jetpack_Subscriptions_Widget::$instance_count,
557 - 'subscribe_email' => $subscribe_email,
558 -
559 - 'wrapper_attributes' => get_block_wrapper_attributes(
694 + 'widget_id' => Jetpack_Subscriptions_Widget::$instance_count,
695 + 'subscribe_email' => $subscribe_email,
696 + 'is_paid_subscriber' => get_attribute( $attributes, 'isPaidSubscriber', false ),
697 + 'wrapper_attributes' => get_block_wrapper_attributes(
560 698 array(
561 699 'class' => $classes['block_wrapper'],
562 700 )
563 701 ),
564 - 'subscribe_placeholder' => get_attribute( $attributes, 'subscribePlaceholder', esc_html__( 'Type your email…', 'jetpack' ) ),
565 - 'submit_button_text' => get_attribute( $attributes, 'submitButtonText', $is_paid_subscriber ? esc_html__( 'Upgrade', 'jetpack' ) : esc_html__( 'Subscribe', 'jetpack' ) ),
566 - 'submit_button_text_subscribed' => get_attribute( $attributes, 'submitButtonTextSubscribed', esc_html__( 'Subscribed', 'jetpack' ) ),
567 - 'success_message' => get_attribute(
702 + 'subscribe_placeholder' => get_attribute( $attributes, 'subscribePlaceholder', __( 'Type your email…', 'jetpack' ) ),
703 + 'submit_button_text' => get_attribute( $attributes, 'submitButtonText', __( 'Subscribe', 'jetpack' ) ),
704 + 'submit_button_text_subscribed' => get_attribute( $attributes, 'submitButtonTextSubscribed', __( 'Subscribed', 'jetpack' ) ),
705 + 'submit_button_text_upgrade' => get_attribute( $attributes, 'submitButtonTextUpgrade', __( 'Upgrade subscription', 'jetpack' ) ),
706 + 'success_message' => get_attribute(
568 707 $attributes,
569 708 'successMessage',
570 - 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' )
709 + esc_html__( "Success! An email was just sent to confirm your subscription. Please find the email now and click 'Confirm' to start subscribing.", 'jetpack' )
571 710 ),
572 - 'show_subscribers_total' => (bool) get_attribute( $attributes, 'showSubscribersTotal' ),
573 - 'subscribers_total' => get_subscriber_count( $include_social_followers ),
574 - 'referer' => esc_url_raw(
711 + 'show_subscribers_total' => (bool) get_attribute( $attributes, 'showSubscribersTotal' ),
712 + 'subscribers_total' => get_attribute( $attributes, 'showSubscribersTotal' ) ? get_subscriber_count( $include_social_followers ) : 0,
713 + 'referer' => esc_url_raw(
575 714 ( is_ssl() ? 'https' : 'http' ) . '://' . ( isset( $_SERVER['HTTP_HOST'] ) ? wp_unslash( $_SERVER['HTTP_HOST'] ) : '' ) .
576 715 ( isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '' )
577 716 ),
578 - 'source' => 'subscribe-block',
717 + 'source' => 'subscribe-block',
718 + 'app_source' => get_attribute( $attributes, 'appSource', null ),
719 + 'class_name' => get_attribute( $attributes, 'className' ),
720 + 'selected_newsletter_categories' => get_attribute( $attributes, 'selectedNewsletterCategoryIds', array() ),
721 + 'preselected_newsletter_categories' => get_attribute( $attributes, 'preselectNewsletterCategories', false ),
579 722 );
580 723
581 - if ( ! jetpack_is_frontend() ) {
724 + // Only render the email version in non-frontend contexts.
725 + if ( is_feed() || wp_is_xml_request() ||
726 + ( defined( 'REST_REQUEST' ) && REST_REQUEST && ! wp_is_json_request() ) ||
727 + ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ) ||
728 + ( defined( 'WP_CLI' ) && WP_CLI ) ||
729 + wp_is_jsonp_request() ) {
582 730 return render_for_email( $data, $styles );
583 731 }
584 732
585 733 return render_for_website( $data, $classes, $styles );
@@ -592,9 +740,9 @@
592 740 */
593 741 function get_post_access_level_for_current_post() {
594 742 if ( ! is_singular() ) {
595 743 // There is no "actual" current post.
596 - return Token_Subscription_Service::POST_ACCESS_LEVEL_EVERYBODY;
744 + return Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_EVERYBODY;
597 745 }
598 746
599 747 return Jetpack_Memberships::get_post_access_level();
600 748 }
@@ -608,22 +756,32 @@
608 756 *
609 757 * @return string
610 758 */
611 759 function render_for_website( $data, $classes, $styles ) {
612 - $blog_id = \Jetpack_Options::get_option( 'id' );
613 - $widget_id_suffix = Jetpack_Subscriptions_Widget::$instance_count > 1 ? '-' . Jetpack_Subscriptions_Widget::$instance_count : '';
614 - $form_id = 'subscribe-blog' . $widget_id_suffix;
615 - $form_url = defined( 'SUBSCRIBE_BLOG_URL' ) ? SUBSCRIBE_BLOG_URL : '#';
616 - $post_access_level = get_post_access_level_for_current_post();
617 - $post_id = get_the_ID();
618 - $subscribe_field_id = apply_filters( 'subscribe_field_id', 'subscribe-field' . $widget_id_suffix, $data['widget_id'] );
619 - $tier_id = get_post_meta( $post_id, META_NAME_FOR_POST_TIER_ID_SETTINGS, true );
620 - $is_subscribed = Jetpack_Memberships::is_current_user_subscribed();
621 - $button_text = wp_kses(
622 - html_entity_decode( $is_subscribed ? ( '✓ ' . $data['submit_button_text_subscribed'] ) : $data['submit_button_text'], ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ),
623 - Jetpack_Subscriptions_Widget::$allowed_html_tags_for_submit_button
624 - );
760 + $lang = get_locale();
761 + $blog_id = \Jetpack_Options::get_option( 'id' );
762 + $widget_id_suffix = Jetpack_Subscriptions_Widget::$instance_count > 1 ? '-' . Jetpack_Subscriptions_Widget::$instance_count : '';
763 + $form_id = 'subscribe-blog' . $widget_id_suffix;
764 + $form_url = 'https://wordpress.com/email-subscriptions';
765 + $post_access_level = get_post_access_level_for_current_post();
766 + $is_button_only_style = ! empty( $data['class_name'] ) ? is_button_only_style( $data['class_name'] ) : false;
625 767
768 + // Post ID is used for pulling post-specific paid status, and returning to the right post after confirming subscription
769 + $post_id = null;
770 + if ( in_the_loop() ) {
771 + $post_id = get_the_ID();
772 + } elseif ( is_singular( 'post' ) || is_page() ) {
773 + $post_id = get_queried_object_id();
774 + } else {
775 + $post_id = get_option( 'page_on_front' );
776 + }
777 +
778 + $subscribe_field_id = apply_filters( 'subscribe_field_id', 'subscribe-field' . $widget_id_suffix, $data['widget_id'] );
779 + $tier_id = get_post_meta( $post_id, META_NAME_FOR_POST_TIER_ID_SETTINGS, true );
780 + $is_subscribed = Jetpack_Memberships::is_current_user_subscribed();
781 + $button_text = get_submit_button_text( $data );
782 + $show_subscriber_count = $data['show_subscribers_total'] && $data['subscribers_total'] && ! $is_subscribed;
783 +
626 784 ob_start();
627 785
628 786 Jetpack_Subscriptions_Widget::render_widget_status_messages(
629 787 array(
@@ -631,10 +789,28 @@
631 789 )
632 790 );
633 791 ?>
634 792 <div <?php echo wp_kses_data( $data['wrapper_attributes'] ); ?>>
635 - <div class="jetpack_subscription_widget<?php echo ! $is_subscribed ? ' is-not-subscriber' : ''; ?>">
636 - <div class="wp-block-jetpack-subscriptions__container">
793 + <div class="wp-block-jetpack-subscriptions__container<?php echo ! $is_subscribed ? ' is-not-subscriber' : ''; ?>">
794 + <?php if ( is_top_subscription() ) : ?>
795 + <p id="subscribe-submit" class="is-link"
796 + <?php if ( ! empty( $styles['submit_button_wrapper'] ) ) : ?>
797 + style="<?php echo esc_attr( $styles['submit_button_wrapper'] ); ?>"
798 + <?php endif; ?>
799 + >
800 + <a
801 + href="<?php echo esc_url( 'https://wordpress.com/reader/site/subscription/' . $blog_id ); ?>"
802 + <?php if ( ! empty( $classes['submit_button'] ) ) : ?>
803 + class="<?php echo esc_attr( $classes['submit_button'] ); ?>"
804 + <?php endif; ?>
805 + <?php if ( ! empty( $styles['submit_button'] ) ) : ?>
806 + style="<?php echo esc_attr( $styles['submit_button'] ); ?>"
807 + <?php endif; ?>
808 + >
809 + <?php echo sanitize_submit_text( $button_text ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
810 + </a>
811 + </p>
812 + <?php else : ?>
637 813 <form
638 814 action="<?php echo esc_url( $form_url ); ?>"
639 815 method="post"
640 816 accept-charset="utf-8"
@@ -643,9 +819,9 @@
643 819 data-subscriber_email="<?php echo esc_attr( $data['subscribe_email'] ); ?>"
644 820 id="<?php echo esc_attr( $form_id ); ?>"
645 821 >
646 822 <div class="wp-block-jetpack-subscriptions__form-elements">
647 - <?php if ( ! $is_subscribed ) : ?>
823 + <?php if ( ! $is_subscribed && ! $is_button_only_style ) : ?>
648 824 <p id="subscribe-email">
649 825 <label
650 826 id="<?php echo esc_attr( $subscribe_field_id . '-label' ); ?>"
651 827 for="<?php echo esc_attr( $subscribe_field_id ); ?>"
@@ -658,13 +834,15 @@
658 834 '<input
659 835 required="required"
660 836 type="email"
661 837 name="email"
838 + autocomplete="email"
662 839 %1$s
663 840 style="%2$s"
664 841 placeholder="%3$s"
665 842 value="%4$s"
666 843 id="%5$s"
844 + %6$s
667 845 />',
668 846 ( ! empty( $classes['email_field'] )
669 847 ? 'class="' . esc_attr( $classes['email_field'] ) . '"'
670 848 : ''
@@ -674,9 +852,13 @@
674 852 : 'width: 95%; padding: 1px 10px'
675 853 ),
676 854 esc_attr( $data['subscribe_placeholder'] ),
677 855 esc_attr( $data['subscribe_email'] ),
678 - esc_attr( $subscribe_field_id )
856 + esc_attr( $subscribe_field_id ),
857 + ( ! empty( $data['subscribe_email'] )
858 + ? 'disabled title="' . esc_attr__( "You're logged in with this email", 'jetpack' ) . '"'
859 + : 'title="' . esc_attr__( 'Please fill in this field.', 'jetpack' ) . '"'
860 + )
679 861 );
680 862 ?>
681 863 </p>
682 864 <?php endif; ?>
@@ -688,11 +870,13 @@
688 870 <input type="hidden" name="action" value="subscribe"/>
689 871 <input type="hidden" name="blog_id" value="<?php echo (int) $blog_id; ?>"/>
690 872 <input type="hidden" name="source" value="<?php echo esc_url( $data['referer'] ); ?>"/>
691 873 <input type="hidden" name="sub-type" value="<?php echo esc_attr( $data['source'] ); ?>"/>
874 + <input type="hidden" name="app_source" value="<?php echo esc_attr( $data['app_source'] ); ?>"/>
692 875 <input type="hidden" name="redirect_fragment" value="<?php echo esc_attr( $form_id ); ?>"/>
876 + <input type="hidden" name="lang" value="<?php echo esc_attr( $lang ); ?>"/>
693 877 <?php
694 - wp_nonce_field( 'blogsub_subscribe_' . $blog_id, '_wpnonce', false );
878 + wp_nonce_field( 'blogsub_subscribe_' . $blog_id );
695 879
696 880 if ( ! empty( $post_id ) ) {
697 881 echo '<input type="hidden" name="post_id" value="' . esc_attr( $post_id ) . '"/>';
698 882 }
@@ -699,8 +883,12 @@
699 883
700 884 if ( ! empty( $tier_id ) ) {
701 885 echo '<input type="hidden" name="tier_id" value="' . esc_attr( $tier_id ) . '"/>';
702 886 }
887 +
888 + if ( $data['preselected_newsletter_categories'] && ! empty( $data['selected_newsletter_categories'] ) ) {
889 + echo '<input type="hidden" name="selected_newsletter_categories" value="' . esc_attr( implode( ',', $data['selected_newsletter_categories'] ) ) . '"/>';
890 + }
703 891 ?>
704 892 <button type="submit"
705 893 <?php if ( ! empty( $classes['submit_button'] ) ) : ?>
706 894 class="<?php echo esc_attr( $classes['submit_button'] ); ?>"
@@ -709,21 +897,19 @@
709 897 style="<?php echo esc_attr( $styles['submit_button'] ); ?>"
710 898 <?php endif; ?>
711 899 name="jetpack_subscriptions_widget"
712 900 >
713 - <?php echo esc_html( $button_text ); ?>
901 + <?php echo sanitize_submit_text( $button_text ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
714 902 </button>
715 903 </p>
716 904 </div>
717 905 </form>
718 - <?php if ( $data['show_subscribers_total'] && $data['subscribers_total'] ) : ?>
719 - <div class="wp-block-jetpack-subscriptions__subscount">
720 - <?php
721 - echo esc_html( Jetpack_Memberships::get_join_others_text( $data['subscribers_total'] ) );
722 - ?>
723 - </div>
724 - <?php endif; ?>
725 - </div>
906 + <?php endif; ?>
907 + <?php if ( $show_subscriber_count ) : ?>
908 + <div class="wp-block-jetpack-subscriptions__subscount">
909 + <?php echo esc_html( Jetpack_Memberships::get_join_others_text( $data['subscribers_total'] ) ); ?>
910 + </div>
911 + <?php endif; ?>
726 912 </div>
727 913 </div>
728 914 <?php
729 915 return ob_get_clean();
@@ -738,12 +924,9 @@
738 924 * @return string
739 925 */
740 926 function render_for_email( $data, $styles ) {
741 927 $submit_button_wrapper_style = ! empty( $styles['submit_button_wrapper'] ) ? 'style="' . esc_attr( $styles['submit_button_wrapper'] ) . '"' : '';
742 - $button_text = wp_kses(
743 - html_entity_decode( $data['submit_button_text'], ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ),
744 - Jetpack_Subscriptions_Widget::$allowed_html_tags_for_submit_button
745 - );
928 + $button_text = get_submit_button_text( $data );
746 929
747 930 $html = '<div ' . wp_kses_data( $data['wrapper_attributes'] ) . '>
748 931 <div>
749 932 <div>
@@ -748,9 +931,9 @@
748 931 <div>
749 932 <div>
750 933 <div>
751 934 <p ' . $submit_button_wrapper_style . '>
752 - <a href="' . esc_url( get_post_permalink() ) . '" style="text-decoration: none; ' . esc_attr( $styles['submit_button'] ) . '">' . $button_text . '</a>
935 + <a href="' . esc_url( get_post_permalink() ) . '" style="' . esc_attr( $styles['submit_button'] ) . ' text-decoration: none; white-space: nowrap; margin-left: 0">' . sanitize_submit_text( $button_text ) . '</a>
753 936 </p>
754 937 </div>
755 938 </div>
756 939 </div>
@@ -759,26 +942,56 @@
759 942 return $html;
760 943 }
761 944
762 945 /**
763 - * Filter excerpts looking for subscription data.
946 + * WooCommerce Email Editor render callback for the subscriptions block.
764 947 *
765 - * @param string $excerpt The extrapolated excerpt string.
766 - * @param \WP_Post $post The current post being processed (in `get_the_excerpt`).
948 + * @param string $block_content The block content.
949 + * @param array $parsed_block The parsed block data.
950 + * @param object $rendering_context The email rendering context.
767 951 *
768 - * @return mixed
952 + * @return string
769 953 */
770 -function jetpack_filter_excerpt_for_newsletter( $excerpt, $post = null ) {
771 - // The blogmagazine theme is overriding WP core `get_the_excerpt` filter and only passing the excerpt
772 - // TODO: Until this is fixed, return the excerpt without gating. See https://github.com/Automattic/jetpack/pull/28102#issuecomment-1369161116
773 - if ( $post && false !== strpos( $post->post_content, '<!-- wp:jetpack/subscriptions -->' ) ) {
774 - $excerpt .= sprintf(
775 - // translators: %s is the permalink url to the current post.
776 - __( "<p><a href='%s'>View post</a> to subscribe to site newsletter.</p>", 'jetpack' ),
777 - get_post_permalink()
778 - );
954 +function render_email( $block_content, array $parsed_block, $rendering_context ) {
955 + if ( ! isset( $parsed_block['attrs'] ) || ! is_array( $parsed_block['attrs'] ) || ! function_exists( '\Automattic\Jetpack\Extensions\Button\render_email' ) || ! class_exists( '\Automattic\WooCommerce\EmailEditor\Integrations\Core\Renderer\Blocks\Button' ) ) {
956 + return '';
779 957 }
780 - return $excerpt;
958 +
959 + // Map subscription block attributes to button block attributes
960 + $button_attributes = array(
961 + 'text' => ! empty( $parsed_block['attrs']['submitButtonText'] ) ? sanitize_text_field( $parsed_block['attrs']['submitButtonText'] ) : __( 'Subscribe', 'jetpack' ),
962 + 'url' => get_post_permalink(),
963 + 'element' => 'a',
964 + // Map background colors
965 + 'backgroundColor' => $parsed_block['attrs']['buttonBackgroundColor'] ?? null,
966 + 'customBackgroundColor' => $parsed_block['attrs']['customButtonBackgroundColor'] ?? null,
967 + // Map text colors
968 + 'textColor' => $parsed_block['attrs']['textColor'] ?? null,
969 + 'customTextColor' => $parsed_block['attrs']['customTextColor'] ?? null,
970 + // Map borders
971 + 'borderRadius' => $parsed_block['attrs']['borderRadius'] ?? 0,
972 + 'borderWeight' => $parsed_block['attrs']['borderWeight'] ?? 1,
973 + 'borderColor' => $parsed_block['attrs']['borderColor'] ?? null,
974 + 'customBorderColor' => $parsed_block['attrs']['customBorderColor'] ?? null,
975 + // Map typography
976 + 'fontSize' => $parsed_block['attrs']['fontSize'] ?? null,
977 + 'customFontSize' => $parsed_block['attrs']['customFontSize'] ?? null,
978 + // Map spacing
979 + 'padding' => $parsed_block['attrs']['padding'] ?? null,
980 + );
981 +
982 + // Create a mock button block structure
983 + $button_parsed_block = array(
984 + 'attrs' => $button_attributes,
985 + 'email_attrs' => $parsed_block['email_attrs'] ?? array(),
986 + );
987 +
988 + // Call the Jetpack button's email rendering
989 + return \Automattic\Jetpack\Extensions\Button\render_email(
990 + $block_content,
991 + $button_parsed_block,
992 + $rendering_context
993 + );
781 994 }
782 995
783 996 /**
784 997 * Gate access to posts
@@ -792,9 +1005,9 @@
792 1005
793 1006 $post_access_level = Jetpack_Memberships::get_post_access_level();
794 1007
795 1008 if ( Jetpack_Memberships::user_can_view_post() ) {
796 - if ( $post_access_level !== Token_Subscription_Service::POST_ACCESS_LEVEL_EVERYBODY ) {
1009 + if ( $post_access_level !== Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_EVERYBODY ) {
797 1010 do_action(
798 1011 'earn_track_paywalled_post_view',
799 1012 array(
800 1013 'post_id' => get_the_ID(),
@@ -803,17 +1016,10 @@
803 1016 }
804 1017 return $the_content;
805 1018 }
806 1019
807 - require_once JETPACK__PLUGIN_DIR . 'extensions/blocks/premium-content/_inc/subscription-service/include.php';
808 - $token_service = is_wpcom() ? new WPCOM_Token_Subscription_Service() : new Jetpack_Token_Subscription_Service();
809 - $token = $token_service->get_and_set_token_from_request();
810 - $payload = $token_service->decode_token( $token );
811 - $is_valid_token = ! empty( $payload );
812 - $email_confirmation_pending = $is_valid_token && isset( $payload['blog_sub'] ) && $payload['blog_sub'] === 'pending';
1020 + $paywalled_content = get_paywall_content();
813 1021
814 - $paywalled_content = get_paywall_content( $post_access_level, $email_confirmation_pending ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
815 -
816 1022 if ( has_block( \Automattic\Jetpack\Extensions\Paywall\BLOCK_NAME ) ) {
817 1023 if ( strpos( $the_content, \Automattic\Jetpack\Extensions\Paywall\BLOCK_HTML ) ) {
818 1024 return strstr( $the_content, \Automattic\Jetpack\Extensions\Paywall\BLOCK_HTML, true ) . $paywalled_content;
819 1025 }
@@ -863,22 +1069,46 @@
863 1069 return '';
864 1070 }
865 1071
866 1072 /**
1073 + * Is the Jetpack_Token_Subscription_Service class loaded
1074 + *
1075 + * @return bool
1076 + */
1077 +function is_jetpack_token_subscription_service_loaded(): bool {
1078 + return class_exists( 'Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Jetpack_Token_Subscription_Service' );
1079 +}
1080 +
1081 +/**
1082 + * Adds support for WP Super cache and Boost cache
1083 + */
1084 +function maybe_prevent_super_cache_caching() {
1085 + // Prevents cached page to be served if the Membership cookie is present
1086 + if ( is_jetpack_token_subscription_service_loaded() ) {
1087 + do_action( 'wpsc_add_cookie', Jetpack_Token_Subscription_Service::JWT_AUTH_TOKEN_COOKIE_NAME );
1088 + }
1089 +
1090 + if ( is_user_auth() ) {
1091 + // Do not cache the page if user is auth with Membership token
1092 + if ( ! defined( 'DONOTCACHEPAGE' ) ) {
1093 + define( 'DONOTCACHEPAGE', true );
1094 + }
1095 + }
1096 +}
1097 +
1098 +/**
867 1099 * Returns paywall content blocks
868 1100 *
869 - * @param string $post_access_level The newsletter access level.
870 - * @param string $email_confirmation_pending True if the current user needs to validate their email.
871 1101 * @return string
872 1102 */
873 -function get_paywall_content( $post_access_level, $email_confirmation_pending = false ) {
874 - if ( $email_confirmation_pending ) {
1103 +function get_paywall_content() {
1104 + if ( Jetpack_Memberships::user_is_pending_subscriber() ) {
875 1105 return get_paywall_blocks_subscribe_pending();
876 1106 }
877 1107 if ( doing_filter( 'get_the_excerpt' ) ) {
878 1108 return '';
879 1109 }
880 - return get_paywall_blocks( $post_access_level );
1110 + return get_paywall_blocks();
881 1111 }
882 1112
883 1113 /**
884 1114 * Returns the current URL.
@@ -898,9 +1128,9 @@
898 1128 $host = ! empty( $_SERVER['HTTP_HOST'] ) ? wp_unslash( $_SERVER['HTTP_HOST'] ) : null;
899 1129 $path = ! empty( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '';
900 1130
901 1131 // Support for local plugin development and testing using ngrok.
902 - if ( ! empty( $_SERVER['HTTP_X_ORIGINAL_HOST'] ) && false !== strpos( $_SERVER['HTTP_X_ORIGINAL_HOST'], 'ngrok.io' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- This is validating.
1132 + if ( ! empty( $_SERVER['HTTP_X_ORIGINAL_HOST'] ) && str_contains( $_SERVER['HTTP_X_ORIGINAL_HOST'], 'ngrok.io' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- This is validating.
903 1133 $host = wp_unslash( $_SERVER['HTTP_X_ORIGINAL_HOST'] );
904 1134 }
905 1135 // phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
906 1136
@@ -912,24 +1142,70 @@
912 1142 return $current_url;
913 1143 }
914 1144
915 1145 /**
1146 + * Get the submit button text based on the subscription status.
1147 + *
1148 + * @param array $data Array containing block view data.
1149 + *
1150 + * @return string
1151 + */
1152 +function get_submit_button_text( $data ) {
1153 + if ( ! Jetpack_Memberships::is_current_user_subscribed() ) {
1154 + return $data['submit_button_text'];
1155 + }
1156 + if ( ! Jetpack_Memberships::user_can_view_post() ) {
1157 + return $data['submit_button_text_upgrade'];
1158 + }
1159 + return '✓ ' . $data['submit_button_text_subscribed'];
1160 +}
1161 +
1162 +/**
1163 + * Returns true if there are no more tiers to upgrade to.
1164 + *
1165 + * @return boolean
1166 + */
1167 +function is_top_subscription() {
1168 + if ( ! Jetpack_Memberships::is_current_user_subscribed() ) {
1169 + return false;
1170 + }
1171 + if ( ! Jetpack_Memberships::user_can_view_post() ) {
1172 + return false;
1173 + }
1174 + return true;
1175 +}
1176 +
1177 +/**
1178 + * Sanitize the submit button text.
1179 + *
1180 + * @param string $text String containing the submit button text.
1181 + *
1182 + * @return string
1183 + */
1184 +function sanitize_submit_text( $text ) {
1185 + return wp_kses(
1186 + html_entity_decode( $text, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ),
1187 + Jetpack_Subscriptions_Widget::$allowed_html_tags_for_submit_button
1188 + );
1189 +}
1190 +
1191 +/**
916 1192 * Returns paywall content blocks if user is not authenticated
917 1193 *
918 - * @param string $newsletter_access_level The newsletter access level.
919 1194 * @return string
920 1195 */
921 -function get_paywall_blocks( $newsletter_access_level ) {
1196 +function get_paywall_blocks() {
922 1197 $custom_paywall = apply_filters( 'jetpack_custom_paywall_blocks', false );
923 1198 if ( ! empty( $custom_paywall ) ) {
924 1199 return $custom_paywall;
925 1200 }
926 - if ( ! jetpack_is_frontend() ) { // emails
1201 +
1202 + if ( ! Request::is_frontend() ) { // emails
927 1203 return get_paywall_simple();
928 1204 }
1205 +
929 1206 require_once JETPACK__PLUGIN_DIR . 'modules/memberships/class-jetpack-memberships.php';
930 - // Only display paid texts when Stripe is connected and the post is marked for paid subscribers
931 - $is_paid_post = $newsletter_access_level === 'paid_subscribers' && Jetpack_Memberships::has_connected_account();
1207 + $is_paid_post = is_paid_post();
932 1208 $is_paid_subscriber = Jetpack_Memberships::user_is_paid_subscriber();
933 1209
934 1210 $access_heading = $is_paid_subscriber
935 1211 ? esc_html__( 'Upgrade to continue reading', 'jetpack' )
@@ -936,43 +1212,42 @@
936 1212 : esc_html__( 'Subscribe to continue reading', 'jetpack' );
937 1213
938 1214 $subscribe_text = $is_paid_post
939 1215 // translators: %s is the name of the site.
940 - ? $is_paid_subscriber
941 - ? esc_html__( 'Upgrade to get access to the rest of this post and other exclusive content.', 'jetpack' )
942 - : esc_html__( 'Become a paid subscriber to get access to the rest of this post and other exclusive content.', 'jetpack' )
1216 + ? (
1217 + $is_paid_subscriber
1218 + ? esc_html__( 'Upgrade to get access to the rest of this post and other exclusive content.', 'jetpack' )
1219 + : esc_html__( 'Become a paid subscriber to get access to the rest of this post and other exclusive content.', 'jetpack' )
1220 + )
943 1221 // translators: %s is the name of the site.
944 1222 : esc_html__( 'Subscribe to get access to the rest of this post and other subscriber-only content.', 'jetpack' );
945 1223
946 - $sign_in = '';
947 - $switch_accounts = '';
1224 + $login_block = '';
1225 +
948 1226 if ( is_user_auth() ) {
949 1227 if ( ( new Host() )->is_wpcom_simple() ) {
950 - $switch_accounts_link = wp_logout_url( get_current_url() );
951 - $switch_accounts = '<!-- wp:paragraph {"align":"center","style":{"typography":{"fontSize":"14px"}}} -->
952 -<p class="has-text-align-center" style="font-size:14px"><a href="' . $switch_accounts_link . '">' . __( 'Switch Accounts', 'jetpack' ) . '</a></p>
953 -<!-- /wp:paragraph -->';
954 -
955 - }
956 - } else {
957 - if ( ( new Host() )->is_wpcom_simple() ) {
958 - // custom domain
959 - $sign_in_link = wpcom_logmein_redirect_url( get_current_url(), false, null, 'link', get_current_blog_id() );
960 - } else {
961 - $sign_in_link = add_query_arg(
1228 + // We cannot use wpcom_logmein_redirect_url since it returns redirect URL when user is already logged in.
1229 + $login_link = add_query_arg(
962 1230 array(
963 - 'site_id' => intval( \Jetpack_Options::get_option( 'id' ) ),
964 - 'redirect_url' => rawurlencode( get_current_url() ),
965 - 'v2' => '',
1231 + 'redirect_to' => rawurlencode( get_current_url() ),
1232 + 'blog_id' => get_current_blog_id(),
966 1233 ),
967 - 'https://subscribe.wordpress.com/memberships/jwt'
1234 + 'https://wordpress.com/log-in/link'
968 1235 );
1236 + $switch_accounts_link = wp_logout_url( $login_link );
1237 + $login_block = '<!-- wp:paragraph {"align":"center","style":{"typography":{"fontSize":"14px"}}} -->
1238 +<p class="has-text-align-center" style="font-size:14px">
1239 + <a href="' . $switch_accounts_link . '">' . __( 'Switch accounts', 'jetpack' ) . '</a>
1240 +</p>
1241 +<!-- /wp:paragraph -->';
969 1242 }
970 - $access_question = get_paywall_access_question( $newsletter_access_level );
971 -
972 - $sign_in = '<!-- wp:paragraph {"align":"center","style":{"typography":{"fontSize":"14px"}}} -->
973 -<p class="has-text-align-center" style="font-size:14px"><a href="' . $sign_in_link . '">' . $access_question . '</a></p>
974 -<!-- /wp:paragraph -->';
1243 + } else {
1244 + $access_question = $is_paid_post ? esc_html__( 'Already a paid subscriber?', 'jetpack' ) : esc_html__( 'Already a subscriber?', 'jetpack' );
1245 + $login_block = '<!-- wp:group {"style":{"typography":{"fontSize":"14px"}},"layout":{"type":"flex","justifyContent":"center"}} -->
1246 +<div class="wp-block-group" style="font-size:14px">
1247 + <!-- wp:jetpack/subscriber-login {"logInLabel":"' . $access_question . '"} /-->
1248 +</div>
1249 +<!-- /wp:group -->';
975 1250 }
976 1251
977 1252 $lock_svg = plugins_url( 'images/lock-paywall.svg', JETPACK__PLUGIN_FILE );
978 1253
@@ -991,10 +1266,9 @@
991 1266 <p class="has-text-align-center" style="margin-top:10px;margin-bottom:10px;font-size:14px">' . $subscribe_text . '</p>
992 1267 <!-- /wp:paragraph -->
993 1268
994 1269 <!-- wp:jetpack/subscriptions {"borderRadius":50,"borderColor":"primary","className":"is-style-compact","isPaidSubscriber":' . ( $is_paid_subscriber ? 'true' : 'false' ) . '} /-->
995 -' . $sign_in . '
996 -' . $switch_accounts . '
1270 +' . $login_block . '
997 1271 </div>
998 1272 <!-- /wp:group -->
999 1273 ';
1000 1274 }
@@ -999,38 +1273,13 @@
999 1273 ';
1000 1274 }
1001 1275
1002 1276 /**
1003 - * Returns Get Access question for the paywall
1004 - *
1005 - * @param string $post_access_level The newsletter access level.
1006 - * @return string.
1007 - */
1008 -function get_paywall_access_question( $post_access_level ) {
1009 - switch ( $post_access_level ) {
1010 - case Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS:
1011 - case Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS_ALL_TIERS:
1012 - $tier = Jetpack_Memberships::get_post_tier();
1013 - if ( $tier !== null ) {
1014 - return sprintf(
1015 - // translators: Placeholder is the tier name
1016 - __( 'I am already a <i>%s</i> paid-subscriber', 'jetpack' ),
1017 - esc_html( $tier->post_title )
1018 - );
1019 - } else {
1020 - return esc_html__( 'I am already a paid-subscriber', 'jetpack' );
1021 - }
1022 - default:
1023 - return esc_html__( 'I am already a subscriber', 'jetpack' );
1024 - }
1025 -}
1026 -
1027 -/**
1028 1277 * Returns true if user is auth for subscriptions check, otherwise returns false.
1029 1278 *
1030 - * @return boolean
1279 + * @return bool
1031 1280 */
1032 -function is_user_auth() {
1281 +function is_user_auth(): bool {
1033 1282 if ( ( new Host() )->is_wpcom_simple() && is_user_logged_in() ) {
1034 1283 return true;
1035 1284 }
1036 1285 if ( current_user_can( 'manage_options' ) ) {
@@ -1035,9 +1284,10 @@
1035 1284 }
1036 1285 if ( current_user_can( 'manage_options' ) ) {
1037 1286 return true;
1038 1287 }
1039 - if ( class_exists( 'Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Jetpack_Token_Subscription_Service' ) ) {
1288 +
1289 + if ( is_jetpack_token_subscription_service_loaded() ) {
1040 1290 if ( Jetpack_Token_Subscription_Service::has_token_from_cookie() ) {
1041 1291 return true;
1042 1292 }
1043 1293 }
@@ -1044,22 +1294,61 @@
1044 1294 return false;
1045 1295 }
1046 1296
1047 1297 /**
1298 + * Returns `true` if the post is a paid post.
1299 + */
1300 +function is_paid_post(): bool {
1301 + require_once JETPACK__PLUGIN_DIR . 'modules/memberships/class-jetpack-memberships.php';
1302 +
1303 + // Make sure Stripe is connected and the post is marked for paid subscribers.
1304 + if ( Jetpack_Memberships::has_connected_account() && is_jetpack_token_subscription_service_loaded() ) {
1305 + return Jetpack_Memberships::get_post_access_level() === Jetpack_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS;
1306 + }
1307 +
1308 + return false;
1309 +}
1310 +
1311 +/**
1312 + * Returns true if the post is a subscribers post.
1313 + */
1314 +function is_subscribers_post(): bool {
1315 + require_once JETPACK__PLUGIN_DIR . 'modules/memberships/class-jetpack-memberships.php';
1316 +
1317 + // Make sure Stripe is connected and the post is marked for paid subscribers.
1318 + if ( Jetpack_Memberships::has_connected_account() && is_jetpack_token_subscription_service_loaded() ) {
1319 + return Jetpack_Memberships::get_post_access_level() === Jetpack_Token_Subscription_Service::POST_ACCESS_LEVEL_SUBSCRIBERS;
1320 + }
1321 +
1322 + return false;
1323 +}
1324 +
1325 +/**
1048 1326 * Returns paywall content blocks when email confirmation is pending
1049 1327 *
1050 1328 * @return string
1051 1329 */
1052 1330 function get_paywall_blocks_subscribe_pending() {
1053 - $access_heading = esc_html__( 'Verify your email to continue reading', 'jetpack' );
1331 + $subscribe_email = Jetpack_Memberships::get_current_user_email();
1054 1332
1055 - $subscribe_text = esc_html__( 'Please check your inbox to confirm your subscription.', 'jetpack' );
1333 + /** This filter is documented in \Automattic\Jetpack\Forms\ContactForm\Contact_Form */
1334 + if ( is_wpcom() || false !== apply_filters( 'jetpack_auto_fill_logged_in_user', false ) ) {
1335 + $current_user = wp_get_current_user();
1336 + if ( ! empty( $current_user->user_email ) ) {
1337 + $subscribe_email = $current_user->user_email;
1338 + }
1339 + }
1056 1340
1341 + $access_heading = esc_html__( 'Confirm your subscription to continue reading', 'jetpack' );
1342 +
1343 + /* translators: %s: email address */
1344 + $subscribe_text = sprintf( esc_html__( 'Head to your inbox and confirm your email address %s.', 'jetpack' ), $subscribe_email );
1345 +
1057 1346 $lock_svg = plugins_url( 'images/lock-paywall.svg', JETPACK__PLUGIN_FILE );
1058 1347
1059 1348 return '
1060 -<!-- wp:group {"style":{"border":{"width":"1px","radius":"4px"},"spacing":{"padding":{"top":"var:preset|spacing|70","bottom":"var:preset|spacing|70","left":"32px","right":"32px"}}},"borderColor":"primary","className":"jetpack-subscribe-paywall","layout":{"type":"constrained","contentSize":"400px"}} -->
1061 -<div class="wp-block-group jetpack-subscribe-paywall has-border-color has-primary-border-color" style="border-width:1px;border-radius:4px;padding-top:var(--wp--preset--spacing--70);padding-right:32px;padding-bottom:var(--wp--preset--spacing--70);padding-left:32px">
1349 +<!-- wp:group {"style":{"border":{"width":"1px","radius":"4px"},"spacing":{"padding":{"top":"32px","bottom":"32px","left":"32px","right":"32px"}}},"borderColor":"primary","className":"jetpack-subscribe-paywall","layout":{"type":"constrained","contentSize":"400px"}} -->
1350 +<div class="wp-block-group jetpack-subscribe-paywall has-border-color has-primary-border-color" style="border-width:1px;border-radius:4px;padding-top:32px;padding-right:32px;padding-bottom:32px;padding-left:32px">
1062 1351 <!-- wp:image {"align":"center","width":24,"height":24,"sizeSlug":"large","linkDestination":"none"} -->
1063 1352 <figure class="wp-block-image aligncenter size-large is-resized"><img src="' . $lock_svg . '" alt="" width="24" height="24"/></figure>
1064 1353 <!-- /wp:image -->
1065 1354
@@ -1075,22 +1364,37 @@
1075 1364 ';
1076 1365 }
1077 1366
1078 1367 /**
1079 - * Return content for non frontend views like emails.
1080 - *
1081 - * @return string
1368 + * Return content for non frontend views like Reader, emails.
1082 1369 */
1083 -function get_paywall_simple() {
1084 - $access_heading = esc_html__( "You're currently a free subscriber. Upgrade your subscription to get access to the rest of this post and other paid-subscriber only content.", 'jetpack' );
1370 +function get_paywall_simple(): string {
1371 + $is_paid_post = is_paid_post();
1372 + $is_subscribers_post = is_subscribers_post();
1373 + $is_subscriber = is_jetpack_memberships_loaded() && Jetpack_Memberships::is_current_user_subscribed();
1374 + $paywall_heading = esc_html__( 'Subscribe to keep reading', 'jetpack' );
1085 1375
1086 - $subscribe_text = esc_html__( 'Upgrade subscription', 'jetpack' );
1376 + if ( $is_subscribers_post && ! $is_subscriber ) {
1377 + $paywall_description = esc_html__( "It's a subscribers only post. Subscribe to get access to the rest of this post and other subscriber-only content.", 'jetpack' );
1378 + $paywall_action_btn = esc_html__( 'Subscribe', 'jetpack' );
1379 + } elseif ( $is_paid_post && $is_subscriber ) {
1380 + $paywall_description = esc_html__( "You're currently a free subscriber. Upgrade your subscription to get access to the rest of this post and other paid-subscriber only content.", 'jetpack' );
1381 + $paywall_action_btn = esc_html__( 'Upgrade subscription', 'jetpack' );
1382 + } else {
1383 + // - For paid post when the user is not a subscriber.
1384 + // - Default for all other cases.
1385 + $paywall_description = esc_html__( 'Become a paid subscriber to get access to the rest of this post and other exclusive content.', 'jetpack' );
1386 + $paywall_action_btn = esc_html__( 'Subscribe', 'jetpack' );
1387 + }
1087 1388
1088 1389 return '
1089 1390 <!-- wp:columns -->
1090 -<div class="wp-block-columns" style="display: inline-block; width: 90%">
1391 +<div class="wp-block-columns jetpack-paywall-simple" style="display: inline-block; width: 90%">
1091 1392 <!-- wp:column -->
1092 1393 <div class="wp-block-column" style="background-color: #F6F7F7; padding: 32px; 24px;">
1394 + <!-- wp:heading -->
1395 + <h2 class="has-text-align-center" style="margin: 0 0 12px; font-weight: 600;">' . $paywall_heading . '</h2>
1396 + <!-- /wp:heading -->
1093 1397 <!-- wp:paragraph -->
1094 1398 <p class="has-text-align-center"
1095 1399 style="text-align: center;
1096 1400 color: #50575E;
@@ -1097,21 +1401,20 @@
1097 1401 font-weight: 400;
1098 1402 font-size: 16px;
1099 1403 font-family: \'SF Pro Text\', sans-serif;
1100 1404 line-height: 28.8px;">
1101 - ' . $access_heading . '
1405 + ' . $paywall_description . '
1102 1406 </p>
1103 1407 <!-- /wp:paragraph -->
1104 -
1105 1408 <!-- wp:buttons -->
1106 1409 <div class="wp-block-buttons" style="text-align: center;">
1107 1410 <!-- wp:button -->
1108 - <div class="wp-block-button" style="display: inline-block; margin: 10px 0;">
1411 + <div class="wp-block-button" style="display: inline-block; margin: 10px 0; border-style: none; padding: 0;">
1109 1412 <a href="' . esc_url( get_post_permalink() ) . '" class="wp-block-button__link wp-element-button"
1110 1413 data-wpcom-track data-tracks-link-desc="paywall-email-click"
1111 1414 style="display: inline-block;
1112 - padding: 15px 20px;
1113 - background-color: #0675C4;
1415 + padding: 12px 15px;
1416 + background-color: #3858e9;
1114 1417 color: #FFFFFF;
1115 1418 text-decoration: none;
1116 1419 border-radius: 5px;
1117 1420 font-family: \'SF Pro Display\', sans-serif;
@@ -1116,9 +1419,9 @@
1116 1419 border-radius: 5px;
1117 1420 font-family: \'SF Pro Display\', sans-serif;
1118 1421 font-weight: 500;
1119 1422 font-size: 16px;
1120 - text-align: center;">' . $subscribe_text . '</a>
1423 + text-align: center;">' . $paywall_action_btn . '</a>
1121 1424 </div>
1122 1425 <!-- /wp:button -->
1123 1426 </div>
1124 1427 <!-- /wp:buttons -->