| 1 |
<?php |
| 2 |
|
| 3 |
namespace SureCart\Database; |
| 4 |
|
| 5 |
use SureCartCore\ServiceProviders\ServiceProviderInterface; |
| 6 |
|
| 7 |
/** |
| 8 |
* This service provider runs on every single update. |
| 9 |
*/ |
| 10 |
class UpdateMigrationServiceProvider implements ServiceProviderInterface { |
| 11 |
/** |
| 12 |
* {@inheritDoc} |
| 13 |
* |
| 14 |
* @param \Pimple\Container $container Service Container. |
| 15 |
*/ |
| 16 |
public function register( $container ) { |
| 17 |
// nothing to register. |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* {@inheritDoc} |
| 22 |
* |
| 23 |
* @param \Pimple\Container $container Service Container. |
| 24 |
*/ |
| 25 |
public function bootstrap( $container ) { |
| 26 |
// only run the migration if the version changes. |
| 27 |
add_action( 'admin_init', [ $this, 'run' ] ); |
| 28 |
// update the migration version on admin_init lower priority, after all migrations have run. |
| 29 |
add_action( 'admin_init', [ $this, 'updateMigrationVersion' ], 9999999 ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Run the migration. |
| 34 |
*/ |
| 35 |
public function run() { |
| 36 |
if ( ! $this->versionChanged() ) { |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
// flush roles on every update. |
| 41 |
\SureCart::plugin()->roles()->create(); |
| 42 |
// make sure to check for and create cart post on every update. |
| 43 |
\SureCart::page_seeder()->createCartPost(); |
| 44 |
// make sure to check for and create cart post on every update. |
| 45 |
\SureCart::page_seeder()->createShopPage(); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Update the migration version. |
| 50 |
* |
| 51 |
* @return void |
| 52 |
*/ |
| 53 |
public function updateMigrationVersion() { |
| 54 |
if ( ! $this->versionChanged() ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
update_option( 'surecart_migration_version', \SureCart::plugin()->version() ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Version has changed? |
| 62 |
* |
| 63 |
* @return boolean |
| 64 |
*/ |
| 65 |
public function versionChanged() { |
| 66 |
return version_compare( \SureCart::plugin()->version(), get_option( 'surecart_migration_version', '0.0.0' ), '!=' ); |
| 67 |
} |
| 68 |
} |
| 69 |
|