PluginProbe
Booktics – Appointment Booking Calendar for Service Businesses / 1.0.12
Booktics – Appointment Booking Calendar for Service Businesses v1.0.12
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.12, at base/installer.php

284 lines 11.7 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 price_breakdown JSON DEFAULT NULL,
93 tax_total VARCHAR(100) DEFAULT NULL,
94 coupon_code VARCHAR(100) DEFAULT NULL,
95 coupon_discount VARCHAR(100) DEFAULT NULL,
96 subtotal VARCHAR(100) DEFAULT 0,
97 total VARCHAR(100) DEFAULT 0,
98 payment_method VARCHAR(50) DEFAULT NULL,
99 payment_intent_id VARCHAR(250) DEFAULT NULL,
100 payment_transaction_id VARCHAR(250) DEFAULT NULL,
101 directorist_id BIGINT UNSIGNED DEFAULT NULL,
102 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
103 updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
104 PRIMARY KEY (id),
105 INDEX (order_no),
106 INDEX (directorist_id)
107 ) $charset_collate;",
108
109 "CREATE TABLE IF NOT EXISTS {$table_prefix}booktics_payments (
110 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
111 order_id BIGINT UNSIGNED NOT NULL,
112 customer_id BIGINT UNSIGNED NOT NULL,
113 amount VARCHAR(20) NOT NULL,
114 currency VARCHAR(10) DEFAULT 'usd',
115 intent_id VARCHAR(255),
116 transaction_id VARCHAR(255),
117 status varchar(100) NOT NULL DEFAULT 'pending',
118 payment_method VARCHAR(50) NOT NULL,
119 refunded_amount VARCHAR(20) DEFAULT '0.00',
120 failure_reason TEXT,
121 date DATE NOT NULL,
122 date_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
123 INDEX (order_id),
124 INDEX (customer_id),
125 INDEX (intent_id),
126 INDEX (transaction_id)
127 ) $charset_collate;",
128
129 "CREATE TABLE IF NOT EXISTS {$table_prefix}booktics_guests (
130 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
131 wp_user_id BIGINT UNSIGNED DEFAULT NULL,
132 first_name VARCHAR(100) DEFAULT NULL,
133 last_name VARCHAR(100) DEFAULT NULL,
134 user_login VARCHAR(100) DEFAULT NULL,
135 email VARCHAR(100) DEFAULT NULL,
136 phone VARCHAR(10) DEFAULT NULL,
137 description VARCHAR(250) DEFAULT NULL,
138 image VARCHAR(250) DEFAULT NULL,
139 status INT DEFAULT 0,
140 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
141 updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
142 INDEX (wp_user_id),
143 INDEX (email),
144 INDEX (first_name)
145 ) $charset_collate;",
146 );
147 foreach ( $table_schema as $table_sql ) {
148 dbDelta( $table_sql );
149 }
150
151 // Handle booktics_guests table
152 $guests_table = "{$table_prefix}booktics_guests";
153 $guests_columns = $wpdb->get_col( 'DESC `' . \esc_sql( $guests_table ) . '`', 0 );
154
155 // Drop index on first_name if it exists
156 $indexes = $wpdb->get_results( "SHOW INDEX FROM `" . \esc_sql( $guests_table ) . "` WHERE Key_name = 'first_name'", ARRAY_A );
157 if ( ! empty( $indexes ) ) {
158 $wpdb->query(
159 sprintf(
160 'ALTER TABLE `%s` DROP INDEX `first_name`',
161 \esc_sql( $guests_table )
162 )
163 );
164 }
165
166 // Rename first_name to name if first_name exists and name does not
167 if ( in_array( 'first_name', $guests_columns, true ) && ! in_array( 'name', $guests_columns, true ) ) {
168 $wpdb->query(
169 sprintf(
170 'ALTER TABLE `%s` CHANGE COLUMN `first_name` `name` VARCHAR(100) DEFAULT NULL',
171 \esc_sql( $guests_table )
172 )
173 );
174 }
175
176 // Remove last_name column if it exists
177 if ( in_array( 'last_name', $guests_columns, true ) ) {
178 $wpdb->query(
179 sprintf(
180 'ALTER TABLE `%s` DROP COLUMN `last_name`',
181 \esc_sql( $guests_table )
182 )
183 );
184 }
185
186 // Add index on name if it does not exist
187 $indexes_name = $wpdb->get_results( "SHOW INDEX FROM `" . \esc_sql( $guests_table ) . "` WHERE Key_name = 'name'", ARRAY_A );
188 if ( empty( $indexes_name ) && in_array( 'name', $guests_columns, true ) ) {
189 $wpdb->query(
190 sprintf(
191 'ALTER TABLE `%s` ADD INDEX (`name`)',
192 \esc_sql( $guests_table )
193 )
194 );
195 }
196
197 // Alter first_name column if it exists
198 if ( in_array( 'first_name', $guests_columns, true ) ) {
199 $wpdb->query(
200 sprintf(
201 'ALTER TABLE `%s` MODIFY COLUMN `first_name` VARCHAR(100) DEFAULT NULL',
202 \esc_sql( $guests_table )
203 )
204 );
205 }
206
207 // Add missing columns to booktics_orders table
208 $orders_table = "{$table_prefix}booktics_orders";
209 $orders_columns = $wpdb->get_col( 'DESC `' . esc_sql( $orders_table ) . '`', 0 );
210
211 $orders_columns_to_add = array(
212 'payment_method' => 'VARCHAR(50) DEFAULT NULL',
213 'payment_intent_id' => 'VARCHAR(250) DEFAULT NULL',
214 'payment_transaction_id' => 'VARCHAR(250) DEFAULT NULL',
215 'custom_fields' => 'TEXT DEFAULT NULL',
216 'directorist_id' => 'BIGINT UNSIGNED DEFAULT NULL',
217 );
218
219 foreach ( $orders_columns_to_add as $column => $definition ) {
220 if ( ! in_array( $column, $orders_columns, true ) ) {
221 $query = sprintf(
222 'ALTER TABLE `%s` ADD COLUMN `%s` %s',
223 esc_sql( $orders_table ),
224 esc_sql( $column ),
225 $definition
226 );
227 $wpdb->query( $query );
228 }
229 }
230
231 // Add index for directorist_id if it doesn't exist
232 $indexes = $wpdb->get_results( "SHOW INDEX FROM `" . esc_sql( $orders_table ) . "` WHERE Key_name = 'directorist_id'", ARRAY_A );
233 if ( empty( $indexes ) && in_array( 'directorist_id', $orders_columns, true ) ) {
234 $wpdb->query(
235 sprintf(
236 'ALTER TABLE `%s` ADD INDEX (`directorist_id`)',
237 esc_sql( $orders_table )
238 )
239 );
240 }
241
242 // Handle booktics_cart_items table
243 $cart_items_table = "{$table_prefix}booktics_cart_items";
244 $cart_items_columns = $wpdb->get_col( 'DESC `' . esc_sql( $cart_items_table ) . '`', 0 );
245
246 // Rename sub_total to subtotal if exists
247 if ( in_array( 'sub_total', $cart_items_columns, true ) ) {
248 $wpdb->query(
249 sprintf(
250 'ALTER TABLE `%s` CHANGE COLUMN `sub_total` `subtotal` VARCHAR(20) NOT NULL',
251 esc_sql( $cart_items_table )
252 )
253 );
254 } elseif ( ! in_array( 'subtotal', $cart_items_columns, true ) ) {
255 $wpdb->query(
256 sprintf(
257 'ALTER TABLE `%s` ADD COLUMN `subtotal` VARCHAR(20) NOT NULL',
258 esc_sql( $cart_items_table )
259 )
260 );
261 }
262
263 $cart_items_columns_to_add = array(
264 'extra_services' => 'TEXT DEFAULT NULL',
265 'total' => 'VARCHAR(20) NOT NULL',
266 'additional_duration_id' => 'VARCHAR(100) NOT NULL',
267 'group_booking' => 'TEXT DEFAULT NULL',
268 'time_format' => 'VARCHAR(10) DEFAULT NULL',
269 );
270
271 foreach ( $cart_items_columns_to_add as $column => $definition ) {
272 if ( ! in_array( $column, $cart_items_columns, true ) ) {
273 $query = sprintf(
274 'ALTER TABLE `%s` ADD COLUMN `%s` %s',
275 esc_sql( $cart_items_table ),
276 esc_sql( $column ),
277 $definition
278 );
279 $wpdb->query( $query );
280 }
281 }
282 }
283 }
284