Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 20 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Cache | |
0.00% |
0 / 20 |
|
0.00% |
0 / 5 |
56 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| hooks | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| on_rewrite | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| on_cache_level_change | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| add_to_runtime | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Performance\Cache; |
| 4 | |
| 5 | use NewfoldLabs\WP\ModuleLoader\Container; |
| 6 | |
| 7 | use NewfoldLabs\WP\Module\Performance\Cache\Types\ObjectCache; |
| 8 | |
| 9 | use function NewfoldLabs\WP\Module\Performance\get_cache_exclusion; |
| 10 | use function NewfoldLabs\WP\Module\Performance\get_cache_level; |
| 11 | |
| 12 | /** |
| 13 | * Cache manager. |
| 14 | */ |
| 15 | class Cache { |
| 16 | |
| 17 | /** |
| 18 | * Dependency injection container. |
| 19 | * |
| 20 | * @var Container |
| 21 | */ |
| 22 | protected $container; |
| 23 | |
| 24 | /** |
| 25 | * Constructor. |
| 26 | * |
| 27 | * @param Container $container the container |
| 28 | */ |
| 29 | public function __construct( Container $container ) { |
| 30 | $this->container = $container; |
| 31 | |
| 32 | $cacheManager = new CacheManager( $container ); |
| 33 | $cachePurger = new CachePurgingService( $cacheManager->get_instances() ); |
| 34 | |
| 35 | $container->set( 'cachePurger', $cachePurger ); |
| 36 | |
| 37 | new CacheExclusion( $container ); |
| 38 | |
| 39 | $container->set( 'hasMustUsePlugin', file_exists( WPMU_PLUGIN_DIR . '/endurance-page-cache.php' ) ); |
| 40 | |
| 41 | $this->hooks(); |
| 42 | |
| 43 | add_filter( 'newfold-runtime', array( $this, 'add_to_runtime' ), 100 ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Add hooks. |
| 48 | */ |
| 49 | public function hooks() { |
| 50 | add_action( 'after_mod_rewrite_rules', array( $this, 'on_rewrite' ) ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * When updating mod rewrite rules, also update our rewrites as appropriate. |
| 55 | */ |
| 56 | public function on_rewrite() { |
| 57 | $this->on_cache_level_change(); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * On cache level change, update the response headers. |
| 62 | */ |
| 63 | public function on_cache_level_change() { |
| 64 | // Remove the old option from EPC, if it exists. |
| 65 | if ( $this->container->get( 'hasMustUsePlugin' ) && absint( get_option( 'endurance_cache_level', 0 ) ) ) { |
| 66 | update_option( 'endurance_cache_level', 0 ); |
| 67 | delete_option( 'endurance_cache_level' ); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Add to Newfold SDK runtime. |
| 73 | * |
| 74 | * @param array $sdk SDK data. |
| 75 | * @return array SDK data. |
| 76 | */ |
| 77 | public function add_to_runtime( $sdk ) { |
| 78 | // If preference is "on" but the drop-in is missing, restore so runtime has correct enabled state. |
| 79 | ObjectCache::maybe_restore_dropin(); |
| 80 | $values = array( |
| 81 | 'level' => get_cache_level(), |
| 82 | 'exclusion' => get_cache_exclusion(), |
| 83 | 'objectCache' => ObjectCache::get_state(), |
| 84 | ); |
| 85 | |
| 86 | return array_merge( $sdk, array( 'cache' => $values ) ); |
| 87 | } |
| 88 | } |