PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.5
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.5
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 +260 -146 13.4.5 → 16.3-a.5 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,42 +74,37 @@
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 - }
74 -
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 - }
82 -
83 78 register_post_meta(
84 79 'post',
85 80 META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS,
86 81 array(
87 - 'show_in_rest' => true,
88 - 'single' => true,
89 - 'type' => 'string',
90 - 'auth_callback' => function () {
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 () {
91 94 return wp_get_current_user()->has_cap( 'edit_posts' );
92 95 },
93 96 )
94 97 );
95 98
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 );
101 +
96 102 register_post_meta(
97 103 'post',
98 104 META_NAME_FOR_POST_DONT_EMAIL_TO_SUBS,
99 105 array(
100 - 'default' => false,
106 + 'default' => $dont_email_default,
101 107 'show_in_rest' => true,
102 108 'single' => true,
103 109 'type' => 'boolean',
104 110 'auth_callback' => function () {
@@ -142,8 +148,10 @@
142 148 array(
143 149 META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS,
144 150 META_NAME_FOR_POST_DONT_EMAIL_TO_SUBS,
145 151 META_NAME_CONTAINS_PAYWALLED_CONTENT,
152 + META_NAME_FOR_POST_TIER_ID_SETTINGS,
153 + META_NAME_CONTAINS_PAID_CONTENT,
146 154 )
147 155 );
148 156 }
149 157 );
@@ -157,11 +165,8 @@
157 165
158 166 // Hide existing comments
159 167 add_filter( 'get_comment', __NAMESPACE__ . '\maybe_gate_existing_comments' );
160 168
161 - // Gate the excerpt for a post
162 - add_filter( 'get_the_excerpt', __NAMESPACE__ . '\jetpack_filter_excerpt_for_newsletter', 10, 2 );
163 -
164 169 // Add a 'Newsletter' column to the Edit posts page
165 170 // We only display the "Newsletter" column if we have configured the paid newsletter plan
166 171 if ( defined( 'WP_ADMIN' ) && WP_ADMIN && Jetpack_Memberships::has_configured_plans_jetpack_recurring_payments( 'newsletter' ) ) {
167 172 add_action( 'manage_post_posts_columns', __NAMESPACE__ . '\register_newsletter_access_column' );
@@ -170,14 +175,15 @@
170 175 }
171 176
172 177 add_action( 'init', __NAMESPACE__ . '\maybe_prevent_super_cache_caching' );
173 178
174 - add_action( 'save_post_post', __NAMESPACE__ . '\add_paywalled_content_post_meta', 99, 1 );
179 + add_action( 'wp_after_insert_post', __NAMESPACE__ . '\add_paywalled_content_post_meta', 99, 2 );
175 180
176 181 add_filter(
177 182 'jetpack_options_whitelist',
178 183 function ( $options ) {
179 184 $options[] = 'jetpack_subscriptions_subscribe_post_end_enabled';
185 + $options[] = 'jetpack_subscriptions_subscribe_navigation_enabled';
180 186
181 187 return $options;
182 188 }
183 189 );
@@ -182,9 +188,9 @@
182 188 }
183 189 );
184 190
185 191 // If called via REST API, we need to register later in the lifecycle
186 - if ( ( new Host() )->is_wpcom_platform() && ! jetpack_is_frontend() ) {
192 + if ( ( new Host() )->is_wpcom_platform() && ! Request::is_frontend() ) {
187 193 add_action(
188 194 'restapi_theme_init',
189 195 function () {
190 196 Jetpack_Subscription_Site::init()->handle_subscribe_block_placements();
@@ -223,12 +229,17 @@
223 229
224 230 /**
225 231 * Add a meta to prevent publication on firehose, ES AI or Reader
226 232 *
227 - * @param int $post_id Post id being saved.
233 + * @param int $post_id Post id being saved.
234 + * @param \WP_Post $post Post being saved.
228 235 * @return void
229 236 */
230 -function add_paywalled_content_post_meta( int $post_id ) {
237 +function add_paywalled_content_post_meta( int $post_id, \WP_Post $post ) {
238 + if ( $post->post_type !== 'post' ) {
239 + return;
240 + }
241 +
231 242 $access_level = get_post_meta( $post_id, META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS, true );
232 243
233 244 $is_paywalled = false;
234 245 switch ( $access_level ) {
@@ -236,9 +247,14 @@
236 247 case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS:
237 248 case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_SUBSCRIBERS:
238 249 $is_paywalled = true;
239 250 }
240 - update_post_meta( $post_id, META_NAME_CONTAINS_PAYWALLED_CONTENT, $is_paywalled );
251 + if ( $is_paywalled ) {
252 + update_post_meta( $post_id, META_NAME_CONTAINS_PAYWALLED_CONTENT, $is_paywalled );
253 + }
254 + if ( ! $is_paywalled ) {
255 + delete_post_meta( $post_id, META_NAME_CONTAINS_PAYWALLED_CONTENT );
256 + }
241 257 }
242 258
243 259 /**
244 260 * Displays the newsletter access level.
@@ -278,11 +294,11 @@
278 294 echo '<style id="jetpack-newsletter-newsletter-access-column"> table.fixed .column-newsletter_access { width: 10%; } </style>';
279 295 }
280 296
281 297 /**
282 - * Determine the amount of folks currently subscribed to the blog, splitted out in email_subscribers & social_followers & paid_subscribers
298 + * Determine the amount of folks currently subscribed to the blog, splitted out in total_subscribers, email_subscribers, social_followers & paid_subscribers.
283 299 *
284 - * @return array containing ['value' => ['email_subscribers' => 0, 'paid_subscribers' => 0, 'social_followers' => 0]]
300 + * @return array containing ['value' => ['total_subscribers' => 0, 'email_subscribers' => 0, 'paid_subscribers' => 0, 'social_followers' => 0]]
285 301 */
286 302 function fetch_subscriber_counts() {
287 303 $subs_count = 0;
288 304 if ( is_wpcom() ) {
@@ -300,9 +316,10 @@
300 316 $subs_count = array(
301 317 'status' => 'failed',
302 318 'code' => $xml->getErrorCode(),
303 319 'message' => $xml->getErrorMessage(),
304 - 'value' => ( isset( $subs_count['value'] ) ) ? $subs_count['value'] : array(
320 + 'value' => $subs_count['value'] ?? array(
321 + 'total_subscribers' => 0,
305 322 'email_subscribers' => 0,
306 323 'social_followers' => 0,
307 324 'paid_subscribers' => 0,
308 325 ),
@@ -328,11 +345,11 @@
328 345 function get_subscriber_count( $include_social_followers ) {
329 346 $counts = fetch_subscriber_counts();
330 347
331 348 if ( $include_social_followers ) {
332 - $subscriber_count = $counts['value']['email_subscribers'] + $counts['value']['social_followers'];
349 + $subscriber_count = $counts['value']['total_subscribers'] + $counts['value']['social_followers'];
333 350 } else {
334 - $subscriber_count = $counts['value']['email_subscribers'];
351 + $subscriber_count = $counts['value']['total_subscribers'];
335 352 }
336 353 return $subscriber_count;
337 354 }
338 355
@@ -435,8 +452,24 @@
435 452 );
436 453 }
437 454
438 455 /**
456 + * Checks if block style is "button only"
457 + *
458 + * @param string $class_name Block attribute className; multiple names are spearated by space.
459 + *
460 + * @return bool
461 + */
462 +function is_button_only_style( $class_name ) {
463 + if ( empty( $class_name ) ) {
464 + return false;
465 + }
466 +
467 + $class_names = explode( ' ', $class_name );
468 + return in_array( 'is-style-button', $class_names, true );
469 +}
470 +
471 +/**
439 472 * Uses block attributes to generate an array containing the styles for various block elements.
440 473 * Based on Jetpack_Subscriptions_Widget::do_subscription_form() which the block was originally using.
441 474 *
442 475 * @param array $attributes Array containing the block attributes.
@@ -443,8 +476,10 @@
443 476 *
444 477 * @return array
445 478 */
446 479 function get_element_styles_from_attributes( $attributes ) {
480 + $is_button_only_style = is_button_only_style( get_attribute( $attributes, 'className', '' ) );
481 +
447 482 $button_background_style = ! has_attribute( $attributes, 'buttonBackgroundColor' ) && has_attribute( $attributes, 'customButtonGradient' )
448 483 ? get_attribute( $attributes, 'customButtonGradient' )
449 484 : get_attribute( $attributes, 'customButtonBackgroundColor' );
450 485
@@ -460,15 +495,15 @@
460 495 $submit_button_styles .= sprintf( 'color: %s;', get_attribute( $attributes, 'customTextColor' ) );
461 496 }
462 497
463 498 if ( has_attribute( $attributes, 'buttonWidth' ) ) {
464 - $submit_button_wrapper_styles .= sprintf( 'width: %s;', get_attribute( $attributes, 'buttonWidth' ) );
499 + $submit_button_wrapper_styles .= sprintf( 'width: %s;', get_attribute( $attributes, 'buttonWidth', DEFAULT_BUTTON_WIDTH ) );
465 500 $submit_button_wrapper_styles .= 'max-width: 100%;';
466 501
467 502 // Account for custom margins on inline forms.
468 503 $submit_button_styles .= true === get_attribute( $attributes, 'buttonOnNewLine' )
469 - ? sprintf( 'width: calc(100%% - %dpx);', get_attribute( $attributes, 'spacing', DEFAULT_SPACING_VALUE ) )
470 - : 'width: 100%;';
504 + ? 'width: 100%;'
505 + : sprintf( 'width: calc(100%% - %dpx);', get_attribute( $attributes, 'spacing', DEFAULT_SPACING_VALUE ) );
471 506 }
472 507
473 508 $font_size = get_attribute( $attributes, 'customFontSize', DEFAULT_FONTSIZE_VALUE );
474 509 $style = sprintf( 'font-size: %s%s;', $font_size, is_numeric( $font_size ) ? 'px' : '' );
@@ -481,14 +516,16 @@
481 516
482 517 $submit_button_styles .= $style;
483 518 $email_field_styles .= $style;
484 519
485 - $button_spacing = get_attribute( $attributes, 'spacing', DEFAULT_SPACING_VALUE );
486 - if ( true === get_attribute( $attributes, 'buttonOnNewLine' ) ) {
487 - $submit_button_styles .= sprintf( 'margin-top: %dpx;', $button_spacing );
488 - } else {
489 - $submit_button_styles .= 'margin: 0px; '; // Reset Safari's 2px default margin for buttons affecting input and button union
490 - $submit_button_styles .= sprintf( 'margin-left: %dpx;', $button_spacing );
520 + if ( ! $is_button_only_style ) {
521 + $button_spacing = get_attribute( $attributes, 'spacing', DEFAULT_SPACING_VALUE );
522 + if ( true === get_attribute( $attributes, 'buttonOnNewLine' ) ) {
523 + $submit_button_styles .= sprintf( 'margin-top: %dpx;', $button_spacing );
524 + } else {
525 + $submit_button_styles .= 'margin: 0; '; // Reset Safari's 2px default margin for buttons affecting input and button union
526 + $submit_button_styles .= sprintf( 'margin-left: %dpx;', $button_spacing );
527 + }
491 528 }
492 529
493 530 if ( has_attribute( $attributes, 'borderColor' ) ) {
494 531 $style = sprintf( 'border-color: %s;', get_attribute( $attributes, 'borderColor', '' ) );
@@ -510,9 +547,9 @@
510 547 $submit_button_styles .= $style;
511 548 $email_field_styles .= $style;
512 549 }
513 550
514 - if ( ! jetpack_is_frontend() ) {
551 + if ( ! Request::is_frontend() ) {
515 552 $background_color_style = get_attribute_color( 'buttonBackgroundColor', $attributes, '#113AF5' /* default lettre theme color */ );
516 553 $text_color_style = get_attribute_color( 'textColor', $attributes, '#FFFFFF' );
517 554 $submit_button_styles .= sprintf( ' background-color: %s; color: %s;', $background_color_style, $text_color_style );
518 555 }
@@ -597,8 +634,15 @@
597 634 return null;
598 635 }
599 636
600 637 /**
638 + * Is the Jetpack_Memberships class loaded.
639 + */
640 +function is_jetpack_memberships_loaded(): bool {
641 + return class_exists( '\Jetpack_Memberships' );
642 +}
643 +
644 +/**
601 645 * Subscriptions block render callback.
602 646 *
603 647 * @param array $attributes Array containing the block attributes.
604 648 *
@@ -605,13 +649,13 @@
605 649 * @return string
606 650 */
607 651 function render_block( $attributes ) {
608 652 // If the Subscriptions module is not active, don't render the block.
609 - if ( ! Jetpack::is_module_active( 'subscriptions' ) ) {
653 + if ( ! ( new Modules() )->is_active( 'subscriptions' ) ) {
610 654 return '';
611 655 }
612 656
613 - if ( class_exists( '\Jetpack_Memberships' ) ) {
657 + if ( is_jetpack_memberships_loaded() ) {
614 658 // We only want the sites that have newsletter feature enabled to be graced by this JavaScript.
615 659 Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
616 660 } else {
617 661 Jetpack_Gutenberg::load_styles_as_required( FEATURE_NAME );
@@ -616,14 +660,21 @@
616 660 } else {
617 661 Jetpack_Gutenberg::load_styles_as_required( FEATURE_NAME );
618 662 }
619 663
664 + if ( ! class_exists( 'Jetpack_Subscriptions_Widget' ) ) {
665 + return '';
666 + }
667 +
668 + // Prefill the email field with the current user's email if they are logged in via Memberships premium content token
620 669 $subscribe_email = Jetpack_Memberships::get_current_user_email();
621 670
622 - /** This filter is documented in \Automattic\Jetpack\Forms\ContactForm\Contact_Form */
623 - if ( is_wpcom() || false !== apply_filters( 'jetpack_auto_fill_logged_in_user', false ) ) {
624 - $current_user = wp_get_current_user();
625 - $subscribe_email = ! empty( $current_user->user_email ) ? $current_user->user_email : '';
671 + // If no email, then prefill the email field with the current user's email if they are logged in
672 + if ( empty( $subscribe_email ) ) {
673 + $current_user = wp_get_current_user();
674 + if ( ! empty( $current_user->user_email ) ) {
675 + $subscribe_email = $current_user->user_email;
676 + }
626 677 }
627 678
628 679 // The block is using the Jetpack_Subscriptions_Widget backend, hence the need to increase the instance count.
629 680 ++Jetpack_Subscriptions_Widget::$instance_count;
@@ -638,36 +689,44 @@
638 689 // We want to disencourage including social count as it's misleading.
639 690 $include_social_followers = isset( $attributes['includeSocialFollowers'] ) ? (bool) get_attribute( $attributes, 'includeSocialFollowers' ) : true;
640 691
641 692 $data = array(
642 - 'widget_id' => Jetpack_Subscriptions_Widget::$instance_count,
643 - 'subscribe_email' => $subscribe_email,
644 - 'is_paid_subscriber' => get_attribute( $attributes, 'isPaidSubscriber', false ),
645 - 'wrapper_attributes' => get_block_wrapper_attributes(
693 + 'widget_id' => Jetpack_Subscriptions_Widget::$instance_count,
694 + 'subscribe_email' => $subscribe_email,
695 + 'is_paid_subscriber' => get_attribute( $attributes, 'isPaidSubscriber', false ),
696 + 'wrapper_attributes' => get_block_wrapper_attributes(
646 697 array(
647 698 'class' => $classes['block_wrapper'],
648 699 )
649 700 ),
650 - 'subscribe_placeholder' => get_attribute( $attributes, 'subscribePlaceholder', __( 'Type your email…', 'jetpack' ) ),
651 - 'submit_button_text' => get_attribute( $attributes, 'submitButtonText', __( 'Subscribe', 'jetpack' ) ),
652 - 'submit_button_text_subscribed' => get_attribute( $attributes, 'submitButtonTextSubscribed', __( 'Subscribed', 'jetpack' ) ),
653 - 'submit_button_text_upgrade' => get_attribute( $attributes, 'submitButtonTextUpgrade', __( 'Upgrade subscription', 'jetpack' ) ),
654 - 'success_message' => get_attribute(
701 + 'subscribe_placeholder' => get_attribute( $attributes, 'subscribePlaceholder', __( 'Type your email…', 'jetpack' ) ),
702 + 'submit_button_text' => get_attribute( $attributes, 'submitButtonText', __( 'Subscribe', 'jetpack' ) ),
703 + 'submit_button_text_subscribed' => get_attribute( $attributes, 'submitButtonTextSubscribed', __( 'Subscribed', 'jetpack' ) ),
704 + 'submit_button_text_upgrade' => get_attribute( $attributes, 'submitButtonTextUpgrade', __( 'Upgrade subscription', 'jetpack' ) ),
705 + 'success_message' => get_attribute(
655 706 $attributes,
656 707 'successMessage',
657 708 esc_html__( "Success! An email was just sent to confirm your subscription. Please find the email now and click 'Confirm' to start subscribing.", 'jetpack' )
658 709 ),
659 - 'show_subscribers_total' => (bool) get_attribute( $attributes, 'showSubscribersTotal' ),
660 - 'subscribers_total' => get_subscriber_count( $include_social_followers ),
661 - 'referer' => esc_url_raw(
710 + 'show_subscribers_total' => (bool) get_attribute( $attributes, 'showSubscribersTotal' ),
711 + 'subscribers_total' => get_attribute( $attributes, 'showSubscribersTotal' ) ? get_subscriber_count( $include_social_followers ) : 0,
712 + 'referer' => esc_url_raw(
662 713 ( is_ssl() ? 'https' : 'http' ) . '://' . ( isset( $_SERVER['HTTP_HOST'] ) ? wp_unslash( $_SERVER['HTTP_HOST'] ) : '' ) .
663 714 ( isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '' )
664 715 ),
665 - 'source' => 'subscribe-block',
666 - 'app_source' => get_attribute( $attributes, 'appSource', null ),
716 + 'source' => 'subscribe-block',
717 + 'app_source' => get_attribute( $attributes, 'appSource', null ),
718 + 'class_name' => get_attribute( $attributes, 'className' ),
719 + 'selected_newsletter_categories' => get_attribute( $attributes, 'selectedNewsletterCategoryIds', array() ),
720 + 'preselected_newsletter_categories' => get_attribute( $attributes, 'preselectNewsletterCategories', false ),
667 721 );
668 722
669 - if ( ! jetpack_is_frontend() ) {
723 + // Only render the email version in non-frontend contexts.
724 + if ( is_feed() || wp_is_xml_request() ||
725 + ( defined( 'REST_REQUEST' ) && REST_REQUEST && ! wp_is_json_request() ) ||
726 + ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ) ||
727 + ( defined( 'WP_CLI' ) && WP_CLI ) ||
728 + wp_is_jsonp_request() ) {
670 729 return render_for_email( $data, $styles );
671 730 }
672 731
673 732 return render_for_website( $data, $classes, $styles );
@@ -696,14 +755,15 @@
696 755 *
697 756 * @return string
698 757 */
699 758 function render_for_website( $data, $classes, $styles ) {
700 - $lang = get_locale();
701 - $blog_id = \Jetpack_Options::get_option( 'id' );
702 - $widget_id_suffix = Jetpack_Subscriptions_Widget::$instance_count > 1 ? '-' . Jetpack_Subscriptions_Widget::$instance_count : '';
703 - $form_id = 'subscribe-blog' . $widget_id_suffix;
704 - $form_url = 'https://wordpress.com/email-subscriptions';
705 - $post_access_level = get_post_access_level_for_current_post();
759 + $lang = get_locale();
760 + $blog_id = \Jetpack_Options::get_option( 'id' );
761 + $widget_id_suffix = Jetpack_Subscriptions_Widget::$instance_count > 1 ? '-' . Jetpack_Subscriptions_Widget::$instance_count : '';
762 + $form_id = 'subscribe-blog' . $widget_id_suffix;
763 + $form_url = 'https://wordpress.com/email-subscriptions';
764 + $post_access_level = get_post_access_level_for_current_post();
765 + $is_button_only_style = ! empty( $data['class_name'] ) ? is_button_only_style( $data['class_name'] ) : false;
706 766
707 767 // Post ID is used for pulling post-specific paid status, and returning to the right post after confirming subscription
708 768 $post_id = null;
709 769 if ( in_the_loop() ) {
@@ -736,9 +796,9 @@
736 796 style="<?php echo esc_attr( $styles['submit_button_wrapper'] ); ?>"
737 797 <?php endif; ?>
738 798 >
739 799 <a
740 - href="<?php echo esc_url( 'https://wordpress.com/read/site/subscription/' . $blog_id ); ?>"
800 + href="<?php echo esc_url( 'https://wordpress.com/reader/site/subscription/' . $blog_id ); ?>"
741 801 <?php if ( ! empty( $classes['submit_button'] ) ) : ?>
742 802 class="<?php echo esc_attr( $classes['submit_button'] ); ?>"
743 803 <?php endif; ?>
744 804 <?php if ( ! empty( $styles['submit_button'] ) ) : ?>
@@ -758,9 +818,9 @@
758 818 data-subscriber_email="<?php echo esc_attr( $data['subscribe_email'] ); ?>"
759 819 id="<?php echo esc_attr( $form_id ); ?>"
760 820 >
761 821 <div class="wp-block-jetpack-subscriptions__form-elements">
762 - <?php if ( ! $is_subscribed ) : ?>
822 + <?php if ( ! $is_subscribed && ! $is_button_only_style ) : ?>
763 823 <p id="subscribe-email">
764 824 <label
765 825 id="<?php echo esc_attr( $subscribe_field_id . '-label' ); ?>"
766 826 for="<?php echo esc_attr( $subscribe_field_id ); ?>"
@@ -773,8 +833,9 @@
773 833 '<input
774 834 required="required"
775 835 type="email"
776 836 name="email"
837 + autocomplete="email"
777 838 %1$s
778 839 style="%2$s"
779 840 placeholder="%3$s"
780 841 value="%4$s"
@@ -821,8 +882,12 @@
821 882
822 883 if ( ! empty( $tier_id ) ) {
823 884 echo '<input type="hidden" name="tier_id" value="' . esc_attr( $tier_id ) . '"/>';
824 885 }
886 +
887 + if ( $data['preselected_newsletter_categories'] && ! empty( $data['selected_newsletter_categories'] ) ) {
888 + echo '<input type="hidden" name="selected_newsletter_categories" value="' . esc_attr( implode( ',', $data['selected_newsletter_categories'] ) ) . '"/>';
889 + }
825 890 ?>
826 891 <button type="submit"
827 892 <?php if ( ! empty( $classes['submit_button'] ) ) : ?>
828 893 class="<?php echo esc_attr( $classes['submit_button'] ); ?>"
@@ -876,26 +941,56 @@
876 941 return $html;
877 942 }
878 943
879 944 /**
880 - * Filter excerpts looking for subscription data.
945 + * WooCommerce Email Editor render callback for the subscriptions block.
881 946 *
882 - * @param string $excerpt The extrapolated excerpt string.
883 - * @param \WP_Post $post The current post being processed (in `get_the_excerpt`).
947 + * @param string $block_content The block content.
948 + * @param array $parsed_block The parsed block data.
949 + * @param object $rendering_context The email rendering context.
884 950 *
885 - * @return mixed
951 + * @return string
886 952 */
887 -function jetpack_filter_excerpt_for_newsletter( $excerpt, $post = null ) {
888 - // The blogmagazine theme is overriding WP core `get_the_excerpt` filter and only passing the excerpt
889 - // TODO: Until this is fixed, return the excerpt without gating. See https://github.com/Automattic/jetpack/pull/28102#issuecomment-1369161116
890 - if ( $post && str_contains( $post->post_content, '<!-- wp:jetpack/subscriptions -->' ) ) {
891 - $excerpt .= sprintf(
892 - // translators: %s is the permalink url to the current post.
893 - __( "<p><a href='%s'>View post</a> to subscribe to site newsletter.</p>", 'jetpack' ),
894 - get_post_permalink()
895 - );
953 +function render_email( $block_content, array $parsed_block, $rendering_context ) {
954 + 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' ) ) {
955 + return '';
896 956 }
897 - return $excerpt;
957 +
958 + // Map subscription block attributes to button block attributes
959 + $button_attributes = array(
960 + 'text' => ! empty( $parsed_block['attrs']['submitButtonText'] ) ? sanitize_text_field( $parsed_block['attrs']['submitButtonText'] ) : __( 'Subscribe', 'jetpack' ),
961 + 'url' => get_post_permalink(),
962 + 'element' => 'a',
963 + // Map background colors
964 + 'backgroundColor' => $parsed_block['attrs']['buttonBackgroundColor'] ?? null,
965 + 'customBackgroundColor' => $parsed_block['attrs']['customButtonBackgroundColor'] ?? null,
966 + // Map text colors
967 + 'textColor' => $parsed_block['attrs']['textColor'] ?? null,
968 + 'customTextColor' => $parsed_block['attrs']['customTextColor'] ?? null,
969 + // Map borders
970 + 'borderRadius' => $parsed_block['attrs']['borderRadius'] ?? 0,
971 + 'borderWeight' => $parsed_block['attrs']['borderWeight'] ?? 1,
972 + 'borderColor' => $parsed_block['attrs']['borderColor'] ?? null,
973 + 'customBorderColor' => $parsed_block['attrs']['customBorderColor'] ?? null,
974 + // Map typography
975 + 'fontSize' => $parsed_block['attrs']['fontSize'] ?? null,
976 + 'customFontSize' => $parsed_block['attrs']['customFontSize'] ?? null,
977 + // Map spacing
978 + 'padding' => $parsed_block['attrs']['padding'] ?? null,
979 + );
980 +
981 + // Create a mock button block structure
982 + $button_parsed_block = array(
983 + 'attrs' => $button_attributes,
984 + 'email_attrs' => $parsed_block['email_attrs'] ?? array(),
985 + );
986 +
987 + // Call the Jetpack button's email rendering
988 + return \Automattic\Jetpack\Extensions\Button\render_email(
989 + $block_content,
990 + $button_parsed_block,
991 + $rendering_context
992 + );
898 993 }
899 994
900 995 /**
901 996 * Gate access to posts
@@ -920,9 +1015,9 @@
920 1015 }
921 1016 return $the_content;
922 1017 }
923 1018
924 - $paywalled_content = get_paywall_content( $post_access_level ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
1019 + $paywalled_content = get_paywall_content();
925 1020
926 1021 if ( has_block( \Automattic\Jetpack\Extensions\Paywall\BLOCK_NAME ) ) {
927 1022 if ( strpos( $the_content, \Automattic\Jetpack\Extensions\Paywall\BLOCK_HTML ) ) {
928 1023 return strstr( $the_content, \Automattic\Jetpack\Extensions\Paywall\BLOCK_HTML, true ) . $paywalled_content;
@@ -1001,12 +1096,11 @@
1001 1096
1002 1097 /**
1003 1098 * Returns paywall content blocks
1004 1099 *
1005 - * @param string $post_access_level The newsletter access level.
1006 1100 * @return string
1007 1101 */
1008 -function get_paywall_content( $post_access_level ) {
1102 +function get_paywall_content() {
1009 1103 if ( Jetpack_Memberships::user_is_pending_subscriber() ) {
1010 1104 return get_paywall_blocks_subscribe_pending();
1011 1105 }
1012 1106 if ( doing_filter( 'get_the_excerpt' ) ) {
@@ -1011,9 +1105,9 @@
1011 1105 }
1012 1106 if ( doing_filter( 'get_the_excerpt' ) ) {
1013 1107 return '';
1014 1108 }
1015 - return get_paywall_blocks( $post_access_level );
1109 + return get_paywall_blocks();
1016 1110 }
1017 1111
1018 1112 /**
1019 1113 * Returns the current URL.
@@ -1095,22 +1189,22 @@
1095 1189
1096 1190 /**
1097 1191 * Returns paywall content blocks if user is not authenticated
1098 1192 *
1099 - * @param string $newsletter_access_level The newsletter access level.
1100 1193 * @return string
1101 1194 */
1102 -function get_paywall_blocks( $newsletter_access_level ) {
1195 +function get_paywall_blocks() {
1103 1196 $custom_paywall = apply_filters( 'jetpack_custom_paywall_blocks', false );
1104 1197 if ( ! empty( $custom_paywall ) ) {
1105 1198 return $custom_paywall;
1106 1199 }
1107 - if ( ! jetpack_is_frontend() ) { // emails
1200 +
1201 + if ( ! Request::is_frontend() ) { // emails
1108 1202 return get_paywall_simple();
1109 1203 }
1204 +
1110 1205 require_once JETPACK__PLUGIN_DIR . 'modules/memberships/class-jetpack-memberships.php';
1111 - // Only display paid texts when Stripe is connected and the post is marked for paid subscribers
1112 - $is_paid_post = $newsletter_access_level === 'paid_subscribers' && Jetpack_Memberships::has_connected_account();
1206 + $is_paid_post = is_paid_post();
1113 1207 $is_paid_subscriber = Jetpack_Memberships::user_is_paid_subscriber();
1114 1208
1115 1209 $access_heading = $is_paid_subscriber
1116 1210 ? esc_html__( 'Upgrade to continue reading', 'jetpack' )
@@ -1117,11 +1211,13 @@
1117 1211 : esc_html__( 'Subscribe to continue reading', 'jetpack' );
1118 1212
1119 1213 $subscribe_text = $is_paid_post
1120 1214 // translators: %s is the name of the site.
1121 - ? $is_paid_subscriber
1122 - ? esc_html__( 'Upgrade to get access to the rest of this post and other exclusive content.', 'jetpack' )
1123 - : esc_html__( 'Become a paid subscriber to get access to the rest of this post and other exclusive content.', 'jetpack' )
1215 + ? (
1216 + $is_paid_subscriber
1217 + ? esc_html__( 'Upgrade to get access to the rest of this post and other exclusive content.', 'jetpack' )
1218 + : esc_html__( 'Become a paid subscriber to get access to the rest of this post and other exclusive content.', 'jetpack' )
1219 + )
1124 1220 // translators: %s is the name of the site.
1125 1221 : esc_html__( 'Subscribe to get access to the rest of this post and other subscriber-only content.', 'jetpack' );
1126 1222
1127 1223 $login_block = '';
@@ -1143,9 +1239,9 @@
1143 1239 </p>
1144 1240 <!-- /wp:paragraph -->';
1145 1241 }
1146 1242 } else {
1147 - $access_question = get_paywall_access_question( $newsletter_access_level );
1243 + $access_question = $is_paid_post ? esc_html__( 'Already a paid subscriber?', 'jetpack' ) : esc_html__( 'Already a subscriber?', 'jetpack' );
1148 1244 $login_block = '<!-- wp:group {"style":{"typography":{"fontSize":"14px"}},"layout":{"type":"flex","justifyContent":"center"}} -->
1149 1245 <div class="wp-block-group" style="font-size:14px">
1150 1246 <!-- wp:jetpack/subscriber-login {"logInLabel":"' . $access_question . '"} /-->
1151 1247 </div>
@@ -1176,33 +1272,8 @@
1176 1272 ';
1177 1273 }
1178 1274
1179 1275 /**
1180 - * Returns Get Access question for the paywall
1181 - *
1182 - * @param string $post_access_level The newsletter access level.
1183 - * @return string
1184 - */
1185 -function get_paywall_access_question( $post_access_level ) {
1186 - switch ( $post_access_level ) {
1187 - case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS:
1188 - case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS_ALL_TIERS:
1189 - $tier = Jetpack_Memberships::get_post_tier();
1190 - if ( $tier !== null ) {
1191 - return sprintf(
1192 - // translators: Placeholder is the tier name
1193 - __( 'Already a higher-tier paid subscriber?', 'jetpack' ),
1194 - esc_html( $tier->post_title )
1195 - );
1196 - } else {
1197 - return esc_html__( 'Already a paid subscriber?', 'jetpack' );
1198 - }
1199 - default:
1200 - return esc_html__( 'Already a subscriber?', 'jetpack' );
1201 - }
1202 -}
1203 -
1204 -/**
1205 1276 * Returns true if user is auth for subscriptions check, otherwise returns false.
1206 1277 *
1207 1278 * @return bool
1208 1279 */
@@ -1222,8 +1293,36 @@
1222 1293 return false;
1223 1294 }
1224 1295
1225 1296 /**
1297 + * Returns `true` if the post is a paid post.
1298 + */
1299 +function is_paid_post(): bool {
1300 + require_once JETPACK__PLUGIN_DIR . 'modules/memberships/class-jetpack-memberships.php';
1301 +
1302 + // Make sure Stripe is connected and the post is marked for paid subscribers.
1303 + if ( Jetpack_Memberships::has_connected_account() && is_jetpack_token_subscription_service_loaded() ) {
1304 + return Jetpack_Memberships::get_post_access_level() === Jetpack_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS;
1305 + }
1306 +
1307 + return false;
1308 +}
1309 +
1310 +/**
1311 + * Returns true if the post is a subscribers post.
1312 + */
1313 +function is_subscribers_post(): bool {
1314 + require_once JETPACK__PLUGIN_DIR . 'modules/memberships/class-jetpack-memberships.php';
1315 +
1316 + // Make sure Stripe is connected and the post is marked for paid subscribers.
1317 + if ( Jetpack_Memberships::has_connected_account() && is_jetpack_token_subscription_service_loaded() ) {
1318 + return Jetpack_Memberships::get_post_access_level() === Jetpack_Token_Subscription_Service::POST_ACCESS_LEVEL_SUBSCRIBERS;
1319 + }
1320 +
1321 + return false;
1322 +}
1323 +
1324 +/**
1226 1325 * Returns paywall content blocks when email confirmation is pending
1227 1326 *
1228 1327 * @return string
1229 1328 */
@@ -1264,22 +1363,37 @@
1264 1363 ';
1265 1364 }
1266 1365
1267 1366 /**
1268 - * Return content for non frontend views like emails.
1269 - *
1270 - * @return string
1367 + * Return content for non frontend views like Reader, emails.
1271 1368 */
1272 -function get_paywall_simple() {
1273 - $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' );
1369 +function get_paywall_simple(): string {
1370 + $is_paid_post = is_paid_post();
1371 + $is_subscribers_post = is_subscribers_post();
1372 + $is_subscriber = is_jetpack_memberships_loaded() && Jetpack_Memberships::is_current_user_subscribed();
1373 + $paywall_heading = esc_html__( 'Subscribe to keep reading', 'jetpack' );
1274 1374
1275 - $subscribe_text = esc_html__( 'Upgrade subscription', 'jetpack' );
1375 + if ( $is_subscribers_post && ! $is_subscriber ) {
1376 + $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' );
1377 + $paywall_action_btn = esc_html__( 'Subscribe', 'jetpack' );
1378 + } elseif ( $is_paid_post && $is_subscriber ) {
1379 + $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' );
1380 + $paywall_action_btn = esc_html__( 'Upgrade subscription', 'jetpack' );
1381 + } else {
1382 + // - For paid post when the user is not a subscriber.
1383 + // - Default for all other cases.
1384 + $paywall_description = esc_html__( 'Become a paid subscriber to get access to the rest of this post and other exclusive content.', 'jetpack' );
1385 + $paywall_action_btn = esc_html__( 'Subscribe', 'jetpack' );
1386 + }
1276 1387
1277 1388 return '
1278 1389 <!-- wp:columns -->
1279 -<div class="wp-block-columns" style="display: inline-block; width: 90%">
1390 +<div class="wp-block-columns jetpack-paywall-simple" style="display: inline-block; width: 90%">
1280 1391 <!-- wp:column -->
1281 1392 <div class="wp-block-column" style="background-color: #F6F7F7; padding: 32px; 24px;">
1393 + <!-- wp:heading -->
1394 + <h2 class="has-text-align-center" style="margin: 0 0 12px; font-weight: 600;">' . $paywall_heading . '</h2>
1395 + <!-- /wp:heading -->
1282 1396 <!-- wp:paragraph -->
1283 1397 <p class="has-text-align-center"
1284 1398 style="text-align: center;
1285 1399 color: #50575E;
@@ -1286,20 +1400,20 @@
1286 1400 font-weight: 400;
1287 1401 font-size: 16px;
1288 1402 font-family: \'SF Pro Text\', sans-serif;
1289 1403 line-height: 28.8px;">
1290 - ' . $access_heading . '
1404 + ' . $paywall_description . '
1291 1405 </p>
1292 1406 <!-- /wp:paragraph -->
1293 1407 <!-- wp:buttons -->
1294 1408 <div class="wp-block-buttons" style="text-align: center;">
1295 1409 <!-- wp:button -->
1296 - <div class="wp-block-button" style="display: inline-block; margin: 10px 0;">
1410 + <div class="wp-block-button" style="display: inline-block; margin: 10px 0; border-style: none; padding: 0;">
1297 1411 <a href="' . esc_url( get_post_permalink() ) . '" class="wp-block-button__link wp-element-button"
1298 1412 data-wpcom-track data-tracks-link-desc="paywall-email-click"
1299 1413 style="display: inline-block;
1300 - padding: 15px 20px;
1301 - background-color: #0675C4;
1414 + padding: 12px 15px;
1415 + background-color: #3858e9;
1302 1416 color: #FFFFFF;
1303 1417 text-decoration: none;
1304 1418 border-radius: 5px;
1305 1419 font-family: \'SF Pro Display\', sans-serif;
@@ -1304,9 +1418,9 @@
1304 1418 border-radius: 5px;
1305 1419 font-family: \'SF Pro Display\', sans-serif;
1306 1420 font-weight: 500;
1307 1421 font-size: 16px;
1308 - text-align: center;">' . $subscribe_text . '</a>
1422 + text-align: center;">' . $paywall_action_btn . '</a>
1309 1423 </div>
1310 1424 <!-- /wp:button -->
1311 1425 </div>
1312 1426 <!-- /wp:buttons -->