PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.14
Booktics – Appointment Booking Calendar for Service Businesses v1.0.14
1.0.29 1.0.28 1.0.27 1.0.26 1.0.25 1.0.24 1.0.23 1.0.22 1.0.21 1.0.20 1.0.19 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.2 1.0.3 All 31 releases
booktics / base / installer.php

installer.php in Booktics – Appointment Booking Calendar for Service Businesses 1.0.14, at base/installer.php

296 lines 12.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Booktics;
4
5 use Booktics\Dummy_Data\Dummy_Data_Manager;
6
7 class Installer {
8 /**
9 * Initializes the Booktics plugin by performing necessary setup tasks.
10 *
11 * This method performs the following setup operations:
12 * - Creates or updates required database tables
13 * - Initializes user roles and capabilities
14 * - Generates initial onboarding data using dummy data manager
15 */
16 public static function run(): void {
17 self::create_tables();
18 ( new Role() )->init();
19 ( new Dummy_Data_Manager() )->generate_onboarding_data();
20 }
21
22 /**
23 * Creates and updates database tables required for the Booktics plugin.
24 *
25 * Creates the following tables if they don't exist:
26 * - booktics_schedules: Stores scheduling information for team members and services
27 * - booktics_carts: Stores shopping cart information
28 * - booktics_cart_items: Stores individual items in shopping carts
29 * - booktics_orders: Stores order information and payment details
30 * - booktics_payments: Stores payment transaction records
31 *
32 * Also handles table structure updates by adding missing columns to existing tables
33 * and performing necessary column modifications.
34 */
35 public static function create_tables(): void {
36 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
37
38 global $wpdb;
39 $charset_collate = $wpdb->get_charset_collate();
40
41 $table_prefix = $wpdb->prefix;
42 $table_schema = array(
43 "CREATE TABLE IF NOT EXISTS {$table_prefix}booktics_schedules (
44 id mediumint(9) NOT NULL AUTO_INCREMENT,
45 team_member_id int(11) NOT NULL,
46 service_id int(11) NOT NULL,
47 location_id int(11) NOT NULL,
48 start_time varchar(100) NOT NULL,
49 end_time varchar(100) NOT NULL,
50 week_day varchar(100) NOT NULL,
51 custom_date varchar(100) NOT NULL,
52 created_at datetime DEFAULT NULL,
53 updated_at datetime DEFAULT NULL,
54 PRIMARY KEY (id)
55 ) $charset_collate;",
56
57 "CREATE TABLE IF NOT EXISTS {$table_prefix}booktics_carts (
58 id mediumint(9) NOT NULL AUTO_INCREMENT,
59 uuid varchar(36) NOT NULL,
60 user_id int(11) NOT NULL,
61 coupon_code varchar(100) DEFAULT NULL,
62 total decimal(10,2) DEFAULT NULL,
63 created_at datetime DEFAULT NULL,
64 updated_at datetime DEFAULT NULL,
65 PRIMARY KEY (id)
66 ) $charset_collate;",
67
68 "CREATE TABLE IF NOT EXISTS {$table_prefix}booktics_cart_items (
69 id mediumint(9) NOT NULL AUTO_INCREMENT,
70 uuid varchar(36) NOT NULL,
71 cart_id mediumint(9) NOT NULL,
72 team_member_id int(11) DEFAULT NULL,
73 service_id int(11) DEFAULT NULL,
74 location_id int(11) DEFAULT NULL,
75 date date DEFAULT NULL,
76 start_time varchar(100) DEFAULT NULL,
77 end_time varchar(100) DEFAULT NULL,
78 price VARCHAR(100) DEFAULT NULL,
79 created_at datetime DEFAULT NULL,
80 updated_at datetime DEFAULT NULL,
81 PRIMARY KEY (id)
82 ) $charset_collate;",
83
84 "CREATE TABLE IF NOT EXISTS {$table_prefix}booktics_orders (
85 id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
86 order_no VARCHAR(100) NOT NULL,
87 customer_id BIGINT UNSIGNED NOT NULL,
88 customer_note VARCHAR(100) DEFAULT NULL,
89 status VARCHAR(50) DEFAULT 'pending',
90 payment_status VARCHAR(50) DEFAULT NULL,
91 timezone VARCHAR(50) DEFAULT 'UTC',
92 currency VARCHAR(10) DEFAULT NULL,
93 price_breakdown JSON DEFAULT NULL,
94 tax_total VARCHAR(100) DEFAULT NULL,
95 coupon_code VARCHAR(100) DEFAULT NULL,
96 coupon_discount VARCHAR(100) DEFAULT NULL,
97 subtotal VARCHAR(100) DEFAULT 0,
98 total VARCHAR(100) DEFAULT 0,
99 payment_method VARCHAR(50) DEFAULT NULL,
100 payment_intent_id VARCHAR(250) DEFAULT NULL,
101 payment_transaction_id VARCHAR(250) DEFAULT NULL,
102 directorist_id BIGINT UNSIGNED DEFAULT NULL,
103 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
104 updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
105 PRIMARY KEY (id),
106 INDEX (order_no),
107 INDEX (directorist_id)
108 ) $charset_collate;",
109
110 "CREATE TABLE IF NOT EXISTS {$table_prefix}booktics_payments (
111 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
112 order_id BIGINT UNSIGNED NOT NULL,
113 customer_id BIGINT UNSIGNED NOT NULL,
114 amount VARCHAR(20) NOT NULL,
115 currency VARCHAR(10) DEFAULT 'usd',
116 intent_id VARCHAR(255),
117 transaction_id VARCHAR(255),
118 status varchar(100) NOT NULL DEFAULT 'pending',
119 payment_method VARCHAR(50) NOT NULL,
120 refunded_amount VARCHAR(20) DEFAULT '0.00',
121 failure_reason TEXT,
122 date DATE NOT NULL,
123 date_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
124 INDEX (order_id),
125 INDEX (customer_id),
126 INDEX (intent_id),
127 INDEX (transaction_id)
128 ) $charset_collate;",
129
130 "CREATE TABLE IF NOT EXISTS {$table_prefix}booktics_guests (
131 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
132 wp_user_id BIGINT UNSIGNED DEFAULT NULL,
133 first_name VARCHAR(100) DEFAULT NULL,
134 last_name VARCHAR(100) DEFAULT NULL,
135 user_login VARCHAR(100) DEFAULT NULL,
136 email VARCHAR(100) DEFAULT NULL,
137 phone VARCHAR(10) DEFAULT NULL,
138 description VARCHAR(250) DEFAULT NULL,
139 image VARCHAR(250) DEFAULT NULL,
140 status INT DEFAULT 0,
141 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
142 updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
143 INDEX (wp_user_id),
144 INDEX (email),
145 INDEX (first_name)
146 ) $charset_collate;",
147 );
148 foreach ( $table_schema as $table_sql ) {
149 dbDelta( $table_sql );
150 }
151
152 // Add currency column to existing orders table if it doesn't exist
153 $orders_table = "{$table_prefix}booktics_orders";
154 $column_exists = $wpdb->get_results(
155 "SHOW COLUMNS FROM `" . \esc_sql( $orders_table ) . "` LIKE 'currency'"
156 );
157 if ( empty( $column_exists ) ) {
158 $wpdb->query(
159 "ALTER TABLE `" . \esc_sql( $orders_table ) . "` ADD COLUMN currency VARCHAR(10) DEFAULT NULL AFTER timezone"
160 );
161 }
162
163 // Handle booktics_guests table
164 $guests_table = "{$table_prefix}booktics_guests";
165 $guests_columns = $wpdb->get_col( 'DESC `' . \esc_sql( $guests_table ) . '`', 0 );
166
167 // Drop index on first_name if it exists
168 $indexes = $wpdb->get_results( "SHOW INDEX FROM `" . \esc_sql( $guests_table ) . "` WHERE Key_name = 'first_name'", ARRAY_A );
169 if ( ! empty( $indexes ) ) {
170 $wpdb->query(
171 sprintf(
172 'ALTER TABLE `%s` DROP INDEX `first_name`',
173 \esc_sql( $guests_table )
174 )
175 );
176 }
177
178 // Rename first_name to name if first_name exists and name does not
179 if ( in_array( 'first_name', $guests_columns, true ) && ! in_array( 'name', $guests_columns, true ) ) {
180 $wpdb->query(
181 sprintf(
182 'ALTER TABLE `%s` CHANGE COLUMN `first_name` `name` VARCHAR(100) DEFAULT NULL',
183 \esc_sql( $guests_table )
184 )
185 );
186 }
187
188 // Remove last_name column if it exists
189 if ( in_array( 'last_name', $guests_columns, true ) ) {
190 $wpdb->query(
191 sprintf(
192 'ALTER TABLE `%s` DROP COLUMN `last_name`',
193 \esc_sql( $guests_table )
194 )
195 );
196 }
197
198 // Add index on name if it does not exist
199 $indexes_name = $wpdb->get_results( "SHOW INDEX FROM `" . \esc_sql( $guests_table ) . "` WHERE Key_name = 'name'", ARRAY_A );
200 if ( empty( $indexes_name ) && in_array( 'name', $guests_columns, true ) ) {
201 $wpdb->query(
202 sprintf(
203 'ALTER TABLE `%s` ADD INDEX (`name`)',
204 \esc_sql( $guests_table )
205 )
206 );
207 }
208
209 // Alter first_name column if it exists
210 if ( in_array( 'first_name', $guests_columns, true ) ) {
211 $wpdb->query(
212 sprintf(
213 'ALTER TABLE `%s` MODIFY COLUMN `first_name` VARCHAR(100) DEFAULT NULL',
214 \esc_sql( $guests_table )
215 )
216 );
217 }
218
219 // Add missing columns to booktics_orders table
220 $orders_table = "{$table_prefix}booktics_orders";
221 $orders_columns = $wpdb->get_col( 'DESC `' . esc_sql( $orders_table ) . '`', 0 );
222
223 $orders_columns_to_add = array(
224 'payment_method' => 'VARCHAR(50) DEFAULT NULL',
225 'payment_intent_id' => 'VARCHAR(250) DEFAULT NULL',
226 'payment_transaction_id' => 'VARCHAR(250) DEFAULT NULL',
227 'custom_fields' => 'TEXT DEFAULT NULL',
228 'directorist_id' => 'BIGINT UNSIGNED DEFAULT NULL',
229 );
230
231 foreach ( $orders_columns_to_add as $column => $definition ) {
232 if ( ! in_array( $column, $orders_columns, true ) ) {
233 $query = sprintf(
234 'ALTER TABLE `%s` ADD COLUMN `%s` %s',
235 esc_sql( $orders_table ),
236 esc_sql( $column ),
237 $definition
238 );
239 $wpdb->query( $query );
240 }
241 }
242
243 // Add index for directorist_id if it doesn't exist
244 $indexes = $wpdb->get_results( "SHOW INDEX FROM `" . esc_sql( $orders_table ) . "` WHERE Key_name = 'directorist_id'", ARRAY_A );
245 if ( empty( $indexes ) && in_array( 'directorist_id', $orders_columns, true ) ) {
246 $wpdb->query(
247 sprintf(
248 'ALTER TABLE `%s` ADD INDEX (`directorist_id`)',
249 esc_sql( $orders_table )
250 )
251 );
252 }
253
254 // Handle booktics_cart_items table
255 $cart_items_table = "{$table_prefix}booktics_cart_items";
256 $cart_items_columns = $wpdb->get_col( 'DESC `' . esc_sql( $cart_items_table ) . '`', 0 );
257
258 // Rename sub_total to subtotal if exists
259 if ( in_array( 'sub_total', $cart_items_columns, true ) ) {
260 $wpdb->query(
261 sprintf(
262 'ALTER TABLE `%s` CHANGE COLUMN `sub_total` `subtotal` VARCHAR(20) NOT NULL',
263 esc_sql( $cart_items_table )
264 )
265 );
266 } elseif ( ! in_array( 'subtotal', $cart_items_columns, true ) ) {
267 $wpdb->query(
268 sprintf(
269 'ALTER TABLE `%s` ADD COLUMN `subtotal` VARCHAR(20) NOT NULL',
270 esc_sql( $cart_items_table )
271 )
272 );
273 }
274
275 $cart_items_columns_to_add = array(
276 'extra_services' => 'TEXT DEFAULT NULL',
277 'total' => 'VARCHAR(20) NOT NULL',
278 'additional_duration_id' => 'VARCHAR(100) NOT NULL',
279 'group_booking' => 'TEXT DEFAULT NULL',
280 'time_format' => 'VARCHAR(10) DEFAULT NULL',
281 );
282
283 foreach ( $cart_items_columns_to_add as $column => $definition ) {
284 if ( ! in_array( $column, $cart_items_columns, true ) ) {
285 $query = sprintf(
286 'ALTER TABLE `%s` ADD COLUMN `%s` %s',
287 esc_sql( $cart_items_table ),
288 esc_sql( $column ),
289 $definition
290 );
291 $wpdb->query( $query );
292 }
293 }
294 }
295 }
296