Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.83% covered (success)
95.83%
46 / 48
66.67% covered (warning)
66.67%
4 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Config
95.83% covered (success)
95.83%
46 / 48
66.67% covered (warning)
66.67%
4 / 6
13
0.00% covered (danger)
0.00%
0 / 1
 constants
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
1
 get_development_features
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
3.04
 get_custom_post_types
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 get_hosting_provider
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 is_website_public
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 is_my_jetpack_available
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
3.33
1<?php
2
3namespace Automattic\Jetpack_Boost\Admin;
4
5use Automattic\Jetpack\Boost\App\Contracts\Is_Dev_Feature;
6use Automattic\Jetpack\My_Jetpack\Initializer as My_Jetpack_Initializer;
7use Automattic\Jetpack\Status;
8use Automattic\Jetpack\Status\Host;
9use Automattic\Jetpack_Boost\Lib\Cache_Compatibility;
10use Automattic\Jetpack_Boost\Modules\Features_Index;
11
12/**
13 * Handle the configuration constants.
14 *
15 * This is a global state of Jetpack Boost and passed on to the front-end.
16 */
17class Config {
18    public function constants() {
19        /**
20         * Filters the internal path to the distributed assets used by the plugin
21         *
22         * @param string $path the path to the assets
23         *
24         * @since   1.0.0
25         */
26        $internal_path = apply_filters( 'jetpack_boost_asset_internal_path', 'app/assets/dist/' );
27
28        $constants = array(
29            'version'             => JETPACK_BOOST_VERSION,
30            'pluginDirUrl'        => untrailingslashit( JETPACK_BOOST_PLUGINS_DIR_URL ),
31            'assetPath'           => plugins_url( $internal_path, JETPACK_BOOST_PATH ),
32            'canResizeImages'     => wp_image_editor_supports( array( 'methods' => array( 'resize' ) ) ),
33            'site'                => array(
34                'url'       => get_home_url(),
35                'domain'    => ( new Status() )->get_site_suffix(),
36                'online'    => self::is_website_public(),
37                'myJetpack' => self::is_my_jetpack_available(),
38                'host'      => $this->get_hosting_provider(),
39                'hasCache'  => Cache_Compatibility::has_cache(),
40            ),
41            'api'                 => array(
42                'namespace' => JETPACK_BOOST_REST_NAMESPACE,
43                'prefix'    => JETPACK_BOOST_REST_PREFIX,
44            ),
45            'postTypes'           => (object) $this->get_custom_post_types(),
46            'developmentFeatures' => self::get_development_features(),
47        );
48
49        /**
50         * Filters the constants so each module can define extra ones
51         *
52         * @param array $constant The array of constants used by the plugin
53         *
54         * @since   1.0.0
55         */
56        return apply_filters( 'jetpack_boost_js_constants', $constants );
57    }
58
59    /**
60     * Get a list of features that are marked as development features.
61     *
62     * @return array<string, bool> Slugs of features and their status.
63     */
64    private static function get_development_features() {
65        $features = Features_Index::get_all_features();
66
67        $development_features = array();
68        foreach ( $features as $feature ) {
69            if ( is_subclass_of( $feature, Is_Dev_Feature::class ) ) {
70                $development_features[] = ( new $feature() )->get_slug();
71            }
72        }
73
74        return $development_features;
75    }
76
77    /**
78     * Retrieves custom post types.
79     *
80     * @return array Associative array of custom post types
81     * with their labels as keys and names as values.
82     */
83    private static function get_custom_post_types() {
84        $post_types = get_post_types(
85            array(
86                'public'   => true,
87                '_builtin' => false,
88            ),
89            false
90        );
91        unset( $post_types['attachment'] );
92
93        $post_types = array_filter( $post_types, 'is_post_type_viewable' );
94
95        return wp_list_pluck( $post_types, 'label', 'name' );
96    }
97
98    /**
99     * Retrieves the hosting provider.
100     * We're only interested in 'atomic' or 'woa' for now.
101     *
102     * A new value here also changes CSS and JS delivery. See
103     * jetpack_boost_minify_use_static_cache_urls(), which reads anything but 'other' as a host
104     * that may answer wp-content 404s itself.
105     *
106     * @since 3.10.0
107     *
108     * @return string The hosting provider.
109     */
110    public static function get_hosting_provider() {
111        $host = new Host();
112
113        if ( $host->is_woa_site() ) {
114            return 'woa';
115        }
116
117        if ( $host->is_atomic_platform() ) {
118            return 'atomic';
119        }
120
121        return 'other';
122    }
123
124    /**
125     * Checks if the website is publicly accessible.
126     *
127     * @return bool True if the website is public, false otherwise.
128     */
129    public static function is_website_public() {
130        return ! ( new Status() )->is_offline_mode() && ! ( new Status() )->is_private_site();
131    }
132
133    /**
134     * Checks if My Jetpack pages, where the upgrade flow lives, are available on this site.
135     *
136     * A bundled copy of the package predating `is_admin_page_available()` can only report
137     * whether My Jetpack initialized, not whether this user can reach its page.
138     *
139     * @since 4.8.0
140     *
141     * @return bool True if My Jetpack is reachable by the current user, false otherwise.
142     */
143    public static function is_my_jetpack_available() {
144        if ( method_exists( My_Jetpack_Initializer::class, 'is_admin_page_available' ) ) {
145            return My_Jetpack_Initializer::is_admin_page_available();
146        }
147        return class_exists( My_Jetpack_Initializer::class ) && did_action( 'my_jetpack_init' ) > 0;
148    }
149}