Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
17.24% covered (danger)
17.24%
5 / 29
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Plan
17.24% covered (danger)
17.24%
5 / 29
25.00% covered (danger)
25.00%
1 / 4
66.68
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
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
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     * Every Scan product's slug starts with this: monthly, bi-yearly and the legacy real-time ones.
37     *
38     * @since $$next-version$$
39     *
40     * @var string
41     */
42    const SCAN_PRODUCT_PREFIX = 'jetpack_scan';
43
44    /**
45     * Checks if the cache is old, meaning we need to fetch new data from WPCOM
46     */
47    private static function is_cache_old() {
48        if ( empty( self::get_product_from_cache() ) ) {
49            return true;
50        }
51
52        $cache_date = get_user_meta( get_current_user_id(), self::CACHE_DATE_META_NAME, true );
53        return time() - (int) $cache_date > ( self::CACHE_VALIDITY_PERIOD );
54    }
55
56    /**
57     * Gets the product list from the user cache
58     */
59    private static function get_product_from_cache() {
60        return get_user_meta( get_current_user_id(), self::CACHE_META_NAME, true );
61    }
62
63    /**
64     * Gets the product data
65     *
66     * @param string $wpcom_product The product slug.
67     * @return array
68     */
69    public static function get_product( $wpcom_product = 'jetpack_scan' ) {
70        if ( ! self::is_cache_old() ) {
71            return self::get_product_from_cache();
72        }
73
74        $request_url   = 'https://public-api.wordpress.com/rest/v1.1/products?locale=' . get_user_locale() . '&type=jetpack';
75        $wpcom_request = wp_remote_get( esc_url_raw( $request_url ) );
76        $response_code = wp_remote_retrieve_response_code( $wpcom_request );
77
78        if ( 200 === $response_code ) {
79            $products = json_decode( wp_remote_retrieve_body( $wpcom_request ) );
80
81            // Pick the desired product...
82            $product = $products->{$wpcom_product};
83
84            // ... and store it into the cache.
85            update_user_meta( get_current_user_id(), self::CACHE_DATE_META_NAME, time() );
86            update_user_meta( get_current_user_id(), self::CACHE_META_NAME, $product );
87
88            return $product;
89        }
90
91        return new \WP_Error(
92            'failed_to_fetch_data',
93            esc_html__( 'Unable to fetch the requested data.', 'jetpack-protect-status' ),
94            array(
95                'status'  => $response_code,
96                'request' => $wpcom_request,
97            )
98        );
99    }
100
101    /**
102     * Has Required Plan
103     *
104     * @param bool $force_refresh Refresh the local plan cache from wpcom.
105     * @return bool True when the site has a plan or product that supports the paid Protect tier.
106     */
107    public static function has_required_plan( $force_refresh = false ) {
108        // WordPress.com's Personal and Premium include `scan` without Scan's UI,
109        // so gate on `scan-self-serve` there, as Jetpack::show_scan_ui() does.
110        $feature = function_exists( 'wpcom_feature_exists' ) && wpcom_feature_exists( 'scan-self-serve' ) ? 'scan-self-serve' : 'scan';
111
112        // Before the products: a refresh updates them too.
113        if ( Current_Plan::supports( $feature, $force_refresh ) ) {
114            return true;
115        }
116
117        $products = array_column( Current_Plan::get_products(), 'product_slug' );
118
119        return array() !== array_filter( $products, fn( $slug ) => 0 === strpos( (string) $slug, self::SCAN_PRODUCT_PREFIX ) );
120    }
121}