PluginProbe
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution / 1.5.0
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution v1.5.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 / Cache_Path.php

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

149 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * A single place to get cache path directories.
4 *
5 * @package SolidWP\Performance
6 */
7
8 declare( strict_types=1 );
9
10 namespace SolidWP\Performance\Page_Cache;
11
12 use RecursiveDirectoryIterator;
13 use RecursiveIteratorIterator;
14 use RegexIterator;
15 use RuntimeException;
16
17 /**
18 * A single place to get cache path directories.
19 *
20 * @package SolidWP\Performance
21 */
22 class Cache_Path {
23
24 /**
25 * The cache directory, e.g. /app/wp-content/cache.
26 *
27 * @var string
28 */
29 private string $cache_dir;
30
31 /**
32 * @param string $cache_dir The cache directory, e.g. /app/wp-content/cache.
33 */
34 public function __construct( string $cache_dir ) {
35 $this->cache_dir = $cache_dir;
36 }
37
38 /**
39 * Returns the server path to the cache directory.
40 *
41 * @example /app/wp-content/cache
42 *
43 * @return string
44 */
45 public function get_cache_dir(): string {
46 return $this->cache_dir;
47 }
48
49 /**
50 * Get the path to where pages are cached.
51 *
52 * @example /app/wp-content/cache/page
53 *
54 * @return string
55 */
56 public function get_page_cache_dir(): string {
57 return $this->get_cache_dir() . '/page';
58 }
59
60 /**
61 * Get the host site cache directory.
62 *
63 * @example /app/wp-content/cache/page/www.wordpress.test
64 *
65 * @return string
66 */
67 public function get_site_cache_dir(): string {
68 $path = $this->get_page_cache_dir();
69 $site_host = wp_parse_url( get_site_url(), PHP_URL_HOST );
70
71
72 return $path . DIRECTORY_SEPARATOR . $site_host;
73 }
74
75 /**
76 * Converts a URL into a directory structure.
77 *
78 * All cached requests will be saved to a directory structure that matches
79 * the relative URL. This provides a way to consistently get the cached
80 * resource from the URL.
81 *
82 * @example /app/wp-content/cache/page/local.test.com/test-post
83 *
84 * @throws RuntimeException When URL does not have valid host.
85 *
86 * @param string $url The full URL of the request (e.g. https://solidwp.com/about).
87 *
88 * @return string
89 */
90 public function get_path_from_url( string $url ): string {
91 $url_parts = parse_url( $url ); // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url
92
93 if ( ! isset( $url_parts['host'] ) || $url_parts['host'] === '' ) {
94 throw new RuntimeException( 'URL needs a valid host.' );
95 }
96
97 $host = trim( $url_parts['host'], '/' );
98 $path = trim( $url_parts['path'] ?? '', '/' );
99
100 if ( $path === '' ) {
101 $path = 'index';
102 }
103
104 return "{$this->get_page_cache_dir()}/$host/$path";
105 }
106
107 /**
108 * Recursively count how many cache files we have.
109 *
110 * @note Keep in mind one URL can generate multiple cache files, depending on the
111 * compression strategies available on the server and what different browsers
112 * ask for.
113 *
114 * @param string $path The optional path.
115 *
116 * @return int
117 */
118 public function count( string $path = '' ): int {
119 $count = 0;
120
121 if ( ! $path ) {
122 $path = $this->get_site_cache_dir();
123 }
124
125 if ( ! file_exists( $path ) ) {
126 return $count;
127 }
128
129 $iterator = new RecursiveIteratorIterator(
130 new RecursiveDirectoryIterator( $path, RecursiveDirectoryIterator::SKIP_DOTS )
131 );
132
133 // Exclude the .meta.php files.
134 $filtered = new RegexIterator(
135 $iterator,
136 '/^(?!.*\.php$).*$/i',
137 RegexIterator::MATCH
138 );
139
140 foreach ( $filtered as $file ) {
141 if ( $file->isFile() && $file->getFilename()[0] !== '.' ) {
142 ++$count;
143 }
144 }
145
146 return $count;
147 }
148 }
149