| 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\Post_Bootstrap\Advanced_Cache_Restorer; |
| 23 |
use SolidWP\Performance\Update\Tasks\Pre_Bootstrap\Advanced_Cache_Remover; |
| 24 |
|
| 25 |
/** |
| 26 |
* The Service Provider to register updater tasks in the container. |
| 27 |
* |
| 28 |
* This provider is booted early, when advanced-cache.php is. |
| 29 |
* |
| 30 |
* @package SolidWP\Performance |
| 31 |
*/ |
| 32 |
final class Provider extends Service_Provider { |
| 33 |
|
| 34 |
public const DB = 'solid_performance.update_db'; |
| 35 |
|
| 36 |
/** |
| 37 |
* @inheritDoc |
| 38 |
*/ |
| 39 |
public function register(): void { |
| 40 |
$this->register_key_value_store(); |
| 41 |
$this->register_updater(); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Flintstone is a simple file based key/value store that tasks can use |
| 46 |
* to read and write to, so that a pre bootstrap task can signal to a post |
| 47 |
* bootstrap task that it should perform some action. |
| 48 |
* |
| 49 |
* DO NOT store sensitive information in this store. |
| 50 |
* |
| 51 |
* @throws RuntimeException If we can't find a writable directory. |
| 52 |
* |
| 53 |
* @return void |
| 54 |
*/ |
| 55 |
private function register_key_value_store(): void { |
| 56 |
$this->container->singleton( |
| 57 |
Flintstone::class, |
| 58 |
static function (): Flintstone { |
| 59 |
// DB path is: wp-content/solid_performance_updates.dat. |
| 60 |
// List of directories to try to write to, in order. |
| 61 |
return new Flintstone( |
| 62 |
'solid_performance_updates', |
| 63 |
[ |
| 64 |
'dir' => rtrim( WP_CONTENT_DIR, '/\\' ) . '/', |
| 65 |
] |
| 66 |
); |
| 67 |
} |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Register the updater and the tasks that will run be pre/post boostrap. |
| 73 |
* |
| 74 |
* @return void |
| 75 |
*/ |
| 76 |
private function register_updater(): void { |
| 77 |
$this->container->when( Advanced_Cache_Remover::class ) |
| 78 |
->needs( '$version' ) |
| 79 |
->give( static fn( $c ): string => $c->get( Core::PLUGIN_VERSION ) ); |
| 80 |
|
| 81 |
$this->container->when( Advanced_Cache_Remover::class ) |
| 82 |
->needs( '$destination' ) |
| 83 |
->give( static fn( $c ): string => $c->get( Core::ADVANCED_CACHE_PATH ) ); |
| 84 |
|
| 85 |
$this->container->when( Updater::class ) |
| 86 |
->needs( '$pre_bootstrap_tasks' ) |
| 87 |
->give( |
| 88 |
static fn( $c ): array => [ |
| 89 |
// Add tasks that will run before WordPress is fully bootstrapped. |
| 90 |
// WARNING: Be careful with which WP functions you use, they may not be available. |
| 91 |
$c->get( Advanced_Cache_Remover::class ), |
| 92 |
] |
| 93 |
); |
| 94 |
|
| 95 |
$this->container->when( Updater::class ) |
| 96 |
->needs( '$post_bootstrap_tasks' ) |
| 97 |
->give( |
| 98 |
static fn( $c ): array => [ |
| 99 |
// Add tasks that will run after WordPress is fully bootstrapped. |
| 100 |
$c->get( Advanced_Cache_Restorer::class ), |
| 101 |
] |
| 102 |
); |
| 103 |
|
| 104 |
// Ensure singleton for terminable shutdown task. |
| 105 |
$this->container->singleton( Updater::class, Updater::class ); |
| 106 |
|
| 107 |
/* |
| 108 |
* Attempt to run any pre bootstrap tasks BEFORE WordPress is fully bootstrapped. |
| 109 |
* |
| 110 |
* WARNING: This runs BEFORE WordPress is fully bootstrapped, be careful |
| 111 |
* with the tasks you assign above, they must not rely on any WordPress functionality |
| 112 |
* that doesn't exist when advanced-cache.php is loaded. |
| 113 |
*/ |
| 114 |
$this->container->get( Updater::class )->run_pre_bootstrap_tasks(); |
| 115 |
|
| 116 |
// Attempt to run any post bootstrap tasks, AFTER WordPress is fully bootstrapped. |
| 117 |
add_action( 'plugins_loaded', $this->container->callback( Updater::class, 'run_post_bootstrap_tasks' ) ); |
| 118 |
} |
| 119 |
} |
| 120 |
|