CLI
1 year ago
Core
1 month ago
DemoSites
1 month ago
AssetsDependencyInjector.php
1 year ago
Config.php
1 year ago
FileLog.php
1 year ago
Flags.php
1 year ago
GoogleFontsLocalLoader.php
1 year ago
GutenbergControls.php
1 year ago
Migrations.php
1 year ago
NotificationsManager.php
1 month ago
PluginsManager.php
2 years ago
Migrations.php
107 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 | private static function parseMigration( $migration ) { |
| 35 | preg_match( '#(\d+?)-(.*$)#', $migration, $matches ); |
| 36 | |
| 37 | if ( count( $matches ) === 3 ) { |
| 38 | return array( |
| 39 | 'slug' => $migration, |
| 40 | 'callback' => $matches[2], |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | return null; |
| 45 | } |
| 46 | public static function loadMissingMigrations() { |
| 47 | |
| 48 | $is_actived = Flags::get( 'kubio_activation_time' ) || Flags::get( 'kubio_pro_activation_time' ); |
| 49 | if ( ! $is_actived ) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | if ( defined( 'REST_REQUEST ' ) && REST_REQUEST ) { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | Migrations::executeMigrations(); |
| 62 | } |
| 63 | |
| 64 | private static function executeMigrations() { |
| 65 | $available_migrations = Migrations::getMigrations(); |
| 66 | $executed_migrations = kubio_get_global_data( 'migrations', array() ); |
| 67 | |
| 68 | $callbacks = array(); |
| 69 | |
| 70 | foreach ( $available_migrations as $migration ) { |
| 71 | $slug = $migration['slug']; |
| 72 | $callback = $migration['callback']; |
| 73 | |
| 74 | if ( ! isset( $executed_migrations[ $slug ] ) ) { |
| 75 | require_once KUBIO_ROOT_DIR . "/migrations/{$slug}.php"; |
| 76 | if ( ! function_exists( $callback ) ) { |
| 77 | if ( CoreUtils::isDebug() ) { |
| 78 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 79 | wp_die( "Migrations functon kubio_{$callback} does not exists" ); |
| 80 | } |
| 81 | return; // leave migration process |
| 82 | } |
| 83 | $callbacks[ $slug ] = $callback; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | foreach ( $callbacks as $slug => $callback ) { |
| 88 | try { |
| 89 | call_user_func( $callback ); |
| 90 | } catch ( \Exception $e ) { |
| 91 | if ( CoreUtils::isDebug() ) { |
| 92 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 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 | public static function load() { |
| 104 | add_action( 'admin_init', array( Migrations::class, 'loadMissingMigrations' ) ); |
| 105 | } |
| 106 | } |
| 107 |