Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
84.00% |
21 / 25 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Premium_Features | |
84.00% |
21 / 25 |
|
0.00% |
0 / 4 |
9.33 | |
0.00% |
0 / 1 |
| has_feature | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| get_features | |
94.74% |
18 / 19 |
|
0.00% |
0 / 1 |
5.00 | |||
| has_any | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| clear_cache | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\Jetpack_Boost\Lib; |
| 4 | |
| 5 | use Automattic\Jetpack\Boost_Core\Lib\Boost_API; |
| 6 | use Automattic\Jetpack\Boost_Core\Lib\Transient; |
| 7 | |
| 8 | class Premium_Features { |
| 9 | |
| 10 | const CLOUD_CSS = 'cloud-critical-css'; |
| 11 | const PERFORMANCE_HISTORY = 'performance-history'; |
| 12 | const IMAGE_CDN_LIAR = 'image-cdn-liar'; |
| 13 | const IMAGE_CDN_QUALITY = 'image-cdn-quality'; |
| 14 | const PRIORITY_SUPPORT = 'support'; |
| 15 | const PAGE_CACHE = 'page-cache'; |
| 16 | const CORNERSTONE_TEN_PAGES = 'cornerstone-10-pages'; |
| 17 | |
| 18 | const TRANSIENT_KEY = 'premium_features'; |
| 19 | |
| 20 | public static function has_feature( $feature ) { |
| 21 | $features = self::get_features(); |
| 22 | |
| 23 | if ( is_array( $features ) ) { |
| 24 | return in_array( $feature, $features, true ); |
| 25 | } |
| 26 | return false; |
| 27 | } |
| 28 | |
| 29 | public static function get_features() { |
| 30 | $available_features = Transient::get( self::TRANSIENT_KEY, false ); |
| 31 | $all_features = array( |
| 32 | self::CLOUD_CSS, |
| 33 | self::IMAGE_CDN_LIAR, |
| 34 | self::IMAGE_CDN_QUALITY, |
| 35 | self::PERFORMANCE_HISTORY, |
| 36 | self::PRIORITY_SUPPORT, |
| 37 | self::CORNERSTONE_TEN_PAGES, |
| 38 | ); |
| 39 | |
| 40 | if ( ! is_array( $available_features ) ) { |
| 41 | $available_features = Boost_API::get( 'features' ); |
| 42 | if ( ! is_array( $available_features ) ) { |
| 43 | $available_features = array(); |
| 44 | } |
| 45 | Transient::set( self::TRANSIENT_KEY, $available_features, 3 * DAY_IN_SECONDS ); |
| 46 | } |
| 47 | |
| 48 | $features = array(); |
| 49 | // Prepare a list of features after applying jetpack_boost_has_feature_* filter for each feature. |
| 50 | foreach ( $all_features as $feature ) { |
| 51 | /** |
| 52 | * Filter the availability of a feature |
| 53 | * |
| 54 | * @param bool $has_feature if the feature is available |
| 55 | * |
| 56 | * @since 1.0.0 |
| 57 | */ |
| 58 | if ( apply_filters( "jetpack_boost_has_feature_{$feature}", in_array( $feature, $available_features, true ) ) ) { |
| 59 | $features[] = $feature; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return $features; |
| 64 | } |
| 65 | |
| 66 | public static function has_any() { |
| 67 | return count( self::get_features() ) > 0; |
| 68 | } |
| 69 | |
| 70 | public static function clear_cache() { |
| 71 | Transient::delete( self::TRANSIENT_KEY ); |
| 72 | } |
| 73 | } |