Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.34% covered (success)
94.34%
50 / 53
62.50% covered (warning)
62.50%
5 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Premium_Features
94.34% covered (success)
94.34%
50 / 53
62.50% covered (warning)
62.50%
5 / 8
31.17
0.00% covered (danger)
0.00%
0 / 1
 record_cloud_css_baseline
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
3.03
 remember_cloud_css_baseline
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 enable_cloud_css_after_upgrade
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
7
 has_feature
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 get_features
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
5
 is_cache_confirmed
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
9
 has_any
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 clear_cache
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Automattic\Jetpack_Boost\Lib;
4
5use Automattic\Jetpack\Boost_Core\Lib\Boost_API;
6use Automattic\Jetpack\Boost_Core\Lib\Transient;
7use Automattic\Jetpack\My_Jetpack\Product;
8use Automattic\Jetpack_Boost\Data_Sync\Modules_State_Entry;
9use Automattic\Jetpack_Boost\Modules\Module;
10use Automattic\Jetpack_Boost\Modules\Optimizations\Cloud_CSS\Cloud_CSS;
11
12class Premium_Features {
13
14    const CLOUD_CSS             = 'cloud-critical-css';
15    const PERFORMANCE_HISTORY   = 'performance-history';
16    const IMAGE_CDN_LIAR        = 'image-cdn-liar';
17    const IMAGE_CDN_QUALITY     = 'image-cdn-quality';
18    const PRIORITY_SUPPORT      = 'support';
19    const PAGE_CACHE            = 'page-cache';
20    const CORNERSTONE_TEN_PAGES = 'cornerstone-10-pages';
21
22    private const ALL_FEATURES = array(
23        self::CLOUD_CSS,
24        self::IMAGE_CDN_LIAR,
25        self::IMAGE_CDN_QUALITY,
26        self::PERFORMANCE_HISTORY,
27        self::PRIORITY_SUPPORT,
28        self::CORNERSTONE_TEN_PAGES,
29    );
30
31    const TRANSIENT_KEY              = 'premium_features';
32    const CLOUD_CSS_BASELINE_OPTION  = 'jetpack_boost_cloud_css_plan_baseline';
33    const CLOUD_CSS_ACTIVATED_OPTION = 'jetpack_boost_cloud_css_plan_activated';
34
35    /**
36     * Record a verified plan baseline when the site connects, without activating Cloud CSS.
37     *
38     * @since 4.8.2
39     */
40    public static function record_cloud_css_baseline() {
41        if ( false !== get_option( self::CLOUD_CSS_BASELINE_OPTION ) ) {
42            return;
43        }
44        self::clear_cache();
45        $verified = null;
46        $features = self::get_features( $verified );
47        if ( $verified ) {
48            self::remember_cloud_css_baseline( $features );
49        }
50    }
51
52    private static function remember_cloud_css_baseline( $features ) {
53        // A stored Cloud CSS choice survives a plan that lapsed before the first observation.
54        $has_cloud_css_choice = null !== get_option( Status::get_option_name( Cloud_CSS::get_slug() ), null );
55        $baseline             = $has_cloud_css_choice || in_array( self::CLOUD_CSS, $features, true ) ? 'premium' : 'free';
56        add_option( self::CLOUD_CSS_BASELINE_OPTION, $baseline, '', false );
57    }
58
59    /**
60     * Enable Cloud CSS once when an observed free site gains the feature.
61     *
62     * @since 4.8.2
63     *
64     * @param bool $cache_confirmed Whether is_cache_confirmed() vouched for the cached features, so they count as fetched.
65     */
66    public static function enable_cloud_css_after_upgrade( $cache_confirmed = false ) {
67        $verified = null;
68        $features = self::get_features( $verified );
69        if ( ! $verified && ! $cache_confirmed ) {
70            return;
71        }
72        $has_cloud_css = in_array( self::CLOUD_CSS, $features, true );
73
74        self::remember_cloud_css_baseline( $features );
75        if ( ! $has_cloud_css || 'free' !== get_option( self::CLOUD_CSS_BASELINE_OPTION ) ) {
76            return;
77        }
78        if ( ! ( new Module( new Cloud_CSS() ) )->is_available() ) {
79            return;
80        }
81
82        // Claim the upgrade before activation can re-enter feature checks or another request can run it.
83        if ( ! add_option( self::CLOUD_CSS_ACTIVATED_OPTION, true, '', false ) ) {
84            return;
85        }
86
87        $entry = new Modules_State_Entry( array( Cloud_CSS::class ) );
88        $entry->set( array( Cloud_CSS::get_slug() => array( 'active' => true ) ) );
89    }
90
91    public static function has_feature( $feature ) {
92        $features = self::get_features();
93
94        if ( is_array( $features ) ) {
95            return in_array( $feature, $features, true );
96        }
97        return false;
98    }
99
100    /**
101     * Get features and report whether a valid response was fetched in this call.
102     *
103     * @param bool|null $verified Receives whether a valid unfiltered response was fetched in this call.
104     * @return string[]
105     */
106    public static function get_features( &$verified = null ) {
107        $available_features = Transient::get( self::TRANSIENT_KEY, false );
108        $verified           = false;
109
110        if ( ! is_array( $available_features ) ) {
111            $available_features = Boost_API::get( 'features' );
112            $verified           = is_array( $available_features );
113            if ( ! is_array( $available_features ) ) {
114                $available_features = array();
115            }
116            Transient::set( self::TRANSIENT_KEY, $available_features, 3 * DAY_IN_SECONDS );
117        }
118
119        $features = array();
120        // Prepare a list of features after applying jetpack_boost_has_feature_* filter for each feature.
121        foreach ( self::ALL_FEATURES as $feature ) {
122            /**
123             * Filter the availability of a feature
124             *
125             * @param bool $has_feature if the feature is available
126             *
127             * @since   1.0.0
128             */
129            if ( apply_filters( "jetpack_boost_has_feature_{$feature}", in_array( $feature, $available_features, true ) ) ) {
130                $features[] = $feature;
131            }
132        }
133
134        return $features;
135    }
136
137    /**
138     * Whether the cached features match the site features My Jetpack fetches for the same page.
139     *
140     * A mismatch, or no answer from My Jetpack, means the cache needs a fresh fetch. My Jetpack keeps
141     * its answer for 15 seconds, so a plan change can take that long to show.
142     *
143     * @since $$next-version$$
144     *
145     * @return bool
146     */
147    public static function is_cache_confirmed() {
148        $cached = Transient::get( self::TRANSIENT_KEY, false );
149        if ( ! is_array( $cached ) || ! is_callable( array( Product::class, 'get_site_features_from_wpcom' ) ) || ! ( new Connection() )->is_connected() ) {
150            return false;
151        }
152
153        $site_features = Product::get_site_features_from_wpcom();
154        if ( ! is_array( $site_features ) || ! isset( $site_features['active'] ) || ! is_array( $site_features['active'] ) ) {
155            return false;
156        }
157
158        foreach ( array_unique( array_merge( self::ALL_FEATURES, $cached ) ) as $feature ) {
159            if ( in_array( $feature, $cached, true ) !== in_array( $feature, $site_features['active'], true ) ) {
160                return false;
161            }
162        }
163
164        return true;
165    }
166
167    public static function has_any() {
168        return count( self::get_features() ) > 0;
169    }
170
171    public static function clear_cache() {
172        Transient::delete( self::TRANSIENT_KEY );
173    }
174}