| 1 |
<?php |
| 2 |
|
| 3 |
class Meow_MGL_Migrations { |
| 4 |
private static $db_version = '2.9'; |
| 5 |
|
| 6 |
public function __construct( ) { |
| 7 |
|
| 8 |
} |
| 9 |
|
| 10 |
public static function check_db() { |
| 11 |
global $wpdb; |
| 12 |
|
| 13 |
$current_db_version = get_option( 'mgl_db_version', '0' ); |
| 14 |
$version_outdated = version_compare( $current_db_version, self::$db_version, '<' ); |
| 15 |
|
| 16 |
// Also check if tables actually exist |
| 17 |
$tables_exist = true; |
| 18 |
if ( !$version_outdated ) { |
| 19 |
$shortcodes_table = $wpdb->prefix . 'mgl_gallery_shortcodes'; |
| 20 |
$collections_table = $wpdb->prefix . 'mgl_collections'; |
| 21 |
$tables_exist = $wpdb->get_var( "SHOW TABLES LIKE '$shortcodes_table'" ) === $shortcodes_table |
| 22 |
&& $wpdb->get_var( "SHOW TABLES LIKE '$collections_table'" ) === $collections_table; |
| 23 |
} |
| 24 |
|
| 25 |
if ( $version_outdated || !$tables_exist ) { |
| 26 |
self::run_migrations( $current_db_version ); |
| 27 |
update_option( 'mgl_db_version', self::$db_version ); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
public function check_for_migrations() { |
| 32 |
self::check_db(); |
| 33 |
} |
| 34 |
|
| 35 |
private static function run_migrations( $current_version ) { |
| 36 |
global $wpdb; |
| 37 |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 38 |
|
| 39 |
// Create gallery shortcodes table |
| 40 |
$table_name = $wpdb->prefix . 'mgl_gallery_shortcodes'; |
| 41 |
$charset_collate = $wpdb->get_charset_collate(); |
| 42 |
|
| 43 |
$sql = "CREATE TABLE $table_name ( |
| 44 |
id varchar( 20 ) NOT NULL, |
| 45 |
name varchar( 255 ) NOT NULL, |
| 46 |
description text, |
| 47 |
layout varchar( 50 ) NOT NULL, |
| 48 |
medias longtext, |
| 49 |
lead_image_id varchar( 20 ) DEFAULT NULL, |
| 50 |
order_by varchar( 20 ) DEFAULT NULL, |
| 51 |
is_post_mode tinyint( 1 ) DEFAULT 0, |
| 52 |
is_hero_mode tinyint( 1 ) DEFAULT 0, |
| 53 |
posts longtext, |
| 54 |
latest_posts int( 11 ) DEFAULT NULL, |
| 55 |
tags longtext, |
| 56 |
dynamic_source varchar( 50 ) DEFAULT NULL, |
| 57 |
rml varchar( 255 ) DEFAULT NULL, |
| 58 |
pref_rank int( 11 ) DEFAULT 0, |
| 59 |
created_at datetime DEFAULT CURRENT_TIMESTAMP, |
| 60 |
updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, |
| 61 |
PRIMARY KEY ( id ) |
| 62 |
) $charset_collate;"; |
| 63 |
|
| 64 |
dbDelta( $sql ); |
| 65 |
|
| 66 |
// Create collections table |
| 67 |
$table_name = $wpdb->prefix . 'mgl_collections'; |
| 68 |
$sql = "CREATE TABLE $table_name ( |
| 69 |
id varchar( 20 ) NOT NULL, |
| 70 |
name varchar( 255 ) NOT NULL, |
| 71 |
description text, |
| 72 |
layout varchar( 50 ) NOT NULL, |
| 73 |
galleries_ids longtext NOT NULL, |
| 74 |
created_at datetime DEFAULT CURRENT_TIMESTAMP, |
| 75 |
updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, |
| 76 |
PRIMARY KEY ( id ) |
| 77 |
) $charset_collate;"; |
| 78 |
|
| 79 |
dbDelta( $sql ); |
| 80 |
|
| 81 |
|
| 82 |
// Migrate existing data from options to tables |
| 83 |
if ( version_compare( $current_version, '1.0', '<' ) ) { |
| 84 |
self::migrate_options_to_tables(); |
| 85 |
} |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
private static function migrate_options_to_tables() { |
| 90 |
global $wpdb; |
| 91 |
|
| 92 |
// Migrate shortcodes |
| 93 |
$shortcodes = get_option( 'mgl_shortcodes', array() ); |
| 94 |
$shortcodes_table = $wpdb->prefix . 'mgl_gallery_shortcodes'; |
| 95 |
|
| 96 |
foreach ( $shortcodes as $id => $shortcode ) { |
| 97 |
if ( empty( $id ) ) { |
| 98 |
continue; |
| 99 |
} |
| 100 |
|
| 101 |
$wpdb->insert( |
| 102 |
$shortcodes_table, |
| 103 |
array( |
| 104 |
'id' => $id, |
| 105 |
'name' => $shortcode['name'], |
| 106 |
'description' => $shortcode['description'] ?? '', |
| 107 |
'layout' => $shortcode['layout'], |
| 108 |
'medias' => serialize( Meow_MGL_Core::normalize_medias( $shortcode['medias'] ?? null ) ), |
| 109 |
'lead_image_id' => $shortcode['lead_image_id'] ?? null, |
| 110 |
'order_by' => $shortcode['order_by'] ?? null, |
| 111 |
'is_post_mode' => ( isset( $shortcode['is_post_mode'] ) && $shortcode['is_post_mode'] ) ? 1 : 0, |
| 112 |
'is_hero_mode' => isset( $shortcode['hero'] ) && $shortcode['hero'] ? 1 : 0, |
| 113 |
'posts' => isset( $shortcode['posts'] ) ? serialize( $shortcode['posts'] ) : null, |
| 114 |
'latest_posts' => $shortcode['latest_posts'] ?? null, |
| 115 |
'tags' => isset( $shortcode['tags'] ) ? serialize( $shortcode['tags'] ) : null, |
| 116 |
'dynamic_source' => $shortcode['dynamic_source'] ?? null, |
| 117 |
'updated_at' => date( 'Y-m-d H:i:s', $shortcode['updated'] ) |
| 118 |
) |
| 119 |
); |
| 120 |
} |
| 121 |
|
| 122 |
// Migrate collections |
| 123 |
$collections = get_option( 'mgl_collections', array() ); |
| 124 |
$collections_table = $wpdb->prefix . 'mgl_collections'; |
| 125 |
|
| 126 |
foreach ( $collections as $id => $collection ) { |
| 127 |
$wpdb->insert( |
| 128 |
$collections_table, |
| 129 |
array( |
| 130 |
'id' => $id, |
| 131 |
'name' => $collection['name'], |
| 132 |
'description' => $collection['description'] ?? '', |
| 133 |
'layout' => $collection['layout'], |
| 134 |
'galleries_ids' => serialize( $collection['galleries_ids'] ), |
| 135 |
'updated_at' => date( 'Y-m-d H:i:s', $collection['updated'] ) |
| 136 |
) |
| 137 |
); |
| 138 |
} |
| 139 |
} |
| 140 |
} |