← All changes
|
extensions/blocks/subscriptions/subscriptions.php
+837
-310
12.5.2
→
16.3-a.1
View file →
| @@ -7,17 +7,24 @@ | ||
| 7 | 7 | |
| 8 | 8 | namespace Automattic\Jetpack\Extensions\Subscriptions; |
| 9 | 9 | |
| 10 | 10 | use Automattic\Jetpack\Blocks; |
| 11 | -use Automattic\Jetpack\Connection\Manager as Connection_Manager; | |
| 12 | -use Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Token_Subscription_Service; | |
| 13 | -use Automattic\Jetpack\Status; | |
| 14 | -use Jetpack; | |
| 11 | +use Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Abstract_Token_Subscription_Service; | |
| 12 | +use Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Jetpack_Token_Subscription_Service; | |
| 13 | +use Automattic\Jetpack\Modules; | |
| 14 | +use Automattic\Jetpack\Status\Host; | |
| 15 | +use Automattic\Jetpack\Status\Request; | |
| 15 | 16 | use Jetpack_Gutenberg; |
| 16 | 17 | use Jetpack_Memberships; |
| 17 | 18 | use Jetpack_Subscriptions_Widget; |
| 18 | 19 | |
| 20 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 21 | + exit( 0 ); | |
| 22 | +} | |
| 23 | + | |
| 24 | +require_once __DIR__ . '/class-jetpack-subscription-site.php'; | |
| 19 | 25 | require_once __DIR__ . '/constants.php'; |
| 26 | +require_once JETPACK__PLUGIN_DIR . 'extensions/blocks/premium-content/_inc/subscription-service/include.php'; | |
| 20 | 27 | |
| 21 | 28 | /** |
| 22 | 29 | * These block defaults should match ./constants.js |
| 23 | 30 | */ |
| @@ -25,8 +32,9 @@ | ||
| 25 | 32 | const DEFAULT_BORDER_WEIGHT_VALUE = 1; |
| 26 | 33 | const DEFAULT_FONTSIZE_VALUE = '16px'; |
| 27 | 34 | const DEFAULT_PADDING_VALUE = 15; |
| 28 | 35 | const DEFAULT_SPACING_VALUE = 10; |
| 36 | +const DEFAULT_BUTTON_WIDTH = 'auto'; | |
| 29 | 37 | |
| 30 | 38 | /** |
| 31 | 39 | * Registers the block for use in Gutenberg |
| 32 | 40 | * This is done via an action so that we can disable |
| @@ -40,17 +48,24 @@ | ||
| 40 | 48 | \WPForTeams\is_wpforteams_site( get_current_blog_id() ) ) { |
| 41 | 49 | return; |
| 42 | 50 | } |
| 43 | 51 | |
| 44 | - if ( | |
| 45 | - ( defined( 'IS_WPCOM' ) && IS_WPCOM ) | |
| 46 | - || ( ( new Connection_Manager( 'jetpack' ) )->has_connected_owner() && ! ( new Status() )->is_offline_mode() ) | |
| 47 | - ) { | |
| 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() ) { | |
| 48 | 62 | Blocks::jetpack_register_block( |
| 49 | - BLOCK_NAME, | |
| 63 | + __DIR__, | |
| 50 | 64 | array( |
| 51 | - 'render_callback' => __NAMESPACE__ . '\render_block', | |
| 52 | - 'supports' => array( | |
| 65 | + 'render_callback' => __NAMESPACE__ . '\render_block', | |
| 66 | + 'render_email_callback' => __NAMESPACE__ . '\render_email', | |
| 67 | + 'supports' => array( | |
| 53 | 68 | 'spacing' => array( |
| 54 | 69 | 'margin' => true, |
| 55 | 70 | 'padding' => true, |
| 56 | 71 | ), |
| @@ -59,31 +74,40 @@ | ||
| 59 | 74 | ) |
| 60 | 75 | ); |
| 61 | 76 | } |
| 62 | 77 | |
| 63 | - /* | |
| 64 | - * If the Subscriptions module is not active, | |
| 65 | - * do not make any further changes on the site. | |
| 66 | - */ | |
| 67 | - if ( ! Jetpack::is_module_active( 'subscriptions' ) ) { | |
| 68 | - return; | |
| 69 | - } | |
| 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 | + ); | |
| 70 | 98 | |
| 71 | - /** | |
| 72 | - * Do not proceed if the newsletter feature is not enabled | |
| 73 | - * or if the 'Jetpack_Memberships' class does not exists. | |
| 74 | - */ | |
| 75 | - if ( ! class_exists( '\Jetpack_Memberships' ) ) { | |
| 76 | - return; | |
| 77 | - } | |
| 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 ); | |
| 78 | 101 | |
| 79 | 102 | register_post_meta( |
| 80 | 103 | 'post', |
| 81 | - META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS, | |
| 104 | + META_NAME_FOR_POST_DONT_EMAIL_TO_SUBS, | |
| 82 | 105 | array( |
| 106 | + 'default' => $dont_email_default, | |
| 83 | 107 | 'show_in_rest' => true, |
| 84 | 108 | 'single' => true, |
| 85 | - 'type' => 'string', | |
| 109 | + 'type' => 'boolean', | |
| 86 | 110 | 'auth_callback' => function () { |
| 87 | 111 | return wp_get_current_user()->has_cap( 'edit_posts' ); |
| 88 | 112 | }, |
| 89 | 113 | ) |
| @@ -88,13 +112,48 @@ | ||
| 88 | 112 | }, |
| 89 | 113 | ) |
| 90 | 114 | ); |
| 91 | 115 | |
| 116 | + register_post_meta( | |
| 117 | + 'post', | |
| 118 | + META_NAME_FOR_POST_TIER_ID_SETTINGS, | |
| 119 | + array( | |
| 120 | + 'show_in_rest' => true, | |
| 121 | + 'single' => true, | |
| 122 | + 'type' => 'integer', | |
| 123 | + 'auth_callback' => function () { | |
| 124 | + return wp_get_current_user()->has_cap( 'edit_posts' ); | |
| 125 | + }, | |
| 126 | + ) | |
| 127 | + ); | |
| 128 | + | |
| 129 | + register_post_meta( | |
| 130 | + 'post', | |
| 131 | + META_NAME_CONTAINS_PAYWALLED_CONTENT, | |
| 132 | + array( | |
| 133 | + 'show_in_rest' => true, | |
| 134 | + 'single' => true, | |
| 135 | + 'type' => 'boolean', | |
| 136 | + 'auth_callback' => function () { | |
| 137 | + return wp_get_current_user()->has_cap( 'edit_posts' ); | |
| 138 | + }, | |
| 139 | + ) | |
| 140 | + ); | |
| 141 | + | |
| 92 | 142 | // This ensures Jetpack will sync this post meta to WPCOM. |
| 93 | 143 | add_filter( |
| 94 | 144 | 'jetpack_sync_post_meta_whitelist', |
| 95 | 145 | function ( $allowed_meta ) { |
| 96 | - return array_merge( $allowed_meta, array( META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS ) ); | |
| 146 | + return array_merge( | |
| 147 | + $allowed_meta, | |
| 148 | + array( | |
| 149 | + META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS, | |
| 150 | + META_NAME_FOR_POST_DONT_EMAIL_TO_SUBS, | |
| 151 | + META_NAME_CONTAINS_PAYWALLED_CONTENT, | |
| 152 | + META_NAME_FOR_POST_TIER_ID_SETTINGS, | |
| 153 | + META_NAME_CONTAINS_PAID_CONTENT, | |
| 154 | + ) | |
| 155 | + ); | |
| 97 | 156 | } |
| 98 | 157 | ); |
| 99 | 158 | |
| 100 | 159 | // Hide the content – Priority 8 makes it run before do_blocks gets called for the content |
| @@ -106,14 +165,41 @@ | ||
| 106 | 165 | |
| 107 | 166 | // Hide existing comments |
| 108 | 167 | add_filter( 'get_comment', __NAMESPACE__ . '\maybe_gate_existing_comments' ); |
| 109 | 168 | |
| 110 | - // Gate the excerpt for a post | |
| 111 | - add_filter( 'get_the_excerpt', __NAMESPACE__ . '\jetpack_filter_excerpt_for_newsletter', 10, 2 ); | |
| 169 | + // Add a 'Newsletter' column to the Edit posts page | |
| 170 | + // We only display the "Newsletter" column if we have configured the paid newsletter plan | |
| 171 | + if ( defined( 'WP_ADMIN' ) && WP_ADMIN && Jetpack_Memberships::has_configured_plans_jetpack_recurring_payments( 'newsletter' ) ) { | |
| 172 | + add_action( 'manage_post_posts_columns', __NAMESPACE__ . '\register_newsletter_access_column' ); | |
| 173 | + add_action( 'manage_post_posts_custom_column', __NAMESPACE__ . '\render_newsletter_access_rows', 10, 2 ); | |
| 174 | + add_action( 'admin_head', __NAMESPACE__ . '\newsletter_access_column_styles' ); | |
| 175 | + } | |
| 112 | 176 | |
| 113 | - // Add a 'Newsletter access' column to the Edit posts page | |
| 114 | - add_action( 'manage_post_posts_columns', __NAMESPACE__ . '\register_newsletter_access_column' ); | |
| 115 | - add_action( 'manage_post_posts_custom_column', __NAMESPACE__ . '\render_newsletter_access_rows', 10, 2 ); | |
| 177 | + add_action( 'init', __NAMESPACE__ . '\maybe_prevent_super_cache_caching' ); | |
| 178 | + | |
| 179 | + add_action( 'wp_after_insert_post', __NAMESPACE__ . '\add_paywalled_content_post_meta', 99, 2 ); | |
| 180 | + | |
| 181 | + add_filter( | |
| 182 | + 'jetpack_options_whitelist', | |
| 183 | + function ( $options ) { | |
| 184 | + $options[] = 'jetpack_subscriptions_subscribe_post_end_enabled'; | |
| 185 | + $options[] = 'jetpack_subscriptions_subscribe_navigation_enabled'; | |
| 186 | + | |
| 187 | + return $options; | |
| 188 | + } | |
| 189 | + ); | |
| 190 | + | |
| 191 | + // If called via REST API, we need to register later in the lifecycle | |
| 192 | + if ( ( new Host() )->is_wpcom_platform() && ! Request::is_frontend() ) { | |
| 193 | + add_action( | |
| 194 | + 'restapi_theme_init', | |
| 195 | + function () { | |
| 196 | + Jetpack_Subscription_Site::init()->handle_subscribe_block_placements(); | |
| 197 | + } | |
| 198 | + ); | |
| 199 | + } else { | |
| 200 | + Jetpack_Subscription_Site::init()->handle_subscribe_block_placements(); | |
| 201 | + } | |
| 116 | 202 | } |
| 117 | 203 | add_action( 'init', __NAMESPACE__ . '\register_block', 9 ); |
| 118 | 204 | |
| 119 | 205 | /** |
| @@ -131,15 +217,10 @@ | ||
| 131 | 217 | * @param array $columns An array of column names. |
| 132 | 218 | * @return array An array of column names. |
| 133 | 219 | */ |
| 134 | 220 | function register_newsletter_access_column( $columns ) { |
| 135 | - if ( ! Jetpack_Memberships::has_configured_plans_jetpack_recurring_payments( 'newsletter' ) ) { | |
| 136 | - // We only display the "NL access" column if we have published one paid-newsletter | |
| 137 | - return $columns; | |
| 138 | - } | |
| 139 | - | |
| 140 | 221 | $position = array_search( 'title', array_keys( $columns ), true ); |
| 141 | - $new_column = array( NEWSLETTER_COLUMN_ID => '<span>' . __( 'Newsletter', 'jetpack' ) . '</span>' ); | |
| 222 | + $new_column = array( NEWSLETTER_COLUMN_ID => __( 'Newsletter', 'jetpack' ) ); | |
| 142 | 223 | return array_merge( |
| 143 | 224 | array_slice( $columns, 0, $position + 1, true ), |
| 144 | 225 | $new_column, |
| 145 | 226 | array_slice( $columns, $position, null, true ) |
| @@ -146,8 +227,37 @@ | ||
| 146 | 227 | ); |
| 147 | 228 | } |
| 148 | 229 | |
| 149 | 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 | + $is_paywalled = false; | |
| 245 | + switch ( $access_level ) { | |
| 246 | + case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS_ALL_TIERS: | |
| 247 | + case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS: | |
| 248 | + case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_SUBSCRIBERS: | |
| 249 | + $is_paywalled = true; | |
| 250 | + } | |
| 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 | + } | |
| 257 | +} | |
| 258 | + | |
| 259 | +/** | |
| 150 | 260 | * Displays the newsletter access level. |
| 151 | 261 | * |
| 152 | 262 | * @param string $column_id The ID of the column to display. |
| 153 | 263 | * @param int $post_id The current post ID. |
| @@ -159,15 +269,18 @@ | ||
| 159 | 269 | |
| 160 | 270 | $access_level = get_post_meta( $post_id, META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS, true ); |
| 161 | 271 | |
| 162 | 272 | switch ( $access_level ) { |
| 163 | - case Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS: | |
| 273 | + case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS_ALL_TIERS: | |
| 274 | + echo esc_html__( 'Paid Subscribers (all plans)', 'jetpack' ); | |
| 275 | + break; | |
| 276 | + case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS: | |
| 164 | 277 | echo esc_html__( 'Paid Subscribers', 'jetpack' ); |
| 165 | 278 | break; |
| 166 | - case Token_Subscription_Service::POST_ACCESS_LEVEL_SUBSCRIBERS: | |
| 279 | + case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_SUBSCRIBERS: | |
| 167 | 280 | echo esc_html__( 'Subscribers', 'jetpack' ); |
| 168 | 281 | break; |
| 169 | - case Token_Subscription_Service::POST_ACCESS_LEVEL_EVERYBODY: | |
| 282 | + case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_EVERYBODY: | |
| 170 | 283 | echo esc_html__( 'Everybody', 'jetpack' ); |
| 171 | 284 | break; |
| 172 | 285 | default: |
| 173 | 286 | echo ''; |
| @@ -174,11 +287,18 @@ | ||
| 174 | 287 | } |
| 175 | 288 | } |
| 176 | 289 | |
| 177 | 290 | /** |
| 178 | - * Determine the amount of folks currently subscribed to the blog, splitted out in email_subscribers & social_followers & paid_subscribers | |
| 291 | + * Adds the Newsletter column styles | |
| 292 | + */ | |
| 293 | +function newsletter_access_column_styles() { | |
| 294 | + echo '<style id="jetpack-newsletter-newsletter-access-column"> table.fixed .column-newsletter_access { width: 10%; } </style>'; | |
| 295 | +} | |
| 296 | + | |
| 297 | +/** | |
| 298 | + * Determine the amount of folks currently subscribed to the blog, splitted out in total_subscribers, email_subscribers, social_followers & paid_subscribers. | |
| 179 | 299 | * |
| 180 | - * @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]] | |
| 181 | 301 | */ |
| 182 | 302 | function fetch_subscriber_counts() { |
| 183 | 303 | $subs_count = 0; |
| 184 | 304 | if ( is_wpcom() ) { |
| @@ -196,9 +316,10 @@ | ||
| 196 | 316 | $subs_count = array( |
| 197 | 317 | 'status' => 'failed', |
| 198 | 318 | 'code' => $xml->getErrorCode(), |
| 199 | 319 | 'message' => $xml->getErrorMessage(), |
| 200 | - 'value' => ( isset( $subs_count['value'] ) ) ? $subs_count['value'] : array( | |
| 320 | + 'value' => $subs_count['value'] ?? array( | |
| 321 | + 'total_subscribers' => 0, | |
| 201 | 322 | 'email_subscribers' => 0, |
| 202 | 323 | 'social_followers' => 0, |
| 203 | 324 | 'paid_subscribers' => 0, |
| 204 | 325 | ), |
| @@ -224,11 +345,11 @@ | ||
| 224 | 345 | function get_subscriber_count( $include_social_followers ) { |
| 225 | 346 | $counts = fetch_subscriber_counts(); |
| 226 | 347 | |
| 227 | 348 | if ( $include_social_followers ) { |
| 228 | - $subscriber_count = $counts['value']['email_subscribers'] + $counts['value']['social_followers']; | |
| 349 | + $subscriber_count = $counts['value']['total_subscribers'] + $counts['value']['social_followers']; | |
| 229 | 350 | } else { |
| 230 | - $subscriber_count = $counts['value']['email_subscribers']; | |
| 351 | + $subscriber_count = $counts['value']['total_subscribers']; | |
| 231 | 352 | } |
| 232 | 353 | return $subscriber_count; |
| 233 | 354 | } |
| 234 | 355 | |
| @@ -331,8 +452,24 @@ | ||
| 331 | 452 | ); |
| 332 | 453 | } |
| 333 | 454 | |
| 334 | 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 | +/** | |
| 335 | 472 | * Uses block attributes to generate an array containing the styles for various block elements. |
| 336 | 473 | * Based on Jetpack_Subscriptions_Widget::do_subscription_form() which the block was originally using. |
| 337 | 474 | * |
| 338 | 475 | * @param array $attributes Array containing the block attributes. |
| @@ -339,8 +476,10 @@ | ||
| 339 | 476 | * |
| 340 | 477 | * @return array |
| 341 | 478 | */ |
| 342 | 479 | function get_element_styles_from_attributes( $attributes ) { |
| 480 | + $is_button_only_style = is_button_only_style( get_attribute( $attributes, 'className', '' ) ); | |
| 481 | + | |
| 343 | 482 | $button_background_style = ! has_attribute( $attributes, 'buttonBackgroundColor' ) && has_attribute( $attributes, 'customButtonGradient' ) |
| 344 | 483 | ? get_attribute( $attributes, 'customButtonGradient' ) |
| 345 | 484 | : get_attribute( $attributes, 'customButtonBackgroundColor' ); |
| 346 | 485 | |
| @@ -356,15 +495,15 @@ | ||
| 356 | 495 | $submit_button_styles .= sprintf( 'color: %s;', get_attribute( $attributes, 'customTextColor' ) ); |
| 357 | 496 | } |
| 358 | 497 | |
| 359 | 498 | if ( has_attribute( $attributes, 'buttonWidth' ) ) { |
| 360 | - $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 ) ); | |
| 361 | 500 | $submit_button_wrapper_styles .= 'max-width: 100%;'; |
| 362 | 501 | |
| 363 | 502 | // Account for custom margins on inline forms. |
| 364 | 503 | $submit_button_styles .= true === get_attribute( $attributes, 'buttonOnNewLine' ) |
| 365 | - ? sprintf( 'width: calc(100%% - %dpx);', get_attribute( $attributes, 'spacing', DEFAULT_SPACING_VALUE ) ) | |
| 366 | - : 'width: 100%;'; | |
| 504 | + ? 'width: 100%;' | |
| 505 | + : sprintf( 'width: calc(100%% - %dpx);', get_attribute( $attributes, 'spacing', DEFAULT_SPACING_VALUE ) ); | |
| 367 | 506 | } |
| 368 | 507 | |
| 369 | 508 | $font_size = get_attribute( $attributes, 'customFontSize', DEFAULT_FONTSIZE_VALUE ); |
| 370 | 509 | $style = sprintf( 'font-size: %s%s;', $font_size, is_numeric( $font_size ) ? 'px' : '' ); |
| @@ -377,14 +516,16 @@ | ||
| 377 | 516 | |
| 378 | 517 | $submit_button_styles .= $style; |
| 379 | 518 | $email_field_styles .= $style; |
| 380 | 519 | |
| 381 | - $button_spacing = get_attribute( $attributes, 'spacing', DEFAULT_SPACING_VALUE ); | |
| 382 | - if ( true === get_attribute( $attributes, 'buttonOnNewLine' ) ) { | |
| 383 | - $submit_button_styles .= sprintf( 'margin-top: %dpx;', $button_spacing ); | |
| 384 | - } else { | |
| 385 | - $submit_button_styles .= 'margin: 0px; '; // Reset Safari's 2px default margin for buttons affecting input and button union | |
| 386 | - $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 | + } | |
| 387 | 528 | } |
| 388 | 529 | |
| 389 | 530 | if ( has_attribute( $attributes, 'borderColor' ) ) { |
| 390 | 531 | $style = sprintf( 'border-color: %s;', get_attribute( $attributes, 'borderColor', '' ) ); |
| @@ -406,8 +547,14 @@ | ||
| 406 | 547 | $submit_button_styles .= $style; |
| 407 | 548 | $email_field_styles .= $style; |
| 408 | 549 | } |
| 409 | 550 | |
| 551 | + if ( ! Request::is_frontend() ) { | |
| 552 | + $background_color_style = get_attribute_color( 'buttonBackgroundColor', $attributes, '#113AF5' /* default lettre theme color */ ); | |
| 553 | + $text_color_style = get_attribute_color( 'textColor', $attributes, '#FFFFFF' ); | |
| 554 | + $submit_button_styles .= sprintf( ' background-color: %s; color: %s;', $background_color_style, $text_color_style ); | |
| 555 | + } | |
| 556 | + | |
| 410 | 557 | return array( |
| 411 | 558 | 'email_field' => $email_field_styles, |
| 412 | 559 | 'submit_button' => $submit_button_styles, |
| 413 | 560 | 'submit_button_wrapper' => $submit_button_wrapper_styles, |
| @@ -414,8 +561,88 @@ | ||
| 414 | 561 | ); |
| 415 | 562 | } |
| 416 | 563 | |
| 417 | 564 | /** |
| 565 | + * Retrieve the resolved color for a given attribute. | |
| 566 | + * | |
| 567 | + * @param string $attribute_name The name of the attribute to resolve. | |
| 568 | + * @param array $attributes An array of all attributes. | |
| 569 | + * @param string $default_color A fallback color in case no color can be resolved. | |
| 570 | + * | |
| 571 | + * @return string Returns the resolved color or the default color if no color is found. | |
| 572 | + */ | |
| 573 | +function get_attribute_color( $attribute_name, $attributes, $default_color ) { | |
| 574 | + if ( has_attribute( $attributes, $attribute_name ) ) { | |
| 575 | + $color_slug = get_attribute( $attributes, $attribute_name ); | |
| 576 | + $resolved_color = get_color_from_slug( $color_slug ); | |
| 577 | + | |
| 578 | + if ( $resolved_color ) { | |
| 579 | + return $resolved_color; | |
| 580 | + } | |
| 581 | + } | |
| 582 | + | |
| 583 | + return get_global_style_color( $attribute_name, $default_color ); | |
| 584 | +} | |
| 585 | + | |
| 586 | +/** | |
| 587 | + * Retrieve the global style color based on a provided style key. | |
| 588 | + * | |
| 589 | + * @param string $style_key The key for the desired style. | |
| 590 | + * @param string $default_color A fallback color in case the global style is not set. | |
| 591 | + * | |
| 592 | + * @return string Returns the color defined in global styles or the default color if not defined. | |
| 593 | + */ | |
| 594 | +function get_global_style_color( $style_key, $default_color ) { | |
| 595 | + $global_styles = wp_get_global_styles( | |
| 596 | + array( 'color' ), | |
| 597 | + array( | |
| 598 | + 'block_name' => 'core/button', | |
| 599 | + 'transforms' => array( 'resolve-variables' ), | |
| 600 | + ) | |
| 601 | + ); | |
| 602 | + | |
| 603 | + if ( isset( $global_styles[ $style_key ] ) ) { | |
| 604 | + return $global_styles[ $style_key ]; | |
| 605 | + } | |
| 606 | + | |
| 607 | + return $default_color; | |
| 608 | +} | |
| 609 | + | |
| 610 | +/** | |
| 611 | + * Convert a color slug into its corresponding color value. | |
| 612 | + * | |
| 613 | + * @param string $slug The slug representation of the color. | |
| 614 | + * | |
| 615 | + * @return string|null Returns the color value if found, or null otherwise. | |
| 616 | + */ | |
| 617 | +function get_color_from_slug( $slug ) { | |
| 618 | + $color_palettes = wp_get_global_settings( array( 'color', 'palette' ) ); | |
| 619 | + | |
| 620 | + if ( ! is_array( $color_palettes ) ) { | |
| 621 | + return null; | |
| 622 | + } | |
| 623 | + | |
| 624 | + foreach ( $color_palettes as $palette ) { | |
| 625 | + if ( is_array( $palette ) ) { | |
| 626 | + foreach ( $palette as $color ) { | |
| 627 | + if ( isset( $color['slug'] ) && $color['slug'] === $slug && isset( $color['color'] ) ) { | |
| 628 | + return $color['color']; | |
| 629 | + } | |
| 630 | + } | |
| 631 | + } | |
| 632 | + } | |
| 633 | + | |
| 634 | + return null; | |
| 635 | +} | |
| 636 | + | |
| 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 | +/** | |
| 418 | 645 | * Subscriptions block render callback. |
| 419 | 646 | * |
| 420 | 647 | * @param array $attributes Array containing the block attributes. |
| 421 | 648 | * |
| @@ -422,67 +649,88 @@ | ||
| 422 | 649 | * @return string |
| 423 | 650 | */ |
| 424 | 651 | function render_block( $attributes ) { |
| 425 | 652 | // If the Subscriptions module is not active, don't render the block. |
| 426 | - if ( ! Jetpack::is_module_active( 'subscriptions' ) ) { | |
| 653 | + if ( ! ( new Modules() )->is_active( 'subscriptions' ) ) { | |
| 427 | 654 | return ''; |
| 428 | 655 | } |
| 429 | 656 | |
| 430 | - if ( class_exists( '\Jetpack_Memberships' ) ) { | |
| 431 | - // We only want the sites that have newsletter feature enabled to be graced by this JavaScript and thickbox. | |
| 432 | - Jetpack_Gutenberg::load_assets_as_required( FEATURE_NAME, array( 'thickbox' ) ); | |
| 433 | - if ( ! wp_style_is( 'enqueued' ) ) { | |
| 434 | - wp_enqueue_style( 'thickbox' ); | |
| 435 | - } | |
| 657 | + if ( is_jetpack_memberships_loaded() ) { | |
| 658 | + // We only want the sites that have newsletter feature enabled to be graced by this JavaScript. | |
| 659 | + Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); | |
| 436 | 660 | } else { |
| 437 | 661 | Jetpack_Gutenberg::load_styles_as_required( FEATURE_NAME ); |
| 438 | 662 | } |
| 439 | 663 | |
| 440 | - $subscribe_email = ''; | |
| 664 | + if ( ! class_exists( 'Jetpack_Subscriptions_Widget' ) ) { | |
| 665 | + return ''; | |
| 666 | + } | |
| 441 | 667 | |
| 442 | - /** This filter is documented in modules/contact-form/grunion-contact-form.php */ | |
| 443 | - if ( is_wpcom() || false !== apply_filters( 'jetpack_auto_fill_logged_in_user', false ) ) { | |
| 444 | - $current_user = wp_get_current_user(); | |
| 445 | - $subscribe_email = ! empty( $current_user->user_email ) ? $current_user->user_email : ''; | |
| 668 | + // Prefill the email field with the current user's email if they are logged in via Memberships premium content token | |
| 669 | + $subscribe_email = Jetpack_Memberships::get_current_user_email(); | |
| 670 | + | |
| 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 | + } | |
| 446 | 677 | } |
| 447 | 678 | |
| 448 | 679 | // The block is using the Jetpack_Subscriptions_Widget backend, hence the need to increase the instance count. |
| 449 | 680 | ++Jetpack_Subscriptions_Widget::$instance_count; |
| 450 | 681 | |
| 451 | - $classes = get_element_class_names_from_attributes( $attributes ); | |
| 452 | - $styles = get_element_styles_from_attributes( $attributes ); | |
| 682 | + $classes = get_element_class_names_from_attributes( $attributes ); | |
| 683 | + $styles = get_element_styles_from_attributes( $attributes ); | |
| 684 | + | |
| 685 | + // The default value was previously "true" in block.json. We don't want to rely setting "default" in block.json to falsy, | |
| 686 | + // because it would change the setting for previously saved blocks. Block editor doesn't store default values in attributes at all. | |
| 687 | + // Hence users without this set will still get social counts included in the subscriber counter. | |
| 688 | + // Lowering the subscriber count on their behalf with code change would be controversial. | |
| 689 | + // We want to disencourage including social count as it's misleading. | |
| 453 | 690 | $include_social_followers = isset( $attributes['includeSocialFollowers'] ) ? (bool) get_attribute( $attributes, 'includeSocialFollowers' ) : true; |
| 454 | 691 | |
| 455 | 692 | $data = array( |
| 456 | - 'widget_id' => Jetpack_Subscriptions_Widget::$instance_count, | |
| 457 | - 'subscribe_email' => $subscribe_email, | |
| 458 | - | |
| 459 | - '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( | |
| 460 | 697 | array( |
| 461 | 698 | 'class' => $classes['block_wrapper'], |
| 462 | 699 | ) |
| 463 | 700 | ), |
| 464 | - 'subscribe_placeholder' => get_attribute( $attributes, 'subscribePlaceholder', esc_html__( 'Type your email…', 'jetpack' ) ), | |
| 465 | - 'submit_button_text' => get_attribute( $attributes, 'submitButtonText', esc_html__( 'Subscribe', 'jetpack' ) ), | |
| 466 | - '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( | |
| 467 | 706 | $attributes, |
| 468 | 707 | 'successMessage', |
| 469 | - 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' ) | |
| 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' ) | |
| 470 | 709 | ), |
| 471 | - 'show_subscribers_total' => (bool) get_attribute( $attributes, 'showSubscribersTotal' ), | |
| 472 | - 'subscribers_total' => get_subscriber_count( $include_social_followers ), | |
| 473 | - '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( | |
| 474 | 713 | ( is_ssl() ? 'https' : 'http' ) . '://' . ( isset( $_SERVER['HTTP_HOST'] ) ? wp_unslash( $_SERVER['HTTP_HOST'] ) : '' ) . |
| 475 | 714 | ( isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '' ) |
| 476 | 715 | ), |
| 477 | - 'source' => 'subscribe-block', | |
| 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 ), | |
| 478 | 721 | ); |
| 479 | 722 | |
| 480 | - if ( is_wpcom() ) { | |
| 481 | - return render_wpcom_subscribe_form( $data, $classes, $styles ); | |
| 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() ) { | |
| 729 | + return render_for_email( $data, $styles ); | |
| 482 | 730 | } |
| 483 | 731 | |
| 484 | - return render_jetpack_subscribe_form( $data, $classes, $styles ); | |
| 732 | + return render_for_website( $data, $classes, $styles ); | |
| 485 | 733 | } |
| 486 | 734 | |
| 487 | 735 | /** |
| 488 | 736 | * Get the post access level for the current post. Defaults to 'everybody' if the query is not for a single post |
| @@ -491,9 +739,9 @@ | ||
| 491 | 739 | */ |
| 492 | 740 | function get_post_access_level_for_current_post() { |
| 493 | 741 | if ( ! is_singular() ) { |
| 494 | 742 | // There is no "actual" current post. |
| 495 | - return Token_Subscription_Service::POST_ACCESS_LEVEL_EVERYBODY; | |
| 743 | + return Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_EVERYBODY; | |
| 496 | 744 | } |
| 497 | 745 | |
| 498 | 746 | return Jetpack_Memberships::get_post_access_level(); |
| 499 | 747 | } |
| @@ -498,9 +746,9 @@ | ||
| 498 | 746 | return Jetpack_Memberships::get_post_access_level(); |
| 499 | 747 | } |
| 500 | 748 | |
| 501 | 749 | /** |
| 502 | - * Renders the WP.com version of the subscriptions block. | |
| 750 | + * Renders the subscriptions block at the site. | |
| 503 | 751 | * |
| 504 | 752 | * @param array $data Array containing block view data. |
| 505 | 753 | * @param array $classes Array containing the classes for different block elements. |
| 506 | 754 | * @param array $styles Array containing the styles for different block elements. |
| @@ -506,14 +754,33 @@ | ||
| 506 | 754 | * @param array $styles Array containing the styles for different block elements. |
| 507 | 755 | * |
| 508 | 756 | * @return string |
| 509 | 757 | */ |
| 510 | -function render_wpcom_subscribe_form( $data, $classes, $styles ) { | |
| 511 | - global $current_blog; | |
| 758 | +function render_for_website( $data, $classes, $styles ) { | |
| 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; | |
| 512 | 766 | |
| 513 | - $form_id = 'subscribe-blog' . ( Jetpack_Subscriptions_Widget::$instance_count > 1 ? '-' . Jetpack_Subscriptions_Widget::$instance_count : '' ); | |
| 514 | - $url = defined( 'SUBSCRIBE_BLOG_URL' ) ? SUBSCRIBE_BLOG_URL : ''; | |
| 767 | + // Post ID is used for pulling post-specific paid status, and returning to the right post after confirming subscription | |
| 768 | + $post_id = null; | |
| 769 | + if ( in_the_loop() ) { | |
| 770 | + $post_id = get_the_ID(); | |
| 771 | + } elseif ( is_singular( 'post' ) || is_page() ) { | |
| 772 | + $post_id = get_queried_object_id(); | |
| 773 | + } else { | |
| 774 | + $post_id = get_option( 'page_on_front' ); | |
| 775 | + } | |
| 515 | 776 | |
| 777 | + $subscribe_field_id = apply_filters( 'subscribe_field_id', 'subscribe-field' . $widget_id_suffix, $data['widget_id'] ); | |
| 778 | + $tier_id = get_post_meta( $post_id, META_NAME_FOR_POST_TIER_ID_SETTINGS, true ); | |
| 779 | + $is_subscribed = Jetpack_Memberships::is_current_user_subscribed(); | |
| 780 | + $button_text = get_submit_button_text( $data ); | |
| 781 | + $show_subscriber_count = $data['show_subscribers_total'] && $data['subscribers_total'] && ! $is_subscribed; | |
| 782 | + | |
| 516 | 783 | ob_start(); |
| 517 | 784 | |
| 518 | 785 | Jetpack_Subscriptions_Widget::render_widget_status_messages( |
| 519 | 786 | array( |
| @@ -519,231 +786,211 @@ | ||
| 519 | 786 | array( |
| 520 | 787 | 'success_message' => $data['success_message'], |
| 521 | 788 | ) |
| 522 | 789 | ); |
| 523 | - | |
| 524 | - $post_access_level = get_post_access_level_for_current_post(); | |
| 525 | - | |
| 526 | 790 | ?> |
| 527 | 791 | <div <?php echo wp_kses_data( $data['wrapper_attributes'] ); ?>> |
| 528 | - <div class="wp-block-jetpack-subscriptions__container"> | |
| 529 | - <form | |
| 530 | - action="<?php echo esc_url( $url ); ?>" | |
| 531 | - method="post" | |
| 532 | - accept-charset="utf-8" | |
| 533 | - data-blog="<?php echo esc_attr( get_current_blog_id() ); ?>" | |
| 534 | - data-post_access_level="<?php echo esc_attr( $post_access_level ); ?>" | |
| 535 | - id="<?php echo esc_attr( $form_id ); ?>" | |
| 536 | - > | |
| 537 | - <?php | |
| 538 | - $email_field_id = 'subscribe-field'; | |
| 539 | - $email_field_id .= Jetpack_Subscriptions_Widget::$instance_count > 1 | |
| 540 | - ? '-' . Jetpack_Subscriptions_Widget::$instance_count | |
| 541 | - : ''; | |
| 542 | - $label_field_id = $email_field_id . '-label'; | |
| 543 | - ?> | |
| 544 | - <p id="subscribe-email"> | |
| 545 | - <label | |
| 546 | - id="<?php echo esc_attr( $label_field_id ); ?>" | |
| 547 | - for="<?php echo esc_attr( $email_field_id ); ?>" | |
| 548 | - class="screen-reader-text" | |
| 549 | - > | |
| 550 | - <?php echo esc_html( $data['subscribe_placeholder'] ); ?> | |
| 551 | - </label> | |
| 552 | - | |
| 553 | - <?php | |
| 554 | - printf( | |
| 555 | - '<input | |
| 556 | - required="required" | |
| 557 | - type="email" | |
| 558 | - name="email" | |
| 559 | - %1$s | |
| 560 | - style="%2$s" | |
| 561 | - placeholder="%3$s" | |
| 562 | - value="%4$s" | |
| 563 | - id="%5$s" | |
| 564 | - />', | |
| 565 | - ( ! empty( $classes['email_field'] ) | |
| 566 | - ? 'class="' . esc_attr( $classes['email_field'] ) . '"' | |
| 567 | - : '' | |
| 568 | - ), | |
| 569 | - ( ! empty( $styles['email_field'] ) | |
| 570 | - ? esc_attr( $styles['email_field'] ) | |
| 571 | - : 'width: 95%; padding: 1px 10px' | |
| 572 | - ), | |
| 573 | - esc_attr( $data['subscribe_placeholder'] ), | |
| 574 | - esc_attr( $data['subscribe_email'] ), | |
| 575 | - esc_attr( $email_field_id ) | |
| 576 | - ); | |
| 577 | - ?> | |
| 578 | - </p> | |
| 579 | - | |
| 580 | - <p id="subscribe-submit" | |
| 792 | + <div class="wp-block-jetpack-subscriptions__container<?php echo ! $is_subscribed ? ' is-not-subscriber' : ''; ?>"> | |
| 793 | + <?php if ( is_top_subscription() ) : ?> | |
| 794 | + <p id="subscribe-submit" class="is-link" | |
| 581 | 795 | <?php if ( ! empty( $styles['submit_button_wrapper'] ) ) : ?> |
| 582 | 796 | style="<?php echo esc_attr( $styles['submit_button_wrapper'] ); ?>" |
| 583 | 797 | <?php endif; ?> |
| 584 | 798 | > |
| 585 | - <input type="hidden" name="action" value="subscribe"/> | |
| 586 | - <input type="hidden" name="blog_id" value="<?php echo (int) $current_blog->blog_id; ?>"/> | |
| 587 | - <input type="hidden" name="source" value="<?php echo esc_url( $data['referer'] ); ?>"/> | |
| 588 | - <input type="hidden" name="sub-type" value="<?php echo esc_attr( $data['source'] ); ?>"/> | |
| 589 | - <input type="hidden" name="redirect_fragment" value="<?php echo esc_attr( $form_id ); ?>"/> | |
| 590 | - <?php wp_nonce_field( 'blogsub_subscribe_' . $current_blog->blog_id, '_wpnonce', false ); ?> | |
| 591 | - <button type="submit" | |
| 592 | - <?php if ( ! empty( $classes['submit_button'] ) ) : ?> | |
| 593 | - class="<?php echo esc_attr( $classes['submit_button'] ); ?>" | |
| 799 | + <a | |
| 800 | + href="<?php echo esc_url( 'https://wordpress.com/reader/site/subscription/' . $blog_id ); ?>" | |
| 801 | + <?php if ( ! empty( $classes['submit_button'] ) ) : ?> | |
| 802 | + class="<?php echo esc_attr( $classes['submit_button'] ); ?>" | |
| 803 | + <?php endif; ?> | |
| 804 | + <?php if ( ! empty( $styles['submit_button'] ) ) : ?> | |
| 805 | + style="<?php echo esc_attr( $styles['submit_button'] ); ?>" | |
| 806 | + <?php endif; ?> | |
| 807 | + > | |
| 808 | + <?php echo sanitize_submit_text( $button_text ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> | |
| 809 | + </a> | |
| 810 | + </p> | |
| 811 | + <?php else : ?> | |
| 812 | + <form | |
| 813 | + action="<?php echo esc_url( $form_url ); ?>" | |
| 814 | + method="post" | |
| 815 | + accept-charset="utf-8" | |
| 816 | + data-blog="<?php echo esc_attr( $blog_id ); ?>" | |
| 817 | + data-post_access_level="<?php echo esc_attr( $post_access_level ); ?>" | |
| 818 | + data-subscriber_email="<?php echo esc_attr( $data['subscribe_email'] ); ?>" | |
| 819 | + id="<?php echo esc_attr( $form_id ); ?>" | |
| 820 | + > | |
| 821 | + <div class="wp-block-jetpack-subscriptions__form-elements"> | |
| 822 | + <?php if ( ! $is_subscribed && ! $is_button_only_style ) : ?> | |
| 823 | + <p id="subscribe-email"> | |
| 824 | + <label | |
| 825 | + id="<?php echo esc_attr( $subscribe_field_id . '-label' ); ?>" | |
| 826 | + for="<?php echo esc_attr( $subscribe_field_id ); ?>" | |
| 827 | + class="screen-reader-text" | |
| 828 | + > | |
| 829 | + <?php echo esc_html( $data['subscribe_placeholder'] ); ?> | |
| 830 | + </label> | |
| 831 | + <?php | |
| 832 | + printf( | |
| 833 | + '<input | |
| 834 | + required="required" | |
| 835 | + type="email" | |
| 836 | + name="email" | |
| 837 | + autocomplete="email" | |
| 838 | + %1$s | |
| 839 | + style="%2$s" | |
| 840 | + placeholder="%3$s" | |
| 841 | + value="%4$s" | |
| 842 | + id="%5$s" | |
| 843 | + %6$s | |
| 844 | + />', | |
| 845 | + ( ! empty( $classes['email_field'] ) | |
| 846 | + ? 'class="' . esc_attr( $classes['email_field'] ) . '"' | |
| 847 | + : '' | |
| 848 | + ), | |
| 849 | + ( ! empty( $styles['email_field'] ) | |
| 850 | + ? esc_attr( $styles['email_field'] ) | |
| 851 | + : 'width: 95%; padding: 1px 10px' | |
| 852 | + ), | |
| 853 | + esc_attr( $data['subscribe_placeholder'] ), | |
| 854 | + esc_attr( $data['subscribe_email'] ), | |
| 855 | + esc_attr( $subscribe_field_id ), | |
| 856 | + ( ! empty( $data['subscribe_email'] ) | |
| 857 | + ? 'disabled title="' . esc_attr__( "You're logged in with this email", 'jetpack' ) . '"' | |
| 858 | + : 'title="' . esc_attr__( 'Please fill in this field.', 'jetpack' ) . '"' | |
| 859 | + ) | |
| 860 | + ); | |
| 861 | + ?> | |
| 862 | + </p> | |
| 594 | 863 | <?php endif; ?> |
| 595 | - <?php if ( ! empty( $styles['submit_button'] ) ) : ?> | |
| 596 | - style="<?php echo esc_attr( $styles['submit_button'] ); ?>" | |
| 597 | - <?php endif; ?> | |
| 598 | - > | |
| 599 | - <?php | |
| 600 | - echo wp_kses( | |
| 601 | - html_entity_decode( $data['submit_button_text'], ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ), | |
| 602 | - Jetpack_Subscriptions_Widget::$allowed_html_tags_for_submit_button | |
| 603 | - ); | |
| 604 | - ?> | |
| 605 | - </button> | |
| 606 | - </p> | |
| 607 | - </form> | |
| 608 | - <?php if ( $data['show_subscribers_total'] && $data['subscribers_total'] ) : ?> | |
| 864 | + <p id="subscribe-submit" | |
| 865 | + <?php if ( ! empty( $styles['submit_button_wrapper'] ) ) : ?> | |
| 866 | + style="<?php echo esc_attr( $styles['submit_button_wrapper'] ); ?>" | |
| 867 | + <?php endif; ?> | |
| 868 | + > | |
| 869 | + <input type="hidden" name="action" value="subscribe"/> | |
| 870 | + <input type="hidden" name="blog_id" value="<?php echo (int) $blog_id; ?>"/> | |
| 871 | + <input type="hidden" name="source" value="<?php echo esc_url( $data['referer'] ); ?>"/> | |
| 872 | + <input type="hidden" name="sub-type" value="<?php echo esc_attr( $data['source'] ); ?>"/> | |
| 873 | + <input type="hidden" name="app_source" value="<?php echo esc_attr( $data['app_source'] ); ?>"/> | |
| 874 | + <input type="hidden" name="redirect_fragment" value="<?php echo esc_attr( $form_id ); ?>"/> | |
| 875 | + <input type="hidden" name="lang" value="<?php echo esc_attr( $lang ); ?>"/> | |
| 876 | + <?php | |
| 877 | + wp_nonce_field( 'blogsub_subscribe_' . $blog_id ); | |
| 878 | + | |
| 879 | + if ( ! empty( $post_id ) ) { | |
| 880 | + echo '<input type="hidden" name="post_id" value="' . esc_attr( $post_id ) . '"/>'; | |
| 881 | + } | |
| 882 | + | |
| 883 | + if ( ! empty( $tier_id ) ) { | |
| 884 | + echo '<input type="hidden" name="tier_id" value="' . esc_attr( $tier_id ) . '"/>'; | |
| 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 | + } | |
| 890 | + ?> | |
| 891 | + <button type="submit" | |
| 892 | + <?php if ( ! empty( $classes['submit_button'] ) ) : ?> | |
| 893 | + class="<?php echo esc_attr( $classes['submit_button'] ); ?>" | |
| 894 | + <?php endif; ?> | |
| 895 | + <?php if ( ! empty( $styles['submit_button'] ) ) : ?> | |
| 896 | + style="<?php echo esc_attr( $styles['submit_button'] ); ?>" | |
| 897 | + <?php endif; ?> | |
| 898 | + name="jetpack_subscriptions_widget" | |
| 899 | + > | |
| 900 | + <?php echo sanitize_submit_text( $button_text ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> | |
| 901 | + </button> | |
| 902 | + </p> | |
| 903 | + </div> | |
| 904 | + </form> | |
| 905 | + <?php endif; ?> | |
| 906 | + <?php if ( $show_subscriber_count ) : ?> | |
| 609 | 907 | <div class="wp-block-jetpack-subscriptions__subscount"> |
| 610 | - <?php | |
| 611 | - /* translators: %s: number of folks following the blog */ | |
| 612 | - echo esc_html( sprintf( _n( 'Join %s other follower', 'Join %s other followers', $data['subscribers_total'], 'jetpack' ), number_format_i18n( $data['subscribers_total'] ) ) ); | |
| 613 | - ?> | |
| 908 | + <?php echo esc_html( Jetpack_Memberships::get_join_others_text( $data['subscribers_total'] ) ); ?> | |
| 614 | 909 | </div> |
| 615 | 910 | <?php endif; ?> |
| 616 | 911 | </div> |
| 617 | 912 | </div> |
| 618 | 913 | <?php |
| 619 | - | |
| 620 | 914 | return ob_get_clean(); |
| 621 | 915 | } |
| 622 | 916 | |
| 623 | 917 | /** |
| 624 | - * Renders the Jetpack version of the subscriptions block. | |
| 918 | + * Renders the email version of the subscriptions block. | |
| 625 | 919 | * |
| 626 | 920 | * @param array $data Array containing block view data. |
| 627 | - * @param array $classes Array containing the classes for different block elements. | |
| 628 | 921 | * @param array $styles Array containing the styles for different block elements. |
| 629 | 922 | * |
| 630 | 923 | * @return string |
| 631 | 924 | */ |
| 632 | -function render_jetpack_subscribe_form( $data, $classes, $styles ) { | |
| 633 | - $form_id = sprintf( 'subscribe-blog-%s', $data['widget_id'] ); | |
| 634 | - $subscribe_field_id = apply_filters( 'subscribe_field_id', 'subscribe-field', $data['widget_id'] ); | |
| 635 | - ob_start(); | |
| 925 | +function render_for_email( $data, $styles ) { | |
| 926 | + $submit_button_wrapper_style = ! empty( $styles['submit_button_wrapper'] ) ? 'style="' . esc_attr( $styles['submit_button_wrapper'] ) . '"' : ''; | |
| 927 | + $button_text = get_submit_button_text( $data ); | |
| 636 | 928 | |
| 637 | - Jetpack_Subscriptions_Widget::render_widget_status_messages( | |
| 638 | - array( | |
| 639 | - 'success_message' => $data['success_message'], | |
| 640 | - ) | |
| 641 | - ); | |
| 642 | - | |
| 643 | - $blog_id = \Jetpack_Options::get_option( 'id' ); | |
| 644 | - $post_access_level = get_post_access_level_for_current_post(); | |
| 645 | - | |
| 646 | - ?> | |
| 647 | - <div <?php echo wp_kses_data( $data['wrapper_attributes'] ); ?>> | |
| 648 | - <div class="jetpack_subscription_widget"> | |
| 649 | - <div class="wp-block-jetpack-subscriptions__container"> | |
| 650 | - <form | |
| 651 | - action="#" | |
| 652 | - method="post" | |
| 653 | - accept-charset="utf-8" | |
| 654 | - data-blog="<?php echo esc_attr( $blog_id ); ?>" | |
| 655 | - data-post_access_level="<?php echo esc_attr( $post_access_level ); ?>" | |
| 656 | - id="<?php echo esc_attr( $form_id ); ?>" | |
| 657 | - > | |
| 658 | - <p id="subscribe-email"> | |
| 659 | - <label id="jetpack-subscribe-label" | |
| 660 | - class="screen-reader-text" | |
| 661 | - for="<?php echo esc_attr( $subscribe_field_id . '-' . $data['widget_id'] ); ?>"> | |
| 662 | - <?php echo esc_html( $data['subscribe_placeholder'] ); ?> | |
| 663 | - </label> | |
| 664 | - <input type="email" name="email" required="required" | |
| 665 | - <?php if ( ! empty( $classes['email_field'] ) ) : ?> | |
| 666 | - class="<?php echo esc_attr( $classes['email_field'] ); ?> required" | |
| 667 | - <?php endif; ?> | |
| 668 | - <?php if ( ! empty( $styles['email_field'] ) ) : ?> | |
| 669 | - style="<?php echo esc_attr( $styles['email_field'] ); ?>" | |
| 670 | - <?php endif; ?> | |
| 671 | - value="<?php echo esc_attr( $data['subscribe_email'] ); ?>" | |
| 672 | - id="<?php echo esc_attr( $subscribe_field_id . '-' . $data['widget_id'] ); ?>" | |
| 673 | - placeholder="<?php echo esc_attr( $data['subscribe_placeholder'] ); ?>" | |
| 674 | - /> | |
| 929 | + $html = '<div ' . wp_kses_data( $data['wrapper_attributes'] ) . '> | |
| 930 | + <div> | |
| 931 | + <div> | |
| 932 | + <div> | |
| 933 | + <p ' . $submit_button_wrapper_style . '> | |
| 934 | + <a href="' . esc_url( get_post_permalink() ) . '" style="' . esc_attr( $styles['submit_button'] ) . ' text-decoration: none; white-space: nowrap; margin-left: 0">' . sanitize_submit_text( $button_text ) . '</a> | |
| 675 | 935 | </p> |
| 676 | - | |
| 677 | - <p id="subscribe-submit" | |
| 678 | - <?php if ( ! empty( $styles['submit_button_wrapper'] ) ) : ?> | |
| 679 | - style="<?php echo esc_attr( $styles['submit_button_wrapper'] ); ?>" | |
| 680 | - <?php endif; ?> | |
| 681 | - > | |
| 682 | - <input type="hidden" name="action" value="subscribe"/> | |
| 683 | - <input type="hidden" name="blog_id" value="<?php echo (int) $blog_id; ?>"/> | |
| 684 | - <input type="hidden" name="source" value="<?php echo esc_url( $data['referer'] ); ?>"/> | |
| 685 | - <input type="hidden" name="sub-type" value="<?php echo esc_attr( $data['source'] ); ?>"/> | |
| 686 | - <input type="hidden" name="redirect_fragment" value="<?php echo esc_attr( $form_id ); ?>"/> | |
| 687 | - <?php | |
| 688 | - if ( is_user_logged_in() ) { | |
| 689 | - wp_nonce_field( 'blogsub_subscribe_' . get_current_blog_id(), '_wpnonce', false ); | |
| 690 | - } | |
| 691 | - ?> | |
| 692 | - <button type="submit" | |
| 693 | - <?php if ( ! empty( $classes['submit_button'] ) ) : ?> | |
| 694 | - class="<?php echo esc_attr( $classes['submit_button'] ); ?>" | |
| 695 | - <?php endif; ?> | |
| 696 | - <?php if ( ! empty( $styles['submit_button'] ) ) : ?> | |
| 697 | - style="<?php echo esc_attr( $styles['submit_button'] ); ?>" | |
| 698 | - <?php endif; ?> | |
| 699 | - name="jetpack_subscriptions_widget" | |
| 700 | - > | |
| 701 | - <?php | |
| 702 | - echo wp_kses( | |
| 703 | - html_entity_decode( $data['submit_button_text'], ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ), | |
| 704 | - Jetpack_Subscriptions_Widget::$allowed_html_tags_for_submit_button | |
| 705 | - ); | |
| 706 | - ?> | |
| 707 | - </button> | |
| 708 | - </p> | |
| 709 | - </form> | |
| 710 | - | |
| 711 | - <?php if ( $data['show_subscribers_total'] && $data['subscribers_total'] ) : ?> | |
| 712 | - <div class="wp-block-jetpack-subscriptions__subscount"> | |
| 713 | - <?php | |
| 714 | - /* translators: %s: number of folks following the blog */ | |
| 715 | - echo esc_html( sprintf( _n( 'Join %s other subscriber', 'Join %s other subscribers', $data['subscribers_total'], 'jetpack' ), number_format_i18n( $data['subscribers_total'] ) ) ); | |
| 716 | - ?> | |
| 717 | - </div> | |
| 718 | - <?php endif; ?> | |
| 936 | + </div> | |
| 719 | 937 | </div> |
| 720 | 938 | </div> |
| 721 | - </div> | |
| 722 | - <?php | |
| 939 | + </div>'; | |
| 723 | 940 | |
| 724 | - return ob_get_clean(); | |
| 941 | + return $html; | |
| 725 | 942 | } |
| 726 | 943 | |
| 727 | 944 | /** |
| 728 | - * Filter excerpts looking for subscription data. | |
| 945 | + * WooCommerce Email Editor render callback for the subscriptions block. | |
| 729 | 946 | * |
| 730 | - * @param string $excerpt The extrapolated excerpt string. | |
| 731 | - * @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. | |
| 732 | 950 | * |
| 733 | - * @return mixed | |
| 951 | + * @return string | |
| 734 | 952 | */ |
| 735 | -function jetpack_filter_excerpt_for_newsletter( $excerpt, $post = null ) { | |
| 736 | - // The blogmagazine theme is overriding WP core `get_the_excerpt` filter and only passing the excerpt | |
| 737 | - // TODO: Until this is fixed, return the excerpt without gating. See https://github.com/Automattic/jetpack/pull/28102#issuecomment-1369161116 | |
| 738 | - if ( $post && false !== strpos( $post->post_content, '<!-- wp:jetpack/subscriptions -->' ) ) { | |
| 739 | - $excerpt .= sprintf( | |
| 740 | - // translators: %s is the permalink url to the current post. | |
| 741 | - __( "<p><a href='%s'>View post</a> to subscribe to site newsletter.</p>", 'jetpack' ), | |
| 742 | - get_post_permalink() | |
| 743 | - ); | |
| 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 ''; | |
| 744 | 956 | } |
| 745 | - 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 | + ); | |
| 746 | 993 | } |
| 747 | 994 | |
| 748 | 995 | /** |
| 749 | 996 | * Gate access to posts |
| @@ -752,24 +999,35 @@ | ||
| 752 | 999 | * |
| 753 | 1000 | * @return string |
| 754 | 1001 | */ |
| 755 | 1002 | function add_paywall( $the_content ) { |
| 756 | - $block_name = 'jetpack/paywall'; | |
| 757 | 1003 | require_once JETPACK__PLUGIN_DIR . 'modules/memberships/class-jetpack-memberships.php'; |
| 758 | 1004 | |
| 1005 | + $post_access_level = Jetpack_Memberships::get_post_access_level(); | |
| 1006 | + | |
| 759 | 1007 | if ( Jetpack_Memberships::user_can_view_post() ) { |
| 1008 | + if ( $post_access_level !== Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_EVERYBODY ) { | |
| 1009 | + do_action( | |
| 1010 | + 'earn_track_paywalled_post_view', | |
| 1011 | + array( | |
| 1012 | + 'post_id' => get_the_ID(), | |
| 1013 | + ) | |
| 1014 | + ); | |
| 1015 | + } | |
| 760 | 1016 | return $the_content; |
| 761 | 1017 | } |
| 762 | 1018 | |
| 763 | - if ( has_block( $block_name ) ) { | |
| 764 | - $post_access_level = Jetpack_Memberships::get_post_access_level(); | |
| 765 | - if ( jetpack_is_frontend() ) { | |
| 766 | - $paywalled_content = get_paywall_blocks( $post_access_level ); | |
| 767 | - } else { | |
| 768 | - // emails | |
| 769 | - $paywalled_content = get_paywall_simple(); | |
| 1019 | + $paywalled_content = get_paywall_content(); | |
| 1020 | + | |
| 1021 | + if ( has_block( \Automattic\Jetpack\Extensions\Paywall\BLOCK_NAME ) ) { | |
| 1022 | + if ( strpos( $the_content, \Automattic\Jetpack\Extensions\Paywall\BLOCK_HTML ) ) { | |
| 1023 | + return strstr( $the_content, \Automattic\Jetpack\Extensions\Paywall\BLOCK_HTML, true ) . $paywalled_content; | |
| 770 | 1024 | } |
| 771 | - $paywalled_content = strstr( $the_content, '<!-- wp:' . $block_name . ' /-->', true ) . $paywalled_content; | |
| 1025 | + // WordPress generates excerpts by either rendering or stripping blocks before invoking the `the_content` filter. | |
| 1026 | + // In the context of generating an excerpt, the Paywall block specifically renders THE_EXCERPT_BLOCK. | |
| 1027 | + if ( strpos( $the_content, \Automattic\Jetpack\Extensions\Paywall\THE_EXCERPT_BLOCK ) ) { | |
| 1028 | + return strstr( $the_content, \Automattic\Jetpack\Extensions\Paywall\THE_EXCERPT_BLOCK, true ); | |
| 1029 | + } | |
| 772 | 1030 | } |
| 773 | 1031 | |
| 774 | 1032 | return $paywalled_content; |
| 775 | 1033 | } |
| @@ -810,31 +1068,192 @@ | ||
| 810 | 1068 | return ''; |
| 811 | 1069 | } |
| 812 | 1070 | |
| 813 | 1071 | /** |
| 1072 | + * Is the Jetpack_Token_Subscription_Service class loaded | |
| 1073 | + * | |
| 1074 | + * @return bool | |
| 1075 | + */ | |
| 1076 | +function is_jetpack_token_subscription_service_loaded(): bool { | |
| 1077 | + return class_exists( 'Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Jetpack_Token_Subscription_Service' ); | |
| 1078 | +} | |
| 1079 | + | |
| 1080 | +/** | |
| 1081 | + * Adds support for WP Super cache and Boost cache | |
| 1082 | + */ | |
| 1083 | +function maybe_prevent_super_cache_caching() { | |
| 1084 | + // Prevents cached page to be served if the Membership cookie is present | |
| 1085 | + if ( is_jetpack_token_subscription_service_loaded() ) { | |
| 1086 | + do_action( 'wpsc_add_cookie', Jetpack_Token_Subscription_Service::JWT_AUTH_TOKEN_COOKIE_NAME ); | |
| 1087 | + } | |
| 1088 | + | |
| 1089 | + if ( is_user_auth() ) { | |
| 1090 | + // Do not cache the page if user is auth with Membership token | |
| 1091 | + if ( ! defined( 'DONOTCACHEPAGE' ) ) { | |
| 1092 | + define( 'DONOTCACHEPAGE', true ); | |
| 1093 | + } | |
| 1094 | + } | |
| 1095 | +} | |
| 1096 | + | |
| 1097 | +/** | |
| 814 | 1098 | * Returns paywall content blocks |
| 815 | 1099 | * |
| 816 | - * @param string $newsletter_access_level The newsletter access level. | |
| 817 | 1100 | * @return string |
| 818 | 1101 | */ |
| 819 | -function get_paywall_blocks( $newsletter_access_level ) { | |
| 820 | - // Only display paid texts when Stripe is connected and the post is marked for paid subscribers | |
| 821 | - $is_paid_post = $newsletter_access_level === 'paid_subscribers' | |
| 822 | - && ! empty( Jetpack_Memberships::get_connected_account_id() ); | |
| 1102 | +function get_paywall_content() { | |
| 1103 | + if ( Jetpack_Memberships::user_is_pending_subscriber() ) { | |
| 1104 | + return get_paywall_blocks_subscribe_pending(); | |
| 1105 | + } | |
| 1106 | + if ( doing_filter( 'get_the_excerpt' ) ) { | |
| 1107 | + return ''; | |
| 1108 | + } | |
| 1109 | + return get_paywall_blocks(); | |
| 1110 | +} | |
| 823 | 1111 | |
| 824 | - $access_heading = esc_html__( 'Subscribe to continue reading', 'jetpack' ); | |
| 1112 | +/** | |
| 1113 | + * Returns the current URL. | |
| 1114 | + * | |
| 1115 | + * TODO: Copied from https://github.com/Automattic/jetpack/blob/bb885061dc3ee7a80a78a5f0116ab3fcebfddb09/projects/packages/boost-core/src/lib/class-url.php#L39 | |
| 1116 | + * TODO: Move to a shared package | |
| 1117 | + * | |
| 1118 | + * @return string | |
| 1119 | + */ | |
| 1120 | +function get_current_url() { | |
| 1121 | + // Fallback to the site URL if we're unable to determine the URL from $_SERVER global. | |
| 1122 | + $current_url = site_url(); | |
| 825 | 1123 | |
| 1124 | + if ( isset( $_SERVER ) && is_array( $_SERVER ) ) { | |
| 1125 | + // phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitization happens at the end | |
| 1126 | + $scheme = isset( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ? 'https' : 'http'; | |
| 1127 | + $host = ! empty( $_SERVER['HTTP_HOST'] ) ? wp_unslash( $_SERVER['HTTP_HOST'] ) : null; | |
| 1128 | + $path = ! empty( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : ''; | |
| 1129 | + | |
| 1130 | + // Support for local plugin development and testing using ngrok. | |
| 1131 | + if ( ! empty( $_SERVER['HTTP_X_ORIGINAL_HOST'] ) && str_contains( $_SERVER['HTTP_X_ORIGINAL_HOST'], 'ngrok.io' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- This is validating. | |
| 1132 | + $host = wp_unslash( $_SERVER['HTTP_X_ORIGINAL_HOST'] ); | |
| 1133 | + } | |
| 1134 | + // phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 1135 | + | |
| 1136 | + if ( $host ) { | |
| 1137 | + $current_url = esc_url_raw( sprintf( '%s://%s%s', $scheme, $host, $path ) ); | |
| 1138 | + } | |
| 1139 | + } | |
| 1140 | + | |
| 1141 | + return $current_url; | |
| 1142 | +} | |
| 1143 | + | |
| 1144 | +/** | |
| 1145 | + * Get the submit button text based on the subscription status. | |
| 1146 | + * | |
| 1147 | + * @param array $data Array containing block view data. | |
| 1148 | + * | |
| 1149 | + * @return string | |
| 1150 | + */ | |
| 1151 | +function get_submit_button_text( $data ) { | |
| 1152 | + if ( ! Jetpack_Memberships::is_current_user_subscribed() ) { | |
| 1153 | + return $data['submit_button_text']; | |
| 1154 | + } | |
| 1155 | + if ( ! Jetpack_Memberships::user_can_view_post() ) { | |
| 1156 | + return $data['submit_button_text_upgrade']; | |
| 1157 | + } | |
| 1158 | + return '✓ ' . $data['submit_button_text_subscribed']; | |
| 1159 | +} | |
| 1160 | + | |
| 1161 | +/** | |
| 1162 | + * Returns true if there are no more tiers to upgrade to. | |
| 1163 | + * | |
| 1164 | + * @return boolean | |
| 1165 | + */ | |
| 1166 | +function is_top_subscription() { | |
| 1167 | + if ( ! Jetpack_Memberships::is_current_user_subscribed() ) { | |
| 1168 | + return false; | |
| 1169 | + } | |
| 1170 | + if ( ! Jetpack_Memberships::user_can_view_post() ) { | |
| 1171 | + return false; | |
| 1172 | + } | |
| 1173 | + return true; | |
| 1174 | +} | |
| 1175 | + | |
| 1176 | +/** | |
| 1177 | + * Sanitize the submit button text. | |
| 1178 | + * | |
| 1179 | + * @param string $text String containing the submit button text. | |
| 1180 | + * | |
| 1181 | + * @return string | |
| 1182 | + */ | |
| 1183 | +function sanitize_submit_text( $text ) { | |
| 1184 | + return wp_kses( | |
| 1185 | + html_entity_decode( $text, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ), | |
| 1186 | + Jetpack_Subscriptions_Widget::$allowed_html_tags_for_submit_button | |
| 1187 | + ); | |
| 1188 | +} | |
| 1189 | + | |
| 1190 | +/** | |
| 1191 | + * Returns paywall content blocks if user is not authenticated | |
| 1192 | + * | |
| 1193 | + * @return string | |
| 1194 | + */ | |
| 1195 | +function get_paywall_blocks() { | |
| 1196 | + $custom_paywall = apply_filters( 'jetpack_custom_paywall_blocks', false ); | |
| 1197 | + if ( ! empty( $custom_paywall ) ) { | |
| 1198 | + return $custom_paywall; | |
| 1199 | + } | |
| 1200 | + | |
| 1201 | + if ( ! Request::is_frontend() ) { // emails | |
| 1202 | + return get_paywall_simple(); | |
| 1203 | + } | |
| 1204 | + | |
| 1205 | + require_once JETPACK__PLUGIN_DIR . 'modules/memberships/class-jetpack-memberships.php'; | |
| 1206 | + $is_paid_post = is_paid_post(); | |
| 1207 | + $is_paid_subscriber = Jetpack_Memberships::user_is_paid_subscriber(); | |
| 1208 | + | |
| 1209 | + $access_heading = $is_paid_subscriber | |
| 1210 | + ? esc_html__( 'Upgrade to continue reading', 'jetpack' ) | |
| 1211 | + : esc_html__( 'Subscribe to continue reading', 'jetpack' ); | |
| 1212 | + | |
| 826 | 1213 | $subscribe_text = $is_paid_post |
| 827 | 1214 | // translators: %s is the name of the site. |
| 828 | - ? 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 | + ) | |
| 829 | 1220 | // translators: %s is the name of the site. |
| 830 | 1221 | : esc_html__( 'Subscribe to get access to the rest of this post and other subscriber-only content.', 'jetpack' ); |
| 831 | 1222 | |
| 1223 | + $login_block = ''; | |
| 1224 | + | |
| 1225 | + if ( is_user_auth() ) { | |
| 1226 | + if ( ( new Host() )->is_wpcom_simple() ) { | |
| 1227 | + // We cannot use wpcom_logmein_redirect_url since it returns redirect URL when user is already logged in. | |
| 1228 | + $login_link = add_query_arg( | |
| 1229 | + array( | |
| 1230 | + 'redirect_to' => rawurlencode( get_current_url() ), | |
| 1231 | + 'blog_id' => get_current_blog_id(), | |
| 1232 | + ), | |
| 1233 | + 'https://wordpress.com/log-in/link' | |
| 1234 | + ); | |
| 1235 | + $switch_accounts_link = wp_logout_url( $login_link ); | |
| 1236 | + $login_block = '<!-- wp:paragraph {"align":"center","style":{"typography":{"fontSize":"14px"}}} --> | |
| 1237 | +<p class="has-text-align-center" style="font-size:14px"> | |
| 1238 | + <a href="' . $switch_accounts_link . '">' . __( 'Switch accounts', 'jetpack' ) . '</a> | |
| 1239 | +</p> | |
| 1240 | +<!-- /wp:paragraph -->'; | |
| 1241 | + } | |
| 1242 | + } else { | |
| 1243 | + $access_question = $is_paid_post ? esc_html__( 'Already a paid subscriber?', 'jetpack' ) : esc_html__( 'Already a subscriber?', 'jetpack' ); | |
| 1244 | + $login_block = '<!-- wp:group {"style":{"typography":{"fontSize":"14px"}},"layout":{"type":"flex","justifyContent":"center"}} --> | |
| 1245 | +<div class="wp-block-group" style="font-size:14px"> | |
| 1246 | + <!-- wp:jetpack/subscriber-login {"logInLabel":"' . $access_question . '"} /--> | |
| 1247 | +</div> | |
| 1248 | +<!-- /wp:group -->'; | |
| 1249 | + } | |
| 1250 | + | |
| 832 | 1251 | $lock_svg = plugins_url( 'images/lock-paywall.svg', JETPACK__PLUGIN_FILE ); |
| 833 | 1252 | |
| 834 | 1253 | return ' |
| 835 | -<!-- wp:group {"style":{"border":{"width":"1px","radius":"4px"},"spacing":{"padding":{"top":"var:preset|spacing|70","bottom":"var:preset|spacing|70","left":"32px","right":"32px"}}},"borderColor":"primary","className":"jetpack-subscribe-paywall","layout":{"type":"constrained","contentSize":"400px"}} --> | |
| 836 | -<div class="wp-block-group jetpack-subscribe-paywall has-border-color has-primary-border-color" style="border-width:1px;border-radius:4px;padding-top:var(--wp--preset--spacing--70);padding-right:32px;padding-bottom:var(--wp--preset--spacing--70);padding-left:32px"> | |
| 1254 | +<!-- wp:group {"style":{"border":{"width":"1px","radius":"4px"},"spacing":{"padding":{"top":"32px","bottom":"32px","left":"32px","right":"32px"}}},"borderColor":"primary","className":"jetpack-subscribe-paywall","layout":{"type":"constrained","contentSize":"400px"}} --> | |
| 1255 | +<div class="wp-block-group jetpack-subscribe-paywall has-border-color has-primary-border-color" style="border-width:1px;border-radius:4px;padding-top:32px;padding-right:32px;padding-bottom:32px;padding-left:32px"> | |
| 837 | 1256 | <!-- wp:image {"align":"center","width":24,"height":24,"sizeSlug":"large","linkDestination":"none"} --> |
| 838 | 1257 | <figure class="wp-block-image aligncenter size-large is-resized"><img src="' . $lock_svg . '" alt="" width="24" height="24"/></figure> |
| 839 | 1258 | <!-- /wp:image --> |
| 840 | 1259 | |
| @@ -845,28 +1264,136 @@ | ||
| 845 | 1264 | <!-- wp:paragraph {"align":"center","style":{"typography":{"fontSize":"14px"},"spacing":{"margin":{"top":"10px","bottom":"10px"}}}} --> |
| 846 | 1265 | <p class="has-text-align-center" style="margin-top:10px;margin-bottom:10px;font-size:14px">' . $subscribe_text . '</p> |
| 847 | 1266 | <!-- /wp:paragraph --> |
| 848 | 1267 | |
| 849 | -<!-- wp:jetpack/subscriptions {"borderRadius":50,"borderColor":"primary","className":"is-style-compact"} /--></div> | |
| 1268 | +<!-- wp:jetpack/subscriptions {"borderRadius":50,"borderColor":"primary","className":"is-style-compact","isPaidSubscriber":' . ( $is_paid_subscriber ? 'true' : 'false' ) . '} /--> | |
| 1269 | +' . $login_block . ' | |
| 1270 | +</div> | |
| 850 | 1271 | <!-- /wp:group --> |
| 851 | 1272 | '; |
| 852 | 1273 | } |
| 853 | 1274 | |
| 854 | 1275 | /** |
| 855 | - * Return content for non frontend views like emails. | |
| 1276 | + * Returns true if user is auth for subscriptions check, otherwise returns false. | |
| 856 | 1277 | * |
| 1278 | + * @return bool | |
| 1279 | + */ | |
| 1280 | +function is_user_auth(): bool { | |
| 1281 | + if ( ( new Host() )->is_wpcom_simple() && is_user_logged_in() ) { | |
| 1282 | + return true; | |
| 1283 | + } | |
| 1284 | + if ( current_user_can( 'manage_options' ) ) { | |
| 1285 | + return true; | |
| 1286 | + } | |
| 1287 | + | |
| 1288 | + if ( is_jetpack_token_subscription_service_loaded() ) { | |
| 1289 | + if ( Jetpack_Token_Subscription_Service::has_token_from_cookie() ) { | |
| 1290 | + return true; | |
| 1291 | + } | |
| 1292 | + } | |
| 1293 | + return false; | |
| 1294 | +} | |
| 1295 | + | |
| 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 | +/** | |
| 1325 | + * Returns paywall content blocks when email confirmation is pending | |
| 1326 | + * | |
| 857 | 1327 | * @return string |
| 858 | 1328 | */ |
| 859 | -function get_paywall_simple() { | |
| 860 | - $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' ); | |
| 1329 | +function get_paywall_blocks_subscribe_pending() { | |
| 1330 | + $subscribe_email = Jetpack_Memberships::get_current_user_email(); | |
| 861 | 1331 | |
| 862 | - $subscribe_text = esc_html__( 'Upgrade subscription', 'jetpack' ); | |
| 1332 | + /** This filter is documented in \Automattic\Jetpack\Forms\ContactForm\Contact_Form */ | |
| 1333 | + if ( is_wpcom() || false !== apply_filters( 'jetpack_auto_fill_logged_in_user', false ) ) { | |
| 1334 | + $current_user = wp_get_current_user(); | |
| 1335 | + if ( ! empty( $current_user->user_email ) ) { | |
| 1336 | + $subscribe_email = $current_user->user_email; | |
| 1337 | + } | |
| 1338 | + } | |
| 863 | 1339 | |
| 1340 | + $access_heading = esc_html__( 'Confirm your subscription to continue reading', 'jetpack' ); | |
| 1341 | + | |
| 1342 | + /* translators: %s: email address */ | |
| 1343 | + $subscribe_text = sprintf( esc_html__( 'Head to your inbox and confirm your email address %s.', 'jetpack' ), $subscribe_email ); | |
| 1344 | + | |
| 1345 | + $lock_svg = plugins_url( 'images/lock-paywall.svg', JETPACK__PLUGIN_FILE ); | |
| 1346 | + | |
| 864 | 1347 | return ' |
| 1348 | +<!-- wp:group {"style":{"border":{"width":"1px","radius":"4px"},"spacing":{"padding":{"top":"32px","bottom":"32px","left":"32px","right":"32px"}}},"borderColor":"primary","className":"jetpack-subscribe-paywall","layout":{"type":"constrained","contentSize":"400px"}} --> | |
| 1349 | +<div class="wp-block-group jetpack-subscribe-paywall has-border-color has-primary-border-color" style="border-width:1px;border-radius:4px;padding-top:32px;padding-right:32px;padding-bottom:32px;padding-left:32px"> | |
| 1350 | +<!-- wp:image {"align":"center","width":24,"height":24,"sizeSlug":"large","linkDestination":"none"} --> | |
| 1351 | +<figure class="wp-block-image aligncenter size-large is-resized"><img src="' . $lock_svg . '" alt="" width="24" height="24"/></figure> | |
| 1352 | +<!-- /wp:image --> | |
| 1353 | + | |
| 1354 | +<!-- wp:heading {"textAlign":"center","style":{"typography":{"fontStyle":"normal","fontWeight":"600","fontSize":"24px", "maxWidth":"initial"},"layout":{"selfStretch":"fit"}}} --> | |
| 1355 | +<h2 class="wp-block-heading has-text-align-center" style="font-size:24px;font-style:normal;font-weight:600;max-width:initial">' . $access_heading . '</h2> | |
| 1356 | +<!-- /wp:heading --> | |
| 1357 | + | |
| 1358 | +<!-- wp:paragraph {"align":"center","style":{"typography":{"fontSize":"14px"},"spacing":{"margin":{"top":"10px","bottom":"10px"}}}} --> | |
| 1359 | +<p class="has-text-align-center" style="margin-top:10px;margin-bottom:10px;font-size:14px">' . $subscribe_text . '</p> | |
| 1360 | +<!-- /wp:paragraph --> | |
| 1361 | +</div> | |
| 1362 | +<!-- /wp:group --> | |
| 1363 | +'; | |
| 1364 | +} | |
| 1365 | + | |
| 1366 | +/** | |
| 1367 | + * Return content for non frontend views like Reader, emails. | |
| 1368 | + */ | |
| 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' ); | |
| 1374 | + | |
| 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 | + } | |
| 1387 | + | |
| 1388 | + return ' | |
| 865 | 1389 | <!-- wp:columns --> |
| 866 | -<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%"> | |
| 867 | 1391 | <!-- wp:column --> |
| 868 | 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 --> | |
| 869 | 1396 | <!-- wp:paragraph --> |
| 870 | 1397 | <p class="has-text-align-center" |
| 871 | 1398 | style="text-align: center; |
| 872 | 1399 | color: #50575E; |
| @@ -873,20 +1400,20 @@ | ||
| 873 | 1400 | font-weight: 400; |
| 874 | 1401 | font-size: 16px; |
| 875 | 1402 | font-family: \'SF Pro Text\', sans-serif; |
| 876 | 1403 | line-height: 28.8px;"> |
| 877 | - ' . $access_heading . ' | |
| 1404 | + ' . $paywall_description . ' | |
| 878 | 1405 | </p> |
| 879 | 1406 | <!-- /wp:paragraph --> |
| 880 | - | |
| 881 | 1407 | <!-- wp:buttons --> |
| 882 | 1408 | <div class="wp-block-buttons" style="text-align: center;"> |
| 883 | 1409 | <!-- wp:button --> |
| 884 | - <div class="wp-block-button" style="display: inline-block; margin: 10px 0;"> | |
| 885 | - <a href="#" class="wp-block-button__link wp-element-button" | |
| 1410 | + <div class="wp-block-button" style="display: inline-block; margin: 10px 0; border-style: none; padding: 0;"> | |
| 1411 | + <a href="' . esc_url( get_post_permalink() ) . '" class="wp-block-button__link wp-element-button" | |
| 1412 | + data-wpcom-track data-tracks-link-desc="paywall-email-click" | |
| 886 | 1413 | style="display: inline-block; |
| 887 | - padding: 15px 20px; | |
| 888 | - background-color: #0675C4; | |
| 1414 | + padding: 12px 15px; | |
| 1415 | + background-color: #3858e9; | |
| 889 | 1416 | color: #FFFFFF; |
| 890 | 1417 | text-decoration: none; |
| 891 | 1418 | border-radius: 5px; |
| 892 | 1419 | font-family: \'SF Pro Display\', sans-serif; |
| @@ -891,9 +1418,9 @@ | ||
| 891 | 1418 | border-radius: 5px; |
| 892 | 1419 | font-family: \'SF Pro Display\', sans-serif; |
| 893 | 1420 | font-weight: 500; |
| 894 | 1421 | font-size: 16px; |
| 895 | - text-align: center;">' . $subscribe_text . '</a> | |
| 1422 | + text-align: center;">' . $paywall_action_btn . '</a> | |
| 896 | 1423 | </div> |
| 897 | 1424 | <!-- /wp:button --> |
| 898 | 1425 | </div> |
| 899 | 1426 | <!-- /wp:buttons --> |