| 1 |
<?php |
| 2 |
|
| 3 |
// If this file is called directly, abort. |
| 4 |
if (!defined('WPINC')) { |
| 5 |
die; |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* Install tools |
| 10 |
* |
| 11 |
* Create whole DB structure |
| 12 |
*/ |
| 13 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound |
| 14 |
class EAInstallTools |
| 15 |
{ |
| 16 |
|
| 17 |
/** |
| 18 |
* DB version |
| 19 |
*/ |
| 20 |
public $easy_app_db_version; |
| 21 |
|
| 22 |
/** |
| 23 |
* @var wpdb |
| 24 |
*/ |
| 25 |
protected $wpdb; |
| 26 |
|
| 27 |
/** |
| 28 |
* @var EADBModels |
| 29 |
*/ |
| 30 |
protected $models; |
| 31 |
|
| 32 |
/** |
| 33 |
* @var EAOptions |
| 34 |
*/ |
| 35 |
protected $options; |
| 36 |
|
| 37 |
/** |
| 38 |
* EAInstallTools constructor. |
| 39 |
* @param wpdb $wpdb |
| 40 |
* @param EADBModels $models |
| 41 |
* @param EAOptions $options |
| 42 |
*/ |
| 43 |
function __construct($wpdb, $models, $options) |
| 44 |
{ |
| 45 |
$this->easy_app_db_version = EASY_APPOINTMENTS_VERSION; |
| 46 |
|
| 47 |
$this->wpdb = $wpdb; |
| 48 |
$this->models = $models; |
| 49 |
$this->options = $options; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Create db |
| 54 |
*/ |
| 55 |
public function init_db() |
| 56 |
{ |
| 57 |
|
| 58 |
global $wpdb; |
| 59 |
|
| 60 |
$table_prefix = $wpdb->prefix; |
| 61 |
$charset_collate = $wpdb->get_charset_collate(); |
| 62 |
|
| 63 |
$table_querys = array(); |
| 64 |
$alter_querys = array(); |
| 65 |
|
| 66 |
// ea_appointments |
| 67 |
$table_querys[] = |
| 68 |
'CREATE TABLE ' . $table_prefix . 'ea_appointments ( |
| 69 |
id int(11) NOT NULL AUTO_INCREMENT, |
| 70 |
location int(11) NOT NULL, |
| 71 |
service int(11) NOT NULL, |
| 72 |
worker int(11) NOT NULL, |
| 73 |
name varchar(255) DEFAULT NULL, |
| 74 |
email varchar(255) DEFAULT NULL, |
| 75 |
phone varchar(45) DEFAULT NULL, |
| 76 |
date date DEFAULT NULL, |
| 77 |
start time DEFAULT NULL, |
| 78 |
end time DEFAULT NULL, |
| 79 |
end_date date DEFAULT NULL, |
| 80 |
description text, |
| 81 |
status varchar(45) DEFAULT NULL, |
| 82 |
user int(11) DEFAULT NULL, |
| 83 |
created timestamp NULL DEFAULT CURRENT_TIMESTAMP, |
| 84 |
price decimal(10,2) DEFAULT NULL, |
| 85 |
ip varchar(45) DEFAULT NULL, |
| 86 |
session varchar(32) DEFAULT NULL, |
| 87 |
customer_id int(11) DEFAULT NULL, |
| 88 |
recurrence_id varchar(255) DEFAULT NULL, |
| 89 |
PRIMARY KEY (id), |
| 90 |
KEY appointments_location (location), |
| 91 |
KEY appointments_service (service), |
| 92 |
KEY appointments_worker (worker) |
| 93 |
) ' . $charset_collate . ';'; |
| 94 |
|
| 95 |
// ea_connections |
| 96 |
$table_querys[] = |
| 97 |
'CREATE TABLE ' . $table_prefix . 'ea_connections ( |
| 98 |
id int(11) NOT NULL AUTO_INCREMENT, |
| 99 |
group_id int(11) DEFAULT NULL, |
| 100 |
location int(11) DEFAULT NULL, |
| 101 |
service int(11) DEFAULT NULL, |
| 102 |
worker int(11) DEFAULT NULL, |
| 103 |
slot_count int(11) DEFAULT 1, |
| 104 |
day_of_week varchar(60) DEFAULT NULL, |
| 105 |
time_from time DEFAULT NULL, |
| 106 |
time_to time DEFAULT NULL, |
| 107 |
day_from date DEFAULT NULL, |
| 108 |
day_to date DEFAULT NULL, |
| 109 |
is_working smallint(3) DEFAULT NULL, |
| 110 |
repeat_week smallint(3) DEFAULT NULL, |
| 111 |
repeat_booking smallint(3) DEFAULT NULL, |
| 112 |
PRIMARY KEY (id), |
| 113 |
KEY location_to_connection (location), |
| 114 |
KEY service_to_location (service), |
| 115 |
KEY worker_to_connection (worker) |
| 116 |
) ' . $charset_collate . ';'; |
| 117 |
|
| 118 |
// ea_locations |
| 119 |
$table_querys[] = |
| 120 |
'CREATE TABLE ' . $table_prefix . 'ea_locations ( |
| 121 |
id int(11) NOT NULL AUTO_INCREMENT, |
| 122 |
name varchar(255) NOT NULL, |
| 123 |
address text NOT NULL, |
| 124 |
location varchar(255) DEFAULT NULL, |
| 125 |
cord varchar(255) DEFAULT NULL, |
| 126 |
PRIMARY KEY (id) |
| 127 |
) ' . $charset_collate . ';'; |
| 128 |
|
| 129 |
// ea_options |
| 130 |
$table_querys[] = |
| 131 |
'CREATE TABLE ' . $table_prefix . 'ea_options ( |
| 132 |
id int(11) NOT NULL AUTO_INCREMENT, |
| 133 |
ea_key varchar(45) DEFAULT NULL, |
| 134 |
ea_value text, |
| 135 |
type varchar(45) DEFAULT NULL, |
| 136 |
PRIMARY KEY (id) |
| 137 |
) ' . $charset_collate . ';'; |
| 138 |
|
| 139 |
// ea_staff |
| 140 |
$table_querys[] = |
| 141 |
'CREATE TABLE ' . $table_prefix . 'ea_staff ( |
| 142 |
id int(11) NOT NULL AUTO_INCREMENT, |
| 143 |
name varchar(100) DEFAULT NULL, |
| 144 |
description text, |
| 145 |
email varchar(100) DEFAULT NULL, |
| 146 |
phone varchar(45) DEFAULT NULL, |
| 147 |
PRIMARY KEY (id) |
| 148 |
) ' . $charset_collate . ';'; |
| 149 |
|
| 150 |
// ea_services |
| 151 |
$table_querys[] = |
| 152 |
'CREATE TABLE ' . $table_prefix . 'ea_services ( |
| 153 |
id int(11) NOT NULL AUTO_INCREMENT, |
| 154 |
name varchar(255) NOT NULL, |
| 155 |
service_color varchar(7) DEFAULT \'#0693E3\', |
| 156 |
sequence int(11) NOT NULL, |
| 157 |
duration int(11) NOT NULL, |
| 158 |
slot_step int(11) DEFAULT NULL, |
| 159 |
block_before int(11) DEFAULT 0, |
| 160 |
block_after int(11) DEFAULT 0, |
| 161 |
daily_limit int(11) DEFAULT 0, |
| 162 |
price decimal(10,2) DEFAULT NULL, |
| 163 |
advance_booking_days int(4) DEFAULT 0, |
| 164 |
description TEXT NULL, |
| 165 |
PRIMARY KEY (id) |
| 166 |
) ' . $charset_collate . ';'; |
| 167 |
|
| 168 |
// ea_meta_fields |
| 169 |
$table_querys[] = |
| 170 |
'CREATE TABLE ' . $table_prefix . 'ea_meta_fields ( |
| 171 |
id int(11) NOT NULL AUTO_INCREMENT, |
| 172 |
type varchar(50) NOT NULL, |
| 173 |
slug varchar(255) NOT NULL, |
| 174 |
label varchar(255) NOT NULL, |
| 175 |
mixed text NOT NULL, |
| 176 |
default_value varchar(50) NOT NULL, |
| 177 |
visible tinyint(4) NOT NULL, |
| 178 |
required tinyint(4) NOT NULL, |
| 179 |
validation varchar(50) DEFAULT NULL, |
| 180 |
position int(11) NOT NULL, |
| 181 |
PRIMARY KEY (id) |
| 182 |
) ' . $charset_collate . ';'; |
| 183 |
|
| 184 |
// ea_fields |
| 185 |
$table_querys[] = |
| 186 |
'CREATE TABLE ' . $table_prefix . 'ea_fields ( |
| 187 |
id int(11) NOT NULL AUTO_INCREMENT, |
| 188 |
app_id int(11) NOT NULL, |
| 189 |
field_id int(11) NOT NULL, |
| 190 |
value varchar(500) DEFAULT NULL, |
| 191 |
PRIMARY KEY (id) |
| 192 |
) ' . $charset_collate . ';'; |
| 193 |
|
| 194 |
// ea_error_logs |
| 195 |
$table_querys[] = |
| 196 |
'CREATE TABLE ' . $table_prefix . 'ea_error_logs ( |
| 197 |
id int(11) NOT NULL AUTO_INCREMENT, |
| 198 |
error_type varchar(50) DEFAULT NULL, |
| 199 |
errors text, |
| 200 |
errors_data text, |
| 201 |
PRIMARY KEY (id) |
| 202 |
) ' . $charset_collate . ';'; |
| 203 |
|
| 204 |
// Foreign keys (dbDelta does NOT handle these) |
| 205 |
$alter_querys[] = |
| 206 |
'ALTER TABLE ' . $table_prefix . 'ea_appointments |
| 207 |
ADD CONSTRAINT ' . $table_prefix . 'ea_appointments_ibfk_1 FOREIGN KEY (location) REFERENCES ' . $table_prefix . 'ea_locations (id) ON DELETE CASCADE, |
| 208 |
ADD CONSTRAINT ' . $table_prefix . 'ea_appointments_ibfk_2 FOREIGN KEY (service) REFERENCES ' . $table_prefix . 'ea_services (id) ON DELETE CASCADE, |
| 209 |
ADD CONSTRAINT ' . $table_prefix . 'ea_appointments_ibfk_3 FOREIGN KEY (worker) REFERENCES ' . $table_prefix . 'ea_staff (id) ON DELETE CASCADE'; |
| 210 |
|
| 211 |
$alter_querys[] = |
| 212 |
'ALTER TABLE ' . $table_prefix . 'ea_connections |
| 213 |
ADD CONSTRAINT ' . $table_prefix . 'ea_connections_ibfk_1 FOREIGN KEY (location) REFERENCES ' . $table_prefix . 'ea_locations (id) ON DELETE CASCADE, |
| 214 |
ADD CONSTRAINT ' . $table_prefix . 'ea_connections_ibfk_2 FOREIGN KEY (service) REFERENCES ' . $table_prefix . 'ea_services (id) ON DELETE CASCADE, |
| 215 |
ADD CONSTRAINT ' . $table_prefix . 'ea_connections_ibfk_3 FOREIGN KEY (worker) REFERENCES ' . $table_prefix . 'ea_staff (id) ON DELETE CASCADE'; |
| 216 |
|
| 217 |
$alter_querys[] = 'ALTER TABLE ' . $table_prefix . 'ea_services ADD COLUMN description TEXT NULL'; |
| 218 |
|
| 219 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 220 |
|
| 221 |
foreach ($table_querys as $query) { |
| 222 |
dbDelta($query); |
| 223 |
} |
| 224 |
|
| 225 |
foreach ($alter_querys as $query) { |
| 226 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 227 |
$wpdb->query($query); |
| 228 |
} |
| 229 |
|
| 230 |
$this->ea_create_customers_table(); |
| 231 |
|
| 232 |
update_option('easy_app_db_version', $this->easy_app_db_version); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
/** |
| 237 |
* Insert start data into options |
| 238 |
*/ |
| 239 |
public function init_data() |
| 240 |
{ |
| 241 |
// safety check if we already have meta fields |
| 242 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 243 |
$count_query = $this->wpdb->prepare('SELECT COUNT(*) FROM ' . $this->wpdb->prefix . 'ea_meta_fields' ); |
| 244 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 245 |
$num = (int) $this->wpdb->get_var($count_query); |
| 246 |
if ($num > 0) { |
| 247 |
return; |
| 248 |
} |
| 249 |
|
| 250 |
// options table |
| 251 |
$table_name = $this->wpdb->prefix . 'ea_options'; |
| 252 |
|
| 253 |
// rows data |
| 254 |
$wp_ea_options = $this->options->get_insert_options(); |
| 255 |
|
| 256 |
// insert options |
| 257 |
foreach ($wp_ea_options as $row) { |
| 258 |
$this->wpdb->insert( |
| 259 |
$table_name, |
| 260 |
$row |
| 261 |
); |
| 262 |
} |
| 263 |
|
| 264 |
// create custom form fields |
| 265 |
$default_fields = $this->migrateFormFields(); |
| 266 |
|
| 267 |
$table_name = $this->wpdb->prefix . 'ea_meta_fields'; |
| 268 |
|
| 269 |
foreach ($default_fields as $row) { |
| 270 |
$this->wpdb->insert( |
| 271 |
$table_name, |
| 272 |
$row |
| 273 |
); |
| 274 |
} |
| 275 |
|
| 276 |
// Set default UI mode to 'new' for fresh installation |
| 277 |
if (get_option('easy_ea_ui_mode') === false) { |
| 278 |
add_option('easy_ea_ui_mode', 'new'); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
public function update() |
| 283 |
{ |
| 284 |
|
| 285 |
// get table prefix |
| 286 |
$table_prefix = $this->wpdb->prefix; |
| 287 |
|
| 288 |
$charset_collate = $this->wpdb->get_charset_collate(); |
| 289 |
|
| 290 |
$version = get_option('easy_app_db_version', '1.0'); |
| 291 |
|
| 292 |
// For upgrades from versions prior to 4.0.0, default UI mode to 'old' (classic) if not explicitly set |
| 293 |
if (version_compare($version, '4.0.0', '<')) { |
| 294 |
if (get_option('easy_ea_ui_mode') === false) { |
| 295 |
add_option('easy_ea_ui_mode', 'old'); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
// if it is already latest version |
| 300 |
if (version_compare($version, $this->easy_app_db_version, '=')) { |
| 301 |
return; |
| 302 |
} |
| 303 |
|
| 304 |
// Migrate from 1.0 > 1.1 |
| 305 |
if (version_compare($version, '1.1', '<')) { |
| 306 |
|
| 307 |
$this->init_db(); |
| 308 |
|
| 309 |
// options table |
| 310 |
$table_name = $this->wpdb->prefix . 'ea_options'; |
| 311 |
// rows data |
| 312 |
$wp_ea_options = array( |
| 313 |
array('ea_key' => 'pending.email', 'ea_value' => '', 'type' => 'default'), |
| 314 |
array('ea_key' => 'price.hide', 'ea_value' => '0', 'type' => 'default') |
| 315 |
); |
| 316 |
// insert options |
| 317 |
foreach ($wp_ea_options as $row) { |
| 318 |
$this->wpdb->insert($table_name, $row); |
| 319 |
} |
| 320 |
|
| 321 |
$version = '1.1'; |
| 322 |
} |
| 323 |
|
| 324 |
// Migrate from 1.2.1- > 1.2.2 |
| 325 |
if (version_compare($version, '1.2.2', '<')) { |
| 326 |
$version = '1.2.2'; |
| 327 |
|
| 328 |
$alter_querys = array(); |
| 329 |
|
| 330 |
$alter_querys[] = 'ALTER TABLE ' . $table_prefix . 'ea_appointments DROP FOREIGN KEY ' . $table_prefix . 'ea_appointments_ibfk_1;'; |
| 331 |
$alter_querys[] = 'ALTER TABLE ' . $table_prefix . 'ea_appointments DROP FOREIGN KEY ' . $table_prefix . 'ea_appointments_ibfk_2;'; |
| 332 |
$alter_querys[] = 'ALTER TABLE ' . $table_prefix . 'ea_appointments DROP FOREIGN KEY ' . $table_prefix . 'ea_appointments_ibfk_3;'; |
| 333 |
$alter_querys[] = 'ALTER TABLE ' . $table_prefix . 'ea_connections DROP FOREIGN KEY ' . $table_prefix . 'ea_connections_ibfk_1;'; |
| 334 |
$alter_querys[] = 'ALTER TABLE ' . $table_prefix . 'ea_connections DROP FOREIGN KEY ' . $table_prefix . 'ea_connections_ibfk_2;'; |
| 335 |
$alter_querys[] = 'ALTER TABLE ' . $table_prefix . 'ea_connections DROP FOREIGN KEY ' . $table_prefix . 'ea_connections_ibfk_3;'; |
| 336 |
|
| 337 |
$alter_querys[] = |
| 338 |
'DELETE FROM ' . $table_prefix . 'ea_connections |
| 339 |
WHERE location NOT IN (SELECT id FROM ' . $table_prefix . 'ea_locations) |
| 340 |
OR service NOT IN (SELECT id FROM ' . $table_prefix . 'ea_services) |
| 341 |
OR worker NOT IN (SELECT id FROM ' . $table_prefix . 'ea_staff);'; |
| 342 |
|
| 343 |
$alter_querys[] = |
| 344 |
'DELETE FROM ' . $table_prefix . 'ea_appointments |
| 345 |
WHERE location NOT IN (SELECT id FROM ' . $table_prefix . 'ea_locations) |
| 346 |
OR service NOT IN (SELECT id FROM ' . $table_prefix . 'ea_services) |
| 347 |
OR worker NOT IN (SELECT id FROM ' . $table_prefix . 'ea_staff);'; |
| 348 |
|
| 349 |
// add relations |
| 350 |
foreach ($alter_querys as $alter_query) { |
| 351 |
// phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.PreparedSQL.NotPrepared |
| 352 |
$this->wpdb->query($alter_query); |
| 353 |
} |
| 354 |
|
| 355 |
$this->init_db(); |
| 356 |
} |
| 357 |
|
| 358 |
// Migrate from 1.2.2 > 1.2.3 |
| 359 |
if (version_compare($version, '1.2.3', '<')) { |
| 360 |
$version = '1.2.3'; |
| 361 |
} |
| 362 |
|
| 363 |
// Migrate form 1.2.3 > 1.2.4 |
| 364 |
if (version_compare($version, '1.2.4', '<')) { |
| 365 |
$option = array('ea_key' => 'datepicker', 'ea_value' => 'en-US', 'type' => 'default'); |
| 366 |
|
| 367 |
$table_name = $this->wpdb->prefix . 'ea_options'; |
| 368 |
|
| 369 |
$this->wpdb->insert($table_name, $option); |
| 370 |
|
| 371 |
$version = '1.2.4'; |
| 372 |
} |
| 373 |
|
| 374 |
// Migrate form 1.2.4 > 1.2.7 |
| 375 |
if (version_compare($version, '1.2.7', '<')) { |
| 376 |
$version = '1.2.7'; |
| 377 |
} |
| 378 |
|
| 379 |
// Migrate form 1.2.7 > 1.2.8 |
| 380 |
if (version_compare($version, '1.2.8', '<')) { |
| 381 |
$option = array('ea_key' => 'send.user.email', 'ea_value' => '0', 'type' => 'default'); |
| 382 |
|
| 383 |
$table_name = $this->wpdb->prefix . 'ea_options'; |
| 384 |
|
| 385 |
$this->wpdb->insert($table_name, $option); |
| 386 |
|
| 387 |
$version = '1.2.8'; |
| 388 |
} |
| 389 |
|
| 390 |
// Migrate form 1.2.8 > 1.2.9 |
| 391 |
if (version_compare($version, '1.2.9', '<')) { |
| 392 |
$option = array('ea_key' => 'custom.css', 'ea_value' => '', 'type' => 'default'); |
| 393 |
|
| 394 |
$table_name = $this->wpdb->prefix . 'ea_options'; |
| 395 |
|
| 396 |
$this->wpdb->insert($table_name, $option); |
| 397 |
|
| 398 |
$version = '1.2.9'; |
| 399 |
} |
| 400 |
|
| 401 |
if (version_compare($version, '1.3.0', '<')) { |
| 402 |
|
| 403 |
$wp_ea_options = array( |
| 404 |
array('ea_key' => 'show.iagree', 'ea_value' => '0', 'type' => 'default'), |
| 405 |
array('ea_key' => 'cancel.scroll', 'ea_value' => 'calendar', 'type' => 'default') |
| 406 |
); |
| 407 |
|
| 408 |
$table_name = $this->wpdb->prefix . 'ea_options'; |
| 409 |
|
| 410 |
foreach ($wp_ea_options as $row) { |
| 411 |
$this->wpdb->insert($table_name, $row); |
| 412 |
} |
| 413 |
|
| 414 |
$version = '1.3.0'; |
| 415 |
} |
| 416 |
|
| 417 |
if (version_compare($version, '1.4.0', '<')) { |
| 418 |
$version = '1.4.0'; |
| 419 |
} |
| 420 |
|
| 421 |
// Migrate to last version |
| 422 |
if (version_compare($version, '1.5.0', '<')) { |
| 423 |
$version = '1.5.0'; |
| 424 |
$table_querys = array(); |
| 425 |
|
| 426 |
$table_querys[] = |
| 427 |
'CREATE TABLE ' . $table_prefix . 'ea_fields ( |
| 428 |
id int(11) NOT NULL AUTO_INCREMENT, |
| 429 |
app_id int(11) NOT NULL, |
| 430 |
field_id int(11) NOT NULL, |
| 431 |
value varchar(500) DEFAULT NULL, |
| 432 |
PRIMARY KEY (id) |
| 433 |
) ' . $charset_collate . ';'; |
| 434 |
|
| 435 |
$table_querys[] = |
| 436 |
'CREATE TABLE ' . $table_prefix . 'ea_meta_fields ( |
| 437 |
id int(11) NOT NULL AUTO_INCREMENT, |
| 438 |
type varchar(50) NOT NULL, |
| 439 |
slug varchar(255) NOT NULL, |
| 440 |
label varchar(255) NOT NULL, |
| 441 |
mixed text NOT NULL, |
| 442 |
default_value varchar(50) NOT NULL, |
| 443 |
visible tinyint(4) NOT NULL, |
| 444 |
required tinyint(4) NOT NULL, |
| 445 |
validation varchar(50) DEFAULT NULL, |
| 446 |
position int(11) NOT NULL, |
| 447 |
PRIMARY KEY (id) |
| 448 |
) ' . $charset_collate . ';'; |
| 449 |
|
| 450 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 451 |
|
| 452 |
foreach ($table_querys as $table) { |
| 453 |
dbDelta($table); |
| 454 |
} |
| 455 |
|
| 456 |
$default_fields = $this->migrateFormFields(); |
| 457 |
$table_name = $this->wpdb->prefix . 'ea_meta_fields'; |
| 458 |
|
| 459 |
$ids = array(); |
| 460 |
|
| 461 |
foreach ($default_fields as $row) { |
| 462 |
$this->wpdb->insert($table_name, $row); |
| 463 |
$ids[$row['slug']] = $this->wpdb->insert_id; |
| 464 |
} |
| 465 |
|
| 466 |
$this->migrateOldFormValues($ids); |
| 467 |
} |
| 468 |
|
| 469 |
if (version_compare($version, '1.9.3', '<')) { |
| 470 |
$table_queries = array(); |
| 471 |
|
| 472 |
$table_queries[] = |
| 473 |
'CREATE TABLE ' . $table_prefix . 'ea_error_logs ( |
| 474 |
id int(11) NOT NULL AUTO_INCREMENT, |
| 475 |
error_type varchar(50) DEFAULT NULL, |
| 476 |
errors text, |
| 477 |
errors_data text, |
| 478 |
PRIMARY KEY (id) |
| 479 |
) ' . $charset_collate . ';'; |
| 480 |
|
| 481 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 482 |
|
| 483 |
foreach ($table_queries as $table) { |
| 484 |
dbDelta($table); |
| 485 |
} |
| 486 |
|
| 487 |
$version = '1.9.3'; |
| 488 |
} |
| 489 |
|
| 490 |
update_option('easy_app_db_version', $version); |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
public function update_email_options($type = 'user') |
| 495 |
{ |
| 496 |
global $wpdb; |
| 497 |
$table_name = $wpdb->prefix . 'ea_options'; |
| 498 |
|
| 499 |
// Define parent key and children based on type |
| 500 |
if ($type === 'user') { |
| 501 |
$parent_key = 'send.user.email'; |
| 502 |
$children = [ |
| 503 |
'send.user.pending_email', |
| 504 |
'send.user.reservation_email', |
| 505 |
'send.user.cancelled_email', |
| 506 |
'send.user.confirmed_email', |
| 507 |
]; |
| 508 |
} elseif ($type === 'worker') { |
| 509 |
$parent_key = 'send.worker.email'; |
| 510 |
$children = [ |
| 511 |
'send.worker.pending_email', |
| 512 |
'send.worker.reservation_email', |
| 513 |
'send.worker.cancelled_email', |
| 514 |
'send.worker.confirmed_email', |
| 515 |
]; |
| 516 |
} else { |
| 517 |
return false; // Invalid type |
| 518 |
} |
| 519 |
|
| 520 |
// Check if parent is enabled |
| 521 |
// phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 522 |
$parent_value = $wpdb->get_var($wpdb->prepare( "SELECT ea_value FROM $table_name WHERE ea_key = %s", $parent_key )); |
| 523 |
|
| 524 |
if ($parent_value !== '1') { |
| 525 |
return false; // Parent not enabled, do nothing |
| 526 |
} |
| 527 |
|
| 528 |
// Insert or update each child key |
| 529 |
foreach ($children as $key) { |
| 530 |
// phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 531 |
$existing = $wpdb->get_var($wpdb->prepare( "SELECT COUNT(*) FROM $table_name WHERE ea_key = %s", $key )); |
| 532 |
|
| 533 |
if ($existing) { |
| 534 |
// Update to 1 |
| 535 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 536 |
$wpdb->update( |
| 537 |
$table_name, |
| 538 |
['ea_value' => '1'], |
| 539 |
['ea_key' => $key], |
| 540 |
['%s'], |
| 541 |
['%s'] |
| 542 |
); |
| 543 |
} else { |
| 544 |
// Insert as 1 |
| 545 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery |
| 546 |
$wpdb->insert( |
| 547 |
$table_name, |
| 548 |
[ |
| 549 |
'ea_key' => $key, |
| 550 |
'ea_value' => '1', |
| 551 |
], |
| 552 |
['%s', '%s'] |
| 553 |
); |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
return true; |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
function ea_create_customers_table() |
| 562 |
{ |
| 563 |
global $wpdb; |
| 564 |
|
| 565 |
$table_name = $wpdb->prefix . 'ea_customers'; |
| 566 |
|
| 567 |
$charset_collate = $wpdb->get_charset_collate(); |
| 568 |
|
| 569 |
$sql = "CREATE TABLE $table_name ( |
| 570 |
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, |
| 571 |
user_id BIGINT(20) UNSIGNED DEFAULT NULL, |
| 572 |
name VARCHAR(255) NOT NULL, |
| 573 |
email VARCHAR(255) DEFAULT '', |
| 574 |
mobile VARCHAR(50) DEFAULT '', |
| 575 |
dob VARCHAR(50) DEFAULT '', |
| 576 |
address TEXT, |
| 577 |
PRIMARY KEY (id) |
| 578 |
) $charset_collate;"; |
| 579 |
|
| 580 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 581 |
dbDelta($sql); |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
private function migrateFormFields() |
| 586 |
{ |
| 587 |
$email = __('EMail', 'easy-appointments'); |
| 588 |
$name = __('Name', 'easy-appointments'); |
| 589 |
$phone = __('Phone', 'easy-appointments'); |
| 590 |
$comment = __('Description', 'easy-appointments'); |
| 591 |
|
| 592 |
$data = array(); |
| 593 |
|
| 594 |
// email |
| 595 |
$data[] = array( |
| 596 |
'type' => 'EMAIL', |
| 597 |
'slug' => str_replace('-', '_', sanitize_title('email')), |
| 598 |
'label' => $email, |
| 599 |
'default_value' => '', |
| 600 |
'validation' => 'email', |
| 601 |
'mixed' => '', |
| 602 |
'visible' => 1, |
| 603 |
'required' => 1, |
| 604 |
'position' => 1 |
| 605 |
); |
| 606 |
|
| 607 |
$data[] = array( |
| 608 |
'type' => 'INPUT', |
| 609 |
'slug' => str_replace('-', '_', sanitize_title('name')), |
| 610 |
'label' => $name, |
| 611 |
'default_value' => '', |
| 612 |
'validation' => 'minlength-3', |
| 613 |
'mixed' => '', |
| 614 |
'visible' => 1, |
| 615 |
'required' => 1, |
| 616 |
'position' => 2 |
| 617 |
); |
| 618 |
|
| 619 |
$data[] = array( |
| 620 |
'type' => 'INPUT', |
| 621 |
'slug' => str_replace('-', '_', sanitize_title('phone')), |
| 622 |
'label' => $phone, |
| 623 |
'default_value' => '', |
| 624 |
'validation' => 'minlength-3', |
| 625 |
'mixed' => '', |
| 626 |
'visible' => 1, |
| 627 |
'required' => 1, |
| 628 |
'position' => 3 |
| 629 |
); |
| 630 |
|
| 631 |
$data[] = array( |
| 632 |
'type' => 'TEXTAREA', |
| 633 |
'slug' => str_replace('-', '_', sanitize_title('description')), |
| 634 |
'label' => $comment, |
| 635 |
'default_value' => '', |
| 636 |
'validation' => NULL, |
| 637 |
'mixed' => '', |
| 638 |
'visible' => 1, |
| 639 |
'required' => 0, |
| 640 |
'position' => 4 |
| 641 |
); |
| 642 |
|
| 643 |
return $data; |
| 644 |
} |
| 645 |
|
| 646 |
/** |
| 647 |
* Insert all the old values from appointments |
| 648 |
* @param $ids |
| 649 |
*/ |
| 650 |
private function migrateOldFormValues($ids) |
| 651 |
{ |
| 652 |
$table_name = 'ea_appointments'; |
| 653 |
|
| 654 |
$apps = $this->models->get_all_rows($table_name); |
| 655 |
|
| 656 |
$chunks = array_chunk($apps, 100); |
| 657 |
|
| 658 |
$rows = array(); |
| 659 |
$keys = array('email', 'name', 'phone', 'description'); |
| 660 |
|
| 661 |
$table_name = $this->wpdb->prefix . 'ea_fields'; |
| 662 |
|
| 663 |
foreach ($chunks as $chunk) { |
| 664 |
// helpers |
| 665 |
$values = array(); |
| 666 |
$place_holders = array(); |
| 667 |
|
| 668 |
$query = "INSERT INTO $table_name (app_id, field_id, value) VALUES "; |
| 669 |
|
| 670 |
// all appointments |
| 671 |
foreach ($chunk as $app) { |
| 672 |
// set insert for every key email, name, phone, description |
| 673 |
foreach ($keys as $key) { |
| 674 |
array_push($values, $app->id, $ids[$key], $app->{$key}); |
| 675 |
$place_holders[] = "('%d', '%d', '%s')"; |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
$query .= implode(', ', $place_holders); |
| 680 |
// phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare |
| 681 |
$this->wpdb->query($this->wpdb->prepare("$query ", $values)); |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
/** |
| 686 |
* |
| 687 |
*/ |
| 688 |
public function set_demo_data() |
| 689 |
{ |
| 690 |
|
| 691 |
$data = array( |
| 692 |
'ea_staff' => array( |
| 693 |
array('id' => 1, 'name' => 'John Smit', 'description' => 'Worker 1', 'email' => 'someemail@email.com', 'phone' => '123456'), |
| 694 |
array('id' => 2, 'name' => 'Peter Dalas', 'description' => 'Worker 2', 'email' => 'dummy@email.com', 'phone' => '112233') |
| 695 |
), |
| 696 |
'ea_locations' => array( |
| 697 |
array('id' => 1, 'name' => 'New York', 'address' => 'Street 1', 'location' => 'New York', 'cord' => ''), |
| 698 |
array('id' => 2, 'name' => 'Washington DC', 'address' => 'Street 10', 'location' => 'Wasington DC', 'cord' => '') |
| 699 |
), |
| 700 |
'ea_services' => array( |
| 701 |
array('id' => 1, 'name' => 'Car wash', 'duration' => 60, 'price' => 25, 'service_color' => '#0693E3'), |
| 702 |
array('id' => 2, 'name' => 'Car polishing', 'duration' => 45, 'price' => 10, 'service_color' => '#FF6900') |
| 703 |
), |
| 704 |
); |
| 705 |
|
| 706 |
foreach ($data as $table => $rows) { |
| 707 |
$tableName = $this->wpdb->prefix . $table; |
| 708 |
|
| 709 |
foreach ($rows as $row) { |
| 710 |
$this->wpdb->insert( |
| 711 |
$tableName, |
| 712 |
$row |
| 713 |
); |
| 714 |
} |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
function ea_sync_customers_from_appointments() |
| 719 |
{ |
| 720 |
global $wpdb; |
| 721 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 722 |
$oldest_appointment = $wpdb->get_row(" |
| 723 |
SELECT * |
| 724 |
FROM {$wpdb->prefix}ea_appointments |
| 725 |
ORDER BY date ASC |
| 726 |
LIMIT 1 |
| 727 |
", ARRAY_A); |
| 728 |
|
| 729 |
if ($oldest_appointment) { |
| 730 |
$start_date = $oldest_appointment['date']; |
| 731 |
$end_date = gmdate('Y-m-d'); |
| 732 |
$appointments = $this->models->get_all_appointments(['from' => $start_date, 'to' => $end_date]); |
| 733 |
foreach ($appointments as $appointment) { |
| 734 |
$app_id = (int) $appointment->id; |
| 735 |
$name = $appointment->name; |
| 736 |
$email = $appointment->email; |
| 737 |
$mobile = $appointment->phone; |
| 738 |
$user_id = $appointment->user; |
| 739 |
|
| 740 |
if (empty($email)) { |
| 741 |
continue; |
| 742 |
} |
| 743 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 744 |
$customer_id = $wpdb->get_var($wpdb->prepare( |
| 745 |
"SELECT id FROM {$wpdb->prefix}ea_customers WHERE email = %s", |
| 746 |
$email |
| 747 |
)); |
| 748 |
|
| 749 |
// Insert new customer if not found |
| 750 |
if (!$customer_id) { |
| 751 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery |
| 752 |
$inserted = $wpdb->insert("{$wpdb->prefix}ea_customers", [ |
| 753 |
'name' => $name, |
| 754 |
'email' => $email, |
| 755 |
'mobile' => $mobile, |
| 756 |
'user_id' => $user_id ? (int)$user_id : null, |
| 757 |
], ['%s', '%s', '%s', '%d']); |
| 758 |
|
| 759 |
if ($inserted) { |
| 760 |
$customer_id = $wpdb->insert_id; |
| 761 |
if (empty($appointment->customer_id) && $customer_id) { |
| 762 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 763 |
$wpdb->update( |
| 764 |
"{$wpdb->prefix}ea_appointments", |
| 765 |
['customer_id' => $customer_id], |
| 766 |
['id' => $app_id], |
| 767 |
['%d'], |
| 768 |
['%d'] |
| 769 |
); |
| 770 |
} |
| 771 |
} |
| 772 |
} |
| 773 |
} |
| 774 |
} |
| 775 |
} |
| 776 |
} |
| 777 |
|