| 1 |
<?php |
| 2 |
/** |
| 3 |
* Locations table. |
| 4 |
* |
| 5 |
* @package Orderable/Database |
| 6 |
*/ |
| 7 |
|
| 8 |
defined( 'ABSPATH' ) || exit; |
| 9 |
|
| 10 |
/** |
| 11 |
* Locations table class. |
| 12 |
*/ |
| 13 |
class Orderable_Location_Locations_Table { |
| 14 |
/** |
| 15 |
* Run table operations. |
| 16 |
* |
| 17 |
* @return void |
| 18 |
*/ |
| 19 |
public static function run() { |
| 20 |
add_action( 'orderable_upgrade_database_routine', array( __CLASS__, 'upgrades' ), 5 ); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Get the table name without the prefix. |
| 25 |
* |
| 26 |
* @return string |
| 27 |
*/ |
| 28 |
public static function get_table_name() { |
| 29 |
return 'orderable_locations'; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Get the table schema. |
| 34 |
* |
| 35 |
* The schema returned is used as input to |
| 36 |
* dbDelta() function to create or update the |
| 37 |
* table structure. |
| 38 |
* |
| 39 |
* dbDelta has some rules that need to be followed: |
| 40 |
* https://codex.wordpress.org/Creating_Tables_with_Plugins |
| 41 |
* |
| 42 |
* @return string |
| 43 |
*/ |
| 44 |
public static function get_schema() { |
| 45 |
$schema = '( |
| 46 |
location_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, |
| 47 |
post_id BIGINT UNSIGNED NULL, |
| 48 |
title text NULL, |
| 49 |
address_line_1 text NULL, |
| 50 |
address_line_2 text NULL, |
| 51 |
city text NULL, |
| 52 |
country_state text NULL, |
| 53 |
postcode_zip text NULL, |
| 54 |
override_default_open_hours boolean DEFAULT NULL, |
| 55 |
open_hours longtext NULL, |
| 56 |
delivery boolean DEFAULT NULL, |
| 57 |
pickup boolean DEFAULT NULL, |
| 58 |
pickup_hours_same_as_delivery boolean DEFAULT NULL, |
| 59 |
asap_date boolean DEFAULT NULL, |
| 60 |
asap_time boolean DEFAULT NULL, |
| 61 |
lead_time tinytext NULL, |
| 62 |
lead_time_period tinytext NULL, |
| 63 |
preorder tinytext NULL, |
| 64 |
delivery_days_calculation_method tinytext NULL, |
| 65 |
enable_default_holidays boolean DEFAULT NULL, |
| 66 |
is_main_location boolean DEFAULT NULL, |
| 67 |
image_id BIGINT NULL, |
| 68 |
menu_order int(11) NULL, |
| 69 |
PRIMARY KEY (location_id), |
| 70 |
UNIQUE KEY post_id (post_id) |
| 71 |
)'; |
| 72 |
|
| 73 |
return $schema; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Run database upgrades. |
| 78 |
* |
| 79 |
* @param string $version The plugin version. |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
public static function upgrades( $version ) { |
| 83 |
if ( '1.8.0' === $version ) { |
| 84 |
self::migrate_main_location_to_custom_table(); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Migrate the main store data to the Orderable custom table. |
| 90 |
* |
| 91 |
* @return void |
| 92 |
*/ |
| 93 |
protected static function migrate_main_location_to_custom_table() { |
| 94 |
global $wpdb; |
| 95 |
|
| 96 |
$settings = get_option( 'orderable_settings' ); |
| 97 |
|
| 98 |
if ( empty( $settings['store_general_services'] ) || ! is_array( $settings['store_general_services'] ) ) { |
| 99 |
$delivery = (int) false; |
| 100 |
$pickup = (int) false; |
| 101 |
} else { |
| 102 |
$delivery = (int) in_array( 'delivery', $settings['store_general_services'], true ); |
| 103 |
$pickup = (int) in_array( 'pickup', $settings['store_general_services'], true ); |
| 104 |
|
| 105 |
update_option( '_orderable_main_location_store_general_services_settings_to_migrate', $settings['store_general_services'] ); |
| 106 |
} |
| 107 |
|
| 108 |
if ( empty( $settings['store_general_asap'] ) || ! is_array( $settings['store_general_asap'] ) ) { |
| 109 |
$asap_date = (int) false; |
| 110 |
$asap_time = (int) false; |
| 111 |
} else { |
| 112 |
$asap_date = (int) in_array( 'day', $settings['store_general_asap'], true ); |
| 113 |
$asap_time = (int) in_array( 'slot', $settings['store_general_asap'], true ); |
| 114 |
|
| 115 |
update_option( '_orderable_main_location_store_general_asap_settings_to_migrate', $settings['store_general_asap'] ); |
| 116 |
} |
| 117 |
|
| 118 |
if ( ! empty( $settings['store_general_open_hours'] ) ) { |
| 119 |
foreach ( $settings['store_general_open_hours'] as $day_number => $open_hour ) { |
| 120 |
$settings['store_general_open_hours'][ $day_number ]['enabled'] = ! empty( $open_hour['enabled'] ); |
| 121 |
} |
| 122 |
|
| 123 |
update_option( '_orderable_main_location_store_general_open_hours_settings_to_migrate', $settings['store_general_open_hours'] ); |
| 124 |
} |
| 125 |
|
| 126 |
if ( ! empty( $settings['store_general_service_hours_pickup_same'] ) ) { |
| 127 |
update_option( '_orderable_main_location_store_general_service_hours_pickup_same_settings_to_migrate', $settings['store_general_service_hours_pickup_same'] ); |
| 128 |
} |
| 129 |
|
| 130 |
if ( ! empty( $settings['store_general_lead_time'] ) ) { |
| 131 |
update_option( '_orderable_main_location_store_general_lead_time_settings_to_migrate', $settings['store_general_lead_time'] ); |
| 132 |
} |
| 133 |
|
| 134 |
if ( ! empty( $settings['store_general_preorder'] ) ) { |
| 135 |
update_option( '_orderable_main_location_store_general_preorder_settings_to_migrate', $settings['store_general_preorder'] ); |
| 136 |
} |
| 137 |
|
| 138 |
if ( ! empty( $settings['store_general_calculation_method'] ) ) { |
| 139 |
update_option( '_orderable_main_location_store_general_calculation_method_settings_to_migrate', $settings['store_general_calculation_method'] ); |
| 140 |
} |
| 141 |
|
| 142 |
$main_location_id = Orderable_Location::get_main_location_id(); |
| 143 |
|
| 144 |
/** |
| 145 |
* Filter whether we should skip the migration of the main location |
| 146 |
* data from `orderable_settings` to the orderable custom table. |
| 147 |
* |
| 148 |
* By default, we don't migrate if the custom table already |
| 149 |
* has the main location data. |
| 150 |
* |
| 151 |
* @since 1.8.0 |
| 152 |
* @hook orderable_skip_migrate_main_location_data_to_custom_table |
| 153 |
* @param bool $skip_migration If we should migrate. |
| 154 |
* @return bool New value |
| 155 |
*/ |
| 156 |
$skip_migration = apply_filters( 'orderable_skip_migrate_main_location_data_to_custom_table', (bool) $main_location_id ); |
| 157 |
|
| 158 |
if ( $skip_migration ) { |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
if ( ! empty( $main_location_id ) ) { |
| 163 |
$wpdb->delete( |
| 164 |
$wpdb->prefix . self::get_table_name(), |
| 165 |
array( |
| 166 |
'location_id' => $main_location_id, |
| 167 |
), |
| 168 |
array( |
| 169 |
'location_id' => '%d', |
| 170 |
) |
| 171 |
); |
| 172 |
} |
| 173 |
|
| 174 |
$data = array( |
| 175 |
'open_hours' => empty( $settings['store_general_open_hours'] ) ? '' : maybe_serialize( $settings['store_general_open_hours'] ), |
| 176 |
'delivery' => $delivery, |
| 177 |
'pickup' => $pickup, |
| 178 |
'pickup_hours_same_as_delivery' => empty( $settings['store_general_service_hours_pickup_same'] ) ? '' : (int) $settings['store_general_service_hours_pickup_same'], |
| 179 |
'asap_date' => $asap_date, |
| 180 |
'asap_time' => $asap_time, |
| 181 |
'lead_time' => empty( $settings['store_general_lead_time'] ) ? '' : $settings['store_general_lead_time'], |
| 182 |
'preorder' => empty( $settings['store_general_preorder'] ) ? '' : $settings['store_general_preorder'], |
| 183 |
'delivery_days_calculation_method' => empty( $settings['store_general_calculation_method'] ) ? '' : $settings['store_general_calculation_method'], |
| 184 |
); |
| 185 |
|
| 186 |
$data = wp_parse_args( $data, self::get_default_main_location_data() ); |
| 187 |
|
| 188 |
$wpdb->insert( $wpdb->prefix . self::get_table_name(), $data ); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Get default main location data. |
| 193 |
* |
| 194 |
* @return array |
| 195 |
*/ |
| 196 |
public static function get_default_main_location_data() { |
| 197 |
$data = array( |
| 198 |
'title' => __( 'Main Location', 'orderable' ), |
| 199 |
'address_line_1' => get_option( 'woocommerce_store_address', '' ), |
| 200 |
'address_line_2' => get_option( 'woocommerce_store_address_2', '' ), |
| 201 |
'city' => get_option( 'woocommerce_store_city', '' ), |
| 202 |
'country_state' => get_option( 'woocommerce_default_country', '' ), |
| 203 |
'postcode_zip' => get_option( 'woocommerce_store_postcode', '' ), |
| 204 |
'override_default_open_hours' => (int) true, |
| 205 |
'enable_default_holidays' => (int) true, |
| 206 |
'open_hours' => '', |
| 207 |
'delivery' => (int) false, |
| 208 |
'pickup' => (int) false, |
| 209 |
'pickup_hours_same_as_delivery' => '', |
| 210 |
'asap_date' => (int) false, |
| 211 |
'asap_time' => (int) false, |
| 212 |
'lead_time' => '', |
| 213 |
'lead_time_period' => 'days', |
| 214 |
'preorder' => '', |
| 215 |
'delivery_days_calculation_method' => '', |
| 216 |
'is_main_location' => 1, |
| 217 |
'image_id' => null, |
| 218 |
'menu_order' => null, |
| 219 |
); |
| 220 |
|
| 221 |
return $data; |
| 222 |
} |
| 223 |
} |
| 224 |
|