Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.92% covered (success)
95.92%
47 / 49
66.67% covered (warning)
66.67%
4 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Config
95.92% covered (success)
95.92%
47 / 49
66.67% covered (warning)
66.67%
4 / 6
14
0.00% covered (danger)
0.00%
0 / 1
 constants
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
1 / 1
2
 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                'addLicense' => self::is_my_jetpack_available() && (bool) apply_filters( 'jetpack_my_jetpack_should_enable_add_license_screen', true ),
39                'host'       => $this->get_hosting_provider(),
40                'hasCache'   => Cache_Compatibility::has_cache(),
41            ),
42            'api'                 => array(
43                'namespace' => JETPACK_BOOST_REST_NAMESPACE,
44                'prefix'    => JETPACK_BOOST_REST_PREFIX,
45            ),
46            'postTypes'           => (object) $this->get_custom_post_types(),
47            'developmentFeatures' => self::get_development_features(),
48        );
49
50        /**
51         * Filters the constants so each module can define extra ones
52         *
53         * @param array $constant The array of constants used by the plugin
54         *
55         * @since   1.0.0
56         */
57        return apply_filters( 'jetpack_boost_js_constants', $constants );
58    }
59
60    /**
61     * Get a list of features that are marked as development features.
62     *
63     * @return array<string, bool> Slugs of features and their status.
64     */
65    private static function get_development_features() {
66        $features = Features_Index::get_all_features();
67
68        $development_features = array();
69        foreach ( $features as $feature ) {
70            if ( is_subclass_of( $feature, Is_Dev_Feature::class ) ) {
71                $development_features[] = ( new $feature() )->get_slug();
72            }
73        }
74
75        return $development_features;
76    }
77
78    /**
79     * Retrieves custom post types.
80     *
81     * @return array Associative array of custom post types
82     * with their labels as keys and names as values.
83     */
84    private static function get_custom_post_types() {
85        $post_types = get_post_types(
86            array(
87                'public'   => true,
88                '_builtin' => false,
89            ),
90            false
91        );
92        unset( $post_types['attachment'] );
93
94        $post_types = array_filter( $post_types, 'is_post_type_viewable' );
95
96        return wp_list_pluck( $post_types, 'label', 'name' );
97    }
98
99    /**
100     * Retrieves the hosting provider.
101     * We're only interested in 'atomic' or 'woa' for now.
102     *
103     * A new value here also changes CSS and JS delivery. See
104     * jetpack_boost_minify_use_static_cache_urls(), which reads anything but 'other' as a host
105     * that may answer wp-content 404s itself.
106     *
107     * @since 3.10.0
108     *
109     * @return string The hosting provider.
110     */
111    public static function get_hosting_provider() {
112        $host = new Host();
113
114        if ( $host->is_woa_site() ) {
115            return 'woa';
116        }
117
118        if ( $host->is_atomic_platform() ) {
119            return 'atomic';
120        }
121
122        return 'other';
123    }
124
125    /**
126     * Checks if the website is publicly accessible.
127     *
128     * @return bool True if the website is public, false otherwise.
129     */
130    public static function is_website_public() {
131        return ! ( new Status() )->is_offline_mode() && ! ( new Status() )->is_private_site();
132    }
133
134    /**
135     * Checks if My Jetpack pages, where the upgrade flow lives, are available on this site.
136     *
137     * A bundled copy of the package predating `is_admin_page_available()` can only report
138     * whether My Jetpack initialized, not whether this user can reach its page.
139     *
140     * @since 4.8.0
141     *
142     * @return bool True if My Jetpack is reachable by the current user, false otherwise.
143     */
144    public static function is_my_jetpack_available() {
145        if ( method_exists( My_Jetpack_Initializer::class, 'is_admin_page_available' ) ) {
146            return My_Jetpack_Initializer::is_admin_page_available();
147        }
148        return class_exists( My_Jetpack_Initializer::class ) && did_action( 'my_jetpack_init' ) > 0;
149    }
150}