Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
73.33% covered (warning)
73.33%
11 / 15
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_Offline_Subscription_Service
84.62% covered (warning)
84.62%
11 / 13
0.00% covered (danger)
0.00%
0 / 2
6.13
0.00% covered (danger)
0.00%
0 / 1
 available
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 subscriber_can_receive_post_by_mail
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
5.01
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
8namespace Automattic\Jetpack\Extensions\Premium_Content\Subscription_Service;
9
10use const Automattic\Jetpack\Extensions\Subscriptions\META_NAME_FOR_POST_LEVEL_ACCESS_SETTINGS;
11
12if ( ! 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 */
23class 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}