| 1 |
<?php |
| 2 |
/** |
| 3 |
* Activate DB table for time slots availability. |
| 4 |
* |
| 5 |
* @package Booking Calendar |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
if ( ! defined( 'WPBC_AVAILABILITY_TIMESLOTS_DB_VERSION' ) ) { |
| 13 |
define( 'WPBC_AVAILABILITY_TIMESLOTS_DB_VERSION', '1.0.0' ); |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Create or update blocked time-slot intervals table. |
| 18 |
* |
| 19 |
* @return void |
| 20 |
*/ |
| 21 |
function wpbc_activation__availability_timeslots() { |
| 22 |
|
| 23 |
global $wpdb; |
| 24 |
|
| 25 |
$charset_collate = ( ! empty( $wpdb->charset ) ) ? "DEFAULT CHARACTER SET $wpdb->charset" : ''; |
| 26 |
$charset_collate .= ( ! empty( $wpdb->collate ) ) ? " COLLATE $wpdb->collate" : ''; |
| 27 |
$table = wpbc_availability_timeslots__table_name(); |
| 28 |
|
| 29 |
if ( ! wpbc_is_table_exists( 'booking_availability_timeslots' ) ) { |
| 30 |
$simple_sql = "CREATE TABLE {$table} ( |
| 31 |
timeslot_id bigint(20) unsigned NOT NULL auto_increment, |
| 32 |
resource_id bigint(20) unsigned NOT NULL default 1, |
| 33 |
block_date date NOT NULL, |
| 34 |
start_second mediumint(8) unsigned NOT NULL default 0, |
| 35 |
end_second mediumint(8) unsigned NOT NULL default 0, |
| 36 |
status varchar(20) NOT NULL default 'active', |
| 37 |
group_uid varchar(64) NOT NULL default '', |
| 38 |
source varchar(30) NOT NULL default 'manual', |
| 39 |
note text, |
| 40 |
created_by bigint(20) unsigned NOT NULL default 0, |
| 41 |
modified_by bigint(20) unsigned NOT NULL default 0, |
| 42 |
creation_date datetime NOT NULL default '0000-00-00 00:00:00', |
| 43 |
modification_date datetime NOT NULL default '0000-00-00 00:00:00', |
| 44 |
PRIMARY KEY (timeslot_id), |
| 45 |
KEY resource_date_start (resource_id, block_date, start_second), |
| 46 |
KEY date_resource (block_date, resource_id), |
| 47 |
KEY status_resource_date (status, resource_id, block_date), |
| 48 |
KEY group_uid (group_uid) |
| 49 |
) {$charset_collate}"; |
| 50 |
|
| 51 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter |
| 52 |
$wpdb->query( $simple_sql ); |
| 53 |
} |
| 54 |
|
| 55 |
$fields = array( |
| 56 |
'status' => "ALTER TABLE {$table} ADD status varchar(20) NOT NULL default 'active'", |
| 57 |
'group_uid' => "ALTER TABLE {$table} ADD group_uid varchar(64) NOT NULL default ''", |
| 58 |
'source' => "ALTER TABLE {$table} ADD source varchar(30) NOT NULL default 'manual'", |
| 59 |
'note' => "ALTER TABLE {$table} ADD note text", |
| 60 |
'created_by' => "ALTER TABLE {$table} ADD created_by bigint(20) unsigned NOT NULL default 0", |
| 61 |
'modified_by' => "ALTER TABLE {$table} ADD modified_by bigint(20) unsigned NOT NULL default 0", |
| 62 |
'creation_date' => "ALTER TABLE {$table} ADD creation_date datetime NOT NULL default '0000-00-00 00:00:00'", |
| 63 |
'modification_date' => "ALTER TABLE {$table} ADD modification_date datetime NOT NULL default '0000-00-00 00:00:00'", |
| 64 |
); |
| 65 |
|
| 66 |
foreach ( $fields as $field_name => $sql ) { |
| 67 |
if ( ! wpbc_is_field_in_table_exists( 'booking_availability_timeslots', $field_name ) ) { |
| 68 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter |
| 69 |
$wpdb->query( $sql ); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
$indexes = array( |
| 74 |
'resource_date_start' => "ALTER TABLE {$table} ADD INDEX resource_date_start (resource_id, block_date, start_second)", |
| 75 |
'date_resource' => "ALTER TABLE {$table} ADD INDEX date_resource (block_date, resource_id)", |
| 76 |
'status_resource_date' => "ALTER TABLE {$table} ADD INDEX status_resource_date (status, resource_id, block_date)", |
| 77 |
'group_uid' => "ALTER TABLE {$table} ADD INDEX group_uid (group_uid)", |
| 78 |
); |
| 79 |
|
| 80 |
foreach ( $indexes as $index_name => $sql ) { |
| 81 |
if ( ! wpbc_is_index_in_table_exists( 'booking_availability_timeslots', $index_name ) ) { |
| 82 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter |
| 83 |
$wpdb->query( $sql ); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
// Older beta builds used a soft-delete status. Unblocking now removes rows physically to keep this table small. |
| 88 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter |
| 89 |
$wpdb->query( "DELETE FROM {$table} WHERE status = 'deleted'" ); |
| 90 |
|
| 91 |
update_bk_option( 'booking_availability_timeslots_db_version', WPBC_AVAILABILITY_TIMESLOTS_DB_VERSION ); |
| 92 |
} |
| 93 |
add_bk_action( 'wpbc_free_version_activation', 'wpbc_activation__availability_timeslots' ); |
| 94 |
add_bk_action( 'wpbc_other_versions_activation', 'wpbc_activation__availability_timeslots' ); |
| 95 |
|
| 96 |
/** |
| 97 |
* Run DB upgrade on plugin update from admin side. |
| 98 |
* |
| 99 |
* @return void |
| 100 |
*/ |
| 101 |
function wpbc_activation__availability_timeslots__maybe_upgrade() { |
| 102 |
|
| 103 |
if ( ! is_admin() ) { |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
$db_version = get_bk_option( 'booking_availability_timeslots_db_version' ); |
| 108 |
if ( ( WPBC_AVAILABILITY_TIMESLOTS_DB_VERSION !== $db_version ) || ! wpbc_is_table_exists( 'booking_availability_timeslots' ) ) { |
| 109 |
wpbc_activation__availability_timeslots(); |
| 110 |
update_bk_option( 'booking_availability_timeslots_db_version', WPBC_AVAILABILITY_TIMESLOTS_DB_VERSION ); |
| 111 |
} |
| 112 |
} |
| 113 |
add_action( 'admin_init', 'wpbc_activation__availability_timeslots__maybe_upgrade' ); |
| 114 |
|
| 115 |
/** |
| 116 |
* Deactivate: remove blocked time-slot table together with Booking Calendar data. |
| 117 |
* |
| 118 |
* @return void |
| 119 |
*/ |
| 120 |
function wpbc_deactivation__availability_timeslots() { |
| 121 |
|
| 122 |
global $wpdb; |
| 123 |
$table = wpbc_availability_timeslots__table_name(); |
| 124 |
|
| 125 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 126 |
$wpdb->query( "DROP TABLE IF EXISTS {$table}" ); |
| 127 |
} |
| 128 |
add_bk_action( 'wpbc_free_version_deactivation', 'wpbc_deactivation__availability_timeslots' ); |
| 129 |
add_bk_action( 'wpbc_other_versions_deactivation', 'wpbc_deactivation__availability_timeslots' ); |
| 130 |
|