Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 39 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
SiteHealth | |
0.00% |
0 / 39 |
|
0.00% |
0 / 3 |
420 | |
0.00% |
0 / 1 |
get_raw_data | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
get_safe_data | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
272 | |||
calculate_score | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace NewfoldLabs\WP\Module\Data\Helpers; |
4 | |
5 | /** |
6 | * Helper class for gathering and formatting Site Health data |
7 | * |
8 | * @since 1.7.0 |
9 | */ |
10 | class SiteHealth { |
11 | |
12 | /** |
13 | * Raw Site health data. |
14 | * |
15 | * @since 1.7.0 |
16 | * |
17 | * @var array |
18 | */ |
19 | private static $raw_debug_data; |
20 | |
21 | /** |
22 | * Safe Site Health data. |
23 | * |
24 | * All empty and private fields have been removed from self:$raw_debug_data. |
25 | * |
26 | * @since 1.7.0 |
27 | * |
28 | * @var array |
29 | */ |
30 | private static $safe_debug_data; |
31 | |
32 | /** |
33 | * Retrieves a site's debug data through Site health. |
34 | * |
35 | * @since 1.7.0 |
36 | * |
37 | * @return array The site's debug data. |
38 | */ |
39 | public static function get_raw_data() { |
40 | if ( ! empty( self::$raw_debug_data ) ) { |
41 | return self::$raw_debug_data; |
42 | } |
43 | |
44 | require_once wp_normalize_path( ABSPATH . '/wp-admin/includes/class-wp-debug-data.php' ); |
45 | |
46 | self::$raw_debug_data = \WP_Debug_Data::debug_data(); |
47 | |
48 | return self::$raw_debug_data; |
49 | } |
50 | |
51 | /** |
52 | * Retrieves the debug data for a site that is safe for sharing. |
53 | * |
54 | * Any data marked `private` in Site Health (database user, for example) will not be included in this list. |
55 | * |
56 | * @since 1.7.0 |
57 | * |
58 | * @return array List of Site Health debug data. |
59 | */ |
60 | public static function get_safe_data() { |
61 | if ( ! empty( self::$safe_debug_data ) ) { |
62 | return self::$safe_debug_data; |
63 | } |
64 | |
65 | $safe_data = array(); |
66 | |
67 | foreach ( self::get_raw_data() as $section => $details ) { |
68 | // Skip this section if there are no fields, or the section has been declared as private. |
69 | if ( empty( $details['fields'] ) || ( isset( $details['private'] ) && $details['private'] ) ) { |
70 | continue; |
71 | } |
72 | |
73 | foreach ( $details['fields'] as $field_name => $field ) { |
74 | if ( isset( $field['private'] ) && true === $field['private'] ) { |
75 | continue; |
76 | } |
77 | |
78 | if ( isset( $field['debug'] ) ) { |
79 | $debug_data = $field['debug']; |
80 | } else { |
81 | $debug_data = $field['value']; |
82 | } |
83 | |
84 | // Can be array, one level deep only. |
85 | if ( is_array( $debug_data ) ) { |
86 | $value = array(); |
87 | |
88 | foreach ( $debug_data as $sub_field_name => $sub_field_value ) { |
89 | $value[ $sub_field_name ] = $sub_field_value; |
90 | } |
91 | } elseif ( is_bool( $debug_data ) ) { |
92 | $value = $debug_data ? 'true' : 'false'; |
93 | } elseif ( empty( $debug_data ) && '0' !== $debug_data ) { |
94 | $value = 'undefined'; |
95 | } else { |
96 | $value = $debug_data; |
97 | } |
98 | |
99 | $safe_data[ $section ][ $field_name ] = $value; |
100 | } |
101 | } |
102 | |
103 | self::$safe_debug_data = $safe_data; |
104 | |
105 | return self::$safe_debug_data; |
106 | } |
107 | |
108 | /** |
109 | * Calculates the Site Health score for a site. |
110 | * |
111 | * The score is the number of successful tests (good) divided by the total number of tests. |
112 | * |
113 | * @since 1.7.0 |
114 | * |
115 | * @param string $results A JSON encoded string of Site Health test results. |
116 | * This will usually be the value of the `health-check-site-status-result` transient |
117 | * in WordPress Core. |
118 | * @return int Site Health score. |
119 | */ |
120 | public static function calculate_score( $results ) { |
121 | $results = json_decode( $results, true ); |
122 | |
123 | $total_tests = array_reduce( |
124 | $results, |
125 | function ( $total, $item ) { |
126 | return $total += (int) $item; |
127 | } |
128 | ); |
129 | |
130 | // Report a -1 when there are no Site Health tests |
131 | if ( 0 >= $total_tests ) { |
132 | return -1; |
133 | } |
134 | |
135 | return round( (int) $results['good'] / $total_tests * 100 ); |
136 | } |
137 | } |