PluginProbe
Meow Gallery / 5.4.9
Meow Gallery v5.4.9
5.5.5 5.5.4 5.5.3 5.5.2 5.5.1 5.5.0 5.4.9 5.4.8 5.4.7 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.3.0 All 157 releases
meow-gallery / classes / migrations.php

migrations.php in Meow Gallery 5.4.9, at classes/migrations.php

141 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Meow_MGL_Migrations {
4 private static $db_version = '2.8';
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 pref_rank int( 11 ) DEFAULT 0,
58 created_at datetime DEFAULT CURRENT_TIMESTAMP,
59 updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
60 PRIMARY KEY ( id )
61 ) $charset_collate;";
62
63 dbDelta( $sql );
64
65 // Create collections table
66 $table_name = $wpdb->prefix . 'mgl_collections';
67 $sql = "CREATE TABLE $table_name (
68 id varchar( 20 ) NOT NULL,
69 name varchar( 255 ) NOT NULL,
70 description text,
71 layout varchar( 50 ) NOT NULL,
72 galleries_ids longtext NOT NULL,
73 created_at datetime DEFAULT CURRENT_TIMESTAMP,
74 updated_at datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
75 PRIMARY KEY ( id )
76 ) $charset_collate;";
77
78 dbDelta( $sql );
79
80
81 // Migrate existing data from options to tables
82 if ( version_compare( $current_version, '1.0', '<' ) ) {
83 self::migrate_options_to_tables();
84 }
85
86 }
87
88 private static function migrate_options_to_tables() {
89 global $wpdb;
90
91 // Migrate shortcodes
92 $shortcodes = get_option( 'mgl_shortcodes', array() );
93 $shortcodes_table = $wpdb->prefix . 'mgl_gallery_shortcodes';
94
95 foreach ( $shortcodes as $id => $shortcode ) {
96 if ( empty( $id ) ) {
97 continue;
98 }
99
100 $wpdb->insert(
101 $shortcodes_table,
102 array(
103 'id' => $id,
104 'name' => $shortcode['name'],
105 'description' => $shortcode['description'] ?? '',
106 'layout' => $shortcode['layout'],
107 'medias' => serialize( $shortcode['medias'] ),
108 'lead_image_id' => $shortcode['lead_image_id'] ?? null,
109 'order_by' => $shortcode['order_by'] ?? null,
110 'is_post_mode' => ( isset( $shortcode['is_post_mode'] ) && $shortcode['is_post_mode'] ) ? 1 : 0,
111 'is_hero_mode' => isset( $shortcode['hero'] ) && $shortcode['hero'] ? 1 : 0,
112 'posts' => isset( $shortcode['posts'] ) ? serialize( $shortcode['posts'] ) : null,
113 'latest_posts' => $shortcode['latest_posts'] ?? null,
114 'tags' => isset( $shortcode['tags'] ) ? serialize( $shortcode['tags'] ) : null,
115 'dynamic_source' => $shortcode['dynamic_source'] ?? null,
116 'updated_at' => date( 'Y-m-d H:i:s', $shortcode['updated'] )
117 )
118 );
119 }
120
121 // Migrate collections
122 $collections = get_option( 'mgl_collections', array() );
123 $collections_table = $wpdb->prefix . 'mgl_collections';
124
125 foreach ( $collections as $id => $collection ) {
126 $wpdb->insert(
127 $collections_table,
128 array(
129 'id' => $id,
130 'name' => $collection['name'],
131 'description' => $collection['description'] ?? '',
132 'layout' => $collection['layout'],
133 'galleries_ids' => serialize( $collection['galleries_ids'] ),
134 'tags' => isset( $collection['tags'] ) ? serialize( $collection['tags'] ) : null,
135 'dynamic_source' => $collection['dynamic_source'] ?? null,
136 'updated_at' => date( 'Y-m-d H:i:s', $collection['updated'] )
137 )
138 );
139 }
140 }
141 }