Form
1 week ago
Migrations
1 week ago
Option
1 week ago
Route
1 week ago
Tab
1 week ago
Forms.php
1 week ago
Menu.php
1 week ago
MigrationsManager.php
1 week ago
Page.php
1 week ago
Routes.php
1 week ago
Tabs.php
1 week ago
MigrationsManager.php
61 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPDesk\FCF\Free\Settings; |
| 4 | |
| 5 | use WPDesk\FCF\Free\Settings\Migrations\Migration; |
| 6 | use FcfVendor\WPDesk\PluginBuilder\Plugin\Hookable; |
| 7 | use WPDesk\FCF\Free\Settings\Migrations\Migration320; |
| 8 | use WPDesk\FCF\Free\Settings\Migrations\Migration400; |
| 9 | |
| 10 | /** |
| 11 | * Manage migration of plugin settings after plugin update. |
| 12 | */ |
| 13 | class MigrationsManager implements Hookable { |
| 14 | |
| 15 | const PLUGIN_MIGRATION_OPTION_KEY = 'fcf_migration_version'; |
| 16 | |
| 17 | /** |
| 18 | * @var string |
| 19 | */ |
| 20 | private $plugin_version; |
| 21 | |
| 22 | /** |
| 23 | * @var Migration[] |
| 24 | */ |
| 25 | private $migrations = []; |
| 26 | |
| 27 | public function __construct( string $plugin_version ) { |
| 28 | $this->plugin_version = $plugin_version; |
| 29 | |
| 30 | $this->migrations[] = new Migration320(); |
| 31 | $this->migrations[] = new Migration400(); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * {@inheritdoc} |
| 36 | */ |
| 37 | public function hooks() { |
| 38 | add_action( 'init', [ $this, 'make_migrations' ] ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * @return void |
| 43 | * |
| 44 | * @internal |
| 45 | */ |
| 46 | public function make_migrations() { |
| 47 | $current_migration = get_option( self::PLUGIN_MIGRATION_OPTION_KEY, '1.0.0' ); |
| 48 | if ( $current_migration === $this->plugin_version ) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | foreach ( $this->migrations as $migration ) { |
| 53 | if ( $migration->get_version() > $current_migration ) { |
| 54 | $migration->up(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | update_option( self::PLUGIN_MIGRATION_OPTION_KEY, $this->plugin_version ); |
| 59 | } |
| 60 | } |
| 61 |