| 1 |
<?php |
| 2 |
/** |
| 3 |
* The Service Provider to register updater tasks in the container. |
| 4 |
* |
| 5 |
* This provider is booted early, when advanced-cache.php is. |
| 6 |
* |
| 7 |
* @package SolidWP\Performance |
| 8 |
*/ |
| 9 |
|
| 10 |
declare( strict_types=1 ); |
| 11 |
|
| 12 |
namespace SolidWP\Performance\Update; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
use RuntimeException; |
| 19 |
use SolidWP\Performance\Contracts\Service_Provider; |
| 20 |
use SolidWP\Performance\Core; |
| 21 |
use SolidWP\Performance\Flintstone\Flintstone; |
| 22 |
use SolidWP\Performance\Update\Tasks\Factories\Non_Essential_Task_Factory; |
| 23 |
use SolidWP\Performance\Update\Tasks\Factories\Post_Bootstrap_Task_Factory; |
| 24 |
use SolidWP\Performance\Update\Tasks\Factories\Pre_Bootstrap_Task_Factory; |
| 25 |
use SolidWP\Performance\Update\Tasks\Post_Bootstrap\Enable_Scheduled_Tasks; |
| 26 |
use SolidWP\Performance\Update\Tasks\Post_Bootstrap\Advanced_Cache_Restorer; |
| 27 |
use SolidWP\Performance\Update\Tasks\Once\Clean_Up_Old_Cache_Dir; |
| 28 |
use SolidWP\Performance\Update\Tasks\Post_Bootstrap\Update_Htaccess_Rules; |
| 29 |
use SolidWP\Performance\Update\Tasks\Pre_Bootstrap\Advanced_Cache_Remover; |
| 30 |
|
| 31 |
/** |
| 32 |
* The Service Provider to register updater tasks in the container. |
| 33 |
* |
| 34 |
* This provider is booted early, when advanced-cache.php is. |
| 35 |
* |
| 36 |
* @package SolidWP\Performance |
| 37 |
*/ |
| 38 |
final class Provider extends Service_Provider { |
| 39 |
|
| 40 |
public const DB = 'solid_performance.update_db'; |
| 41 |
|
| 42 |
/** |
| 43 |
* @inheritDoc |
| 44 |
*/ |
| 45 |
public function register(): void { |
| 46 |
$this->register_key_value_store(); |
| 47 |
$this->register_pre_bootstrap_updater_tasks(); |
| 48 |
$this->register_post_bootstrap_updater_tasks(); |
| 49 |
$this->register_non_essential_updater_tasks(); |
| 50 |
$this->register_updater(); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Flintstone is a simple file based key/value store that tasks can use |
| 55 |
* to read and write to, so that a pre bootstrap task can signal to a post |
| 56 |
* bootstrap task that it should perform some action. |
| 57 |
* |
| 58 |
* DO NOT store sensitive information in this store. |
| 59 |
* |
| 60 |
* @throws RuntimeException If we can't find a writable directory. |
| 61 |
* |
| 62 |
* @return void |
| 63 |
*/ |
| 64 |
private function register_key_value_store(): void { |
| 65 |
$this->container->singleton( |
| 66 |
Flintstone::class, |
| 67 |
static function (): Flintstone { |
| 68 |
// DB path is: wp-content/solid_performance_updates.dat. |
| 69 |
// List of directories to try to write to, in order. |
| 70 |
return new Flintstone( |
| 71 |
'solid_performance_updates', |
| 72 |
[ |
| 73 |
'dir' => rtrim( WP_CONTENT_DIR, '/\\' ) . '/', |
| 74 |
] |
| 75 |
); |
| 76 |
} |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Register tasks that run before WordPress is fully bootstrapped. |
| 82 |
* |
| 83 |
* Use caution, as not all WordPress functions are available and the database isn't initialized |
| 84 |
* yet. |
| 85 |
* |
| 86 |
* @return void |
| 87 |
*/ |
| 88 |
private function register_pre_bootstrap_updater_tasks(): void { |
| 89 |
$this->container->when( Advanced_Cache_Remover::class ) |
| 90 |
->needs( '$version' ) |
| 91 |
->give( static fn( $c ): string => $c->get( Core::PLUGIN_VERSION ) ); |
| 92 |
|
| 93 |
$this->container->when( Advanced_Cache_Remover::class ) |
| 94 |
->needs( '$destination' ) |
| 95 |
->give( static fn( $c ): string => $c->get( Core::ADVANCED_CACHE_PATH ) ); |
| 96 |
|
| 97 |
$this->container->when( Pre_Bootstrap_Task_Factory::class ) |
| 98 |
->needs( '$tasks' ) |
| 99 |
->give( |
| 100 |
static fn(): array => [ |
| 101 |
// Add tasks that will run before WordPress is fully bootstrapped. |
| 102 |
// WARNING: Be careful with which WP functions you use, they may not be available. |
| 103 |
Advanced_Cache_Remover::class, |
| 104 |
] |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Register tasks that run once WordPress is fully bootstrapped. |
| 110 |
* |
| 111 |
* @return void |
| 112 |
*/ |
| 113 |
private function register_post_bootstrap_updater_tasks(): void { |
| 114 |
$this->container->when( Post_Bootstrap_Task_Factory::class ) |
| 115 |
->needs( '$tasks' ) |
| 116 |
->give( |
| 117 |
static fn(): array => [ |
| 118 |
// Add tasks that will run after WordPress is fully bootstrapped. |
| 119 |
Advanced_Cache_Restorer::class, |
| 120 |
Update_Htaccess_Rules::class, |
| 121 |
Enable_Scheduled_Tasks::class, |
| 122 |
] |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Register tasks that aren't required for the plugin to function. |
| 128 |
* |
| 129 |
* These run on the run on the `upgrader_process_complete` hook. |
| 130 |
* |
| 131 |
* @return void |
| 132 |
*/ |
| 133 |
private function register_non_essential_updater_tasks(): void { |
| 134 |
$this->container->when( Non_Essential_Task_Factory::class ) |
| 135 |
->needs( '$tasks' ) |
| 136 |
->give( |
| 137 |
static fn(): array => [ |
| 138 |
// Add tasks that will run on the `upgrader_process_complete` hook. |
| 139 |
Clean_Up_Old_Cache_Dir::class, |
| 140 |
] |
| 141 |
); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Register the updater and the tasks that will run be pre/post boostrap. |
| 146 |
* |
| 147 |
* @return void |
| 148 |
*/ |
| 149 |
private function register_updater(): void { |
| 150 |
$this->container->when( Updater::class ) |
| 151 |
->needs( '$plugin_file' ) |
| 152 |
->give( static fn( $c ) => $c->get( Core::PLUGIN_FILE ) ); |
| 153 |
|
| 154 |
// Ensure singleton for terminable shutdown task. |
| 155 |
$this->container->singleton( Updater::class, Updater::class ); |
| 156 |
|
| 157 |
/* |
| 158 |
* Attempt to run any pre bootstrap tasks BEFORE WordPress is fully bootstrapped. |
| 159 |
* |
| 160 |
* WARNING: This runs BEFORE WordPress is fully bootstrapped, be careful |
| 161 |
* with the tasks you assign above, they must not rely on any WordPress functionality |
| 162 |
* that doesn't exist when advanced-cache.php is loaded. |
| 163 |
*/ |
| 164 |
$this->container->get( Updater::class )->run_pre_bootstrap_tasks(); |
| 165 |
|
| 166 |
// Attempt to run any post bootstrap tasks, AFTER WordPress is fully bootstrapped. |
| 167 |
add_action( 'init', $this->container->callback( Updater::class, 'run_post_bootstrap_tasks' ), 1 ); |
| 168 |
|
| 169 |
// Run non-essential tasks when WordPress fires the upgrader_process_complete action for our plugin. |
| 170 |
add_action( |
| 171 |
'upgrader_process_complete', |
| 172 |
$this->container->callback( Updater::class, 'run_non_essential_tasks' ), |
| 173 |
10, |
| 174 |
2 |
| 175 |
); |
| 176 |
} |
| 177 |
} |
| 178 |
|