AdvancedCache.php
66 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Nitropack\WordPress\AdvancedCache; |
| 4 | |
| 5 | /** |
| 6 | * Here we handle the installation and uninstallation of the advanced_cache.php file stored in wp-content folder. |
| 7 | */ |
| 8 | class AdvancedCache { |
| 9 | public function __construct() { |
| 10 | if ( $this->has_advanced_cache() ) { |
| 11 | // Handle automated updates |
| 12 | if ( ! defined( "NITROPACK_ADVANCED_CACHE_VERSION" ) || NITROPACK_VERSION != NITROPACK_ADVANCED_CACHE_VERSION ) { |
| 13 | add_action( 'plugins_loaded', [ $this, 'install_advanced_cache' ] ); |
| 14 | } |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * @return void|false Returns false if the advanced-cache.php file cannot be created, otherwise returns void. |
| 20 | * @description Installs the advanced-cache.php file in the WP_CONTENT_DIR directory. This file is required for NitroPack to work properly. If the file cannot be created, the function returns |
| 21 | */ |
| 22 | public function install_advanced_cache() { |
| 23 | $conflictingPlugins = \NitroPack\WordPress\ConflictingPlugins::getInstance(); |
| 24 | $nitropack_is_conflicting_plugin_active = $conflictingPlugins->nitropack_is_conflicting_plugin_active(); |
| 25 | if ( $nitropack_is_conflicting_plugin_active || ! nitropack_is_advanced_cache_allowed() ) { |
| 26 | return false; |
| 27 | } |
| 28 | |
| 29 | $templatePath = nitropack_trailingslashit( NITROPACK_PLUGIN_DIR ) . "/classes/WordPress/AdvancedCache/advanced-cache.php"; |
| 30 | if ( file_exists( $templatePath ) ) { |
| 31 | $contents = file_get_contents( $templatePath ); |
| 32 | $contents = str_replace( "/*NITROPACK_FUNCTIONS_FILE*/", NITROPACK_PLUGIN_DIR . 'functions.php', $contents ); |
| 33 | $contents = str_replace( "/*NITROPACK_ABSPATH*/", ABSPATH, $contents ); |
| 34 | $contents = str_replace( "/*LOGIN_COOKIES*/", defined( "LOGGED_IN_COOKIE" ) ? LOGGED_IN_COOKIE : "", $contents ); |
| 35 | $contents = str_replace( "/*NP_VERSION*/", NITROPACK_VERSION, $contents ); |
| 36 | |
| 37 | $advancedCacheFile = nitropack_trailingslashit( WP_CONTENT_DIR ) . 'advanced-cache.php'; |
| 38 | return WP_DEBUG |
| 39 | ? file_put_contents( $advancedCacheFile, $contents ) |
| 40 | : @file_put_contents( $advancedCacheFile, $contents ); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @return void|false Returns false if the advanced-cache.php file cannot be created, otherwise returns void. |
| 46 | * @description Uninstalls the advanced-cache.php file in the WP_CONTENT_DIR directory. |
| 47 | */ |
| 48 | public function uninstall_advanced_cache() { |
| 49 | $advancedCacheFile = nitropack_trailingslashit( WP_CONTENT_DIR ) . 'advanced-cache.php'; |
| 50 | if ( file_exists( $advancedCacheFile ) ) { |
| 51 | if ( WP_DEBUG ) { |
| 52 | return file_put_contents( $advancedCacheFile, "" ); |
| 53 | } else { |
| 54 | return @file_put_contents( $advancedCacheFile, "" ); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @return bool |
| 61 | * @description Checks whether the constant NITROPACK_ADVANCED_CACHE is defined. |
| 62 | */ |
| 63 | public function has_advanced_cache() { |
| 64 | return defined( 'NITROPACK_ADVANCED_CACHE' ); |
| 65 | } |
| 66 | } |