| 1 |
<?php |
| 2 |
/** |
| 3 |
* Runs pre/post bootstrap tasks when the plugin is updated. |
| 4 |
* |
| 5 |
* @package SolidWP\Performance |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace SolidWP\Performance\Update; |
| 11 |
|
| 12 |
use SolidWP\Performance\Flintstone\Flintstone; |
| 13 |
use SolidWP\Performance\Shutdown\Contracts\Terminable; |
| 14 |
use SolidWP\Performance\Update\Tasks\Contracts\Task; |
| 15 |
|
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
exit; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Runs pre/post bootstrap tasks when the plugin is updated. |
| 22 |
* |
| 23 |
* @package SolidWP\Performance |
| 24 |
*/ |
| 25 |
final class Updater implements Terminable { |
| 26 |
|
| 27 |
/** |
| 28 |
* Tasks that may run before WordPress is bootstrapped. |
| 29 |
* |
| 30 |
* @var Task[] |
| 31 |
*/ |
| 32 |
private array $pre_bootstrap_tasks; |
| 33 |
|
| 34 |
/** |
| 35 |
* Task that may run after WordPress is fully bootstrapped. |
| 36 |
* |
| 37 |
* @var Task[] |
| 38 |
*/ |
| 39 |
private array $post_bootstrap_tasks; |
| 40 |
|
| 41 |
/** |
| 42 |
* The flat file key/value store db. |
| 43 |
* |
| 44 |
* @var Flintstone |
| 45 |
*/ |
| 46 |
private Flintstone $db; |
| 47 |
|
| 48 |
/** |
| 49 |
* Whether we should flush the key/value store db. |
| 50 |
* |
| 51 |
* @var bool |
| 52 |
*/ |
| 53 |
private bool $should_flush = false; |
| 54 |
|
| 55 |
/** |
| 56 |
* @param Task[] $pre_bootstrap_tasks Tasks that may run before WordPress is bootstrapped. |
| 57 |
* @param Task[] $post_bootstrap_tasks Task that may run after WordPress is fully bootstrapped. |
| 58 |
* @param Flintstone $db The flat file key/value store db. |
| 59 |
*/ |
| 60 |
public function __construct( array $pre_bootstrap_tasks, array $post_bootstrap_tasks, Flintstone $db ) { |
| 61 |
$this->pre_bootstrap_tasks = $pre_bootstrap_tasks; |
| 62 |
$this->post_bootstrap_tasks = $post_bootstrap_tasks; |
| 63 |
$this->db = $db; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Attempt to execute any pre bootstrap tasks. |
| 68 |
* |
| 69 |
* WARNING: This runs BEFORE WordPress is fully bootstrapped, so not all WP functions are available. |
| 70 |
* |
| 71 |
* @return void |
| 72 |
*/ |
| 73 |
public function run_pre_bootstrap_tasks(): void { |
| 74 |
foreach ( $this->pre_bootstrap_tasks as $task ) { |
| 75 |
if ( ! $task->should_run() ) { |
| 76 |
continue; |
| 77 |
} |
| 78 |
|
| 79 |
$task->run(); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Attempt to execute any post bootstrap tasks. |
| 85 |
* |
| 86 |
* @action plugins_loaded |
| 87 |
* |
| 88 |
* @return void |
| 89 |
*/ |
| 90 |
public function run_post_bootstrap_tasks(): void { |
| 91 |
foreach ( $this->post_bootstrap_tasks as $task ) { |
| 92 |
if ( ! $task->should_run() ) { |
| 93 |
continue; |
| 94 |
} |
| 95 |
|
| 96 |
$this->should_flush = true; |
| 97 |
$task->run(); |
| 98 |
} |
| 99 |
} |
| 100 |
/** |
| 101 |
* Clear the key/value store if any of the post bootstrap tasks ran. |
| 102 |
* |
| 103 |
* @action shutdown |
| 104 |
* @action solidwp/performance/terminate |
| 105 |
* |
| 106 |
* @return void |
| 107 |
*/ |
| 108 |
public function terminate(): void { |
| 109 |
if ( ! $this->should_flush ) { |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
// Terminable shutdown runs twice, we only need this once. |
| 114 |
$this->should_flush = false; |
| 115 |
|
| 116 |
$this->db->flush(); |
| 117 |
} |
| 118 |
} |
| 119 |
|