| 1 | <?php |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | namespace Automattic\Jetpack\Extensions\Subscriptions; |
| 9 | |
| 10 | use Automattic\Jetpack\Blocks; |
| 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; |
| 16 | use Jetpack_Gutenberg; |
| 17 | use Jetpack_Memberships; |
| 18 | use Jetpack_Subscriptions_Widget; |
| 19 | |
| 20 | if ( ! defined( 'ABSPATH' ) ) { |
| 21 | exit( 0 ); |
| 22 | } |
| 23 | |
| 24 | require_once __DIR__ . '/class-jetpack-subscription-site.php'; |
| 25 | require_once __DIR__ . '/constants.php'; |
| 26 | require_once JETPACK__PLUGIN_DIR . 'extensions/blocks/premium-content/_inc/subscription-service/include.php'; |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | const DEFAULT_BORDER_RADIUS_VALUE = 0; |
| 32 | const DEFAULT_BORDER_WEIGHT_VALUE = 1; |
| 33 | const DEFAULT_FONTSIZE_VALUE = '16px'; |
| 34 | const DEFAULT_PADDING_VALUE = 15; |
| 35 | const DEFAULT_SPACING_VALUE = 10; |
| 36 | const DEFAULT_BUTTON_WIDTH = 'auto'; |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | function register_block() { |
| 44 | |
| 45 | |
| 46 | |
| 47 | if ( function_exists( '\WPForTeams\is_wpforteams_site' ) && |
| 48 | \WPForTeams\is_wpforteams_site( get_current_blog_id() ) ) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | |
| 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() ) { |
| 62 | Blocks::jetpack_register_block( |
| 63 | __DIR__, |
| 64 | array( |
| 65 | 'render_callback' => __NAMESPACE__ . '\render_block', |
| 66 | 'render_email_callback' => __NAMESPACE__ . '\render_email', |
| 67 | 'supports' => array( |
| 68 | 'spacing' => array( |
| 69 | 'margin' => true, |
| 70 | 'padding' => true, |
| 71 | ), |
| 72 | 'align' => array( 'wide', 'full' ), |
| 73 | ), |
| 74 | ) |
| 75 | ); |
| 76 | } |
| 77 | |
| 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 | 'auth_callback' => function () { |
| 86 | return wp_get_current_user()->has_cap( 'edit_posts' ); |
| 87 | }, |
| 88 | ) |
| 89 | ); |
| 90 | |
| 91 | register_post_meta( |
| 92 | 'post', |
| 93 | META_NAME_FOR_POST_DONT_EMAIL_TO_SUBS, |
| 94 | array( |
| 95 | 'default' => false, |
| 96 | 'show_in_rest' => true, |
| 97 | 'single' => true, |
| 98 | 'type' => 'boolean', |
| 99 | 'auth_callback' => function () { |
| 100 | return wp_get_current_user()->has_cap( 'edit_posts' ); |
| 101 | }, |
| 102 | ) |
| 103 | ); |
| 104 | |
| 105 | register_post_meta( |
| 106 | 'post', |
| 107 | META_NAME_FOR_POST_TIER_ID_SETTINGS, |
| 108 | array( |
| 109 | 'show_in_rest' => true, |
| 110 | 'single' => true, |
| 111 | 'type' => 'integer', |
| 112 | 'auth_callback' => function () { |
| 113 | return wp_get_current_user()->has_cap( 'edit_posts' ); |
| 114 | }, |
| 115 | ) |
| 116 | ); |
| 117 | |
| 118 | register_post_meta( |
| 119 | 'post', |
| 120 | META_NAME_CONTAINS_PAYWALLED_CONTENT, |
| 121 | array( |
| 122 | 'show_in_rest' => true, |
| 123 | 'single' => true, |
| 124 | 'type' => 'boolean', |
| 125 | 'auth_callback' => function () { |
| 126 | return wp_get_current_user()->has_cap( 'edit_posts' ); |
| 127 | }, |
| 128 | ) |
| 129 | ); |
| 130 | |
| 131 | |
| 132 | add_filter( |
| 133 | 'jetpack_sync_post_meta_whitelist', |
| 134 | function ( $allowed_meta ) { |
| 135 | return array_merge( |
| 136 | $allowed_meta, |
| 137 | array( |
| 138 | META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS, |
| 139 | META_NAME_FOR_POST_DONT_EMAIL_TO_SUBS, |
| 140 | META_NAME_CONTAINS_PAYWALLED_CONTENT, |
| 141 | ) |
| 142 | ); |
| 143 | } |
| 144 | ); |
| 145 | |
| 146 | |
| 147 | add_filter( 'the_content', __NAMESPACE__ . '\add_paywall', 8 ); |
| 148 | |
| 149 | |
| 150 | add_filter( 'comments_open', __NAMESPACE__ . '\maybe_close_comments', 10, 2 ); |
| 151 | add_filter( 'pings_open', __NAMESPACE__ . '\maybe_close_comments', 10, 2 ); |
| 152 | |
| 153 | |
| 154 | add_filter( 'get_comment', __NAMESPACE__ . '\maybe_gate_existing_comments' ); |
| 155 | |
| 156 | |
| 157 | add_filter( 'get_the_excerpt', __NAMESPACE__ . '\jetpack_filter_excerpt_for_newsletter', 10, 2 ); |
| 158 | |
| 159 | |
| 160 | |
| 161 | if ( defined( 'WP_ADMIN' ) && WP_ADMIN && Jetpack_Memberships::has_configured_plans_jetpack_recurring_payments( 'newsletter' ) ) { |
| 162 | add_action( 'manage_post_posts_columns', __NAMESPACE__ . '\register_newsletter_access_column' ); |
| 163 | add_action( 'manage_post_posts_custom_column', __NAMESPACE__ . '\render_newsletter_access_rows', 10, 2 ); |
| 164 | add_action( 'admin_head', __NAMESPACE__ . '\newsletter_access_column_styles' ); |
| 165 | } |
| 166 | |
| 167 | add_action( 'init', __NAMESPACE__ . '\maybe_prevent_super_cache_caching' ); |
| 168 | |
| 169 | add_action( 'wp_after_insert_post', __NAMESPACE__ . '\add_paywalled_content_post_meta', 99, 2 ); |
| 170 | |
| 171 | add_filter( |
| 172 | 'jetpack_options_whitelist', |
| 173 | function ( $options ) { |
| 174 | $options[] = 'jetpack_subscriptions_subscribe_post_end_enabled'; |
| 175 | $options[] = 'jetpack_subscriptions_subscribe_navigation_enabled'; |
| 176 | |
| 177 | return $options; |
| 178 | } |
| 179 | ); |
| 180 | |
| 181 | |
| 182 | if ( ( new Host() )->is_wpcom_platform() && ! Request::is_frontend() ) { |
| 183 | add_action( |
| 184 | 'restapi_theme_init', |
| 185 | function () { |
| 186 | Jetpack_Subscription_Site::init()->handle_subscribe_block_placements(); |
| 187 | } |
| 188 | ); |
| 189 | } else { |
| 190 | Jetpack_Subscription_Site::init()->handle_subscribe_block_placements(); |
| 191 | } |
| 192 | } |
| 193 | add_action( 'init', __NAMESPACE__ . '\register_block', 9 ); |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | function is_wpcom() { |
| 201 | return defined( 'IS_WPCOM' ) && IS_WPCOM; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | function register_newsletter_access_column( $columns ) { |
| 211 | $position = array_search( 'title', array_keys( $columns ), true ); |
| 212 | $new_column = array( NEWSLETTER_COLUMN_ID => __( 'Newsletter', 'jetpack' ) ); |
| 213 | return array_merge( |
| 214 | array_slice( $columns, 0, $position + 1, true ), |
| 215 | $new_column, |
| 216 | array_slice( $columns, $position, null, true ) |
| 217 | ); |
| 218 | } |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | function add_paywalled_content_post_meta( int $post_id, \WP_Post $post ) { |
| 228 | if ( $post->post_type !== 'post' ) { |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | $access_level = get_post_meta( $post_id, META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS, true ); |
| 233 | |
| 234 | $is_paywalled = false; |
| 235 | switch ( $access_level ) { |
| 236 | case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS_ALL_TIERS: |
| 237 | case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS: |
| 238 | case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_SUBSCRIBERS: |
| 239 | $is_paywalled = true; |
| 240 | } |
| 241 | if ( $is_paywalled ) { |
| 242 | update_post_meta( $post_id, META_NAME_CONTAINS_PAYWALLED_CONTENT, $is_paywalled ); |
| 243 | } |
| 244 | if ( ! $is_paywalled ) { |
| 245 | delete_post_meta( $post_id, META_NAME_CONTAINS_PAYWALLED_CONTENT ); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | function render_newsletter_access_rows( $column_id, $post_id ) { |
| 256 | if ( NEWSLETTER_COLUMN_ID !== $column_id ) { |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | $access_level = get_post_meta( $post_id, META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS, true ); |
| 261 | |
| 262 | switch ( $access_level ) { |
| 263 | case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS_ALL_TIERS: |
| 264 | echo esc_html__( 'Paid Subscribers (all plans)', 'jetpack' ); |
| 265 | break; |
| 266 | case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS: |
| 267 | echo esc_html__( 'Paid Subscribers', 'jetpack' ); |
| 268 | break; |
| 269 | case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_SUBSCRIBERS: |
| 270 | echo esc_html__( 'Subscribers', 'jetpack' ); |
| 271 | break; |
| 272 | case Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_EVERYBODY: |
| 273 | echo esc_html__( 'Everybody', 'jetpack' ); |
| 274 | break; |
| 275 | default: |
| 276 | echo ''; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | function newsletter_access_column_styles() { |
| 284 | echo '<style id="jetpack-newsletter-newsletter-access-column"> table.fixed .column-newsletter_access { width: 10%; } </style>'; |
| 285 | } |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | function fetch_subscriber_counts() { |
| 293 | $subs_count = 0; |
| 294 | if ( is_wpcom() ) { |
| 295 | $subs_count = array( |
| 296 | 'value' => \wpcom_fetch_subs_counts( true ), |
| 297 | ); |
| 298 | } else { |
| 299 | $cache_key = 'wpcom_subscribers_totals'; |
| 300 | $subs_count = get_transient( $cache_key ); |
| 301 | if ( false === $subs_count || 'failed' === $subs_count['status'] ) { |
| 302 | $xml = new \Jetpack_IXR_Client(); |
| 303 | $xml->query( 'jetpack.fetchSubscriberCounts' ); |
| 304 | |
| 305 | if ( $xml->isError() ) { |
| 306 | $subs_count = array( |
| 307 | 'status' => 'failed', |
| 308 | 'code' => $xml->getErrorCode(), |
| 309 | 'message' => $xml->getErrorMessage(), |
| 310 | 'value' => ( isset( $subs_count['value'] ) ) ? $subs_count['value'] : array( |
| 311 | 'total_subscribers' => 0, |
| 312 | 'email_subscribers' => 0, |
| 313 | 'social_followers' => 0, |
| 314 | 'paid_subscribers' => 0, |
| 315 | ), |
| 316 | ); |
| 317 | } else { |
| 318 | $subs_count = array( |
| 319 | 'status' => 'success', |
| 320 | 'value' => $xml->getResponse(), |
| 321 | ); |
| 322 | } |
| 323 | set_transient( $cache_key, $subs_count, 3600 ); |
| 324 | } |
| 325 | } |
| 326 | return $subs_count; |
| 327 | } |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | function get_subscriber_count( $include_social_followers ) { |
| 336 | $counts = fetch_subscriber_counts(); |
| 337 | |
| 338 | if ( $include_social_followers ) { |
| 339 | $subscriber_count = $counts['value']['total_subscribers'] + $counts['value']['social_followers']; |
| 340 | } else { |
| 341 | $subscriber_count = $counts['value']['total_subscribers']; |
| 342 | } |
| 343 | return $subscriber_count; |
| 344 | } |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | function has_attribute( $attributes, $key ) { |
| 355 | return isset( $attributes[ $key ] ) && $attributes[ $key ] !== 'undefined'; |
| 356 | } |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | function get_attribute( $attributes, $key, $default = null ) { |
| 368 | return has_attribute( $attributes, $key ) ? $attributes[ $key ] : $default; |
| 369 | } |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | function get_setting_class_name( $setting, $value ) { |
| 380 | if ( ! $setting || ! $value ) { |
| 381 | return ''; |
| 382 | } |
| 383 | |
| 384 | return sprintf( 'has-%s-%s', $value, $setting ); |
| 385 | } |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | function get_element_class_names_from_attributes( $attributes ) { |
| 396 | $text_color_class = get_setting_class_name( 'color', get_attribute( $attributes, 'textColor' ) ); |
| 397 | $font_size_class = get_setting_class_name( 'font-size', get_attribute( $attributes, 'fontSize' ) ); |
| 398 | $border_class = get_setting_class_name( 'border-color', get_attribute( $attributes, 'borderColor' ) ); |
| 399 | |
| 400 | $button_background_class = get_setting_class_name( 'background-color', get_attribute( $attributes, 'buttonBackgroundColor' ) ); |
| 401 | $button_gradient_class = get_setting_class_name( 'gradient-background', get_attribute( $attributes, 'buttonGradient' ) ); |
| 402 | |
| 403 | $email_field_background_class = get_setting_class_name( 'background-color', get_attribute( $attributes, 'emailFieldBackgroundColor' ) ); |
| 404 | $email_field_gradient_class = get_setting_class_name( 'gradient-background', get_attribute( $attributes, 'emailFieldGradient' ) ); |
| 405 | |
| 406 | $submit_button_classes = array_filter( |
| 407 | array( |
| 408 | 'wp-block-button__link' => true, |
| 409 | 'no-border-radius' => 0 === get_attribute( $attributes, 'borderRadius', 0 ), |
| 410 | $font_size_class => true, |
| 411 | $border_class => true, |
| 412 | 'has-text-color' => ! empty( $text_color_class ), |
| 413 | $text_color_class => true, |
| 414 | 'has-background' => ! empty( $button_background_class ) || ! empty( $button_gradient_class ), |
| 415 | $button_background_class => ! empty( $button_background_class ), |
| 416 | $button_gradient_class => ! empty( $button_gradient_class ), |
| 417 | ) |
| 418 | ); |
| 419 | |
| 420 | $email_field_classes = array_filter( |
| 421 | array( |
| 422 | 'no-border-radius' => 0 === get_attribute( $attributes, 'borderRadius', 0 ), |
| 423 | $font_size_class => true, |
| 424 | $border_class => true, |
| 425 | $email_field_background_class => true, |
| 426 | $email_field_gradient_class => true, |
| 427 | ) |
| 428 | ); |
| 429 | |
| 430 | $block_wrapper_classes = array_filter( |
| 431 | array( |
| 432 | 'wp-block-jetpack-subscriptions__supports-newline' => true, |
| 433 | 'wp-block-jetpack-subscriptions__use-newline' => (bool) get_attribute( $attributes, 'buttonOnNewLine' ), |
| 434 | 'wp-block-jetpack-subscriptions__show-subs' => (bool) get_attribute( $attributes, 'showSubscribersTotal' ), |
| 435 | ) |
| 436 | ); |
| 437 | |
| 438 | return array( |
| 439 | 'block_wrapper' => implode( ' ', array_keys( $block_wrapper_classes ) ), |
| 440 | 'email_field' => implode( ' ', array_keys( $email_field_classes ) ), |
| 441 | 'submit_button' => implode( ' ', array_keys( $submit_button_classes ) ), |
| 442 | ); |
| 443 | } |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | function is_button_only_style( $class_name ) { |
| 453 | if ( empty( $class_name ) ) { |
| 454 | return false; |
| 455 | } |
| 456 | |
| 457 | $class_names = explode( ' ', $class_name ); |
| 458 | return in_array( 'is-style-button', $class_names, true ); |
| 459 | } |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | function get_element_styles_from_attributes( $attributes ) { |
| 470 | $is_button_only_style = is_button_only_style( get_attribute( $attributes, 'className', '' ) ); |
| 471 | |
| 472 | $button_background_style = ! has_attribute( $attributes, 'buttonBackgroundColor' ) && has_attribute( $attributes, 'customButtonGradient' ) |
| 473 | ? get_attribute( $attributes, 'customButtonGradient' ) |
| 474 | : get_attribute( $attributes, 'customButtonBackgroundColor' ); |
| 475 | |
| 476 | $email_field_styles = ''; |
| 477 | $submit_button_wrapper_styles = ''; |
| 478 | $submit_button_styles = ''; |
| 479 | |
| 480 | if ( ! empty( $button_background_style ) ) { |
| 481 | $submit_button_styles .= sprintf( 'background: %s;', $button_background_style ); |
| 482 | } |
| 483 | |
| 484 | if ( has_attribute( $attributes, 'customTextColor' ) ) { |
| 485 | $submit_button_styles .= sprintf( 'color: %s;', get_attribute( $attributes, 'customTextColor' ) ); |
| 486 | } |
| 487 | |
| 488 | if ( has_attribute( $attributes, 'buttonWidth' ) ) { |
| 489 | $submit_button_wrapper_styles .= sprintf( 'width: %s;', get_attribute( $attributes, 'buttonWidth', DEFAULT_BUTTON_WIDTH ) ); |
| 490 | $submit_button_wrapper_styles .= 'max-width: 100%;'; |
| 491 | |
| 492 | |
| 493 | $submit_button_styles .= true === get_attribute( $attributes, 'buttonOnNewLine' ) |
| 494 | ? 'width: 100%;' |
| 495 | : sprintf( 'width: calc(100%% - %dpx);', get_attribute( $attributes, 'spacing', DEFAULT_SPACING_VALUE ) ); |
| 496 | } |
| 497 | |
| 498 | $font_size = get_attribute( $attributes, 'customFontSize', DEFAULT_FONTSIZE_VALUE ); |
| 499 | $style = sprintf( 'font-size: %s%s;', $font_size, is_numeric( $font_size ) ? 'px' : '' ); |
| 500 | |
| 501 | $submit_button_styles .= $style; |
| 502 | $email_field_styles .= $style; |
| 503 | |
| 504 | $padding = get_attribute( $attributes, 'padding', DEFAULT_PADDING_VALUE ); |
| 505 | $style = sprintf( 'padding: %1$dpx %2$dpx %1$dpx %2$dpx;', $padding, round( $padding * 1.5 ) ); |
| 506 | |
| 507 | $submit_button_styles .= $style; |
| 508 | $email_field_styles .= $style; |
| 509 | |
| 510 | if ( ! $is_button_only_style ) { |
| 511 | $button_spacing = get_attribute( $attributes, 'spacing', DEFAULT_SPACING_VALUE ); |
| 512 | if ( true === get_attribute( $attributes, 'buttonOnNewLine' ) ) { |
| 513 | $submit_button_styles .= sprintf( 'margin-top: %dpx;', $button_spacing ); |
| 514 | } else { |
| 515 | $submit_button_styles .= 'margin: 0; '; |
| 516 | $submit_button_styles .= sprintf( 'margin-left: %dpx;', $button_spacing ); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | if ( has_attribute( $attributes, 'borderColor' ) ) { |
| 521 | $style = sprintf( 'border-color: %s;', get_attribute( $attributes, 'borderColor', '' ) ); |
| 522 | $submit_button_styles .= $style; |
| 523 | $email_field_styles .= $style; |
| 524 | } |
| 525 | |
| 526 | $style = sprintf( 'border-radius: %dpx;', get_attribute( $attributes, 'borderRadius', DEFAULT_BORDER_RADIUS_VALUE ) ); |
| 527 | $submit_button_styles .= $style; |
| 528 | $email_field_styles .= $style; |
| 529 | |
| 530 | $style = sprintf( 'border-width: %dpx;', get_attribute( $attributes, 'borderWeight', DEFAULT_BORDER_WEIGHT_VALUE ) ); |
| 531 | $submit_button_styles .= $style; |
| 532 | $email_field_styles .= $style; |
| 533 | |
| 534 | if ( has_attribute( $attributes, 'customBorderColor' ) ) { |
| 535 | $style = sprintf( 'border-color: %s; border-style: solid;', get_attribute( $attributes, 'customBorderColor' ) ); |
| 536 | |
| 537 | $submit_button_styles .= $style; |
| 538 | $email_field_styles .= $style; |
| 539 | } |
| 540 | |
| 541 | if ( ! Request::is_frontend() ) { |
| 542 | $background_color_style = get_attribute_color( 'buttonBackgroundColor', $attributes, '#113AF5' ); |
| 543 | $text_color_style = get_attribute_color( 'textColor', $attributes, '#FFFFFF' ); |
| 544 | $submit_button_styles .= sprintf( ' background-color: %s; color: %s;', $background_color_style, $text_color_style ); |
| 545 | } |
| 546 | |
| 547 | return array( |
| 548 | 'email_field' => $email_field_styles, |
| 549 | 'submit_button' => $submit_button_styles, |
| 550 | 'submit_button_wrapper' => $submit_button_wrapper_styles, |
| 551 | ); |
| 552 | } |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | |
| 558 | |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | function get_attribute_color( $attribute_name, $attributes, $default_color ) { |
| 564 | if ( has_attribute( $attributes, $attribute_name ) ) { |
| 565 | $color_slug = get_attribute( $attributes, $attribute_name ); |
| 566 | $resolved_color = get_color_from_slug( $color_slug ); |
| 567 | |
| 568 | if ( $resolved_color ) { |
| 569 | return $resolved_color; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | return get_global_style_color( $attribute_name, $default_color ); |
| 574 | } |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | function get_global_style_color( $style_key, $default_color ) { |
| 585 | $global_styles = wp_get_global_styles( |
| 586 | array( 'color' ), |
| 587 | array( |
| 588 | 'block_name' => 'core/button', |
| 589 | 'transforms' => array( 'resolve-variables' ), |
| 590 | ) |
| 591 | ); |
| 592 | |
| 593 | if ( isset( $global_styles[ $style_key ] ) ) { |
| 594 | return $global_styles[ $style_key ]; |
| 595 | } |
| 596 | |
| 597 | return $default_color; |
| 598 | } |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | function get_color_from_slug( $slug ) { |
| 608 | $color_palettes = wp_get_global_settings( array( 'color', 'palette' ) ); |
| 609 | |
| 610 | if ( ! is_array( $color_palettes ) ) { |
| 611 | return null; |
| 612 | } |
| 613 | |
| 614 | foreach ( $color_palettes as $palette ) { |
| 615 | if ( is_array( $palette ) ) { |
| 616 | foreach ( $palette as $color ) { |
| 617 | if ( isset( $color['slug'] ) && $color['slug'] === $slug && isset( $color['color'] ) ) { |
| 618 | return $color['color']; |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | return null; |
| 625 | } |
| 626 | |
| 627 | |
| 628 | |
| 629 | |
| 630 | function is_jetpack_memberships_loaded(): bool { |
| 631 | return class_exists( '\Jetpack_Memberships' ); |
| 632 | } |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | |
| 638 | |
| 639 | |
| 640 | |
| 641 | function render_block( $attributes ) { |
| 642 | |
| 643 | if ( ! ( new Modules() )->is_active( 'subscriptions' ) ) { |
| 644 | return ''; |
| 645 | } |
| 646 | |
| 647 | if ( is_jetpack_memberships_loaded() ) { |
| 648 | |
| 649 | Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); |
| 650 | } else { |
| 651 | Jetpack_Gutenberg::load_styles_as_required( FEATURE_NAME ); |
| 652 | } |
| 653 | |
| 654 | if ( ! class_exists( 'Jetpack_Subscriptions_Widget' ) ) { |
| 655 | return ''; |
| 656 | } |
| 657 | |
| 658 | |
| 659 | $subscribe_email = Jetpack_Memberships::get_current_user_email(); |
| 660 | |
| 661 | |
| 662 | if ( empty( $subscribe_email ) ) { |
| 663 | $current_user = wp_get_current_user(); |
| 664 | if ( ! empty( $current_user->user_email ) ) { |
| 665 | $subscribe_email = $current_user->user_email; |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | |
| 670 | ++Jetpack_Subscriptions_Widget::$instance_count; |
| 671 | |
| 672 | $classes = get_element_class_names_from_attributes( $attributes ); |
| 673 | $styles = get_element_styles_from_attributes( $attributes ); |
| 674 | |
| 675 | |
| 676 | |
| 677 | |
| 678 | |
| 679 | |
| 680 | $include_social_followers = isset( $attributes['includeSocialFollowers'] ) ? (bool) get_attribute( $attributes, 'includeSocialFollowers' ) : true; |
| 681 | |
| 682 | $data = array( |
| 683 | 'widget_id' => Jetpack_Subscriptions_Widget::$instance_count, |
| 684 | 'subscribe_email' => $subscribe_email, |
| 685 | 'is_paid_subscriber' => get_attribute( $attributes, 'isPaidSubscriber', false ), |
| 686 | 'wrapper_attributes' => get_block_wrapper_attributes( |
| 687 | array( |
| 688 | 'class' => $classes['block_wrapper'], |
| 689 | ) |
| 690 | ), |
| 691 | 'subscribe_placeholder' => get_attribute( $attributes, 'subscribePlaceholder', __( 'Type your email…', 'jetpack' ) ), |
| 692 | 'submit_button_text' => get_attribute( $attributes, 'submitButtonText', __( 'Subscribe', 'jetpack' ) ), |
| 693 | 'submit_button_text_subscribed' => get_attribute( $attributes, 'submitButtonTextSubscribed', __( 'Subscribed', 'jetpack' ) ), |
| 694 | 'submit_button_text_upgrade' => get_attribute( $attributes, 'submitButtonTextUpgrade', __( 'Upgrade subscription', 'jetpack' ) ), |
| 695 | 'success_message' => get_attribute( |
| 696 | $attributes, |
| 697 | 'successMessage', |
| 698 | esc_html__( "Success! An email was just sent to confirm your subscription. Please find the email now and click 'Confirm' to start subscribing.", 'jetpack' ) |
| 699 | ), |
| 700 | 'show_subscribers_total' => (bool) get_attribute( $attributes, 'showSubscribersTotal' ), |
| 701 | 'subscribers_total' => get_attribute( $attributes, 'showSubscribersTotal' ) ? get_subscriber_count( $include_social_followers ) : 0, |
| 702 | 'referer' => esc_url_raw( |
| 703 | ( is_ssl() ? 'https' : 'http' ) . '://' . ( isset( $_SERVER['HTTP_HOST'] ) ? wp_unslash( $_SERVER['HTTP_HOST'] ) : '' ) . |
| 704 | ( isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : '' ) |
| 705 | ), |
| 706 | 'source' => 'subscribe-block', |
| 707 | 'app_source' => get_attribute( $attributes, 'appSource', null ), |
| 708 | 'class_name' => get_attribute( $attributes, 'className' ), |
| 709 | 'selected_newsletter_categories' => get_attribute( $attributes, 'selectedNewsletterCategoryIds', array() ), |
| 710 | 'preselected_newsletter_categories' => get_attribute( $attributes, 'preselectNewsletterCategories', false ), |
| 711 | ); |
| 712 | |
| 713 | |
| 714 | if ( is_feed() || wp_is_xml_request() || |
| 715 | ( defined( 'REST_REQUEST' ) && REST_REQUEST && ! wp_is_json_request() ) || |
| 716 | ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ) || |
| 717 | ( defined( 'WP_CLI' ) && WP_CLI ) || |
| 718 | wp_is_jsonp_request() ) { |
| 719 | return render_for_email( $data, $styles ); |
| 720 | } |
| 721 | |
| 722 | return render_for_website( $data, $classes, $styles ); |
| 723 | } |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | |
| 729 | |
| 730 | function get_post_access_level_for_current_post() { |
| 731 | if ( ! is_singular() ) { |
| 732 | |
| 733 | return Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_EVERYBODY; |
| 734 | } |
| 735 | |
| 736 | return Jetpack_Memberships::get_post_access_level(); |
| 737 | } |
| 738 | |
| 739 | |
| 740 | |
| 741 | |
| 742 | |
| 743 | |
| 744 | |
| 745 | |
| 746 | |
| 747 | |
| 748 | function render_for_website( $data, $classes, $styles ) { |
| 749 | $lang = get_locale(); |
| 750 | $blog_id = \Jetpack_Options::get_option( 'id' ); |
| 751 | $widget_id_suffix = Jetpack_Subscriptions_Widget::$instance_count > 1 ? '-' . Jetpack_Subscriptions_Widget::$instance_count : ''; |
| 752 | $form_id = 'subscribe-blog' . $widget_id_suffix; |
| 753 | $form_url = 'https://wordpress.com/email-subscriptions'; |
| 754 | $post_access_level = get_post_access_level_for_current_post(); |
| 755 | $is_button_only_style = ! empty( $data['class_name'] ) ? is_button_only_style( $data['class_name'] ) : false; |
| 756 | |
| 757 | |
| 758 | $post_id = null; |
| 759 | if ( in_the_loop() ) { |
| 760 | $post_id = get_the_ID(); |
| 761 | } elseif ( is_singular( 'post' ) || is_page() ) { |
| 762 | $post_id = get_queried_object_id(); |
| 763 | } else { |
| 764 | $post_id = get_option( 'page_on_front' ); |
| 765 | } |
| 766 | |
| 767 | $subscribe_field_id = apply_filters( 'subscribe_field_id', 'subscribe-field' . $widget_id_suffix, $data['widget_id'] ); |
| 768 | $tier_id = get_post_meta( $post_id, META_NAME_FOR_POST_TIER_ID_SETTINGS, true ); |
| 769 | $is_subscribed = Jetpack_Memberships::is_current_user_subscribed(); |
| 770 | $button_text = get_submit_button_text( $data ); |
| 771 | $show_subscriber_count = $data['show_subscribers_total'] && $data['subscribers_total'] && ! $is_subscribed; |
| 772 | |
| 773 | ob_start(); |
| 774 | |
| 775 | Jetpack_Subscriptions_Widget::render_widget_status_messages( |
| 776 | array( |
| 777 | 'success_message' => $data['success_message'], |
| 778 | ) |
| 779 | ); |
| 780 | ?> |
| 781 | <div <?php echo wp_kses_data( $data['wrapper_attributes'] ); ?>> |
| 782 | <div class="wp-block-jetpack-subscriptions__container<?php echo ! $is_subscribed ? ' is-not-subscriber' : ''; ?>"> |
| 783 | <?php if ( is_top_subscription() ) : ?> |
| 784 | <p id="subscribe-submit" class="is-link" |
| 785 | <?php if ( ! empty( $styles['submit_button_wrapper'] ) ) : ?> |
| 786 | style="<?php echo esc_attr( $styles['submit_button_wrapper'] ); ?>" |
| 787 | <?php endif; ?> |
| 788 | > |
| 789 | <a |
| 790 | href="<?php echo esc_url( 'https://wordpress.com/reader/site/subscription/' . $blog_id ); ?>" |
| 791 | <?php if ( ! empty( $classes['submit_button'] ) ) : ?> |
| 792 | class="<?php echo esc_attr( $classes['submit_button'] ); ?>" |
| 793 | <?php endif; ?> |
| 794 | <?php if ( ! empty( $styles['submit_button'] ) ) : ?> |
| 795 | style="<?php echo esc_attr( $styles['submit_button'] ); ?>" |
| 796 | <?php endif; ?> |
| 797 | > |
| 798 | <?php echo sanitize_submit_text( $button_text ); ?> |
| 799 | </a> |
| 800 | </p> |
| 801 | <?php else : ?> |
| 802 | <form |
| 803 | action="<?php echo esc_url( $form_url ); ?>" |
| 804 | method="post" |
| 805 | accept-charset="utf-8" |
| 806 | data-blog="<?php echo esc_attr( $blog_id ); ?>" |
| 807 | data-post_access_level="<?php echo esc_attr( $post_access_level ); ?>" |
| 808 | data-subscriber_email="<?php echo esc_attr( $data['subscribe_email'] ); ?>" |
| 809 | id="<?php echo esc_attr( $form_id ); ?>" |
| 810 | > |
| 811 | <div class="wp-block-jetpack-subscriptions__form-elements"> |
| 812 | <?php if ( ! $is_subscribed && ! $is_button_only_style ) : ?> |
| 813 | <p id="subscribe-email"> |
| 814 | <label |
| 815 | id="<?php echo esc_attr( $subscribe_field_id . '-label' ); ?>" |
| 816 | for="<?php echo esc_attr( $subscribe_field_id ); ?>" |
| 817 | class="screen-reader-text" |
| 818 | > |
| 819 | <?php echo esc_html( $data['subscribe_placeholder'] ); ?> |
| 820 | </label> |
| 821 | <?php |
| 822 | printf( |
| 823 | '<input |
| 824 | required="required" |
| 825 | type="email" |
| 826 | name="email" |
| 827 | autocomplete="email" |
| 828 | %1$s |
| 829 | style="%2$s" |
| 830 | placeholder="%3$s" |
| 831 | value="%4$s" |
| 832 | id="%5$s" |
| 833 | %6$s |
| 834 | />', |
| 835 | ( ! empty( $classes['email_field'] ) |
| 836 | ? 'class="' . esc_attr( $classes['email_field'] ) . '"' |
| 837 | : '' |
| 838 | ), |
| 839 | ( ! empty( $styles['email_field'] ) |
| 840 | ? esc_attr( $styles['email_field'] ) |
| 841 | : 'width: 95%; padding: 1px 10px' |
| 842 | ), |
| 843 | esc_attr( $data['subscribe_placeholder'] ), |
| 844 | esc_attr( $data['subscribe_email'] ), |
| 845 | esc_attr( $subscribe_field_id ), |
| 846 | ( ! empty( $data['subscribe_email'] ) |
| 847 | ? 'disabled title="' . esc_attr__( "You're logged in with this email", 'jetpack' ) . '"' |
| 848 | : 'title="' . esc_attr__( 'Please fill in this field.', 'jetpack' ) . '"' |
| 849 | ) |
| 850 | ); |
| 851 | ?> |
| 852 | </p> |
| 853 | <?php endif; ?> |
| 854 | <p id="subscribe-submit" |
| 855 | <?php if ( ! empty( $styles['submit_button_wrapper'] ) ) : ?> |
| 856 | style="<?php echo esc_attr( $styles['submit_button_wrapper'] ); ?>" |
| 857 | <?php endif; ?> |
| 858 | > |
| 859 | <input type="hidden" name="action" value="subscribe"/> |
| 860 | <input type="hidden" name="blog_id" value="<?php echo (int) $blog_id; ?>"/> |
| 861 | <input type="hidden" name="source" value="<?php echo esc_url( $data['referer'] ); ?>"/> |
| 862 | <input type="hidden" name="sub-type" value="<?php echo esc_attr( $data['source'] ); ?>"/> |
| 863 | <input type="hidden" name="app_source" value="<?php echo esc_attr( $data['app_source'] ); ?>"/> |
| 864 | <input type="hidden" name="redirect_fragment" value="<?php echo esc_attr( $form_id ); ?>"/> |
| 865 | <input type="hidden" name="lang" value="<?php echo esc_attr( $lang ); ?>"/> |
| 866 | <?php |
| 867 | wp_nonce_field( 'blogsub_subscribe_' . $blog_id ); |
| 868 | |
| 869 | if ( ! empty( $post_id ) ) { |
| 870 | echo '<input type="hidden" name="post_id" value="' . esc_attr( $post_id ) . '"/>'; |
| 871 | } |
| 872 | |
| 873 | if ( ! empty( $tier_id ) ) { |
| 874 | echo '<input type="hidden" name="tier_id" value="' . esc_attr( $tier_id ) . '"/>'; |
| 875 | } |
| 876 | |
| 877 | if ( $data['preselected_newsletter_categories'] && ! empty( $data['selected_newsletter_categories'] ) ) { |
| 878 | echo '<input type="hidden" name="selected_newsletter_categories" value="' . esc_attr( implode( ',', $data['selected_newsletter_categories'] ) ) . '"/>'; |
| 879 | } |
| 880 | ?> |
| 881 | <button type="submit" |
| 882 | <?php if ( ! empty( $classes['submit_button'] ) ) : ?> |
| 883 | class="<?php echo esc_attr( $classes['submit_button'] ); ?>" |
| 884 | <?php endif; ?> |
| 885 | <?php if ( ! empty( $styles['submit_button'] ) ) : ?> |
| 886 | style="<?php echo esc_attr( $styles['submit_button'] ); ?>" |
| 887 | <?php endif; ?> |
| 888 | name="jetpack_subscriptions_widget" |
| 889 | > |
| 890 | <?php echo sanitize_submit_text( $button_text ); ?> |
| 891 | </button> |
| 892 | </p> |
| 893 | </div> |
| 894 | </form> |
| 895 | <?php endif; ?> |
| 896 | <?php if ( $show_subscriber_count ) : ?> |
| 897 | <div class="wp-block-jetpack-subscriptions__subscount"> |
| 898 | <?php echo esc_html( Jetpack_Memberships::get_join_others_text( $data['subscribers_total'] ) ); ?> |
| 899 | </div> |
| 900 | <?php endif; ?> |
| 901 | </div> |
| 902 | </div> |
| 903 | <?php |
| 904 | return ob_get_clean(); |
| 905 | } |
| 906 | |
| 907 | |
| 908 | |
| 909 | |
| 910 | |
| 911 | |
| 912 | |
| 913 | |
| 914 | |
| 915 | function render_for_email( $data, $styles ) { |
| 916 | $submit_button_wrapper_style = ! empty( $styles['submit_button_wrapper'] ) ? 'style="' . esc_attr( $styles['submit_button_wrapper'] ) . '"' : ''; |
| 917 | $button_text = get_submit_button_text( $data ); |
| 918 | |
| 919 | $html = '<div ' . wp_kses_data( $data['wrapper_attributes'] ) . '> |
| 920 | <div> |
| 921 | <div> |
| 922 | <div> |
| 923 | <p ' . $submit_button_wrapper_style . '> |
| 924 | <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> |
| 925 | </p> |
| 926 | </div> |
| 927 | </div> |
| 928 | </div> |
| 929 | </div>'; |
| 930 | |
| 931 | return $html; |
| 932 | } |
| 933 | |
| 934 | |
| 935 | |
| 936 | |
| 937 | |
| 938 | |
| 939 | |
| 940 | |
| 941 | |
| 942 | |
| 943 | function render_email( $block_content, array $parsed_block, $rendering_context ) { |
| 944 | 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' ) ) { |
| 945 | return ''; |
| 946 | } |
| 947 | |
| 948 | |
| 949 | $button_attributes = array( |
| 950 | 'text' => ! empty( $parsed_block['attrs']['submitButtonText'] ) ? sanitize_text_field( $parsed_block['attrs']['submitButtonText'] ) : __( 'Subscribe', 'jetpack' ), |
| 951 | 'url' => get_post_permalink(), |
| 952 | 'element' => 'a', |
| 953 | |
| 954 | 'backgroundColor' => $parsed_block['attrs']['buttonBackgroundColor'] ?? null, |
| 955 | 'customBackgroundColor' => $parsed_block['attrs']['customButtonBackgroundColor'] ?? null, |
| 956 | |
| 957 | 'textColor' => $parsed_block['attrs']['textColor'] ?? null, |
| 958 | 'customTextColor' => $parsed_block['attrs']['customTextColor'] ?? null, |
| 959 | |
| 960 | 'borderRadius' => $parsed_block['attrs']['borderRadius'] ?? 0, |
| 961 | 'borderWeight' => $parsed_block['attrs']['borderWeight'] ?? 1, |
| 962 | 'borderColor' => $parsed_block['attrs']['borderColor'] ?? null, |
| 963 | 'customBorderColor' => $parsed_block['attrs']['customBorderColor'] ?? null, |
| 964 | |
| 965 | 'fontSize' => $parsed_block['attrs']['fontSize'] ?? null, |
| 966 | 'customFontSize' => $parsed_block['attrs']['customFontSize'] ?? null, |
| 967 | |
| 968 | 'padding' => $parsed_block['attrs']['padding'] ?? null, |
| 969 | ); |
| 970 | |
| 971 | |
| 972 | $button_parsed_block = array( |
| 973 | 'attrs' => $button_attributes, |
| 974 | 'email_attrs' => $parsed_block['email_attrs'] ?? array(), |
| 975 | ); |
| 976 | |
| 977 | |
| 978 | return \Automattic\Jetpack\Extensions\Button\render_email( |
| 979 | $block_content, |
| 980 | $button_parsed_block, |
| 981 | $rendering_context |
| 982 | ); |
| 983 | } |
| 984 | |
| 985 | |
| 986 | |
| 987 | |
| 988 | |
| 989 | |
| 990 | |
| 991 | |
| 992 | |
| 993 | function jetpack_filter_excerpt_for_newsletter( $excerpt, $post = null ) { |
| 994 | |
| 995 | |
| 996 | if ( $post instanceof \WP_Post && has_block( 'jetpack/subscriptions', $post ) ) { |
| 997 | $excerpt .= '<br/><br/>'; |
| 998 | $excerpt .= sprintf( |
| 999 | |
| 1000 | __( "<p><a href='%s'>View post</a> to subscribe to the site's newsletter.</p>", 'jetpack' ), |
| 1001 | get_post_permalink() |
| 1002 | ); |
| 1003 | } |
| 1004 | |
| 1005 | return $excerpt; |
| 1006 | } |
| 1007 | |
| 1008 | |
| 1009 | |
| 1010 | |
| 1011 | |
| 1012 | |
| 1013 | |
| 1014 | |
| 1015 | function add_paywall( $the_content ) { |
| 1016 | require_once JETPACK__PLUGIN_DIR . 'modules/memberships/class-jetpack-memberships.php'; |
| 1017 | |
| 1018 | $post_access_level = Jetpack_Memberships::get_post_access_level(); |
| 1019 | |
| 1020 | if ( Jetpack_Memberships::user_can_view_post() ) { |
| 1021 | if ( $post_access_level !== Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_EVERYBODY ) { |
| 1022 | do_action( |
| 1023 | 'earn_track_paywalled_post_view', |
| 1024 | array( |
| 1025 | 'post_id' => get_the_ID(), |
| 1026 | ) |
| 1027 | ); |
| 1028 | } |
| 1029 | return $the_content; |
| 1030 | } |
| 1031 | |
| 1032 | $paywalled_content = get_paywall_content(); |
| 1033 | |
| 1034 | if ( has_block( \Automattic\Jetpack\Extensions\Paywall\BLOCK_NAME ) ) { |
| 1035 | if ( strpos( $the_content, \Automattic\Jetpack\Extensions\Paywall\BLOCK_HTML ) ) { |
| 1036 | return strstr( $the_content, \Automattic\Jetpack\Extensions\Paywall\BLOCK_HTML, true ) . $paywalled_content; |
| 1037 | } |
| 1038 | |
| 1039 | |
| 1040 | if ( strpos( $the_content, \Automattic\Jetpack\Extensions\Paywall\THE_EXCERPT_BLOCK ) ) { |
| 1041 | return strstr( $the_content, \Automattic\Jetpack\Extensions\Paywall\THE_EXCERPT_BLOCK, true ); |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | return $paywalled_content; |
| 1046 | } |
| 1047 | |
| 1048 | |
| 1049 | |
| 1050 | |
| 1051 | |
| 1052 | |
| 1053 | |
| 1054 | |
| 1055 | |
| 1056 | function maybe_close_comments( $default_comments_open, $post_id ) { |
| 1057 | if ( ! $default_comments_open || ! $post_id ) { |
| 1058 | return $default_comments_open; |
| 1059 | } |
| 1060 | |
| 1061 | require_once JETPACK__PLUGIN_DIR . 'modules/memberships/class-jetpack-memberships.php'; |
| 1062 | return Jetpack_Memberships::user_can_view_post(); |
| 1063 | } |
| 1064 | |
| 1065 | |
| 1066 | |
| 1067 | |
| 1068 | |
| 1069 | |
| 1070 | |
| 1071 | |
| 1072 | function maybe_gate_existing_comments( $comment ) { |
| 1073 | if ( empty( $comment ) ) { |
| 1074 | return $comment; |
| 1075 | } |
| 1076 | |
| 1077 | require_once JETPACK__PLUGIN_DIR . 'modules/memberships/class-jetpack-memberships.php'; |
| 1078 | if ( Jetpack_Memberships::user_can_view_post() ) { |
| 1079 | return $comment; |
| 1080 | } |
| 1081 | return ''; |
| 1082 | } |
| 1083 | |
| 1084 | |
| 1085 | |
| 1086 | |
| 1087 | |
| 1088 | |
| 1089 | function is_jetpack_token_subscription_service_loaded(): bool { |
| 1090 | return class_exists( 'Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Jetpack_Token_Subscription_Service' ); |
| 1091 | } |
| 1092 | |
| 1093 | |
| 1094 | |
| 1095 | |
| 1096 | function maybe_prevent_super_cache_caching() { |
| 1097 | |
| 1098 | if ( is_jetpack_token_subscription_service_loaded() ) { |
| 1099 | do_action( 'wpsc_add_cookie', Jetpack_Token_Subscription_Service::JWT_AUTH_TOKEN_COOKIE_NAME ); |
| 1100 | } |
| 1101 | |
| 1102 | if ( is_user_auth() ) { |
| 1103 | |
| 1104 | if ( ! defined( 'DONOTCACHEPAGE' ) ) { |
| 1105 | define( 'DONOTCACHEPAGE', true ); |
| 1106 | } |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | |
| 1111 | |
| 1112 | |
| 1113 | |
| 1114 | |
| 1115 | function get_paywall_content() { |
| 1116 | if ( Jetpack_Memberships::user_is_pending_subscriber() ) { |
| 1117 | return get_paywall_blocks_subscribe_pending(); |
| 1118 | } |
| 1119 | if ( doing_filter( 'get_the_excerpt' ) ) { |
| 1120 | return ''; |
| 1121 | } |
| 1122 | return get_paywall_blocks(); |
| 1123 | } |
| 1124 | |
| 1125 | |
| 1126 | |
| 1127 | |
| 1128 | |
| 1129 | |
| 1130 | |
| 1131 | |
| 1132 | |
| 1133 | function get_current_url() { |
| 1134 | |
| 1135 | $current_url = site_url(); |
| 1136 | |
| 1137 | if ( isset( $_SERVER ) && is_array( $_SERVER ) ) { |
| 1138 | |
| 1139 | $scheme = isset( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ? 'https' : 'http'; |
| 1140 | $host = ! empty( $_SERVER['HTTP_HOST'] ) ? wp_unslash( $_SERVER['HTTP_HOST'] ) : null; |
| 1141 | $path = ! empty( $_SERVER['REQUEST_URI'] ) ? wp_unslash( $_SERVER['REQUEST_URI'] ) : ''; |
| 1142 | |
| 1143 | |
| 1144 | if ( ! empty( $_SERVER['HTTP_X_ORIGINAL_HOST'] ) && str_contains( $_SERVER['HTTP_X_ORIGINAL_HOST'], 'ngrok.io' ) ) { |
| 1145 | $host = wp_unslash( $_SERVER['HTTP_X_ORIGINAL_HOST'] ); |
| 1146 | } |
| 1147 | |
| 1148 | |
| 1149 | if ( $host ) { |
| 1150 | $current_url = esc_url_raw( sprintf( '%s://%s%s', $scheme, $host, $path ) ); |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | return $current_url; |
| 1155 | } |
| 1156 | |
| 1157 | |
| 1158 | |
| 1159 | |
| 1160 | |
| 1161 | |
| 1162 | |
| 1163 | |
| 1164 | function get_submit_button_text( $data ) { |
| 1165 | if ( ! Jetpack_Memberships::is_current_user_subscribed() ) { |
| 1166 | return $data['submit_button_text']; |
| 1167 | } |
| 1168 | if ( ! Jetpack_Memberships::user_can_view_post() ) { |
| 1169 | return $data['submit_button_text_upgrade']; |
| 1170 | } |
| 1171 | return '✓ ' . $data['submit_button_text_subscribed']; |
| 1172 | } |
| 1173 | |
| 1174 | |
| 1175 | |
| 1176 | |
| 1177 | |
| 1178 | |
| 1179 | function is_top_subscription() { |
| 1180 | if ( ! Jetpack_Memberships::is_current_user_subscribed() ) { |
| 1181 | return false; |
| 1182 | } |
| 1183 | if ( ! Jetpack_Memberships::user_can_view_post() ) { |
| 1184 | return false; |
| 1185 | } |
| 1186 | return true; |
| 1187 | } |
| 1188 | |
| 1189 | |
| 1190 | |
| 1191 | |
| 1192 | |
| 1193 | |
| 1194 | |
| 1195 | |
| 1196 | function sanitize_submit_text( $text ) { |
| 1197 | return wp_kses( |
| 1198 | html_entity_decode( $text, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401 ), |
| 1199 | Jetpack_Subscriptions_Widget::$allowed_html_tags_for_submit_button |
| 1200 | ); |
| 1201 | } |
| 1202 | |
| 1203 | |
| 1204 | |
| 1205 | |
| 1206 | |
| 1207 | |
| 1208 | function get_paywall_blocks() { |
| 1209 | $custom_paywall = apply_filters( 'jetpack_custom_paywall_blocks', false ); |
| 1210 | if ( ! empty( $custom_paywall ) ) { |
| 1211 | return $custom_paywall; |
| 1212 | } |
| 1213 | |
| 1214 | if ( ! Request::is_frontend() ) { |
| 1215 | return get_paywall_simple(); |
| 1216 | } |
| 1217 | |
| 1218 | require_once JETPACK__PLUGIN_DIR . 'modules/memberships/class-jetpack-memberships.php'; |
| 1219 | $is_paid_post = is_paid_post(); |
| 1220 | $is_paid_subscriber = Jetpack_Memberships::user_is_paid_subscriber(); |
| 1221 | |
| 1222 | $access_heading = $is_paid_subscriber |
| 1223 | ? esc_html__( 'Upgrade to continue reading', 'jetpack' ) |
| 1224 | : esc_html__( 'Subscribe to continue reading', 'jetpack' ); |
| 1225 | |
| 1226 | $subscribe_text = $is_paid_post |
| 1227 | |
| 1228 | ? ( |
| 1229 | $is_paid_subscriber |
| 1230 | ? esc_html__( 'Upgrade to get access to the rest of this post and other exclusive content.', 'jetpack' ) |
| 1231 | : esc_html__( 'Become a paid subscriber to get access to the rest of this post and other exclusive content.', 'jetpack' ) |
| 1232 | ) |
| 1233 | |
| 1234 | : esc_html__( 'Subscribe to get access to the rest of this post and other subscriber-only content.', 'jetpack' ); |
| 1235 | |
| 1236 | $login_block = ''; |
| 1237 | |
| 1238 | if ( is_user_auth() ) { |
| 1239 | if ( ( new Host() )->is_wpcom_simple() ) { |
| 1240 | |
| 1241 | $login_link = add_query_arg( |
| 1242 | array( |
| 1243 | 'redirect_to' => rawurlencode( get_current_url() ), |
| 1244 | 'blog_id' => get_current_blog_id(), |
| 1245 | ), |
| 1246 | 'https://wordpress.com/log-in/link' |
| 1247 | ); |
| 1248 | $switch_accounts_link = wp_logout_url( $login_link ); |
| 1249 | $login_block = '<!-- wp:paragraph {"align":"center","style":{"typography":{"fontSize":"14px"}}} --> |
| 1250 | <p class="has-text-align-center" style="font-size:14px"> |
| 1251 | <a href="' . $switch_accounts_link . '">' . __( 'Switch accounts', 'jetpack' ) . '</a> |
| 1252 | </p> |
| 1253 | <!-- /wp:paragraph -->'; |
| 1254 | } |
| 1255 | } else { |
| 1256 | $access_question = $is_paid_post ? esc_html__( 'Already a paid subscriber?', 'jetpack' ) : esc_html__( 'Already a subscriber?', 'jetpack' ); |
| 1257 | $login_block = '<!-- wp:group {"style":{"typography":{"fontSize":"14px"}},"layout":{"type":"flex","justifyContent":"center"}} --> |
| 1258 | <div class="wp-block-group" style="font-size:14px"> |
| 1259 | <!-- wp:jetpack/subscriber-login {"logInLabel":"' . $access_question . '"} /--> |
| 1260 | </div> |
| 1261 | <!-- /wp:group -->'; |
| 1262 | } |
| 1263 | |
| 1264 | $lock_svg = plugins_url( 'images/lock-paywall.svg', JETPACK__PLUGIN_FILE ); |
| 1265 | |
| 1266 | return ' |
| 1267 | <!-- 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"}} --> |
| 1268 | <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"> |
| 1269 | <!-- wp:image {"align":"center","width":24,"height":24,"sizeSlug":"large","linkDestination":"none"} --> |
| 1270 | <figure class="wp-block-image aligncenter size-large is-resized"><img src="' . $lock_svg . '" alt="" width="24" height="24"/></figure> |
| 1271 | <!-- /wp:image --> |
| 1272 | |
| 1273 | <!-- wp:heading {"textAlign":"center","style":{"typography":{"fontStyle":"normal","fontWeight":"600","fontSize":"24px"},"layout":{"selfStretch":"fit"}}} --> |
| 1274 | <h2 class="wp-block-heading has-text-align-center" style="font-size:24px;font-style:normal;font-weight:600">' . $access_heading . '</h2> |
| 1275 | <!-- /wp:heading --> |
| 1276 | |
| 1277 | <!-- wp:paragraph {"align":"center","style":{"typography":{"fontSize":"14px"},"spacing":{"margin":{"top":"10px","bottom":"10px"}}}} --> |
| 1278 | <p class="has-text-align-center" style="margin-top:10px;margin-bottom:10px;font-size:14px">' . $subscribe_text . '</p> |
| 1279 | <!-- /wp:paragraph --> |
| 1280 | |
| 1281 | <!-- wp:jetpack/subscriptions {"borderRadius":50,"borderColor":"primary","className":"is-style-compact","isPaidSubscriber":' . ( $is_paid_subscriber ? 'true' : 'false' ) . '} /--> |
| 1282 | ' . $login_block . ' |
| 1283 | </div> |
| 1284 | <!-- /wp:group --> |
| 1285 | '; |
| 1286 | } |
| 1287 | |
| 1288 | |
| 1289 | |
| 1290 | |
| 1291 | |
| 1292 | |
| 1293 | function is_user_auth(): bool { |
| 1294 | if ( ( new Host() )->is_wpcom_simple() && is_user_logged_in() ) { |
| 1295 | return true; |
| 1296 | } |
| 1297 | if ( current_user_can( 'manage_options' ) ) { |
| 1298 | return true; |
| 1299 | } |
| 1300 | |
| 1301 | if ( is_jetpack_token_subscription_service_loaded() ) { |
| 1302 | if ( Jetpack_Token_Subscription_Service::has_token_from_cookie() ) { |
| 1303 | return true; |
| 1304 | } |
| 1305 | } |
| 1306 | return false; |
| 1307 | } |
| 1308 | |
| 1309 | |
| 1310 | |
| 1311 | |
| 1312 | function is_paid_post(): bool { |
| 1313 | require_once JETPACK__PLUGIN_DIR . 'modules/memberships/class-jetpack-memberships.php'; |
| 1314 | |
| 1315 | |
| 1316 | if ( Jetpack_Memberships::has_connected_account() && is_jetpack_token_subscription_service_loaded() ) { |
| 1317 | return Jetpack_Memberships::get_post_access_level() === Jetpack_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS; |
| 1318 | } |
| 1319 | |
| 1320 | return false; |
| 1321 | } |
| 1322 | |
| 1323 | |
| 1324 | |
| 1325 | |
| 1326 | function is_subscribers_post(): bool { |
| 1327 | require_once JETPACK__PLUGIN_DIR . 'modules/memberships/class-jetpack-memberships.php'; |
| 1328 | |
| 1329 | |
| 1330 | if ( Jetpack_Memberships::has_connected_account() && is_jetpack_token_subscription_service_loaded() ) { |
| 1331 | return Jetpack_Memberships::get_post_access_level() === Jetpack_Token_Subscription_Service::POST_ACCESS_LEVEL_SUBSCRIBERS; |
| 1332 | } |
| 1333 | |
| 1334 | return false; |
| 1335 | } |
| 1336 | |
| 1337 | |
| 1338 | |
| 1339 | |
| 1340 | |
| 1341 | |
| 1342 | function get_paywall_blocks_subscribe_pending() { |
| 1343 | $subscribe_email = Jetpack_Memberships::get_current_user_email(); |
| 1344 | |
| 1345 | |
| 1346 | if ( is_wpcom() || false !== apply_filters( 'jetpack_auto_fill_logged_in_user', false ) ) { |
| 1347 | $current_user = wp_get_current_user(); |
| 1348 | if ( ! empty( $current_user->user_email ) ) { |
| 1349 | $subscribe_email = $current_user->user_email; |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | $access_heading = esc_html__( 'Confirm your subscription to continue reading', 'jetpack' ); |
| 1354 | |
| 1355 | |
| 1356 | $subscribe_text = sprintf( esc_html__( 'Head to your inbox and confirm your email address %s.', 'jetpack' ), $subscribe_email ); |
| 1357 | |
| 1358 | $lock_svg = plugins_url( 'images/lock-paywall.svg', JETPACK__PLUGIN_FILE ); |
| 1359 | |
| 1360 | return ' |
| 1361 | <!-- 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"}} --> |
| 1362 | <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"> |
| 1363 | <!-- wp:image {"align":"center","width":24,"height":24,"sizeSlug":"large","linkDestination":"none"} --> |
| 1364 | <figure class="wp-block-image aligncenter size-large is-resized"><img src="' . $lock_svg . '" alt="" width="24" height="24"/></figure> |
| 1365 | <!-- /wp:image --> |
| 1366 | |
| 1367 | <!-- wp:heading {"textAlign":"center","style":{"typography":{"fontStyle":"normal","fontWeight":"600","fontSize":"24px", "maxWidth":"initial"},"layout":{"selfStretch":"fit"}}} --> |
| 1368 | <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> |
| 1369 | <!-- /wp:heading --> |
| 1370 | |
| 1371 | <!-- wp:paragraph {"align":"center","style":{"typography":{"fontSize":"14px"},"spacing":{"margin":{"top":"10px","bottom":"10px"}}}} --> |
| 1372 | <p class="has-text-align-center" style="margin-top:10px;margin-bottom:10px;font-size:14px">' . $subscribe_text . '</p> |
| 1373 | <!-- /wp:paragraph --> |
| 1374 | </div> |
| 1375 | <!-- /wp:group --> |
| 1376 | '; |
| 1377 | } |
| 1378 | |
| 1379 | |
| 1380 | |
| 1381 | |
| 1382 | function get_paywall_simple(): string { |
| 1383 | $is_paid_post = is_paid_post(); |
| 1384 | $is_subscribers_post = is_subscribers_post(); |
| 1385 | $is_subscriber = is_jetpack_memberships_loaded() && Jetpack_Memberships::is_current_user_subscribed(); |
| 1386 | $paywall_heading = esc_html__( 'Subscribe to keep reading', 'jetpack' ); |
| 1387 | |
| 1388 | if ( $is_subscribers_post && ! $is_subscriber ) { |
| 1389 | $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' ); |
| 1390 | $paywall_action_btn = esc_html__( 'Subscribe', 'jetpack' ); |
| 1391 | } elseif ( $is_paid_post && $is_subscriber ) { |
| 1392 | $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' ); |
| 1393 | $paywall_action_btn = esc_html__( 'Upgrade subscription', 'jetpack' ); |
| 1394 | } else { |
| 1395 | |
| 1396 | |
| 1397 | $paywall_description = esc_html__( 'Become a paid subscriber to get access to the rest of this post and other exclusive content.', 'jetpack' ); |
| 1398 | $paywall_action_btn = esc_html__( 'Subscribe', 'jetpack' ); |
| 1399 | } |
| 1400 | |
| 1401 | return ' |
| 1402 | <!-- wp:columns --> |
| 1403 | <div class="wp-block-columns jetpack-paywall-simple" style="display: inline-block; width: 90%"> |
| 1404 | <!-- wp:column --> |
| 1405 | <div class="wp-block-column" style="background-color: #F6F7F7; padding: 32px; 24px;"> |
| 1406 | <!-- wp:heading --> |
| 1407 | <h2 class="has-text-align-center" style="margin: 0 0 12px; font-weight: 600;">' . $paywall_heading . '</h2> |
| 1408 | <!-- /wp:heading --> |
| 1409 | <!-- wp:paragraph --> |
| 1410 | <p class="has-text-align-center" |
| 1411 | style="text-align: center; |
| 1412 | color: #50575E; |
| 1413 | font-weight: 400; |
| 1414 | font-size: 16px; |
| 1415 | font-family: \'SF Pro Text\', sans-serif; |
| 1416 | line-height: 28.8px;"> |
| 1417 | ' . $paywall_description . ' |
| 1418 | </p> |
| 1419 | <!-- /wp:paragraph --> |
| 1420 | <!-- wp:buttons --> |
| 1421 | <div class="wp-block-buttons" style="text-align: center;"> |
| 1422 | <!-- wp:button --> |
| 1423 | <div class="wp-block-button" style="display: inline-block; margin: 10px 0; border-style: none; padding: 0;"> |
| 1424 | <a href="' . esc_url( get_post_permalink() ) . '" class="wp-block-button__link wp-element-button" |
| 1425 | data-wpcom-track data-tracks-link-desc="paywall-email-click" |
| 1426 | style="display: inline-block; |
| 1427 | padding: 12px 15px; |
| 1428 | background-color: #3858e9; |
| 1429 | color: #FFFFFF; |
| 1430 | text-decoration: none; |
| 1431 | border-radius: 5px; |
| 1432 | font-family: \'SF Pro Display\', sans-serif; |
| 1433 | font-weight: 500; |
| 1434 | font-size: 16px; |
| 1435 | text-align: center;">' . $paywall_action_btn . '</a> |
| 1436 | </div> |
| 1437 | <!-- /wp:button --> |
| 1438 | </div> |
| 1439 | <!-- /wp:buttons --> |
| 1440 | </div> |
| 1441 | <!-- /wp:column --> |
| 1442 | </div> |
| 1443 | <!-- /wp:columns --> |
| 1444 | '; |
| 1445 | } |