| 1 | <?php |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 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 | |
| 13 | require_once __DIR__ . '/subscription-service/include.php'; |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | function membership_checks() { |
| 21 | |
| 22 | if ( ! class_exists( '\Jetpack_Memberships' ) ) { |
| 23 | return false; |
| 24 | } |
| 25 | |
| 26 | if ( ! \Jetpack_Memberships::has_connected_account() ) { |
| 27 | return false; |
| 28 | } |
| 29 | return true; |
| 30 | } |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | function required_plan_checks() { |
| 39 | $availability = \Jetpack_Gutenberg::get_cached_availability(); |
| 40 | $slug = 'premium-content/container'; |
| 41 | return ( isset( $availability[ $slug ] ) && $availability[ $slug ]['available'] ); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | function pre_render_checks() { |
| 52 | return ( current_user_can_edit() || membership_checks() ); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | function current_user_can_edit() { |
| 61 | $user = wp_get_current_user(); |
| 62 | |
| 63 | return 0 !== $user->ID && current_user_can( 'edit_post', get_the_ID() ); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | function maybe_renew_session_cookie( $paywall, $user_id, $abbreviated_subscriptions ) { |
| 86 | if ( empty( $abbreviated_subscriptions ) ) { |
| 87 | return null; |
| 88 | } |
| 89 | if ( Abstract_Token_Subscription_Service::has_token_from_cookie() ) { |
| 90 | $session_cookie_name = Abstract_Token_Subscription_Service::JWT_AUTH_TOKEN_COOKIE_NAME; |
| 91 | $existing_payload = null; |
| 92 | if ( isset( $_COOKIE[ $session_cookie_name ] ) ) { |
| 93 | |
| 94 | $existing_payload = $paywall->decode_token( $_COOKIE[ $session_cookie_name ] ); |
| 95 | } |
| 96 | if ( ! empty( $existing_payload ) ) { |
| 97 | return null; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | $key = $paywall->get_key(); |
| 102 | if ( ! $key ) { |
| 103 | return null; |
| 104 | } |
| 105 | |
| 106 | $payload_subscriptions = array(); |
| 107 | foreach ( $abbreviated_subscriptions as $pid => $sub ) { |
| 108 | $sub = (array) $sub; |
| 109 | $payload_subscriptions[ $pid ] = array( |
| 110 | 'status' => 'active', |
| 111 | 'end_date' => $sub['end_date'] ?? gmdate( 'Y-m-d H:i:s', time() + MONTH_IN_SECONDS ), |
| 112 | 'product_id' => (int) $pid, |
| 113 | ); |
| 114 | if ( ! empty( $sub['is_comp'] ) ) { |
| 115 | $payload_subscriptions[ $pid ]['is_comp'] = true; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | $token = JWT::encode( |
| 120 | array( |
| 121 | 'user_id' => $user_id, |
| 122 | 'blog_sub' => 'active', |
| 123 | 'subscriptions' => $payload_subscriptions, |
| 124 | ), |
| 125 | $key |
| 126 | ); |
| 127 | |
| 128 | if ( ! ( defined( 'TESTING_IN_JETPACK' ) && TESTING_IN_JETPACK ) && ! headers_sent() ) { |
| 129 | |
| 130 | setcookie( Abstract_Token_Subscription_Service::JWT_AUTH_TOKEN_COOKIE_NAME, $token, strtotime( '+1 month' ), '/', '', is_ssl(), false ); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | |
| 135 | $_COOKIE[ Abstract_Token_Subscription_Service::JWT_AUTH_TOKEN_COOKIE_NAME ] = $token; |
| 136 | |
| 137 | return $token; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | function get_subscriptions_for_logged_in_user( $paywall ) { |
| 153 | $local_user_id = wp_get_current_user()->ID; |
| 154 | $wpcom_user_id = (int) get_user_meta( $local_user_id, 'wpcom_user_id', true ); |
| 155 | $user_id = $wpcom_user_id > 0 ? $wpcom_user_id : $local_user_id; |
| 156 | $site_id = $paywall->get_site_id(); |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | $raw_subscriptions = apply_filters( 'earn_get_user_subscriptions_for_site_id', array(), $user_id, $site_id ); |
| 168 | $abbreviated_subscriptions = WPCOM_Online_Subscription_Service::abbreviate_subscriptions( $raw_subscriptions ); |
| 169 | |
| 170 | return array( $user_id, $raw_subscriptions, $abbreviated_subscriptions ); |
| 171 | } |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | function prewarm_premium_content_session_cookie() { |
| 185 | if ( ! is_user_logged_in() ) { |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | |
| 190 | if ( is_admin() || is_feed() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) { |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | |
| 195 | |
| 196 | $queried = get_queried_object(); |
| 197 | if ( ! ( $queried instanceof \WP_Post ) || ! has_block( 'premium-content/container', $queried ) ) { |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | |
| 203 | if ( current_user_can( 'edit_post', $queried->ID ) ) { |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | $paywall = subscription_service(); |
| 208 | |
| 209 | |
| 210 | $existing_payload = $paywall->decode_token( $paywall->get_and_set_token_from_request() ); |
| 211 | if ( ! empty( $existing_payload ) ) { |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | list( $user_id, , $abbreviated_subscriptions ) = get_subscriptions_for_logged_in_user( $paywall ); |
| 216 | maybe_renew_session_cookie( $paywall, $user_id, $abbreviated_subscriptions ); |
| 217 | } |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | function current_visitor_can_access( $attributes, $block ) { |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | if ( current_user_can_edit() ) { |
| 234 | return true; |
| 235 | } |
| 236 | |
| 237 | $selected_plan_ids = array(); |
| 238 | |
| 239 | if ( isset( $attributes['selectedPlanIds'] ) ) { |
| 240 | $selected_plan_ids = $attributes['selectedPlanIds']; |
| 241 | } elseif ( isset( $attributes['selectedPlanId'] ) ) { |
| 242 | $selected_plan_ids = array( $attributes['selectedPlanId'] ); |
| 243 | } |
| 244 | |
| 245 | if ( isset( $block ) && ! empty( $block->context['premium-content/planId'] ) ) { |
| 246 | $selected_plan_ids = array( $block->context['premium-content/planId'] ); |
| 247 | } elseif ( isset( $block ) && ! empty( $block->context['premium-content/planIds'] ) ) { |
| 248 | $selected_plan_ids = $block->context['premium-content/planIds']; |
| 249 | } |
| 250 | |
| 251 | if ( empty( $selected_plan_ids ) ) { |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | $can_view = visitor_can_access_plan_ids( $selected_plan_ids ); |
| 256 | |
| 257 | if ( $can_view ) { |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | do_action( 'jetpack_earn_remove_cache_headers' ); |
| 264 | } |
| 265 | |
| 266 | return $can_view; |
| 267 | } |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | function visitor_can_access_plan_ids( $selected_plan_ids, $post_id = null ) { |
| 283 | return check_subscription_plan_access( $selected_plan_ids, $post_id, false ); |
| 284 | } |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | function visitor_has_subscription_access_to_plan_ids( $selected_plan_ids, $post_id = null ) { |
| 296 | return check_subscription_plan_access( $selected_plan_ids, $post_id, true ); |
| 297 | } |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | function check_subscription_plan_access( $selected_plan_ids, $post_id, $require_entitlement ) { |
| 310 | |
| 311 | require_once JETPACK__PLUGIN_DIR . 'modules/memberships/class-jetpack-memberships.php'; |
| 312 | |
| 313 | $can_view = false; |
| 314 | $paywall = subscription_service(); |
| 315 | $access_level = Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS; |
| 316 | $tier_ids = \Jetpack_Memberships::get_all_newsletter_plan_ids(); |
| 317 | $tier_ids = array_intersect( $tier_ids, $selected_plan_ids ); |
| 318 | if ( ! empty( $tier_ids ) ) { |
| 319 | |
| 320 | |
| 321 | |
| 322 | $subscriptions = array(); |
| 323 | |
| 324 | |
| 325 | |
| 326 | $token = $paywall->get_and_set_token_from_request(); |
| 327 | $payload = $paywall->decode_token( $token ); |
| 328 | if ( ! empty( $payload ) ) { |
| 329 | $subscriptions = (array) $payload['subscriptions']; |
| 330 | } |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | if ( empty( $subscriptions ) && is_user_logged_in() ) { |
| 336 | list( , , $subscriptions ) = get_subscriptions_for_logged_in_user( $paywall ); |
| 337 | } |
| 338 | |
| 339 | foreach ( $tier_ids as $tier_id ) { |
| 340 | $can_view = ! $paywall->maybe_gate_access_for_user_if_tier( $tier_id, $subscriptions ); |
| 341 | if ( $can_view ) { |
| 342 | break; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | if ( |
| 353 | ! $can_view |
| 354 | && method_exists( $paywall, 'token_has_matching_product' ) |
| 355 | && $paywall->token_has_matching_product( $tier_ids, $subscriptions ) |
| 356 | ) { |
| 357 | $fresh_payload = $paywall->refresh_token_payload(); |
| 358 | if ( ! empty( $fresh_payload ) ) { |
| 359 | $subscriptions = isset( $fresh_payload['subscriptions'] ) ? (array) $fresh_payload['subscriptions'] : array(); |
| 360 | foreach ( $tier_ids as $tier_id ) { |
| 361 | $can_view = ! $paywall->maybe_gate_access_for_user_if_tier( $tier_id, $subscriptions ); |
| 362 | if ( $can_view ) { |
| 363 | break; |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | $non_tier_ids = array_diff( $selected_plan_ids, $tier_ids ); |
| 371 | if ( ! $can_view ) { |
| 372 | if ( $require_entitlement ) { |
| 373 | |
| 374 | $can_view = is_callable( array( $paywall, 'visitor_has_subscription_access' ) ) |
| 375 | && $paywall->visitor_has_subscription_access( $non_tier_ids, $access_level, $post_id ); |
| 376 | } else { |
| 377 | |
| 378 | |
| 379 | $can_view = $paywall->visitor_can_view_content( $non_tier_ids, $access_level, $post_id ); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | return $can_view; |
| 384 | } |