Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Plan
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 4
110
0.00% covered (danger)
0.00%
0 / 1
 is_cache_old
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 get_product_from_cache
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_product
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
12
 has_required_plan
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * Class to handle the Protect plan
4 *
5 * @package automattic/jetpack-protect-status
6 */
7
8namespace Automattic\Jetpack\Protect_Status;
9
10use Automattic\Jetpack\Current_Plan;
11
12/**
13 * The Plan class.
14 */
15class Plan {
16    /**
17     * The meta name used to store the cache date
18     *
19     * @var string
20     */
21    const CACHE_DATE_META_NAME = 'protect-cache-date';
22
23    /**
24     * Valid pediord for the cache: One week.
25     */
26    const CACHE_VALIDITY_PERIOD = 7 * DAY_IN_SECONDS;
27
28    /**
29     * The meta name used to store the cache
30     *
31     * @var string
32     */
33    const CACHE_META_NAME = 'protect-cache';
34
35    /**
36     * Checks if the cache is old, meaning we need to fetch new data from WPCOM
37     */
38    private static function is_cache_old() {
39        if ( empty( self::get_product_from_cache() ) ) {
40            return true;
41        }
42
43        $cache_date = get_user_meta( get_current_user_id(), self::CACHE_DATE_META_NAME, true );
44        return time() - (int) $cache_date > ( self::CACHE_VALIDITY_PERIOD );
45    }
46
47    /**
48     * Gets the product list from the user cache
49     */
50    private static function get_product_from_cache() {
51        return get_user_meta( get_current_user_id(), self::CACHE_META_NAME, true );
52    }
53
54    /**
55     * Gets the product data
56     *
57     * @param string $wpcom_product The product slug.
58     * @return array
59     */
60    public static function get_product( $wpcom_product = 'jetpack_scan' ) {
61        if ( ! self::is_cache_old() ) {
62            return self::get_product_from_cache();
63        }
64
65        $request_url   = 'https://public-api.wordpress.com/rest/v1.1/products?locale=' . get_user_locale() . '&type=jetpack';
66        $wpcom_request = wp_remote_get( esc_url_raw( $request_url ) );
67        $response_code = wp_remote_retrieve_response_code( $wpcom_request );
68
69        if ( 200 === $response_code ) {
70            $products = json_decode( wp_remote_retrieve_body( $wpcom_request ) );
71
72            // Pick the desired product...
73            $product = $products->{$wpcom_product};
74
75            // ... and store it into the cache.
76            update_user_meta( get_current_user_id(), self::CACHE_DATE_META_NAME, time() );
77            update_user_meta( get_current_user_id(), self::CACHE_META_NAME, $product );
78
79            return $product;
80        }
81
82        return new \WP_Error(
83            'failed_to_fetch_data',
84            esc_html__( 'Unable to fetch the requested data.', 'jetpack-protect-status' ),
85            array(
86                'status'  => $response_code,
87                'request' => $wpcom_request,
88            )
89        );
90    }
91
92    /**
93     * Has Required Plan
94     *
95     * @param bool $force_refresh Refresh the local plan cache from wpcom.
96     * @return bool True when the site has a plan or product that supports the paid Protect tier.
97     */
98    public static function has_required_plan( $force_refresh = false ) {
99        static $has_scan = null;
100        if ( null === $has_scan || $force_refresh ) {
101            $products = array_column( Current_Plan::get_products(), 'product_slug' );
102
103            // Check for a plan or product that enables scan.
104            $plan_supports_scan = Current_Plan::supports( 'scan', $force_refresh );
105            $has_scan_product   = count( array_intersect( array( 'jetpack_scan', 'jetpack_scan_monthly' ), $products ) ) > 0;
106            $has_scan           = $plan_supports_scan || $has_scan_product;
107        }
108
109        return $has_scan;
110    }
111}