PluginProbe
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution / 1.2.0
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution v1.2.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 1.2.0, at src/Performance/Page_Cache/Debug.php

134 lines 2.9 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 DateTime;
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 = ( new DateTime() )->format( 'Y-m-d h:i:s' );
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