Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Boost_Health
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 7
240
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
 get_total_issues
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_all_issues
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 is_critical_css_enabled
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 critical_css_needs_regeneration
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 critical_css_has_errors
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 cache_engine_not_loading
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace Automattic\Jetpack_Boost\Lib;
4
5use Automattic\Jetpack_Boost\Lib\Critical_CSS\Critical_CSS_State;
6use Automattic\Jetpack_Boost\Modules\Optimizations\Cloud_CSS\Cloud_CSS;
7use Automattic\Jetpack_Boost\Modules\Optimizations\Critical_CSS\Critical_CSS;
8use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Page_Cache;
9use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Pre_WordPress\Boost_Cache;
10
11/**
12 * Class Boost_Health
13 *
14 * Represents the health of the Jetpack Boost plugin.
15 */
16class Boost_Health {
17
18    /**
19     * @var array List of issues affecting the health of the plugin.
20     */
21    private $issues = array();
22
23    /**
24     * Boost_Health constructor.
25     *
26     * Initializes the Boost_Health object and checks for any health issues.
27     */
28    public function __construct() {
29        if ( self::critical_css_needs_regeneration() ) {
30            $this->issues[] = __( 'Outdated Critical CSS', 'jetpack-boost' );
31        }
32
33        if ( self::critical_css_has_errors() ) {
34            $this->issues[] = __( 'Failed to generate Critical CSS', 'jetpack-boost' );
35        }
36
37        if ( self::cache_engine_not_loading() ) {
38            $this->issues[] = __( 'Cache engine is not loading', 'jetpack-boost' );
39        }
40    }
41
42    /**
43     * Get the total number of issues affecting the health of the plugin.
44     *
45     * @return int Total number of issues.
46     */
47    public function get_total_issues() {
48        return count( $this->issues );
49    }
50
51    /**
52     * Get all the issues affecting the health of the plugin.
53     *
54     * @return array List of issues.
55     */
56    public function get_all_issues() {
57        return $this->issues;
58    }
59
60    private static function is_critical_css_enabled() {
61        return ( new Status( Critical_CSS::get_slug() ) )->get();
62    }
63
64    /**
65     * Check if Critical CSS needs regeneration.
66     *
67     * @return bool True if regeneration is needed, false otherwise.
68     */
69    public static function critical_css_needs_regeneration() {
70        if ( Cloud_CSS::is_available() || ! self::is_critical_css_enabled() ) {
71            return false;
72        }
73
74        $suggest_regenerate = jetpack_boost_ds_get( 'critical_css_suggest_regenerate' );
75
76        return in_array( $suggest_regenerate, Environment_Change_Detector::get_available_env_change_statuses(), true );
77    }
78
79    /**
80     * Check if Critical CSS generation has errors.
81     *
82     * @return bool True if errors are present, false otherwise.
83     */
84    public static function critical_css_has_errors() {
85        if ( ! self::is_critical_css_enabled() ) {
86            return false;
87        }
88        return ( new Critical_CSS_State() )->has_errors();
89    }
90
91    /**
92     * Check if the cache engine is not loading.
93     *
94     * @return bool True if the cache engine is not loading, false otherwise.
95     */
96    public static function cache_engine_not_loading() {
97        if ( ! ( new Status( Page_Cache::get_slug() ) )->get() ) {
98            return false;
99        }
100
101        if ( Boost_Cache::is_loaded() ) {
102            return false;
103        }
104
105        return true;
106    }
107}