access-check.php
112 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\Token_Subscription_Service; |
| 11 | |
| 12 | require_once __DIR__ . '/subscription-service/include.php'; |
| 13 | |
| 14 | /** |
| 15 | * Determines if the memberships module is set up. |
| 16 | * |
| 17 | * @return bool Whether the memberships module is set up. |
| 18 | */ |
| 19 | function membership_checks() { |
| 20 | // If Jetpack is not yet configured, don't show anything ... |
| 21 | if ( ! class_exists( '\Jetpack_Memberships' ) ) { |
| 22 | return false; |
| 23 | } |
| 24 | // if stripe not connected don't show anything... |
| 25 | if ( empty( \Jetpack_Memberships::get_connected_account_id() ) ) { |
| 26 | return false; |
| 27 | } |
| 28 | return true; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Determines if the site has a plan that supports the |
| 33 | * Premium Content block. |
| 34 | * |
| 35 | * @return bool |
| 36 | */ |
| 37 | function required_plan_checks() { |
| 38 | $availability = \Jetpack_Gutenberg::get_cached_availability(); |
| 39 | $slug = 'premium-content/container'; |
| 40 | return ( isset( $availability[ $slug ] ) && $availability[ $slug ]['available'] ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Determines if the block should be rendered. Returns true |
| 45 | * if the block passes all required checks, or if the user is |
| 46 | * an editor. |
| 47 | * |
| 48 | * @return bool Whether the block should be rendered. |
| 49 | */ |
| 50 | function pre_render_checks() { |
| 51 | return ( current_user_can_edit() || membership_checks() ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Determines whether the current user can edit. |
| 56 | * |
| 57 | * @return bool Whether the user can edit. |
| 58 | */ |
| 59 | function current_user_can_edit() { |
| 60 | $user = wp_get_current_user(); |
| 61 | |
| 62 | return 0 !== $user->ID && current_user_can( 'edit_post', get_the_ID() ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Determines if the current user can view the protected content of the given block. |
| 67 | * |
| 68 | * @param array $attributes Block attributes. |
| 69 | * @param object $block Block to check. |
| 70 | * |
| 71 | * @return bool Whether the use can view the content. |
| 72 | */ |
| 73 | function current_visitor_can_access( $attributes, $block ) { |
| 74 | /** |
| 75 | * If the current WordPress install has as signed in user |
| 76 | * they can see the content. |
| 77 | */ |
| 78 | |
| 79 | if ( current_user_can_edit() ) { |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | $selected_plan_id = null; |
| 84 | |
| 85 | if ( isset( $attributes['selectedPlanId'] ) ) { |
| 86 | $selected_plan_id = (int) $attributes['selectedPlanId']; |
| 87 | } |
| 88 | |
| 89 | if ( isset( $block ) && isset( $block->context['premium-content/planId'] ) ) { |
| 90 | $selected_plan_id = (int) $block->context['premium-content/planId']; |
| 91 | } |
| 92 | |
| 93 | if ( empty( $selected_plan_id ) ) { |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | $paywall = subscription_service(); |
| 98 | $access_level = Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS; // Only paid subscribers should be granted access to the premium content |
| 99 | $can_view = $paywall->visitor_can_view_content( array( $selected_plan_id ), $access_level ); |
| 100 | |
| 101 | if ( $can_view ) { |
| 102 | /** |
| 103 | * Fires when a visitor can view protected content on a site. |
| 104 | * |
| 105 | * @since 9.4.0 |
| 106 | */ |
| 107 | do_action( 'jetpack_earn_remove_cache_headers' ); |
| 108 | } |
| 109 | |
| 110 | return $can_view; |
| 111 | } |
| 112 |