PluginProbe
Orderable – Restaurant & Food Ordering System / 1.19.2
Orderable – Restaurant & Food Ordering System v1.19.2
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 / class-ajax.php

class-ajax.php in Orderable – Restaurant & Food Ordering System 1.19.2, at inc/class-ajax.php

286 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Ajax methods.
4 *
5 * @package Orderable/Classes
6 */
7
8 defined( 'ABSPATH' ) || exit;
9
10 /**
11 * Ajax class.
12 */
13 class Orderable_Ajax {
14 /**
15 * Run.
16 */
17 public static function run() {
18 $methods = array(
19 'get_product_options' => true,
20 'add_to_cart' => true,
21 'get_onboard_woo_fields' => false,
22 'get_cart_item_options' => true,
23 );
24
25 self::add_ajax_methods( $methods, __CLASS__ );
26 }
27
28 /**
29 * Add ajax methods helper.
30 *
31 * @param array $methods
32 * @param object|string $class
33 */
34 public static function add_ajax_methods( $methods, $class ) {
35 if ( empty( $methods ) ) {
36 return;
37 }
38
39 foreach ( $methods as $method => $nopriv ) {
40 add_action( 'wp_ajax_orderable_' . $method, array( $class, $method ) );
41
42 if ( $nopriv ) {
43 add_action( 'wp_ajax_nopriv_orderable_' . $method, array( $class, $method ) );
44 }
45 }
46 }
47
48 /**
49 * Get product options for a variable product.
50 */
51 public static function get_product_options() {
52 $product_id = absint( filter_input( INPUT_POST, 'product_id', FILTER_SANITIZE_NUMBER_INT ) );
53
54 if ( empty( $product_id ) ) {
55 wp_send_json_error();
56 }
57
58 // phpcs:ignore WordPress.Security.NonceVerification
59 $focus = empty( $_POST['focus'] ) ? '' : sanitize_text_field( wp_unslash( $_POST['focus'] ) );
60 $product = wc_get_product( $product_id );
61
62 $response = array(
63 'product_id' => $product_id,
64 'product' => $product,
65 );
66
67 if ( Orderable_Helpers::is_variable_product( $product->get_type() ) ) {
68 $attributes = Orderable_Products::get_available_attributes( $product );
69 $available_variations = $product->get_available_variations();
70 $variations_json = wp_json_encode( $available_variations );
71 }
72
73 $args = array(
74 'images' => true,
75 'focus' => $focus,
76 );
77
78 ob_start();
79
80 include Orderable_Helpers::get_template_path( 'templates/product/options.php' );
81
82 $response['html'] = ob_get_clean();
83
84 wp_send_json_success( $response );
85 }
86
87 /**
88 * Get cart item options for a variable product.
89 */
90 public static function get_cart_item_options() {
91 // phpcs:ignore WordPress.Security.NonceVerification.Missing
92 if ( empty( $_POST['cart_item_key'] ) ) {
93 wp_send_json_error();
94 }
95
96 // phpcs:ignore WordPress.Security.NonceVerification.Missing
97 $cart_item_key = sanitize_text_field( wp_unslash( $_POST['cart_item_key'] ) );
98
99 if ( empty( $cart_item_key ) ) {
100 wp_send_json_error();
101 }
102
103 $cart_item = WC()->cart->get_cart_item( $cart_item_key );
104
105 if ( empty( $cart_item ) ) {
106 wp_send_json_error();
107 }
108
109 $product_id = $cart_item['product_id'];
110 $product = wc_get_product( $product_id );
111
112 $response = array(
113 'product_id' => $product_id,
114 'product' => $product,
115 );
116
117 if ( Orderable_Helpers::is_variable_product( $product->get_type() ) ) {
118 $selected = $cart_item['variation'];
119 $attributes = Orderable_Products::get_available_attributes( $product );
120 $available_variations = $product->get_available_variations();
121 $variations_json = html_entity_decode( wp_json_encode( $available_variations ) );
122 }
123
124 $args = array(
125 'images' => true,
126 );
127
128 add_filter(
129 'orderable_get_group_data',
130 /**
131 * Fill the value of the fields.
132 *
133 * @param array $field_group The field group data.
134 *
135 * @return array
136 */
137 function( $field_group ) use ( $cart_item ) {
138 foreach ( $field_group as $key => $value ) {
139 if ( empty( $cart_item['orderable_fields'][ $value['id'] ] ) ) {
140 continue;
141 }
142
143 switch ( $value['type'] ) {
144 case 'text':
145 $field_group[ $key ]['default'] = $cart_item['orderable_fields'][ $value['id'] ]['value'];
146
147 break;
148
149 case 'select':
150 case 'visual_radio':
151 foreach ( $value['options'] as $key_option => $option ) {
152 if ( $option['label'] === $cart_item['orderable_fields'][ $value['id'] ]['value'] ) {
153 $field_group[ $key ]['options'][ $key_option ]['selected'] = '1';
154
155 break;
156 }
157 }
158
159 break;
160
161 case 'visual_checkbox':
162 foreach ( $value['options'] as $key_option => $option ) {
163 if ( empty( $cart_item['orderable_fields'][ $value['id'] ]['value'] ) ) {
164 continue;
165 }
166
167 $field_value = $cart_item['orderable_fields'][ $value['id'] ]['value'];
168
169 if ( ! is_array( $field_value ) ) {
170 continue;
171 }
172
173 if ( ! in_array( $option['label'], $field_value, true ) ) {
174 $field_group[ $key ]['options'][ $key_option ]['selected'] = '0';
175 continue;
176 }
177
178 $field_group[ $key ]['options'][ $key_option ]['selected'] = '1';
179 }
180
181 break;
182 }
183 }
184
185 return $field_group;
186 }
187 );
188
189 ob_start();
190
191 include ORDERABLE_TEMPLATES_PATH . 'product/options.php';
192
193 $response['html'] = ob_get_clean();
194
195 wp_send_json_success( $response );
196 }
197
198 /**
199 * AJAX add to cart.
200 */
201 public static function add_to_cart() {
202 ob_start();
203
204 // phpcs:disable WordPress.Security.NonceVerification.Missing
205 $product_id = absint( filter_input( INPUT_POST, 'product_id', FILTER_SANITIZE_NUMBER_INT ) );
206
207 if ( empty( $product_id ) ) {
208 return;
209 }
210
211 $variation_id = absint( filter_input( INPUT_POST, 'variation_id', FILTER_SANITIZE_NUMBER_INT ) );
212 $quantity = empty( $_POST['quantity'] ) ? 1 : wc_stock_amount( wp_unslash( $_POST['quantity'] ) );
213 $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity );
214 $product_status = get_post_status( $product_id );
215 $attributes = empty( $_POST['attributes'] ) ? array() : (array) json_decode( sanitize_text_field( wp_unslash( $_POST['attributes'] ) ), true );
216 $attributes = array_map( 'wp_unslash', array_filter( $attributes ) );
217
218 if ( $passed_validation && false !== WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $attributes ) && 'publish' === $product_status ) {
219 do_action( 'woocommerce_ajax_added_to_cart', $product_id );
220 }
221
222 WC_AJAX::get_refreshed_fragments();
223 // phpcs:enable
224 }
225
226 /**
227 * Get countries states.
228 */
229 public static function get_onboard_woo_fields() {
230 $response = array(
231 'default_country' => self::get_default_country_options(),
232 'business_address' => WC()->countries->get_base_address(),
233 'business_address_2' => WC()->countries->get_base_address_2(),
234 'business_city' => WC()->countries->get_base_city(),
235 'business_postcode' => WC()->countries->get_base_postcode(),
236 );
237
238 wp_send_json_success( $response );
239 }
240
241 /**
242 * Get country/state options.
243 *
244 * @return string
245 */
246 public static function get_default_country_options() {
247 $countries_states = Orderable_Settings::get_countries_states();
248
249 if ( empty( $countries_states ) ) {
250 return false;
251 }
252
253 ob_start();
254
255 require ORDERABLE_INC_PATH . '/vendor/iconic-onboard/inc/class-settings.php';
256
257 $base = wc_get_base_location();
258 $default = '';
259
260 if ( isset( $base['country'] ) && isset( $countries_states[ 'country:' . $base['country'] ] ) ) {
261 $default = 'country:' . $base['country'];
262 }
263
264 if ( isset( $base['country'] ) && isset( $base['state'] ) && isset( $countries_states[ $base['country'] ] ) ) {
265 $state = 'state:' . $base['country'] . ':' . $base['state'];
266 if ( isset( $countries_states[ $base['country'] ]['values'][ $state ] ) ) {
267 $default = $state;
268 }
269 }
270
271 Orderable_Onboard_Settings::generate_select_field(
272 array(
273 'id' => 'default_country',
274 'title' => __( 'Country / State', 'orderable' ),
275 'desc' => '',
276 'choices' => $countries_states,
277 'value' => $default,
278 'name' => '',
279 'class' => '',
280 )
281 );
282
283 return strip_tags( ob_get_clean(), '<option><optgroup>' );
284 }
285 }
286