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 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| add_to_runtime | |
0.00% |
0 / 5 |
|
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 function NewfoldLabs\WP\Module\Performance\get_cache_exclusion; |
| 8 | use function NewfoldLabs\WP\Module\Performance\get_cache_level; |
| 9 | |
| 10 | /** |
| 11 | * Cache manager. |
| 12 | */ |
| 13 | class Cache { |
| 14 | |
| 15 | /** |
| 16 | * Dependency injection container. |
| 17 | * |
| 18 | * @var Container |
| 19 | */ |
| 20 | protected $container; |
| 21 | |
| 22 | /** |
| 23 | * Constructor. |
| 24 | * |
| 25 | * @param Container $container the container |
| 26 | */ |
| 27 | public function __construct( Container $container ) { |
| 28 | $this->container = $container; |
| 29 | |
| 30 | $cacheManager = new CacheManager( $container ); |
| 31 | $cachePurger = new CachePurgingService( $cacheManager->get_instances() ); |
| 32 | |
| 33 | $container->set( 'cachePurger', $cachePurger ); |
| 34 | |
| 35 | new CacheExclusion( $container ); |
| 36 | |
| 37 | $container->set( 'hasMustUsePlugin', file_exists( WPMU_PLUGIN_DIR . '/endurance-page-cache.php' ) ); |
| 38 | |
| 39 | $this->hooks(); |
| 40 | |
| 41 | add_filter( 'newfold-runtime', array( $this, 'add_to_runtime' ), 100 ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Add hooks. |
| 46 | */ |
| 47 | public function hooks() { |
| 48 | add_action( 'after_mod_rewrite_rules', array( $this, 'on_rewrite' ) ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * When updating mod rewrite rules, also update our rewrites as appropriate. |
| 53 | */ |
| 54 | public function on_rewrite() { |
| 55 | $this->on_cache_level_change( get_cache_level() ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * On cache level change, update the response headers. |
| 60 | * |
| 61 | * @param int|null $cacheLevel The cache level. |
| 62 | */ |
| 63 | public function on_cache_level_change( $cacheLevel ) { |
| 64 | /** |
| 65 | * Respone Header Manager from container |
| 66 | * |
| 67 | * @var ResponseHeaderManager $responseHeaderManager |
| 68 | */ |
| 69 | $responseHeaderManager = $this->container->get( 'responseHeaderManager' ); |
| 70 | $responseHeaderManager->add_header( 'X-Newfold-Cache-Level', absint( $cacheLevel ) ); |
| 71 | |
| 72 | // Remove the old option from EPC, if it exists. |
| 73 | if ( $this->container->get( 'hasMustUsePlugin' ) && absint( get_option( 'endurance_cache_level', 0 ) ) ) { |
| 74 | update_option( 'endurance_cache_level', 0 ); |
| 75 | delete_option( 'endurance_cache_level' ); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Add to Newfold SDK runtime. |
| 81 | * |
| 82 | * @param array $sdk SDK data. |
| 83 | * @return array SDK data. |
| 84 | */ |
| 85 | public function add_to_runtime( $sdk ) { |
| 86 | |
| 87 | $values = array( |
| 88 | 'level' => get_cache_level(), |
| 89 | 'exclusion' => get_cache_exclusion(), |
| 90 | ); |
| 91 | |
| 92 | return array_merge( $sdk, array( 'cache' => $values ) ); |
| 93 | } |
| 94 | } |