PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-beta
Jetpack – WP Security, Backup, Speed, & Growth v16.3-beta
16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 All 507 releases
jetpack / jetpack_vendor / automattic / jetpack-protect-status / src / class-plan.php

class-plan.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3-beta, at jetpack_vendor/automattic/jetpack-protect-status/src/class-plan.php

112 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class to handle the Protect plan
4 *
5 * @package automattic/jetpack-protect-status
6 */
7
8 namespace Automattic\Jetpack\Protect_Status;
9
10 use Automattic\Jetpack\Current_Plan;
11
12 /**
13 * The Plan class.
14 */
15 class 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 }
112