| 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-file}}/', SWPSP_PLUGIN_FILE, $advanced_cache_content ); |
| 72 |
$advanced_cache_content = preg_replace( '/{{swpsp-plugin-dir}}/', $plugin_dir, $advanced_cache_content ); |
| 73 |
$advanced_cache_content = preg_replace( '/{{swpsp-advanced-cache-version}}/', $this->version, $advanced_cache_content ); |
| 74 |
|
| 75 |
return $filesystem->put_contents( $this->destination, $advanced_cache_content, swpsp_get_file_mode() ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Whether an advanced-cache.php file exists. |
| 80 |
* |
| 81 |
* @return bool |
| 82 |
*/ |
| 83 |
public function exists(): bool { |
| 84 |
return is_file( $this->destination ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Removes the advanced-cache drop-in. |
| 89 |
* |
| 90 |
* @since 0.1.0 |
| 91 |
* |
| 92 |
* @return bool |
| 93 |
*/ |
| 94 |
public function remove(): bool { |
| 95 |
// There is no file to remove, just return successfully. |
| 96 |
if ( ! $this->exists() ) { |
| 97 |
return true; |
| 98 |
} |
| 99 |
|
| 100 |
return swpsp_direct_filesystem()->delete( $this->destination ); |
| 101 |
} |
| 102 |
} |
| 103 |
|