← All changes
|
extensions/blocks/subscriber-login/subscriber-login.php
+168
-0
16.2-beta
→
16.3
View file →
| @@ -1,0 +1,168 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Subscriber Login Block. | |
| 4 | + * | |
| 5 | + * @since 13.1 | |
| 6 | + * | |
| 7 | + * @package automattic/jetpack | |
| 8 | + */ | |
| 9 | + | |
| 10 | +namespace Automattic\Jetpack\Extensions\Subscriber_Login; | |
| 11 | + | |
| 12 | +use Automattic\Jetpack\Blocks; | |
| 13 | +use Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Abstract_Token_Subscription_Service; | |
| 14 | +use Automattic\Jetpack\Status\Host; | |
| 15 | +use Automattic\Jetpack\Status\Request; | |
| 16 | +use Jetpack; | |
| 17 | +use Jetpack_Gutenberg; | |
| 18 | +use Jetpack_Memberships; | |
| 19 | +use Jetpack_Options; | |
| 20 | + | |
| 21 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 22 | + exit( 0 ); | |
| 23 | +} | |
| 24 | + | |
| 25 | +require_once __DIR__ . '/class-jetpack-subscription-site.php'; | |
| 26 | + | |
| 27 | +/** | |
| 28 | + * Registers the block for use in Gutenberg | |
| 29 | + * This is done via an action so that we can disable | |
| 30 | + * registration if we need to. | |
| 31 | + */ | |
| 32 | +function register_block() { | |
| 33 | + if ( | |
| 34 | + ! Jetpack::is_module_active( 'subscriptions' ) || | |
| 35 | + ! class_exists( 'Jetpack_Memberships' ) || | |
| 36 | + ! class_exists( 'Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Abstract_Token_Subscription_Service' ) | |
| 37 | + ) { | |
| 38 | + return; | |
| 39 | + } | |
| 40 | + | |
| 41 | + Blocks::jetpack_register_block( | |
| 42 | + __DIR__, | |
| 43 | + array( 'render_callback' => __NAMESPACE__ . '\render_block' ) | |
| 44 | + ); | |
| 45 | + | |
| 46 | + add_filter( | |
| 47 | + 'jetpack_options_whitelist', | |
| 48 | + function ( $options ) { | |
| 49 | + $options[] = 'jetpack_subscriptions_login_navigation_enabled'; | |
| 50 | + | |
| 51 | + return $options; | |
| 52 | + } | |
| 53 | + ); | |
| 54 | + | |
| 55 | + // If called via REST API, we need to register later in the lifecycle | |
| 56 | + if ( ( new Host() )->is_wpcom_platform() && ! Request::is_frontend() ) { | |
| 57 | + add_action( | |
| 58 | + 'restapi_theme_init', | |
| 59 | + function () { | |
| 60 | + Jetpack_Subscription_Site::init()->handle_subscriber_login_block_placements(); | |
| 61 | + } | |
| 62 | + ); | |
| 63 | + } else { | |
| 64 | + Jetpack_Subscription_Site::init()->handle_subscriber_login_block_placements(); | |
| 65 | + } | |
| 66 | +} | |
| 67 | +add_action( 'init', __NAMESPACE__ . '\register_block' ); | |
| 68 | + | |
| 69 | +/** | |
| 70 | + * Returns current URL. | |
| 71 | + * | |
| 72 | + * @return string | |
| 73 | + */ | |
| 74 | +function get_current_url() { | |
| 75 | + if ( ! isset( $_SERVER['HTTP_HOST'] ) || ! isset( $_SERVER['REQUEST_URI'] ) ) { | |
| 76 | + return ''; | |
| 77 | + } | |
| 78 | + | |
| 79 | + return ( is_ssl() ? 'https://' : 'http://' ) . wp_unslash( $_SERVER['HTTP_HOST'] ) . wp_unslash( $_SERVER['REQUEST_URI'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 80 | +} | |
| 81 | + | |
| 82 | +/** | |
| 83 | + * Returns subscriber log in URL. | |
| 84 | + * | |
| 85 | + * @param string $redirect Path to redirect to on login. | |
| 86 | + * | |
| 87 | + * @return string | |
| 88 | + */ | |
| 89 | +function get_subscriber_login_url( $redirect ) { | |
| 90 | + $redirect = ! empty( $redirect ) ? $redirect : get_site_url(); | |
| 91 | + | |
| 92 | + if ( ( new Host() )->is_wpcom_simple() ) { | |
| 93 | + // On WPCOM we will redirect immediately | |
| 94 | + return wpcom_logmein_redirect_url( $redirect, false, null, 'link', get_current_blog_id() ); | |
| 95 | + } | |
| 96 | + | |
| 97 | + // On self-hosted we will save and hide the token. | |
| 98 | + // rawurlencode the redirect before nesting it: it is already percent-encoded | |
| 99 | + // (e.g. an emoji or non-ASCII slug comes through as %F0%9F%8C%91), and add_query_arg | |
| 100 | + // does not encode the values it inserts. Without this extra layer the value is | |
| 101 | + // over-decoded to raw bytes by the time it reaches the subscribers/auth endpoint, | |
| 102 | + // which strips it and 404s. See NL-273. | |
| 103 | + $redirect_url = get_site_url() . '/wp-json/jetpack/v4/subscribers/auth'; | |
| 104 | + $redirect_url = add_query_arg( 'redirect_url', rawurlencode( $redirect ), $redirect_url ); | |
| 105 | + | |
| 106 | + return add_query_arg( | |
| 107 | + array( | |
| 108 | + 'site_id' => intval( Jetpack_Options::get_option( 'id' ) ), | |
| 109 | + 'redirect_url' => rawurlencode( $redirect_url ), | |
| 110 | + ), | |
| 111 | + 'https://subscribe.wordpress.com/memberships/jwt/' | |
| 112 | + ); | |
| 113 | +} | |
| 114 | + | |
| 115 | +/** | |
| 116 | + * Determines whether the visitor has a subscriber session for the login UI. | |
| 117 | + * | |
| 118 | + * WordPress sessions count on Simple; other hosts require the subscriber cookie. | |
| 119 | + * Content access validates the token separately. | |
| 120 | + * | |
| 121 | + * @return bool | |
| 122 | + */ | |
| 123 | +function is_subscriber_logged_in() { | |
| 124 | + return ( ( new Host() )->is_wpcom_simple() && is_user_logged_in() ) || Abstract_Token_Subscription_Service::has_token_from_cookie(); | |
| 125 | +} | |
| 126 | + | |
| 127 | +/** | |
| 128 | + * Renders Subscriber Login block. | |
| 129 | + * | |
| 130 | + * @param array $attributes The block attributes. | |
| 131 | + * | |
| 132 | + * @return string | |
| 133 | + */ | |
| 134 | +function render_block( $attributes ) { | |
| 135 | + Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); | |
| 136 | + | |
| 137 | + $block_template = '<div %1$s><a href="%2$s">%3$s</a></div>'; | |
| 138 | + $redirect_url = ! empty( $attributes['redirectToCurrent'] ) ? get_current_url() : get_site_url(); | |
| 139 | + $log_in_label = ! empty( $attributes['logInLabel'] ) ? sanitize_text_field( $attributes['logInLabel'] ) : esc_html__( 'Log in', 'jetpack' ); | |
| 140 | + $log_out_label = ! empty( $attributes['logOutLabel'] ) ? sanitize_text_field( $attributes['logOutLabel'] ) : esc_html__( 'Log out', 'jetpack' ); | |
| 141 | + $show_manage_link = ! empty( $attributes['showManageSubscriptionsLink'] ); | |
| 142 | + $manage_subscriptions_label = ! empty( $attributes['manageSubscriptionsLabel'] ) ? sanitize_text_field( $attributes['manageSubscriptionsLabel'] ) : esc_html__( 'Manage subscription', 'jetpack' ); | |
| 143 | + | |
| 144 | + if ( ! is_subscriber_logged_in() ) { | |
| 145 | + return sprintf( | |
| 146 | + $block_template, | |
| 147 | + get_block_wrapper_attributes(), | |
| 148 | + get_subscriber_login_url( $redirect_url ), | |
| 149 | + $log_in_label | |
| 150 | + ); | |
| 151 | + } | |
| 152 | + | |
| 153 | + if ( $show_manage_link && Jetpack_Memberships::is_current_user_subscribed() ) { | |
| 154 | + return sprintf( | |
| 155 | + $block_template, | |
| 156 | + get_block_wrapper_attributes(), | |
| 157 | + 'https://wordpress.com/reader/site/subscription/' . Jetpack_Memberships::get_blog_id(), | |
| 158 | + $manage_subscriptions_label | |
| 159 | + ); | |
| 160 | + } | |
| 161 | + | |
| 162 | + return sprintf( | |
| 163 | + $block_template, | |
| 164 | + get_block_wrapper_attributes(), | |
| 165 | + wp_logout_url( $redirect_url ), | |
| 166 | + $log_out_label | |
| 167 | + ); | |
| 168 | +} | |