| 1 |
<?php |
| 2 |
/** |
| 3 |
* Migration Service Provider |
| 4 |
* |
| 5 |
* @package Forge12\DoubleOptIn\Providers |
| 6 |
* @since 4.3.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace Forge12\DoubleOptIn\Providers; |
| 10 |
|
| 11 |
use Forge12\DoubleOptIn\Container\BootableProviderInterface; |
| 12 |
use Forge12\DoubleOptIn\Container\Container; |
| 13 |
use Forge12\DoubleOptIn\Migration\MigrationFormCompletenessSweep; |
| 14 |
use Forge12\DoubleOptIn\Migration\MigrationRegistry; |
| 15 |
use Forge12\Shared\LoggerInterface; |
| 16 |
|
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
exit; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Class MigrationServiceProvider |
| 23 |
* |
| 24 |
* Registers the MigrationRegistry in the container and schedules the |
| 25 |
* pending-migration runner on `admin_init` priority 20 (after |
| 26 |
* `f12_cf7_doubleoptin_register_addons` fires at plugins_loaded:20, |
| 27 |
* giving addons their chance to register migrations before they run). |
| 28 |
* |
| 29 |
* Running only on `admin_init` keeps DDL out of the frontend request |
| 30 |
* cycle: migrations apply when a site admin visits the admin, which is |
| 31 |
* the same trigger WordPress core uses for database upgrades. |
| 32 |
*/ |
| 33 |
class MigrationServiceProvider implements BootableProviderInterface { |
| 34 |
|
| 35 |
/** |
| 36 |
* {@inheritdoc} |
| 37 |
*/ |
| 38 |
public function register( Container $container ): void { |
| 39 |
$container->singleton( |
| 40 |
MigrationRegistry::class, |
| 41 |
function () use ( $container ) { |
| 42 |
$registry = MigrationRegistry::getInstance(); |
| 43 |
|
| 44 |
if ( $container->has( LoggerInterface::class ) ) { |
| 45 |
$registry->setLogger( $container->get( LoggerInterface::class ) ); |
| 46 |
} |
| 47 |
|
| 48 |
return $registry; |
| 49 |
} |
| 50 |
); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* {@inheritdoc} |
| 55 |
*/ |
| 56 |
public function boot( Container $container ): void { |
| 57 |
// Register core migrations. Addons register their own migrations |
| 58 |
// in their own boot() methods — see MigrationInterface docblock. |
| 59 |
$registry = $container->get( MigrationRegistry::class ); |
| 60 |
$registry->register( new MigrationFormCompletenessSweep() ); |
| 61 |
|
| 62 |
add_action( |
| 63 |
'admin_init', |
| 64 |
function () use ( $container ) { |
| 65 |
$registry = $container->get( MigrationRegistry::class ); |
| 66 |
$registry->runPending(); |
| 67 |
}, |
| 68 |
20 |
| 69 |
); |
| 70 |
} |
| 71 |
} |
| 72 |
|