subscriber-login.php
157 lines
| 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 | require_once __DIR__ . '/class-jetpack-subscription-site.php'; |
| 22 | |
| 23 | /** |
| 24 | * Registers the block for use in Gutenberg |
| 25 | * This is done via an action so that we can disable |
| 26 | * registration if we need to. |
| 27 | */ |
| 28 | function register_block() { |
| 29 | if ( |
| 30 | ! Jetpack::is_module_active( 'subscriptions' ) || |
| 31 | ! class_exists( 'Jetpack_Memberships' ) || |
| 32 | ! class_exists( 'Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Abstract_Token_Subscription_Service' ) |
| 33 | ) { |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | Blocks::jetpack_register_block( |
| 38 | __DIR__, |
| 39 | array( 'render_callback' => __NAMESPACE__ . '\render_block' ) |
| 40 | ); |
| 41 | |
| 42 | add_filter( |
| 43 | 'jetpack_options_whitelist', |
| 44 | function ( $options ) { |
| 45 | $options[] = 'jetpack_subscriptions_login_navigation_enabled'; |
| 46 | |
| 47 | return $options; |
| 48 | } |
| 49 | ); |
| 50 | |
| 51 | // If called via REST API, we need to register later in the lifecycle |
| 52 | if ( ( new Host() )->is_wpcom_platform() && ! Request::is_frontend() ) { |
| 53 | add_action( |
| 54 | 'restapi_theme_init', |
| 55 | function () { |
| 56 | Jetpack_Subscription_Site::init()->handle_subscriber_login_block_placements(); |
| 57 | } |
| 58 | ); |
| 59 | } else { |
| 60 | Jetpack_Subscription_Site::init()->handle_subscriber_login_block_placements(); |
| 61 | } |
| 62 | } |
| 63 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 64 | |
| 65 | /** |
| 66 | * Returns current URL. |
| 67 | * |
| 68 | * @return string |
| 69 | */ |
| 70 | function get_current_url() { |
| 71 | if ( ! isset( $_SERVER['HTTP_HOST'] ) || ! isset( $_SERVER['REQUEST_URI'] ) ) { |
| 72 | return ''; |
| 73 | } |
| 74 | |
| 75 | return ( is_ssl() ? 'https://' : 'http://' ) . wp_unslash( $_SERVER['HTTP_HOST'] ) . wp_unslash( $_SERVER['REQUEST_URI'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Returns subscriber log in URL. |
| 80 | * |
| 81 | * @param string $redirect Path to redirect to on login. |
| 82 | * |
| 83 | * @return string |
| 84 | */ |
| 85 | function get_subscriber_login_url( $redirect ) { |
| 86 | $redirect = ! empty( $redirect ) ? $redirect : get_site_url(); |
| 87 | |
| 88 | if ( ( new Host() )->is_wpcom_simple() ) { |
| 89 | // On WPCOM we will redirect immediately |
| 90 | return wpcom_logmein_redirect_url( $redirect, false, null, 'link', get_current_blog_id() ); |
| 91 | } |
| 92 | |
| 93 | // On self-hosted we will save and hide the token |
| 94 | $redirect_url = get_site_url() . '/wp-json/jetpack/v4/subscribers/auth'; |
| 95 | $redirect_url = add_query_arg( 'redirect_url', $redirect, $redirect_url ); |
| 96 | |
| 97 | return add_query_arg( |
| 98 | array( |
| 99 | 'site_id' => intval( Jetpack_Options::get_option( 'id' ) ), |
| 100 | 'redirect_url' => rawurlencode( $redirect_url ), |
| 101 | ), |
| 102 | 'https://subscribe.wordpress.com/memberships/jwt/' |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Determines whether the current visitor is a logged in user or a subscriber. |
| 108 | * |
| 109 | * @return bool |
| 110 | */ |
| 111 | function is_subscriber_logged_in() { |
| 112 | return is_user_logged_in() || Abstract_Token_Subscription_Service::has_token_from_cookie(); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Renders Subscriber Login block. |
| 117 | * |
| 118 | * @param array $attributes The block attributes. |
| 119 | * |
| 120 | * @return string |
| 121 | */ |
| 122 | function render_block( $attributes ) { |
| 123 | Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); |
| 124 | |
| 125 | $block_template = '<div %1$s><a href="%2$s">%3$s</a></div>'; |
| 126 | $redirect_url = ! empty( $attributes['redirectToCurrent'] ) ? get_current_url() : get_site_url(); |
| 127 | $log_in_label = ! empty( $attributes['logInLabel'] ) ? sanitize_text_field( $attributes['logInLabel'] ) : esc_html__( 'Log in', 'jetpack' ); |
| 128 | $log_out_label = ! empty( $attributes['logOutLabel'] ) ? sanitize_text_field( $attributes['logOutLabel'] ) : esc_html__( 'Log out', 'jetpack' ); |
| 129 | $show_manage_link = ! empty( $attributes['showManageSubscriptionsLink'] ); |
| 130 | $manage_subscriptions_label = ! empty( $attributes['manageSubscriptionsLabel'] ) ? sanitize_text_field( $attributes['manageSubscriptionsLabel'] ) : esc_html__( 'Manage subscription', 'jetpack' ); |
| 131 | |
| 132 | if ( ! is_subscriber_logged_in() ) { |
| 133 | return sprintf( |
| 134 | $block_template, |
| 135 | get_block_wrapper_attributes(), |
| 136 | get_subscriber_login_url( $redirect_url ), |
| 137 | $log_in_label |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | if ( $show_manage_link && Jetpack_Memberships::is_current_user_subscribed() ) { |
| 142 | return sprintf( |
| 143 | $block_template, |
| 144 | get_block_wrapper_attributes(), |
| 145 | 'https://wordpress.com/reader/site/subscription/' . Jetpack_Memberships::get_blog_id(), |
| 146 | $manage_subscriptions_label |
| 147 | ); |
| 148 | } |
| 149 | |
| 150 | return sprintf( |
| 151 | $block_template, |
| 152 | get_block_wrapper_attributes(), |
| 153 | wp_logout_url( $redirect_url ), |
| 154 | $log_out_label |
| 155 | ); |
| 156 | } |
| 157 |