| 1 |
<?php |
| 2 |
/** |
| 3 |
* The Service Provider for Terminal Shutdown tasks. |
| 4 |
* |
| 5 |
* @package SolidWP\Performance |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types=1 ); |
| 9 |
|
| 10 |
namespace SolidWP\Performance\Shutdown; |
| 11 |
|
| 12 |
use SolidWP\Performance\Config\Config; |
| 13 |
use SolidWP\Performance\Contracts\Service_Provider; |
| 14 |
use SolidWP\Performance\Page_Cache\Purge\Batch\Batch_Purger; |
| 15 |
use SolidWP\Performance\Shutdown\Rules\Query_Monitor_Rule; |
| 16 |
use SolidWP\Performance\Update\Updater; |
| 17 |
|
| 18 |
if ( ! defined( 'ABSPATH' ) ) { |
| 19 |
exit; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* The Service Provider for Terminal Shutdown tasks. |
| 24 |
* |
| 25 |
* @package SolidWP\Performance |
| 26 |
*/ |
| 27 |
final class Provider extends Service_Provider { |
| 28 |
|
| 29 |
/** |
| 30 |
* @inheritDoc |
| 31 |
*/ |
| 32 |
public function register(): void { |
| 33 |
$this->register_priority_rules(); |
| 34 |
|
| 35 |
$this->container->singleton( |
| 36 |
Shutdown_Handler::class, |
| 37 |
fn() => new Shutdown_Handler( |
| 38 |
// Add any terminable tasks to the collection to run on shutdown. |
| 39 |
// Important: these will run in the order provided. |
| 40 |
$this->container->get( Config::class ), |
| 41 |
$this->container->get( Updater::class ), |
| 42 |
$this->container->get( Batch_Purger::class ), |
| 43 |
) |
| 44 |
); |
| 45 |
|
| 46 |
// Create an action that can be called manually. |
| 47 |
add_action( 'solidwp/performance/terminate', $this->container->callback( Shutdown_Handler::class, 'handle' ) ); |
| 48 |
|
| 49 |
// Run early to get ahead of any potentially bad code that could kill execution. |
| 50 |
add_action( |
| 51 |
'shutdown', |
| 52 |
static function (): void { |
| 53 |
do_action( 'solidwp/performance/terminate' ); |
| 54 |
}, |
| 55 |
$this->container->get( Priority_Selector::class )->get_priority() |
| 56 |
); |
| 57 |
|
| 58 |
// Run this again in case any other code was updated in the shutdown action that could trigger our Terminable tasks. |
| 59 |
add_action( |
| 60 |
'shutdown', |
| 61 |
static function (): void { |
| 62 |
do_action( 'solidwp/performance/terminate' ); |
| 63 |
}, |
| 64 |
9999 |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Register priority rules to determine the initial shutdown hook priority depending on which |
| 70 |
* other plugins are active. |
| 71 |
* |
| 72 |
* @return void |
| 73 |
*/ |
| 74 |
private function register_priority_rules(): void { |
| 75 |
$this->container->when( Priority_Selector::class ) |
| 76 |
->needs( '$rules' ) |
| 77 |
->give( |
| 78 |
fn(): array => [ |
| 79 |
$this->container->get( Query_Monitor_Rule::class ), |
| 80 |
] |
| 81 |
); |
| 82 |
} |
| 83 |
} |
| 84 |
|