Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 22 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Critical_CSS_Invalidator | |
0.00% |
0 / 22 |
|
0.00% |
0 / 5 |
90 | |
0.00% |
0 / 1 |
| init | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| reset_data | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
| handle_environment_change | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| update_boost_problem_count | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| is_cloud_css | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Critical CSS Invalidator |
| 4 | * |
| 5 | * Reset critical CSS when existing critical css values are stale. |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack_Boost\Lib\Critical_CSS; |
| 9 | |
| 10 | use Automattic\Jetpack_Boost\Admin\Regenerate_Admin_Notice; |
| 11 | use Automattic\Jetpack_Boost\Lib\Boost_Health; |
| 12 | use Automattic\Jetpack_Boost\Lib\Critical_CSS\Source_Providers\Source_Providers; |
| 13 | use Automattic\Jetpack_Boost\Modules\Modules_Setup; |
| 14 | use Automattic\Jetpack_Boost\Modules\Optimizations\Cloud_CSS\Cloud_CSS; |
| 15 | use Automattic\Jetpack_Boost\Modules\Optimizations\Cloud_CSS\Cloud_CSS_Followup; |
| 16 | |
| 17 | /** |
| 18 | * Handler for invalidating Critical CSS; both Cloud and Local. Watches relevant |
| 19 | * hooks and clears data when necessary. Also sends out its own action |
| 20 | * (after_critical_css_invalidate) so that the Cloud or Local implementations of |
| 21 | * Critical CSS can respond to the invalidation. |
| 22 | */ |
| 23 | class Critical_CSS_Invalidator { |
| 24 | public static function init() { |
| 25 | add_action( 'jetpack_boost_deactivate', array( __CLASS__, 'reset_data' ) ); |
| 26 | add_action( 'jetpack_boost_environment_changed', array( __CLASS__, 'handle_environment_change' ) ); |
| 27 | add_filter( 'jetpack_boost_total_problem_count', array( __CLASS__, 'update_boost_problem_count' ) ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Reset Critical CSS data. |
| 32 | * For Cloud CSS, we need to make sure there are providers in the state, |
| 33 | * otherwise Cloud CSS cannot be stored after it is generated by the cloud. |
| 34 | */ |
| 35 | public static function reset_data() { |
| 36 | $storage = new Critical_CSS_Storage(); |
| 37 | $storage->clear(); |
| 38 | |
| 39 | $state = new Critical_CSS_State(); |
| 40 | $state->clear(); |
| 41 | |
| 42 | if ( self::is_cloud_css() ) { |
| 43 | $state->prepare_for_generation( ( new Source_Providers() )->get_provider_sources() ); |
| 44 | $state->save(); |
| 45 | |
| 46 | // Clear the regenerate flag so things are nice and tidy. |
| 47 | jetpack_boost_ds_delete( 'critical_css_suggest_regenerate' ); |
| 48 | // Also clear the admin notice flag, so the notice doesn't show up in case |
| 49 | // the user is reverted to free Boost. |
| 50 | Regenerate_Admin_Notice::dismiss(); |
| 51 | |
| 52 | } |
| 53 | |
| 54 | Cloud_CSS_Followup::unschedule(); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Respond to environment changes; deciding whether or not to clear Critical CSS data. |
| 59 | */ |
| 60 | public static function handle_environment_change( $is_major_change ) { |
| 61 | if ( $is_major_change ) { |
| 62 | self::reset_data(); |
| 63 | |
| 64 | /** |
| 65 | * Indicate that all existing critical CSS has been invalidated. |
| 66 | */ |
| 67 | do_action( 'jetpack_boost_critical_css_invalidated' ); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | public static function update_boost_problem_count( $count ) { |
| 72 | $css_needs_regeneration = Boost_Health::critical_css_needs_regeneration(); |
| 73 | if ( $css_needs_regeneration ) { |
| 74 | ++$count; |
| 75 | } |
| 76 | |
| 77 | return $count; |
| 78 | } |
| 79 | |
| 80 | public static function is_cloud_css() { |
| 81 | $optimizations = ( new Modules_Setup() )->get_status(); |
| 82 | return isset( $optimizations[ Cloud_CSS::get_slug() ] ) && $optimizations[ Cloud_CSS::get_slug() ]; |
| 83 | } |
| 84 | } |