PluginProbe
Welcart e-Commerce / 2.11.35
Welcart e-Commerce v2.11.35
2.12.4 2.12.3 2.11.35 2.12.2 2.12.1 2.11.34 2.11.33 2.11.32 2.11.31 2.11.30 1.3.16 1.3.17 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 All 292 releases
usc-e-shop / functions / system_post.php

system_post.php in Welcart e-Commerce 2.11.35, at functions/system_post.php

973 lines 31.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Welcart System Options
4 *
5 * Functions for system option operations.
6 *
7 * @package Welcart
8 */
9
10 defined( 'ABSPATH' ) || exit;
11
12 /**
13 * Add system option
14 *
15 * @param string $option_name Option name.
16 * @param mixed $newvalue New value.
17 * @return int
18 */
19 function usces_add_system_option( $option_name, $newvalue ) {
20 global $usces;
21
22 $newvalue = $usces->stripslashes_deep_post( $newvalue );
23 $option_value = get_option( $option_name, array() );
24
25 if ( ! empty( $option_value ) && is_array( $option_value ) ) {
26 $option_num = count( $option_value );
27 $unique = true;
28 $sortnull = true;
29 $sort = array();
30 foreach ( (array) $option_value as $value ) {
31 if ( $value['name'] == $newvalue['name'] ) {
32 $unique = false;
33 }
34 if ( ! isset( $value['sort'] ) ) {
35 $sortnull = false;
36 }
37 $sort[] = $value['sort'];
38 }
39 if ( ! $unique ) {
40 return -1;
41 }
42 rsort( $sort );
43 $next_number = reset( $sort ) + 1;
44 $unique_sort = array_unique( $sort );
45 $unique_sort_count = count( $unique_sort );
46 if ( $option_num !== $unique_sort_count || $option_num !== $next_number || ! $sortnull ) {
47 // To repair the sort data.
48 $i = 0;
49 foreach ( $option_value as $opkey => $opvalue ) {
50 $option_value[ $opkey ]['sort'] = $i;
51 $i++;
52 }
53 }
54 }
55 $newvalue['sort'] = ! empty( $option_num ) ? $option_num : 0;
56 $option_value[] = $newvalue;
57 update_option( $option_name, $option_value );
58 krsort( $option_value );
59 reset( $option_value );
60 $last_index = key( $option_value );
61
62 return $last_index;
63 }
64
65 /**
66 * Update system option
67 *
68 * @param string $option_name Option name.
69 * @param int $index Index.
70 * @param mixed $newvalue New value.
71 * @return int
72 */
73 function usces_update_system_option( $option_name, $index, $newvalue ) {
74 global $usces;
75
76 $newvalue = $usces->stripslashes_deep_post( $newvalue );
77 $option_value = get_option( $option_name );
78
79 if ( ! empty( $option_value ) ) {
80 $unique = true;
81 foreach ( (array) $option_value as $value_id => $value ) {
82 if ( $value['name'] == $newvalue['name'] && $value_id != $index ) {
83 $unique = false;
84 break;
85 }
86 }
87 if ( ! $unique ) {
88 return -1;
89 }
90 $option_value[ $index ] = $newvalue;
91 update_option( $option_name, $option_value );
92 $lid = $index;
93 } else {
94 $lid = -1;
95 }
96
97 return $lid;
98 }
99
100 /**
101 * Get system option
102 *
103 * @param string $option_name Option name.
104 * @param string $keyflag Key.
105 * @return array
106 */
107 function usces_get_system_option( $option_name, $keyflag = 'sort' ) {
108 $sysopts = array();
109 $option_value = get_option( $option_name );
110
111 if ( ! is_array( $option_value ) ) {
112 return $sysopts;
113 }
114
115 foreach ( $option_value as $id => $value ) {
116 $key = isset( $value[ $keyflag ] ) ? $value[ $keyflag ] : $value['sort'];
117 switch ( $option_name ) {
118 case 'usces_payment_method':
119 $sysopts[ $key ] = array(
120 'id' => $id,
121 'name' => esc_js( $value['name'] ),
122 'explanation' => wel_esc_script( $value['explanation'] ),
123 'settlement' => $value['settlement'],
124 'module' => $value['module'],
125 'sort' => $value['sort'],
126 'use' => isset( $value['use'] ) ? $value['use'] : 'activate',
127 );
128 break;
129 }
130 }
131 ksort( $sysopts );
132
133 return $sysopts;
134 }
135
136 /**
137 * Sort system option
138 *
139 * @param string $option_name Option name.
140 * @param string $idstr Ids.
141 * @return void
142 */
143 function usces_sort_system_option( $option_name, $idstr ) {
144 $option_value = get_option( $option_name );
145
146 if ( ! empty( $option_value ) ) {
147 $ids = explode( ',', $idstr );
148 $c = 0;
149 foreach ( (array) $ids as $id ) {
150 $option_value[ $id ]['sort'] = $c;
151 $c++;
152 }
153 update_option( $option_name, $option_value );
154 }
155 return;
156 }
157
158 /**
159 * Delete system option
160 *
161 * @param string $option_name Option name.
162 * @param string $id Id.
163 * @return void
164 */
165 function usces_del_system_option( $option_name, $id ) {
166 $option_value = get_option( $option_name );
167
168 if ( ! empty( $option_value ) && isset( $option_value[ $id ] ) && ! empty( $option_value ) ) {
169 unset( $option_value[ $id ] );
170 $op = array();
171 foreach ( (array) $option_value as $opkey => $opvalue ) {
172 $op[ $opvalue['sort'] ] = $opkey;
173 }
174 ksort( $op );
175 $c = 0;
176 foreach ( $op as $opid ) {
177 $option_value[ $opid ]['sort'] = $c;
178 $c++;
179 }
180 update_option( $option_name, $option_value );
181 }
182 return;
183 }
184
185 /**
186 * Payment list
187 *
188 * @param array $option_value Payment values.
189 */
190 function payment_list( $option_value ) {
191 // Exit if no meta.
192 if ( empty( $option_value ) ) :
193 ?>
194 <table id="payment-table" class="list" style="display: none;">
195 <thead>
196 <tr>
197 <th class="hanldh">&emsp;</th>
198 <th class="left"><?php esc_html_e( 'A payment method name', 'usces' ); ?></th>
199 <th><?php esc_html_e( 'explanation', 'usces' ); ?></th>
200 <th><?php esc_html_e( 'Type of payment', 'usces' ); ?></th>
201 <th><?php esc_html_e( 'Payment module', 'usces' ); ?></th>
202 </tr>
203 </thead>
204 <tbody id="payment-list">
205 <tr><td></td><td></td><td></td></tr>
206 </tbody>
207 </table>
208 <?php
209 else :
210 ?>
211 <table id="payment-table" class="list">
212 <thead>
213 <tr>
214 <th class="hanldh">&emsp;</th>
215 <th class="paymentname"><?php esc_html_e( 'A payment method name', 'usces' ); ?></th>
216 <th class="paymentexplanation"><?php esc_html_e( 'explanation', 'usces' ); ?></th>
217 <th class="paymentsettlement"><?php esc_html_e( 'Type of payment', 'usces' ); ?></th>
218 <th class="paymentmodule"><?php esc_html_e( 'Payment module', 'usces' ); ?></th>
219 </tr>
220 </thead>
221 <tbody id="payment-list">
222 <?php
223 foreach ( $option_value as $value ) {
224 echo _payment_list_row( $value );
225 }
226 ?>
227 </tbody>
228 </table>
229 <div id="payment_ajax-response"></div>
230 <?php
231 endif;
232 }
233
234 /**
235 * Payment list row
236 *
237 * @param array $value Payment value.
238 */
239 function _payment_list_row( $value ) {
240
241 if ( empty( $value ) ) {
242 return;
243 }
244
245 $r = '';
246 $style = '';
247 $id = (int) $value['id'];
248 $name = $value['name'];
249 $explanation = ( ! empty( $value['explanation'] ) ) ? $value['explanation'] : '';
250 $settlement = $value['settlement'];
251 $module = ( ! empty( $value['module'] ) ) ? $value['module'] : '';
252 $sort = (int) $value['sort'];
253 $use = ( isset( $value['use'] ) ) ? $value['use'] : 'activate';
254
255 $payment_type = wel_get_payment_type();
256
257 ob_start();
258 ?>
259 <tr class="metastuffrow">
260 <td colspan="5">
261 <table id="payment-<?php echo esc_attr( $id ); ?>" class="metastufftable">
262 <tr>
263 <th class="handlb" rowspan="2">&emsp;</th>
264 <td class="paymentname">
265 <div><input name="payment[<?php echo esc_attr( $id ); ?>][name]" id="payment[<?php echo esc_attr( $id ); ?>][name]" class="metaboxfield" type="text" value="<?php echo esc_attr( $name ); ?>" /></div>
266 <div><input name="payment[<?php echo esc_attr( $id ); ?>][use]" id="payment[<?php echo esc_attr( $id ); ?>][use_activate]" type="radio" value="activate"<?php checked( 'deactivate' !== $use, true ); ?> /><label for="payment[<?php echo esc_attr( $id ); ?>][use_activate]"><?php esc_html_e( 'Activate', 'usces' ); ?></label>&emsp;
267 <input name="payment[<?php echo esc_attr( $id ); ?>][use]" id="payment[<?php echo esc_attr( $id ); ?>][use_deactivate]" type="radio" value="deactivate"<?php checked( 'deactivate' === $use, true ); ?> /><label for="payment[<?php echo esc_attr( $id ); ?>][use_deactivate]"><?php esc_html_e( 'Deactivate', 'usces' ); ?></label></div>
268 </td>
269 <td class="paymentexplanation"><textarea name="payment[<?php echo esc_attr( $id ); ?>][explanation]" id="payment[<?php echo esc_attr( $id ); ?>][explanation]" class="metaboxfield"><?php echo esc_attr( $explanation ); ?></textarea></td>
270 <td class="paymentsettlement">
271 <select name="payment[<?php echo esc_attr( $id ); ?>][settlement]" id="payment[<?php echo esc_attr( $id ); ?>][settlement]" class="metaboxfield">
272 <option value="#NONE#"><?php esc_html_e( '-- Select --', 'usces' ); ?></option>
273 <?php
274 foreach ( $payment_type as $psk => $psv ) :
275 ?>
276 <option value="<?php echo esc_attr( $psk ); ?>"<?php selected( $settlement, $psk ); ?>><?php echo esc_html( $psv ); ?></option>
277 <?php
278 endforeach;
279 ?>
280 </select>
281 </td>
282 <td class="paymentmodule"><div><input name="payment[<?php echo esc_attr( $id ); ?>][module]" id="payment[<?php echo esc_attr( $id ); ?>][module]" class="metaboxfield" type="text" value="<?php echo esc_attr( $module ); ?>" /></div></td>
283 </tr>
284 <tr>
285 <td colspan="4" class="submittd">
286 <div id="paymentsubmit-<?php echo esc_attr( $id ); ?>" class="submit">
287 <input name="deletepayment" id="deletepayment[<?php echo esc_attr( $id ); ?>]" type="button" class="button" value="<?php esc_attr_e( 'Delete' ); ?>" onclick="payment.post('del', <?php echo esc_attr( $id ); ?>);" />
288 <input name="updatepayment" id="updatepayment[<?php echo esc_attr( $id ); ?>]" type="button" class="button" value="<?php esc_attr_e( 'Update' ); ?>" onclick="payment.post('update', <?php echo esc_attr( $id ); ?>);" />
289 <input name="payment[<?php echo esc_attr( $id ); ?>][sort]" id="payment[<?php echo esc_attr( $id ); ?>][sort]" type="hidden" value="<?php echo esc_attr( $sort ); ?>" />
290 </div>
291 <div id="payment_loading-<?php echo esc_attr( $id ); ?>" class="meta_submit_loading"></div>
292 </td>
293 </tr>
294 </table>
295 </td>
296 </tr>
297 <?php
298 $r = ob_get_contents();
299 ob_end_clean();
300 return $r;
301 }
302
303 /**
304 * Add payment form
305 */
306 function payment_form() {
307 $payment_type = wel_get_payment_type();
308 ?>
309 <p><strong><?php esc_html_e( 'Add a new method forpayment ', 'usces' ); ?> : </strong></p>
310 <table id="newmeta2">
311 <thead>
312 <tr>
313 <th class="left"><?php esc_html_e( 'A payment method name', 'usces' ); ?></th>
314 <th><?php esc_html_e( 'explanation', 'usces' ); ?></th>
315 <th><?php esc_html_e( 'Type of payment', 'usces' ); ?></th>
316 <th><?php esc_html_e( 'Payment module', 'usces' ); ?></th>
317 </tr>
318 </thead>
319 <tbody>
320 <tr>
321 <td class="paymentname"><input type="text" id="newname" name="newname" class="metaboxfield" tabindex="7" value="" /></td>
322 <td class="paymentexplanation"><textarea id="newexplanation" name="newexplanation" class="metaboxfield"></textarea></td>
323 <td class="paymentsettlement">
324 <select name="newsettlement" id="newsettlement" class="metaboxfield">
325 <option value="#NONE#"><?php esc_html_e( '-- Select --', 'usces' ); ?></option>
326 <?php foreach ( $payment_type as $psk => $psv ) : ?>
327 <option value="<?php echo esc_attr( $psk ); ?>"><?php echo esc_html( $psv ); ?></option>
328 <?php endforeach; ?>
329 </select>
330 </td>
331 <td class="paymentmodule"><input type="text" id="newmodule" name="newmodule" class="metaboxfield" tabindex="9" value="" /></td>
332 </tr>
333 <tr>
334 <td colspan="4" class="submittd">
335 <div id="newpaymentsubmit" class="submit"><input name="add_payment" type="button" class="button" id="add_payment" tabindex="9" value="<?php esc_attr_e( 'Add a new method forpayment ', 'usces' ); ?>" onclick="payment.post('add', 0);" /></div>
336 <div id="newpayment_loading" class="meta_submit_loading"></div>
337 </td>
338 </tr>
339 </tbody>
340 </table>
341 <?php
342 }
343
344 /**
345 * Payment Type
346 *
347 * @return array
348 */
349 function wel_get_payment_type() {
350 global $usces, $payment_structure;
351
352 $payment_type = $usces->payment_structure;
353 foreach ( $payment_structure as $key => $value ) {
354 if ( ! array_key_exists( $key, $payment_type ) ) {
355 $payment_type[ $key ] = $value;
356 }
357 }
358 return $payment_type;
359 }
360
361 /**
362 * Payment Method Ajax
363 */
364 function payment_ajax() {
365
366 check_admin_referer( 'admin_setup', 'wc_nonce' );
367 if ( ! current_user_can( 'wel_manage_setting' ) ) {
368 wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
369 }
370
371 $id = null;
372
373 if ( 'payment_ajax' !== wp_unslash( $_POST['action'] ) ) {
374 die(0);
375 }
376
377 if ( isset( $_POST['update'] ) ) {
378 $id = up_payment_method();
379 } elseif ( isset( $_POST['delete'] ) ) {
380 $id = del_payment_method();
381 } elseif ( isset( $_POST['sort'] ) ) {
382 sort_payment_method();
383 } else {
384 $id = add_payment_method();
385 }
386
387 $option_value = usces_get_system_option( 'usces_payment_method', 'sort' );
388 $r = '';
389 foreach ( $option_value as $value ) {
390 $r .= _payment_list_row( $value );
391 }
392 $response = array(
393 'meta_id' => $id,
394 'meta_row' => $r,
395 );
396
397 wp_send_json( $response );
398 }
399
400 /**
401 * Add payment
402 *
403 * @return mixed
404 */
405 function add_payment_method() {
406 $newvalue['name'] = isset( $_POST['newname'] ) ? trim( wp_unslash( $_POST['newname'] ) ) : '';
407 $newvalue['explanation'] = isset( $_POST['newexplanation'] ) ? trim( wp_unslash( $_POST['newexplanation'] ) ) : '';
408 $newvalue['settlement'] = isset( $_POST['newsettlement'] ) ? wp_unslash( $_POST['newsettlement'] ) : '';
409 $newvalue['module'] = isset( $_POST['newmodule'] ) ? trim( wp_unslash( $_POST['newmodule'] ) ) : '';
410 $newvalue = apply_filters( 'usces_filter_add_payment_method', $newvalue );
411 // if ( 'acting_paypal_ec' == $newvalue['settlement'] ) {
412 // $newvalue = usces_add_payment_method_paypal_explanation( $newvalue );
413 // }
414 if ( ! empty( $newvalue['name'] ) ) {
415 $lid = usces_add_system_option( 'usces_payment_method', $newvalue );
416 // if ( USCES_JP && 0 <= $lid && 'acting_paypal_ec' == $newvalue['settlement'] ) {
417 // $lid = usces_add_payment_method_paypal_bank( $newvalue );
418 // }
419 return $lid;
420 }
421 return false;
422 }
423
424 /**
425 * Update payment
426 *
427 * @return mixed
428 */
429 function up_payment_method() {
430 $value = array();
431 $id = wp_unslash( $_POST['id'] );
432 $value['name'] = isset( $_POST['name'] ) ? trim( wp_unslash( $_POST['name'] ) ) : '';
433 $value['explanation'] = isset( $_POST['explanation'] ) ? trim( wp_unslash( $_POST['explanation'] ) ) : '';
434 $value['settlement'] = isset( $_POST['settlement'] ) ? wp_unslash( $_POST['settlement'] ) : '';
435 $value['module'] = isset( $_POST['module'] ) ? trim( wp_unslash( $_POST['module'] ) ) : '';
436 $value['sort'] = isset( $_POST['sort'] ) ? (int) wp_unslash( $_POST['sort'] ) : 0;
437 $value['use'] = isset( $_POST['use'] ) ? wp_unslash( $_POST['use'] ) : 'activate';
438 $value = apply_filters( 'usces_filter_up_payment_method', $value );
439
440 if ( ! empty( $value['name'] ) && ! WCUtils::is_blank( $id ) ) {
441
442 $id = usces_update_system_option( 'usces_payment_method', $id, $value );
443
444 return $id;
445 }
446 return -2;
447 }
448
449 /**
450 * Delete payment
451 *
452 * @return mixed
453 */
454 function del_payment_method() {
455
456 $id = wp_unslash( $_POST['id'] );
457
458 if ( ! WCUtils::is_blank( $id ) ) {
459
460 usces_del_system_option( 'usces_payment_method', $id );
461
462 return $id;
463 }
464 return false;
465 }
466
467 /**
468 * Sort payment method
469 *
470 * @return mixed
471 */
472 function sort_payment_method() {
473
474 return usces_sort_system_option( 'usces_payment_method', wp_unslash( $_POST['idstr'] ) );
475 }
476
477 /**
478 * Add delivery method
479 *
480 * @return mixed
481 */
482 function add_delivery_method() {
483 $options = get_option( 'usces', array() );
484 $name = trim( esc_js( $_POST['name'] ) );
485 $ids = array();
486 foreach ( (array) $options['delivery_method'] as $deli ) {
487 $ids[] = (int) $deli['id'];
488 }
489 if ( ! empty( $ids ) ) {
490 rsort( $ids );
491 $newid = $ids[0] + 1;
492 } else {
493 $newid = 0;
494 }
495 $index = ( isset( $options['delivery_method'] ) && is_array( $options['delivery_method'] ) ) ? count( $options['delivery_method'] ) : 0;
496
497 $time = wel_esc_script( str_replace( "\r\n", "\n", wp_unslash( $_POST['timeband'] ) ) );
498 $time = str_replace( "\r", "\n", $time );
499 $time_array = explode( "\n", $time );
500 $times = array();
501 foreach ( (array) $time_array as $val ) {
502 $times[] = esc_js( stripslashes( $val ) );
503 }
504
505 $options['delivery_method'][ $index ]['id'] = $newid;
506 $options['delivery_method'][ $index ]['name'] = esc_js( $name );
507 $options['delivery_method'][ $index ]['time'] = implode( "\n", $times );
508 $options['delivery_method'][ $index ]['charge'] = (int) esc_js( $_POST['charge'] );
509 $options['delivery_method'][ $index ]['days'] = (int) esc_js( $_POST['days'] );
510 $options['delivery_method'][ $index ]['nocod'] = esc_js( $_POST['nocod'] );
511 $options['delivery_method'][ $index ]['intl'] = esc_js( $_POST['intl'] );
512 $options['delivery_method'][ $index ]['cool_category'] = (int) esc_js( $_POST['cool_category'] );
513 update_option( 'usces', $options );
514
515 $response = array(
516 'id' => $newid,
517 'name' => esc_js( stripslashes( $name ) ),
518 'time' => implode( "\n", $times ),
519 'charge' => $options['delivery_method'][ $index ]['charge'],
520 'days' => $options['delivery_method'][ $index ]['days'],
521 'nocod' => $options['delivery_method'][ $index ]['nocod'],
522 'intl' => $options['delivery_method'][ $index ]['intl'],
523 'cool_category' => $options['delivery_method'][ $index ]['cool_category'],
524 );
525
526 return json_encode( $response );
527 }
528
529 /**
530 * Update delivery method
531 *
532 * @return mixed
533 */
534 function update_delivery_method() {
535 $options = get_option( 'usces', array() );
536 $name = trim( esc_js( $_POST['name'] ) );
537 $id = (int) esc_js( $_POST['id'] );
538 $charge = (int) esc_js( $_POST['charge'] );
539 $index = 0;
540 $ct = ( isset( $options['delivery_method'] ) && is_array( $options['delivery_method'] ) ) ? count( $options['delivery_method'] ) : 0;
541 for ( $i = 0; $i < $ct; $i++ ) {
542 if ( $options['delivery_method'][ $i ]['id'] === $id ) {
543 $index = $i;
544 }
545 }
546 $time = wel_esc_script( str_replace( "\r\n", "\n", wp_unslash( $_POST['timeband'] ) ) );
547 $time = str_replace( "\r", "\n", $time );
548 $time_array = explode( "\n", $time );
549 $times = array();
550 foreach ( (array) $time_array as $val ) {
551 $times[] = esc_js( stripslashes( $val ) );
552 }
553
554 $options['delivery_method'][ $index ]['name'] = esc_js( $name );
555 $options['delivery_method'][ $index ]['charge'] = $charge;
556 $options['delivery_method'][ $index ]['time'] = implode( "\n", $times );
557 $options['delivery_method'][ $index ]['days'] = (int) esc_js( $_POST['days'] );
558 $options['delivery_method'][ $index ]['nocod'] = esc_js( $_POST['nocod'] );
559 $options['delivery_method'][ $index ]['intl'] = esc_js( $_POST['intl'] );
560 $options['delivery_method'][ $index ]['cool_category'] = (int) esc_js( $_POST['cool_category'] );
561 update_option( 'usces', $options );
562
563 $response = array(
564 'id' => $id,
565 'name' => esc_js( stripslashes( $name ) ),
566 'time' => implode( "\n", $times ),
567 'charge' => $options['delivery_method'][ $index ]['charge'],
568 'days' => $options['delivery_method'][ $index ]['days'],
569 'nocod' => $options['delivery_method'][ $index ]['nocod'],
570 'intl' => $options['delivery_method'][ $index ]['intl'],
571 'cool_category' => $options['delivery_method'][ $index ]['cool_category'],
572 );
573
574 return json_encode( $response );
575 }
576
577 /**
578 * Delete delivery method
579 *
580 * @return mixed
581 */
582 function delete_delivery_method() {
583 $options = get_option( 'usces', array() );
584 $id = (int) wp_unslash( $_POST['id'] );
585 $index = 0;
586 $ct = ( isset( $options['delivery_method'] ) && is_array( $options['delivery_method'] ) ) ? count( $options['delivery_method'] ) : 0;
587 for ( $i = 0; $i < $ct; $i++ ) {
588 if ( $options['delivery_method'][ $i ]['id'] === $id ) {
589 $index = $i;
590 }
591 }
592 array_splice( $options['delivery_method'], $index, 1 );
593 update_option( 'usces', $options );
594
595 $response = array( 'id' => $id );
596
597 return json_encode( $response );
598 }
599
600 /**
601 * Moveup delivery method
602 *
603 * @return mixed
604 */
605 function moveup_delivery_method() {
606 $options = get_option( 'usces', array() );
607 $selected_id = (int) wp_unslash( $_POST['id'] );
608 $index = 0;
609 $ct = ( isset( $options['delivery_method'] ) && is_array( $options['delivery_method'] ) ) ? count( $options['delivery_method'] ) : 0;
610 for ( $i = 0; $i < $ct; $i++ ) {
611 if ( $options['delivery_method'][ $i ]['id'] === $selected_id ) {
612 $index = $i;
613 }
614 }
615 if ( 0 !== $index ) {
616 $from_index = $index;
617 $to_index = $index - 1;
618 $from_dm = $options['delivery_method'][ $from_index ];
619 $to_dm = $options['delivery_method'][ $to_index ];
620 for ( $i = 0; $i < $ct; $i++ ) {
621 if ( $i === $to_index ) {
622 $options['delivery_method'][ $i ] = $from_dm;
623 } elseif ( $i === $from_index ) {
624 $options['delivery_method'][ $i ] = $to_dm;
625 }
626 }
627 update_option( 'usces', $options );
628 }
629
630 $id = '';
631 $name = '';
632 $charge = '';
633 $time = '';
634 $days = '';
635 $nocod = '';
636 $intl = '';
637 $cool_category = '';
638 for ( $i = 0; $i < $ct; $i++ ) {
639 $id .= $options['delivery_method'][ $i ]['id'] . ',';
640 $name .= $options['delivery_method'][ $i ]['name'] . ',';
641 $charge .= $options['delivery_method'][ $i ]['charge'] . ',';
642 $time .= $options['delivery_method'][ $i ]['time'] . ',';
643 $days .= $options['delivery_method'][ $i ]['days'] . ',';
644 $nocod .= $options['delivery_method'][ $i ]['nocod'] . ',';
645 $intl .= $options['delivery_method'][ $i ]['intl'] . ',';
646 $cool_category .= isset( $options['delivery_method'][ $i ]['cool_category'] ) ? ( $options['delivery_method'][ $i ]['cool_category'] . ',' ) : '0,';
647 }
648
649 $response = array(
650 'id' => rtrim( $id, ',' ),
651 'name' => rtrim( $name, ',' ),
652 'time' => rtrim( $time, ',' ),
653 'charge' => rtrim( $charge, ',' ),
654 'days' => rtrim( $days, ',' ),
655 'nocod' => rtrim( $nocod, ',' ),
656 'intl' => rtrim( $intl, ',' ),
657 'cool_category' => rtrim( $cool_category, ',' ),
658 'selected' => $selected_id,
659 );
660
661 return json_encode( $response );
662 }
663
664 /**
665 * Movedown delivery method
666 *
667 * @return mixed
668 */
669 function movedown_delivery_method() {
670 $options = get_option( 'usces', array() );
671 $selected_id = (int) wp_unslash( $_POST['id'] );
672 $index = 0;
673 $ct = ( isset( $options['delivery_method'] ) && is_array( $options['delivery_method'] ) ) ? count( $options['delivery_method'] ) : 0;
674 for ( $i = 0; $i < $ct; $i++ ) {
675 if ( $options['delivery_method'][ $i ]['id'] === $selected_id ) {
676 $index = $i;
677 }
678 }
679 if ( $index < $ct - 1 ) {
680 $from_index = $index;
681 $to_index = $index + 1;
682 $from_dm = $options['delivery_method'][ $from_index ];
683 $to_dm = $options['delivery_method'][ $to_index ];
684 for ( $i = 0; $i < $ct; $i++ ) {
685 if ( $i === $to_index ) {
686 $options['delivery_method'][ $i ] = $from_dm;
687 } elseif ( $i === $from_index ) {
688 $options['delivery_method'][ $i ] = $to_dm;
689 }
690 }
691 update_option( 'usces', $options );
692 }
693
694 $id = '';
695 $name = '';
696 $charge = '';
697 $time = '';
698 $days = '';
699 $nocod = '';
700 $intl = '';
701 $cool_category = '';
702 for ( $i = 0; $i < $ct; $i++ ) {
703 $id .= $options['delivery_method'][ $i ]['id'] . ',';
704 $name .= $options['delivery_method'][ $i ]['name'] . ',';
705 $charge .= $options['delivery_method'][ $i ]['charge'] . ',';
706 $time .= $options['delivery_method'][ $i ]['time'] . ',';
707 $days .= $options['delivery_method'][ $i ]['days'] . ',';
708 $nocod .= $options['delivery_method'][ $i ]['nocod'] . ',';
709 $intl .= $options['delivery_method'][ $i ]['intl'] . ',';
710 $cool_category .= isset( $options['delivery_method'][ $i ]['cool_category'] ) ? ( $options['delivery_method'][ $i ]['cool_category'] . ',' ) : '0,';
711 }
712 $response = array(
713 'id' => rtrim( $id, ',' ),
714 'name' => rtrim( $name, ',' ),
715 'time' => rtrim( $time, ',' ),
716 'charge' => rtrim( $charge, ',' ),
717 'days' => rtrim( $days, ',' ),
718 'nocod' => rtrim( $nocod, ',' ),
719 'intl' => rtrim( $intl, ',' ),
720 'cool_category' => rtrim( $cool_category, ',' ),
721 'selected' => $selected_id,
722 );
723
724 return json_encode( $response );
725 }
726
727 /**
728 * Add shipping charge
729 *
730 * @return string
731 */
732 function add_shipping_charge() {
733 $options = get_option( 'usces', array() );
734 $name = trim( wp_unslash( $_POST['name'] ) );
735 $ids = array();
736 foreach ( (array) $options['shipping_charge'] as $charge ) {
737 $ids[] = (int) $charge['id'];
738 }
739 if ( ! empty( $ids ) ) {
740 rsort( $ids );
741 $newid = $ids[0] + 1;
742 } else {
743 $newid = 0;
744 }
745 $index = ( isset( $options['shipping_charge'] ) && is_array( $options['shipping_charge'] ) ) ? count( $options['shipping_charge'] ) : 0;
746 $target_market = ( isset( $options['system']['target_market'] ) && ! empty( $options['system']['target_market'] ) ) ? $options['system']['target_market'] : usces_get_local_target_market();
747 foreach ( (array) $target_market as $tm ) {
748 $prefs = get_usces_states( $tm );
749 array_shift( $prefs );
750 $value = wp_unslash( $_POST[ 'value_' . $tm ] );
751
752 $options['shipping_charge'][ $index ]['id'] = $newid;
753 $options['shipping_charge'][ $index ]['name'] = esc_js( $name );
754 $prefs_count = count( $prefs );
755 for ( $i = 0; $i < $prefs_count; $i++ ) {
756 $options['shipping_charge'][ $index ][ $tm ][ $prefs[ $i ] ] = (float) $value[ $i ];
757 }
758 }
759 update_option( 'usces', $options );
760
761 $res = (string) $newid;
762 return $res;
763 }
764
765 /**
766 * Update shipping charge
767 *
768 * @return string
769 */
770 function update_shipping_charge() {
771 $options = get_option( 'usces', array() );
772 $name = trim( wp_unslash( $_POST['name'] ) );
773 $id = (int) wp_unslash( $_POST['id'] );
774 $index = 0;
775
776 $shipping_charge_count = ( isset( $options['shipping_charge'] ) && is_array( $options['shipping_charge'] ) ) ? count( $options['shipping_charge'] ) : 0;
777 for ( $i = 0; $i < $shipping_charge_count; $i++ ) {
778 if ( $options['shipping_charge'][ $i ]['id'] === $id ) {
779 $index = $i;
780 }
781 }
782 $options['shipping_charge'][ $index ]['name'] = esc_js( $name );
783 $target_market = ( isset( $options['system']['target_market'] ) && ! empty( $options['system']['target_market'] ) ) ? $options['system']['target_market'] : usces_get_local_target_market();
784 foreach ( (array) $target_market as $tm ) {
785 $prefs = get_usces_states( $tm );
786 array_shift( $prefs );
787 $value = wp_unslash( $_POST[ 'value_' . $tm ] );
788 $prefs_count = count( $prefs );
789 for ( $i = 0; $i < $prefs_count; $i++ ) {
790 $options['shipping_charge'][ $index ][ $tm ][ $prefs[ $i ] ] = (float) $value[ $i ];
791 }
792 }
793 $options = apply_filters( 'usces_filter_update_shipping_charge', $options, $index, $name, $id );
794
795 update_option( 'usces', $options );
796
797 $res = (string) $id;
798 return $res;
799 }
800
801 /**
802 * Delete shipping charge
803 *
804 * @return string
805 */
806 function delete_shipping_charge() {
807 $options = get_option( 'usces', array() );
808 $id = (int) wp_unslash( $_POST['id'] );
809 $index = 0;
810
811 $shipping_charge_count = ( isset( $options['shipping_charge'] ) && is_array( $options['shipping_charge'] ) ) ? count( $options['shipping_charge'] ) : 0;
812 for ( $i = 0; $i < $shipping_charge_count; $i++ ) {
813 if ( $options['shipping_charge'][ $i ]['id'] === $id ) {
814 $index = $i;
815 }
816 }
817 array_splice( $options['shipping_charge'], $index, 1 );
818 update_option( 'usces', $options );
819
820 $res = (string) $id;
821 return $res;
822 }
823
824 /**
825 * Add delivery days
826 *
827 * @return string
828 */
829 function add_delivery_days() {
830 $options = get_option( 'usces', array() );
831 $name = trim( wp_unslash( $_POST['name'] ) );
832 $ids = array();
833 foreach ( (array) $options['delivery_days'] as $charge ) {
834 $ids[] = (int) $charge['id'];
835 }
836 if ( ! empty( $ids ) ) {
837 rsort( $ids );
838 $newid = $ids[0] + 1;
839 } else {
840 $newid = 0;
841 }
842 $index = ( isset( $options['delivery_days'] ) && is_array( $options['delivery_days'] ) ) ? count( $options['delivery_days'] ) : 0;
843 $target_market = ( isset( $options['system']['target_market'] ) && ! empty( $options['system']['target_market'] ) ) ? $options['system']['target_market'] : usces_get_local_target_market();
844 foreach ( (array) $target_market as $tm ) {
845 $prefs = get_usces_states( $tm );
846 array_shift( $prefs );
847 $value = wp_unslash( $_POST[ 'value_' . $tm ] );
848
849 $options['delivery_days'][ $index ]['id'] = $newid;
850 $options['delivery_days'][ $index ]['name'] = esc_js( $name );
851 $prefs_count = count( $prefs );
852 for ( $i = 0; $i < $prefs_count; $i++ ) {
853 $options['delivery_days'][ $index ][ $tm ][ $prefs[ $i ] ] = (int) $value[ $i ];
854 }
855 }
856 update_option( 'usces', $options );
857
858 $res = (string) $newid;
859 return $res;
860 }
861
862 /**
863 * Update delivery days
864 *
865 * @return string
866 */
867 function update_delivery_days() {
868 $options = get_option( 'usces', array() );
869 $name = trim( wp_unslash( $_POST['name'] ) );
870 $id = (int) wp_unslash( $_POST['id'] );
871 $index = 0;
872
873 $delivery_days_count = ( isset( $options['delivery_days'] ) && is_array( $options['delivery_days'] ) ) ? count( $options['delivery_days'] ) : 0;
874 for ( $i = 0; $i < $delivery_days_count; $i++ ) {
875 if ( $options['delivery_days'][ $i ]['id'] === $id ) {
876 $index = $i;
877 }
878 }
879 $options['delivery_days'][ $index ]['name'] = esc_js( $name );
880 $target_market = ( isset( $options['system']['target_market'] ) && ! empty( $options['system']['target_market'] ) ) ? $options['system']['target_market'] : usces_get_local_target_market();
881 foreach ( (array) $target_market as $tm ) {
882 $prefs = get_usces_states( $tm );
883 array_shift( $prefs );
884 $value = wp_unslash( $_POST[ 'value_' . $tm ] );
885 $prefs_count = count( $prefs );
886 for ( $i = 0; $i < $prefs_count; $i++ ) {
887 $options['delivery_days'][ $index ][ $tm ][ $prefs[ $i ] ] = (int) $value[ $i ];
888 }
889 }
890 update_option( 'usces', $options );
891
892 $res = (string) $id;
893 return $res;
894 }
895
896 /**
897 * Delete delivery days
898 *
899 * @return string
900 */
901 function delete_delivery_days() {
902 $options = get_option( 'usces', array() );
903 $id = (int) wp_unslash( $_POST['id'] );
904 $index = 0;
905 $delivery_days_count = ( isset( $options['delivery_days'] ) && is_array( $options['delivery_days'] ) ) ? count( $options['delivery_days'] ) : 0;
906 for ( $i = 0; $i < $delivery_days_count; $i++ ) {
907 if ( $options['delivery_days'][ $i ]['id'] === $id ) {
908 $index = $i;
909 }
910 }
911 array_splice( $options['delivery_days'], $index, 1 );
912 update_option( 'usces', $options );
913
914 $res = (string) $id;
915 return $res;
916 }
917
918 /**
919 * Shop Options Ajax
920 *
921 * @return void
922 */
923 function shop_options_ajax() {
924
925 check_admin_referer( 'admin_delivery', 'wc_nonce' );
926 if ( ! current_user_can( 'wel_manage_setting' ) ) {
927 wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
928 }
929
930 if ( isset( $_POST['action'] ) && 'shop_options_ajax' !== wp_unslash( $_POST['action'] ) ) {
931 die(0);
932 }
933
934 $mode = ( isset( $_POST['mode'] ) ) ? wp_unslash( $_POST['mode'] ) : '';
935 switch ( $mode ) {
936 case 'add_delivery_method':
937 $res = add_delivery_method();
938 break;
939 case 'update_delivery_method':
940 $res = update_delivery_method();
941 break;
942 case 'delete_delivery_method':
943 $res = delete_delivery_method();
944 break;
945 case 'moveup_delivery_method':
946 $res = moveup_delivery_method();
947 break;
948 case 'movedown_delivery_method':
949 $res = movedown_delivery_method();
950 break;
951 case 'add_shipping_charge':
952 $res = add_shipping_charge();
953 break;
954 case 'update_shipping_charge':
955 $res = update_shipping_charge();
956 break;
957 case 'delete_shipping_charge':
958 $res = delete_shipping_charge();
959 break;
960 case 'add_delivery_days':
961 $res = add_delivery_days();
962 break;
963 case 'update_delivery_days':
964 $res = update_delivery_days();
965 break;
966 case 'delete_delivery_days':
967 $res = delete_delivery_days();
968 break;
969 }
970
971 die( $res );
972 }
973