← All changes
|
extensions/blocks/premium-content/login-button/login-button.php
+82
-10
13.3.3
→
16.3-a.5
View file →
| @@ -10,8 +10,9 @@ | ||
| 10 | 10 | use Automattic\Jetpack\Blocks; |
| 11 | 11 | use Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Abstract_Token_Subscription_Service; |
| 12 | 12 | use Automattic\Jetpack\Status\Host; |
| 13 | 13 | use Jetpack_Gutenberg; |
| 14 | +use Jetpack_Options; | |
| 14 | 15 | |
| 15 | 16 | require_once dirname( __DIR__ ) . '/_inc/subscription-service/include.php'; |
| 16 | 17 | |
| 17 | 18 | const LOGIN_BUTTON_NAME = 'premium-content/login-button'; |
| @@ -24,9 +25,10 @@ | ||
| 24 | 25 | function register_login_button_block() { |
| 25 | 26 | Blocks::jetpack_register_block( |
| 26 | 27 | LOGIN_BUTTON_NAME, |
| 27 | 28 | array( |
| 28 | - 'render_callback' => __NAMESPACE__ . '\render_login_button_block', | |
| 29 | + 'render_callback' => __NAMESPACE__ . '\render_login_button_block', | |
| 30 | + 'render_email_callback' => __NAMESPACE__ . '\render_login_button_block_email', | |
| 29 | 31 | ) |
| 30 | 32 | ); |
| 31 | 33 | } |
| 32 | 34 | add_action( 'init', __NAMESPACE__ . '\register_login_button_block' ); |
| @@ -31,8 +33,66 @@ | ||
| 31 | 33 | } |
| 32 | 34 | add_action( 'init', __NAMESPACE__ . '\register_login_button_block' ); |
| 33 | 35 | |
| 34 | 36 | /** |
| 37 | + * Returns current URL. | |
| 38 | + * | |
| 39 | + * @return string | |
| 40 | + */ | |
| 41 | +function get_current_url() { | |
| 42 | + if ( ! isset( $_SERVER['HTTP_HOST'] ) || ! isset( $_SERVER['REQUEST_URI'] ) ) { | |
| 43 | + return ''; | |
| 44 | + } | |
| 45 | + | |
| 46 | + return ( is_ssl() ? 'https://' : 'http://' ) . wp_unslash( $_SERVER['HTTP_HOST'] ) . wp_unslash( $_SERVER['REQUEST_URI'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 47 | +} | |
| 48 | + | |
| 49 | +/** | |
| 50 | + * Returns subscriber log in URL. | |
| 51 | + * | |
| 52 | + * @param string $redirect Path to redirect to on login. | |
| 53 | + * | |
| 54 | + * @return string | |
| 55 | + */ | |
| 56 | +function get_subscriber_login_url( $redirect ) { | |
| 57 | + $redirect = ! empty( $redirect ) ? $redirect : get_site_url(); | |
| 58 | + | |
| 59 | + if ( ( new Host() )->is_wpcom_simple() ) { | |
| 60 | + // On WPCOM we will redirect immediately | |
| 61 | + return wpcom_logmein_redirect_url( $redirect, false, null, 'link', get_current_blog_id() ); | |
| 62 | + } | |
| 63 | + | |
| 64 | + // On self-hosted we will save and hide the token. | |
| 65 | + // rawurlencode the redirect before nesting it: it is already percent-encoded | |
| 66 | + // (e.g. an emoji or non-ASCII slug comes through as %F0%9F%8C%91), and add_query_arg | |
| 67 | + // does not encode the values it inserts. Without this extra layer the value is | |
| 68 | + // over-decoded to raw bytes by the time it reaches the subscribers/auth endpoint, | |
| 69 | + // which strips it and 404s. See NL-273. | |
| 70 | + $redirect_url = get_site_url() . '/wp-json/jetpack/v4/subscribers/auth'; | |
| 71 | + $redirect_url = add_query_arg( 'redirect_url', rawurlencode( $redirect ), $redirect_url ); | |
| 72 | + | |
| 73 | + return add_query_arg( | |
| 74 | + array( | |
| 75 | + 'site_id' => intval( Jetpack_Options::get_option( 'id' ) ), | |
| 76 | + 'redirect_url' => rawurlencode( $redirect_url ), | |
| 77 | + ), | |
| 78 | + 'https://subscribe.wordpress.com/memberships/jwt/' | |
| 79 | + ); | |
| 80 | +} | |
| 81 | + | |
| 82 | +/** | |
| 83 | + * Determines whether the visitor has a subscriber session for the login UI. | |
| 84 | + * | |
| 85 | + * WordPress sessions count on Simple; other hosts require the subscriber cookie. | |
| 86 | + * Content access validates the token separately. | |
| 87 | + * | |
| 88 | + * @return bool | |
| 89 | + */ | |
| 90 | +function is_subscriber_logged_in() { | |
| 91 | + return ( ( new Host() )->is_wpcom_simple() && is_user_logged_in() ) || Abstract_Token_Subscription_Service::has_token_from_cookie(); | |
| 92 | +} | |
| 93 | + | |
| 94 | +/** | |
| 35 | 95 | * Render callback. |
| 36 | 96 | * |
| 37 | 97 | * @param array $attributes Array containing the block attributes. |
| 38 | 98 | * @param string $content String containing the block content. |
| @@ -43,20 +103,32 @@ | ||
| 43 | 103 | if ( ! pre_render_checks() ) { |
| 44 | 104 | return ''; |
| 45 | 105 | } |
| 46 | 106 | |
| 47 | - $has_auth_cookie = isset( $_COOKIE[ Abstract_Token_Subscription_Service::JWT_AUTH_TOKEN_COOKIE_NAME ] ); | |
| 48 | - // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 49 | - $has_token_parameter = isset( $_GET['token'] ); | |
| 50 | - | |
| 51 | - $is_user_logged_in_on_wpcom = ( new Host() )->is_wpcom_simple() && is_user_logged_in(); | |
| 52 | - if ( $is_user_logged_in_on_wpcom || $has_auth_cookie || $has_token_parameter ) { | |
| 53 | - // The viewer is logged it, so they shouldn't see the login button. | |
| 107 | + // The viewer is logged it, so they shouldn't see the login button. | |
| 108 | + if ( is_subscriber_logged_in() ) { | |
| 54 | 109 | return ''; |
| 55 | 110 | } |
| 56 | 111 | |
| 57 | 112 | Jetpack_Gutenberg::load_styles_as_required( LOGIN_BUTTON_NAME ); |
| 58 | 113 | |
| 59 | - $url = subscription_service()->access_url(); | |
| 114 | + $redirect_url = get_current_url(); | |
| 115 | + $url = get_subscriber_login_url( $redirect_url ); | |
| 60 | 116 | |
| 61 | - return preg_replace( '/(<a\b[^><]*)>/i', '$1 href="' . esc_url( $url ) . '">', $content ); | |
| 117 | + $content = preg_replace( '/(<a\b[^><]*)>/i', '$1 href="' . esc_url( $url ) . '">', $content ); | |
| 118 | + | |
| 119 | + // Defense in depth: the label is inner block content (KSES-filtered on save for | |
| 120 | + // roles without `unfiltered_html`), but escape it again on output so a stored | |
| 121 | + // payload can never render as live markup. | |
| 122 | + return wp_kses_post( $content ); | |
| 123 | +} | |
| 124 | + | |
| 125 | +/** | |
| 126 | + * Render email callback. | |
| 127 | + * | |
| 128 | + * @return string | |
| 129 | + */ | |
| 130 | +function render_login_button_block_email() { | |
| 131 | + // We don't want to render the login button in emails. | |
| 132 | + // The subscriber is already considered logged in in emails. | |
| 133 | + return ''; | |
| 62 | 134 | } |