| 1 |
<?php |
| 2 |
/** |
| 3 |
* All functionality related to deactivating the plugin. |
| 4 |
* |
| 5 |
* @since 0.1.0 |
| 6 |
* |
| 7 |
* @package SolidWP\Performance |
| 8 |
*/ |
| 9 |
|
| 10 |
declare( strict_types=1 ); |
| 11 |
|
| 12 |
namespace SolidWP\Performance\Plugin; |
| 13 |
|
| 14 |
use SolidWP\Performance\Cache_Delivery\Nginx\Manager as NginxManager; |
| 15 |
use SolidWP\Performance\Config\Advanced_Cache; |
| 16 |
use SolidWP\Performance\Container; |
| 17 |
use SolidWP\Performance\Cache_Delivery\Htaccess\Manager as HtaccessManager; |
| 18 |
use SolidWP\Performance\Cron\Scheduler; |
| 19 |
use SolidWP\Performance\Psr\Log\LoggerInterface; |
| 20 |
use Throwable; |
| 21 |
|
| 22 |
if ( ! defined( 'ABSPATH' ) ) { |
| 23 |
exit; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Handles actions that should run when the plugin is deactivated. |
| 28 |
* |
| 29 |
* @since 0.1.0 |
| 30 |
* |
| 31 |
* @package SolidWP\Performance |
| 32 |
*/ |
| 33 |
final class Deactivator { |
| 34 |
|
| 35 |
/** |
| 36 |
* @var Container |
| 37 |
*/ |
| 38 |
private Container $container; |
| 39 |
|
| 40 |
/** |
| 41 |
* The logger. |
| 42 |
* |
| 43 |
* @var LoggerInterface |
| 44 |
*/ |
| 45 |
private LoggerInterface $logger; |
| 46 |
|
| 47 |
/** |
| 48 |
* @param Container $container The container. |
| 49 |
* @param LoggerInterface $logger The logger. |
| 50 |
*/ |
| 51 |
private function __construct( |
| 52 |
Container $container, |
| 53 |
LoggerInterface $logger |
| 54 |
) { |
| 55 |
$this->container = $container; |
| 56 |
$this->logger = $logger; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Lazy-instantiated callable for register_deactivation_hook. |
| 61 |
* |
| 62 |
* @return callable |
| 63 |
*/ |
| 64 |
public static function callback(): callable { |
| 65 |
return static function (): void { |
| 66 |
$container = swpsp_plugin()->init()->container(); |
| 67 |
$logger = $container->get( LoggerInterface::class ); |
| 68 |
|
| 69 |
$instance = new self( $container, $logger ); |
| 70 |
|
| 71 |
$instance->deactivate(); |
| 72 |
}; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Deactivation hook. |
| 77 |
* |
| 78 |
* @since 0.1.0 |
| 79 |
* |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
private function deactivate(): void { |
| 83 |
$this->remove_advanced_cache(); |
| 84 |
$this->remove_htaccess_rules(); |
| 85 |
$this->add_nginx_bypass_rules(); |
| 86 |
$this->clear_scheduled_tasks(); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Remove the generated advanced-cache.php drop-in. |
| 91 |
* |
| 92 |
* @since 0.1.0 |
| 93 |
* |
| 94 |
* @return void |
| 95 |
*/ |
| 96 |
private function remove_advanced_cache(): void { |
| 97 |
$this->container->get( Advanced_Cache::class )->remove(); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Remove our htaccess rules. |
| 102 |
* |
| 103 |
* @return void |
| 104 |
*/ |
| 105 |
private function remove_htaccess_rules(): void { |
| 106 |
$this->container->get( HtaccessManager::class )->remove_rules(); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Write the "bypass cache rules" to the swpsp-nginx.conf. We don't want to delete |
| 111 |
* this file as it could bring down their site on the next Nginx reload. |
| 112 |
* |
| 113 |
* @return void |
| 114 |
*/ |
| 115 |
private function add_nginx_bypass_rules(): void { |
| 116 |
$manager = $this->container->get( NginxManager::class ); |
| 117 |
|
| 118 |
try { |
| 119 |
if ( ! $manager->exists() ) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
$manager->bypass(); |
| 124 |
} catch ( Throwable $e ) { |
| 125 |
$this->logger->error( |
| 126 |
'Deactivation: error adding Nginx bypass rules', |
| 127 |
[ |
| 128 |
'error' => $e->getMessage(), |
| 129 |
'exception' => $e, |
| 130 |
] |
| 131 |
); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Clear scheduled tasks. |
| 137 |
* |
| 138 |
* @return void |
| 139 |
*/ |
| 140 |
private function clear_scheduled_tasks(): void { |
| 141 |
$this->container->get( Scheduler::class )->disable_tasks(); |
| 142 |
} |
| 143 |
} |
| 144 |
|