| 1 |
<?php |
| 2 |
/** |
| 3 |
* A set of methods that let other developers interact with Solid Performance. |
| 4 |
* |
| 5 |
* @since 0.1.0 |
| 6 |
* |
| 7 |
* @package SolidWP\Performance |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace SolidWP\Performance; |
| 11 |
|
| 12 |
use SolidWP\Performance\Config\Advanced_Cache; |
| 13 |
use SolidWP\Performance\Config\Config; |
| 14 |
use SolidWP\Performance\Config\WP_Config; |
| 15 |
use SolidWP\Performance\Page_Cache\Purge\Purge; |
| 16 |
|
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
exit; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* A set of methods that let other developers interact with Solid Performance. |
| 23 |
* |
| 24 |
* @since 0.1.0 |
| 25 |
* |
| 26 |
* @package SolidWP\Performance |
| 27 |
*/ |
| 28 |
class Page_Cache { |
| 29 |
|
| 30 |
/** |
| 31 |
* @var Config |
| 32 |
*/ |
| 33 |
private Config $config; |
| 34 |
|
| 35 |
/** |
| 36 |
* @var WP_Config |
| 37 |
*/ |
| 38 |
private WP_Config $wp_config; |
| 39 |
|
| 40 |
/** |
| 41 |
* @var Purge |
| 42 |
*/ |
| 43 |
private Purge $purge; |
| 44 |
|
| 45 |
/** |
| 46 |
* @var Advanced_Cache |
| 47 |
*/ |
| 48 |
private Advanced_Cache $advanced_cache; |
| 49 |
|
| 50 |
/** |
| 51 |
* @param Config $config The config class. |
| 52 |
* @param WP_Config $wp_config The wp config class. |
| 53 |
* @param Purge $purge The cache purger. |
| 54 |
* @param Advanced_Cache $advanced_cache The advanced cache?. |
| 55 |
*/ |
| 56 |
public function __construct( |
| 57 |
Config $config, |
| 58 |
WP_Config $wp_config, |
| 59 |
Purge $purge, |
| 60 |
Advanced_Cache $advanced_cache |
| 61 |
) { |
| 62 |
$this->config = $config; |
| 63 |
$this->wp_config = $wp_config; |
| 64 |
$this->purge = $purge; |
| 65 |
$this->advanced_cache = $advanced_cache; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Enables the page cache. |
| 70 |
* |
| 71 |
* @since 0.1.0 |
| 72 |
* |
| 73 |
* @return bool true on success, false on failure |
| 74 |
*/ |
| 75 |
public function on(): bool { |
| 76 |
$this->config->set( 'page_cache.enabled', true )->queue(); |
| 77 |
$this->wp_config->set_wp_cache( 'true' ); |
| 78 |
|
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Disables the page cache. |
| 84 |
* |
| 85 |
* @since 0.1.0 |
| 86 |
* |
| 87 |
* @return bool true on success, false on failure |
| 88 |
*/ |
| 89 |
public function off(): bool { |
| 90 |
$this->config->set( 'page_cache.enabled', false )->queue(); |
| 91 |
$this->wp_config->set_wp_cache( 'false' ); |
| 92 |
|
| 93 |
return true; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Checks if the page cache is currently enabled. |
| 98 |
* |
| 99 |
* @since 0.1.0 |
| 100 |
* |
| 101 |
* @return bool true when enabled, false when disabled. |
| 102 |
*/ |
| 103 |
public function is_on(): bool { |
| 104 |
return $this->wp_config->get_wp_cache_status(); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Clears all pages from the page cache. |
| 109 |
* |
| 110 |
* @since 0.1.0 |
| 111 |
* |
| 112 |
* @return bool |
| 113 |
*/ |
| 114 |
public function clear(): bool { |
| 115 |
return $this->purge->all_pages(); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Regenerates the advanced-cache.php file. |
| 120 |
* |
| 121 |
* @since 0.1.0 |
| 122 |
* |
| 123 |
* @return bool |
| 124 |
*/ |
| 125 |
public function regenerate_advanced_cache(): bool { |
| 126 |
return $this->advanced_cache->generate(); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Gets the current status of debug mode. |
| 131 |
* |
| 132 |
* @since 0.1.0 |
| 133 |
* |
| 134 |
* @return bool |
| 135 |
*/ |
| 136 |
public function is_debug_on(): bool { |
| 137 |
return $this->config->get( 'page_cache.debug' ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Sets the state of debug mode. |
| 142 |
* |
| 143 |
* @since 0.1.0 |
| 144 |
* |
| 145 |
* @param bool $state The state for debug mode. |
| 146 |
* |
| 147 |
* @return bool |
| 148 |
*/ |
| 149 |
public function debug( bool $state ): bool { |
| 150 |
$this->config->set( 'page_cache.debug', $state )->queue(); |
| 151 |
|
| 152 |
return (bool) $this->config->get( 'page_cache.debug' ); |
| 153 |
} |
| 154 |
} |
| 155 |
|