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 +443 -265 13.2.4 → 16.3-a.7 View file →
@@ -7,18 +7,21 @@
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 11 use Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Abstract_Token_Subscription_Service;
13 12 use Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Jetpack_Token_Subscription_Service;
14 -use Automattic\Jetpack\Status;
13 +use Automattic\Jetpack\Modules;
15 14 use Automattic\Jetpack\Status\Host;
16 -use Jetpack;
15 +use Automattic\Jetpack\Status\Request;
17 16 use Jetpack_Gutenberg;
18 17 use Jetpack_Memberships;
19 18 use Jetpack_Subscriptions_Widget;
20 19
20 +if ( ! defined( 'ABSPATH' ) ) {
21 + exit( 0 );
22 +}
23 +
21 24 require_once __DIR__ . '/class-jetpack-subscription-site.php';
22 25 require_once __DIR__ . '/constants.php';
23 26 require_once JETPACK__PLUGIN_DIR . 'extensions/blocks/premium-content/_inc/subscription-service/include.php';
24 27
@@ -29,8 +32,9 @@
29 32 const DEFAULT_BORDER_WEIGHT_VALUE = 1;
30 33 const DEFAULT_FONTSIZE_VALUE = '16px';
31 34 const DEFAULT_PADDING_VALUE = 15;
32 35 const DEFAULT_SPACING_VALUE = 10;
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 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 )
@@ -94,14 +114,13 @@
94 114 );
95 115
96 116 register_post_meta(
97 117 'post',
98 - META_NAME_FOR_POST_DONT_EMAIL_TO_SUBS,
118 + META_NAME_FOR_POST_TIER_ID_SETTINGS,
99 119 array(
100 - 'default' => false,
101 120 'show_in_rest' => true,
102 121 'single' => true,
103 - 'type' => 'boolean',
122 + 'type' => 'integer',
104 123 'auth_callback' => function () {
105 124 return wp_get_current_user()->has_cap( 'edit_posts' );
106 125 },
107 126 )
@@ -108,13 +127,13 @@
108 127 );
109 128
110 129 register_post_meta(
111 130 'post',
112 - META_NAME_FOR_POST_TIER_ID_SETTINGS,
131 + META_NAME_CONTAINS_PAYWALLED_CONTENT,
113 132 array(
114 133 'show_in_rest' => true,
115 134 'single' => true,
116 - 'type' => 'integer',
135 + 'type' => 'boolean',
117 136 'auth_callback' => function () {
118 137 return wp_get_current_user()->has_cap( 'edit_posts' );
119 138 },
120 139 )
@@ -128,8 +147,11 @@
128 147 $allowed_meta,
129 148 array(
130 149 META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS,
131 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,
132 154 )
133 155 );
134 156 }
135 157 );
@@ -143,14 +165,11 @@
143 165
144 166 // Hide existing comments
145 167 add_filter( 'get_comment', __NAMESPACE__ . '\maybe_gate_existing_comments' );
146 168
147 - // Gate the excerpt for a post
148 - add_filter( 'get_the_excerpt', __NAMESPACE__ . '\jetpack_filter_excerpt_for_newsletter', 10, 2 );
149 -
150 169 // Add a 'Newsletter' column to the Edit posts page
151 170 // We only display the "Newsletter" column if we have configured the paid newsletter plan
152 - if ( Jetpack_Memberships::has_configured_plans_jetpack_recurring_payments( 'newsletter' ) ) {
171 + if ( defined( 'WP_ADMIN' ) && WP_ADMIN && Jetpack_Memberships::has_configured_plans_jetpack_recurring_payments( 'newsletter' ) ) {
153 172 add_action( 'manage_post_posts_columns', __NAMESPACE__ . '\register_newsletter_access_column' );
154 173 add_action( 'manage_post_posts_custom_column', __NAMESPACE__ . '\render_newsletter_access_rows', 10, 2 );
155 174 add_action( 'admin_head', __NAMESPACE__ . '\newsletter_access_column_styles' );
156 175 }
@@ -156,9 +175,31 @@
156 175 }
157 176
158 177 add_action( 'init', __NAMESPACE__ . '\maybe_prevent_super_cache_caching' );
159 178
160 - Jetpack_Subscription_Site::init()->handle_subscribe_block_placements();
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 + }
161 202 }
162 203 add_action( 'init', __NAMESPACE__ . '\register_block', 9 );
163 204
164 205 /**
@@ -186,8 +227,38 @@
186 227 );
187 228 }
188 229
189 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 +/**
190 261 * Displays the newsletter access level.
191 262 *
192 263 * @param string $column_id The ID of the column to display.
193 264 * @param int $post_id The current post ID.
@@ -196,9 +267,9 @@
196 267 if ( NEWSLETTER_COLUMN_ID !== $column_id ) {
197 268 return;
198 269 }
199 270
200 - $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 );
201 272
202 273 switch ( $access_level ) {
203 274 case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS_ALL_TIERS:
204 275 echo esc_html__( 'Paid Subscribers (all plans)', 'jetpack' );
@@ -224,11 +295,11 @@
224 295 echo '<style id="jetpack-newsletter-newsletter-access-column"> table.fixed .column-newsletter_access { width: 10%; } </style>';
225 296 }
226 297
227 298 /**
228 - * Determine the amount of folks currently subscribed to the blog, splitted out in email_subscribers & social_followers & paid_subscribers
299 + * Determine the amount of folks currently subscribed to the blog, splitted out in total_subscribers, email_subscribers, social_followers & paid_subscribers.
229 300 *
230 - * @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]]
231 302 */
232 303 function fetch_subscriber_counts() {
233 304 $subs_count = 0;
234 305 if ( is_wpcom() ) {
@@ -246,9 +317,10 @@
246 317 $subs_count = array(
247 318 'status' => 'failed',
248 319 'code' => $xml->getErrorCode(),
249 320 'message' => $xml->getErrorMessage(),
250 - 'value' => ( isset( $subs_count['value'] ) ) ? $subs_count['value'] : array(
321 + 'value' => $subs_count['value'] ?? array(
322 + 'total_subscribers' => 0,
251 323 'email_subscribers' => 0,
252 324 'social_followers' => 0,
253 325 'paid_subscribers' => 0,
254 326 ),
@@ -274,11 +346,11 @@
274 346 function get_subscriber_count( $include_social_followers ) {
275 347 $counts = fetch_subscriber_counts();
276 348
277 349 if ( $include_social_followers ) {
278 - $subscriber_count = $counts['value']['email_subscribers'] + $counts['value']['social_followers'];
350 + $subscriber_count = $counts['value']['total_subscribers'] + $counts['value']['social_followers'];
279 351 } else {
280 - $subscriber_count = $counts['value']['email_subscribers'];
352 + $subscriber_count = $counts['value']['total_subscribers'];
281 353 }
282 354 return $subscriber_count;
283 355 }
284 356
@@ -381,8 +453,24 @@
381 453 );
382 454 }
383 455
384 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 +/**
385 473 * Uses block attributes to generate an array containing the styles for various block elements.
386 474 * Based on Jetpack_Subscriptions_Widget::do_subscription_form() which the block was originally using.
387 475 *
388 476 * @param array $attributes Array containing the block attributes.
@@ -389,8 +477,10 @@
389 477 *
390 478 * @return array
391 479 */
392 480 function get_element_styles_from_attributes( $attributes ) {
481 + $is_button_only_style = is_button_only_style( get_attribute( $attributes, 'className', '' ) );
482 +
393 483 $button_background_style = ! has_attribute( $attributes, 'buttonBackgroundColor' ) && has_attribute( $attributes, 'customButtonGradient' )
394 484 ? get_attribute( $attributes, 'customButtonGradient' )
395 485 : get_attribute( $attributes, 'customButtonBackgroundColor' );
396 486
@@ -406,15 +496,15 @@
406 496 $submit_button_styles .= sprintf( 'color: %s;', get_attribute( $attributes, 'customTextColor' ) );
407 497 }
408 498
409 499 if ( has_attribute( $attributes, 'buttonWidth' ) ) {
410 - $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 ) );
411 501 $submit_button_wrapper_styles .= 'max-width: 100%;';
412 502
413 503 // Account for custom margins on inline forms.
414 504 $submit_button_styles .= true === get_attribute( $attributes, 'buttonOnNewLine' )
415 - ? sprintf( 'width: calc(100%% - %dpx);', get_attribute( $attributes, 'spacing', DEFAULT_SPACING_VALUE ) )
416 - : 'width: 100%;';
505 + ? 'width: 100%;'
506 + : sprintf( 'width: calc(100%% - %dpx);', get_attribute( $attributes, 'spacing', DEFAULT_SPACING_VALUE ) );
417 507 }
418 508
419 509 $font_size = get_attribute( $attributes, 'customFontSize', DEFAULT_FONTSIZE_VALUE );
420 510 $style = sprintf( 'font-size: %s%s;', $font_size, is_numeric( $font_size ) ? 'px' : '' );
@@ -427,14 +517,16 @@
427 517
428 518 $submit_button_styles .= $style;
429 519 $email_field_styles .= $style;
430 520
431 - $button_spacing = get_attribute( $attributes, 'spacing', DEFAULT_SPACING_VALUE );
432 - if ( true === get_attribute( $attributes, 'buttonOnNewLine' ) ) {
433 - $submit_button_styles .= sprintf( 'margin-top: %dpx;', $button_spacing );
434 - } else {
435 - $submit_button_styles .= 'margin: 0px; '; // Reset Safari's 2px default margin for buttons affecting input and button union
436 - $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 + }
437 529 }
438 530
439 531 if ( has_attribute( $attributes, 'borderColor' ) ) {
440 532 $style = sprintf( 'border-color: %s;', get_attribute( $attributes, 'borderColor', '' ) );
@@ -456,9 +548,9 @@
456 548 $submit_button_styles .= $style;
457 549 $email_field_styles .= $style;
458 550 }
459 551
460 - if ( ! jetpack_is_frontend() ) {
552 + if ( ! Request::is_frontend() ) {
461 553 $background_color_style = get_attribute_color( 'buttonBackgroundColor', $attributes, '#113AF5' /* default lettre theme color */ );
462 554 $text_color_style = get_attribute_color( 'textColor', $attributes, '#FFFFFF' );
463 555 $submit_button_styles .= sprintf( ' background-color: %s; color: %s;', $background_color_style, $text_color_style );
464 556 }
@@ -543,8 +635,15 @@
543 635 return null;
544 636 }
545 637
546 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 +/**
547 646 * Subscriptions block render callback.
548 647 *
549 648 * @param array $attributes Array containing the block attributes.
550 649 *
@@ -551,13 +650,13 @@
551 650 * @return string
552 651 */
553 652 function render_block( $attributes ) {
554 653 // If the Subscriptions module is not active, don't render the block.
555 - if ( ! Jetpack::is_module_active( 'subscriptions' ) ) {
654 + if ( ! ( new Modules() )->is_active( 'subscriptions' ) ) {
556 655 return '';
557 656 }
558 657
559 - if ( class_exists( '\Jetpack_Memberships' ) ) {
658 + if ( is_jetpack_memberships_loaded() ) {
560 659 // We only want the sites that have newsletter feature enabled to be graced by this JavaScript.
561 660 Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
562 661 } else {
563 662 Jetpack_Gutenberg::load_styles_as_required( FEATURE_NAME );
@@ -562,14 +661,21 @@
562 661 } else {
563 662 Jetpack_Gutenberg::load_styles_as_required( FEATURE_NAME );
564 663 }
565 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
566 670 $subscribe_email = Jetpack_Memberships::get_current_user_email();
567 671
568 - /** This filter is documented in modules/contact-form/grunion-contact-form.php */
569 - if ( is_wpcom() || false !== apply_filters( 'jetpack_auto_fill_logged_in_user', false ) ) {
570 - $current_user = wp_get_current_user();
571 - $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 + }
572 678 }
573 679
574 680 // The block is using the Jetpack_Subscriptions_Widget backend, hence the need to increase the instance count.
575 681 ++Jetpack_Subscriptions_Widget::$instance_count;
@@ -584,36 +690,44 @@
584 690 // We want to disencourage including social count as it's misleading.
585 691 $include_social_followers = isset( $attributes['includeSocialFollowers'] ) ? (bool) get_attribute( $attributes, 'includeSocialFollowers' ) : true;
586 692
587 693 $data = array(
588 - 'widget_id' => Jetpack_Subscriptions_Widget::$instance_count,
589 - 'subscribe_email' => $subscribe_email,
590 - 'is_paid_subscriber' => get_attribute( $attributes, 'isPaidSubscriber', false ),
591 - '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(
592 698 array(
593 699 'class' => $classes['block_wrapper'],
594 700 )
595 701 ),
596 - 'subscribe_placeholder' => get_attribute( $attributes, 'subscribePlaceholder', __( 'Type your email…', 'jetpack' ) ),
597 - 'submit_button_text' => get_attribute( $attributes, 'submitButtonText', __( 'Subscribe', 'jetpack' ) ),
598 - 'submit_button_text_subscribed' => get_attribute( $attributes, 'submitButtonTextSubscribed', __( 'Subscribed', 'jetpack' ) ),
599 - 'submit_button_text_upgrade' => get_attribute( $attributes, 'submitButtonTextUpgrade', __( 'Upgrade subscription', 'jetpack' ) ),
600 - '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(
601 707 $attributes,
602 708 'successMessage',
603 - 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' )
604 710 ),
605 - 'show_subscribers_total' => (bool) get_attribute( $attributes, 'showSubscribersTotal' ),
606 - 'subscribers_total' => get_subscriber_count( $include_social_followers ),
607 - '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(
608 714 ( is_ssl() ? 'https' : 'http' ) . '://' . ( isset( $_SERVER['HTTP_HOST'] ) ? wp_unslash( $_SERVER['HTTP_HOST'] ) : '' ) .
609 715 ( isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '' )
610 716 ),
611 - 'source' => 'subscribe-block',
612 - 'app_source' => get_attribute( $attributes, 'appSource', null ),
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 ),
613 722 );
614 723
615 - 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() ) {
616 730 return render_for_email( $data, $styles );
617 731 }
618 732
619 733 return render_for_website( $data, $classes, $styles );
@@ -642,14 +756,15 @@
642 756 *
643 757 * @return string
644 758 */
645 759 function render_for_website( $data, $classes, $styles ) {
646 - $lang = get_locale();
647 - $blog_id = \Jetpack_Options::get_option( 'id' );
648 - $widget_id_suffix = Jetpack_Subscriptions_Widget::$instance_count > 1 ? '-' . Jetpack_Subscriptions_Widget::$instance_count : '';
649 - $form_id = 'subscribe-blog' . $widget_id_suffix;
650 - $form_url = 'https://wordpress.com/email-subscriptions';
651 - $post_access_level = get_post_access_level_for_current_post();
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;
652 767
653 768 // Post ID is used for pulling post-specific paid status, and returning to the right post after confirming subscription
654 769 $post_id = null;
655 770 if ( in_the_loop() ) {
@@ -659,12 +774,13 @@
659 774 } else {
660 775 $post_id = get_option( 'page_on_front' );
661 776 }
662 777
663 - $subscribe_field_id = apply_filters( 'subscribe_field_id', 'subscribe-field' . $widget_id_suffix, $data['widget_id'] );
664 - $tier_id = get_post_meta( $post_id, META_NAME_FOR_POST_TIER_ID_SETTINGS, true );
665 - $is_subscribed = Jetpack_Memberships::is_current_user_subscribed();
666 - $button_text = get_submit_button_text( $data );
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;
667 783
668 784 ob_start();
669 785
670 786 Jetpack_Subscriptions_Widget::render_widget_status_messages(
@@ -674,83 +790,16 @@
674 790 );
675 791 ?>
676 792 <div <?php echo wp_kses_data( $data['wrapper_attributes'] ); ?>>
677 793 <div class="wp-block-jetpack-subscriptions__container<?php echo ! $is_subscribed ? ' is-not-subscriber' : ''; ?>">
678 - <form
679 - action="<?php echo esc_url( $form_url ); ?>"
680 - method="post"
681 - accept-charset="utf-8"
682 - data-blog="<?php echo esc_attr( $blog_id ); ?>"
683 - data-post_access_level="<?php echo esc_attr( $post_access_level ); ?>"
684 - data-subscriber_email="<?php echo esc_attr( $data['subscribe_email'] ); ?>"
685 - id="<?php echo esc_attr( $form_id ); ?>"
686 - >
687 - <div class="wp-block-jetpack-subscriptions__form-elements">
688 - <?php if ( ! $is_subscribed ) : ?>
689 - <p id="subscribe-email">
690 - <label
691 - id="<?php echo esc_attr( $subscribe_field_id . '-label' ); ?>"
692 - for="<?php echo esc_attr( $subscribe_field_id ); ?>"
693 - class="screen-reader-text"
694 - >
695 - <?php echo esc_html( $data['subscribe_placeholder'] ); ?>
696 - </label>
697 - <?php
698 - printf(
699 - '<input
700 - required="required"
701 - type="email"
702 - name="email"
703 - %1$s
704 - style="%2$s"
705 - placeholder="%3$s"
706 - value="%4$s"
707 - id="%5$s"
708 - %6$s
709 - />',
710 - ( ! empty( $classes['email_field'] )
711 - ? 'class="' . esc_attr( $classes['email_field'] ) . '"'
712 - : ''
713 - ),
714 - ( ! empty( $styles['email_field'] )
715 - ? esc_attr( $styles['email_field'] )
716 - : 'width: 95%; padding: 1px 10px'
717 - ),
718 - esc_attr( $data['subscribe_placeholder'] ),
719 - esc_attr( $data['subscribe_email'] ),
720 - esc_attr( $subscribe_field_id ),
721 - ( ! empty( $data['subscribe_email'] )
722 - ? 'disabled title="' . esc_attr__( "You're logged in with this email", 'jetpack' ) . '"'
723 - : ''
724 - )
725 - );
726 - ?>
727 - </p>
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'] ); ?>"
728 798 <?php endif; ?>
729 - <p id="subscribe-submit"
730 - <?php if ( ! empty( $styles['submit_button_wrapper'] ) ) : ?>
731 - style="<?php echo esc_attr( $styles['submit_button_wrapper'] ); ?>"
732 - <?php endif; ?>
733 - >
734 - <input type="hidden" name="action" value="<?php echo is_top_subscription() ? 'subscribed' : 'subscribe'; ?>"/>
735 - <input type="hidden" name="blog_id" value="<?php echo (int) $blog_id; ?>"/>
736 - <input type="hidden" name="source" value="<?php echo esc_url( $data['referer'] ); ?>"/>
737 - <input type="hidden" name="sub-type" value="<?php echo esc_attr( $data['source'] ); ?>"/>
738 - <input type="hidden" name="app_source" value="<?php echo esc_attr( $data['app_source'] ); ?>"/>
739 - <input type="hidden" name="redirect_fragment" value="<?php echo esc_attr( $form_id ); ?>"/>
740 - <input type="hidden" name="lang" value="<?php echo esc_attr( $lang ); ?>"/>
741 - <?php
742 - wp_nonce_field( 'blogsub_subscribe_' . $blog_id );
743 -
744 - if ( ! empty( $post_id ) ) {
745 - echo '<input type="hidden" name="post_id" value="' . esc_attr( $post_id ) . '"/>';
746 - }
747 -
748 - if ( ! empty( $tier_id ) ) {
749 - echo '<input type="hidden" name="tier_id" value="' . esc_attr( $tier_id ) . '"/>';
750 - }
751 - ?>
752 - <button type="submit"
799 + >
800 + <a
801 + href="<?php echo esc_url( 'https://wordpress.com/reader/site/subscription/' . $blog_id ); ?>"
753 802 <?php if ( ! empty( $classes['submit_button'] ) ) : ?>
754 803 class="<?php echo esc_attr( $classes['submit_button'] ); ?>"
755 804 <?php endif; ?>
756 805 <?php if ( ! empty( $styles['submit_button'] ) ) : ?>
@@ -755,20 +804,110 @@
755 804 <?php endif; ?>
756 805 <?php if ( ! empty( $styles['submit_button'] ) ) : ?>
757 806 style="<?php echo esc_attr( $styles['submit_button'] ); ?>"
758 807 <?php endif; ?>
759 - name="jetpack_subscriptions_widget"
760 808 >
761 809 <?php echo sanitize_submit_text( $button_text ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
762 - </button>
763 - </p>
764 - </div>
765 - </form>
766 - <?php if ( $data['show_subscribers_total'] && $data['subscribers_total'] ) : ?>
810 + </a>
811 + </p>
812 + <?php else : ?>
813 + <form
814 + action="<?php echo esc_url( $form_url ); ?>"
815 + method="post"
816 + accept-charset="utf-8"
817 + data-blog="<?php echo esc_attr( $blog_id ); ?>"
818 + data-post_access_level="<?php echo esc_attr( $post_access_level ); ?>"
819 + data-subscriber_email="<?php echo esc_attr( $data['subscribe_email'] ); ?>"
820 + id="<?php echo esc_attr( $form_id ); ?>"
821 + >
822 + <div class="wp-block-jetpack-subscriptions__form-elements">
823 + <?php if ( ! $is_subscribed && ! $is_button_only_style ) : ?>
824 + <p id="subscribe-email">
825 + <label
826 + id="<?php echo esc_attr( $subscribe_field_id . '-label' ); ?>"
827 + for="<?php echo esc_attr( $subscribe_field_id ); ?>"
828 + class="screen-reader-text"
829 + >
830 + <?php echo esc_html( $data['subscribe_placeholder'] ); ?>
831 + </label>
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 + ?>
863 + </p>
864 + <?php endif; ?>
865 + <p id="subscribe-submit"
866 + <?php if ( ! empty( $styles['submit_button_wrapper'] ) ) : ?>
867 + style="<?php echo esc_attr( $styles['submit_button_wrapper'] ); ?>"
868 + <?php endif; ?>
869 + >
870 + <input type="hidden" name="action" value="subscribe"/>
871 + <input type="hidden" name="blog_id" value="<?php echo (int) $blog_id; ?>"/>
872 + <input type="hidden" name="source" value="<?php echo esc_url( $data['referer'] ); ?>"/>
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'] ); ?>"/>
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 ); ?>"/>
877 + <?php
878 + wp_nonce_field( 'blogsub_subscribe_' . $blog_id );
879 +
880 + if ( ! empty( $post_id ) ) {
881 + echo '<input type="hidden" name="post_id" value="' . esc_attr( $post_id ) . '"/>';
882 + }
883 +
884 + if ( ! empty( $tier_id ) ) {
885 + echo '<input type="hidden" name="tier_id" value="' . esc_attr( $tier_id ) . '"/>';
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 + }
891 + ?>
892 + <button type="submit"
893 + <?php if ( ! empty( $classes['submit_button'] ) ) : ?>
894 + class="<?php echo esc_attr( $classes['submit_button'] ); ?>"
895 + <?php endif; ?>
896 + <?php if ( ! empty( $styles['submit_button'] ) ) : ?>
897 + style="<?php echo esc_attr( $styles['submit_button'] ); ?>"
898 + <?php endif; ?>
899 + name="jetpack_subscriptions_widget"
900 + >
901 + <?php echo sanitize_submit_text( $button_text ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
902 + </button>
903 + </p>
904 + </div>
905 + </form>
906 + <?php endif; ?>
907 + <?php if ( $show_subscriber_count ) : ?>
767 908 <div class="wp-block-jetpack-subscriptions__subscount">
768 - <?php
769 - echo esc_html( Jetpack_Memberships::get_join_others_text( $data['subscribers_total'] ) );
770 - ?>
909 + <?php echo esc_html( Jetpack_Memberships::get_join_others_text( $data['subscribers_total'] ) ); ?>
771 910 </div>
772 911 <?php endif; ?>
773 912 </div>
774 913 </div>
@@ -803,26 +942,56 @@
803 942 return $html;
804 943 }
805 944
806 945 /**
807 - * Filter excerpts looking for subscription data.
946 + * WooCommerce Email Editor render callback for the subscriptions block.
808 947 *
809 - * @param string $excerpt The extrapolated excerpt string.
810 - * @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.
811 951 *
812 - * @return mixed
952 + * @return string
813 953 */
814 -function jetpack_filter_excerpt_for_newsletter( $excerpt, $post = null ) {
815 - // The blogmagazine theme is overriding WP core `get_the_excerpt` filter and only passing the excerpt
816 - // TODO: Until this is fixed, return the excerpt without gating. See https://github.com/Automattic/jetpack/pull/28102#issuecomment-1369161116
817 - if ( $post && str_contains( $post->post_content, '<!-- wp:jetpack/subscriptions -->' ) ) {
818 - $excerpt .= sprintf(
819 - // translators: %s is the permalink url to the current post.
820 - __( "<p><a href='%s'>View post</a> to subscribe to site newsletter.</p>", 'jetpack' ),
821 - get_post_permalink()
822 - );
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 '';
823 957 }
824 - 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 + );
825 994 }
826 995
827 996 /**
828 997 * Gate access to posts
@@ -847,9 +1016,9 @@
847 1016 }
848 1017 return $the_content;
849 1018 }
850 1019
851 - $paywalled_content = get_paywall_content( $post_access_level ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
1020 + $paywalled_content = get_paywall_content();
852 1021
853 1022 if ( has_block( \Automattic\Jetpack\Extensions\Paywall\BLOCK_NAME ) ) {
854 1023 if ( strpos( $the_content, \Automattic\Jetpack\Extensions\Paywall\BLOCK_HTML ) ) {
855 1024 return strstr( $the_content, \Automattic\Jetpack\Extensions\Paywall\BLOCK_HTML, true ) . $paywalled_content;
@@ -928,12 +1097,11 @@
928 1097
929 1098 /**
930 1099 * Returns paywall content blocks
931 1100 *
932 - * @param string $post_access_level The newsletter access level.
933 1101 * @return string
934 1102 */
935 -function get_paywall_content( $post_access_level ) {
1103 +function get_paywall_content() {
936 1104 if ( Jetpack_Memberships::user_is_pending_subscriber() ) {
937 1105 return get_paywall_blocks_subscribe_pending();
938 1106 }
939 1107 if ( doing_filter( 'get_the_excerpt' ) ) {
@@ -938,9 +1106,9 @@
938 1106 }
939 1107 if ( doing_filter( 'get_the_excerpt' ) ) {
940 1108 return '';
941 1109 }
942 - return get_paywall_blocks( $post_access_level );
1110 + return get_paywall_blocks();
943 1111 }
944 1112
945 1113 /**
946 1114 * Returns the current URL.
@@ -1022,22 +1190,22 @@
1022 1190
1023 1191 /**
1024 1192 * Returns paywall content blocks if user is not authenticated
1025 1193 *
1026 - * @param string $newsletter_access_level The newsletter access level.
1027 1194 * @return string
1028 1195 */
1029 -function get_paywall_blocks( $newsletter_access_level ) {
1196 +function get_paywall_blocks() {
1030 1197 $custom_paywall = apply_filters( 'jetpack_custom_paywall_blocks', false );
1031 1198 if ( ! empty( $custom_paywall ) ) {
1032 1199 return $custom_paywall;
1033 1200 }
1034 - if ( ! jetpack_is_frontend() ) { // emails
1201 +
1202 + if ( ! Request::is_frontend() ) { // emails
1035 1203 return get_paywall_simple();
1036 1204 }
1205 +
1037 1206 require_once JETPACK__PLUGIN_DIR . 'modules/memberships/class-jetpack-memberships.php';
1038 - // Only display paid texts when Stripe is connected and the post is marked for paid subscribers
1039 - $is_paid_post = $newsletter_access_level === 'paid_subscribers' && Jetpack_Memberships::has_connected_account();
1207 + $is_paid_post = is_paid_post();
1040 1208 $is_paid_subscriber = Jetpack_Memberships::user_is_paid_subscriber();
1041 1209
1042 1210 $access_heading = $is_paid_subscriber
1043 1211 ? esc_html__( 'Upgrade to continue reading', 'jetpack' )
@@ -1044,50 +1212,42 @@
1044 1212 : esc_html__( 'Subscribe to continue reading', 'jetpack' );
1045 1213
1046 1214 $subscribe_text = $is_paid_post
1047 1215 // translators: %s is the name of the site.
1048 - ? $is_paid_subscriber
1049 - ? esc_html__( 'Upgrade to get access to the rest of this post and other exclusive content.', 'jetpack' )
1050 - : 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 + )
1051 1221 // translators: %s is the name of the site.
1052 1222 : esc_html__( 'Subscribe to get access to the rest of this post and other subscriber-only content.', 'jetpack' );
1053 1223
1054 - $sign_in = '';
1055 - $switch_accounts = '';
1224 + $login_block = '';
1056 1225
1057 - if ( ( new Host() )->is_wpcom_simple() ) {
1058 - // On WPCOM we will redirect directly to the current page
1059 - $redirect_url = get_current_url();
1060 - } else {
1061 - // On self-hosted we will save and hide the token
1062 - $redirect_url = get_site_url() . '/wp-json/jetpack/v4/subscribers/auth';
1063 - $redirect_url = add_query_arg( 'redirect_url', get_current_url(), $redirect_url );
1064 - }
1065 -
1066 - $sign_in_link = add_query_arg(
1067 - array(
1068 - 'site_id' => intval( \Jetpack_Options::get_option( 'id' ) ),
1069 - 'redirect_url' => rawurlencode( $redirect_url ),
1070 - ),
1071 - 'https://subscribe.wordpress.com/memberships/jwt/'
1072 - );
1073 1226 if ( is_user_auth() ) {
1074 1227 if ( ( new Host() )->is_wpcom_simple() ) {
1075 - $switch_accounts_link = wp_logout_url( $sign_in_link );
1076 - $switch_accounts = '<!-- wp:paragraph {"align":"center","style":{"typography":{"fontSize":"14px"}}} -->
1077 -<p class="has-text-align-center" style="font-size:14px"><a href="' . $switch_accounts_link . '">' . __( 'Switch Accounts', 'jetpack' ) . '</a></p>
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>
1078 1241 <!-- /wp:paragraph -->';
1079 -
1080 1242 }
1081 1243 } else {
1082 - if ( ( new Host() )->is_wpcom_simple() ) {
1083 - // custom domain
1084 - $sign_in_link = wpcom_logmein_redirect_url( get_current_url(), false, null, 'link', get_current_blog_id() );
1085 - }
1086 - $access_question = get_paywall_access_question( $newsletter_access_level );
1087 - $sign_in = '<!-- wp:paragraph {"align":"center","style":{"typography":{"fontSize":"14px"}}} -->
1088 -<p class="has-text-align-center" style="font-size:14px"><a href="' . $sign_in_link . '">' . $access_question . '</a></p>
1089 -<!-- /wp:paragraph -->';
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 -->';
1090 1250 }
1091 1251
1092 1252 $lock_svg = plugins_url( 'images/lock-paywall.svg', JETPACK__PLUGIN_FILE );
1093 1253
@@ -1106,10 +1266,9 @@
1106 1266 <p class="has-text-align-center" style="margin-top:10px;margin-bottom:10px;font-size:14px">' . $subscribe_text . '</p>
1107 1267 <!-- /wp:paragraph -->
1108 1268
1109 1269 <!-- wp:jetpack/subscriptions {"borderRadius":50,"borderColor":"primary","className":"is-style-compact","isPaidSubscriber":' . ( $is_paid_subscriber ? 'true' : 'false' ) . '} /-->
1110 -' . $sign_in . '
1111 -' . $switch_accounts . '
1270 +' . $login_block . '
1112 1271 </div>
1113 1272 <!-- /wp:group -->
1114 1273 ';
1115 1274 }
@@ -1114,33 +1273,8 @@
1114 1273 ';
1115 1274 }
1116 1275
1117 1276 /**
1118 - * Returns Get Access question for the paywall
1119 - *
1120 - * @param string $post_access_level The newsletter access level.
1121 - * @return string.
1122 - */
1123 -function get_paywall_access_question( $post_access_level ) {
1124 - switch ( $post_access_level ) {
1125 - case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS:
1126 - case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS_ALL_TIERS:
1127 - $tier = Jetpack_Memberships::get_post_tier();
1128 - if ( $tier !== null ) {
1129 - return sprintf(
1130 - // translators: Placeholder is the tier name
1131 - __( 'Already a higher-tier paid subscriber?', 'jetpack' ),
1132 - esc_html( $tier->post_title )
1133 - );
1134 - } else {
1135 - return esc_html__( 'Already a paid subscriber?', 'jetpack' );
1136 - }
1137 - default:
1138 - return esc_html__( 'Already a subscriber?', 'jetpack' );
1139 - }
1140 -}
1141 -
1142 -/**
1143 1277 * Returns true if user is auth for subscriptions check, otherwise returns false.
1144 1278 *
1145 1279 * @return bool
1146 1280 */
@@ -1160,8 +1294,36 @@
1160 1294 return false;
1161 1295 }
1162 1296
1163 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 +/**
1164 1326 * Returns paywall content blocks when email confirmation is pending
1165 1327 *
1166 1328 * @return string
1167 1329 */
@@ -1167,12 +1329,14 @@
1167 1329 */
1168 1330 function get_paywall_blocks_subscribe_pending() {
1169 1331 $subscribe_email = Jetpack_Memberships::get_current_user_email();
1170 1332
1171 - /** This filter is documented in modules/contact-form/grunion-contact-form.php */
1333 + /** This filter is documented in \Automattic\Jetpack\Forms\ContactForm\Contact_Form */
1172 1334 if ( is_wpcom() || false !== apply_filters( 'jetpack_auto_fill_logged_in_user', false ) ) {
1173 - $current_user = wp_get_current_user();
1174 - $subscribe_email = ! empty( $current_user->user_email ) ? $current_user->user_email : '';
1335 + $current_user = wp_get_current_user();
1336 + if ( ! empty( $current_user->user_email ) ) {
1337 + $subscribe_email = $current_user->user_email;
1338 + }
1175 1339 }
1176 1340
1177 1341 $access_heading = esc_html__( 'Confirm your subscription to continue reading', 'jetpack' );
1178 1342
@@ -1200,22 +1364,37 @@
1200 1364 ';
1201 1365 }
1202 1366
1203 1367 /**
1204 - * Return content for non frontend views like emails.
1205 - *
1206 - * @return string
1368 + * Return content for non frontend views like Reader, emails.
1207 1369 */
1208 -function get_paywall_simple() {
1209 - $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' );
1210 1375
1211 - $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 + }
1212 1388
1213 1389 return '
1214 1390 <!-- wp:columns -->
1215 -<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%">
1216 1392 <!-- wp:column -->
1217 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 -->
1218 1397 <!-- wp:paragraph -->
1219 1398 <p class="has-text-align-center"
1220 1399 style="text-align: center;
1221 1400 color: #50575E;
@@ -1222,21 +1401,20 @@
1222 1401 font-weight: 400;
1223 1402 font-size: 16px;
1224 1403 font-family: \'SF Pro Text\', sans-serif;
1225 1404 line-height: 28.8px;">
1226 - ' . $access_heading . '
1405 + ' . $paywall_description . '
1227 1406 </p>
1228 1407 <!-- /wp:paragraph -->
1229 -
1230 1408 <!-- wp:buttons -->
1231 1409 <div class="wp-block-buttons" style="text-align: center;">
1232 1410 <!-- wp:button -->
1233 - <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;">
1234 1412 <a href="' . esc_url( get_post_permalink() ) . '" class="wp-block-button__link wp-element-button"
1235 1413 data-wpcom-track data-tracks-link-desc="paywall-email-click"
1236 1414 style="display: inline-block;
1237 - padding: 15px 20px;
1238 - background-color: #0675C4;
1415 + padding: 12px 15px;
1416 + background-color: #3858e9;
1239 1417 color: #FFFFFF;
1240 1418 text-decoration: none;
1241 1419 border-radius: 5px;
1242 1420 font-family: \'SF Pro Display\', sans-serif;
@@ -1241,9 +1419,9 @@
1241 1419 border-radius: 5px;
1242 1420 font-family: \'SF Pro Display\', sans-serif;
1243 1421 font-weight: 500;
1244 1422 font-size: 16px;
1245 - text-align: center;">' . $subscribe_text . '</a>
1423 + text-align: center;">' . $paywall_action_btn . '</a>
1246 1424 </div>
1247 1425 <!-- /wp:button -->
1248 1426 </div>
1249 1427 <!-- /wp:buttons -->