PluginProbe
Orderable – Restaurant & Food Ordering System / 1.14.0
Orderable – Restaurant & Food Ordering System v1.14.0
0.1.4 0.1.5 0.2.0 1.0.0 1.1.0 1.1.1 1.10.0 1.10.1 1.11.0 1.12.0 1.12.1 1.12.2 1.13.0 1.14.0 1.15.0 1.16.0 1.17.0 1.17.1 1.18.0 1.19.0 1.19.1 1.19.2 1.2.0 1.20.0 1.20.1 All 44 releases
orderable / inc / modules / location / class-location.php

class-location.php in Orderable – Restaurant & Food Ordering System 1.14.0, at inc/modules/location/class-location.php

503 lines 11.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Module: Location.
4 *
5 * @package Orderable/Classes
6 */
7
8 defined( 'ABSPATH' ) || exit;
9
10 /**
11 * Addons module class.
12 */
13 class Orderable_Location {
14 /**
15 * Init.
16 */
17 public static function run() {
18 self::load_classes();
19
20 add_filter( 'orderable_settings', array( __CLASS__, 'add_location_settings_data' ), 20, 2 );
21 }
22
23 /**
24 * Load classes.
25 *
26 * @return void
27 */
28 public static function load_classes() {
29 $classes = array(
30 'admin' => array(
31 'location-admin' => 'Orderable_Location_Admin',
32 ),
33 'frontend' => array(
34 'location-single' => 'Orderable_Location_Single',
35 ),
36 'zones' => array(
37 'location-zones' => 'Orderable_Location_Zones',
38 'location-zones-admin' => 'Orderable_Location_Zones_Admin',
39 'location-zones-crud-handler' => 'Orderable_Location_Zones_CRUD_Handler',
40 ),
41 );
42
43 Orderable_Helpers::load_classes( $classes['admin'], 'location/admin', ORDERABLE_MODULES_PATH );
44 Orderable_Helpers::load_classes( $classes['zones'], 'location/zones', ORDERABLE_MODULES_PATH );
45 Orderable_Helpers::load_classes( $classes['frontend'], 'location', ORDERABLE_MODULES_PATH );
46 }
47
48 /**
49 * Append location data to Orderable settings.
50 *
51 * This function is used to intercept Orderable_Settings::get_setting
52 * and add the location settings retrieved from the Orderable custom
53 * tables. That way, we keep the compatibility with
54 * `Orderable_Settings::get_setting()` function.
55 *
56 * @param false|array $settings The orderable settings.
57 * @param string $setting_name The setting name to be retrieved.
58 *
59 * @return false|array
60 */
61 public static function add_location_settings_data( $settings, $setting_name ) {
62 $location_setting_keys = array(
63 'store_general_service_hours_delivery',
64 'store_general_service_hours_pickup',
65 'store_general_services',
66 'store_general_service_hours_pickup_same',
67 'store_general_asap',
68 'store_general_lead_time',
69 'store_general_preorder',
70 'store_general_calculation_method',
71 'orderable_override_open_hours',
72 );
73
74 if ( ! in_array( $setting_name, $location_setting_keys, true ) ) {
75 return $settings;
76 }
77
78 $location = self::get_main_location();
79
80 $settings['store_general_service_hours_delivery'] = $location->get_service_hours( 'delivery' );
81 $settings['store_general_service_hours_pickup'] = $location->get_service_hours( 'pickup' );
82 $settings['store_general_services'] = $location->get_services();
83 $settings['store_general_service_hours_pickup_same'] = $location->get_pickup_hours_same_as_delivery();
84 $settings['store_general_asap'] = $location->get_asap_settings();
85 $settings['store_general_lead_time'] = $location->get_lead_time();
86 $settings['store_general_preorder'] = $location->get_preorder_days();
87 $settings['store_general_calculation_method'] = $location->get_delivery_calculation_method();
88 $settings['orderable_override_open_hours'] = $location->get_override_default_open_hours();
89
90 return $settings;
91 }
92
93 /**
94 * Check if should return the location setting default value.
95 *
96 * @return boolean
97 */
98 protected static function should_return_default_value() {
99 global $post;
100
101 /**
102 * Since we intercept the `Orderable_Settings::get_setting()` function to keep
103 * compatible with Location feature, we need to check it to avoid hitting the database
104 * early.
105 */
106 if ( is_admin() && ! wp_doing_ajax() && ! function_exists( 'get_current_screen' ) ) {
107 return true;
108 }
109
110 // If it's creating a new Location, return the default value.
111 if ( ! empty( $post->post_status ) && 'auto-draft' === $post->post_status ) {
112 return true;
113 }
114
115 return false;
116 }
117
118 /**
119 * Check if the store has multi locations.
120 *
121 * @return bool
122 */
123 public static function store_has_multi_locations() {
124 return class_exists( 'Orderable_Multi_Location_Pro' );
125 }
126
127 /**
128 * Get the post ID associated with the location.
129 *
130 * @return null|int.
131 */
132 protected static function get_post_id() {
133 global $post;
134
135 $id = null;
136
137 if ( ! self::store_has_multi_locations() ) {
138 return $id;
139 }
140
141 if ( is_checkout() || Orderable_Multi_Location_Pro_Search::is_searching() ) {
142 $location = Orderable_Multi_Location_Pro::get_selected_location_data_from_session();
143
144 return ! empty( $location['id'] ) ? self::get_location_post_id( $location['id'] ) : $id;
145 }
146
147 // We use the post ID when we are editing a location that is not the main one.
148 if (
149 ! empty( $post ) &&
150 'orderable_locations' === $post->post_type
151 ) {
152 $id = $post->ID;
153 }
154
155 return $id;
156 }
157
158 /**
159 * Get the WHERE condition to `location_id` field.
160 *
161 * @param int $location_id The location ID.
162 *
163 * @return string
164 */
165 protected static function location_id_where( $location_id ) {
166 return empty( $location_id ) ? ' AND locations.is_main_location = 1' : ' AND locations.location_id = %d';
167 }
168
169 /**
170 * Get a default open hours.
171 *
172 * This function is useful when the plugin couldn't
173 * retrieve the location open hours from the
174 * database.
175 *
176 * @return array
177 */
178 public static function get_default_open_hours() {
179 $default_day = array(
180 'enabled' => false,
181 'from' => array(
182 'hour' => 9,
183 'minute' => '00',
184 'period' => 'AM',
185 ),
186 'to' => array(
187 'hour' => 5,
188 'minute' => '00',
189 'period' => 'PM',
190 ),
191 'max_orders' => '',
192 );
193
194 $open_hours = array(
195 $default_day,
196 $default_day,
197 $default_day,
198 $default_day,
199 $default_day,
200 $default_day,
201 $default_day,
202 );
203
204 return apply_filters( 'orderable_location_get_default_open_hours', $open_hours );
205 }
206
207 /**
208 * Get the location ID.
209 *
210 * @param int|false $post_id The post ID associated with a Location.
211 *
212 * @return bool|int
213 */
214 public static function get_location_id( $post_id = false ) {
215 global $wpdb;
216
217 static $location_post_ids = array();
218
219 if ( empty( $post_id ) ) {
220 $post_id = self::get_post_id();
221 }
222
223 // If it's empty, we should try to find it again.
224 if ( ! empty( $location_post_ids[ $post_id ] ) ) {
225 return $location_post_ids[ $post_id ];
226 }
227
228 if ( empty( $post_id ) ) {
229 $location_id = $wpdb->get_var(
230 "SELECT
231 location_id
232 FROM
233 {$wpdb->orderable_locations}
234 WHERE
235 is_main_location = 1
236 LIMIT 1
237 "
238 );
239 } else {
240 $location_id = $wpdb->get_var(
241 $wpdb->prepare(
242 "SELECT
243 location_id
244 FROM
245 {$wpdb->orderable_locations}
246 WHERE
247 post_id = %d
248 LIMIT 1
249 ",
250 $post_id
251 )
252 );
253 }
254
255 $location_post_ids[ $post_id ] = empty( $location_id ) ? false : absint( $location_id );
256
257 return $location_post_ids[ $post_id ];
258 }
259
260 /**
261 * Get the post ID associated with a location.
262 *
263 * @return int|false
264 */
265 public static function get_main_location_post_id() {
266 global $wpdb;
267
268 $post_id = $wpdb->get_var(
269 "SELECT
270 post_id
271 FROM
272 {$wpdb->orderable_locations}
273 WHERE
274 is_main_location = 1
275 AND
276 post_id IS NOT NULL
277 LIMIT
278 1
279 "
280 );
281
282 return empty( $post_id ) ? false : (int) $post_id;
283 }
284
285 /**
286 * Check if it's the main location.
287 *
288 * @param int $location_id The location ID.
289 *
290 * @return boolean
291 */
292 public static function is_main_location( $location_id ) {
293 global $wpdb;
294
295 if ( empty( $location_id ) ) {
296 return false;
297 }
298
299 $result = $wpdb->get_var(
300 $wpdb->prepare(
301 "SELECT
302 location_id
303 FROM
304 {$wpdb->orderable_locations}
305 WHERE
306 is_main_location = 1
307 AND
308 location_id = %d
309 LIMIT
310 1
311 ",
312 $location_id
313 )
314 );
315
316 return ! empty( $result );
317 }
318
319 /**
320 * Get the post ID associated with a location.
321 *
322 * @param string $location_id The location ID.
323 *
324 * @return int|false
325 */
326 public static function get_location_post_id( $location_id = '' ) {
327 global $wpdb;
328 static $location_post_ids = array();
329
330 if ( empty( $location_id ) ) {
331 $location_id = self::get_location_id();
332 }
333
334 if ( empty( $location_id ) ) {
335 return false;
336 }
337
338 if ( isset( $location_post_ids[ $location_id ] ) ) {
339 return $location_post_ids[ $location_id ];
340 }
341
342 $post_id = $wpdb->get_var(
343 $wpdb->prepare(
344 "SELECT
345 post_id
346 FROM
347 {$wpdb->orderable_locations}
348 WHERE
349 location_id = %d
350 LIMIT
351 1
352 ",
353 $location_id
354 )
355 );
356
357 $location_post_ids[ $location_id ] = empty( $post_id ) ? false : (int) $post_id;
358
359 return $location_post_ids[ $location_id ];
360 }
361
362 /**
363 * Get the main location ID.
364 *
365 * @return int|false
366 */
367 public static function get_main_location_id() {
368 $cache_key = 'orderable_main_location_id';
369 $main_location_id = wp_cache_get( $cache_key );
370
371 if ( false !== $main_location_id ) {
372 return $main_location_id;
373 }
374
375 global $wpdb;
376
377 $main_location_id = $wpdb->get_var(
378 "SELECT
379 location_id
380 FROM
381 {$wpdb->prefix}orderable_locations
382 WHERE
383 is_main_location = 1
384 LIMIT
385 1
386 "
387 );
388
389 $main_location_id = empty( $main_location_id ) ? false : absint( $main_location_id );
390
391 wp_cache_set( $cache_key, $main_location_id );
392
393 return $main_location_id;
394 }
395
396 /**
397 * Get the main location.
398 *
399 * @return Orderable_Location_Single|false
400 */
401 public static function get_main_location() {
402 $cache_key = 'orderable_main_location';
403 $main_location = wp_cache_get( $cache_key );
404
405 if ( false !== $main_location ) {
406 return new Orderable_Location_Single( $main_location );
407 }
408
409 global $wpdb;
410
411 $main_location = $wpdb->get_row(
412 "SELECT
413 *
414 FROM
415 {$wpdb->prefix}orderable_locations
416 WHERE
417 is_main_location = 1
418 LIMIT
419 1
420 ",
421 ARRAY_A
422 );
423
424 $main_location = empty( $main_location ) ? false : $main_location;
425
426 wp_cache_set( $cache_key, $main_location );
427
428 return new Orderable_Location_Single( $main_location );
429 }
430
431 /**
432 * Get location data.
433 *
434 * @param int $location_id The location ID.
435 *
436 * @return array|false
437 */
438 public static function get_location_data( $location_id ) {
439 $cache_key = 'orderable_get_location_data_' . $location_id;
440 $location = wp_cache_get( $cache_key );
441
442 if ( false !== $location ) {
443 return $location;
444 }
445
446 global $wpdb;
447
448 $location = $wpdb->get_row(
449 $wpdb->prepare(
450 "SELECT
451 *
452 FROM
453 {$wpdb->prefix}orderable_locations
454 WHERE
455 location_id = %d
456 LIMIT
457 1
458 ",
459 $location_id
460 ),
461 ARRAY_A
462 );
463
464 $location = empty( $location ) ? false : $location;
465
466 wp_cache_set( $cache_key, $location );
467
468 return $location;
469 }
470
471 /**
472 * Get the selected location.
473 *
474 * @return Orderable_Location_Single|false
475 */
476 public static function get_selected_location() {
477 /**
478 * Filter the selected location.
479 *
480 * @since 1.8.0
481 * @hook orderable_location_get_selected_location
482 * @param Orderable_Location|false $location The location object.
483 */
484 return apply_filters( 'orderable_location_get_selected_location', self::get_main_location() );
485 }
486
487 /**
488 * Get the selected location ID.
489 *
490 * @return Orderable_Location_Single|false
491 */
492 public static function get_selected_location_id() {
493 /**
494 * Filter the selected location ID.
495 *
496 * @since 1.8.0
497 * @hook orderable_location_get_selected_location_id
498 * @param int|false $location_id The location ID.
499 */
500 return apply_filters( 'orderable_location_get_selected_location_id', self::get_main_location_id() );
501 }
502 }
503