| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles adding a new section to the site health data screen. |
| 4 |
* |
| 5 |
* @since 0.1.0 |
| 6 |
* |
| 7 |
* @package SolidWP\Performance |
| 8 |
*/ |
| 9 |
|
| 10 |
declare( strict_types=1 ); |
| 11 |
|
| 12 |
namespace SolidWP\Performance\Telemetry; |
| 13 |
|
| 14 |
use RuntimeException; |
| 15 |
use SolidWP\Performance\Page_Cache\Compression\Collection; |
| 16 |
use SolidWP\Performance\Page_Cache\Compression\Contracts\Compressible; |
| 17 |
use SolidWP\Performance\Preload\Limiter\Core_Counter; |
| 18 |
use SolidWP\Performance\Preload\Limiter\Load\System_Load_Monitor; |
| 19 |
use SolidWP\Performance\Preload\Preload_Mode_Manager; |
| 20 |
|
| 21 |
/** |
| 22 |
* Handles adding a new section to the site health data screen. |
| 23 |
* |
| 24 |
* @since 0.1.0 |
| 25 |
* |
| 26 |
* @package SolidWP\Performance |
| 27 |
*/ |
| 28 |
class Health_Data { |
| 29 |
|
| 30 |
/** |
| 31 |
* The collection of compression strategies. |
| 32 |
* |
| 33 |
* @var Collection |
| 34 |
*/ |
| 35 |
private Collection $compressors; |
| 36 |
|
| 37 |
/** |
| 38 |
* @var System_Load_Monitor |
| 39 |
*/ |
| 40 |
private System_Load_Monitor $system_load_monitor; |
| 41 |
|
| 42 |
/** |
| 43 |
* @var Core_Counter |
| 44 |
*/ |
| 45 |
private Core_Counter $core_counter; |
| 46 |
|
| 47 |
/** |
| 48 |
* @var Preload_Mode_Manager |
| 49 |
*/ |
| 50 |
private Preload_Mode_Manager $preload_mode_manager; |
| 51 |
|
| 52 |
/** |
| 53 |
* @param Collection $compressors The collection of compression strategies. |
| 54 |
* @param System_Load_Monitor $system_load_monitor The system load monitor. |
| 55 |
* @param Core_Counter $core_counter The core counter. |
| 56 |
* @param Preload_Mode_Manager $preload_mode_manager The preload mode manager. |
| 57 |
*/ |
| 58 |
public function __construct( |
| 59 |
Collection $compressors, |
| 60 |
System_Load_Monitor $system_load_monitor, |
| 61 |
Core_Counter $core_counter, |
| 62 |
Preload_Mode_Manager $preload_mode_manager |
| 63 |
) { |
| 64 |
$this->compressors = $compressors; |
| 65 |
$this->system_load_monitor = $system_load_monitor; |
| 66 |
$this->core_counter = $core_counter; |
| 67 |
$this->preload_mode_manager = $preload_mode_manager; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Adds a new Solid Performance section to site health data. |
| 72 |
* |
| 73 |
* @since 0.1.0 |
| 74 |
* |
| 75 |
* @filter debug_information |
| 76 |
* |
| 77 |
* @param array $info The array of site health data. |
| 78 |
* |
| 79 |
* @return array |
| 80 |
*/ |
| 81 |
public function add_summary_to_telemetry( array $info ): array { |
| 82 |
$page_cache = swpsp_config_get( 'page_cache' ); |
| 83 |
$page_cache_status = $page_cache['enabled'] ? esc_html__( 'Enabled', 'LION' ) : esc_html__( 'Disabled', 'LION' ); |
| 84 |
$cache_dir_writeable = swpsp_direct_filesystem()->is_writable( $page_cache['cache_dir'] ) ? esc_html__( 'Writable', 'LION' ) : esc_html__( 'Not Writable', 'LION' ); |
| 85 |
$debug_mode_status = $page_cache['debug'] ? esc_html__( 'Enabled', 'LION' ) : esc_html__( 'Disabled', 'LION' ); |
| 86 |
$exclusion_count = count( $page_cache['exclusions'] ); |
| 87 |
$enabled_compressors = implode( |
| 88 |
', ', |
| 89 |
array_filter( |
| 90 |
array_map( static fn( Compressible $c ): string => $c->encoding(), $this->compressors->enabled() ) |
| 91 |
) |
| 92 |
); |
| 93 |
|
| 94 |
try { |
| 95 |
$system_load = []; |
| 96 |
$system_load_debug = []; |
| 97 |
$load_averages = $this->system_load_monitor->load()->all(); |
| 98 |
|
| 99 |
foreach ( $load_averages as $label => $load_average ) { |
| 100 |
$new_label = sprintf( |
| 101 |
/* translators: %s: The system load time, e.g. 1, 5, or 15. */ |
| 102 |
_n( |
| 103 |
'%s min', |
| 104 |
'%s mins', |
| 105 |
$label, |
| 106 |
'LION' |
| 107 |
), |
| 108 |
$label |
| 109 |
); |
| 110 |
|
| 111 |
$formatted_average = number_format( $load_average, 2 ); |
| 112 |
|
| 113 |
$system_load[ $new_label ] = $formatted_average; |
| 114 |
$system_load_debug[ $label ] = $formatted_average; |
| 115 |
} |
| 116 |
} catch ( RuntimeException $e ) { |
| 117 |
$system_load = $e->getMessage(); |
| 118 |
$system_load_debug = $e->getMessage(); |
| 119 |
} |
| 120 |
|
| 121 |
$core_count = $this->core_counter->core_count(); |
| 122 |
|
| 123 |
$info['solid-performance'] = [ |
| 124 |
'label' => esc_html__( 'Solid Performance', 'LION' ), |
| 125 |
'fields' => [ |
| 126 |
'page_cache_status' => [ |
| 127 |
'label' => esc_html__( 'Page cache', 'LION' ), |
| 128 |
'value' => $page_cache_status, |
| 129 |
'debug' => strtolower( $page_cache_status ), |
| 130 |
], |
| 131 |
'cache_directory' => [ |
| 132 |
'label' => esc_html__( 'Cache directory', 'LION' ), |
| 133 |
'value' => $page_cache['cache_dir'], |
| 134 |
'debug' => $page_cache['cache_dir'], |
| 135 |
], |
| 136 |
'cache_directory_writable' => [ |
| 137 |
'label' => esc_html__( 'Cache directory permissions', 'LION' ), |
| 138 |
'value' => $cache_dir_writeable, |
| 139 |
'debug' => strtolower( $cache_dir_writeable ), |
| 140 |
], |
| 141 |
'debug_mode' => [ |
| 142 |
'label' => esc_html__( 'Debug mode', 'LION' ), |
| 143 |
'value' => $debug_mode_status, |
| 144 |
'debug' => strtolower( $debug_mode_status ), |
| 145 |
], |
| 146 |
'exclusion_count' => [ |
| 147 |
'label' => esc_html__( 'Number of custom exclusions', 'LION' ), |
| 148 |
'value' => $exclusion_count, |
| 149 |
], |
| 150 |
'compression' => [ |
| 151 |
'label' => esc_html__( 'Supported compression algorithms', 'LION' ), |
| 152 |
'value' => $enabled_compressors, |
| 153 |
'debug' => $enabled_compressors, |
| 154 |
], |
| 155 |
'system_load' => [ |
| 156 |
'label' => esc_html__( 'System load average', 'LION' ), |
| 157 |
'value' => $system_load, |
| 158 |
'debug' => $system_load_debug, |
| 159 |
], |
| 160 |
'core_count' => [ |
| 161 |
'label' => esc_html__( 'System CPU core count', 'LION' ), |
| 162 |
'value' => $core_count, |
| 163 |
], |
| 164 |
'high_performance_mode' => [ |
| 165 |
'label' => esc_html__( 'High performance mode', 'LION' ), |
| 166 |
'value' => $this->preload_mode_manager->is_high_performance_mode() ? esc_html__( 'Enabled', 'LION' ) : esc_html__( 'Disabled', 'LION' ), |
| 167 |
'debug' => $this->preload_mode_manager->is_high_performance_mode(), |
| 168 |
], |
| 169 |
], |
| 170 |
]; |
| 171 |
|
| 172 |
return $info; |
| 173 |
} |
| 174 |
} |
| 175 |
|