PluginProbe
Orderable – Restaurant & Food Ordering System / 1.20.0
Orderable – Restaurant & Food Ordering System v1.20.0
0.1.4 0.1.5 0.2.0 1.0.0 1.1.0 1.1.1 1.10.0 1.10.1 1.11.0 1.12.0 1.12.1 1.12.2 1.13.0 1.14.0 1.15.0 1.16.0 1.17.0 1.17.1 1.18.0 1.19.0 1.19.1 1.19.2 1.2.0 1.20.0 1.20.1 All 44 releases
orderable / inc / database / class-database.php

class-database.php in Orderable – Restaurant & Food Ordering System 1.20.0, at inc/database/class-database.php

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