PluginProbe
Orderable – Restaurant & Food Ordering System / 1.19.0
Orderable – Restaurant & Food Ordering System v1.19.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.19.0, at inc/modules/location/class-location.php

505 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 _deprecated_function( __METHOD__, '1.16.0' );
267
268 global $wpdb;
269
270 $post_id = $wpdb->get_var(
271 "SELECT
272 post_id
273 FROM
274 {$wpdb->orderable_locations}
275 WHERE
276 is_main_location = 1
277 AND
278 post_id IS NOT NULL
279 LIMIT
280 1
281 "
282 );
283
284 return empty( $post_id ) ? false : (int) $post_id;
285 }
286
287 /**
288 * Check if it's the main location.
289 *
290 * @param int $location_id The location ID.
291 *
292 * @return boolean
293 */
294 public static function is_main_location( $location_id ) {
295 global $wpdb;
296
297 if ( empty( $location_id ) ) {
298 return false;
299 }
300
301 $result = $wpdb->get_var(
302 $wpdb->prepare(
303 "SELECT
304 location_id
305 FROM
306 {$wpdb->orderable_locations}
307 WHERE
308 is_main_location = 1
309 AND
310 location_id = %d
311 LIMIT
312 1
313 ",
314 $location_id
315 )
316 );
317
318 return ! empty( $result );
319 }
320
321 /**
322 * Get the post ID associated with a location.
323 *
324 * @param string $location_id The location ID.
325 *
326 * @return int|false
327 */
328 public static function get_location_post_id( $location_id = '' ) {
329 global $wpdb;
330 static $location_post_ids = array();
331
332 if ( empty( $location_id ) ) {
333 $location_id = self::get_location_id();
334 }
335
336 if ( empty( $location_id ) ) {
337 return false;
338 }
339
340 if ( isset( $location_post_ids[ $location_id ] ) ) {
341 return $location_post_ids[ $location_id ];
342 }
343
344 $post_id = $wpdb->get_var(
345 $wpdb->prepare(
346 "SELECT
347 post_id
348 FROM
349 {$wpdb->orderable_locations}
350 WHERE
351 location_id = %d
352 LIMIT
353 1
354 ",
355 $location_id
356 )
357 );
358
359 $location_post_ids[ $location_id ] = empty( $post_id ) ? false : (int) $post_id;
360
361 return $location_post_ids[ $location_id ];
362 }
363
364 /**
365 * Get the main location ID.
366 *
367 * @return int|false
368 */
369 public static function get_main_location_id() {
370 $cache_key = 'orderable_main_location_id';
371 $main_location_id = wp_cache_get( $cache_key );
372
373 if ( false !== $main_location_id ) {
374 return $main_location_id;
375 }
376
377 global $wpdb;
378
379 $main_location_id = $wpdb->get_var(
380 "SELECT
381 location_id
382 FROM
383 {$wpdb->prefix}orderable_locations
384 WHERE
385 is_main_location = 1
386 LIMIT
387 1
388 "
389 );
390
391 $main_location_id = empty( $main_location_id ) ? false : absint( $main_location_id );
392
393 wp_cache_set( $cache_key, $main_location_id );
394
395 return $main_location_id;
396 }
397
398 /**
399 * Get the main location.
400 *
401 * @return Orderable_Location_Single|false
402 */
403 public static function get_main_location() {
404 $cache_key = 'orderable_main_location';
405 $main_location = wp_cache_get( $cache_key );
406
407 if ( false !== $main_location ) {
408 return new Orderable_Location_Single( $main_location );
409 }
410
411 global $wpdb;
412
413 $main_location = $wpdb->get_row(
414 "SELECT
415 *
416 FROM
417 {$wpdb->prefix}orderable_locations
418 WHERE
419 is_main_location = 1
420 LIMIT
421 1
422 ",
423 ARRAY_A
424 );
425
426 $main_location = empty( $main_location ) ? false : $main_location;
427
428 wp_cache_set( $cache_key, $main_location );
429
430 return new Orderable_Location_Single( $main_location );
431 }
432
433 /**
434 * Get location data.
435 *
436 * @param int $location_id The location ID.
437 *
438 * @return array|false
439 */
440 public static function get_location_data( $location_id ) {
441 $cache_key = 'orderable_get_location_data_' . $location_id;
442 $location = wp_cache_get( $cache_key );
443
444 if ( false !== $location ) {
445 return $location;
446 }
447
448 global $wpdb;
449
450 $location = $wpdb->get_row(
451 $wpdb->prepare(
452 "SELECT
453 *
454 FROM
455 {$wpdb->prefix}orderable_locations
456 WHERE
457 location_id = %d
458 LIMIT
459 1
460 ",
461 $location_id
462 ),
463 ARRAY_A
464 );
465
466 $location = empty( $location ) ? false : $location;
467
468 wp_cache_set( $cache_key, $location );
469
470 return $location;
471 }
472
473 /**
474 * Get the selected location.
475 *
476 * @return Orderable_Location_Single|false
477 */
478 public static function get_selected_location() {
479 /**
480 * Filter the selected location.
481 *
482 * @since 1.8.0
483 * @hook orderable_location_get_selected_location
484 * @param Orderable_Location|false $location The location object.
485 */
486 return apply_filters( 'orderable_location_get_selected_location', self::get_main_location() );
487 }
488
489 /**
490 * Get the selected location ID.
491 *
492 * @return Orderable_Location_Single|false
493 */
494 public static function get_selected_location_id() {
495 /**
496 * Filter the selected location ID.
497 *
498 * @since 1.8.0
499 * @hook orderable_location_get_selected_location_id
500 * @param int|false $location_id The location ID.
501 */
502 return apply_filters( 'orderable_location_get_selected_location_id', self::get_main_location_id() );
503 }
504 }
505