Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 22 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| CacheFeatureHooks | |
0.00% |
0 / 22 |
|
0.00% |
0 / 6 |
90 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| plugin_hooks | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| hooks | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| on_activation | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| on_deactivation | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| on_cache_level_change | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Performance\Cache; |
| 4 | |
| 5 | use NewfoldLabs\WP\ModuleLoader\Container; |
| 6 | use NewfoldLabs\WP\Module\Performance\OptionListener; |
| 7 | use NewfoldLabs\WP\Module\Performance\Cache\Types\Browser; |
| 8 | use NewfoldLabs\WP\Module\Performance\Cache\Types\File; |
| 9 | |
| 10 | use function NewfoldLabs\WP\Module\Performance\get_cache_level; |
| 11 | |
| 12 | /** |
| 13 | * Add activation/deactivation hooks for the performance feature. |
| 14 | **/ |
| 15 | class CacheFeatureHooks { |
| 16 | |
| 17 | /** |
| 18 | * Dependency injection container. |
| 19 | * |
| 20 | * @var Container |
| 21 | */ |
| 22 | protected $container; |
| 23 | |
| 24 | /** |
| 25 | * Constructor. |
| 26 | */ |
| 27 | public function __construct() { |
| 28 | if ( function_exists( 'add_action' ) ) { |
| 29 | add_action( 'newfold_container_set', array( $this, 'plugin_hooks' ) ); |
| 30 | add_action( 'plugins_loaded', array( $this, 'hooks' ) ); |
| 31 | new OptionListener( CacheManager::OPTION_CACHE_LEVEL, array( $this, 'on_cache_level_change' ) ); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Hooks for plugin activation/deactivation |
| 37 | * |
| 38 | * @param Container $container from the plugin |
| 39 | */ |
| 40 | public function plugin_hooks( Container $container ) { |
| 41 | $this->container = $container; |
| 42 | register_activation_hook( $container->plugin()->file, array( $this, 'on_activation' ) ); |
| 43 | register_deactivation_hook( $container->plugin()->file, array( $this, 'on_deactivation' ) ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Add hooks. |
| 48 | */ |
| 49 | public function hooks() { |
| 50 | add_action( 'newfold/features/action/onEnable:performance', array( $this, 'on_activation' ) ); |
| 51 | add_action( 'newfold/features/action/onDisable:performance', array( $this, 'on_deactivation' ) ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Activation hook to perform when plugin is activated or feature is enabled |
| 56 | */ |
| 57 | public function on_activation() { |
| 58 | File::on_activation(); |
| 59 | Browser::on_activation(); |
| 60 | // Add headers to .htaccess |
| 61 | $responseHeaderManager = new ResponseHeaderManager(); |
| 62 | $responseHeaderManager->add_header( 'X-Newfold-Cache-Level', absint( get_cache_level() ) ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Deactivation hook to perform when plugin is deactivated or feature is disabled |
| 67 | */ |
| 68 | public function on_deactivation() { |
| 69 | File::on_deactivation(); |
| 70 | Browser::on_deactivation(); |
| 71 | // Remove all headers from .htaccess |
| 72 | $responseHeaderManager = new ResponseHeaderManager(); |
| 73 | $responseHeaderManager->remove_all_headers(); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * On cache level change, update the response headers. |
| 78 | * |
| 79 | * @param int|null $cacheLevel The cache level. |
| 80 | */ |
| 81 | public function on_cache_level_change( $cacheLevel ) { |
| 82 | /** |
| 83 | * Respone Header Manager from container |
| 84 | * |
| 85 | * @var ResponseHeaderManager $responseHeaderManager |
| 86 | */ |
| 87 | $responseHeaderManager = $this->container->get( 'responseHeaderManager' ); |
| 88 | $responseHeaderManager->add_header( 'X-Newfold-Cache-Level', absint( $cacheLevel ) ); |
| 89 | |
| 90 | // Remove the old option from EPC, if it exists. |
| 91 | if ( $this->container->get( 'hasMustUsePlugin' ) && absint( get_option( 'endurance_cache_level', 0 ) ) ) { |
| 92 | update_option( 'endurance_cache_level', 0 ); |
| 93 | delete_option( 'endurance_cache_level' ); |
| 94 | } |
| 95 | } |
| 96 | } |