PluginProbe
PostNL for WooCommerce / 5.9.12
PostNL for WooCommerce v5.9.12
5.9.12 5.9.11 5.9.10 5.9.9 5.9.8 5.9.7 5.9.6 trunk 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.1.4 3.1.5 3.1.6 3.1.7 4.0.0 4.0.1 4.0.2 4.3.2 4.3.3 4.4.0 4.4.1 All 72 releases
woo-postnl / src / Rest_API / Legacy / Checkout / Item_Info.php

Item_Info.php in PostNL for WooCommerce 5.9.12, at src/Rest_API/Legacy/Checkout/Item_Info.php

329 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class Rest_API\Checkout\Item_Info file.
4 *
5 * @package PostNLWooCommerce\Rest_API\Legacy\Checkout
6 */
7
8 namespace PostNLWooCommerce\Rest_API\Legacy\Checkout;
9
10 use PostNLWooCommerce\Address_Utils;
11 use PostNLWooCommerce\Rest_API\Base_Info;
12 use PostNLWooCommerce\Shipping_Method\Settings;
13 use PostNLWooCommerce\Utils;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit;
17 }
18
19 /**
20 * Class Item_Info
21 *
22 * @package PostNLWooCommerce\Rest_API\Legacy\Checkout
23 */
24 class Item_Info extends Base_Info {
25 /**
26 * API args.
27 *
28 * @var api_args
29 */
30 protected $api_args;
31
32 /**
33 * Settings class instance.
34 *
35 * @var PostNLWooCommerce\Shipping_Method\Settings
36 */
37 protected $settings;
38
39 /**
40 * Body of the item info.
41 *
42 * @var body
43 */
44 public $body;
45
46 /**
47 * Shipper data of the item info.
48 *
49 * @var shipper
50 */
51 public $shipper;
52
53 /**
54 * Receiver data of the item info.
55 *
56 * @var receiver
57 */
58 public $receiver;
59
60 /**
61 * Parses the arguments and sets the instance's properties.
62 *
63 * @throws \Exception If some data in $args did not pass validation.
64 */
65 protected function parse_args() {
66
67 $settings = $this->api_args['settings'];
68
69 $settings['current_time'] = $this->get_current_time();
70
71 $this->body = Utils::parse_args( $settings, $this->get_body_info_schema() );
72 $this->receiver = Utils::parse_args( $this->api_args['shipping_address'], $this->get_receiver_info_schema() );
73 $this->shipper = Utils::parse_args( $this->api_args['store_address'], $this->get_store_info_schema() );
74 }
75
76 /**
77 * Method to convert the post data to API args.
78 *
79 * @param Array $post_data Data from post variable in checkout page.
80 */
81 public function convert_data_to_args( $post_data ) {
82 $post_data = Address_Utils::set_post_data_address( $post_data );
83
84 $this->api_args['shipping_address'] = array(
85 'first_name' => ( ! empty( $post_data['shipping_first_name'] ) ) ? $post_data['shipping_first_name'] : '',
86 'last_name' => ( ! empty( $post_data['shipping_last_name'] ) ) ? $post_data['shipping_last_name'] : '',
87 'company' => ( ! empty( $post_data['shipping_company'] ) ) ? $post_data['shipping_company'] : '',
88 'address_1' => ( ! empty( $post_data['shipping_address_1'] ) ) ? $post_data['shipping_address_1'] : '',
89 'address_2' => ( ! empty( $post_data['shipping_address_2'] ) ) ? $post_data['shipping_address_2'] : '',
90 'city' => ( ! empty( $post_data['shipping_city'] ) ) ? $post_data['shipping_city'] : '',
91 'state' => ( ! empty( $post_data['shipping_state'] ) ) ? $post_data['shipping_state'] : '',
92 'country' => ( ! empty( $post_data['shipping_country'] ) ) ? $post_data['shipping_country'] : '',
93 'postcode' => ( ! empty( $post_data['shipping_postcode'] ) ) ? $post_data['shipping_postcode'] : '',
94 );
95 }
96
97 /**
98 * Retrieves the args scheme to use with for parsing store address info.
99 *
100 * @return array
101 */
102 protected function get_body_info_schema() {
103 // Closures in PHP 5.3 do not inherit class context
104 // So we need to copy $this into a lexical variable and pass it to closures manually.
105 $self = $this;
106
107 return array(
108 'location_code' => array(
109 'default' => '',
110 ),
111 'customer_code' => array(
112 'default' => '',
113 ),
114 'customer_num' => array(
115 'default' => '',
116 ),
117 'current_time' => array(
118 'rename' => 'order_date',
119 'error' => __( 'Order date is empty!', 'postnl-for-woocommerce' ),
120 ),
121 'transit_time' => array(
122 'rename' => 'shipping_duration',
123 'error' => __( 'Shipping duration is empty!', 'postnl-for-woocommerce' ),
124 ),
125 'cut_off_time' => array(
126 'default' => '23:00',
127 'validate' => function ( $time ) {
128 $result = preg_match( '/^(?:2[0-4]|[01][1-9]|10):([0-5][0-9])$/', $time );
129 if ( 1 !== $result ) {
130 throw new \Exception(
131 __( 'Wrong format for cut off time!', 'postnl-for-woocommerce' )
132 );
133 }
134 },
135 'sanitize' => function ( $time ) use ( $self ) {
136 return $self->string_length_sanitization( $time, 5 );
137 },
138 ),
139 'dropoff_days' => array(
140 'default' => array(),
141 'sanitize' => function ( $dropoff_days ) use ( $self ) {
142 if ( empty( $dropoff_days ) || ! is_array( $dropoff_days ) ) {
143 return array();
144 }
145
146 $return = array();
147 foreach ( $dropoff_days as $day ) {
148 $return[] = $self->convert_day_to_number( $day );
149 }
150
151 return $return;
152 },
153 ),
154 'excluded_dropoff_days' => array(
155 'default' => array(),
156 'sanitize' => function ( $dropoff_days ) use ( $self ) {
157 if ( empty( $dropoff_days ) || ! is_array( $dropoff_days ) ) {
158 return array();
159 }
160
161 $return = array();
162 foreach ( $dropoff_days as $day ) {
163 $return[] = $self->convert_day_to_number( $day );
164 }
165
166 return $return;
167 },
168 ),
169 'pickup_points_enabled' => array(
170 'default' => false,
171 'sanitize' => function ( $enabled ) {
172 if ( ! is_bool( $enabled ) ) {
173 return false;
174 }
175
176 return $enabled;
177 },
178 ),
179 'delivery_days_enabled' => array(
180 'default' => false,
181 'sanitize' => function ( $enabled ) {
182 if ( ! is_bool( $enabled ) ) {
183 return false;
184 }
185
186 return $enabled;
187 },
188 ),
189 'evening_delivery_enabled' => array(
190 'default' => false,
191 'sanitize' => function ( $enabled ) {
192 if ( ! is_bool( $enabled ) ) {
193 return false;
194 }
195
196 return $enabled;
197 },
198 ),
199 'morning_delivery_enabled' => array(
200 'default' => false,
201 'sanitize' => function ( $enabled ) {
202 if ( ! is_bool( $enabled ) ) {
203 return false;
204 }
205
206 return $enabled;
207 },
208 ),
209 'number_pickup_points' => array(
210 'rename' => 'locations',
211 'default' => 10,
212 'validate' => function ( $value ) {
213 if ( ! is_numeric( $value ) ) {
214 throw new \Exception(
215 __( 'Locations value is not a number!', 'postnl-for-woocommerce' )
216 );
217 }
218 },
219 'sanitize' => function ( $value ) {
220 return intval( $value );
221 },
222 ),
223 'number_delivery_days' => array(
224 'rename' => 'days',
225 'default' => 10,
226 'sanitize' => function ( $value ) {
227 return intval( $value );
228 },
229 ),
230 );
231 }
232
233 /**
234 * Retrieves the args scheme to use with for parsing shipping address info.
235 *
236 * @return array
237 */
238 protected function get_receiver_info_schema() {
239 // Closures in PHP 5.3 do not inherit class context
240 // So we need to copy $this into a lexical variable and pass it to closures manually.
241 $self = $this;
242
243 return array(
244 'address_1' => array(
245 'default' => '',
246 'sanitize' => function ( $value ) use ( $self ) {
247 return $self->string_length_sanitization( $value, 35 );
248 },
249 ),
250 'address_2' => array(
251 'default' => '',
252 'sanitize' => function ( $value ) use ( $self ) {
253 return $self->string_length_sanitization( $value, 5 );
254 },
255 ),
256 'city' => array(
257 'default' => '',
258 ),
259 'postcode' => array(
260 'error' => esc_html__( 'Shipping "Postcode" is empty!', 'postnl-for-woocommerce' ),
261 'sanitize' => function ( $value ) use ( $self ) {
262 $value = str_replace( ' ', '', $value );
263 return $self->string_length_sanitization( $value, 7 );
264 },
265 ),
266 'country' => array(
267 'default' => '',
268 'sanitize' => function ( $value ) use ( $self ) {
269 return $self->string_length_sanitization( $value, 2 );
270 },
271 ),
272 );
273 }
274
275 /**
276 * Convert day string to number.
277 *
278 * @param String $day Three character of day name.
279 *
280 * @return String
281 */
282 public function convert_day_to_number( $day ) {
283 $code = '';
284
285 switch ( $day ) {
286 case 'mon':
287 case 'monday':
288 $code = '01';
289 break;
290
291 case 'tue':
292 case 'tuesday':
293 $code = '02';
294 break;
295
296 case 'wed':
297 case 'wednesday':
298 $code = '03';
299 break;
300
301 case 'thu':
302 case 'thursday':
303 $code = '04';
304 break;
305
306 case 'fri':
307 case 'friday':
308 $code = '05';
309 break;
310
311 case 'sat':
312 case 'saturday':
313 $code = '06';
314 break;
315
316 case 'sun':
317 case 'sunday':
318 $code = '07';
319 break;
320
321 default:
322 $code = '';
323 break;
324 }
325
326 return $code;
327 }
328 }
329