Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
8.14% covered (danger)
8.14%
7 / 86
23.53% covered (danger)
23.53%
4 / 17
CRAP
0.00% covered (danger)
0.00%
0 / 1
Cloud_CSS
8.14% covered (danger)
8.14%
7 / 86
23.53% covered (danger)
23.53%
4 / 17
1040.59
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 setup
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 activate
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 is_ready
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_change_output_action_names
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 is_available
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_slug
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_always_available_endpoints
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 display_critical_css
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
90
 generate_cloud_css
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
6
 handle_save_post
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
42
 regenerate_cloud_css
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 is_post_in_latest_providers_list
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 handle_critical_css_invalidated
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 get_all_providers
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 get_existing_sources
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 update_total_problem_count
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace Automattic\Jetpack_Boost\Modules\Optimizations\Cloud_CSS;
4
5use Automattic\Jetpack\Boost_Core\Lib\Boost_API;
6use Automattic\Jetpack_Boost\Contracts\Changes_Output_After_Activation;
7use Automattic\Jetpack_Boost\Contracts\Feature;
8use Automattic\Jetpack_Boost\Contracts\Has_Activate;
9use Automattic\Jetpack_Boost\Contracts\Needs_To_Be_Ready;
10use Automattic\Jetpack_Boost\Contracts\Needs_Website_To_Be_Public;
11use Automattic\Jetpack_Boost\Contracts\Optimization;
12use Automattic\Jetpack_Boost\Lib\Cornerstone\Cornerstone_Utils;
13use Automattic\Jetpack_Boost\Lib\Critical_CSS\Admin_Bar_Compatibility;
14use Automattic\Jetpack_Boost\Lib\Critical_CSS\Critical_CSS_Invalidator;
15use Automattic\Jetpack_Boost\Lib\Critical_CSS\Critical_CSS_State;
16use Automattic\Jetpack_Boost\Lib\Critical_CSS\Critical_CSS_Storage;
17use Automattic\Jetpack_Boost\Lib\Critical_CSS\Display_Critical_CSS;
18use Automattic\Jetpack_Boost\Lib\Critical_CSS\Generator;
19use Automattic\Jetpack_Boost\Lib\Critical_CSS\Regenerate;
20use Automattic\Jetpack_Boost\Lib\Critical_CSS\Source_Providers\Source_Providers;
21use Automattic\Jetpack_Boost\Lib\Premium_Features;
22use Automattic\Jetpack_Boost\REST_API\Contracts\Has_Always_Available_Endpoints;
23use Automattic\Jetpack_Boost\REST_API\Endpoints\Update_Cloud_CSS;
24
25class Cloud_CSS implements Feature, Has_Activate, Has_Always_Available_Endpoints, Changes_Output_After_Activation, Optimization, Needs_To_Be_Ready, Needs_Website_To_Be_Public {
26
27    /** User has requested regeneration manually or through activating the module. */
28    const REGENERATE_REASON_USER_REQUEST = 'user_request';
29
30    /** A post was updated/created. */
31    const REGENERATE_REASON_SAVE_POST = 'save_post';
32
33    /** A cornerstone page or the list of cornerstone pages was updated. */
34    const REGENERATE_REASON_CORNERSTONE_UPDATE = 'cornerstone_update';
35
36    /** Existing critical CSS invalidated because of a significant change, e.g. Theme changed. */
37    const REGENERATE_REASON_INVALIDATED = 'invalidated';
38
39    /** Requesting a regeneration because the previous request had failed and this is a followup attempt to regenerate Critical CSS. */
40    const REGENERATE_REASON_FOLLOWUP = 'followup';
41
42    /**
43     * Critical CSS storage class instance.
44     *
45     * @var Critical_CSS_Storage
46     */
47    protected $storage;
48
49    /**
50     * Critical CSS Provider Paths.
51     *
52     * @var Source_Providers
53     */
54    protected $paths;
55
56    public function __construct() {
57        $this->storage = new Critical_CSS_Storage();
58        $this->paths   = new Source_Providers();
59    }
60
61    public function setup() {
62        add_action( 'wp', array( $this, 'display_critical_css' ) );
63        add_action( 'save_post', array( $this, 'handle_save_post' ), 10, 2 );
64        add_action( 'jetpack_boost_critical_css_invalidated', array( $this, 'handle_critical_css_invalidated' ) );
65        add_filter( 'jetpack_boost_total_problem_count', array( $this, 'update_total_problem_count' ) );
66
67        Generator::init();
68        Critical_CSS_Invalidator::init();
69        Cloud_CSS_Followup::init();
70
71        return true;
72    }
73
74    public static function activate() {
75        ( new Regenerate() )->start();
76    }
77
78    /**
79     * Check if the module is ready and already serving critical CSS.
80     *
81     * @return bool
82     */
83    public function is_ready() {
84        return ( new Critical_CSS_State() )->is_generated();
85    }
86
87    /**
88     * Get the action names that will be triggered when the module is ready and serving critical CSS.
89     *
90     * @return string[]
91     */
92    public static function get_change_output_action_names() {
93        return array( 'jetpack_boost_critical_css_invalidated', 'jetpack_boost_critical_css_generated' );
94    }
95
96    public static function is_available() {
97        return true === Premium_Features::has_feature( Premium_Features::CLOUD_CSS );
98    }
99
100    public static function get_slug() {
101        return 'cloud_css';
102    }
103
104    public function get_always_available_endpoints() {
105        return array(
106            new Update_Cloud_CSS(),
107        );
108    }
109
110    public function display_critical_css() {
111
112        // Don't look for Critical CSS in the dashboard.
113        if ( is_admin() ) {
114            return;
115        }
116
117        // Don't show Critical CSS in customizer previews.
118        if ( is_customize_preview() ) {
119            return;
120        }
121
122        // Don't display Critical CSS, if current page load is by the Critical CSS generator.
123        if ( Generator::is_generating_critical_css() ) {
124            return;
125        }
126
127        // Get the Critical CSS to show.
128        $critical_css = $this->paths->get_current_request_css();
129        if ( ! $critical_css ) {
130            $keys    = $this->paths->get_current_request_css_keys();
131            $pending = ( new Critical_CSS_State() )->has_pending_provider( $keys );
132
133            // If Cloud CSS is still generating and the user is logged in, render the status information in a comment.
134            if ( $pending && is_user_logged_in() ) {
135                $display = new Display_Critical_CSS( '/* ' . __( 'Jetpack Boost is currently generating critical css for this page', 'jetpack-boost' ) . ' */' );
136                add_action( 'wp_head', array( $display, 'display_critical_css' ), 0 );
137            }
138            return;
139        }
140
141        if ( defined( 'WP_DEBUG' ) && WP_DEBUG === true ) {
142            $critical_css = "/* Critical CSS Key: {$this->paths->get_current_critical_css_key()} */\n" . $critical_css;
143        }
144
145        $display = new Display_Critical_CSS( $critical_css );
146        add_action( 'wp_head', array( $display, 'display_critical_css' ), 0 );
147        add_filter( 'style_loader_tag', array( $display, 'asynchronize_stylesheets' ), 10, 4 );
148        add_action( 'wp_footer', array( $display, 'onload_flip_stylesheets' ) );
149
150        // Ensure admin bar compatibility.
151        Admin_Bar_Compatibility::init();
152    }
153
154    /**
155     * Create a Cloud CSS requests for provider groups.
156     *
157     * Initialize the Cloud CSS request. Provide $post parameter to limit generating to provider groups only associated
158     * with a specific post.
159     */
160    public function generate_cloud_css( $reason, $providers = array() ) {
161        $grouped_urls   = array();
162        $grouped_ratios = array();
163
164        foreach ( $providers as $source ) {
165            $provider                    = $source['key'];
166            $grouped_urls[ $provider ]   = $source['urls'];
167            $grouped_ratios[ $provider ] = $source['success_ratio'];
168        }
169
170        // Send the request to the Cloud.
171        $payload              = array(
172            'providers'     => $grouped_urls,
173            'successRatios' => $grouped_ratios,
174        );
175        $payload['requestId'] = md5(
176            wp_json_encode(
177                $payload,
178                0 // phpcs:ignore Jetpack.Functions.JsonEncodeFlags.ZeroFound -- No `json_encode()` flags because this needs to match whatever is calculating the hash on the other end.
179            ) . time()
180        );
181        $payload['reason']    = $reason;
182        return Boost_API::post( 'cloud-css', $payload );
183    }
184
185    /**
186     * Handle regeneration of Cloud CSS when a post is saved.
187     */
188    public function handle_save_post( $post_id, $post ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
189        if ( ! $post || ! isset( $post->post_type ) || ! is_post_publicly_viewable( $post ) ) {
190            return;
191        }
192
193        if ( Cornerstone_Utils::is_cornerstone_page( $post_id ) ) {
194            $this->regenerate_cloud_css( self::REGENERATE_REASON_CORNERSTONE_UPDATE, $this->get_all_providers() );
195            return;
196        }
197
198        // This checks against the latest providers list, not the list
199        // stored in the database because newly added posts are always
200        // included in the providers list that will be used to generate
201        // the Cloud CSS.
202        if ( $this->is_post_in_latest_providers_list( $post ) ) {
203            $this->regenerate_cloud_css( self::REGENERATE_REASON_SAVE_POST, $this->get_all_providers( array( $post ) ) );
204        }
205    }
206
207    public function regenerate_cloud_css( $reason, $providers ) {
208        $result = $this->generate_cloud_css( $reason, $providers );
209        if ( is_wp_error( $result ) ) {
210            $state = new Critical_CSS_State();
211            $state->set_error( $result->get_error_message() )->save();
212        }
213        return $result;
214    }
215
216    /**
217     * Check if the post is in the latest providers list.
218     *
219     * @param int|\WP_Post $post The post to check.
220     *
221     * @return bool
222     */
223    public function is_post_in_latest_providers_list( $post ) {
224        $post_link = get_permalink( $post );
225        $providers = $this->get_all_providers();
226
227        foreach ( $providers as $provider ) {
228            if ( in_array( $post_link, $provider['urls'], true ) ) {
229                return true;
230            }
231        }
232
233        return false;
234    }
235
236    /**
237     * Called when stored Critical CSS has been invalidated. Triggers a new Cloud CSS request.
238     */
239    public function handle_critical_css_invalidated() {
240        $this->regenerate_cloud_css( self::REGENERATE_REASON_INVALIDATED, $this->get_all_providers() );
241        Cloud_CSS_Followup::schedule();
242    }
243
244    public function get_all_providers( $context_posts = array() ) {
245        $source_providers = new Source_Providers();
246        return $source_providers->get_provider_sources( $context_posts );
247    }
248
249    public function get_existing_sources() {
250        $state = new Critical_CSS_State();
251        $data  = $state->get();
252        if ( ! empty( $data['providers'] ) ) {
253            $providers = $data['providers'];
254        } else {
255            $providers = $this->get_all_providers();
256        }
257
258        return $providers;
259    }
260
261    /**
262     * Updates the total problem count for Boost if something's
263     * wrong with Cloud CSS.
264     *
265     * @param int $count The current problem count.
266     *
267     * @return int
268     */
269    public function update_total_problem_count( $count ) {
270        return ( new Critical_CSS_State() )->has_errors() ? ++$count : $count;
271    }
272}