| 1 |
<?php |
| 2 |
/** |
| 3 |
* All functionality related to activating 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 InvalidArgumentException; |
| 15 |
use SolidWP\Performance\Cache_Delivery\Htaccess\Updater; |
| 16 |
use SolidWP\Performance\Config\Advanced_Cache; |
| 17 |
use SolidWP\Performance\Config\Config; |
| 18 |
use SolidWP\Performance\Config\WP_Config; |
| 19 |
use SolidWP\Performance\Config\Writers\File_Writer; |
| 20 |
use SolidWP\Performance\Container; |
| 21 |
use SolidWP\Performance\Cron\Scheduler; |
| 22 |
|
| 23 |
if ( ! defined( 'ABSPATH' ) ) { |
| 24 |
exit; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Handles actions that should run when the plugin is activated. |
| 29 |
* |
| 30 |
* @since 0.1.0 |
| 31 |
* |
| 32 |
* @package SolidWP\Performance |
| 33 |
*/ |
| 34 |
final class Activator { |
| 35 |
|
| 36 |
/** |
| 37 |
* @var Container |
| 38 |
*/ |
| 39 |
private Container $container; |
| 40 |
|
| 41 |
/** |
| 42 |
* @param Container $container The container. |
| 43 |
*/ |
| 44 |
private function __construct( Container $container ) { |
| 45 |
$this->container = $container; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Lazy-instantiated callable for register_activation_hook. |
| 50 |
* |
| 51 |
* @return callable |
| 52 |
*/ |
| 53 |
public static function callback(): callable { |
| 54 |
return static function (): void { |
| 55 |
$instance = new self( swpsp_plugin()->init()->container() ); |
| 56 |
|
| 57 |
$instance->activate(); |
| 58 |
}; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Activation hook. |
| 63 |
* |
| 64 |
* @since 0.1.0 |
| 65 |
* |
| 66 |
* @return void |
| 67 |
*/ |
| 68 |
private function activate(): void { |
| 69 |
$this->advanced_cache_dropin_init(); |
| 70 |
$this->cache_directory_init(); |
| 71 |
$this->write_settings(); |
| 72 |
$this->wp_cache_init(); |
| 73 |
$this->update_htaccess_rules(); |
| 74 |
$this->register_scheduled_tasks(); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Ensure that the advanced-cache.php drop-in is copied into place. |
| 79 |
* |
| 80 |
* @since 0.1.0 |
| 81 |
* |
| 82 |
* @return void |
| 83 |
*/ |
| 84 |
private function advanced_cache_dropin_init(): void { |
| 85 |
$this->container->get( Advanced_Cache::class )->generate(); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Ensure that the cache directory exists. |
| 90 |
* |
| 91 |
* @since 0.1.0 |
| 92 |
* |
| 93 |
* @return void |
| 94 |
*/ |
| 95 |
private function cache_directory_init(): void { |
| 96 |
$cache_dir = $this->container->get( Config::class )->get( 'page_cache.cache_dir' ); |
| 97 |
|
| 98 |
if ( ! is_dir( $cache_dir ) ) { |
| 99 |
wp_mkdir_p( $cache_dir ); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
/** |
| 105 |
* Set the WP_CACHE constant. |
| 106 |
* |
| 107 |
* @since 0.1.0 |
| 108 |
* |
| 109 |
* @return void |
| 110 |
*/ |
| 111 |
private function wp_cache_init(): void { |
| 112 |
$this->container->get( WP_Config::class )->set_wp_cache( 'true' ); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Saves a set of default values in the config. |
| 117 |
* |
| 118 |
* @since 0.1.0 |
| 119 |
* |
| 120 |
* @return void |
| 121 |
*/ |
| 122 |
private function write_settings(): void { |
| 123 |
// Save config to the database. |
| 124 |
$this->container->get( Config::class )->save(); |
| 125 |
|
| 126 |
// Force writing the config.php file again. |
| 127 |
$this->container->get( File_Writer::class )->save(); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Update our htaccess rules. |
| 132 |
* |
| 133 |
* @throws InvalidArgumentException If an invalid cache delivery method is provided. |
| 134 |
* |
| 135 |
* @return void |
| 136 |
*/ |
| 137 |
private function update_htaccess_rules(): void { |
| 138 |
$this->container->get( Updater::class )->update_rules(); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Register scheduled tasks. |
| 143 |
* |
| 144 |
* @return void |
| 145 |
*/ |
| 146 |
private function register_scheduled_tasks(): void { |
| 147 |
$this->container->get( Scheduler::class )->enable_tasks(); |
| 148 |
} |
| 149 |
} |
| 150 |
|