| 1 |
<?php |
| 2 |
/** |
| 3 |
* The class that handles debug functionality for the page cache. |
| 4 |
* |
| 5 |
* @since 0.1.0 |
| 6 |
* |
| 7 |
* @package SolidWP\Performance |
| 8 |
*/ |
| 9 |
|
| 10 |
declare( strict_types=1 ); |
| 11 |
|
| 12 |
namespace SolidWP\Performance\Page_Cache; |
| 13 |
|
| 14 |
use DateTimeImmutable; |
| 15 |
use SolidWP\Performance\Config\Config; |
| 16 |
use SolidWP\Performance\Http\Request; |
| 17 |
use SolidWP\Performance\Page_Cache\Compression\Contracts\Compressible; |
| 18 |
|
| 19 |
if ( ! defined( 'ABSPATH' ) ) { |
| 20 |
exit; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Handles the debug functionality for the page cache. |
| 25 |
* |
| 26 |
* @since 0.1.0 |
| 27 |
* |
| 28 |
* @package SolidWP\Performance |
| 29 |
*/ |
| 30 |
class Debug { |
| 31 |
|
| 32 |
/** |
| 33 |
* The padding to insert between definition labels and values. |
| 34 |
* |
| 35 |
* @since 0.1.0 |
| 36 |
* |
| 37 |
* @var int |
| 38 |
*/ |
| 39 |
private int $pad = 23; |
| 40 |
|
| 41 |
/** |
| 42 |
* @var Config |
| 43 |
*/ |
| 44 |
private Config $config; |
| 45 |
|
| 46 |
/** |
| 47 |
* @param Config $config The config object. |
| 48 |
*/ |
| 49 |
public function __construct( Config $config ) { |
| 50 |
$this->config = $config; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Get the content for the page cache debug comment. |
| 55 |
* |
| 56 |
* @since 0.1.0 |
| 57 |
* |
| 58 |
* @param Request $request The current request to output information about. |
| 59 |
* @param Compressible $compressor The compressor used to save the cache file. |
| 60 |
* |
| 61 |
* @return string |
| 62 |
*/ |
| 63 |
public function get_debug_comment( Request $request, Compressible $compressor ): string { |
| 64 |
if ( $this->config->get( 'page_cache.debug' ) !== true ) { |
| 65 |
return ''; |
| 66 |
} |
| 67 |
|
| 68 |
$date_time = $this->get_timestamp(); |
| 69 |
$engine = str_pad( 'Engine:', $this->pad ) . 'Disk'; |
| 70 |
$creation_time = str_pad( 'Creation Time:', $this->pad ) . $request->start . 's'; |
| 71 |
$host = filter_var( $request->server['HTTP_HOST'], FILTER_SANITIZE_SPECIAL_CHARS ); |
| 72 |
|
| 73 |
$encoding = $compressor->encoding() ?: 'None'; |
| 74 |
$compression = str_pad( 'Compression Algorithm:', $this->pad ) . $encoding; |
| 75 |
|
| 76 |
$header_content = $this->get_headers( |
| 77 |
$request, |
| 78 |
[ |
| 79 |
'X-Powered-By' => 'HTTP_X_POWERED_BY', |
| 80 |
'Expires' => '', |
| 81 |
'Cache Control' => 'HTTP_CACHE_CONTROL', |
| 82 |
'Content-Type' => 'HTTP_CONTENT_TYPE', |
| 83 |
] |
| 84 |
); |
| 85 |
return " |
| 86 |
<!-- |
| 87 |
Solid Performance page optimization. Learn more: https://solidwp.com/performance |
| 88 |
|
| 89 |
Page cache debug info: |
| 90 |
|
| 91 |
$engine |
| 92 |
$creation_time |
| 93 |
$compression |
| 94 |
|
| 95 |
Header Info: |
| 96 |
|
| 97 |
$header_content |
| 98 |
|
| 99 |
Served from: $host @ $date_time by Solid Performance |
| 100 |
-->"; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Gets a list of headers to output in a list of labels and values. |
| 105 |
* |
| 106 |
* @since 0.1.0 |
| 107 |
* |
| 108 |
* @param Request $request The request in which to get headers. |
| 109 |
* @param array<string,string> $headers The labels and headers to output. |
| 110 |
* |
| 111 |
* @return string |
| 112 |
*/ |
| 113 |
public function get_headers( Request $request, array $headers = [] ): string { |
| 114 |
|
| 115 |
$header_content = ''; |
| 116 |
|
| 117 |
foreach ( $headers as $label => $header ) { |
| 118 |
if ( isset( $request->server[ $header ] ) ) { |
| 119 |
$label = sprintf( |
| 120 |
'%s:', |
| 121 |
$label |
| 122 |
); |
| 123 |
$header_content .= str_pad( $label, $this->pad ) . $request->server[ $header ]; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
if ( strlen( $header_content ) > 0 ) { |
| 128 |
return $header_content; |
| 129 |
} |
| 130 |
|
| 131 |
return ''; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Get the generated comment, shown on cached pages when debugging is DISABLED. |
| 136 |
* |
| 137 |
* @return string |
| 138 |
*/ |
| 139 |
public function get_generated_by_comment(): string { |
| 140 |
if ( $this->config->get( 'page_cache.debug' ) ) { |
| 141 |
return ''; |
| 142 |
} |
| 143 |
|
| 144 |
return sprintf( |
| 145 |
' |
| 146 |
<!-- Cached page generated by Solid Performance on %s -->', |
| 147 |
esc_html( $this->get_timestamp() ) |
| 148 |
); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Get a timestamp using the configured timezone from the WP dashboard. |
| 153 |
* |
| 154 |
* @return string |
| 155 |
*/ |
| 156 |
private function get_timestamp(): string { |
| 157 |
return ( new DateTimeImmutable( 'now', wp_timezone() ) )->format( 'Y-m-d H:i:s T' ); |
| 158 |
} |
| 159 |
} |
| 160 |
|