PluginProbe
Depicter — Popup & Slider Builder / 1.2.0
Depicter — Popup & Slider Builder v1.2.0
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / vendor / averta / wordpress / src / Database / Migration.php

Migration.php in Depicter — Popup & Slider Builder 1.2.0, at vendor/averta/wordpress/src/Database/Migration.php

259 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Averta\WordPress\Database;
3
4
5
6 /**
7 * Creates and manages the structure of WP custom tables.
8 */
9 class Migration{
10
11 /**
12 * Current tables migration version
13 */
14 const MIGRATION_VERSION = "1.0.0";
15
16 /**
17 * Prefix for version option_name in options table
18 */
19 const VERSION_PREFIX = "plugin_slug_";
20
21 /**
22 * Table prefix
23 */
24 const TABLE_PREFIX = 'plugin_slug_';
25
26 /**
27 * Table names
28 * Example: ['documents', 'options']
29 */
30 protected $table_names = [];
31
32
33 /**
34 * Constructor
35 */
36 public function __construct() {
37 add_filter( 'wpmu_drop_tables', array( $this, 'wpmu_drop_tables' ), 11, 2 );
38 }
39
40 /**
41 * Returns prefixfied name of table or tables
42 *
43 * @param string property name
44 *
45 * @since 1.0
46 * @return string|array
47 */
48 public function __get( $name ){
49
50 if( in_array( $name, $this->table_names ) ){
51 global $wpdb;
52 return $wpdb->prefix . static::TABLE_PREFIX . $name;
53
54 // Get list of table names
55 } elseif( 'tables' == $name ){
56 global $wpdb;
57 $tables = [];
58
59 foreach ($this->table_names as $table_name ){
60 $tables[ $table_name ] = $wpdb->prefix . static::TABLE_PREFIX . $table_name;
61 }
62 return $tables;
63
64 } else {
65 return NULL;
66 }
67 }
68
69 /**
70 * Uncomment the following method and define table structures
71 *
72 protected function create_table_{table_name}() {
73
74 $sql_create_table = "CREATE TABLE IF NOT EXISTS {$this->{table_name}} (
75 id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
76 name text NOT NULL,
77 slug varchar(100) NOT NULL,
78 author bigint(20) unsigned NOT NULL DEFAULT '0',
79 created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
80 modified_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
81 content longtext NOT NULL,
82 status varchar(20) NOT NULL DEFAULT 'draft',
83 password varchar(255) NOT NULL DEFAULT '',
84 PRIMARY KEY (id),
85 KEY created_at (created_at),
86 KEY slug (slug)
87 ) {$this->charset_collate()};\n";
88
89 $this->dbDelta( $sql_create_table );
90 }
91 */
92
93
94 /**
95 * Create tables
96 *
97 * Should be invoked on plugin activation
98 *
99 * @since 1.0
100 * @return null
101 */
102 protected function create_tables() {
103
104 // call create_table_{table_name} of all tables if are defined
105 foreach ( $this->table_names as $table_name ){
106 $method_name = 'create_table_' . $table_name;
107 if( method_exists( $this, $method_name ) ){
108 $this->$method_name();
109 }
110 }
111
112 do_action( 'averta/database/tables/created', $this->tables );
113 }
114
115
116 /**
117 * Updates tables
118 *
119 * @param $last_executed_version
120 *
121 * @since 1.0
122 * @return null
123 */
124 protected function update_tables( $last_executed_version ) {
125 /**
126 * Example:
127 * Execute table changes for new version 1.0.1
128 *
129 * if( version_compare( '1.0.1', $last_executed_version, '>' ) ){
130 $this->runSql( "ALTER TABLE {$this->table_name} DROP COLUMN type" );
131 }
132 */
133 do_action( 'averta/database/tables/updated', $this->tables );
134 }
135
136
137 /**
138 * Updates tables if update is required
139 *
140 * @param bool $force force to create tables if not exists
141 *
142 * @since 1.0
143 * @return bool is any update required for tables?
144 */
145 public function migrate( $force = false ){
146 $last_executed_version = $this->get_last_executed_migration_version();
147 // check if the update is required
148 if( ! $force && ( $last_executed_version == static::MIGRATION_VERSION ) )
149 return false;
150
151 $this->create_tables();
152 $this->update_tables( $last_executed_version );
153
154 // update tables version to current version
155 $this->update_last_executed_migration_version( static::MIGRATION_VERSION );
156
157 return true;
158 }
159
160
161 /**
162 * Drop all custom tables of this class
163 *
164 * @since 1.0
165 * @return void
166 */
167 protected function delete_tables(){
168 global $wpdb;
169
170 foreach ( $this->tables as $table_name) {
171 $wpdb->query("DROP TABLE IF EXISTS $table_name");
172 }
173 }
174
175
176 /**
177 * Filter custom tables to drop when the blog is deleted
178 *
179 * @since 1.0
180 * @return array $tables
181 */
182 public function wpmu_drop_tables( $tables, $blog_id ){
183 global $wpdb;
184
185 foreach ( $this->table_names as $table_name ){
186 $tables[] = $wpdb->prefix . $blog_id . '_' . static::TABLE_PREFIX . $name;
187 }
188
189 return $tables;
190 }
191
192 /**
193 * Creates new tables and updates existing tables to a new structure.
194 *
195 * @param string $sql
196 * @since 1.0
197 */
198 protected function dbDelta( $sql = '' ){
199 if( ! function_exists( 'dbDelta' ) ){
200 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
201 }
202 dbDelta( $sql );
203 }
204
205 /**
206 * Executes SQL
207 *
208 * @param string $sql
209 * @since 1.0
210 */
211 protected function runSql( $sql = '' ){
212 global $wpdb;
213 $wpdb->query( $sql );
214 }
215
216 /**
217 * Retrieves migration version
218 *
219 * @since 1.0
220 * @return string
221 */
222 protected function get_migration_version_id(){
223 return static::VERSION_PREFIX . 'db_version';
224 }
225
226 /**
227 * Updates current migration version
228 *
229 * @param $version The version to be saved
230 *
231 * @since 1.0
232 * @return bool
233 */
234 protected function update_last_executed_migration_version( $version ){
235 return update_option( $this->get_migration_version_id(), $version );
236 }
237
238 /**
239 * Retrieves Last executed migration version
240 *
241 * @since 1.0
242 * @return mixed|void
243 */
244 protected function get_last_executed_migration_version(){
245 return get_option( $this->get_migration_version_id(), '1.0.0' );
246 }
247
248 /**
249 * Gets database charset collate
250 *
251 * @return string
252 */
253 protected function charset_collate(){
254 global $wpdb;
255 return $wpdb->get_charset_collate();
256 }
257
258 }
259