| 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 |
add_action( 'wp_initialize_site', array( $this, 'ms_site_initialized' ), 12, 2 ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Returns prefixfied name of table or tables |
| 43 |
* |
| 44 |
* @param string property name |
| 45 |
* |
| 46 |
* @since 1.0 |
| 47 |
* @return string|array |
| 48 |
*/ |
| 49 |
public function __get( $name ){ |
| 50 |
|
| 51 |
if( in_array( $name, $this->table_names ) ){ |
| 52 |
global $wpdb; |
| 53 |
return $wpdb->prefix . static::TABLE_PREFIX . $name; |
| 54 |
|
| 55 |
// Get list of table names |
| 56 |
} elseif( 'tables' == $name ){ |
| 57 |
global $wpdb; |
| 58 |
$tables = []; |
| 59 |
|
| 60 |
foreach ($this->table_names as $table_name ){ |
| 61 |
$tables[ $table_name ] = $wpdb->prefix . static::TABLE_PREFIX . $table_name; |
| 62 |
} |
| 63 |
return $tables; |
| 64 |
|
| 65 |
} else { |
| 66 |
return NULL; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Uncomment the following method and define table structures |
| 72 |
* |
| 73 |
protected function create_table_{table_name}() { |
| 74 |
|
| 75 |
$sql_create_table = "CREATE TABLE IF NOT EXISTS {$this->{table_name}} ( |
| 76 |
id bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
| 77 |
name text NOT NULL, |
| 78 |
slug varchar(100) NOT NULL, |
| 79 |
author bigint(20) unsigned NOT NULL DEFAULT '0', |
| 80 |
created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 81 |
modified_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 82 |
content longtext NOT NULL, |
| 83 |
status varchar(20) NOT NULL DEFAULT 'draft', |
| 84 |
password varchar(255) NOT NULL DEFAULT '', |
| 85 |
PRIMARY KEY (id), |
| 86 |
KEY created_at (created_at), |
| 87 |
KEY slug (slug) |
| 88 |
) {$this->charset_collate()};\n"; |
| 89 |
|
| 90 |
$this->dbDelta( $sql_create_table ); |
| 91 |
} |
| 92 |
*/ |
| 93 |
|
| 94 |
|
| 95 |
/** |
| 96 |
* Create tables |
| 97 |
* |
| 98 |
* Should be invoked on plugin activation |
| 99 |
* |
| 100 |
* @since 1.0 |
| 101 |
* @return null |
| 102 |
*/ |
| 103 |
protected function create_tables() { |
| 104 |
|
| 105 |
// call create_table_{table_name} of all tables if are defined |
| 106 |
foreach ( $this->table_names as $table_name ){ |
| 107 |
$method_name = 'create_table_' . $table_name; |
| 108 |
if( method_exists( $this, $method_name ) ){ |
| 109 |
$this->$method_name(); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
do_action( 'averta/database/tables/created', $this->tables ); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
/** |
| 118 |
* Updates tables |
| 119 |
* |
| 120 |
* @param $last_executed_version |
| 121 |
* |
| 122 |
* @since 1.0 |
| 123 |
* @return null |
| 124 |
*/ |
| 125 |
protected function update_tables( $last_executed_version ) { |
| 126 |
/** |
| 127 |
* Example: |
| 128 |
* Execute table changes for new version 1.0.1 |
| 129 |
* |
| 130 |
* if( version_compare( '1.0.1', $last_executed_version, '>' ) ){ |
| 131 |
$this->runSql( "ALTER TABLE {$this->table_name} DROP COLUMN type" ); |
| 132 |
} |
| 133 |
*/ |
| 134 |
do_action( 'averta/database/tables/updated', $this->tables ); |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
/** |
| 139 |
* Updates tables if update is required |
| 140 |
* |
| 141 |
* @param bool $force force to create tables if not exists |
| 142 |
* |
| 143 |
* @since 1.0 |
| 144 |
* @return bool is any update required for tables? |
| 145 |
*/ |
| 146 |
public function migrate( $force = false ){ |
| 147 |
$last_executed_version = $this->get_last_executed_migration_version(); |
| 148 |
// check if the update is required |
| 149 |
if( ! $force && ( $last_executed_version == static::MIGRATION_VERSION ) ) |
| 150 |
return false; |
| 151 |
|
| 152 |
$this->create_tables(); |
| 153 |
$this->update_tables( $last_executed_version ); |
| 154 |
|
| 155 |
// update tables version to current version |
| 156 |
$this->update_last_executed_migration_version( static::MIGRATION_VERSION ); |
| 157 |
|
| 158 |
return true; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Inserts custom tables for a new site into the database. |
| 163 |
* |
| 164 |
* @since WP 5.1.0 |
| 165 |
* |
| 166 |
* @param int|WP_Site $site_id Site ID or object. |
| 167 |
* @param array $args |
| 168 |
* |
| 169 |
* @return bool|null |
| 170 |
*/ |
| 171 |
public function ms_site_initialized( $site_id, array $args = array() ){ |
| 172 |
if ( empty( $site_id ) ) { |
| 173 |
return null; |
| 174 |
} |
| 175 |
|
| 176 |
if ( ! $site = get_site( $site_id ) ) { |
| 177 |
return null; |
| 178 |
} |
| 179 |
|
| 180 |
$switch = false; |
| 181 |
if ( get_current_blog_id() !== $site->id ) { |
| 182 |
$switch = true; |
| 183 |
switch_to_blog( $site->id ); |
| 184 |
} |
| 185 |
|
| 186 |
$this->migrate( true ); |
| 187 |
|
| 188 |
if ( $switch ) { |
| 189 |
restore_current_blog(); |
| 190 |
} |
| 191 |
|
| 192 |
return true; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Drop all custom tables of this class |
| 197 |
* |
| 198 |
* @since 1.0 |
| 199 |
* @return void |
| 200 |
*/ |
| 201 |
protected function delete_tables(){ |
| 202 |
global $wpdb; |
| 203 |
|
| 204 |
foreach ( $this->tables as $table_name) { |
| 205 |
$wpdb->query("DROP TABLE IF EXISTS $table_name"); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
/** |
| 211 |
* Filter custom tables to drop when the blog is deleted |
| 212 |
* |
| 213 |
* @since 1.0 |
| 214 |
* @return array $tables |
| 215 |
*/ |
| 216 |
public function wpmu_drop_tables( $tables, $blog_id ){ |
| 217 |
global $wpdb; |
| 218 |
|
| 219 |
foreach ( $this->table_names as $table_name ){ |
| 220 |
$tables[] = $wpdb->prefix . $blog_id . '_' . static::TABLE_PREFIX . $name; |
| 221 |
} |
| 222 |
|
| 223 |
return $tables; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Creates new tables and updates existing tables to a new structure. |
| 228 |
* |
| 229 |
* @param string $sql |
| 230 |
* @since 1.0 |
| 231 |
*/ |
| 232 |
protected function dbDelta( $sql = '' ){ |
| 233 |
if( ! function_exists( 'dbDelta' ) ){ |
| 234 |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 235 |
} |
| 236 |
dbDelta( $sql ); |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Executes SQL |
| 241 |
* |
| 242 |
* @param string $sql |
| 243 |
* @since 1.0 |
| 244 |
*/ |
| 245 |
protected function runSql( $sql = '' ){ |
| 246 |
global $wpdb; |
| 247 |
$wpdb->query( $sql ); |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Retrieves migration version |
| 252 |
* |
| 253 |
* @since 1.0 |
| 254 |
* @return string |
| 255 |
*/ |
| 256 |
protected function get_migration_version_id(){ |
| 257 |
return static::VERSION_PREFIX . 'db_version'; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Updates current migration version |
| 262 |
* |
| 263 |
* @param $version The version to be saved |
| 264 |
* |
| 265 |
* @since 1.0 |
| 266 |
* @return bool |
| 267 |
*/ |
| 268 |
protected function update_last_executed_migration_version( $version ){ |
| 269 |
return update_option( $this->get_migration_version_id(), $version ); |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Retrieves Last executed migration version |
| 274 |
* |
| 275 |
* @since 1.0 |
| 276 |
* @return mixed|void |
| 277 |
*/ |
| 278 |
protected function get_last_executed_migration_version(){ |
| 279 |
return get_option( $this->get_migration_version_id(), '1.0.0' ); |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* Gets database charset collate |
| 284 |
* |
| 285 |
* @return string |
| 286 |
*/ |
| 287 |
protected function charset_collate(){ |
| 288 |
global $wpdb; |
| 289 |
return $wpdb->get_charset_collate(); |
| 290 |
} |
| 291 |
|
| 292 |
} |
| 293 |
|