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

104 lines 2.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 RuntimeException;
13
14 /**
15 * A single place to get cache path directories.
16 *
17 * @package SolidWP\Performance
18 */
19 final class Cache_Path {
20
21 /**
22 * The cache directory, e.g. /app/wp-content/cache.
23 *
24 * @var string
25 */
26 private string $cache_dir;
27
28 /**
29 * @param string $cache_dir The cache directory, e.g. /app/wp-content/cache.
30 */
31 public function __construct( string $cache_dir ) {
32 $this->cache_dir = $cache_dir;
33 }
34
35 /**
36 * Returns the server path to the cache directory.
37 *
38 * @example /app/wp-content/cache
39 *
40 * @return string
41 */
42 public function get_cache_dir(): string {
43 return $this->cache_dir;
44 }
45
46 /**
47 * Get the path to where pages are cached.
48 *
49 * @example /app/wp-content/cache/page
50 *
51 * @return string
52 */
53 public function get_page_cache_dir(): string {
54 return $this->get_cache_dir() . '/page';
55 }
56
57 /**
58 * Get the host site cache directory.
59 *
60 * @example /app/wp-content/cache/page/www.wordpress.test
61 *
62 * @return string
63 */
64 public function get_site_cache_dir(): string {
65 $path = $this->get_page_cache_dir();
66 $site_host = wp_parse_url( get_site_url(), PHP_URL_HOST );
67
68
69 return $path . DIRECTORY_SEPARATOR . $site_host;
70 }
71
72 /**
73 * Converts a URL into a directory structure.
74 *
75 * All cached requests will be saved to a directory structure that matches
76 * the relative URL. This provides a way to consistently get the cached
77 * resource from the URL.
78 *
79 * @example /app/wp-content/cache/page/local.test.com/test-post
80 *
81 * @throws RuntimeException When URL does not have valid host.
82 *
83 * @param string $url The full URL of the request (e.g. https://solidwp.com/about).
84 *
85 * @return string
86 */
87 public function get_path_from_url( string $url ): string {
88 $url_parts = parse_url( $url ); // phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url
89
90 if ( ! isset( $url_parts['host'] ) || $url_parts['host'] === '' ) {
91 throw new RuntimeException( 'URL needs a valid host.' );
92 }
93
94 $host = trim( $url_parts['host'], '/' );
95 $path = trim( $url_parts['path'] ?? '', '/' );
96
97 if ( $path === '' ) {
98 $path = 'index';
99 }
100
101 return "{$this->get_page_cache_dir()}/$host/$path";
102 }
103 }
104