jetpack
/
extensions
/
blocks
/
premium-content
/
_inc
/
subscription-service
/
class-wpcom-offline-subscription-service.php
class-abstract-token-subscription-service.php
1 month ago
class-jetpack-token-subscription-service.php
1 month ago
class-jwt.php
2 months ago
class-unconfigured-subscription-service.php
8 months ago
class-wpcom-offline-subscription-service.php
8 months ago
class-wpcom-online-subscription-service.php
3 months ago
include.php
8 months ago
interface-subscription-service.php
8 months ago
class-wpcom-offline-subscription-service.php
71 lines
| 1 | <?php |
| 2 | /** |
| 3 | * This subscription service is used when a subscriber is offline and a token is not available. |
| 4 | * |
| 5 | * @package Automattic\Jetpack\Extensions\Premium_Content |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service; |
| 9 | |
| 10 | use const Automattic\Jetpack\Extensions\Subscriptions\META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS; |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit( 0 ); |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Class WPCOM_Offline_Subscription_Service |
| 18 | * This subscription service is used when a subscriber is offline and a token is not available. |
| 19 | * This subscription service will be used when sending emails to subscribers |
| 20 | * |
| 21 | * @package Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service |
| 22 | */ |
| 23 | class WPCOM_Offline_Subscription_Service extends WPCOM_Online_Subscription_Service { |
| 24 | |
| 25 | /** |
| 26 | * Is available() |
| 27 | * |
| 28 | * @return bool |
| 29 | */ |
| 30 | public static function available() { |
| 31 | // Return available if the user is logged in and we are on WPCOM. |
| 32 | return false; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Check if the subscriber can receive the newsletter. |
| 37 | * This is the only method where is user does not need to be logged in. |
| 38 | * |
| 39 | * @param int $user_id User id. |
| 40 | * @param int $post_id Post id. |
| 41 | * |
| 42 | * @return bool |
| 43 | * @throws \Exception Throws an exception when used outside of WPCOM. |
| 44 | */ |
| 45 | public function subscriber_can_receive_post_by_mail( $user_id, $post_id ) { |
| 46 | |
| 47 | if ( 0 === $user_id || empty( $user_id ) ) { |
| 48 | // Email cannot be sent to non-users |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | $previous_user = wp_get_current_user(); |
| 53 | wp_set_current_user( $user_id ); |
| 54 | |
| 55 | $access_level = get_post_meta( $post_id, META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS, true ); |
| 56 | |
| 57 | if ( ! $access_level || self::POST_ACCESS_LEVEL_EVERYBODY === $access_level ) { |
| 58 | // The post is not gated, we return early |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | $valid_plan_ids = \Jetpack_Memberships::get_all_newsletter_plan_ids(); |
| 63 | $is_blog_subscriber = true; // it is a subscriber as this is used in async when lopping through subscribers... |
| 64 | $allowed = $this->user_can_view_content( $valid_plan_ids, $access_level, $is_blog_subscriber, $post_id ); |
| 65 | |
| 66 | wp_set_current_user( $previous_user->ID ); |
| 67 | |
| 68 | return $allowed; |
| 69 | } |
| 70 | } |
| 71 |