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 / Config / Advanced_Cache.php

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

102 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * All functionality related to creating and modifying the advanced-cache.php drop-in.
4 *
5 * @since 0.1.0
6 *
7 * @package SolidWP\Performance
8 */
9
10 namespace SolidWP\Performance\Config;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 /**
17 * Class to handle all functionality related to the advanced-cache.php drop-in.
18 *
19 * @since 0.1.0
20 *
21 * @package SolidWP\Performance
22 */
23 class Advanced_Cache {
24
25 /**
26 * The plugin's current version.
27 *
28 * @since 0.1.0
29 *
30 * @var string
31 */
32 private string $version;
33
34 /**
35 * The server path to the advanced-cache.php file.
36 *
37 * @var string
38 */
39 private string $destination;
40
41 /**
42 * @param string $version The plugin's current version.
43 * @param string $destination The server path to advanced-cache.php.
44 */
45 public function __construct( string $version, string $destination ) {
46 $this->version = $version;
47 $this->destination = $destination;
48 }
49
50
51 /**
52 * Generates a copy of an advanced-cache.php template and adds it to the wp-content directory.
53 *
54 * @since 0.1.0
55 *
56 * @param string $template Full path to a template used when generating the drop-in.
57 *
58 * @return bool
59 */
60 public function generate( string $template = '' ): bool {
61 $filesystem = swpsp_direct_filesystem();
62
63 $template = $template ?: dirname( SWPSP_PLUGIN_FILE ) . '/src/views/cache/advanced-cache.php';
64
65 // The plugin directory relative to WP_CONTENT_DIR.
66 $plugin_dir = basename( WP_PLUGIN_DIR ) . '/' . basename( dirname( SWPSP_PLUGIN_FILE ) );
67
68 $advanced_cache_content = $filesystem->get_contents( $template );
69
70 $advanced_cache_content = preg_replace( '/{{swpsp-cache-dir}}/', swpsp_config_get( 'page_cache.cache_dir' ), $advanced_cache_content );
71 $advanced_cache_content = preg_replace( '/{{swpsp-plugin-dir}}/', $plugin_dir, $advanced_cache_content );
72 $advanced_cache_content = preg_replace( '/{{swpsp-advanced-cache-version}}/', $this->version, $advanced_cache_content );
73
74 return $filesystem->put_contents( $this->destination, $advanced_cache_content, swpsp_get_file_mode() );
75 }
76
77 /**
78 * Whether an advanced-cache.php file exists.
79 *
80 * @return bool
81 */
82 public function exists(): bool {
83 return is_file( $this->destination );
84 }
85
86 /**
87 * Removes the advanced-cache drop-in.
88 *
89 * @since 0.1.0
90 *
91 * @return bool
92 */
93 public function remove(): bool {
94 // There is no file to remove, just return successfully.
95 if ( ! $this->exists() ) {
96 return true;
97 }
98
99 return swpsp_direct_filesystem()->delete( $this->destination );
100 }
101 }
102