| 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 SolidWP\Performance\Config\Advanced_Cache; |
| 15 |
use SolidWP\Performance\Config\Config; |
| 16 |
use SolidWP\Performance\Config\WP_Config; |
| 17 |
use SolidWP\Performance\Config\Writers\File_Writer; |
| 18 |
use SolidWP\Performance\Container; |
| 19 |
|
| 20 |
if ( ! defined( 'ABSPATH' ) ) { |
| 21 |
exit; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Handles actions that should run when the plugin is activated. |
| 26 |
* |
| 27 |
* @since 0.1.0 |
| 28 |
* |
| 29 |
* @package SolidWP\Performance |
| 30 |
*/ |
| 31 |
final class Activator { |
| 32 |
|
| 33 |
/** |
| 34 |
* @var Container |
| 35 |
*/ |
| 36 |
private Container $container; |
| 37 |
|
| 38 |
/** |
| 39 |
* @param Container $container The container. |
| 40 |
*/ |
| 41 |
public function __construct( Container $container ) { |
| 42 |
$this->container = $container; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Activation hook. |
| 47 |
* |
| 48 |
* @since 0.1.0 |
| 49 |
* |
| 50 |
* @return void |
| 51 |
*/ |
| 52 |
public function __invoke(): void { |
| 53 |
$this->advanced_cache_dropin_init(); |
| 54 |
$this->cache_directory_init(); |
| 55 |
$this->write_settings(); |
| 56 |
$this->wp_cache_init(); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Ensure that the advanced-cache.php drop-in is copied into place. |
| 61 |
* |
| 62 |
* @since 0.1.0 |
| 63 |
* |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
private function advanced_cache_dropin_init(): void { |
| 67 |
$this->container->get( Advanced_Cache::class )->generate(); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Ensure that the cache directory exists. |
| 72 |
* |
| 73 |
* @since 0.1.0 |
| 74 |
* |
| 75 |
* @return void |
| 76 |
*/ |
| 77 |
private function cache_directory_init(): void { |
| 78 |
$cache_dir = $this->container->get( Config::class )->get( 'page_cache.cache_dir' ); |
| 79 |
|
| 80 |
if ( ! is_dir( $cache_dir ) ) { |
| 81 |
wp_mkdir_p( $cache_dir ); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
/** |
| 87 |
* Set the WP_CACHE constant. |
| 88 |
* |
| 89 |
* @since 0.1.0 |
| 90 |
* |
| 91 |
* @return void |
| 92 |
*/ |
| 93 |
private function wp_cache_init(): void { |
| 94 |
$this->container->get( WP_Config::class )->set_wp_cache( 'true' ); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Saves a set of default values in the config. |
| 99 |
* |
| 100 |
* @since 0.1.0 |
| 101 |
* |
| 102 |
* @return void |
| 103 |
*/ |
| 104 |
private function write_settings(): void { |
| 105 |
// Save config to the database. |
| 106 |
$this->container->get( Config::class )->save(); |
| 107 |
|
| 108 |
// Force writing the config.php file again. |
| 109 |
$this->container->get( File_Writer::class )->save(); |
| 110 |
} |
| 111 |
} |
| 112 |
|