← All changes
|
modules/memberships/class-jetpack-memberships.php
+42
-3
16.3-a.5
→
16.3
View file →
| @@ -710,8 +710,17 @@ | ||
| 710 | 710 | if ( empty( $post_access_level ) || ! is_string( $post_access_level ) ) { |
| 711 | 711 | $post_access_level = Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_EVERYBODY; |
| 712 | 712 | } |
| 713 | 713 | |
| 714 | + // Only the editor switches a Paywall post to subscribers; REST, WP-CLI and importer saves don't. | |
| 715 | + // The block's name constant isn't loaded everywhere this runs, hence the literal. | |
| 716 | + if ( | |
| 717 | + Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_EVERYBODY === $post_access_level | |
| 718 | + && has_block( 'jetpack/paywall', $post_id ) | |
| 719 | + ) { | |
| 720 | + $post_access_level = Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_SUBSCRIBERS; | |
| 721 | + } | |
| 722 | + | |
| 714 | 723 | self::$post_access_level_cache[ $cache_key ] = $post_access_level; |
| 715 | 724 | |
| 716 | 725 | return $post_access_level; |
| 717 | 726 | } |
| @@ -816,8 +825,31 @@ | ||
| 816 | 825 | * |
| 817 | 826 | * @return bool Whether the post can be viewed |
| 818 | 827 | */ |
| 819 | 828 | public static function user_can_view_post( $post_id = null ) { |
| 829 | + return self::check_post_access( $post_id, true ); | |
| 830 | + } | |
| 831 | + | |
| 832 | + /** | |
| 833 | + * Check the post's subscription requirement without granting access for editing it. | |
| 834 | + * | |
| 835 | + * @since $$next-version$$ | |
| 836 | + * | |
| 837 | + * @param int|null $post_id Explicit post ID, or the loop post when omitted. | |
| 838 | + * @return bool Whether the visitor meets the post's subscription requirement. | |
| 839 | + */ | |
| 840 | + public static function user_has_subscription_access( $post_id = null ) { | |
| 841 | + return self::check_post_access( $post_id, false ); | |
| 842 | + } | |
| 843 | + | |
| 844 | + /** | |
| 845 | + * Evaluate and cache post access with or without the editorial exception. | |
| 846 | + * | |
| 847 | + * @param int|null $post_id Post to check. | |
| 848 | + * @param bool $allow_editor_access Whether editing the post can grant access. | |
| 849 | + * @return bool Whether access is granted. | |
| 850 | + */ | |
| 851 | + private static function check_post_access( $post_id, $allow_editor_access ) { | |
| 820 | 852 | $user_id = get_current_user_id(); |
| 821 | 853 | if ( null === $post_id ) { |
| 822 | 854 | $post_id = get_the_ID(); |
| 823 | 855 | } |
| @@ -825,9 +857,9 @@ | ||
| 825 | 857 | if ( false === $post_id ) { |
| 826 | 858 | $post_id = 0; |
| 827 | 859 | } |
| 828 | 860 | |
| 829 | - $cache_key = sprintf( '%d_%d', $user_id, $post_id ); | |
| 861 | + $cache_key = sprintf( '%d_%d_%d', $user_id, $post_id, (int) $allow_editor_access ); | |
| 830 | 862 | if ( isset( self::$user_can_view_post_cache[ $cache_key ] ) ) { |
| 831 | 863 | return self::$user_can_view_post_cache[ $cache_key ]; |
| 832 | 864 | } |
| 833 | 865 | |
| @@ -837,9 +869,9 @@ | ||
| 837 | 869 | return true; |
| 838 | 870 | } |
| 839 | 871 | |
| 840 | 872 | // we are sending the post to subscribers so the user is a subscriber |
| 841 | - if ( defined( 'WPCOM_SENDING_POST_TO_SUBSCRIBERS' ) && WPCOM_SENDING_POST_TO_SUBSCRIBERS && Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_SUBSCRIBERS === $post_access_level ) { | |
| 873 | + if ( $allow_editor_access && defined( 'WPCOM_SENDING_POST_TO_SUBSCRIBERS' ) && WPCOM_SENDING_POST_TO_SUBSCRIBERS && Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_SUBSCRIBERS === $post_access_level ) { | |
| 842 | 874 | self::$user_can_view_post_cache[ $cache_key ] = true; |
| 843 | 875 | return true; |
| 844 | 876 | } |
| 845 | 877 | |
| @@ -858,9 +890,16 @@ | ||
| 858 | 890 | // We downgrade the post level to subscribers-only |
| 859 | 891 | $post_access_level = Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_SUBSCRIBERS; |
| 860 | 892 | } |
| 861 | 893 | |
| 862 | - $can_view_post = $paywall->visitor_can_view_content( $all_newsletters_plan_ids, $post_access_level ); | |
| 894 | + // Pass the post explicitly: callers outside the loop have no get_the_ID() to fall back on. | |
| 895 | + if ( $allow_editor_access ) { | |
| 896 | + // @phan-suppress-next-line PhanParamTooMany -- Concrete services accept the optional $post_id; interface omits it on purpose. | |
| 897 | + $can_view_post = $paywall->visitor_can_view_content( $all_newsletters_plan_ids, $post_access_level, $post_id ); | |
| 898 | + } else { | |
| 899 | + $can_view_post = is_callable( array( $paywall, 'visitor_has_subscription_access' ) ) | |
| 900 | + && $paywall->visitor_has_subscription_access( $all_newsletters_plan_ids, $post_access_level, $post_id ); | |
| 901 | + } | |
| 863 | 902 | |
| 864 | 903 | self::$user_can_view_post_cache[ $cache_key ] = $can_view_post; |
| 865 | 904 | return $can_view_post; |
| 866 | 905 | } |