| 1 |
<?php |
| 2 |
/** |
| 3 |
* Orderable database. |
| 4 |
* |
| 5 |
* @package Orderable/Database |
| 6 |
*/ |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
/** |
| 11 |
* Addons module class. |
| 12 |
*/ |
| 13 |
class Orderable_Database { |
| 14 |
|
| 15 |
/** |
| 16 |
* Run database related operations. |
| 17 |
* |
| 18 |
* @return void |
| 19 |
*/ |
| 20 |
public static function run() { |
| 21 |
self::load_table_classes(); |
| 22 |
|
| 23 |
if ( is_admin() && ! wp_doing_ajax() ) { |
| 24 |
add_action( 'orderable_after_create_custom_tables', array( __CLASS__, 'upgrades' ) ); |
| 25 |
self::create_tables(); |
| 26 |
} |
| 27 |
|
| 28 |
self::append_custom_tables_to_wpdb(); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Load table classes. |
| 33 |
* |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
protected static function load_table_classes() { |
| 37 |
Orderable_Helpers::load_classes( |
| 38 |
self::get_table_classes(), |
| 39 |
'database/tables', |
| 40 |
ORDERABLE_INC_PATH |
| 41 |
); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Get table classes name. |
| 46 |
* |
| 47 |
* @return array |
| 48 |
*/ |
| 49 |
protected static function get_table_classes() { |
| 50 |
return array( |
| 51 |
'location-holidays-table' => 'Orderable_Location_Holidays_Table', |
| 52 |
'location-time-slots-table' => 'Orderable_Location_Time_Slots_Table', |
| 53 |
'location-locations-table' => 'Orderable_Location_Locations_Table', |
| 54 |
'location-delivery-zones-lookup-table' => 'Orderable_Location_Delivery_Zones_Lookup_Table', |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Create Orderable custom tables. |
| 60 |
* |
| 61 |
* @return void |
| 62 |
*/ |
| 63 |
protected static function create_tables() { |
| 64 |
global $wpdb; |
| 65 |
|
| 66 |
$current_db_version = get_option( '_orderable_db_version' ); |
| 67 |
|
| 68 |
if ( ! empty( $current_db_version ) ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
if ( 'yes' === get_transient( 'orderable_creating_database' ) ) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
$wpdb->hide_errors(); |
| 77 |
|
| 78 |
$collate = $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : ''; |
| 79 |
|
| 80 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 81 |
|
| 82 |
set_transient( 'orderable_creating_database', 'yes', MINUTE_IN_SECONDS * 10 ); |
| 83 |
|
| 84 |
/** |
| 85 |
* Fires before creating Orderable custom tables. |
| 86 |
* |
| 87 |
* @since 1.8.0 |
| 88 |
* @hook orderable_before_create_custom_tables |
| 89 |
*/ |
| 90 |
do_action( 'orderable_before_create_custom_tables' ); |
| 91 |
|
| 92 |
$schema = ''; |
| 93 |
$create_table_query = ''; |
| 94 |
foreach ( self::get_table_classes() as $table_class ) { |
| 95 |
if ( ! class_exists( $table_class ) ) { |
| 96 |
continue; |
| 97 |
} |
| 98 |
|
| 99 |
$table_name = $wpdb->prefix . $table_class::get_table_name(); |
| 100 |
$schema = $table_class::get_schema(); |
| 101 |
|
| 102 |
$create_table_query .= "CREATE TABLE $table_name $schema $collate;"; |
| 103 |
} |
| 104 |
|
| 105 |
dbDelta( $create_table_query ); |
| 106 |
|
| 107 |
delete_transient( 'orderable_creating_database' ); |
| 108 |
|
| 109 |
/** |
| 110 |
* Fires after creating Orderable custom tables. |
| 111 |
* |
| 112 |
* @since 1.8.0 |
| 113 |
* @hook orderable_after_create_custom_tables |
| 114 |
*/ |
| 115 |
do_action( 'orderable_after_create_custom_tables' ); |
| 116 |
|
| 117 |
update_option( '_orderable_db_version', ORDERABLE_VERSION ); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Append Orderable custom tables names to $wpdb object. |
| 122 |
* |
| 123 |
* That way, Orderable custom tables name can be accessed |
| 124 |
* like that `$wpdb->orderable_location_holidays`. |
| 125 |
* |
| 126 |
* @return void |
| 127 |
*/ |
| 128 |
protected static function append_custom_tables_to_wpdb() { |
| 129 |
global $wpdb; |
| 130 |
|
| 131 |
if ( ! get_option( '_orderable_db_version' ) ) { |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
foreach ( self::get_table_classes() as $table_class ) { |
| 136 |
if ( ! class_exists( $table_class ) ) { |
| 137 |
continue; |
| 138 |
} |
| 139 |
|
| 140 |
$table_name = $table_class::get_table_name(); |
| 141 |
|
| 142 |
$wpdb->$table_name = $wpdb->prefix . $table_name; |
| 143 |
$wpdb->tables[] = $table_name; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Run database upgrades. |
| 149 |
* |
| 150 |
* @return void |
| 151 |
*/ |
| 152 |
public static function upgrades() { |
| 153 |
$routines = array( |
| 154 |
'1.8.0' => 'upgrade_1_8_0', |
| 155 |
); |
| 156 |
|
| 157 |
array_walk( $routines, array( __CLASS__, 'run_upgrade_routine' ) ); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Run a upgrade routine |
| 162 |
* |
| 163 |
* @param string $routine The function name. |
| 164 |
* @param string $version The version number to be tested. |
| 165 |
* @return void |
| 166 |
*/ |
| 167 |
protected static function run_upgrade_routine( $routine, $version ) { |
| 168 |
$orderable_db_version = get_option( '_orderable_db_version' ); |
| 169 |
|
| 170 |
if ( ! empty( $orderable_db_version ) && version_compare( $orderable_db_version, $version, '>=' ) ) { |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
self::$routine(); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Upgrade routine to v1.8.0. |
| 179 |
* |
| 180 |
* @return void |
| 181 |
*/ |
| 182 |
public static function upgrade_1_8_0() { |
| 183 |
/** |
| 184 |
* Fires on the database upgrade routine. |
| 185 |
* |
| 186 |
* @since 1.8.0 |
| 187 |
* @hook orderable_upgrade_database_routine |
| 188 |
* @param string $version The upgrade version routine. E.g.: 1.8.0. |
| 189 |
*/ |
| 190 |
do_action( 'orderable_upgrade_database_routine', '1.8.0' ); |
| 191 |
} |
| 192 |
} |
| 193 |
|