ID && current_user_can( 'edit_post', get_the_ID() ); } /** * Determines if the current user can view the protected content of the given block. * * @param array $attributes Block attributes. * @param object $block Block to check. * * @return bool Whether the use can view the content. */ function current_visitor_can_access( $attributes, $block ) { /** * If the current WordPress install has as signed in user * they can see the content. */ if ( current_user_can_edit() ) { return true; } $selected_plan_ids = array(); if ( isset( $attributes['selectedPlanIds'] ) ) { $selected_plan_ids = $attributes['selectedPlanIds']; } elseif ( isset( $attributes['selectedPlanId'] ) ) { $selected_plan_ids = array( $attributes['selectedPlanId'] ); } if ( isset( $block ) && ! empty( $block->context['premium-content/planId'] ) ) { $selected_plan_ids = array( $block->context['premium-content/planId'] ); } elseif ( isset( $block ) && ! empty( $block->context['premium-content/planIds'] ) ) { $selected_plan_ids = $block->context['premium-content/planIds']; } if ( empty( $selected_plan_ids ) ) { return false; } $can_view = false; $paywall = subscription_service(); $access_level = Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS; // Only paid subscribers should be granted access to the premium content $tier_ids = \Jetpack_Memberships::get_all_newsletter_plan_ids(); $tier_ids = array_intersect( $tier_ids, $selected_plan_ids ); if ( ! empty( $tier_ids ) ) { // If the selected plan is a tier, we want to check directly if user has a higher "tier". // This is to prevent situation where the user upgrades and lose access to premium-gated content $subscriptions = array(); if ( ( new Host() )->is_wpcom_simple() && is_user_logged_in() ) { $user_id = wp_get_current_user()->ID; /** * Filter the subscriptions attached to a specific user on a given site. * * @since 9.4.0 * * @param array $subscriptions Array of subscriptions. * @param int $user_id The user's ID. * @param int $site_id ID of the current site. */ $subscriptions = apply_filters( 'earn_get_user_subscriptions_for_site_id', array(), $user_id, get_current_blog_id() ); // format the subscriptions so that they can be validated. $subscriptions = WPCOM_Online_Subscription_Service::abbreviate_subscriptions( $subscriptions ); } else { $token = $paywall->get_and_set_token_from_request(); $payload = $paywall->decode_token( $token ); $is_valid_token = ! empty( $payload ); if ( $is_valid_token ) { $subscriptions = (array) $payload['subscriptions']; } } foreach ( $tier_ids as $tier_id ) { $can_view = ! $paywall->maybe_gate_access_for_user_if_tier( $tier_id, $subscriptions ); if ( $can_view ) { break; } } } $non_tier_ids = array_diff( $selected_plan_ids, $tier_ids ); if ( ! $can_view ) { // For selected plans that are not tiers, we want to check if the user has any of the selected plans. $can_view = $paywall->visitor_can_view_content( $non_tier_ids, $access_level ); } if ( $can_view ) { /** * Fires when a visitor can view protected content on a site. * * @since 9.4.0 */ do_action( 'jetpack_earn_remove_cache_headers' ); } return $can_view; }