← All changes
|
extensions/blocks/subscriptions/subscriptions.php
+263
-148
13.4.5
→
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,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,15 +229,21 @@ | ||
| 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 | - $is_paywalled = false; | |
| 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 ); | |
| 234 | 246 | switch ( $access_level ) { |
| 235 | 247 | case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS_ALL_TIERS: |
| 236 | 248 | case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS: |
| 237 | 249 | case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_SUBSCRIBERS: |
| @@ -236,9 +248,14 @@ | ||
| 236 | 248 | case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS: |
| 237 | 249 | case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_SUBSCRIBERS: |
| 238 | 250 | $is_paywalled = true; |
| 239 | 251 | } |
| 240 | - update_post_meta( $post_id, META_NAME_CONTAINS_PAYWALLED_CONTENT, $is_paywalled ); | |
| 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 | + } | |
| 241 | 258 | } |
| 242 | 259 | |
| 243 | 260 | /** |
| 244 | 261 | * Displays the newsletter access level. |
| @@ -250,9 +267,9 @@ | ||
| 250 | 267 | if ( NEWSLETTER_COLUMN_ID !== $column_id ) { |
| 251 | 268 | return; |
| 252 | 269 | } |
| 253 | 270 | |
| 254 | - $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 ); | |
| 255 | 272 | |
| 256 | 273 | switch ( $access_level ) { |
| 257 | 274 | case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS_ALL_TIERS: |
| 258 | 275 | echo esc_html__( 'Paid Subscribers (all plans)', 'jetpack' ); |
| @@ -278,11 +295,11 @@ | ||
| 278 | 295 | echo '<style id="jetpack-newsletter-newsletter-access-column"> table.fixed .column-newsletter_access { width: 10%; } </style>'; |
| 279 | 296 | } |
| 280 | 297 | |
| 281 | 298 | /** |
| 282 | - * 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. | |
| 283 | 300 | * |
| 284 | - * @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]] | |
| 285 | 302 | */ |
| 286 | 303 | function fetch_subscriber_counts() { |
| 287 | 304 | $subs_count = 0; |
| 288 | 305 | if ( is_wpcom() ) { |
| @@ -300,9 +317,10 @@ | ||
| 300 | 317 | $subs_count = array( |
| 301 | 318 | 'status' => 'failed', |
| 302 | 319 | 'code' => $xml->getErrorCode(), |
| 303 | 320 | 'message' => $xml->getErrorMessage(), |
| 304 | - 'value' => ( isset( $subs_count['value'] ) ) ? $subs_count['value'] : array( | |
| 321 | + 'value' => $subs_count['value'] ?? array( | |
| 322 | + 'total_subscribers' => 0, | |
| 305 | 323 | 'email_subscribers' => 0, |
| 306 | 324 | 'social_followers' => 0, |
| 307 | 325 | 'paid_subscribers' => 0, |
| 308 | 326 | ), |
| @@ -328,11 +346,11 @@ | ||
| 328 | 346 | function get_subscriber_count( $include_social_followers ) { |
| 329 | 347 | $counts = fetch_subscriber_counts(); |
| 330 | 348 | |
| 331 | 349 | if ( $include_social_followers ) { |
| 332 | - $subscriber_count = $counts['value']['email_subscribers'] + $counts['value']['social_followers']; | |
| 350 | + $subscriber_count = $counts['value']['total_subscribers'] + $counts['value']['social_followers']; | |
| 333 | 351 | } else { |
| 334 | - $subscriber_count = $counts['value']['email_subscribers']; | |
| 352 | + $subscriber_count = $counts['value']['total_subscribers']; | |
| 335 | 353 | } |
| 336 | 354 | return $subscriber_count; |
| 337 | 355 | } |
| 338 | 356 | |
| @@ -435,8 +453,24 @@ | ||
| 435 | 453 | ); |
| 436 | 454 | } |
| 437 | 455 | |
| 438 | 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 | +/** | |
| 439 | 473 | * Uses block attributes to generate an array containing the styles for various block elements. |
| 440 | 474 | * Based on Jetpack_Subscriptions_Widget::do_subscription_form() which the block was originally using. |
| 441 | 475 | * |
| 442 | 476 | * @param array $attributes Array containing the block attributes. |
| @@ -443,8 +477,10 @@ | ||
| 443 | 477 | * |
| 444 | 478 | * @return array |
| 445 | 479 | */ |
| 446 | 480 | function get_element_styles_from_attributes( $attributes ) { |
| 481 | + $is_button_only_style = is_button_only_style( get_attribute( $attributes, 'className', '' ) ); | |
| 482 | + | |
| 447 | 483 | $button_background_style = ! has_attribute( $attributes, 'buttonBackgroundColor' ) && has_attribute( $attributes, 'customButtonGradient' ) |
| 448 | 484 | ? get_attribute( $attributes, 'customButtonGradient' ) |
| 449 | 485 | : get_attribute( $attributes, 'customButtonBackgroundColor' ); |
| 450 | 486 | |
| @@ -460,15 +496,15 @@ | ||
| 460 | 496 | $submit_button_styles .= sprintf( 'color: %s;', get_attribute( $attributes, 'customTextColor' ) ); |
| 461 | 497 | } |
| 462 | 498 | |
| 463 | 499 | if ( has_attribute( $attributes, 'buttonWidth' ) ) { |
| 464 | - $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 ) ); | |
| 465 | 501 | $submit_button_wrapper_styles .= 'max-width: 100%;'; |
| 466 | 502 | |
| 467 | 503 | // Account for custom margins on inline forms. |
| 468 | 504 | $submit_button_styles .= true === get_attribute( $attributes, 'buttonOnNewLine' ) |
| 469 | - ? sprintf( 'width: calc(100%% - %dpx);', get_attribute( $attributes, 'spacing', DEFAULT_SPACING_VALUE ) ) | |
| 470 | - : 'width: 100%;'; | |
| 505 | + ? 'width: 100%;' | |
| 506 | + : sprintf( 'width: calc(100%% - %dpx);', get_attribute( $attributes, 'spacing', DEFAULT_SPACING_VALUE ) ); | |
| 471 | 507 | } |
| 472 | 508 | |
| 473 | 509 | $font_size = get_attribute( $attributes, 'customFontSize', DEFAULT_FONTSIZE_VALUE ); |
| 474 | 510 | $style = sprintf( 'font-size: %s%s;', $font_size, is_numeric( $font_size ) ? 'px' : '' ); |
| @@ -481,14 +517,16 @@ | ||
| 481 | 517 | |
| 482 | 518 | $submit_button_styles .= $style; |
| 483 | 519 | $email_field_styles .= $style; |
| 484 | 520 | |
| 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 ); | |
| 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 | + } | |
| 491 | 529 | } |
| 492 | 530 | |
| 493 | 531 | if ( has_attribute( $attributes, 'borderColor' ) ) { |
| 494 | 532 | $style = sprintf( 'border-color: %s;', get_attribute( $attributes, 'borderColor', '' ) ); |
| @@ -510,9 +548,9 @@ | ||
| 510 | 548 | $submit_button_styles .= $style; |
| 511 | 549 | $email_field_styles .= $style; |
| 512 | 550 | } |
| 513 | 551 | |
| 514 | - if ( ! jetpack_is_frontend() ) { | |
| 552 | + if ( ! Request::is_frontend() ) { | |
| 515 | 553 | $background_color_style = get_attribute_color( 'buttonBackgroundColor', $attributes, '#113AF5' /* default lettre theme color */ ); |
| 516 | 554 | $text_color_style = get_attribute_color( 'textColor', $attributes, '#FFFFFF' ); |
| 517 | 555 | $submit_button_styles .= sprintf( ' background-color: %s; color: %s;', $background_color_style, $text_color_style ); |
| 518 | 556 | } |
| @@ -597,8 +635,15 @@ | ||
| 597 | 635 | return null; |
| 598 | 636 | } |
| 599 | 637 | |
| 600 | 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 | +/** | |
| 601 | 646 | * Subscriptions block render callback. |
| 602 | 647 | * |
| 603 | 648 | * @param array $attributes Array containing the block attributes. |
| 604 | 649 | * |
| @@ -605,13 +650,13 @@ | ||
| 605 | 650 | * @return string |
| 606 | 651 | */ |
| 607 | 652 | function render_block( $attributes ) { |
| 608 | 653 | // If the Subscriptions module is not active, don't render the block. |
| 609 | - if ( ! Jetpack::is_module_active( 'subscriptions' ) ) { | |
| 654 | + if ( ! ( new Modules() )->is_active( 'subscriptions' ) ) { | |
| 610 | 655 | return ''; |
| 611 | 656 | } |
| 612 | 657 | |
| 613 | - if ( class_exists( '\Jetpack_Memberships' ) ) { | |
| 658 | + if ( is_jetpack_memberships_loaded() ) { | |
| 614 | 659 | // We only want the sites that have newsletter feature enabled to be graced by this JavaScript. |
| 615 | 660 | Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); |
| 616 | 661 | } else { |
| 617 | 662 | Jetpack_Gutenberg::load_styles_as_required( FEATURE_NAME ); |
| @@ -616,14 +661,21 @@ | ||
| 616 | 661 | } else { |
| 617 | 662 | Jetpack_Gutenberg::load_styles_as_required( FEATURE_NAME ); |
| 618 | 663 | } |
| 619 | 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 | |
| 620 | 670 | $subscribe_email = Jetpack_Memberships::get_current_user_email(); |
| 621 | 671 | |
| 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 : ''; | |
| 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 | + } | |
| 626 | 678 | } |
| 627 | 679 | |
| 628 | 680 | // The block is using the Jetpack_Subscriptions_Widget backend, hence the need to increase the instance count. |
| 629 | 681 | ++Jetpack_Subscriptions_Widget::$instance_count; |
| @@ -638,36 +690,44 @@ | ||
| 638 | 690 | // We want to disencourage including social count as it's misleading. |
| 639 | 691 | $include_social_followers = isset( $attributes['includeSocialFollowers'] ) ? (bool) get_attribute( $attributes, 'includeSocialFollowers' ) : true; |
| 640 | 692 | |
| 641 | 693 | $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( | |
| 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( | |
| 646 | 698 | array( |
| 647 | 699 | 'class' => $classes['block_wrapper'], |
| 648 | 700 | ) |
| 649 | 701 | ), |
| 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( | |
| 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( | |
| 655 | 707 | $attributes, |
| 656 | 708 | 'successMessage', |
| 657 | 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' ) |
| 658 | 710 | ), |
| 659 | - 'show_subscribers_total' => (bool) get_attribute( $attributes, 'showSubscribersTotal' ), | |
| 660 | - 'subscribers_total' => get_subscriber_count( $include_social_followers ), | |
| 661 | - '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( | |
| 662 | 714 | ( is_ssl() ? 'https' : 'http' ) . '://' . ( isset( $_SERVER['HTTP_HOST'] ) ? wp_unslash( $_SERVER['HTTP_HOST'] ) : '' ) . |
| 663 | 715 | ( isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '' ) |
| 664 | 716 | ), |
| 665 | - 'source' => 'subscribe-block', | |
| 666 | - '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 ), | |
| 667 | 722 | ); |
| 668 | 723 | |
| 669 | - 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() ) { | |
| 670 | 730 | return render_for_email( $data, $styles ); |
| 671 | 731 | } |
| 672 | 732 | |
| 673 | 733 | return render_for_website( $data, $classes, $styles ); |
| @@ -696,14 +756,15 @@ | ||
| 696 | 756 | * |
| 697 | 757 | * @return string |
| 698 | 758 | */ |
| 699 | 759 | 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(); | |
| 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; | |
| 706 | 767 | |
| 707 | 768 | // Post ID is used for pulling post-specific paid status, and returning to the right post after confirming subscription |
| 708 | 769 | $post_id = null; |
| 709 | 770 | if ( in_the_loop() ) { |
| @@ -736,9 +797,9 @@ | ||
| 736 | 797 | style="<?php echo esc_attr( $styles['submit_button_wrapper'] ); ?>" |
| 737 | 798 | <?php endif; ?> |
| 738 | 799 | > |
| 739 | 800 | <a |
| 740 | - href="<?php echo esc_url( 'https://wordpress.com/read/site/subscription/' . $blog_id ); ?>" | |
| 801 | + href="<?php echo esc_url( 'https://wordpress.com/reader/site/subscription/' . $blog_id ); ?>" | |
| 741 | 802 | <?php if ( ! empty( $classes['submit_button'] ) ) : ?> |
| 742 | 803 | class="<?php echo esc_attr( $classes['submit_button'] ); ?>" |
| 743 | 804 | <?php endif; ?> |
| 744 | 805 | <?php if ( ! empty( $styles['submit_button'] ) ) : ?> |
| @@ -758,9 +819,9 @@ | ||
| 758 | 819 | data-subscriber_email="<?php echo esc_attr( $data['subscribe_email'] ); ?>" |
| 759 | 820 | id="<?php echo esc_attr( $form_id ); ?>" |
| 760 | 821 | > |
| 761 | 822 | <div class="wp-block-jetpack-subscriptions__form-elements"> |
| 762 | - <?php if ( ! $is_subscribed ) : ?> | |
| 823 | + <?php if ( ! $is_subscribed && ! $is_button_only_style ) : ?> | |
| 763 | 824 | <p id="subscribe-email"> |
| 764 | 825 | <label |
| 765 | 826 | id="<?php echo esc_attr( $subscribe_field_id . '-label' ); ?>" |
| 766 | 827 | for="<?php echo esc_attr( $subscribe_field_id ); ?>" |
| @@ -773,8 +834,9 @@ | ||
| 773 | 834 | '<input |
| 774 | 835 | required="required" |
| 775 | 836 | type="email" |
| 776 | 837 | name="email" |
| 838 | + autocomplete="email" | |
| 777 | 839 | %1$s |
| 778 | 840 | style="%2$s" |
| 779 | 841 | placeholder="%3$s" |
| 780 | 842 | value="%4$s" |
| @@ -821,8 +883,12 @@ | ||
| 821 | 883 | |
| 822 | 884 | if ( ! empty( $tier_id ) ) { |
| 823 | 885 | echo '<input type="hidden" name="tier_id" value="' . esc_attr( $tier_id ) . '"/>'; |
| 824 | 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 | + } | |
| 825 | 891 | ?> |
| 826 | 892 | <button type="submit" |
| 827 | 893 | <?php if ( ! empty( $classes['submit_button'] ) ) : ?> |
| 828 | 894 | class="<?php echo esc_attr( $classes['submit_button'] ); ?>" |
| @@ -876,26 +942,56 @@ | ||
| 876 | 942 | return $html; |
| 877 | 943 | } |
| 878 | 944 | |
| 879 | 945 | /** |
| 880 | - * Filter excerpts looking for subscription data. | |
| 946 | + * WooCommerce Email Editor render callback for the subscriptions block. | |
| 881 | 947 | * |
| 882 | - * @param string $excerpt The extrapolated excerpt string. | |
| 883 | - * @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. | |
| 884 | 951 | * |
| 885 | - * @return mixed | |
| 952 | + * @return string | |
| 886 | 953 | */ |
| 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 | - ); | |
| 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 ''; | |
| 896 | 957 | } |
| 897 | - 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 | + ); | |
| 898 | 994 | } |
| 899 | 995 | |
| 900 | 996 | /** |
| 901 | 997 | * Gate access to posts |
| @@ -920,9 +1016,9 @@ | ||
| 920 | 1016 | } |
| 921 | 1017 | return $the_content; |
| 922 | 1018 | } |
| 923 | 1019 | |
| 924 | - $paywalled_content = get_paywall_content( $post_access_level ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 1020 | + $paywalled_content = get_paywall_content(); | |
| 925 | 1021 | |
| 926 | 1022 | if ( has_block( \Automattic\Jetpack\Extensions\Paywall\BLOCK_NAME ) ) { |
| 927 | 1023 | if ( strpos( $the_content, \Automattic\Jetpack\Extensions\Paywall\BLOCK_HTML ) ) { |
| 928 | 1024 | return strstr( $the_content, \Automattic\Jetpack\Extensions\Paywall\BLOCK_HTML, true ) . $paywalled_content; |
| @@ -1001,12 +1097,11 @@ | ||
| 1001 | 1097 | |
| 1002 | 1098 | /** |
| 1003 | 1099 | * Returns paywall content blocks |
| 1004 | 1100 | * |
| 1005 | - * @param string $post_access_level The newsletter access level. | |
| 1006 | 1101 | * @return string |
| 1007 | 1102 | */ |
| 1008 | -function get_paywall_content( $post_access_level ) { | |
| 1103 | +function get_paywall_content() { | |
| 1009 | 1104 | if ( Jetpack_Memberships::user_is_pending_subscriber() ) { |
| 1010 | 1105 | return get_paywall_blocks_subscribe_pending(); |
| 1011 | 1106 | } |
| 1012 | 1107 | if ( doing_filter( 'get_the_excerpt' ) ) { |
| @@ -1011,9 +1106,9 @@ | ||
| 1011 | 1106 | } |
| 1012 | 1107 | if ( doing_filter( 'get_the_excerpt' ) ) { |
| 1013 | 1108 | return ''; |
| 1014 | 1109 | } |
| 1015 | - return get_paywall_blocks( $post_access_level ); | |
| 1110 | + return get_paywall_blocks(); | |
| 1016 | 1111 | } |
| 1017 | 1112 | |
| 1018 | 1113 | /** |
| 1019 | 1114 | * Returns the current URL. |
| @@ -1095,22 +1190,22 @@ | ||
| 1095 | 1190 | |
| 1096 | 1191 | /** |
| 1097 | 1192 | * Returns paywall content blocks if user is not authenticated |
| 1098 | 1193 | * |
| 1099 | - * @param string $newsletter_access_level The newsletter access level. | |
| 1100 | 1194 | * @return string |
| 1101 | 1195 | */ |
| 1102 | -function get_paywall_blocks( $newsletter_access_level ) { | |
| 1196 | +function get_paywall_blocks() { | |
| 1103 | 1197 | $custom_paywall = apply_filters( 'jetpack_custom_paywall_blocks', false ); |
| 1104 | 1198 | if ( ! empty( $custom_paywall ) ) { |
| 1105 | 1199 | return $custom_paywall; |
| 1106 | 1200 | } |
| 1107 | - if ( ! jetpack_is_frontend() ) { // emails | |
| 1201 | + | |
| 1202 | + if ( ! Request::is_frontend() ) { // emails | |
| 1108 | 1203 | return get_paywall_simple(); |
| 1109 | 1204 | } |
| 1205 | + | |
| 1110 | 1206 | 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(); | |
| 1207 | + $is_paid_post = is_paid_post(); | |
| 1113 | 1208 | $is_paid_subscriber = Jetpack_Memberships::user_is_paid_subscriber(); |
| 1114 | 1209 | |
| 1115 | 1210 | $access_heading = $is_paid_subscriber |
| 1116 | 1211 | ? esc_html__( 'Upgrade to continue reading', 'jetpack' ) |
| @@ -1117,11 +1212,13 @@ | ||
| 1117 | 1212 | : esc_html__( 'Subscribe to continue reading', 'jetpack' ); |
| 1118 | 1213 | |
| 1119 | 1214 | $subscribe_text = $is_paid_post |
| 1120 | 1215 | // 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' ) | |
| 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 | + ) | |
| 1124 | 1221 | // translators: %s is the name of the site. |
| 1125 | 1222 | : esc_html__( 'Subscribe to get access to the rest of this post and other subscriber-only content.', 'jetpack' ); |
| 1126 | 1223 | |
| 1127 | 1224 | $login_block = ''; |
| @@ -1143,9 +1240,9 @@ | ||
| 1143 | 1240 | </p> |
| 1144 | 1241 | <!-- /wp:paragraph -->'; |
| 1145 | 1242 | } |
| 1146 | 1243 | } else { |
| 1147 | - $access_question = get_paywall_access_question( $newsletter_access_level ); | |
| 1244 | + $access_question = $is_paid_post ? esc_html__( 'Already a paid subscriber?', 'jetpack' ) : esc_html__( 'Already a subscriber?', 'jetpack' ); | |
| 1148 | 1245 | $login_block = '<!-- wp:group {"style":{"typography":{"fontSize":"14px"}},"layout":{"type":"flex","justifyContent":"center"}} --> |
| 1149 | 1246 | <div class="wp-block-group" style="font-size:14px"> |
| 1150 | 1247 | <!-- wp:jetpack/subscriber-login {"logInLabel":"' . $access_question . '"} /--> |
| 1151 | 1248 | </div> |
| @@ -1176,33 +1273,8 @@ | ||
| 1176 | 1273 | '; |
| 1177 | 1274 | } |
| 1178 | 1275 | |
| 1179 | 1276 | /** |
| 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 | 1277 | * Returns true if user is auth for subscriptions check, otherwise returns false. |
| 1206 | 1278 | * |
| 1207 | 1279 | * @return bool |
| 1208 | 1280 | */ |
| @@ -1222,8 +1294,36 @@ | ||
| 1222 | 1294 | return false; |
| 1223 | 1295 | } |
| 1224 | 1296 | |
| 1225 | 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 | +/** | |
| 1226 | 1326 | * Returns paywall content blocks when email confirmation is pending |
| 1227 | 1327 | * |
| 1228 | 1328 | * @return string |
| 1229 | 1329 | */ |
| @@ -1264,22 +1364,37 @@ | ||
| 1264 | 1364 | '; |
| 1265 | 1365 | } |
| 1266 | 1366 | |
| 1267 | 1367 | /** |
| 1268 | - * Return content for non frontend views like emails. | |
| 1269 | - * | |
| 1270 | - * @return string | |
| 1368 | + * Return content for non frontend views like Reader, emails. | |
| 1271 | 1369 | */ |
| 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' ); | |
| 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' ); | |
| 1274 | 1375 | |
| 1275 | - $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 | + } | |
| 1276 | 1388 | |
| 1277 | 1389 | return ' |
| 1278 | 1390 | <!-- wp:columns --> |
| 1279 | -<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%"> | |
| 1280 | 1392 | <!-- wp:column --> |
| 1281 | 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 --> | |
| 1282 | 1397 | <!-- wp:paragraph --> |
| 1283 | 1398 | <p class="has-text-align-center" |
| 1284 | 1399 | style="text-align: center; |
| 1285 | 1400 | color: #50575E; |
| @@ -1286,20 +1401,20 @@ | ||
| 1286 | 1401 | font-weight: 400; |
| 1287 | 1402 | font-size: 16px; |
| 1288 | 1403 | font-family: \'SF Pro Text\', sans-serif; |
| 1289 | 1404 | line-height: 28.8px;"> |
| 1290 | - ' . $access_heading . ' | |
| 1405 | + ' . $paywall_description . ' | |
| 1291 | 1406 | </p> |
| 1292 | 1407 | <!-- /wp:paragraph --> |
| 1293 | 1408 | <!-- wp:buttons --> |
| 1294 | 1409 | <div class="wp-block-buttons" style="text-align: center;"> |
| 1295 | 1410 | <!-- wp:button --> |
| 1296 | - <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;"> | |
| 1297 | 1412 | <a href="' . esc_url( get_post_permalink() ) . '" class="wp-block-button__link wp-element-button" |
| 1298 | 1413 | data-wpcom-track data-tracks-link-desc="paywall-email-click" |
| 1299 | 1414 | style="display: inline-block; |
| 1300 | - padding: 15px 20px; | |
| 1301 | - background-color: #0675C4; | |
| 1415 | + padding: 12px 15px; | |
| 1416 | + background-color: #3858e9; | |
| 1302 | 1417 | color: #FFFFFF; |
| 1303 | 1418 | text-decoration: none; |
| 1304 | 1419 | border-radius: 5px; |
| 1305 | 1420 | font-family: \'SF Pro Display\', sans-serif; |
| @@ -1304,9 +1419,9 @@ | ||
| 1304 | 1419 | border-radius: 5px; |
| 1305 | 1420 | font-family: \'SF Pro Display\', sans-serif; |
| 1306 | 1421 | font-weight: 500; |
| 1307 | 1422 | font-size: 16px; |
| 1308 | - text-align: center;">' . $subscribe_text . '</a> | |
| 1423 | + text-align: center;">' . $paywall_action_btn . '</a> | |
| 1309 | 1424 | </div> |
| 1310 | 1425 | <!-- /wp:button --> |
| 1311 | 1426 | </div> |
| 1312 | 1427 | <!-- /wp:buttons --> |