| 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 current version of the advanced cache file. |
| 27 |
* |
| 28 |
* @since 0.1.0 |
| 29 |
* |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
private string $version = '0.1.0'; |
| 33 |
|
| 34 |
/** |
| 35 |
* Generates a copy of an advanced-cache.php template and adds it to the wp-content directory. |
| 36 |
* |
| 37 |
* @since 0.1.0 |
| 38 |
* |
| 39 |
* @param string $template Full path to a template used when generating the drop-in. |
| 40 |
* @param string $destination Full path where the advanced-cache drop-in should be placed. Defaults to the wp-content directory. |
| 41 |
* |
| 42 |
* @return bool |
| 43 |
*/ |
| 44 |
public function generate( string $template = '', string $destination = '' ): bool { |
| 45 |
$filesystem = swpsp_direct_filesystem(); |
| 46 |
|
| 47 |
$template = $template ?: dirname( SWPSP_PLUGIN_FILE ) . '/src/views/cache/advanced-cache.php'; |
| 48 |
|
| 49 |
// The plugin directory relative to WP_CONTENT_DIR. |
| 50 |
$plugin_dir = basename( WP_PLUGIN_DIR ) . '/' . basename( dirname( SWPSP_PLUGIN_FILE ) ); |
| 51 |
|
| 52 |
$advanced_cache_content = $filesystem->get_contents( $template ); |
| 53 |
|
| 54 |
$advanced_cache_content = preg_replace( '/{{swpsp-cache-dir}}/', swpsp_config_get( 'page_cache.cache_dir' ), $advanced_cache_content ); |
| 55 |
$advanced_cache_content = preg_replace( '/{{swpsp-plugin-file}}/', SWPSP_PLUGIN_FILE, $advanced_cache_content ); |
| 56 |
$advanced_cache_content = preg_replace( '/{{swpsp-plugin-dir}}/', $plugin_dir, $advanced_cache_content ); |
| 57 |
$advanced_cache_content = preg_replace( '/{{swpsp-advanced-cache-version}}/', $this->version, $advanced_cache_content ); |
| 58 |
|
| 59 |
$destination = $destination ?: WP_CONTENT_DIR . '/advanced-cache.php'; |
| 60 |
|
| 61 |
return $filesystem->put_contents( $destination, $advanced_cache_content, swpsp_get_file_mode() ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Removes the advanced-cache drop-in. |
| 66 |
* |
| 67 |
* @since 0.1.0 |
| 68 |
* |
| 69 |
* @return bool |
| 70 |
*/ |
| 71 |
public function remove(): bool { |
| 72 |
$file = WP_CONTENT_DIR . '/advanced-cache.php'; |
| 73 |
|
| 74 |
if ( ! is_file( $file ) ) { |
| 75 |
return true; // There is no file to remove, just return successfully. |
| 76 |
} |
| 77 |
|
| 78 |
return swpsp_direct_filesystem()->delete( $file ); |
| 79 |
} |
| 80 |
} |
| 81 |
|