| 1 |
<?php |
| 2 |
namespace Elementor\Core\Database; |
| 3 |
|
| 4 |
use Elementor\Core\Utils\Collection; |
| 5 |
|
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; // Exit if accessed directly. |
| 8 |
} |
| 9 |
|
| 10 |
abstract class Base_Database_Updater { |
| 11 |
public function up( $force = false ) { |
| 12 |
$installed_version = $this->get_installed_version(); |
| 13 |
|
| 14 |
if ( ! $force && $this->get_db_version() <= $installed_version ) { |
| 15 |
return; |
| 16 |
} |
| 17 |
|
| 18 |
$migrations = new Collection( $this->get_migrations() ); |
| 19 |
|
| 20 |
if ( ! $force ) { |
| 21 |
$migrations = $migrations->filter( function ( $_, $version ) use ( $installed_version ) { |
| 22 |
return $version > $installed_version; |
| 23 |
} ); |
| 24 |
} |
| 25 |
|
| 26 |
$migrations->map( function ( Base_Migration $migration, $version ) { |
| 27 |
$migration->up(); |
| 28 |
|
| 29 |
$this->update_db_version_option( $version ); |
| 30 |
} ); |
| 31 |
|
| 32 |
$this->update_db_version_option( $this->get_db_version() ); |
| 33 |
} |
| 34 |
|
| 35 |
public function register() { |
| 36 |
add_action( 'admin_init', function () { |
| 37 |
$this->up(); |
| 38 |
} ); |
| 39 |
} |
| 40 |
|
| 41 |
protected function update_db_version_option( $version ) { |
| 42 |
update_option( $this->get_db_version_option_name(), $version ); |
| 43 |
} |
| 44 |
|
| 45 |
protected function get_installed_version() { |
| 46 |
return intval( get_option( $this->get_db_version_option_name() ) ); |
| 47 |
} |
| 48 |
|
| 49 |
abstract protected function get_db_version(); |
| 50 |
|
| 51 |
abstract protected function get_db_version_option_name(): string; |
| 52 |
|
| 53 |
abstract protected function get_migrations(): array; |
| 54 |
} |
| 55 |
|