PluginProbe
Orderable – Restaurant & Food Ordering System / 1.17.1
Orderable – Restaurant & Food Ordering System v1.17.1
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-order.php

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

254 lines 7.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 order.
4 *
5 * @package Orderable/Classes
6 */
7
8 defined( 'ABSPATH' ) || exit;
9
10 /**
11 * Services order class.
12 */
13 class Orderable_Services_Order {
14 /**
15 * Init.
16 */
17 public static function run() {
18 add_action( 'restrict_manage_posts', array( __CLASS__, 'services_filter' ), 50 );
19 add_action( 'woocommerce_order_list_table_restrict_manage_orders', array( __CLASS__, 'services_filter' ), 50 );
20 add_action( 'pre_get_posts', array( __CLASS__, 'update_query_to_filter_admin_orders' ), 100 );
21 add_filter( 'woocommerce_shop_order_list_table_prepare_items_query_args', array( __CLASS__, 'update_query_args_to_filter_admin_orders' ), 100 );
22 add_filter( 'manage_edit-shop_order_columns', array( __CLASS__, 'add_admin_order_columns' ), 10 );
23 add_action( 'manage_shop_order_posts_custom_column', array( __CLASS__, 'add_admin_order_columns_content' ), 10, 2 );
24 add_filter( 'orderable_get_order_date_time', array( __CLASS__, 'modify_order_date_time_labels' ), 10, 2 );
25 add_action( 'woocommerce_before_order_object_save', array( __CLASS__, 'before_save_order' ), 10, 2 );
26
27 // HPOS Compatibility.
28 add_action( 'admin_init', array( __CLASS__, 'add_service_custom_column' ) );
29 }
30
31 /**
32 * Services filter.
33 */
34 public static function services_filter() {
35 $service = self::get_filtered_service();
36
37 $options = [
38 'All services',
39 'delivery',
40 'pickup',
41 ];
42
43 /**
44 * Filter the services filter options.
45 *
46 * @since 1.14.0
47 * @hook orderable_services_filter_options
48 * @param array $options The options.
49 * @param string $filtered_service The filtered service.
50 * @return array New value
51 */
52 $options = apply_filters( 'orderable_services_filter_options', $options, $service );
53
54 if ( ! is_array( $options ) ) {
55 $options = [];
56 }
57
58 ?>
59 <select name="orderable_service">
60 <?php foreach ( $options as $option ) : ?>
61 <option
62 value="<?php echo 'All services' === $option ? '' : esc_attr( $option ); ?>"
63 <?php selected( $service, $option ); ?>
64 >
65 <?php echo esc_html( Orderable_Services::get_service_label( $option ) ? Orderable_Services::get_service_label( $option ) : $option ); ?>
66 </option>
67 <?php endforeach; ?>
68 </select>
69 <?php
70 }
71
72 /**
73 * Get filtered service.
74 *
75 * @return mixed
76 */
77 public static function get_filtered_service() {
78 // phpcs:ignore WordPress.Security.NonceVerification
79 $service = empty( $_GET['orderable_service'] ) ? '' : sanitize_text_field( wp_unslash( $_GET['orderable_service'] ) );
80
81 if ( ! $service ) {
82 return '';
83 }
84
85 return $service;
86 }
87
88 /**
89 * Update the query object to filter orders.
90 *
91 * @param WP_Query $query The query to retrieve the orders.
92 */
93 public static function update_query_to_filter_admin_orders( $query ) {
94 if ( ! Orderable_Orders::is_orders_page() || 'shop_order' !== $query->get( 'post_type' ) ) {
95 return;
96 }
97
98 $meta_query = (array) $query->get( 'meta_query' );
99 $service = self::get_filtered_service();
100
101 if ( ! empty( $service ) ) {
102 $meta_query[] = array(
103 'key' => '_orderable_service_type',
104 'value' => $service,
105 );
106 }
107
108 $query->set( 'meta_query', $meta_query );
109 }
110
111 /**
112 * Update the query args to filter orders.
113 *
114 * @param array $query_args The query args used to retrieve the orders.
115 * @return array
116 */
117 public static function update_query_args_to_filter_admin_orders( $query_args ) {
118 if ( ! Orderable_Orders::is_orders_page() ) {
119 return $query_args;
120 }
121
122 $meta_query = empty( $query_args['meta_query'] ) || ! is_array( $query_args['meta_query'] ) ? array() : $query_args['meta_query'];
123 $service = self::get_filtered_service();
124
125 if ( empty( $service ) ) {
126 return $query_args;
127 }
128
129 $meta_query[] = array(
130 'key' => '_orderable_service_type',
131 'value' => $service,
132 );
133
134 $query_args['meta_query'] = $meta_query; // phpcs:ignore WordPress.DB.SlowDBQuery
135
136 return $query_args;
137 }
138
139 /**
140 * Get service type for an order.
141 *
142 * @param WC_Order $order
143 *
144 * @return bool|string
145 */
146 public static function get_service_type( $order ) {
147 $shipping_methods = $order->get_shipping_methods();
148 $type = false;
149
150 if ( empty( $shipping_methods ) ) {
151 return apply_filters( 'orderable_get_service_type', $type, $order );
152 }
153
154 $shipping_method = array_shift( $shipping_methods );
155 $shipping_method_id = $shipping_method->get_method_id();
156
157 $type = Orderable_Services::is_pickup_method( $shipping_method_id ) ? 'pickup' : 'delivery';
158
159 return apply_filters( 'orderable_get_service_type', $type, $order );
160 }
161
162 /**
163 * Add columns to admin orders screen.
164 *
165 * @param array $columns
166 *
167 * @return array
168 */
169 public static function add_admin_order_columns( $columns ) {
170 $columns['orderable_service_type'] = __( 'Service', 'orderable' );
171
172 return $columns;
173 }
174
175 /**
176 * Add columns content to admin orders screen.
177 *
178 * @param $column_name
179 * @param $post_id
180 */
181 public static function add_admin_order_columns_content( $column_name, $post_id ) {
182 if ( 'orderable_service_type' === $column_name ) {
183 $order = wc_get_order( $post_id );
184 $type = self::get_service_type( $order );
185
186 if ( empty( $type ) ) {
187 echo '&mdash;';
188
189 return;
190 }
191
192 $background = 'pickup' === $type ? '#C6C7E1' : '#c5e2df';
193 $color = 'pickup' === $type ? '#5457a0' : '#356964';
194
195 $background = apply_filters( 'orderable_service_type_column_background_color', $background, $column_name, $post_id, $order, $type );
196 $color = apply_filters( 'orderable_service_type_column_text_color', $color, $column_name, $post_id, $order, $type );
197
198 printf( '<mark class="order-status order-status--%s" style="background-color: %s; color: %s;"><span>%s</span></mark>', esc_attr( $type ), esc_attr( $background ), esc_attr( $color ), Orderable_Services::get_service_label( $type ) );
199 }
200 }
201
202 /**
203 * Modify order date/time labels.
204 *
205 * @param array $labels
206 * @param WC_Order $order
207 *
208 * @return array
209 */
210 public static function modify_order_date_time_labels( $labels, $order ) {
211 $type = self::get_service_type( $order );
212 $type_label = Orderable_Services::get_service_label( $type );
213
214 /* translators: 1: service name; 2: date label. E.g.: "Pickup Date", "Delivery Date" */
215 $labels['order_date']['label'] = sprintf( _x( '%1$s %2$s', 'Order date', 'orderable' ), $type_label, $labels['order_date']['label'] );
216 /* translators: 1: service name; 2: time label. E.g.: "Pickup Time", "Delivery Time" */
217 $labels['order_time']['label'] = sprintf( _x( '%1$s %2$s', 'Order time', 'orderable' ), $type_label, $labels['order_time']['label'] );
218
219 return $labels;
220 }
221
222 /**
223 * Save order meta.
224 *
225 * @param WC_Abstract_Order $abstract_order
226 * @param WC_Data_Store $data_store
227 *
228 * @return void
229 */
230 public static function before_save_order( $abstract_order, $data_store ) {
231 if ( empty( $abstract_order ) ) {
232 return;
233 }
234
235 $type = self::get_service_type( $abstract_order );
236
237 $abstract_order->update_meta_data( '_orderable_service_type', $type );
238 }
239
240 /**
241 * Add Service custom column.
242 *
243 * Compatible with HPOS.
244 *
245 * @return void
246 */
247 public static function add_service_custom_column() {
248 $shop_order_page_screen_id = wc_get_page_screen_id( 'shop-order' );
249
250 add_filter( "manage_{$shop_order_page_screen_id}_columns", array( __CLASS__, 'add_admin_order_columns' ), 10 );
251 add_action( "manage_{$shop_order_page_screen_id}_custom_column", array( __CLASS__, 'add_admin_order_columns_content' ), 10, 2 );
252 }
253 }
254