PluginProbe
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution / 2.0.0
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution v2.0.0
2.0.1 trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.5.0 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.9.0 2.0.0
solid-performance / src / Performance / Page_Cache / Debug.php

Debug.php in Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution 2.0.0, at src/Performance/Page_Cache/Debug.php

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