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 +592 -466 12.7.3 → 16.3-a.7 View file →
@@ -7,18 +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;
12 -use Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Token_Subscription_Service;
13 -use Automattic\Jetpack\Status;
11 +use Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Abstract_Token_Subscription_Service;
12 +use Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Jetpack_Token_Subscription_Service;
13 +use Automattic\Jetpack\Modules;
14 14 use Automattic\Jetpack\Status\Host;
15 -use Jetpack;
15 +use Automattic\Jetpack\Status\Request;
16 16 use Jetpack_Gutenberg;
17 17 use Jetpack_Memberships;
18 18 use Jetpack_Subscriptions_Widget;
19 19
20 +if ( ! defined( 'ABSPATH' ) ) {
21 + exit( 0 );
22 +}
23 +
24 +require_once __DIR__ . '/class-jetpack-subscription-site.php';
20 25 require_once __DIR__ . '/constants.php';
26 +require_once JETPACK__PLUGIN_DIR . 'extensions/blocks/premium-content/_inc/subscription-service/include.php';
21 27
22 28 /**
23 29 * These block defaults should match ./constants.js
24 30 */
@@ -26,11 +32,9 @@
26 32 const DEFAULT_BORDER_WEIGHT_VALUE = 1;
27 33 const DEFAULT_FONTSIZE_VALUE = '16px';
28 34 const DEFAULT_PADDING_VALUE = 15;
29 35 const DEFAULT_SPACING_VALUE = 10;
30 -const DEFAULT_ACCENT_COLOR = '#3c434a';
31 -const DEFAULT_BACKGROUND_COLOR = 'white';
32 -const DEFAULT_TEXT_COLOR = '#3c434a';
36 +const DEFAULT_BUTTON_WIDTH = 'auto';
33 37
34 38 /**
35 39 * Registers the block for use in Gutenberg
36 40 * This is done via an action so that we can disable
@@ -44,17 +48,24 @@
44 48 \WPForTeams\is_wpforteams_site( get_current_blog_id() ) ) {
45 49 return;
46 50 }
47 51
48 - if (
49 - ( defined( 'IS_WPCOM' ) && IS_WPCOM )
50 - || ( ( new Connection_Manager( 'jetpack' ) )->has_connected_owner() && ! ( new Status() )->is_offline_mode() )
51 - ) {
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() ) {
52 62 Blocks::jetpack_register_block(
53 - BLOCK_NAME,
63 + __DIR__,
54 64 array(
55 - 'render_callback' => __NAMESPACE__ . '\render_block',
56 - 'supports' => array(
65 + 'render_callback' => __NAMESPACE__ . '\render_block',
66 + 'render_email_callback' => __NAMESPACE__ . '\render_email',
67 + 'supports' => array(
57 68 'spacing' => array(
58 69 'margin' => true,
59 70 'padding' => true,
60 71 ),
@@ -63,31 +74,40 @@
63 74 )
64 75 );
65 76 }
66 77
67 - /*
68 - * If the Subscriptions module is not active,
69 - * do not make any further changes on the site.
70 - */
71 - if ( ! Jetpack::is_module_active( 'subscriptions' ) ) {
72 - return;
73 - }
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 + );
74 98
75 - /**
76 - * Do not proceed if the newsletter feature is not enabled
77 - * or if the 'Jetpack_Memberships' class does not exists.
78 - */
79 - if ( ! class_exists( '\Jetpack_Memberships' ) ) {
80 - return;
81 - }
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 );
82 101
83 102 register_post_meta(
84 103 'post',
85 - META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS,
104 + META_NAME_FOR_POST_DONT_EMAIL_TO_SUBS,
86 105 array(
106 + 'default' => $dont_email_default,
87 107 'show_in_rest' => true,
88 108 'single' => true,
89 - 'type' => 'string',
109 + 'type' => 'boolean',
90 110 'auth_callback' => function () {
91 111 return wp_get_current_user()->has_cap( 'edit_posts' );
92 112 },
93 113 )
@@ -105,13 +125,35 @@
105 125 },
106 126 )
107 127 );
108 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 +
109 142 // This ensures Jetpack will sync this post meta to WPCOM.
110 143 add_filter(
111 144 'jetpack_sync_post_meta_whitelist',
112 145 function ( $allowed_meta ) {
113 - 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 + );
114 156 }
115 157 );
116 158
117 159 // Hide the content – Priority 8 makes it run before do_blocks gets called for the content
@@ -123,14 +165,41 @@
123 165
124 166 // Hide existing comments
125 167 add_filter( 'get_comment', __NAMESPACE__ . '\maybe_gate_existing_comments' );
126 168
127 - // Gate the excerpt for a post
128 - 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 + }
129 176
130 - // Add a 'Newsletter access' column to the Edit posts page
131 - add_action( 'manage_post_posts_columns', __NAMESPACE__ . '\register_newsletter_access_column' );
132 - 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 + }
133 202 }
134 203 add_action( 'init', __NAMESPACE__ . '\register_block', 9 );
135 204
136 205 /**
@@ -148,15 +217,10 @@
148 217 * @param array $columns An array of column names.
149 218 * @return array An array of column names.
150 219 */
151 220 function register_newsletter_access_column( $columns ) {
152 - if ( ! Jetpack_Memberships::has_configured_plans_jetpack_recurring_payments( 'newsletter' ) ) {
153 - // We only display the "NL access" column if we have published one paid-newsletter
154 - return $columns;
155 - }
156 -
157 221 $position = array_search( 'title', array_keys( $columns ), true );
158 - $new_column = array( NEWSLETTER_COLUMN_ID => '<span>' . __( 'Newsletter', 'jetpack' ) . '</span>' );
222 + $new_column = array( NEWSLETTER_COLUMN_ID => __( 'Newsletter', 'jetpack' ) );
159 223 return array_merge(
160 224 array_slice( $columns, 0, $position + 1, true ),
161 225 $new_column,
162 226 array_slice( $columns, $position, null, true )
@@ -163,8 +227,38 @@
163 227 );
164 228 }
165 229
166 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 +/**
167 261 * Displays the newsletter access level.
168 262 *
169 263 * @param string $column_id The ID of the column to display.
170 264 * @param int $post_id The current post ID.
@@ -173,18 +267,21 @@
173 267 if ( NEWSLETTER_COLUMN_ID !== $column_id ) {
174 268 return;
175 269 }
176 270
177 - $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 );
178 272
179 273 switch ( $access_level ) {
180 - case Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS:
274 + case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS_ALL_TIERS:
275 + echo esc_html__( 'Paid Subscribers (all plans)', 'jetpack' );
276 + break;
277 + case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS:
181 278 echo esc_html__( 'Paid Subscribers', 'jetpack' );
182 279 break;
183 - case Token_Subscription_Service::POST_ACCESS_LEVEL_SUBSCRIBERS:
280 + case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_SUBSCRIBERS:
184 281 echo esc_html__( 'Subscribers', 'jetpack' );
185 282 break;
186 - case Token_Subscription_Service::POST_ACCESS_LEVEL_EVERYBODY:
283 + case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_EVERYBODY:
187 284 echo esc_html__( 'Everybody', 'jetpack' );
188 285 break;
189 286 default:
190 287 echo '';
@@ -191,11 +288,18 @@
191 288 }
192 289 }
193 290
194 291 /**
195 - * 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.
196 300 *
197 - * @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]]
198 302 */
199 303 function fetch_subscriber_counts() {
200 304 $subs_count = 0;
201 305 if ( is_wpcom() ) {
@@ -213,9 +317,10 @@
213 317 $subs_count = array(
214 318 'status' => 'failed',
215 319 'code' => $xml->getErrorCode(),
216 320 'message' => $xml->getErrorMessage(),
217 - 'value' => ( isset( $subs_count['value'] ) ) ? $subs_count['value'] : array(
321 + 'value' => $subs_count['value'] ?? array(
322 + 'total_subscribers' => 0,
218 323 'email_subscribers' => 0,
219 324 'social_followers' => 0,
220 325 'paid_subscribers' => 0,
221 326 ),
@@ -241,64 +346,16 @@
241 346 function get_subscriber_count( $include_social_followers ) {
242 347 $counts = fetch_subscriber_counts();
243 348
244 349 if ( $include_social_followers ) {
245 - $subscriber_count = $counts['value']['email_subscribers'] + $counts['value']['social_followers'];
350 + $subscriber_count = $counts['value']['total_subscribers'] + $counts['value']['social_followers'];
246 351 } else {
247 - $subscriber_count = $counts['value']['email_subscribers'];
352 + $subscriber_count = $counts['value']['total_subscribers'];
248 353 }
249 354 return $subscriber_count;
250 355 }
251 356
252 357 /**
253 - * Returns the newsletter categories from the .com API or the Jetpack XML-RPC API.
254 - *
255 - * @return array containing [ 'enabled' => true|false, 'newsletter_categories' => array ]
256 - */
257 -function fetch_newsletter_categories() {
258 - $categories = array();
259 -
260 - if ( is_wpcom() ) {
261 - $categories = \wpcom_fetch_newsletter_categories();
262 - } else {
263 - $xml = new \Jetpack_IXR_Client();
264 - $xml->query( 'jetpack.fetchNewsletterCategories' );
265 -
266 - if ( ! $xml->isError() ) {
267 - $categories = $xml->getResponse();
268 - }
269 - }
270 -
271 - return $categories;
272 -}
273 -
274 -/**
275 - * Returns the newsletter categories if the feature is enabled, otherwise returns an empty array.
276 - *
277 - * @return array
278 - */
279 -function get_newsletter_categories() {
280 - // opt out of rendering when we display newsletter catgeories in the subscribe modal
281 - $should_render_newsletter_categories = apply_filters( 'wpcom_newsletter_categories_location', 'block' ) === 'block';
282 - if ( ! $should_render_newsletter_categories ) {
283 - return array();
284 - }
285 -
286 - $response = fetch_newsletter_categories();
287 -
288 - if (
289 - isset( $response['enabled'] )
290 - && (bool) $response['enabled']
291 - && isset( $response['newsletter_categories'] )
292 - && is_array( $response['newsletter_categories'] )
293 - ) {
294 - return $response['newsletter_categories'];
295 - }
296 -
297 - return array();
298 -}
299 -
300 -/**
301 358 * Returns true if the block attributes contain a value for the given key.
302 359 *
303 360 * @param array $attributes Array containing the block attributes.
304 361 * @param string $key Block attribute key.
@@ -396,8 +453,24 @@
396 453 );
397 454 }
398 455
399 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 +/**
400 473 * Uses block attributes to generate an array containing the styles for various block elements.
401 474 * Based on Jetpack_Subscriptions_Widget::do_subscription_form() which the block was originally using.
402 475 *
403 476 * @param array $attributes Array containing the block attributes.
@@ -404,8 +477,10 @@
404 477 *
405 478 * @return array
406 479 */
407 480 function get_element_styles_from_attributes( $attributes ) {
481 + $is_button_only_style = is_button_only_style( get_attribute( $attributes, 'className', '' ) );
482 +
408 483 $button_background_style = ! has_attribute( $attributes, 'buttonBackgroundColor' ) && has_attribute( $attributes, 'customButtonGradient' )
409 484 ? get_attribute( $attributes, 'customButtonGradient' )
410 485 : get_attribute( $attributes, 'customButtonBackgroundColor' );
411 486
@@ -421,15 +496,15 @@
421 496 $submit_button_styles .= sprintf( 'color: %s;', get_attribute( $attributes, 'customTextColor' ) );
422 497 }
423 498
424 499 if ( has_attribute( $attributes, 'buttonWidth' ) ) {
425 - $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 ) );
426 501 $submit_button_wrapper_styles .= 'max-width: 100%;';
427 502
428 503 // Account for custom margins on inline forms.
429 504 $submit_button_styles .= true === get_attribute( $attributes, 'buttonOnNewLine' )
430 - ? sprintf( 'width: calc(100%% - %dpx);', get_attribute( $attributes, 'spacing', DEFAULT_SPACING_VALUE ) )
431 - : 'width: 100%;';
505 + ? 'width: 100%;'
506 + : sprintf( 'width: calc(100%% - %dpx);', get_attribute( $attributes, 'spacing', DEFAULT_SPACING_VALUE ) );
432 507 }
433 508
434 509 $font_size = get_attribute( $attributes, 'customFontSize', DEFAULT_FONTSIZE_VALUE );
435 510 $style = sprintf( 'font-size: %s%s;', $font_size, is_numeric( $font_size ) ? 'px' : '' );
@@ -442,14 +517,16 @@
442 517
443 518 $submit_button_styles .= $style;
444 519 $email_field_styles .= $style;
445 520
446 - $button_spacing = get_attribute( $attributes, 'spacing', DEFAULT_SPACING_VALUE );
447 - if ( true === get_attribute( $attributes, 'buttonOnNewLine' ) ) {
448 - $submit_button_styles .= sprintf( 'margin-top: %dpx;', $button_spacing );
449 - } else {
450 - $submit_button_styles .= 'margin: 0px; '; // Reset Safari's 2px default margin for buttons affecting input and button union
451 - $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 + }
452 529 }
453 530
454 531 if ( has_attribute( $attributes, 'borderColor' ) ) {
455 532 $style = sprintf( 'border-color: %s;', get_attribute( $attributes, 'borderColor', '' ) );
@@ -464,39 +541,8 @@
464 541 $style = sprintf( 'border-width: %dpx;', get_attribute( $attributes, 'borderWeight', DEFAULT_BORDER_WEIGHT_VALUE ) );
465 542 $submit_button_styles .= $style;
466 543 $email_field_styles .= $style;
467 544
468 - $categories_styles = sprintf( '--subscribe-block-border-radius: %dpx;', get_attribute( $attributes, 'borderRadius', DEFAULT_BORDER_RADIUS_VALUE ) );
469 -
470 - $subscribe_block_accent_color = DEFAULT_ACCENT_COLOR;
471 - $subscribe_block_background_color = DEFAULT_BACKGROUND_COLOR;
472 - $subscribe_block_text_color = DEFAULT_TEXT_COLOR;
473 -
474 - $global_styles = wp_get_global_styles(
475 - array( 'color' )
476 - );
477 -
478 - if ( isset( $global_styles['background'] ) ) {
479 - $subscribe_block_background_color = $global_styles['background'];
480 - }
481 -
482 - if ( isset( $global_styles['text'] ) ) {
483 - $subscribe_block_text_color = $global_styles['text'];
484 - }
485 -
486 - if ( function_exists( 'wpcom_get_site_accent_color' ) ) {
487 - $site_accent_color = wpcom_get_site_accent_color();
488 - if ( $site_accent_color ) {
489 - $subscribe_block_accent_color = $site_accent_color;
490 - }
491 - } else {
492 - $subscribe_block_accent_color = $subscribe_block_text_color;
493 -
494 - }
495 - $categories_styles .= sprintf( ' --subscribe-block-accent-color: %s; ', $subscribe_block_accent_color );
496 - $categories_styles .= sprintf( ' --subscribe-block-background-color: %s; ', $subscribe_block_background_color );
497 - $categories_styles .= sprintf( ' --subscribe-block-text-color: %s; ', $subscribe_block_text_color );
498 -
499 545 if ( has_attribute( $attributes, 'customBorderColor' ) ) {
500 546 $style = sprintf( 'border-color: %s; border-style: solid;', get_attribute( $attributes, 'customBorderColor' ) );
501 547
502 548 $submit_button_styles .= $style;
@@ -502,9 +548,9 @@
502 548 $submit_button_styles .= $style;
503 549 $email_field_styles .= $style;
504 550 }
505 551
506 - if ( ! jetpack_is_frontend() ) {
552 + if ( ! Request::is_frontend() ) {
507 553 $background_color_style = get_attribute_color( 'buttonBackgroundColor', $attributes, '#113AF5' /* default lettre theme color */ );
508 554 $text_color_style = get_attribute_color( 'textColor', $attributes, '#FFFFFF' );
509 555 $submit_button_styles .= sprintf( ' background-color: %s; color: %s;', $background_color_style, $text_color_style );
510 556 }
@@ -512,9 +558,8 @@
512 558 return array(
513 559 'email_field' => $email_field_styles,
514 560 'submit_button' => $submit_button_styles,
515 561 'submit_button_wrapper' => $submit_button_wrapper_styles,
516 - 'categories' => $categories_styles,
517 562 );
518 563 }
519 564
520 565 /**
@@ -590,8 +635,15 @@
590 635 return null;
591 636 }
592 637
593 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 +/**
594 646 * Subscriptions block render callback.
595 647 *
596 648 * @param array $attributes Array containing the block attributes.
597 649 *
@@ -598,28 +650,32 @@
598 650 * @return string
599 651 */
600 652 function render_block( $attributes ) {
601 653 // If the Subscriptions module is not active, don't render the block.
602 - if ( ! Jetpack::is_module_active( 'subscriptions' ) ) {
654 + if ( ! ( new Modules() )->is_active( 'subscriptions' ) ) {
603 655 return '';
604 656 }
605 657
606 - if ( class_exists( '\Jetpack_Memberships' ) ) {
607 - // We only want the sites that have newsletter feature enabled to be graced by this JavaScript and thickbox.
608 - Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME, array( 'thickbox' ) );
609 - if ( ! wp_style_is( 'enqueued' ) ) {
610 - wp_enqueue_style( 'thickbox' );
611 - }
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__ );
612 661 } else {
613 662 Jetpack_Gutenberg::load_styles_as_required( FEATURE_NAME );
614 663 }
615 664
616 - $subscribe_email = '';
665 + if ( ! class_exists( 'Jetpack_Subscriptions_Widget' ) ) {
666 + return '';
667 + }
617 668
618 - /** This filter is documented in modules/contact-form/grunion-contact-form.php */
619 - if ( is_wpcom() || false !== apply_filters( 'jetpack_auto_fill_logged_in_user', false ) ) {
620 - $current_user = wp_get_current_user();
621 - $subscribe_email = ! empty( $current_user->user_email ) ? $current_user->user_email : '';
669 + // Prefill the email field with the current user's email if they are logged in via Memberships premium content token
670 + $subscribe_email = Jetpack_Memberships::get_current_user_email();
671 +
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 + }
622 678 }
623 679
624 680 // The block is using the Jetpack_Subscriptions_Widget backend, hence the need to increase the instance count.
625 681 ++Jetpack_Subscriptions_Widget::$instance_count;
@@ -626,45 +682,56 @@
626 682
627 683 $classes = get_element_class_names_from_attributes( $attributes );
628 684 $styles = get_element_styles_from_attributes( $attributes );
629 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.
630 691 $include_social_followers = isset( $attributes['includeSocialFollowers'] ) ? (bool) get_attribute( $attributes, 'includeSocialFollowers' ) : true;
631 - $is_paid_subscriber = get_attribute( $attributes, 'isPaidSubscriber', false );
632 692
633 693 $data = array(
634 - 'widget_id' => Jetpack_Subscriptions_Widget::$instance_count,
635 - 'subscribe_email' => $subscribe_email,
636 -
637 - '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(
638 698 array(
639 699 'class' => $classes['block_wrapper'],
640 700 )
641 701 ),
642 - 'subscribe_placeholder' => get_attribute( $attributes, 'subscribePlaceholder', esc_html__( 'Type your email…', 'jetpack' ) ),
643 - 'submit_button_text' => get_attribute( $attributes, 'submitButtonText', $is_paid_subscriber ? esc_html__( 'Upgrade', 'jetpack' ) : esc_html__( 'Subscribe', 'jetpack' ) ),
644 - '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(
645 707 $attributes,
646 708 'successMessage',
647 - 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' )
648 710 ),
649 - 'show_subscribers_total' => (bool) get_attribute( $attributes, 'showSubscribersTotal' ),
650 - 'subscribers_total' => get_subscriber_count( $include_social_followers ),
651 - '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(
652 714 ( is_ssl() ? 'https' : 'http' ) . '://' . ( isset( $_SERVER['HTTP_HOST'] ) ? wp_unslash( $_SERVER['HTTP_HOST'] ) : '' ) .
653 715 ( isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '' )
654 716 ),
655 - 'source' => 'subscribe-block',
656 - 'newsletter_categories' => get_newsletter_categories(),
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 ),
657 722 );
658 723
659 - 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() ) {
660 730 return render_for_email( $data, $styles );
661 731 }
662 - if ( is_wpcom() ) {
663 - return render_wpcom_subscribe_form( $data, $classes, $styles );
664 - }
665 732
666 - return render_jetpack_subscribe_form( $data, $classes, $styles );
733 + return render_for_website( $data, $classes, $styles );
667 734 }
668 735
669 736 /**
670 737 * Get the post access level for the current post. Defaults to 'everybody' if the query is not for a single post
@@ -673,9 +740,9 @@
673 740 */
674 741 function get_post_access_level_for_current_post() {
675 742 if ( ! is_singular() ) {
676 743 // There is no "actual" current post.
677 - return Token_Subscription_Service::POST_ACCESS_LEVEL_EVERYBODY;
744 + return Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_EVERYBODY;
678 745 }
679 746
680 747 return Jetpack_Memberships::get_post_access_level();
681 748 }
@@ -680,45 +747,10 @@
680 747 return Jetpack_Memberships::get_post_access_level();
681 748 }
682 749
683 750 /**
684 - * Renders the newsletter categories in checkbox form.
751 + * Renders the subscriptions block at the site.
685 752 *
686 - * This function outputs a div containing labels and checkboxes for each newsletter category.
687 - * Each checkbox represents a category, allowing users to select multiple newsletter categories.
688 - *
689 - * @param array $newsletter_categories An array of newsletter categories where each category is an associative array
690 - * with keys 'id' and 'name' representing the category's ID and display name, respectively.
691 - * @param array $styles An associative array of style settings. Default is an empty array.
692 - *
693 - * @return void Outputs HTML directly.
694 - */
695 -function render_newsletter_categories( $newsletter_categories, $styles = array() ) {
696 - if ( count( $newsletter_categories ) ) :
697 - ?>
698 - <div
699 - class="wp-block-jetpack-subscriptions__newsletter-categories"
700 - style="<?php echo esc_attr( $styles['categories'] ); ?>"
701 - >
702 - <?php foreach ( $newsletter_categories as $category ) : ?>
703 - <label class="wp-block-jetpack-subscriptions__newsletter-category">
704 - <input type="checkbox" value="<?php echo esc_html( $category['id'] ); ?>" />
705 - <div>
706 - <svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
707 - <path fill-rule="evenodd" clip-rule="evenodd" d="M14.9406 6.6485L8.94211 14.7159L5.1785 11.9174L5.92436 10.9143L8.68487 12.9669L13.9375 5.90264L14.9406 6.6485Z" fill="#1D39EB"/>
708 - </svg>
709 - <?php echo esc_html( $category['name'] ); ?>
710 - </div>
711 - </label>
712 - <?php endforeach; ?>
713 - </div>
714 - <?php
715 - endif;
716 -}
717 -
718 -/**
719 - * Renders the WP.com version of the subscriptions block.
720 - *
721 753 * @param array $data Array containing block view data.
722 754 * @param array $classes Array containing the classes for different block elements.
723 755 * @param array $styles Array containing the styles for different block elements.
724 756 *
@@ -723,14 +755,33 @@
723 755 * @param array $styles Array containing the styles for different block elements.
724 756 *
725 757 * @return string
726 758 */
727 -function render_wpcom_subscribe_form( $data, $classes, $styles ) {
728 - global $current_blog;
759 +function render_for_website( $data, $classes, $styles ) {
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;
729 767
730 - $form_id = 'subscribe-blog' . ( Jetpack_Subscriptions_Widget::$instance_count > 1 ? '-' . Jetpack_Subscriptions_Widget::$instance_count : '' );
731 - $url = defined( 'SUBSCRIBE_BLOG_URL' ) ? SUBSCRIBE_BLOG_URL : '';
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 + }
732 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 +
733 784 ob_start();
734 785
735 786 Jetpack_Subscriptions_Widget::render_widget_status_messages(
736 787 array(
@@ -736,96 +787,19 @@
736 787 array(
737 788 'success_message' => $data['success_message'],
738 789 )
739 790 );
740 -
741 - $post_access_level = get_post_access_level_for_current_post();
742 - $post_id = get_the_ID();
743 - $tier_id = get_post_meta( $post_id, META_NAME_FOR_POST_TIER_ID_SETTINGS, true );
744 -
745 - $newsletter_categories = $data['newsletter_categories'];
746 -
747 791 ?>
748 792 <div <?php echo wp_kses_data( $data['wrapper_attributes'] ); ?>>
749 - <div class="wp-block-jetpack-subscriptions__container">
750 -
751 - <form
752 - action="<?php echo esc_url( $url ); ?>"
753 - method="post"
754 - accept-charset="utf-8"
755 - data-blog="<?php echo esc_attr( get_current_blog_id() ); ?>"
756 - data-post_access_level="<?php echo esc_attr( $post_access_level ); ?>"
757 - id="<?php echo esc_attr( $form_id ); ?>"
758 - >
759 -
760 - <?php render_newsletter_categories( $newsletter_categories, $styles ); ?>
761 -
762 - <div class="wp-block-jetpack-subscriptions__form-elements">
763 - <?php
764 - $email_field_id = 'subscribe-field';
765 - $email_field_id .= Jetpack_Subscriptions_Widget::$instance_count > 1
766 - ? '-' . Jetpack_Subscriptions_Widget::$instance_count
767 - : '';
768 - $label_field_id = $email_field_id . '-label';
769 - ?>
770 - <p id="subscribe-email">
771 - <label
772 - id="<?php echo esc_attr( $label_field_id ); ?>"
773 - for="<?php echo esc_attr( $email_field_id ); ?>"
774 - class="screen-reader-text"
775 - >
776 - <?php echo esc_html( $data['subscribe_placeholder'] ); ?>
777 - </label>
778 -
779 - <?php
780 - printf(
781 - '<input
782 - required="required"
783 - type="email"
784 - name="email"
785 - %1$s
786 - style="%2$s"
787 - placeholder="%3$s"
788 - value="%4$s"
789 - id="%5$s"
790 - />',
791 - ( ! empty( $classes['email_field'] )
792 - ? 'class="' . esc_attr( $classes['email_field'] ) . '"'
793 - : ''
794 - ),
795 - ( ! empty( $styles['email_field'] )
796 - ? esc_attr( $styles['email_field'] )
797 - : 'width: 95%; padding: 1px 10px'
798 - ),
799 - esc_attr( $data['subscribe_placeholder'] ),
800 - esc_attr( $data['subscribe_email'] ),
801 - esc_attr( $email_field_id )
802 - );
803 - ?>
804 - </p>
805 -
806 - <p id="subscribe-submit"
807 - <?php if ( ! empty( $styles['submit_button_wrapper'] ) ) : ?>
808 - style="<?php echo esc_attr( $styles['submit_button_wrapper'] ); ?>"
809 - <?php endif; ?>
810 - >
811 - <input type="hidden" name="action" value="subscribe"/>
812 - <input type="hidden" name="blog_id" value="<?php echo (int) $current_blog->blog_id; ?>"/>
813 - <input type="hidden" name="source" value="<?php echo esc_url( $data['referer'] ); ?>"/>
814 - <input type="hidden" name="sub-type" value="<?php echo esc_attr( $data['source'] ); ?>"/>
815 - <input type="hidden" name="redirect_fragment" value="<?php echo esc_attr( $form_id ); ?>"/>
816 - <?php wp_nonce_field( 'blogsub_subscribe_' . $current_blog->blog_id, '_wpnonce', false ); ?>
817 - <?php
818 - if ( ! empty( $post_id ) ) {
819 - echo '<input type="hidden" name="post_id" value="' . esc_attr( $post_id ) . '"/>';
820 - }
821 - ?>
822 - <?php
823 - if ( ! empty( $tier_id ) ) {
824 - echo '<input type="hidden" name="tier_id" value="' . esc_attr( $tier_id ) . '"/>';
825 - }
826 - ?>
827 - <button type="submit"
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 ); ?>"
828 802 <?php if ( ! empty( $classes['submit_button'] ) ) : ?>
829 803 class="<?php echo esc_attr( $classes['submit_button'] ); ?>"
830 804 <?php endif; ?>
831 805 <?php if ( ! empty( $styles['submit_button'] ) ) : ?>
@@ -831,95 +805,64 @@
831 805 <?php if ( ! empty( $styles['submit_button'] ) ) : ?>
832 806 style="<?php echo esc_attr( $styles['submit_button'] ); ?>"
833 807 <?php endif; ?>
834 808 >
835 - <?php
836 - echo wp_kses(
837 - html_entity_decode( $data['submit_button_text'], ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ),
838 - Jetpack_Subscriptions_Widget::$allowed_html_tags_for_submit_button
839 - );
840 - ?>
841 - </button>
842 - </p>
843 - </div>
844 - </form>
845 - <?php if ( $data['show_subscribers_total'] && $data['subscribers_total'] ) : ?>
846 - <div class="wp-block-jetpack-subscriptions__subscount">
847 - <?php
848 - /* translators: %s: number of folks following the blog */
849 - echo esc_html( sprintf( _n( 'Join %s other follower', 'Join %s other followers', $data['subscribers_total'], 'jetpack' ), number_format_i18n( $data['subscribers_total'] ) ) );
850 - ?>
851 - </div>
852 - <?php endif; ?>
853 - </div>
854 - </div>
855 - <?php
856 -
857 - return ob_get_clean();
858 -}
859 -
860 -/**
861 - * Renders the Jetpack version of the subscriptions block.
862 - *
863 - * @param array $data Array containing block view data.
864 - * @param array $classes Array containing the classes for different block elements.
865 - * @param array $styles Array containing the styles for different block elements.
866 - *
867 - * @return string
868 - */
869 -function render_jetpack_subscribe_form( $data, $classes, $styles ) {
870 - $form_id = sprintf( 'subscribe-blog-%s', $data['widget_id'] );
871 - $subscribe_field_id = apply_filters( 'subscribe_field_id', 'subscribe-field', $data['widget_id'] );
872 -
873 - ob_start();
874 -
875 - Jetpack_Subscriptions_Widget::render_widget_status_messages(
876 - array(
877 - 'success_message' => $data['success_message'],
878 - )
879 - );
880 -
881 - $blog_id = \Jetpack_Options::get_option( 'id' );
882 - $post_access_level = get_post_access_level_for_current_post();
883 - $post_id = get_the_ID();
884 - $tier_id = get_post_meta( $post_id, META_NAME_FOR_POST_TIER_ID_SETTINGS, true );
885 -
886 - $newsletter_categories = $data['newsletter_categories'];
887 -
888 - ?>
889 - <div <?php echo wp_kses_data( $data['wrapper_attributes'] ); ?>>
890 - <div class="jetpack_subscription_widget">
891 - <div class="wp-block-jetpack-subscriptions__container">
892 -
809 + <?php echo sanitize_submit_text( $button_text ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
810 + </a>
811 + </p>
812 + <?php else : ?>
893 813 <form
894 - action="#"
814 + action="<?php echo esc_url( $form_url ); ?>"
895 815 method="post"
896 816 accept-charset="utf-8"
897 817 data-blog="<?php echo esc_attr( $blog_id ); ?>"
898 818 data-post_access_level="<?php echo esc_attr( $post_access_level ); ?>"
819 + data-subscriber_email="<?php echo esc_attr( $data['subscribe_email'] ); ?>"
899 820 id="<?php echo esc_attr( $form_id ); ?>"
900 821 >
901 - <?php render_newsletter_categories( $newsletter_categories, $styles ); ?>
902 822 <div class="wp-block-jetpack-subscriptions__form-elements">
823 + <?php if ( ! $is_subscribed && ! $is_button_only_style ) : ?>
903 824 <p id="subscribe-email">
904 - <label id="jetpack-subscribe-label"
825 + <label
826 + id="<?php echo esc_attr( $subscribe_field_id . '-label' ); ?>"
827 + for="<?php echo esc_attr( $subscribe_field_id ); ?>"
905 828 class="screen-reader-text"
906 - for="<?php echo esc_attr( $subscribe_field_id . '-' . $data['widget_id'] ); ?>">
829 + >
907 830 <?php echo esc_html( $data['subscribe_placeholder'] ); ?>
908 831 </label>
909 - <input type="email" name="email" required="required"
910 - <?php if ( ! empty( $classes['email_field'] ) ) : ?>
911 - class="<?php echo esc_attr( $classes['email_field'] ); ?> required"
912 - <?php endif; ?>
913 - <?php if ( ! empty( $styles['email_field'] ) ) : ?>
914 - style="<?php echo esc_attr( $styles['email_field'] ); ?>"
915 - <?php endif; ?>
916 - value="<?php echo esc_attr( $data['subscribe_email'] ); ?>"
917 - id="<?php echo esc_attr( $subscribe_field_id . '-' . $data['widget_id'] ); ?>"
918 - placeholder="<?php echo esc_attr( $data['subscribe_placeholder'] ); ?>"
919 - />
832 + <?php
833 + printf(
834 + '<input
835 + required="required"
836 + type="email"
837 + name="email"
838 + autocomplete="email"
839 + %1$s
840 + style="%2$s"
841 + placeholder="%3$s"
842 + value="%4$s"
843 + id="%5$s"
844 + %6$s
845 + />',
846 + ( ! empty( $classes['email_field'] )
847 + ? 'class="' . esc_attr( $classes['email_field'] ) . '"'
848 + : ''
849 + ),
850 + ( ! empty( $styles['email_field'] )
851 + ? esc_attr( $styles['email_field'] )
852 + : 'width: 95%; padding: 1px 10px'
853 + ),
854 + esc_attr( $data['subscribe_placeholder'] ),
855 + esc_attr( $data['subscribe_email'] ),
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 + )
861 + );
862 + ?>
920 863 </p>
921 -
864 + <?php endif; ?>
922 865 <p id="subscribe-submit"
923 866 <?php if ( ! empty( $styles['submit_button_wrapper'] ) ) : ?>
924 867 style="<?php echo esc_attr( $styles['submit_button_wrapper'] ); ?>"
925 868 <?php endif; ?>
@@ -927,22 +870,24 @@
927 870 <input type="hidden" name="action" value="subscribe"/>
928 871 <input type="hidden" name="blog_id" value="<?php echo (int) $blog_id; ?>"/>
929 872 <input type="hidden" name="source" value="<?php echo esc_url( $data['referer'] ); ?>"/>
930 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'] ); ?>"/>
931 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 ); ?>"/>
932 877 <?php
878 + wp_nonce_field( 'blogsub_subscribe_' . $blog_id );
879 +
933 880 if ( ! empty( $post_id ) ) {
934 881 echo '<input type="hidden" name="post_id" value="' . esc_attr( $post_id ) . '"/>';
935 882 }
936 - ?>
937 - <?php
883 +
938 884 if ( ! empty( $tier_id ) ) {
939 885 echo '<input type="hidden" name="tier_id" value="' . esc_attr( $tier_id ) . '"/>';
940 886 }
941 - ?>
942 - <?php
943 - if ( is_user_logged_in() ) {
944 - wp_nonce_field( 'blogsub_subscribe_' . get_current_blog_id(), '_wpnonce', false );
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'] ) ) . '"/>';
945 890 }
946 891 ?>
947 892 <button type="submit"
948 893 <?php if ( ! empty( $classes['submit_button'] ) ) : ?>
@@ -952,31 +897,22 @@
952 897 style="<?php echo esc_attr( $styles['submit_button'] ); ?>"
953 898 <?php endif; ?>
954 899 name="jetpack_subscriptions_widget"
955 900 >
956 - <?php
957 - echo wp_kses(
958 - html_entity_decode( $data['submit_button_text'], ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ),
959 - Jetpack_Subscriptions_Widget::$allowed_html_tags_for_submit_button
960 - );
961 - ?>
901 + <?php echo sanitize_submit_text( $button_text ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
962 902 </button>
963 903 </p>
964 904 </div>
965 905 </form>
966 -
967 - <?php if ( $data['show_subscribers_total'] && $data['subscribers_total'] ) : ?>
968 - <div class="wp-block-jetpack-subscriptions__subscount">
969 - <?php
970 - echo esc_html( Jetpack_Memberships::get_join_others_text( $data['subscribers_total'] ) );
971 - ?>
972 - </div>
973 - <?php endif; ?>
974 - </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; ?>
975 912 </div>
976 913 </div>
977 914 <?php
978 -
979 915 return ob_get_clean();
980 916 }
981 917
982 918 /**
@@ -988,12 +924,9 @@
988 924 * @return string
989 925 */
990 926 function render_for_email( $data, $styles ) {
991 927 $submit_button_wrapper_style = ! empty( $styles['submit_button_wrapper'] ) ? 'style="' . esc_attr( $styles['submit_button_wrapper'] ) . '"' : '';
992 - $button_text = wp_kses(
993 - html_entity_decode( $data['submit_button_text'], ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ),
994 - Jetpack_Subscriptions_Widget::$allowed_html_tags_for_submit_button
995 - );
928 + $button_text = get_submit_button_text( $data );
996 929
997 930 $html = '<div ' . wp_kses_data( $data['wrapper_attributes'] ) . '>
998 931 <div>
999 932 <div>
@@ -998,9 +931,9 @@
998 931 <div>
999 932 <div>
1000 933 <div>
1001 934 <p ' . $submit_button_wrapper_style . '>
1002 - <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>
1003 936 </p>
1004 937 </div>
1005 938 </div>
1006 939 </div>
@@ -1009,26 +942,56 @@
1009 942 return $html;
1010 943 }
1011 944
1012 945 /**
1013 - * Filter excerpts looking for subscription data.
946 + * WooCommerce Email Editor render callback for the subscriptions block.
1014 947 *
1015 - * @param string $excerpt The extrapolated excerpt string.
1016 - * @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.
1017 951 *
1018 - * @return mixed
952 + * @return string
1019 953 */
1020 -function jetpack_filter_excerpt_for_newsletter( $excerpt, $post = null ) {
1021 - // The blogmagazine theme is overriding WP core `get_the_excerpt` filter and only passing the excerpt
1022 - // TODO: Until this is fixed, return the excerpt without gating. See https://github.com/Automattic/jetpack/pull/28102#issuecomment-1369161116
1023 - if ( $post && false !== strpos( $post->post_content, '<!-- wp:jetpack/subscriptions -->' ) ) {
1024 - $excerpt .= sprintf(
1025 - // translators: %s is the permalink url to the current post.
1026 - __( "<p><a href='%s'>View post</a> to subscribe to site newsletter.</p>", 'jetpack' ),
1027 - get_post_permalink()
1028 - );
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 '';
1029 957 }
1030 - 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 + );
1031 994 }
1032 995
1033 996 /**
1034 997 * Gate access to posts
@@ -1039,22 +1002,24 @@
1039 1002 */
1040 1003 function add_paywall( $the_content ) {
1041 1004 require_once JETPACK__PLUGIN_DIR . 'modules/memberships/class-jetpack-memberships.php';
1042 1005
1006 + $post_access_level = Jetpack_Memberships::get_post_access_level();
1007 +
1043 1008 if ( Jetpack_Memberships::user_can_view_post() ) {
1044 - do_action(
1045 - 'earn_track_paywalled_post_view',
1046 - array(
1047 - 'post_id' => get_the_ID(),
1048 - )
1049 - );
1009 + if ( $post_access_level !== Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_EVERYBODY ) {
1010 + do_action(
1011 + 'earn_track_paywalled_post_view',
1012 + array(
1013 + 'post_id' => get_the_ID(),
1014 + )
1015 + );
1016 + }
1050 1017 return $the_content;
1051 1018 }
1052 1019
1053 - $post_access_level = Jetpack_Memberships::get_post_access_level();
1020 + $paywalled_content = get_paywall_content();
1054 1021
1055 - $paywalled_content = get_paywall_content( $post_access_level, isset( $_GET['subscribe'] ) && 'success' === $_GET['subscribe'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
1056 -
1057 1022 if ( has_block( \Automattic\Jetpack\Extensions\Paywall\BLOCK_NAME ) ) {
1058 1023 if ( strpos( $the_content, \Automattic\Jetpack\Extensions\Paywall\BLOCK_HTML ) ) {
1059 1024 return strstr( $the_content, \Automattic\Jetpack\Extensions\Paywall\BLOCK_HTML, true ) . $paywalled_content;
1060 1025 }
@@ -1104,22 +1069,46 @@
1104 1069 return '';
1105 1070 }
1106 1071
1107 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 +/**
1108 1099 * Returns paywall content blocks
1109 1100 *
1110 - * @param string $post_access_level The newsletter access level.
1111 - * @param string $email_confirmation_pending True if the current user needs to validate their email.
1112 1101 * @return string
1113 1102 */
1114 -function get_paywall_content( $post_access_level, $email_confirmation_pending = false ) {
1115 - if ( $email_confirmation_pending ) {
1103 +function get_paywall_content() {
1104 + if ( Jetpack_Memberships::user_is_pending_subscriber() ) {
1116 1105 return get_paywall_blocks_subscribe_pending();
1117 1106 }
1118 1107 if ( doing_filter( 'get_the_excerpt' ) ) {
1119 1108 return '';
1120 1109 }
1121 - return get_paywall_blocks( $post_access_level );
1110 + return get_paywall_blocks();
1122 1111 }
1123 1112
1124 1113 /**
1125 1114 * Returns the current URL.
@@ -1139,9 +1128,9 @@
1139 1128 $host = ! empty( $_SERVER['HTTP_HOST'] ) ? wp_unslash( $_SERVER['HTTP_HOST'] ) : null;
1140 1129 $path = ! empty( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '';
1141 1130
1142 1131 // Support for local plugin development and testing using ngrok.
1143 - 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.
1144 1133 $host = wp_unslash( $_SERVER['HTTP_X_ORIGINAL_HOST'] );
1145 1134 }
1146 1135 // phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1147 1136
@@ -1153,24 +1142,70 @@
1153 1142 return $current_url;
1154 1143 }
1155 1144
1156 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 +/**
1157 1192 * Returns paywall content blocks if user is not authenticated
1158 1193 *
1159 - * @param string $newsletter_access_level The newsletter access level.
1160 1194 * @return string
1161 1195 */
1162 -function get_paywall_blocks( $newsletter_access_level ) {
1196 +function get_paywall_blocks() {
1163 1197 $custom_paywall = apply_filters( 'jetpack_custom_paywall_blocks', false );
1164 1198 if ( ! empty( $custom_paywall ) ) {
1165 1199 return $custom_paywall;
1166 1200 }
1167 - if ( ! jetpack_is_frontend() ) { // emails
1201 +
1202 + if ( ! Request::is_frontend() ) { // emails
1168 1203 return get_paywall_simple();
1169 1204 }
1205 +
1170 1206 require_once JETPACK__PLUGIN_DIR . 'modules/memberships/class-jetpack-memberships.php';
1171 - // Only display paid texts when Stripe is connected and the post is marked for paid subscribers
1172 - $is_paid_post = $newsletter_access_level === 'paid_subscribers' && Jetpack_Memberships::has_connected_account();
1207 + $is_paid_post = is_paid_post();
1173 1208 $is_paid_subscriber = Jetpack_Memberships::user_is_paid_subscriber();
1174 1209
1175 1210 $access_heading = $is_paid_subscriber
1176 1211 ? esc_html__( 'Upgrade to continue reading', 'jetpack' )
@@ -1177,32 +1212,49 @@
1177 1212 : esc_html__( 'Subscribe to continue reading', 'jetpack' );
1178 1213
1179 1214 $subscribe_text = $is_paid_post
1180 1215 // translators: %s is the name of the site.
1181 - ? $is_paid_subscriber
1182 - ? esc_html__( 'Upgrade to get access to the rest of this post and other exclusive content.', 'jetpack' )
1183 - : 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 + )
1184 1221 // translators: %s is the name of the site.
1185 1222 : esc_html__( 'Subscribe to get access to the rest of this post and other subscriber-only content.', 'jetpack' );
1186 1223
1187 - $access_question = $is_paid_post
1188 - ? __( 'Already a paid subscriber?', 'jetpack' )
1189 - : __( 'Already a subscriber?', 'jetpack' );
1224 + $login_block = '';
1190 1225
1191 - $sign_in = '';
1192 - if ( ! is_user_logged_in() && ( new Host() )->is_wpcom_simple() ) {
1193 - $sign_in_link = wpcom_logmein_redirect_url( get_current_url(), false, null, 'link' );
1194 -
1195 - $sign_in = '<!-- wp:paragraph {"align":"center","style":{"typography":{"fontSize":"14px"}}} -->
1196 -<p class="has-text-align-center" style="font-size:14px">' . esc_html( $access_question ) . ' <a href="' . $sign_in_link . '">' . esc_html__( 'Log in', 'jetpack' ) . '</a></p>
1226 + if ( is_user_auth() ) {
1227 + if ( ( new Host() )->is_wpcom_simple() ) {
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(
1230 + array(
1231 + 'redirect_to' => rawurlencode( get_current_url() ),
1232 + 'blog_id' => get_current_blog_id(),
1233 + ),
1234 + 'https://wordpress.com/log-in/link'
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>
1197 1241 <!-- /wp:paragraph -->';
1242 + }
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 -->';
1198 1250 }
1199 1251
1200 1252 $lock_svg = plugins_url( 'images/lock-paywall.svg', JETPACK__PLUGIN_FILE );
1201 1253
1202 1254 return '
1203 -<!-- 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"}} -->
1204 -<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">
1255 +<!-- 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"}} -->
1256 +<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">
1205 1257 <!-- wp:image {"align":"center","width":24,"height":24,"sizeSlug":"large","linkDestination":"none"} -->
1206 1258 <figure class="wp-block-image aligncenter size-large is-resized"><img src="' . $lock_svg . '" alt="" width="24" height="24"/></figure>
1207 1259 <!-- /wp:image -->
1208 1260
@@ -1214,9 +1266,9 @@
1214 1266 <p class="has-text-align-center" style="margin-top:10px;margin-bottom:10px;font-size:14px">' . $subscribe_text . '</p>
1215 1267 <!-- /wp:paragraph -->
1216 1268
1217 1269 <!-- wp:jetpack/subscriptions {"borderRadius":50,"borderColor":"primary","className":"is-style-compact","isPaidSubscriber":' . ( $is_paid_subscriber ? 'true' : 'false' ) . '} /-->
1218 -' . $sign_in . '
1270 +' . $login_block . '
1219 1271 </div>
1220 1272 <!-- /wp:group -->
1221 1273 ';
1222 1274 }
@@ -1221,22 +1273,82 @@
1221 1273 ';
1222 1274 }
1223 1275
1224 1276 /**
1277 + * Returns true if user is auth for subscriptions check, otherwise returns false.
1278 + *
1279 + * @return bool
1280 + */
1281 +function is_user_auth(): bool {
1282 + if ( ( new Host() )->is_wpcom_simple() && is_user_logged_in() ) {
1283 + return true;
1284 + }
1285 + if ( current_user_can( 'manage_options' ) ) {
1286 + return true;
1287 + }
1288 +
1289 + if ( is_jetpack_token_subscription_service_loaded() ) {
1290 + if ( Jetpack_Token_Subscription_Service::has_token_from_cookie() ) {
1291 + return true;
1292 + }
1293 + }
1294 + return false;
1295 +}
1296 +
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 +/**
1225 1326 * Returns paywall content blocks when email confirmation is pending
1226 1327 *
1227 1328 * @return string
1228 1329 */
1229 1330 function get_paywall_blocks_subscribe_pending() {
1230 - $access_heading = esc_html__( 'Verify your email and continue reading', 'jetpack' );
1331 + $subscribe_email = Jetpack_Memberships::get_current_user_email();
1231 1332
1232 - $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 + }
1233 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 +
1234 1346 $lock_svg = plugins_url( 'images/lock-paywall.svg', JETPACK__PLUGIN_FILE );
1235 1347
1236 1348 return '
1237 -<!-- 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"}} -->
1238 -<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">
1239 1351 <!-- wp:image {"align":"center","width":24,"height":24,"sizeSlug":"large","linkDestination":"none"} -->
1240 1352 <figure class="wp-block-image aligncenter size-large is-resized"><img src="' . $lock_svg . '" alt="" width="24" height="24"/></figure>
1241 1353 <!-- /wp:image -->
1242 1354
@@ -1252,22 +1364,37 @@
1252 1364 ';
1253 1365 }
1254 1366
1255 1367 /**
1256 - * Return content for non frontend views like emails.
1257 - *
1258 - * @return string
1368 + * Return content for non frontend views like Reader, emails.
1259 1369 */
1260 -function get_paywall_simple() {
1261 - $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' );
1262 1375
1263 - $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 + }
1264 1388
1265 1389 return '
1266 1390 <!-- wp:columns -->
1267 -<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%">
1268 1392 <!-- wp:column -->
1269 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 -->
1270 1397 <!-- wp:paragraph -->
1271 1398 <p class="has-text-align-center"
1272 1399 style="text-align: center;
1273 1400 color: #50575E;
@@ -1274,21 +1401,20 @@
1274 1401 font-weight: 400;
1275 1402 font-size: 16px;
1276 1403 font-family: \'SF Pro Text\', sans-serif;
1277 1404 line-height: 28.8px;">
1278 - ' . $access_heading . '
1405 + ' . $paywall_description . '
1279 1406 </p>
1280 1407 <!-- /wp:paragraph -->
1281 -
1282 1408 <!-- wp:buttons -->
1283 1409 <div class="wp-block-buttons" style="text-align: center;">
1284 1410 <!-- wp:button -->
1285 - <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;">
1286 1412 <a href="' . esc_url( get_post_permalink() ) . '" class="wp-block-button__link wp-element-button"
1287 1413 data-wpcom-track data-tracks-link-desc="paywall-email-click"
1288 1414 style="display: inline-block;
1289 - padding: 15px 20px;
1290 - background-color: #0675C4;
1415 + padding: 12px 15px;
1416 + background-color: #3858e9;
1291 1417 color: #FFFFFF;
1292 1418 text-decoration: none;
1293 1419 border-radius: 5px;
1294 1420 font-family: \'SF Pro Display\', sans-serif;
@@ -1293,9 +1419,9 @@
1293 1419 border-radius: 5px;
1294 1420 font-family: \'SF Pro Display\', sans-serif;
1295 1421 font-weight: 500;
1296 1422 font-size: 16px;
1297 - text-align: center;">' . $subscribe_text . '</a>
1423 + text-align: center;">' . $paywall_action_btn . '</a>
1298 1424 </div>
1299 1425 <!-- /wp:button -->
1300 1426 </div>
1301 1427 <!-- /wp:buttons -->