access-check.php
162 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Determine access to premium content. |
| 4 | * |
| 5 | * @package Automattic\Jetpack\Extensions\Premium_Content |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Extensions\Premium_Content; |
| 9 | |
| 10 | use Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\Abstract_Token_Subscription_Service; |
| 11 | use Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service\WPCOM_Online_Subscription_Service; |
| 12 | use Automattic\Jetpack\Status\Host; |
| 13 | |
| 14 | require_once __DIR__ . '/subscription-service/include.php'; |
| 15 | |
| 16 | /** |
| 17 | * Determines if the memberships module is set up. |
| 18 | * |
| 19 | * @return bool Whether the memberships module is set up. |
| 20 | */ |
| 21 | function membership_checks() { |
| 22 | // If Jetpack is not yet configured, don't show anything ... |
| 23 | if ( ! class_exists( '\Jetpack_Memberships' ) ) { |
| 24 | return false; |
| 25 | } |
| 26 | // if stripe not connected don't show anything... |
| 27 | if ( ! \Jetpack_Memberships::has_connected_account() ) { |
| 28 | return false; |
| 29 | } |
| 30 | return true; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Determines if the site has a plan that supports the |
| 35 | * Premium Content block. |
| 36 | * |
| 37 | * @return bool |
| 38 | */ |
| 39 | function required_plan_checks() { |
| 40 | $availability = \Jetpack_Gutenberg::get_cached_availability(); |
| 41 | $slug = 'premium-content/container'; |
| 42 | return ( isset( $availability[ $slug ] ) && $availability[ $slug ]['available'] ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Determines if the block should be rendered. Returns true |
| 47 | * if the block passes all required checks, or if the user is |
| 48 | * an editor. |
| 49 | * |
| 50 | * @return bool Whether the block should be rendered. |
| 51 | */ |
| 52 | function pre_render_checks() { |
| 53 | return ( current_user_can_edit() || membership_checks() ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Determines whether the current user can edit. |
| 58 | * |
| 59 | * @return bool Whether the user can edit. |
| 60 | */ |
| 61 | function current_user_can_edit() { |
| 62 | $user = wp_get_current_user(); |
| 63 | |
| 64 | return 0 !== $user->ID && current_user_can( 'edit_post', get_the_ID() ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Determines if the current user can view the protected content of the given block. |
| 69 | * |
| 70 | * @param array $attributes Block attributes. |
| 71 | * @param object $block Block to check. |
| 72 | * |
| 73 | * @return bool Whether the use can view the content. |
| 74 | */ |
| 75 | function current_visitor_can_access( $attributes, $block ) { |
| 76 | /** |
| 77 | * If the current WordPress install has as signed in user |
| 78 | * they can see the content. |
| 79 | */ |
| 80 | |
| 81 | if ( current_user_can_edit() ) { |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | $selected_plan_ids = array(); |
| 86 | |
| 87 | if ( isset( $attributes['selectedPlanIds'] ) ) { |
| 88 | $selected_plan_ids = $attributes['selectedPlanIds']; |
| 89 | } elseif ( isset( $attributes['selectedPlanId'] ) ) { |
| 90 | $selected_plan_ids = array( $attributes['selectedPlanId'] ); |
| 91 | } |
| 92 | |
| 93 | if ( isset( $block ) && ! empty( $block->context['premium-content/planId'] ) ) { |
| 94 | $selected_plan_ids = array( $block->context['premium-content/planId'] ); |
| 95 | } elseif ( isset( $block ) && ! empty( $block->context['premium-content/planIds'] ) ) { |
| 96 | $selected_plan_ids = $block->context['premium-content/planIds']; |
| 97 | } |
| 98 | |
| 99 | if ( empty( $selected_plan_ids ) ) { |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | $can_view = false; |
| 104 | $paywall = subscription_service(); |
| 105 | $access_level = Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS; // Only paid subscribers should be granted access to the premium content |
| 106 | $tier_ids = \Jetpack_Memberships::get_all_newsletter_plan_ids(); |
| 107 | $tier_ids = array_intersect( $tier_ids, $selected_plan_ids ); |
| 108 | if ( ! empty( $tier_ids ) ) { |
| 109 | // If the selected plan is a tier, we want to check directly if user has a higher "tier". |
| 110 | // This is to prevent situation where the user upgrades and lose access to premium-gated content |
| 111 | |
| 112 | $subscriptions = array(); |
| 113 | if ( ( new Host() )->is_wpcom_simple() && is_user_logged_in() ) { |
| 114 | $user_id = wp_get_current_user()->ID; |
| 115 | /** |
| 116 | * Filter the subscriptions attached to a specific user on a given site. |
| 117 | * |
| 118 | * @since 9.4.0 |
| 119 | * |
| 120 | * @param array $subscriptions Array of subscriptions. |
| 121 | * @param int $user_id The user's ID. |
| 122 | * @param int $site_id ID of the current site. |
| 123 | */ |
| 124 | $subscriptions = apply_filters( 'earn_get_user_subscriptions_for_site_id', array(), $user_id, get_current_blog_id() ); |
| 125 | // format the subscriptions so that they can be validated. |
| 126 | $subscriptions = WPCOM_Online_Subscription_Service::abbreviate_subscriptions( $subscriptions ); |
| 127 | } else { |
| 128 | $token = $paywall->get_and_set_token_from_request(); |
| 129 | $payload = $paywall->decode_token( $token ); |
| 130 | $is_valid_token = ! empty( $payload ); |
| 131 | |
| 132 | if ( $is_valid_token ) { |
| 133 | $subscriptions = (array) $payload['subscriptions']; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | foreach ( $tier_ids as $tier_id ) { |
| 138 | $can_view = ! $paywall->maybe_gate_access_for_user_if_tier( $tier_id, $subscriptions ); |
| 139 | if ( $can_view ) { |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | $non_tier_ids = array_diff( $selected_plan_ids, $tier_ids ); |
| 146 | if ( ! $can_view ) { |
| 147 | // For selected plans that are not tiers, we want to check if the user has any of the selected plans. |
| 148 | $can_view = $paywall->visitor_can_view_content( $non_tier_ids, $access_level ); |
| 149 | } |
| 150 | |
| 151 | if ( $can_view ) { |
| 152 | /** |
| 153 | * Fires when a visitor can view protected content on a site. |
| 154 | * |
| 155 | * @since 9.4.0 |
| 156 | */ |
| 157 | do_action( 'jetpack_earn_remove_cache_headers' ); |
| 158 | } |
| 159 | |
| 160 | return $can_view; |
| 161 | } |
| 162 |