CLI
2 years ago
Core
2 years ago
DemoSites
2 years ago
AssetsDependencyInjector.php
3 years ago
Config.php
3 years ago
FileLog.php
2 years ago
Flags.php
2 years ago
GoogleFontsLocalLoader.php
2 years ago
Migrations.php
4 years ago
NotificationsManager.php
2 years ago
PluginsManager.php
2 years ago
Migrations.php
108 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio; |
| 4 | |
| 5 | use Kubio\Core\Utils as CoreUtils; |
| 6 | |
| 7 | /** |
| 8 | * |
| 9 | * Kubio migration purpose is to apply certain changes to existing sites besides new ones. |
| 10 | * The migrations that are available inside the /migrations folder in kubio plugin have the following nameing scheme: {index}-{callback}.php |
| 11 | * The {index} is to ensure the migration execution order. |
| 12 | * The {callback} is the function that will be called to execute the migration. |
| 13 | * |
| 14 | */ |
| 15 | class Migrations { |
| 16 | |
| 17 | |
| 18 | private static function getMigrations() { |
| 19 | $files = glob( KUBIO_ROOT_DIR . '/migrations/*.php' ); |
| 20 | |
| 21 | $migrations = array(); |
| 22 | foreach ( $files as $file ) { |
| 23 | $migration = preg_replace( '#(.*)/migrations/(.*).php#', '$2', wp_normalize_path( $file ) ); |
| 24 | $migration = Migrations::parseMigration( $migration ); |
| 25 | |
| 26 | if ( $migration ) { |
| 27 | $migrations[] = $migration; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | return apply_filters( 'kubio/available_migrations', $migrations ); |
| 32 | |
| 33 | } |
| 34 | |
| 35 | private static function parseMigration( $migration ) { |
| 36 | preg_match( '#(\d+?)-(.*$)#', $migration, $matches ); |
| 37 | |
| 38 | if ( count( $matches ) === 3 ) { |
| 39 | return array( |
| 40 | 'slug' => $migration, |
| 41 | 'callback' => $matches[2], |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | return null; |
| 46 | |
| 47 | } |
| 48 | public static function loadMissingMigrations() { |
| 49 | |
| 50 | $is_actived = Flags::get( 'kubio_activation_time' ) || Flags::get( 'kubio_pro_activation_time' ); |
| 51 | if ( ! $is_actived ) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | if ( defined( 'REST_REQUEST ' ) && REST_REQUEST ) { |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | Migrations::executeMigrations(); |
| 64 | } |
| 65 | |
| 66 | private static function executeMigrations() { |
| 67 | $available_migrations = Migrations::getMigrations(); |
| 68 | $executed_migrations = kubio_get_global_data( 'migrations', array() ); |
| 69 | |
| 70 | $callbacks = array(); |
| 71 | |
| 72 | foreach ( $available_migrations as $migration ) { |
| 73 | $slug = $migration['slug']; |
| 74 | $callback = $migration['callback']; |
| 75 | |
| 76 | if ( ! isset( $executed_migrations[ $slug ] ) ) { |
| 77 | require_once KUBIO_ROOT_DIR . "/migrations/{$slug}.php"; |
| 78 | if ( ! function_exists( $callback ) ) { |
| 79 | if ( CoreUtils::isDebug() ) { |
| 80 | wp_die( "Migrations functon kubio_{$callback} does not exists" ); |
| 81 | } |
| 82 | return; // leave migration process |
| 83 | } |
| 84 | $callbacks[ $slug ] = $callback; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | foreach ( $callbacks as $slug => $callback ) { |
| 89 | try { |
| 90 | call_user_func( $callback ); |
| 91 | } catch ( \Exception $e ) { |
| 92 | if ( CoreUtils::isDebug() ) { |
| 93 | wp_die( "Migrations {$callback} error" ); |
| 94 | } |
| 95 | } |
| 96 | $executed_migrations [ $slug ] = true; |
| 97 | } |
| 98 | |
| 99 | kubio_set_global_data( 'migrations', $executed_migrations ); |
| 100 | |
| 101 | } |
| 102 | |
| 103 | |
| 104 | public static function load() { |
| 105 | add_action( 'admin_init', array( Migrations::class, 'loadMissingMigrations' ) ); |
| 106 | } |
| 107 | } |
| 108 |