Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
97.73% |
43 / 44 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Config | |
97.73% |
43 / 44 |
|
80.00% |
4 / 5 |
10 | |
0.00% |
0 / 1 |
| constants | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
1 | |||
| get_development_features | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
3.04 | |||
| get_custom_post_types | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| get_hosting_provider | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| is_website_public | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\Jetpack_Boost\Admin; |
| 4 | |
| 5 | use Automattic\Jetpack\Boost\App\Contracts\Is_Dev_Feature; |
| 6 | use Automattic\Jetpack\Status; |
| 7 | use Automattic\Jetpack\Status\Host; |
| 8 | use Automattic\Jetpack_Boost\Lib\Cache_Compatibility; |
| 9 | use Automattic\Jetpack_Boost\Modules\Features_Index; |
| 10 | |
| 11 | /** |
| 12 | * Handle the configuration constants. |
| 13 | * |
| 14 | * This is a global state of Jetpack Boost and passed on to the front-end. |
| 15 | */ |
| 16 | class Config { |
| 17 | public function constants() { |
| 18 | /** |
| 19 | * Filters the internal path to the distributed assets used by the plugin |
| 20 | * |
| 21 | * @param string $path the path to the assets |
| 22 | * |
| 23 | * @since 1.0.0 |
| 24 | */ |
| 25 | $internal_path = apply_filters( 'jetpack_boost_asset_internal_path', 'app/assets/dist/' ); |
| 26 | |
| 27 | $constants = array( |
| 28 | 'version' => JETPACK_BOOST_VERSION, |
| 29 | 'pluginDirUrl' => untrailingslashit( JETPACK_BOOST_PLUGINS_DIR_URL ), |
| 30 | 'assetPath' => plugins_url( $internal_path, JETPACK_BOOST_PATH ), |
| 31 | 'canResizeImages' => wp_image_editor_supports( array( 'methods' => array( 'resize' ) ) ), |
| 32 | 'site' => array( |
| 33 | 'url' => get_home_url(), |
| 34 | 'domain' => ( new Status() )->get_site_suffix(), |
| 35 | 'online' => self::is_website_public(), |
| 36 | 'host' => $this->get_hosting_provider(), |
| 37 | 'hasCache' => Cache_Compatibility::has_cache(), |
| 38 | ), |
| 39 | 'api' => array( |
| 40 | 'namespace' => JETPACK_BOOST_REST_NAMESPACE, |
| 41 | 'prefix' => JETPACK_BOOST_REST_PREFIX, |
| 42 | ), |
| 43 | 'postTypes' => (object) $this->get_custom_post_types(), |
| 44 | 'developmentFeatures' => self::get_development_features(), |
| 45 | ); |
| 46 | |
| 47 | /** |
| 48 | * Filters the constants so each module can define extra ones |
| 49 | * |
| 50 | * @param array $constant The array of constants used by the plugin |
| 51 | * |
| 52 | * @since 1.0.0 |
| 53 | */ |
| 54 | return apply_filters( 'jetpack_boost_js_constants', $constants ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Get a list of features that are marked as development features. |
| 59 | * |
| 60 | * @return array<string, bool> Slugs of features and their status. |
| 61 | */ |
| 62 | private static function get_development_features() { |
| 63 | $features = Features_Index::get_all_features(); |
| 64 | |
| 65 | $development_features = array(); |
| 66 | foreach ( $features as $feature ) { |
| 67 | if ( is_subclass_of( $feature, Is_Dev_Feature::class ) ) { |
| 68 | $development_features[] = ( new $feature() )->get_slug(); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return $development_features; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Retrieves custom post types. |
| 77 | * |
| 78 | * @return array Associative array of custom post types |
| 79 | * with their labels as keys and names as values. |
| 80 | */ |
| 81 | private static function get_custom_post_types() { |
| 82 | $post_types = get_post_types( |
| 83 | array( |
| 84 | 'public' => true, |
| 85 | '_builtin' => false, |
| 86 | ), |
| 87 | false |
| 88 | ); |
| 89 | unset( $post_types['attachment'] ); |
| 90 | |
| 91 | $post_types = array_filter( $post_types, 'is_post_type_viewable' ); |
| 92 | |
| 93 | return wp_list_pluck( $post_types, 'label', 'name' ); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Retrieves the hosting provider. |
| 98 | * We're only interested in 'atomic' or 'woa' for now. |
| 99 | * |
| 100 | * @since 3.10.0 |
| 101 | * |
| 102 | * @return string The hosting provider. |
| 103 | */ |
| 104 | public static function get_hosting_provider() { |
| 105 | $host = new Host(); |
| 106 | |
| 107 | if ( $host->is_woa_site() ) { |
| 108 | return 'woa'; |
| 109 | } |
| 110 | |
| 111 | if ( $host->is_atomic_platform() ) { |
| 112 | return 'atomic'; |
| 113 | } |
| 114 | |
| 115 | return 'other'; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Checks if the website is publicly accessible. |
| 120 | * |
| 121 | * @return bool True if the website is public, false otherwise. |
| 122 | */ |
| 123 | public static function is_website_public() { |
| 124 | return ! ( new Status() )->is_offline_mode() && ! ( new Status() )->is_private_site(); |
| 125 | } |
| 126 | } |