PluginProbe
Orderable – Restaurant & Food Ordering System / 0.2.0
Orderable – Restaurant & Food Ordering System v0.2.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 / services / class-services.php

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

237 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Module: Services.
4 *
5 * Delivery/pickup services.
6 *
7 * @package Orderable/Classes
8 */
9
10 defined( 'ABSPATH' ) || exit;
11
12 /**
13 * Services module class.
14 */
15 class Orderable_Services {
16 /**
17 * Init.
18 */
19 public static function run() {
20 self::load_classes();
21 add_action( 'init', array( __CLASS__, 'add_shortcodes' ) );
22 }
23
24 /**
25 * Load classes for this module.
26 */
27 public static function load_classes() {
28 $classes = array(
29 'services-order' => 'Orderable_Services_Order',
30 );
31
32 foreach ( $classes as $file_name => $class_name ) {
33 require_once ORDERABLE_MODULES_PATH . 'services/class-' . $file_name . '.php';
34
35 $class_name::run();
36 }
37 }
38
39 /**
40 * Add services shrotcodes.
41 */
42 public static function add_shortcodes() {
43 add_shortcode( 'orderable-services', array( __CLASS__, 'orderable_services_shortcode' ) );
44 }
45
46 /**
47 * Services selector shortcode.
48 *
49 * @param array $args
50 * @param string $content
51 * @param string $name
52 *
53 * @return string|void
54 */
55 public static function orderable_services_shortcode( $args = array(), $content = '', $name = '' ) {
56 ob_start();
57 include ORDERABLE_MODULES_PATH . 'services/templates/service-selector.php';
58
59 return ob_get_clean();
60 }
61
62 /**
63 * Get delivery and pickup methods for address.
64 *
65 * @param array $address Array containing address fields.
66 *
67 * @return array
68 */
69 public static function get_delivery_pickup_methods( $address ) {
70 $shipping_methods = array(
71 'delivery' => false,
72 'pickup' => false,
73 'rates' => false,
74 );
75
76 $default_address = array(
77 'country' => WC()->countries->get_base_country(),
78 'state' => WC()->countries->get_base_state(),
79 'postcode' => '',
80 'city' => WC()->countries->get_base_city(),
81 'address' => '',
82 'address_1' => '', // Provide both address and address_1 for backwards compatibility.
83 'address_2' => '',
84 );
85
86 $address = wp_parse_args( $address, $default_address );
87
88 $packages = WC()->shipping()->calculate_shipping( array(
89 array(
90 'contents' => array(),
91 'contents_cost' => 0,
92 'applied_coupons' => array(),
93 'user' => array(
94 'ID' => get_current_user_id(),
95 ),
96 'destination' => $address,
97 'cart_subtotal' => 0,
98 ),
99 ) );
100
101 if ( empty( $packages ) ) {
102 return $shipping_methods;
103 }
104
105 $package = $packages[0];
106 $rates = $package['rates'];
107 $shipping_methods['rates'] = array_keys( $package['rates'] );
108
109 if ( empty( $rates ) ) {
110 return $shipping_methods;
111 }
112
113 foreach ( $rates as $rate_id => $rate ) {
114 $pickup = self::is_pickup_method( $rate );
115 $rate->service = $pickup ? 'pickup' : 'delivery';
116
117 if ( ! $shipping_methods[ $rate->service ] ) {
118 $shipping_methods[ $rate->service ] = $rate;
119 }
120 }
121
122 return $shipping_methods;
123 }
124
125 /**
126 * Is pickup method?
127 *
128 * @param string|WC_Shipping_Method $shipping_method
129 *
130 * @return bool
131 */
132 public static function is_pickup_method( $shipping_method ) {
133 if ( ! $shipping_method ) {
134 return false;
135 }
136
137 if ( ! is_string( $shipping_method ) ) {
138 $shipping_method = $shipping_method->get_method_id();
139 }
140
141 $explode = explode( ':', $shipping_method );
142
143 return in_array( $explode[0], array( 'local_pickup' ) );
144 }
145
146 /**
147 * Get selected service.
148 *
149 * @param bool $label Return the label?
150 *
151 * @return bool|WC_Shipping_Method
152 */
153 public static function get_selected_service( $label = true ) {
154 if ( empty( WC()->session ) ) {
155 return false;
156 }
157
158 $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
159
160 if ( empty( $chosen_methods ) ) {
161 return false;
162 }
163
164 $chosen_method = $chosen_methods[0];
165 $is_pickup = self::is_pickup_method( $chosen_method );
166 $type = $is_pickup ? 'pickup' : 'delivery';
167
168 if ( ! $label ) {
169 return $type;
170 }
171
172 return self::get_service_label( $type );
173 }
174
175 /**
176 * Get service label.
177 *
178 * @param string $type pickup|delivery
179 * @param bool $plural Return the plural label?
180 *
181 * @return bool|string
182 */
183 public static function get_service_label( $type, $plural = false ) {
184 if ( empty( $type ) ) {
185 return false;
186 }
187
188 $type = $plural ? $type . '_plural' : $type;
189
190 $labels = apply_filters( 'orderable_service_labels', array(
191 'pickup' => __( 'Pickup', 'orderable' ),
192 'delivery' => __( 'Delivery', 'orderable' ),
193 'pickup_plural' => __( 'Pickups', 'orderable' ),
194 'delivery_plural' => __( 'Deliveries', 'orderable' ),
195 ) );
196
197 if ( ! isset( $labels[ $type ] ) ) {
198 return false;
199 }
200
201 return $labels[ $type ];
202 }
203
204 /**
205 * How many services are active?
206 *
207 * @return int
208 */
209 public static function get_services_count() {
210 return count( Orderable_Timings::get_services() );
211 }
212
213 /**
214 * Get services on day.
215 *
216 * @param bool|int $timestamp Timestamp of specific day at 00:00am.
217 *
218 * @return array
219 */
220 public static function get_services_on_day( $timestamp = false ) {
221 $services_on_day = array();
222 $services = Orderable_Timings::get_services();
223
224 if ( empty( $services ) ) {
225 return $services_on_day;
226 }
227
228 $day_to_check = absint( date( 'w', $timestamp ) );
229
230 foreach( $services as $service ) {
231 $service_days = Orderable_Timings::get_service_days( $service );
232 $services_on_day[ $service ] = isset( $service_days[ $day_to_check ] ) && ! Orderable_Timings::is_holiday( $timestamp, $service );
233 }
234
235 return $services_on_day;
236 }
237 }