| 1 |
<?php |
| 2 |
/** |
| 3 |
* Texty migrations bootstrap. |
| 4 |
* |
| 5 |
* Wires wp-kit's MigrationManager and the migration REST controller, and |
| 6 |
* registers the migration NoticeProvider into the central notice bootstrap |
| 7 |
* (`Texty\Notices`). The "database update required" notice surfaces through |
| 8 |
* `/texty/v1/notices/admin` like any other notice — migrations don't own |
| 9 |
* the notice plumbing. |
| 10 |
* |
| 11 |
* Migrations are NOT auto-applied on boot — the user has to click "Update |
| 12 |
* database" on the admin notice, which POSTs to MigrationRESTController. |
| 13 |
* |
| 14 |
* @package Texty |
| 15 |
*/ |
| 16 |
|
| 17 |
namespace Texty; |
| 18 |
|
| 19 |
use Texty\Migrations\NoticeProvider; |
| 20 |
use Texty\Migrations\TextyMigration; |
| 21 |
use Texty\Migrations\V_2_0_0; |
| 22 |
use Texty\Dependencies\WeDevs\WPKit\Migration\MigrationHooks; |
| 23 |
use Texty\Dependencies\WeDevs\WPKit\Migration\MigrationManager; |
| 24 |
use Texty\Dependencies\WeDevs\WPKit\Migration\MigrationRegistry; |
| 25 |
use Texty\Dependencies\WeDevs\WPKit\Migration\MigrationRESTController; |
| 26 |
|
| 27 |
defined( 'ABSPATH' ) || exit; |
| 28 |
|
| 29 |
/** |
| 30 |
* Texty migrations bootstrap. |
| 31 |
*/ |
| 32 |
class Migrations { |
| 33 |
|
| 34 |
/** |
| 35 |
* Plugin prefix used for the migration log/lock options. |
| 36 |
*/ |
| 37 |
public const PREFIX = 'texty'; |
| 38 |
|
| 39 |
/** |
| 40 |
* REST API namespace. |
| 41 |
*/ |
| 42 |
public const REST_NAMESPACE = 'texty/v1'; |
| 43 |
|
| 44 |
/** |
| 45 |
* Migration manager. |
| 46 |
* |
| 47 |
* @var MigrationManager|null |
| 48 |
*/ |
| 49 |
protected ?MigrationManager $manager = null; |
| 50 |
|
| 51 |
/** |
| 52 |
* Constructor — registers the migration notice provider, the post-upgrade |
| 53 |
* hooks, and the REST routes. |
| 54 |
*/ |
| 55 |
public function __construct() { |
| 56 |
texty()->notices()->register_provider( new NoticeProvider() ); |
| 57 |
|
| 58 |
// wp-kit syncs db_version_to_current after a successful upgrade via |
| 59 |
// this hook subscription; without it the option may lag behind a |
| 60 |
// plugin-version bump that ships no new migrations. |
| 61 |
$hooks = new MigrationHooks( $this->get_manager(), self::PREFIX ); |
| 62 |
$hooks->register(); |
| 63 |
|
| 64 |
add_action( 'rest_api_init', [ $this, 'register_rest_routes' ] ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Lazily build the migration manager. |
| 69 |
* |
| 70 |
* @return MigrationManager |
| 71 |
*/ |
| 72 |
public function get_manager(): MigrationManager { |
| 73 |
if ( null === $this->manager ) { |
| 74 |
$registry = new MigrationRegistry( TextyMigration::get_db_version_key(), TEXTY_VERSION ); |
| 75 |
$registry->register_many( $this->get_migrations() ); |
| 76 |
|
| 77 |
$this->manager = new MigrationManager( $registry, self::PREFIX ); |
| 78 |
} |
| 79 |
|
| 80 |
return $this->manager; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Whether any migration is pending. |
| 85 |
* |
| 86 |
* @return bool |
| 87 |
*/ |
| 88 |
public function is_upgrade_required(): bool { |
| 89 |
return $this->get_manager()->is_upgrade_required(); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Register the wp-kit migration REST controller. |
| 94 |
* |
| 95 |
* @return void |
| 96 |
*/ |
| 97 |
public function register_rest_routes(): void { |
| 98 |
$migrations = new MigrationRESTController( $this->get_manager(), self::REST_NAMESPACE ); |
| 99 |
$migrations->register_routes(); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Map of version → migration class. Add new entries in ascending order |
| 104 |
* as the schema evolves; wp-kit sorts by version_compare before running. |
| 105 |
* |
| 106 |
* @return array<string, string> |
| 107 |
*/ |
| 108 |
protected function get_migrations(): array { |
| 109 |
return [ |
| 110 |
'2.0.0' => V_2_0_0::class, |
| 111 |
]; |
| 112 |
} |
| 113 |
} |
| 114 |
|