PluginProbe
Welcart e-Commerce / 2.12.4
Welcart e-Commerce v2.12.4
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 / item_post.php

item_post.php in Welcart e-Commerce 2.12.4, at functions/item_post.php

3,203 lines 119.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Welcart SKU and Options
4 *
5 * Functions for manipulating SKUs and Options in product registration.
6 *
7 * @package Welcart
8 */
9
10 defined( 'ABSPATH' ) || exit;
11
12 /**
13 * Add Item SKU.
14 *
15 * @since 2.3.3
16 * @param string $post_id Post ID.
17 * @param string $new_sku SKU data.
18 * @param boolean $check Switch of check.
19 * @return New meta id.
20 */
21 function usces_add_sku( $post_id, $new_sku, $check = true ) {
22 global $wpdb, $usces;
23
24 if ( $check ) {
25
26 $skus = wel_get_skus( $post_id, 'sort', false );
27
28 if ( ! empty( $skus ) ) {
29
30 $sku_num = count( $skus );
31 $unique = true;
32 $sortnull = true;
33 $sort = array();
34 foreach ( (array) $skus as $sku ) {
35
36 if ( (string) $sku['code'] === (string) $new_sku['code'] ) {
37 $unique = false;
38 }
39 if ( ! isset( $sku['sort'] ) ) {
40 $sortnull = false;
41 }
42 $sort[] = $sku['sort'];
43 }
44 if ( ! $unique ) {
45 return -1;
46 }
47
48 rsort( $sort );
49 $next_number = $sort[0] + 1;
50 $unique_sort = array_unique( $sort );
51 if ( count( $unique_sort ) !== $sku_num || $sku_num != $next_number || ! $sortnull ) {
52 // To repair the sort data.
53 $i = 0;
54 foreach ( (array) $skus as $sku ) {
55 $sku['sort'] = $i;
56 wel_update_sku_data_by_id( $sku['meta_id'], $post_id, $sku );
57 $i++;
58 }
59 }
60 }
61 $new_sku['sort'] = ! empty( $sku_num ) ? $sku_num : 0;
62 }
63 $new_sku = $usces->stripslashes_deep_post( $new_sku );
64 $new_meta_id = wel_add_sku_data( $post_id, $new_sku );
65
66 return $new_meta_id;
67 }
68
69 /**
70 * Add Item SKU by ajax.
71 * Ajax response when a new SKU is registered in the SKU block on the product registration screen.
72 *
73 * @since 2.3.3
74 * @param string $post_ID Post ID.
75 * @return New meta id.
76 */
77 function add_item_sku_meta( $post_ID ) {
78 global $usces;
79
80 $post_id = (int) $post_ID;
81 $value = array();
82 $skus = array();
83 $protected = array( '_wp_attached_file', '_wp_attachment_metadata', '_wp_old_slug', '_wp_page_template' );
84
85 $args = array(
86 'newskuname' => FILTER_DEFAULT,
87 'newskucprice' => FILTER_DEFAULT,
88 'newskuprice' => FILTER_DEFAULT,
89 'newskuzaikonum' => FILTER_DEFAULT,
90 'newskuzaikoselect' => FILTER_DEFAULT,
91 'newskudisp' => FILTER_DEFAULT,
92 'newskuunit' => FILTER_DEFAULT,
93 'newskugptekiyo' => FILTER_DEFAULT,
94 'newskutaxrate' => FILTER_DEFAULT,
95 );
96
97 $inputs = filter_input_array( INPUT_POST, $args );
98
99 $newskuname = ( null !== $inputs['newskuname'] ) ? trim( $inputs['newskuname'] ) : '';
100 $newskucprice = ( null !== $inputs['newskucprice'] ) ? $inputs['newskucprice'] : '';
101 $newskuprice = ( null !== $inputs['newskuprice'] ) ? $inputs['newskuprice'] : '';
102 $newskuzaikonum = ( null !== $inputs['newskuzaikonum'] ) ? $inputs['newskuzaikonum'] : '';
103 $newskuzaikoselect = ( null !== $inputs['newskuzaikoselect'] ) ? $inputs['newskuzaikoselect'] : '';
104 $newskudisp = ( null !== $inputs['newskudisp'] ) ? trim( $inputs['newskudisp'] ) : '';
105 $newskuunit = ( null !== $inputs['newskuunit'] ) ? trim( $inputs['newskuunit'] ) : '';
106 $newskugptekiyo = ( null !== $inputs['newskugptekiyo'] ) ? $inputs['newskugptekiyo'] : '';
107 $newskutaxrate = ( null !== $inputs['newskutaxrate'] ) ? $inputs['newskutaxrate'] : '';
108
109 if ( ! WCUtils::is_blank( $newskuname ) && ! WCUtils::is_blank( $newskuprice ) && ! WCUtils::is_blank( $newskuzaikoselect ) ) {
110
111 if ( in_array( $newskuname, $protected ) ) {
112 return false;
113 }
114
115 $value['code'] = $newskuname;
116 $value['name'] = $newskudisp;
117 $value['cprice'] = $newskucprice;
118 $value['price'] = $newskuprice;
119 $value['unit'] = $newskuunit;
120 $value['stocknum'] = $newskuzaikonum;
121 $value['stock'] = $newskuzaikoselect;
122 $value['gp'] = $newskugptekiyo;
123 $value['taxrate'] = $newskutaxrate;
124
125 $value = apply_filters( 'usces_filter_add_item_sku_meta_value', $value );
126
127 $res = apply_filters( 'usces_filter_before_add_item_sku_meta', false, $post_id, $value );
128 if ( false !== $res ) {
129 return $res;
130 }
131
132 $id = usces_add_sku( $post_ID, $value, true );
133
134 return $id;
135 } else {
136 return false;
137 }
138 }
139
140 /**
141 * Update Item SKU by ajax.
142 * Ajax response when SKU information is updated in the SKU block of the product registration screen.
143 *
144 * @since 2.3.3
145 * @param string $post_ID Post ID.
146 * @return Query result.
147 */
148 function up_item_sku_meta( $post_ID ) {
149 global $wpdb, $usces;
150
151 $post_id = (int) $post_ID;
152 $value = array();
153
154 $skucode = filter_input( INPUT_POST, 'skuname', FILTER_DEFAULT );
155 $skumetaid = filter_input( INPUT_POST, 'skumetaid', FILTER_DEFAULT );
156
157 $res = apply_filters( 'usces_filter_before_up_item_sku_meta', false, $post_ID, $skumetaid, $skucode );
158 if ( false !== $res ) {
159 return $res;
160 }
161
162 $args = array(
163 'skucprice' => FILTER_DEFAULT,
164 'skuprice' => FILTER_DEFAULT,
165 'skuzaikonum' => FILTER_DEFAULT,
166 'skuzaiko' => FILTER_DEFAULT,
167 'skudisp' => FILTER_DEFAULT,
168 'skuunit' => FILTER_DEFAULT,
169 'skugptekiyo' => FILTER_DEFAULT,
170 'sort' => FILTER_DEFAULT,
171 'skutaxrate' => FILTER_DEFAULT,
172 );
173
174 $inputs = filter_input_array( INPUT_POST, $args );
175
176 $skucprice = ( null !== $inputs['skucprice'] ) ? trim( $inputs['skucprice'] ) : 0;
177 $skuprice = ( null !== $inputs['skuprice'] ) ? trim( $inputs['skuprice'] ) : 0;
178 $skuzaikonum = ( null !== $inputs['skuzaikonum'] ) ? trim( $inputs['skuzaikonum'] ) : 0;
179 $skuzaiko = ( null !== $inputs['skuzaiko'] ) ? (int) $inputs['skuzaiko'] : '';
180 $skudisp = ( null !== $inputs['skudisp'] ) ? trim( $inputs['skudisp'] ) : '';
181 $skuunit = ( null !== $inputs['skuunit'] ) ? trim( $inputs['skuunit'] ) : '';
182 $skugptekiyo = ( null !== $inputs['skugptekiyo'] ) ? (int) $inputs['skugptekiyo'] : 0;
183 $skusort = ( null !== $inputs['sort'] ) ? $inputs['sort'] : 0;
184 $skutaxrate = ( null !== $inputs['skutaxrate'] ) ? $inputs['skutaxrate'] : '';
185
186 $value['code'] = $skucode;
187 $value['name'] = $skudisp;
188 $value['cprice'] = $skucprice;
189 $value['price'] = $skuprice;
190 $value['unit'] = $skuunit;
191 $value['stocknum'] = $skuzaikonum;
192 $value['stock'] = $skuzaiko;
193 $value['gp'] = $skugptekiyo;
194 $value['sort'] = $skusort;
195 $value['taxrate'] = $skutaxrate;
196
197 $value = $usces->stripslashes_deep_post( $value );
198
199 $skus = wel_get_skus( $post_id, 'meta_id', false );
200 foreach ( $skus as $sku ) {
201 if ( (string) $sku['code'] === (string) $skucode && $sku['meta_id'] != $skumetaid ) {
202 return -1;
203 }
204 }
205
206 $value = apply_filters( 'usces_filter_up_item_sku_meta_value', $value );
207
208 if ( ! WCUtils::is_blank( $skumetaid ) && ! WCUtils::is_blank( $skucode ) && ! WCUtils::is_blank( $skuprice ) ) {
209
210 $res = wel_update_sku_data_by_id( $skumetaid, $post_id, $value );
211 return $res;
212
213 } else {
214
215 return false;
216 }
217 }
218
219 /**
220 * Delete Item SKU by ajax.
221 * Ajax response when SKU is deleted in the SKU block of the product registration screen.
222 *
223 * @since 2.3.3
224 * @param string $post_ID Post ID.
225 * @return Result.
226 */
227 function del_item_sku_meta( $post_ID ) {
228 global $wpdb, $usces;
229
230 $post_id = (int) $post_ID;
231 $meta_id = filter_input( INPUT_POST, 'skumetaid', FILTER_VALIDATE_INT );
232
233 $res = apply_filters( 'usces_filter_before_del_item_sku_meta', false, $post_id, $meta_id );
234 if ( false !== $res ) {
235 return $res;
236 }
237
238 $res = wel_delete_sku_data_by_id( $meta_id, $post_id );
239
240 // Resort.
241 $skus = wel_get_skus( $post_id, 'meta_id', false );
242
243 if ( ! empty( $skus ) ) {
244 uasort( $skus, function($a, $b) {
245 return $a['sort'] <=> $b['sort'];
246 });
247
248 $i = 0;
249 foreach ( $skus as $sku ) {
250 $sku['sort'] = $i;
251 $meta_id = $sku['meta_id'];
252 wel_update_sku_data_by_id( $meta_id, $post_id, $sku );
253 $i++;
254 }
255 }
256 return;
257 }
258
259 /**
260 * List of Item SKUs.
261 * To generate a list of SKUs in the SKU block of the product registration screen.
262 *
263 * @since 2.3.3
264 * @param string $skus All SKUs.
265 */
266 function list_item_sku_meta( $skus ) {
267
268 if ( empty( $skus ) ) { // Exit if no meta.
269 ?>
270 <table id="skulist-table" class="list" style="display: none;">
271 <thead>
272 <tr>
273 <th class="hanldh" rowspan="2">&emsp;</th>
274 <th><?php esc_html_e( 'SKU code', 'usces' ); ?></th>
275 <th><?php echo apply_filters( 'usces_filter_listprice_label', __( 'normal price', 'usces' ), null, null ); ?>(<?php usces_crcode(); ?>)</th>
276 <th><?php echo apply_filters( 'usces_filter_sellingprice_label', __( 'Sale price', 'usces' ), null, null ); ?>(<?php usces_crcode(); ?>)</th>
277 <th><?php esc_html_e( 'stock', 'usces' ); ?></th>
278 <th><?php esc_html_e( 'stock status', 'usces' ); ?></th><?php echo apply_filters( 'usces_filter_sku_meta_title1', '' ); ?>
279 </tr>
280 </thead>
281 <tbody id="item-sku-list">
282 <tr><td></td><td></td><td></td><td></td><td></td></tr>
283 </tbody>
284 </table>
285 <?php
286 } else {
287 ?>
288 <table id="skulist-table" class="list">
289 <thead>
290 <tr>
291 <th class="hanldh" rowspan="2">&emsp;</th>
292 <th class="item-sku-key"><?php esc_html_e( 'SKU code', 'usces' ); ?></th>
293 <th class="item-sku-cprice"><?php echo apply_filters( 'usces_filter_listprice_label', __( 'normal price', 'usces' ), null, null ); ?>(<?php usces_crcode(); ?>)</th>
294 <th class="item-sku-price"><?php echo apply_filters( 'usces_filter_sellingprice_label', __( 'Sale price', 'usces' ), null, null ); ?>(<?php usces_crcode(); ?>)</th>
295 <th class="item-sku-zaikonum"><?php esc_html_e( 'stock', 'usces' ); ?></th>
296 <th class="item-sku-zaiko"><?php esc_html_e( 'stock status', 'usces' ); ?></th><?php echo apply_filters( 'usces_filter_sku_meta_title1', '' ); ?>
297 </tr>
298 <tr>
299 <th><?php esc_html_e( 'SKU display name ', 'usces' ); ?></th>
300 <th><?php esc_html_e( 'unit', 'usces' ); ?></th>
301 <?php
302 $advance_title = '<th colspan="2">&nbsp;</th>';
303 echo apply_filters( 'usces_filter_sku_meta_form_advance_title', $advance_title );
304 ?>
305 <th><?php esc_html_e( 'Apply business package', 'usces' ); ?></th><?php echo apply_filters( 'usces_filter_sku_meta_title2', '' ); ?>
306 </tr>
307 </thead>
308 <tbody id="item-sku-list">
309 <?php
310 foreach ( $skus as $sku ) {
311 echo _list_item_sku_meta_row( $sku );
312 }
313 ?>
314 </tbody>
315 </table>
316 <?php
317 }
318 }
319
320 /**
321 * Row of Item SKU List.
322 * To generate a list of SKUs in the SKU block of the product registration screen.
323 *
324 * @since 2.3.3
325 * @param string $sku SKU.
326 */
327 function _list_item_sku_meta_row( $sku ) {
328 $r = '';
329 $style = '';
330
331 $key = $sku['code'];
332 $cprice = $sku['cprice'];
333 $price = $sku['price'];
334 $zaikonum = $sku['stocknum'];
335 $zaiko = $sku['stock'];
336 $skudisp = $sku['name'];
337 $skuunit = $sku['unit'];
338 $skugptekiyo = $sku['gp'];
339 $id = (int) $sku['meta_id'];
340 $zaikoselectarray = get_option( 'usces_zaiko_status', array() );
341 $zaikoselectarray = apply_filters( 'usces_filter_zaikoselectarray', $zaikoselectarray );
342 $zaikoselect_count = ( $zaikoselectarray && is_array( $zaikoselectarray ) ) ? count( $zaikoselectarray ) : 0;
343 $sort = (int) $sku['sort'];
344 $sku_colspan = apply_filters( 'usces_filter_sku_meta_colspan', '6' );
345
346 ob_start();
347 ?>
348 <tr class="metastuffrow"><td colspan="<?php echo esc_attr( $sku_colspan ); ?>">
349 <table id="itemsku-<?php echo esc_attr( $id ); ?>" class="metastufftable">
350 <tr>
351 <th class="handlb" rowspan="<?php echo apply_filters( 'usces_filter_sku_meta_rowspan', '3' ); ?>">&emsp;</th>
352 <td class="item-sku-key"><input name="itemsku[<?php echo esc_attr( $id ); ?>][key]" id="itemsku[<?php echo esc_attr( $id ); ?>][key]" class="skuname metaboxfield" type="text" value="<?php echo esc_attr( $key ); ?>" /></td>
353 <td class="item-sku-cprice"><input name="itemsku[<?php echo esc_attr( $id ); ?>][cprice]" id="itemsku[<?php echo esc_attr( $id ); ?>][cprice]" class="skuprice metaboxfield" type="text" value="<?php echo esc_attr( $cprice ); ?>" /></td>
354 <td class="item-sku-price"><input name="itemsku[<?php echo esc_attr( $id ); ?>][price]" id="itemsku[<?php echo esc_attr( $id ); ?>][price]" class="skuprice metaboxfield" type="text" value="<?php echo esc_attr( $price ); ?>" /></td>
355 <td class="item-sku-zaikonum"><input name="itemsku[<?php echo esc_attr( $id ); ?>][zaikonum]" id="itemsku[<?php echo esc_attr( $id ); ?>][zaikonum]" class="skuzaikonum metaboxfield" type="text" value="<?php echo esc_attr( $zaikonum ); ?>" /></td>
356 <td class="item-sku-zaiko">
357 <select id="itemsku[<?php echo esc_attr( $id ); ?>][zaiko]" name="itemsku[<?php echo esc_attr( $id ); ?>][zaiko]" class="skuzaiko metaboxfield">
358 <?php
359 for ( $i = 0; $i < $zaikoselect_count; $i++ ) {
360 ?>
361 <option value="<?php echo esc_attr( $i ); ?>"<?php selected( $zaiko, $i ); ?>><?php echo esc_attr( $zaikoselectarray[ $i ] ); ?></option>
362 <?php
363 }
364 ?>
365 </select>
366 </td><?php echo apply_filters( 'usces_filter_sku_meta_field1', '', $sku ); ?>
367 </tr>
368 <tr>
369 <td class="item-sku-key"><input name="itemsku[<?php echo esc_attr( $id ); ?>][skudisp]" id="itemsku[<?php echo esc_attr( $id ); ?>][skudisp]" class="skudisp metaboxfield" type="text" value="<?php echo esc_attr( $skudisp ); ?>" />
370 </td>
371 <td class="item-sku-cprice"><input name="itemsku[<?php echo esc_attr( $id ); ?>][skuunit]" id="itemsku[<?php echo esc_attr( $id ); ?>][skuunit]" class="skuunit metaboxfield" type="text" value="<?php echo esc_attr( $skuunit ); ?>" /></td>
372 <?php
373 $default_field = "\n\t\t" . '<td colspan="2">&nbsp;</td>';
374 echo apply_filters( 'usces_filter_sku_meta_row_advance', $default_field, $sku );
375 ?>
376 <td class="item-sku-zaiko">
377 <select id="itemsku[<?php echo esc_attr( $id ); ?>][skugptekiyo]" name="itemsku[<?php echo esc_attr( $id ); ?>][skugptekiyo]" class="skugptekiyo metaboxfield">
378 <option value="0"<?php selected( $skugptekiyo, 0 ); ?>><?php esc_html_e( 'Not apply', 'usces' ); ?></option>
379 <option value="1"<?php selected( $skugptekiyo, 1 ); ?>><?php esc_html_e( 'Apply', 'usces' ); ?></option>
380 </select>
381 </td><?php echo apply_filters( 'usces_filter_sku_meta_field2', '', $sku ); ?>
382 </tr>
383 <?php echo apply_filters( 'usces_filter_sku_meta_row', '', $sku ); ?>
384 <tr>
385 <td colspan="<?php echo esc_attr( $sku_colspan - 1 ); ?>" class="submittd">
386 <div id="skusubmit-<?php echo esc_attr( $id ); ?>" class="submit">
387 <input name="deleteitemsku[<?php echo esc_attr( $id ); ?>]" id="deleteitemsku[<?php echo esc_attr( $id ); ?>]" type="button" class="button" value="<?php esc_attr_e( 'Delete' ); ?>" onclick="if( jQuery('#post_ID').val() < 0 ) return; itemSku.post('deleteitemsku', <?php echo esc_attr( $id ); ?>);" />
388 <input name="updateitemsku[<?php echo esc_attr( $id ); ?>]" id="updateitemsku[<?php echo esc_attr( $id ); ?>]" type="button" class="button" value="<?php esc_attr_e( 'Update' ); ?>" onclick="if( jQuery('#post_ID').val() < 0 ) return; itemSku.post('updateitemsku', <?php echo esc_attr( $id ); ?>);" />
389 <input name="itemsku[<?php echo esc_attr( $id ); ?>][sort]" id="itemsku[<?php echo esc_attr( $id ); ?>][sort]" type="hidden" value="<?php echo esc_attr( $sort ); ?>" />
390 <?php usces_sku_meta_row_reduced_taxrate( $sku ); ?>
391 </div>
392 <div id="itemsku_loading-<?php echo esc_attr( $id ); ?>" class="meta_submit_loading"></div>
393 </td>
394 </tr>
395 </table>
396 </td></tr>
397 <?php
398 $r = ob_get_contents();
399 ob_end_clean();
400 return $r;
401 }
402
403 /**
404 * New SKU Form.
405 * The form to add a new SKU in the SKU block on the product registration screen.
406 *
407 * @since 2.3.3
408 */
409 function item_sku_meta_form() {
410 $sku_colspan = apply_filters( 'usces_filter_sku_meta_colspan', '6' );
411 ?>
412 <div id="sku_ajax-response"></div>
413 <p><strong><?php esc_html_e( 'Add new SKU', 'usces' ); ?> : </strong></p>
414 <table id="newsku">
415 <thead>
416 <tr>
417 <th class="left"><?php esc_html_e( 'SKU code', 'usces' ); ?></th>
418 <th><?php echo apply_filters( 'usces_filter_listprice_label', __( 'normal price', 'usces' ), null, null ); ?>(<?php usces_crcode(); ?>)</th>
419 <th><?php echo apply_filters( 'usces_filter_sellingprice_label', __( 'Sale price', 'usces' ), null, null ); ?>(<?php usces_crcode(); ?>)</th>
420 <th><?php esc_html_e( 'stock', 'usces' ); ?></th>
421 <th><?php esc_html_e( 'stock status', 'usces' ); ?></th><?php echo apply_filters( 'usces_filter_sku_meta_title1', '' ); ?>
422 </tr>
423 <tr>
424 <th><?php esc_html_e( 'SKU display name ', 'usces' ); ?></th>
425 <th><?php esc_html_e( 'unit', 'usces' ); ?></th>
426 <?php
427 $advance_title = '<th colspan="2">&nbsp;</th>';
428 echo apply_filters( 'usces_filter_sku_meta_form_advance_title', $advance_title );
429 ?>
430 <th><?php esc_html_e( 'Apply business package', 'usces' ); ?></th><?php echo apply_filters( 'usces_filter_sku_meta_title2', '' ); ?>
431 </tr>
432 </thead>
433 <tbody>
434 <tr>
435 <td id="newskuleft" class="item-sku-key"><input type="text" id="newskuname" name="newskuname" class="newskuname metaboxfield" value="" /></td>
436 <td class="item-sku-cprice"><input type="text" id="newskucprice" name="newskucprice" class="newskuprice metaboxfield" /></td>
437 <td class="item-sku-price"><input type="text" id="newskuprice" name="newskuprice" class="newskuprice metaboxfield" /></td>
438 <td class="item-sku-zaikonum"><input type="text" id="newskuzaikonum" name="newskuzaikonum" class="newskuzaikonum metaboxfield" /></td>
439 <td class="item-sku-zaiko">
440 <select id="newskuzaikoselect" name="newskuzaikoselect" class="newskuzaikoselect metaboxfield">
441 <?php
442 $zaikoselectarray = get_option( 'usces_zaiko_status', array() );
443 foreach ( $zaikoselectarray as $v => $l ) {
444 echo "\n" . '<option value="' . esc_attr( $v ) . '">' . esc_html( $l ) . '</option>';
445 }
446 ?>
447 </select>
448 </td><?php echo apply_filters( 'usces_filter_newsku_meta_field1', '' ); ?>
449 </tr>
450 <tr>
451 <td class="item-sku-key"><input type="text" id="newskudisp" name="newskudisp" class="newskudisp metaboxfield" /></td>
452 <td class="item-sku-cprice"><input type="text" id="newskuunit" name="newskuunit" class="newskuunit metaboxfield" /></td>
453 <?php
454 $advance_field = '<td class="item-sku-price">&nbsp;</td><td class="item-sku-zaikonum">&nbsp;</td>';
455 echo apply_filters( 'usces_filter_sku_meta_form_advance_field', $advance_field );
456 ?>
457 <td class="item-sku-zaiko">
458 <select id="newskugptekiyo" name="newskugptekiyo" class="newskugptekiyo metaboxfield">
459 <option value="0"><?php esc_html_e( 'Not apply', 'usces' ); ?></option>
460 <option value="1"><?php esc_html_e( 'Apply', 'usces' ); ?></option>
461 </select>
462 </td><?php echo apply_filters( 'usces_filter_newsku_meta_field2', '' ); ?>
463 </tr>
464 <?php echo apply_filters( 'usces_filter_newsku_meta_row', '' ); ?>
465 <tr>
466 <td colspan="<?php echo esc_attr( $sku_colspan - 1 ); ?>" class="submittd">
467 <div id="newskusubmit" class="submit">
468 <?php
469 $add_itemsku_button = '<input name="add_itemsku" type="button" class="button" id="add_itemsku" tabindex="9" value="' . esc_html__( 'Add SKU', 'usces' ) . '" onclick="if( jQuery(\'#post_ID\').val() < 0 ) return; itemSku.post(\'additemsku\', 0);" />';
470 echo apply_filters( 'usces_filter_newsku_meta_add_button', $add_itemsku_button );
471 ?>
472 <?php usces_newsku_meta_row_reduced_taxrate(); ?>
473 </div>
474 <div id="newitemsku_loading" class="meta_submit_loading"></div>
475 </td>
476 </tr>
477 </tbody>
478 </table>
479 <?php
480 }
481
482 /**
483 * Get Post Meta using MetaID.
484 *
485 * @since 2.3.3
486 * @param int $meta_id Meta ID.
487 * @return MetaData|false
488 */
489 function usces_get_post_meta_by_metaid( $meta_id ) {
490 global $wpdb;
491
492 $res = $wpdb->get_row(
493 $wpdb->prepare(
494 "SELECT * FROM $wpdb->postmeta WHERE meta_id = %d",
495 $meta_id
496 ),
497 ARRAY_A
498 );
499 return $res;
500 }
501
502 /**
503 * Get Post meta.
504 * Get product metadata by specifying meta_key.
505 *
506 * @since 2.3.3
507 * @param string $post_id Post ID.
508 * @param string $key Meta key.
509 * @param boolean $cache Switch of cache.
510 * @return MetaData|false
511 */
512 function usces_get_post_meta( $post_id, $key, $cache = true ) {
513 global $wpdb;
514
515 $cache_key = 'wel_post_meta_' . $post_id . '_' . $key;
516 if ( true === $cache ) {
517 $meta_data = wp_cache_get( $cache_key );
518 } else {
519 $meta_data = false;
520 }
521 if ( false === $meta_data || is_admin() || wp_doing_ajax() ) {
522 $meta_data = $wpdb->get_results(
523 $wpdb->prepare(
524 "SELECT * FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = %s",
525 $post_id,
526 $key
527 ),
528 ARRAY_A
529 );
530 if ( null !== $meta_data ) {
531 wp_cache_set( $cache_key, $meta_data );
532 }
533 }
534
535 if ( null === $meta_data ) {
536 return false;
537 } else {
538 return $meta_data;
539 }
540 }
541
542 /**
543 * Sort Options.
544 * Ajax response when changing the order of optios in the SKU block of the product registration screen.
545 *
546 * @since 2.3.3
547 * @param int $post_id Post ID.
548 * @param int $metastr Meta ids.
549 */
550 function usces_sort_item_opts( $post_id, $metastr ) {
551 global $wpdb;
552
553 $meta_ids = explode( ',', $metastr );
554 $opts = wel_get_opts( $post_id, 'meta_id', false );
555
556 if ( ! empty( $meta_ids ) ) {
557
558 $i = 0;
559 foreach ( $meta_ids as $meta_id ) {
560
561 $opt = $opts[ $meta_id ];
562 $opt['sort'] = $i;
563
564 wel_update_opt_data_by_id( $meta_id, $post_id, $opt );
565
566 $i++;
567 }
568 }
569 }
570
571 /**
572 * Sort SKUs.
573 * Ajax response when changing the order of SKUs in the SKU block of the product registration screen.
574 *
575 * @since 2.3.3
576 * @param int $post_id Post ID.
577 * @param int $metastr Meta ids.
578 */
579 function usces_sort_item_skus( $post_id, $metastr ) {
580 global $wpdb;
581
582 $meta_ids = explode( ',', $metastr );
583 $skus = wel_get_skus( $post_id, 'meta_id', false );
584
585 if ( ! empty( $meta_ids ) ) {
586
587 $i = 0;
588 foreach ( $meta_ids as $meta_id ) {
589
590 $i = 0;
591 foreach ( $meta_ids as $meta_id ) {
592
593 $sku = $skus[ $meta_id ];
594 $sku['sort'] = $i;
595
596 wel_update_sku_data_by_id( $meta_id, $post_id, $sku );
597
598 $i++;
599 }
600 }
601 }
602 }
603
604 /**
605 * Get all options by post id.
606 *
607 * @since 2.3.3
608 * @param int $post_id Post ID.
609 * @param string $keyflag Sort key.
610 * @param bool $cache Cache.
611 */
612 function usces_get_opts( $post_id, $keyflag = 'sort', $cache = true ) {
613 $opts = wel_get_opts( $post_id, $keyflag, $cache );
614 return $opts;
615 }
616
617 /**
618 * Add Item Option.
619 *
620 * @since 2.3.3
621 * @param string $post_id Post ID.
622 * @param string $new_value SKU data.
623 * @param boolean $check Switch of check.
624 * @return New meta id.
625 */
626 function usces_add_opt( $post_id, $new_value, $check = true ) {
627 global $wpdb, $usces;
628
629 if ( $check ) {
630
631 $opts = wel_get_opts( $post_id, 'meta_id', false );
632
633 if ( ! empty( $opts ) && is_array( $opts ) ) {
634
635 $meta_num = count( $opts );
636 $unique = true;
637 $sortnull = true;
638 $sort = array();
639
640 foreach ( (array) $opts as $opt ) {
641
642 if ( $opt['name'] === $new_value['name'] ) {
643 $unique = false;
644 }
645 if ( ! isset( $opt['sort'] ) ) {
646 $sortnull = false;
647 }
648 $sort[] = $opt['sort'];
649 }
650
651 if ( ! $unique ) {
652 return -1;
653 }
654
655 rsort( $sort );
656 $next_number = reset( $sort ) + 1;
657 $unique_sort = array_unique( $sort );
658 if ( $meta_num !== count( $unique_sort ) || $meta_num !== $next_number || ! $sortnull ) {
659 // To repair the sort data.
660 $i = 0;
661 foreach ( (array) $opts as $opt ) {
662 $opt['sort'] = $i;
663 wel_update_opt_data_by_id( $opt['meta_id'], $post_id, $opt );
664 $i++;
665 }
666 }
667 }
668
669 $new_value['sort'] = ! empty( $meta_num ) ? $meta_num : 0;
670 }
671
672 $id = wel_add_opt_data( $post_id, $new_value );
673 return $id;
674 }
675
676 /**
677 * List item option
678 *
679 * @param array $opts Options.
680 */
681 function list_item_option_meta( $opts ) {
682 // Exit if no meta.
683 if ( ! $opts ) {
684 ?>
685 <table id="optlist-table" class="list" style="display: none;">
686 <thead>
687 <tr>
688 <th class="hanldh">&emsp;</th>
689 <th class="item-opt-key"><?php esc_html_e( 'option name', 'usces' ); ?></th>
690 <th class="item-opt-value"><?php esc_html_e( 'selected amount', 'usces' ); ?></th>
691 </tr>
692 </thead>
693 <tbody id="item-opt-list">
694 <tr><td></td></tr>
695 </tbody>
696 </table>
697 <?php
698 } else {
699 ?>
700 <table id="optlist-table" class="list">
701 <thead>
702 <tr>
703 <th class="hanldh">&emsp;</th>
704 <th class="item-opt-key"><?php esc_html_e( 'option name', 'usces' ); ?></th>
705 <th class="item-opt-value"><?php esc_html_e( 'selected amount', 'usces' ); ?></th>
706 </tr>
707 </thead>
708 <tbody id="item-opt-list">
709 <?php
710 foreach ( $opts as $opt ) {
711 echo _list_item_option_meta_row( $opt );
712 }
713 ?>
714 </tbody>
715 </table>
716 <?php
717 }
718 }
719
720 /**
721 * Option meta row
722 *
723 * @param array $opt Options.
724 */
725 function _list_item_option_meta_row( $opt ) {
726 $r = '';
727 $style = '';
728 $means = get_option( 'usces_item_option_select', array() );
729
730 $name = $opt['name'];
731 $meansoption = '';
732 foreach ( $means as $meankey => $meanvalue ) {
733 $meansoption .= '<option value="' . esc_attr( $meankey ) . '"' . selected( $meankey, $opt['means'], false ) . '>' . esc_html( $meanvalue ) . "</option>\n";
734 }
735 $essential = ( 1 === (int) $opt['essential'] ) ? ' checked="checked"' : '';
736 $value = '';
737 if ( is_array( $opt['value'] ) ) {
738 foreach ( $opt['value'] as $k => $v ) {
739 $value .= $v . "\n";
740 }
741 } else {
742 $value = $opt['value'];
743 }
744 $value = trim( $value );
745 $id = (int) $opt['meta_id'];
746 $sort = (int) $opt['sort'];
747
748 ob_start();
749 ?>
750 <tr class="metastuffrow"><td colspan="3">
751 <table id="itemopt-<?php echo esc_attr( $id ); ?>" class="metastufftable">
752 <tr>
753 <th class="handlb" rowspan="2">&emsp;</th>
754 <td class="item-opt-key">
755 <div><input name="itemopt[<?php echo esc_attr( $id ); ?>][name]" id="itemopt[<?php echo esc_attr( $id ); ?>][name]" class="metaboxfield" type="text" size="20" value="<?php echo esc_attr( $name ); ?>" /></div>
756 <div class="optcheck">
757 <select name="itemopt[<?php echo esc_attr( $id ); ?>][means]" id="itemopt[<?php echo esc_attr( $id ); ?>][means]"><?php wel_esc_script_e( $meansoption ); ?></select>
758 <label for="itemopt[<?php echo esc_attr( $id ); ?>][essential]"><input name="itemopt[<?php echo esc_attr( $id ); ?>][essential]" id="itemopt[<?php echo esc_attr( $id ); ?>][essential]" type="checkbox" value="1"<?php echo esc_attr( $essential ); ?> class="metaboxcheckfield" /><?php esc_html_e( 'Required','usces' ); ?></label>
759 </div>
760 </td>
761 <td class="item-opt-value">
762 <textarea name="itemopt[<?php echo esc_attr( $id ); ?>][value]" id="itemopt[<?php echo esc_attr( $id ); ?>][value]" class="metaboxfield"><?php echo esc_html( $value ); ?></textarea>
763 </td>
764 </tr>
765 <tr>
766 <td colspan="2" class="submittd">
767 <div id="itemoptsubmit-<?php echo esc_attr( $id ); ?>" class="submit">
768 <input name="deleteitemopt[<?php echo esc_attr( $id ); ?>]" id="deleteitemopt[<?php echo esc_attr( $id ); ?>]" type="button" class="button" value="<?php esc_attr_e( 'Delete' ); ?>" onclick="if( jQuery('#post_ID').val() < 0 ) return; itemOpt.post('deleteitemopt', <?php echo esc_attr( $id ); ?>);" />
769 <input name="updateitemopt[<?php echo esc_attr( $id ); ?>]" id="updateitemopt[<?php echo esc_attr( $id ); ?>]" type="button" class="button" value="<?php esc_attr_e( 'Update' ); ?>" onclick="if( jQuery('#post_ID').val() < 0 ) return; itemOpt.post('updateitemopt', <?php echo esc_attr( $id ); ?>);" />
770 <input name="itemopt[<?php echo esc_attr( $id ); ?>][sort]" id="itemopt[<?php echo esc_attr( $id ); ?>][sort]" type="hidden" value="<?php echo esc_attr( $sort ); ?>" />
771 </div>
772 <div id="itemopt_loading-<?php echo esc_attr( $id ); ?>" class="meta_submit_loading"></div>
773 </td>
774 </tr>
775 </table>
776 </td></tr>
777 <?php
778 $r = ob_get_contents();
779 ob_end_clean();
780 return $r;
781 }
782
783 /**
784 * Common option meta form
785 */
786 function common_option_meta_form() {
787 $means = get_option( 'usces_item_option_select', array() );
788 $meansoption = '';
789 foreach ( $means as $meankey => $meanvalue ) {
790 $meansoption .= '<option value="' . esc_attr( $meankey ) . '">' . esc_html( $meanvalue ) . "</option>\n";
791 }
792 ?>
793 <div id="itemopt_ajax-response"></div>
794 <p><strong><?php esc_html_e( 'Add a new option', 'usces' ); ?> : </strong></p>
795 <table id="newmeta2">
796 <thead>
797 <tr>
798 <th class="left"><label for="metakeyselect"><?php esc_html_e( 'option name', 'usces' ); ?></label></th>
799 <th><label for="metavalue"><?php esc_html_e( 'selected amount', 'usces' ); ?></label></th>
800 </tr>
801 </thead>
802 <tbody>
803 <tr>
804 <td class="item-opt-key">
805 <input type="text" id="newoptname" name="newoptname" class="metaboxfield" tabindex="7" value="" />
806 <div class="optcheck">
807 <select name="newoptmeans" id="newoptmeans" class="metaboxfield long"><?php wel_esc_script_e( $meansoption ); ?></select>
808 <label for="newoptessential"><input name="newoptessential" type="checkbox" id="newoptessential" class="metaboxcheckfield" /><?php esc_html_e( 'Required', 'usces' ); ?></label>
809 </div>
810 </td>
811 <td class="item-opt-value"><textarea id="newoptvalue" name="newoptvalue" class="metaboxfield"></textarea></td>
812 </tr>
813 <tr>
814 <td colspan="2" class="submittd">
815 <div id="newcomoptsubmit" class="submit">
816 <input name="add_comopt" type="button" class="button" id="add_comopt" tabindex="9" value="<?php esc_attr_e( 'Add common options', 'usces' ); ?>" onclick="itemOpt.post('addcommonopt', 0);" />
817 </div>
818 <div id="newcomopt_loading" class="meta_submit_loading"></div>
819 </td>
820 </tr>
821 </tbody>
822 </table>
823 <?php
824 }
825
826 /**
827 * Item option meta form
828 */
829 function item_option_meta_form() {
830 $limit = (int) apply_filters( 'postmeta_form_limit', 30 );
831 $cart_number = (int) get_option( 'usces_cart_number' );
832 $opts = usces_get_opts( $cart_number );
833 $means = get_option( 'usces_item_option_select', array() );
834 $meansoption = '';
835 foreach ( $means as $meankey => $meanvalue ) {
836 $meansoption .= '<option value="' . esc_attr( $meankey ) . '">' . esc_html( $meanvalue ) . "</option>\n";
837 }
838 ?>
839 <div id="itemopt_ajax-response"></div>
840 <p><strong><?php esc_html_e( 'Applicable product options', 'usces' ); ?> : </strong></p>
841 <table id="newmeta2">
842 <thead>
843 <tr>
844 <th class="item-opt-key"><label for="metakeyselect"><?php esc_html_e( 'option name', 'usces' ); ?></label></th>
845 <th class="item-opt-value"><label for="metavalue"><?php esc_html_e( 'selected amount', 'usces' ); ?></label></th>
846 </tr>
847 </thead>
848 <tbody>
849 <tr>
850 <td class='item-opt-key'>
851 <?php if ( ! empty( $opts ) ) { ?>
852 <select id="optkeyselect" name="optkeyselect" class="optkeyselect metaboxfield" tabindex="7" onchange="if( jQuery('#post_ID').val() < 0 ) return; itemOpt.post('keyselect', this.value);">
853 <option value="#NONE#"><?php esc_html_e( '-- Select --', 'usces' ); ?></option>
854 <?php foreach ( $opts as $opt ) { ?>
855 <option value="<?php echo esc_attr( $opt['meta_id'] ); ?>"><?php echo esc_attr( $opt['name'] ); ?></option>
856 <?php } ?>
857 </select>
858 <input type="hidden" id="newoptname" name="newoptname" class="metaboxfield" />
859 <div class="optcheck">
860 <select name="newoptmeans" id="newoptmeans"><?php wel_esc_script_e( $meansoption ); ?></select>
861 <label for="newoptessential"><input name="newoptessential" type="checkbox" id="newoptessential" class="metaboxcheckfield" /><?php esc_html_e( 'Required', 'usces' ); ?></label>
862 </div>
863 <?php } else { ?>
864 <p><?php esc_html_e( 'Please create a common option.', 'usces' ); ?></p>
865 <?php } ?>
866 </td>
867 <td class="item-opt-value"><textarea id="newoptvalue" name="newoptvalue" class="metaboxfield"></textarea></td>
868 </tr>
869 <tr>
870 <td colspan="2" class="submittd">
871 <?php if ( is_array( $opts ) ) { ?>
872 <div id="newitemoptsubmit" class="submit">
873 <input name="add_itemopt" type="button" class='button' id="add_itemopt" tabindex="9" value="<?php esc_attr_e( 'Apply an option', 'usces' ); ?>" onclick="if( jQuery('#post_ID').val() < 0 ) return; itemOpt.post('additemopt', 0);" />
874 </div>
875 <div id="newitemopt_loading" class="meta_submit_loading"></div>
876 <?php } ?>
877 </td>
878 </tr>
879 </tbody>
880 </table>
881 <?php
882 }
883
884 /**
885 * Add item option meta
886 *
887 * @param int $post_ID Post ID.
888 * @return mixed
889 */
890 function add_item_option_meta( $post_ID ) {
891 global $usces;
892
893 $post_id = (int) $post_ID;
894 $value = array();
895 $opts = array();
896 $protected = array(
897 '#NONE#',
898 '_wp_attached_file',
899 '_wp_attachment_metadata',
900 '_wp_old_slug',
901 '_wp_page_template',
902 );
903
904 $args = array(
905 'newoptcode' => FILTER_SANITIZE_SPECIAL_CHARS,
906 'newoptname' => FILTER_SANITIZE_SPECIAL_CHARS,
907 'newoptmeans' => array(
908 'filter' => FILTER_VALIDATE_INT,
909 'flags' => FILTER_REQUIRE_SCALAR,
910 'options' => array( 'defalut' => 0 ),
911 ),
912 'newoptessential' => array(
913 'filter' => FILTER_VALIDATE_INT,
914 'flags' => FILTER_REQUIRE_SCALAR,
915 'options' => array( 'defalut' => 0 ),
916 ),
917 'newoptvalue' => FILTER_DEFAULT,
918 );
919 $inputs = filter_input_array( INPUT_POST, $args );
920 $newoptcode = $inputs['newoptcode'];
921 $newoptname = $inputs['newoptname'];
922 $newoptmeans = $inputs['newoptmeans'];
923 $newoptessential = $inputs['newoptessential'];
924 $newoptvalue = $inputs['newoptvalue'];
925
926 if ( ( $newoptmeans >= 2 || WCUtils::is_zero( $newoptvalue ) || ! empty ( $newoptvalue ) ) && ! empty ( $newoptname ) ) {
927
928 if ( $newoptname ) {
929 $metakey = $newoptname; // default.
930 }
931
932 if ( in_array( $metakey, $protected ) ) {
933 return false;
934 }
935
936 $WelItem = new Welcart\ItemData( $post_id, false );
937 $format = $WelItem->get_opt_format();
938
939 $format['post_id'] = $post_id;
940 $format['code'] = $newoptcode;
941 $format['name'] = str_replace( "\\", '', $newoptname );
942 $format['means'] = $newoptmeans;
943 $format['essential'] = $newoptessential;
944 $format['value'] = str_replace( "\\", '', $newoptvalue );
945
946 $value = $usces->stripslashes_deep_post( $format );
947
948 $id = usces_add_opt( $post_id, $value );
949
950 return $id;
951 } else {
952 return false;
953 }
954 }
955
956 /**
957 * Update Item Option by ajax.
958 * Ajax response when Option information is updated in the Option block of the product registration screen.
959 *
960 * @since 2.3.3
961 * @param string $post_ID Post ID.
962 * @return Query result.
963 */
964 function up_item_option_meta( $post_ID ) {
965 global $usces;
966
967 $post_id = (int) $post_ID;
968 $value = array();
969
970 $args = array(
971 'optmetaid' => FILTER_VALIDATE_INT,
972 'optcode' => FILTER_DEFAULT,
973 'optname' => FILTER_DEFAULT,
974 'optmeans' => FILTER_VALIDATE_INT,
975 'optessential' => FILTER_VALIDATE_INT,
976 'sort' => FILTER_VALIDATE_INT,
977 'optvalue' => FILTER_DEFAULT,
978 );
979
980 $inputs = filter_input_array( INPUT_POST, $args );
981
982 $optmetaid = ( null !== $inputs['optmetaid'] ) ? (int) $inputs['optmetaid'] : '';
983 $optcode = ( null !== $inputs['optcode'] ) ? $inputs['optcode'] : '';
984 $optname = ( null !== $inputs['optname'] ) ? $inputs['optname'] : '';
985 $optmeans = ( null !== $inputs['optmeans'] ) ? (int) $inputs['optmeans'] : 0;
986 $optessential = ( null !== $inputs['optessential'] ) ? $inputs['optessential'] : 0;
987 $optsort = ( null !== $inputs['sort'] ) ? $inputs['sort'] : 0;
988 $optvalue = ( null !== $inputs['optvalue'] ) ? trim( $inputs['optvalue'] ) : '';
989
990 $opts = wel_get_opts( $post_id, 'meta_id', false );
991 $opt = $opts[ $optmetaid ];
992
993 $opt['meta_id'] = $optmetaid;
994 $opt['code'] = $optcode;
995 $opt['name'] = str_replace( "\\", '', $optname );
996 $opt['means'] = $optmeans;
997 $opt['essential'] = $optessential;
998 $opt['value'] = str_replace( "\\", '', $optvalue );
999 $opt['sort'] = $optsort;
1000
1001 $value = $usces->stripslashes_deep_post( $opt );
1002
1003 $res = wel_update_opt_data_by_id( $optmetaid, $post_id, $value );
1004
1005 return $res;
1006 }
1007
1008 /**
1009 * Delete item option meta
1010 *
1011 * @param int $post_id Post ID.
1012 */
1013 function del_item_option_meta( $post_id ) {
1014
1015 $optmetaid = filter_input( INPUT_POST, 'optmetaid', FILTER_VALIDATE_INT );
1016 $meta_id = ( null !== $optmetaid ) ? $optmetaid : '';
1017
1018 wel_delete_opt_data_by_id( $meta_id, $post_id );
1019
1020 $opts = wel_get_opts( $post_id, 'sort', false );
1021 if ( ! empty( $opts ) ) {
1022 $i = 0;
1023 foreach ( $opts as $opt ) {
1024 $opt['sort'] = $i;
1025 $meta_id = $opt['meta_id'];
1026 wel_update_opt_data_by_id( $meta_id, $post_id, $opt );
1027 $i++;
1028 }
1029 } else {
1030 return;
1031 }
1032 }
1033
1034 /**
1035 * Select common option
1036 *
1037 * @param int $post_id Post ID.
1038 * @return array
1039 */
1040 function select_common_option( $post_id ) {
1041
1042 $meta_id = filter_input( INPUT_POST, 'meta_id', FILTER_VALIDATE_INT );
1043 if ( ! $meta_id ) {
1044 return array();
1045 }
1046
1047 $opts = wel_get_opts( $post_id, 'meta_id', false );
1048 $opt = $opts[ $meta_id ];
1049 $res = array(
1050 'means' => $opt['means'],
1051 'essential' => $opt['essential'],
1052 'value' => $opt['value'],
1053 );
1054 return $res;
1055 }
1056
1057 /**
1058 * Order item2cart ajax
1059 */
1060 function order_item2cart_ajax() {
1061 global $usces;
1062
1063 check_ajax_referer( 'order_edit', 'wc_nonce' );
1064
1065 if ( 'order_item2cart_ajax' !== $_POST['action'] ) {
1066 die(0);
1067 }
1068
1069 $_POST = $usces->stripslashes_deep_post( $_POST );
1070 $logger = Logger::start( $_POST['order_id'], 'orderedit', 'update' );
1071 $order_id = usces_add_ordercartdata();
1072 if ( ! $order_id ) {
1073 die( 0 );
1074 }
1075
1076 $cart = usces_get_ordercartdata( $order_id );
1077 $return = usces_get_ordercart_row( $order_id, $cart );
1078 $logger->flush();
1079 die( $return );
1080 }
1081
1082 /**
1083 * Order item ajax
1084 */
1085 function order_item_ajax() {
1086 global $usces;
1087
1088 if ( ! current_user_can( 'wel_manage_order' ) ) {
1089 wp_die( __( 'You do not have sufficient privileges to perform this operation.', 'usces' ) );
1090 }
1091
1092 if ( 'order_item_ajax' !== wp_unslash( $_POST['action'] ) ) {
1093 die(0);
1094 }
1095
1096 $res = false;
1097 $_POST = $usces->stripslashes_deep_post( $_POST );
1098 $mode = sanitize_text_field( wp_unslash( $_POST['mode'] ) );
1099
1100 switch ( $mode ) {
1101 case 'completionMail':
1102 case 'orderConfirmMail':
1103 case 'changeConfirmMail':
1104 case 'receiptConfirmMail':
1105 case 'mitumoriConfirmMail':
1106 case 'cancelConfirmMail':
1107 case 'otherConfirmMail':
1108 check_ajax_referer( 'order_edit', 'wc_nonce' );
1109 $order_id = (int) wp_unslash( $_POST['order_id'] );
1110 $res = usces_order_confirm_message( $order_id );
1111 break;
1112 case 'sendmail':
1113 // Nonce('wc_send_mail_order_nonce') verification is executed in the usces_ajax_send_mail function.
1114 $res = usces_ajax_send_mail();
1115 break;
1116 case 'get_order_item':
1117 check_ajax_referer( 'order_edit', 'wc_nonce' );
1118 $itemcode = sanitize_text_field( wp_unslash( $_POST['itemcode'] ) );
1119 $res = get_order_item( $itemcode );
1120 break;
1121 case 'get_item_select_option':
1122 check_ajax_referer( 'order_edit', 'wc_nonce' );
1123 $cat_id = (int) wp_unslash( $_POST['cat_id'] );
1124 $res = usces_get_item_select_option( $cat_id );
1125 break;
1126 case 'ordercheckpost':
1127 check_ajax_referer( 'order_edit', 'wc_nonce' );
1128 $res = usces_update_ordercheck();
1129 break;
1130 case 'getmember':
1131 check_ajax_referer( 'order_edit', 'wc_nonce' );
1132 $res = usces_get_member_neworder();
1133 break;
1134 case 'recalculation':
1135 check_ajax_referer( 'order_edit', 'wc_nonce' );
1136 $change_taxrate = ( isset( $_POST['change_taxrate'] ) ) ? sanitize_text_field( wp_unslash( $_POST['change_taxrate'] ) ) : '';
1137 $order_id = (int) wp_unslash( $_POST['order_id'] );
1138 $mem_id = (int) wp_unslash( $_POST['mem_id'] );
1139 $res = usces_order_recalculation( $order_id, $mem_id, $_POST['post_ids'], $_POST['prices'], $_POST['quants'], $_POST['cart_ids'], $_POST['upoint'], $_POST['shipping_charge'], $_POST['cod_fee'], $_POST['discount'], $change_taxrate );
1140 break;
1141 case 'recalculation_reduced':
1142 check_ajax_referer( 'order_edit', 'wc_nonce' );
1143 $change_taxrate = ( isset( $_POST['change_taxrate'] ) ) ? sanitize_text_field( wp_unslash( $_POST['change_taxrate'] ) ) : '';
1144 $order_id = (int) wp_unslash( $_POST['order_id'] );
1145 $mem_id = (int) wp_unslash( $_POST['mem_id'] );
1146 $res = usces_order_recalculation_reduced( $order_id, $mem_id, $_POST['post_ids'], $_POST['prices'], $_POST['quants'], $_POST['cart_ids'], $_POST['upoint'], $_POST['shipping_charge'], $_POST['cod_fee'], $_POST['discount_standard'], $_POST['discount_reduced'], $change_taxrate );
1147 break;
1148 case 'get_settlement_log':
1149 check_ajax_referer( 'order_list', 'wc_nonce' );
1150 $res = usces_get_settlement_log();
1151 break;
1152 case 'get_settlement_log_detail':
1153 check_ajax_referer( 'order_list', 'wc_nonce' );
1154 $log_key = sanitize_text_field( wp_unslash( $_POST['log_key'] ) );
1155 $res = usces_get_settlement_log_detail( $log_key );
1156 break;
1157 case 'search_settlement_log':
1158 check_ajax_referer( 'order_list', 'wc_nonce' );
1159 $log_key = sanitize_text_field( wp_unslash( $_POST['log_key'] ) );
1160 $res = usces_get_settlement_log( $log_key );
1161 break;
1162 case 'delete_settlement_log':
1163 check_ajax_referer( 'order_list', 'wc_nonce' );
1164 $log_key = sanitize_text_field( wp_unslash( $_POST['log_key'] ) );
1165 usces_delete_settlement_log( $log_key );
1166 $res = usces_get_settlement_log();
1167 break;
1168 case 'delete_settlement_log_all':
1169 check_ajax_referer( 'order_list', 'wc_nonce' );
1170 usces_delete_settlement_log();
1171 $res = usces_get_settlement_log();
1172 break;
1173 case 'revival_order_data':
1174 check_ajax_referer( 'order_list', 'wc_nonce' );
1175 $log_key = sanitize_text_field( wp_unslash( $_POST['log_key'] ) );
1176 $register_date = sanitize_text_field( wp_unslash( $_POST['register_date'] ) );
1177 $res = usces_revival_order_data( $log_key, $register_date );
1178 break;
1179 case 'get_settlement_error_log':
1180 check_ajax_referer( 'order_list', 'wc_nonce' );
1181 $res = usces_get_settlement_error_log();
1182 break;
1183 case 'get_settlement_error_log_detail':
1184 check_ajax_referer( 'order_list', 'wc_nonce' );
1185 $log_id = sanitize_text_field( wp_unslash( $_POST['log_id'] ) );
1186 $res = usces_get_settlement_error_log_detail( $log_id );
1187 break;
1188 case 'delete_settlement_error_log':
1189 check_ajax_referer( 'order_list', 'wc_nonce' );
1190 $log_id = sanitize_text_field( wp_unslash( $_POST['log_id'] ) );
1191 usces_delete_settlement_error_log( $log_id );
1192 $res = usces_get_settlement_error_log();
1193 break;
1194 case 'delete_settlement_error_log_all':
1195 check_ajax_referer( 'order_list', 'wc_nonce' );
1196 usces_delete_settlement_error_log();
1197 $res = usces_get_settlement_error_log();
1198 break;
1199 case 'reset_settlement_notice':
1200 check_ajax_referer( 'order_list', 'wc_nonce' );
1201 $res = usces_reset_settlement_notice();
1202 break;
1203 }
1204
1205 $res = wel_esc_script( apply_filters( 'usces_filter_order_item_ajax', $res ) );
1206
1207 if ( false === $res ) {
1208 die(0);
1209 }
1210
1211 die( $res );
1212 }
1213
1214 /**
1215 * Item select field
1216 *
1217 * @param int $cat_id Category ID.
1218 * @return string
1219 */
1220 function usces_get_item_select_option( $cat_id ) {
1221 global $usces;
1222
1223 $number = apply_filters( 'usces_filter_item_select_numberposts', 50, $cat_id );
1224 $args = array(
1225 'category' => $cat_id,
1226 'numberposts' => $number,
1227 'post_status' => array( 'publish', 'private' ),
1228 );
1229 $args = apply_filters( 'usces_filter_item_select_queryargs', $args, $cat_id );
1230
1231 $items = get_posts( $args );
1232 $option = '<option value="-1">' . __( 'Please select an item', 'usces' ) . '</option>' . "\n";
1233 foreach ( (array) $items as $item ) {
1234 $product = wel_get_product( $item->ID );
1235 $item_name = $product['itemName'];
1236 $item_code = $product['itemCode'];
1237 $option .= '<option value="' . urlencode( $item_code ) . '">' . esc_html( $item_name ) . '(' . esc_html( $item_code ) . ')</option>' . "\n";
1238 }
1239 return $option;
1240 }
1241
1242 /**
1243 * Order Item html
1244 */
1245 function usces_get_member_neworder() {
1246 global $wpdb;
1247
1248 $wpdb->show_errors();
1249 $res = '';
1250 $member_table = usces_get_tablename( 'usces_member' );
1251 $query = $wpdb->prepare( "SELECT * FROM $member_table WHERE mem_email = %s", trim( $_POST['email'] ) );
1252 $value = $wpdb->get_row( $query, ARRAY_A );
1253
1254 if ( ! $value ) {
1255 $response = array( 'status_code' => 'none' );
1256 wp_send_json( $response );
1257 }
1258
1259 $response = array(
1260 'status_code' => 'ok',
1261 'member_id' => $value['ID'],
1262 'customer[name1]' => $value['mem_name1'],
1263 'customer[name2]' => $value['mem_name2'],
1264 'customer[name3]' => $value['mem_name3'],
1265 'customer[name4]' => $value['mem_name4'],
1266 'customer[zipcode]' => $value['mem_zip'],
1267 'customer[pref]' => $value['mem_pref'],
1268 'customer[address1]' => $value['mem_address1'],
1269 'customer[address2]' => $value['mem_address2'],
1270 'customer[address3]' => $value['mem_address3'],
1271 'customer[tel]' => $value['mem_tel'],
1272 'customer[fax]' => $value['mem_fax'],
1273 'delivery[name1]' => $value['mem_name1'],
1274 'delivery[name2]' => $value['mem_name2'],
1275 'delivery[name3]' => $value['mem_name3'],
1276 'delivery[name4]' => $value['mem_name4'],
1277 'delivery[zipcode]' => $value['mem_zip'],
1278 'delivery[pref]' => $value['mem_pref'],
1279 'delivery[address1]' => $value['mem_address1'],
1280 'delivery[address2]' => $value['mem_address2'],
1281 'delivery[address3]' => $value['mem_address3'],
1282 'delivery[tel]' => $value['mem_tel'],
1283 'delivery[fax]' => $value['mem_fax'],
1284 );
1285
1286 $member_metetable = usces_get_tablename( 'usces_member_meta' );
1287 $query = $wpdb->prepare( "SELECT * FROM $member_metetable WHERE meta_key LIKE %s AND member_id = %d", 'csmb_%', $value['ID'] );
1288 $customs = $wpdb->get_results( $query, ARRAY_A );
1289 if ( ! empty( $customs ) ) {
1290 foreach ( $customs as $cusv ) {
1291 $response[ 'custom_customer[' . substr( $cusv['meta_key'], 5 ) . ']'] = $cusv['meta_value'];
1292 $response[ 'custom_delivery[' . substr( $cusv['meta_key'], 5 ) . ']'] = $cusv['meta_value'];
1293 }
1294 }
1295
1296 $response = apply_filters( 'usces_filter_get_member_neworder', $response );
1297
1298 wp_send_json( $response );
1299 }
1300
1301 /**
1302 * Add to order cart
1303 *
1304 * @return mixed
1305 */
1306 function usces_add_ordercartdata() {
1307 global $usces, $wpdb;
1308
1309 $res = 0;
1310
1311 $order_id = (int) $_POST['order_id'];
1312 if ( ! $order_id ) {
1313 return $res;
1314 }
1315
1316 $post_id = (int) $_POST['post_id'];
1317 $sku_code = urldecode( $_POST['sku'] );
1318 $quantity = 1;
1319
1320 $current_cart = usces_get_ordercartdata( $order_id );
1321 $temp_arr = array();
1322 foreach ( $current_cart as $cv ) {
1323 $temp_arr[] = $cv['row_index'];
1324 }
1325 $row_index = ( 0 < count( $temp_arr ) ) ? max( $temp_arr ) + 1 : 0;
1326
1327 $cart_table = $wpdb->prefix . 'usces_ordercart';
1328 $cart_meta_table = $wpdb->prefix . 'usces_ordercart_meta';
1329
1330 $product = wel_get_product( $post_id );
1331 $item_name = $product['itemName'];
1332 $item_code = $product['itemCode'];
1333 $skus = $usces->get_skus( $post_id, 'code' );
1334 $sku = apply_filters( 'usces_filter_add_ordercart_sku', $skus[ $sku_code ], $_POST );
1335 $tax = 0;
1336
1337 $query = $wpdb->prepare(
1338 "INSERT INTO $cart_table
1339 (
1340 order_id, row_index, post_id, item_code, item_name,
1341 sku_code, sku_name, cprice, price, quantity,
1342 unit, tax, destination_id, cart_serial
1343 ) VALUES (
1344 %d, %d, %d, %s, %s,
1345 %s, %s, %f, %f, %d,
1346 %s, %d, %d, %s
1347 )",
1348 $order_id, $row_index, $post_id, $item_code, $item_name,
1349 $sku_code, $sku['name'], $sku['cprice'], $sku['price'], $quantity,
1350 $sku['unit'], $tax, NULL, NULL
1351 );
1352 $res = $wpdb->query( $query );
1353 $cart_id = NULL;
1354 if ( false !== $res ) {
1355 $cart_id = $wpdb->insert_id;
1356
1357 $item_opts = usces_get_opts( $post_id, 'name' );
1358 foreach ( $item_opts as $key => $iopts ) {
1359 if ( 3 === (int) $iopts['means'] || 4 === (int) $iopts['means'] ) {
1360 // POSTが無ければNULLで追加.
1361 if ( ! isset( $_POST['itemOption'][ $key ] ) ) {
1362 $query = $wpdb->prepare(
1363 "INSERT INTO $cart_meta_table
1364 ( cart_id, meta_type, meta_key, meta_value ) VALUES ( %d, %s, %s, %s )",
1365 $cart_id, 'option', $iopts['name'], NULL
1366 );
1367 $wpdb->query( $query );
1368 }
1369 }
1370 }
1371
1372 if ( isset( $_POST['itemOption'] ) ) {
1373 foreach ( (array) $_POST['itemOption'] as $okey => $ovalue ) {
1374 $means = $item_opts[ $okey ]['means'];
1375 if ( is_array( $ovalue ) ) {
1376 $temp = array();
1377 if ( 4 === (int) $means ) {
1378 foreach ( $ovalue as $k => $v ) {
1379 $temp[] = $v;
1380 }
1381 } else {
1382 foreach ( $ovalue as $k => $v ) {
1383 $temp[ urlencode( $k ) ] = $v;
1384 }
1385 }
1386 $ovalue = serialize( $temp );
1387 } else {
1388 $ovalue = $ovalue;
1389 }
1390 $aquery = $wpdb->prepare(
1391 "INSERT INTO $cart_meta_table
1392 ( cart_id, meta_type, meta_key, meta_value ) VALUES (%d, %s, %s, %s)",
1393 $cart_id, 'option', $okey, $ovalue
1394 );
1395 $wpdb->query( $aquery );
1396 }
1397 }
1398
1399 if ( $usces->is_reduced_taxrate() ) {
1400 if ( isset( $sku['taxrate'] ) && 'reduced' === $sku['taxrate'] ) {
1401 $tkey = 'reduced';
1402 $tvalue = $usces->options['tax_rate_reduced'];
1403 } else {
1404 $tkey = 'standard';
1405 $tvalue = $usces->options['tax_rate'];
1406 }
1407 $tquery = $wpdb->prepare(
1408 "INSERT INTO $cart_meta_table
1409 ( cart_id, meta_type, meta_key, meta_value ) VALUES ( %d, 'taxrate', %s, %s )",
1410 $cart_id, $tkey, $tvalue
1411 );
1412 $wpdb->query( $tquery );
1413 }
1414 }
1415
1416 $res = apply_filters( 'usces_filter_add_ordercart', $res, $order_id, $cart_id );
1417
1418 if ( $res ) {
1419 return $order_id;
1420 } else {
1421 return $res;
1422 }
1423 }
1424
1425 /**
1426 * Get order item
1427 *
1428 * @param string $item_code Item code.
1429 * @return string
1430 */
1431 function get_order_item( $item_code ) {
1432 global $usces, $post;
1433
1434 $post_id = wel_get_id_by_item_code( $item_code );
1435 if ( null === $post_id ) {
1436 return false;
1437 }
1438 $product = wel_get_product( $post_id );
1439 if ( null === $product ) {
1440 return false;
1441 }
1442 $post = $product['_pst'];
1443 $res = apply_filters( 'usce_action_ajax_get_order_item', false, $post );
1444 if ( false !== $res ) {
1445 return $res;
1446 }
1447
1448 $pict_id = wel_get_main_pict_id_by_code( $item_code );
1449 $pict_link = wp_get_attachment_image( $pict_id, array( 150, 150 ), true );
1450 preg_match( "/^\<a .+\>(\<img .+\/\>)\<\/a\>$/", $pict_link, $match );
1451 $pict = isset( $match[1] ) ? $match[1] : '';
1452 $skus = $product['_sku'];
1453 $optkeys = $usces->get_itemOptionKey( $post_id );
1454 $item_name = esc_html( $product['itemName'] );
1455
1456 $r = '';
1457 $r .= $pict_link . "\n";
1458 $r .= '<h3>' . $item_name . "</h3>\n";
1459 $r .= "<div class=\"skuform\">\n";
1460
1461 $r .= "<table class=\"skumulti\">\n";
1462 $r .= "<thead>\n";
1463 $r .= "<tr>\n";
1464 $r .= '<th>' . __( 'SKU code', 'usces' ) . "</th>\n";
1465 $r .= '<th>' . __( 'SKU display name ', 'usces' ) . "</th>\n";
1466
1467 $usces_listprice = __( 'List price', 'usces' ) . usces_guid_tax( 'return' );
1468 $r .= '<th>' . apply_filters( 'usces_filter_listprice_label', $usces_listprice, __( 'List price', 'usces' ), usces_guid_tax( 'return' ) ) . "</th>\n";
1469
1470 $usces_sellingprice = __( 'Sale price', 'usces' ) . usces_guid_tax( 'return' );
1471 $r .= '<th>' . apply_filters( 'usces_filter_sellingprice_label', $usces_sellingprice, __( 'Sale price', 'usces' ), usces_guid_tax( 'return' ) ) . "</th>\n";
1472
1473 $r .= '<th>' . __( 'stock status', 'usces' ) . "</th>\n";
1474 $r .= '<th>' . __( 'stock', 'usces' ) . "</th>\n";
1475 $r .= '<th>' . __( 'unit', 'usces' ) . "</th>\n";
1476 $r .= "<th>&nbsp;</th>\n";
1477 $r .= "</tr>\n";
1478 $r .= "</thead>\n";
1479 $r .= "<tbody>\n";
1480 foreach ( $skus as $sku ) :
1481 $key = urlencode( $sku['code'] );
1482 $cprice = esc_attr( $sku['cprice'] );
1483 $price = esc_attr( $sku['price'] );
1484 $zaiko = esc_attr( $usces->zaiko_status[ $sku['stock'] ] );
1485 $zaikonum = esc_attr( $sku['stocknum'] );
1486 $disp = esc_attr( $sku['name'] );
1487 $unit = esc_attr( $sku['unit'] );
1488 $gptekiyo = $sku['gp'];
1489 $sort = (int) $sku['sort'];
1490
1491 $r .= "<tr>\n";
1492 $r .= '<td rowspan="2">' . esc_js( $sku['code'] ) . "</td>\n";
1493 $r .= '<td>' . $disp . "</td>\n";
1494 $r .= '<td><span class="cprice">' . ( ( ! empty( $cprice ) ) ? usces_crform( $cprice, true, false, 'return' ) : '' ) . "</span></td>\n";
1495 $r .= '<td><span class="price">' . usces_crform( $price, true, false, 'return' ) . "</span></td>\n";
1496 $r .= '<td>' . $zaiko . "</td>\n";
1497 $r .= '<td>' . $zaikonum . "</td>\n";
1498 $r .= '<td>' . $unit . "</td>\n";
1499 $r .= "<td>\n";
1500 $r .= "<input name=\"itemNEWName[{$post_id}][{$key}]\" type=\"hidden\" id=\"itemNEWName[{$post_id}][{$key}]\" value=\"{$item_name}\" />\n";
1501 $r .= "<input name=\"itemNEWCode[{$post_id}][{$key}]\" type=\"hidden\" id=\"itemNEWCode[{$post_id}][{$key}]\" value=\"{$item_code}\" />\n";
1502 $r .= "<input name=\"skuNEWName[{$post_id}][{$key}]\" type=\"hidden\" id=\"skuNEWName[{$post_id}][{$key}]\" value=\"{$key}\" />\n";
1503 $r .= "<input name=\"skuNEWCprice[{$post_id}][{$key}]\" type=\"hidden\" id=\"skuNEWCprice[{$post_id}][{$key}]\" value=\"{$cprice}\" />\n";
1504 $r .= "<input name=\"skuNEWDisp[{$post_id}][{$key}]\" type=\"hidden\" id=\"skuNEWDisp[{$post_id}][{$key}]\" value=\"{$disp}\" />\n";
1505 $r .= "<input name=\"zaikoNEWnum[{$post_id}][{$key}]\" type=\"hidden\" id=\"zaikoNEWnum[{$post_id}][{$key}]\" value=\"{$zaikonum}\" />\n";
1506 $r .= "<input name=\"zaiNEWko[{$post_id}][{$key}]\" type=\"hidden\" id=\"zaiNEWko[{$post_id}][{$key}]\" value=\"{$zaiko}\" />\n";
1507 $r .= "<input name=\"uniNEWt[{$post_id}][{$key}]\" type=\"hidden\" id=\"uniNEWt[{$post_id}][{$key}]\" value=\"{$unit}\" />\n";
1508 $r .= "<input name=\"gpNEWtekiyo[{$post_id}][{$key}]\" type=\"hidden\" id=\"gpNEWtekiyo[{$post_id}][{$key}]\" value=\"{$gptekiyo}\" />\n";
1509 $r .= "<input name=\"skuNEWPrice[{$post_id}][{$key}]\" type=\"hidden\" id=\"skuNEWPrice[{$post_id}][{$key}]\" value=\"{$price}\" />\n";
1510 $r .= "<input name=\"inNEWCart[{$post_id}][{$key}]\" type=\"button\" id=\"inNEWCart[{$post_id}][{$key}]\" class=\"skubutton button\" value=\"" . __( 'Add to Whish List', 'usces' ) . "\" onclick=\"orderItem.add2cart('{$post_id}', '{$key}');\" />";
1511 $r .= "</td>\n";
1512 $r .= "</tr>\n";
1513 $r .= "<tr>\n";
1514 if ( $optkeys ) :
1515 $r .= "<td colspan=\"7\">\n";
1516 foreach ( $optkeys as $optkey => $optvalue ) :
1517 $r .= "<div>\n";
1518
1519 $name = esc_attr( $optvalue );
1520 $optcode = urlencode( $name );
1521 $opts = usces_get_opts( $post_id, 'name' );
1522 $opt = $opts[ $optvalue ];
1523 $opt['value'] = usces_change_line_break( $opt['value'] );
1524 $means = (int) $opt['means'];
1525 $essential = (int) $opt['essential'];
1526
1527 $r .= "\n<label for=\"itemNEWOption[{$post_id}][{$key}][{$optcode}]\" class=\"iopt_label\">{$name}</label>\n";
1528 switch ( $means ) {
1529 case 0: // Single-select.
1530 case 1: // Multi-select.
1531 $selects = explode( "\n", $opt['value'] );
1532 $multiple = ( 0 === $means ) ? '' : ' multiple';
1533 $multiple_array = ( 0 === $means ) ? '' : '_multiple';
1534 $r .= "\n<select name=\"itemNEWOption[{$post_id}][{$key}][{$optcode}]\" id=\"itemNEWOption[{$post_id}][{$key}][{$optcode}]\" class=\"iopt_select{$multiple_array}\"{$multiple}>\n";
1535 if ( 1 === $essential ) {
1536 $r .= "\t<option value=\"#NONE#\" selected=\"selected\">" . __( 'Choose', 'usces' ) . "</option>\n";
1537 }
1538 $s = 0;
1539 foreach ( $selects as $v ) {
1540 $v = trim( $v );
1541 if ( 0 === $s && 0 === $essential ) {
1542 $selected = ' selected="selected"';
1543 } else {
1544 $selected = '';
1545 }
1546 $r .= "\t<option value=\"{$v}\"{$selected}>{$v}</option>\n";
1547 $s++;
1548 }
1549 $r .= "</select>\n";
1550 break;
1551 case 2: // Text.
1552 $r .= "\n<input name=\"itemNEWOption[{$post_id}][{$key}][{$optcode}]\" type=\"text\" id=\"itemNEWOption[{$post_id}][{$key}][{$optcode}]\" class=\"iopt_text\" onKeyDown=\"if (event.keyCode == 13) {return false;}\" value=\"\" />\n";
1553 break;
1554 case 3: // Radio-button.
1555 $selects = explode( "\n", $opt['value'] );
1556 $i = 0;
1557 foreach ( $selects as $v ) {
1558 $r .= '<label for="itemNEWOption[' . $post_id . '][' . $key . '][' . $optcode . ']' . $i . '" class="iopt_radio_label"><input name="itemNEWOption[' . $post_id . '][' . $key . '][' . $optcode . ']" type="radio" id="itemNEWOption[' . $post_id . '][' . $key . '][' . $optcode . ']' . $i . '" class="iopt_radio" value="' . urlencode($v) . '">' . esc_html($v) . "</label>\n";
1559 $i++;
1560 }
1561 break;
1562 case 4: // Check-box.
1563 $selects = explode( "\n", $opt['value'] );
1564 $i = 0;
1565 foreach ( $selects as $v ) {
1566 $r .= '<label for="itemNEWOption[' . $post_id . '][' . $key . '][' . $optcode . ']' . $i . '" class="iopt_checkbox_label"><input name="itemNEWOption[' . $post_id . '][' . $key . '][' . $optcode . ']" type="checkbox" id="itemNEWOption[' . $post_id . '][' . $key . '][' . $optcode . ']' . $i . '" class="iopt_checkbox" value="' . urlencode($v) . '">' . esc_html($v) . "</label>\n";
1567 $i++;
1568 }
1569 break;
1570 case 5: // Text-area.
1571 $r .= "\n<textarea name=\"itemNEWOption[{$post_id}][{$key}][{$optcode}]\" id=\"itemNEWOption[{$post_id}][{$key}][{$optcode}]\" class=\"iopt_textarea\"></textarea>\n";
1572 break;
1573 }
1574 $r .= "<input name=\"optNEWCode[{$post_id}][{$key}][{$optcode}]\" type=\"hidden\" id=\"optNEWCode[{$post_id}][{$key}][{$optcode}]\" value=\"{$optcode}\" />\n";
1575 $r .= "<input name=\"optNEWEssential[{$post_id}][{$key}][{$optcode}]\" type=\"hidden\" id=\"optNEWEssential[{$post_id}][{$key}][{$optcode}]\" value=\"{$essential}\" />\n";
1576 $r .= "</div>\n";
1577 endforeach;
1578 $r .= "</td>\n";
1579 endif;
1580 $r .= "</tr>\n";
1581 endforeach;
1582 $r .= "</tbody>\n";
1583 $r .= "</table>\n";
1584
1585 $r .= "</div>\n";
1586
1587 $r = apply_filters( 'usces_filter_get_order_item', $r, $item_code, $post_id );
1588
1589 return $r;
1590 }
1591
1592 /**
1593 * Item option ajax
1594 */
1595 function item_option_ajax() {
1596
1597 $args = array(
1598 'ID' => FILTER_VALIDATE_INT,
1599 'action' => FILTER_DEFAULT,
1600 'update' => FILTER_DEFAULT,
1601 'delete' => FILTER_DEFAULT,
1602 'meta' => FILTER_DEFAULT,
1603 'select' => FILTER_DEFAULT,
1604 'sort' => FILTER_VALIDATE_INT,
1605 );
1606
1607 $inputs = filter_input_array( INPUT_POST, $args );
1608
1609 check_admin_referer( 'admin_setup', 'wc_nonce' );
1610 if ( ! current_user_can( 'edit_posts' ) ) {
1611 wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
1612 }
1613
1614 if ( 'item_option_ajax' !== $inputs['action'] || null === $inputs['ID'] ) {
1615 die(0);
1616 }
1617
1618 $post_id = $inputs['ID'];
1619
1620 if ( null !== $inputs['update'] ) {
1621
1622 $id = up_item_option_meta( $post_id );
1623
1624 } elseif ( null !== $inputs['delete'] ) {
1625
1626 $id = del_item_option_meta( $post_id );
1627
1628 } elseif ( null !== $inputs['select'] ) {
1629
1630 $res = select_common_option( $post_id );
1631 wp_send_json( $res );
1632
1633 } elseif ( null !== $inputs['sort'] ) {
1634
1635 $id = usces_sort_item_opts( $post_id, $inputs['meta'] );
1636
1637 } else {
1638
1639 $id = add_item_option_meta( $post_id );
1640
1641 }
1642
1643 $opts = usces_get_opts( $post_id, 'sort', false );
1644
1645 $r = '';
1646 foreach ( $opts as $opt ) {
1647 $r .= _list_item_option_meta_row( $opt );
1648 }
1649
1650 $response = array(
1651 'meta_id' => $id,
1652 'meta_row' => $r,
1653 );
1654 wp_send_json( $response );
1655 }
1656
1657 /**
1658 * Item SKU ajax
1659 */
1660 function item_sku_ajax() {
1661 global $usces;
1662
1663 $id = '';
1664 if ( 'item_sku_ajax' !== $_POST['action'] ) {
1665 die(0);
1666 }
1667
1668 $post_id = (int) $_POST['ID'];
1669 $msg = '';
1670
1671 if ( ! current_user_can( 'wel_publish_products', $post_id ) ) {
1672 $response = array(
1673 'meta_id' => 0,
1674 'meta_row' => 0,
1675 'meta_msg' => __( 'You are not allowed to edit this item.', 'usces' ),
1676 );
1677 wp_send_json( $response );
1678 }
1679
1680 if ( isset( $_POST['update'] ) ) {
1681 check_ajax_referer( 'admin_setup', 'wc_nonce' );
1682 $id = up_item_sku_meta( $post_id );
1683
1684 } elseif ( isset( $_POST['delete'] ) ) {
1685 check_ajax_referer( 'admin_setup', 'wc_nonce' );
1686 $id = del_item_sku_meta( $post_id );
1687
1688 } elseif ( isset( $_POST['select'] ) ) {
1689 check_ajax_referer( 'admin_setup', 'wc_nonce' );
1690 $response = select_item_sku( $post_id );
1691 wp_send_json( $response );
1692
1693 } elseif ( isset( $_POST['sort'] ) ) {
1694 check_ajax_referer( 'admin_setup', 'wc_nonce' );
1695 $id = usces_sort_item_skus( $post_id, $_POST['meta'] );
1696
1697 } else {
1698 check_ajax_referer( 'admin_setup', 'wc_nonce' );
1699 $id = add_item_sku_meta( $post_id );
1700
1701 }
1702 $msg .= apply_filters( 'usces_filter_item_sku_message', $msg, $id, $post_id );
1703 // $skus = $usces->get_skus( $post_id );
1704 $skus = wel_get_skus( $post_id, 'sort', false );
1705
1706 $r = '';
1707
1708 foreach ( (array) $skus as $sku ) {
1709 $r .= _list_item_sku_meta_row( $sku );
1710 }
1711 $response = array(
1712 'meta_id' => $id,
1713 'meta_row' => $r,
1714 'meta_msg' => $msg,
1715 );
1716
1717 wp_send_json( $response );
1718 }
1719
1720 /**
1721 * Item save metadata
1722 *
1723 * @param int $post_id Post ID.
1724 * @param object $post Post data.
1725 */
1726 function item_save_metadata( $post_id, $post ) {
1727 global $usces, $wpdb;
1728
1729 $message = '';
1730 $item_data = wel_get_item( $post_id, false );
1731 $args = array(
1732 'usces_nonce' => FILTER_SANITIZE_SPECIAL_CHARS,
1733 'itemCode' => FILTER_SANITIZE_SPECIAL_CHARS,
1734 'itemName' => FILTER_DEFAULT,
1735 'itemRestriction' => FILTER_SANITIZE_SPECIAL_CHARS,
1736 'itemPointrate' => FILTER_VALIDATE_INT,
1737 'itemGpNum1' => FILTER_VALIDATE_INT,
1738 'itemGpNum2' => FILTER_VALIDATE_INT,
1739 'itemGpNum3' => FILTER_VALIDATE_INT,
1740 'itemGpDis1' => FILTER_VALIDATE_INT,
1741 'itemGpDis2' => FILTER_VALIDATE_INT,
1742 'itemGpDis3' => FILTER_VALIDATE_INT,
1743 'itemOrderAcceptable' => FILTER_DEFAULT,
1744 'itemShipping' => FILTER_VALIDATE_INT,
1745 'itemDeliveryMethod' => array(
1746 'filter' => FILTER_SANITIZE_SPECIAL_CHARS,
1747 'flags' => FILTER_REQUIRE_ARRAY,
1748 ),
1749 'itemShippingCharge' => FILTER_VALIDATE_INT,
1750 'itemIndividualSCharge' => FILTER_DEFAULT,
1751 'item_charging_type' => FILTER_VALIDATE_INT,
1752 'item_division' => FILTER_SANITIZE_SPECIAL_CHARS,
1753 'dlseller_date' => FILTER_SANITIZE_SPECIAL_CHARS,
1754 'dlseller_file' => FILTER_SANITIZE_SPECIAL_CHARS,
1755 'dlseller_interval' => FILTER_VALIDATE_INT,
1756 'dlseller_validity' => FILTER_VALIDATE_INT,
1757 'dlseller_version' => FILTER_SANITIZE_SPECIAL_CHARS,
1758 'dlseller_author' => FILTER_SANITIZE_SPECIAL_CHARS,
1759 'dlseller_purchases' => FILTER_VALIDATE_INT,
1760 'dlseller_downloads' => FILTER_VALIDATE_INT,
1761 'item_chargingday' => FILTER_DEFAULT,
1762 'item_frequency' => FILTER_DEFAULT,
1763 'wcad_regular_unit' => FILTER_DEFAULT,
1764 'wcad_regular_interval' => FILTER_DEFAULT,
1765 'wcad_regular_frequency' => FILTER_DEFAULT,
1766 'select_sku_switch' => FILTER_VALIDATE_INT,
1767 'select_sku_display' => FILTER_VALIDATE_INT,
1768 'select_sku' => array(
1769 'filter' => FILTER_SANITIZE_SPECIAL_CHARS,
1770 'flags' => FILTER_REQUIRE_ARRAY,
1771 ),
1772 'deferred_payment_propriety' => FILTER_VALIDATE_INT,
1773 'atodene_propriety' => FILTER_VALIDATE_INT,
1774 'structuredDataSku' => FILTER_SANITIZE_SPECIAL_CHARS,
1775 'lower_limit' => FILTER_SANITIZE_SPECIAL_CHARS,
1776 'popularity' => FILTER_SANITIZE_SPECIAL_CHARS,
1777 'main_price' => FILTER_SANITIZE_SPECIAL_CHARS,
1778 'itemPicts' => FILTER_DEFAULT,
1779 'itemAdvanced' => array(
1780 'filter' => FILTER_DEFAULT,
1781 'flags' => FILTER_REQUIRE_ARRAY,
1782 ),
1783 'itemsku' => array(
1784 'filter' => FILTER_DEFAULT,
1785 'flags' => FILTER_REQUIRE_ARRAY,
1786 ),
1787 'itemopt' => array(
1788 'filter' => FILTER_DEFAULT,
1789 'flags' => FILTER_REQUIRE_ARRAY,
1790 ),
1791 );
1792
1793 $inputs = filter_input_array( INPUT_POST, $args );
1794 $page = filter_input( INPUT_GET, 'page', FILTER_SANITIZE_SPECIAL_CHARS );
1795 // Permission check.
1796 if ( 'usces_itemedit' === $page || 'usces_itemnew' === $page ) {
1797 if ( ! current_user_can( 'edit_post', $post_id ) ) {
1798 $usces->set_action_status( 'error', 'ERROR : ' . __( 'Sorry, you do not have the right to edit this post.' ) );
1799 return $post_id;
1800 }
1801
1802 // Do nothing when saving auto-draft.
1803 if ( isset( $post->post_status ) && 'auto-draft' === $post->post_status ) {
1804 return $post_id;
1805 }
1806 $usces->set_item_mime( $post_id, 'item' );
1807
1808 } else {
1809
1810 return $post_id;
1811
1812 }
1813
1814 if ( ! isset( $inputs['usces_nonce'] ) || ! wp_verify_nonce( $inputs['usces_nonce'], 'usc-e-shop' ) ) {
1815 return $post_id;
1816 }
1817
1818 // Check if it is an automatic save routine. If so, do not submit the form (do nothing).
1819 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
1820 return $post_id;
1821 }
1822
1823 // Do nothing when saving other plugins.
1824 if ( isset( $post->post_type ) && 'post' !== $post->post_type ) {
1825 return $post_id;
1826 }
1827
1828 if ( preg_match( '/[^0-9a-zA-Z\-_]/', $inputs['itemCode'] ) ) {
1829 // Do nothing.
1830 }
1831
1832 if ( empty( $inputs['itemCode'] ) ) {
1833 $inputs['itemCode'] = '';
1834 $message .= __( 'Product code has not been entered.', 'usces' ) . '<br />';
1835 } elseif ( $res = usces_is_same_itemcode( $post_id, $inputs['itemCode'] ) ) {
1836 $message .= 'post_ID ';
1837 foreach ( $res as $postid ) {
1838 $message .= $postid . ', ';
1839 }
1840 $message .= __( 'Same product code is registered here.', 'usces' ) . '<br />';
1841 $usces->set_action_status( 'error', 'ERROR : ' . $message );
1842 }
1843 $item_data['itemCode'] = $inputs['itemCode'];
1844
1845 if ( null !== $inputs['itemName'] ) {
1846 $item_data['itemName'] = trim( $inputs['itemName'] );
1847 }
1848 if ( null !== $inputs['itemRestriction'] ) {
1849 $item_data['itemRestriction'] = trim( $inputs['itemRestriction'] );
1850 }
1851 if ( null !== $inputs['itemPointrate'] ) {
1852 $item_data['itemPointrate'] = (int) $inputs['itemPointrate'];
1853 }
1854 if ( null !== $inputs['itemGpNum1'] ) {
1855 $item_data['itemGpNum1'] = (int) $inputs['itemGpNum1'];
1856 }
1857 if ( null !== $inputs['itemGpNum2'] ) {
1858 $item_data['itemGpNum2'] = (int) $inputs['itemGpNum2'];
1859 }
1860 if ( null !== $inputs['itemGpNum3'] ) {
1861 $item_data['itemGpNum3'] = (int) $inputs['itemGpNum3'];
1862 }
1863 if ( null !== $inputs['itemGpDis1'] ) {
1864 $item_data['itemGpDis1'] = (int) $inputs['itemGpDis1'];
1865 }
1866 if ( null !== $inputs['itemGpDis2'] ) {
1867 $item_data['itemGpDis2'] = (int) $inputs['itemGpDis2'];
1868 }
1869 if ( null !== $inputs['itemGpDis3'] ) {
1870 $item_data['itemGpDis3'] = (int) $inputs['itemGpDis3'];
1871 }
1872
1873 $item_data['itemOrderAcceptable'] = null !== $inputs['itemOrderAcceptable'] ? 1 : 0;
1874
1875 if ( null !== $inputs['itemShipping'] ) {
1876 $item_data['itemShipping'] = (int) $inputs['itemShipping'];
1877 }
1878 if ( null !== $inputs['itemDeliveryMethod'] ) {
1879 $dmethod = array();
1880 foreach ( (array) $inputs['itemDeliveryMethod'] as $dmid ) {
1881 $dmethod[] = $dmid;
1882 }
1883 $item_data['itemDeliveryMethod'] = $dmethod;
1884 }
1885 if ( null !== $inputs['itemShippingCharge'] ) {
1886 $item_data['itemShippingCharge'] = (int) $inputs['itemShippingCharge'];
1887 }
1888
1889 $item_data['itemIndividualSCharge'] = null !== $inputs['itemIndividualSCharge'] ? 1 : 0;
1890
1891 $options = get_option( 'usces', array() );
1892 $acting_settings = ( isset( $options['acting_settings'] ) ) ? $options['acting_settings'] : array();
1893 if ( isset( $acting_settings['welcart'] ) && isset( $acting_settings['welcart']['atodene_byitem'] ) && 'on' === $acting_settings['welcart']['atodene_byitem'] ) {
1894 $item_data['atodene_propriety'] = null !== $inputs['atodene_propriety'] ? (int) $inputs['atodene_propriety'] : 0;
1895 }
1896 $options_ex = get_option( 'usces_ex', array() );
1897 $system_settings = ( isset( $options_ex['system'] ) ) ? $options_ex['system'] : array();
1898 if ( isset( $system_settings['atobaraicsv'] ) && isset( $system_settings['atobaraicsv']['each_item'] ) && 1 === (int) $system_settings['atobaraicsv']['each_item'] ) {
1899 $item_data['atobarai_propriety'] = null !== $inputs['deferred_payment_propriety'] ? (int) $inputs['deferred_payment_propriety'] : 0;
1900 }
1901
1902 if ( 1 === (int) USCES_STRUCTURED_DATA_PRODUCT::$opts['status'] ) {
1903 $item_data['structuredDataSku'] = ( isset( $inputs['structuredDataSku'] ) ) ? trim( $inputs['structuredDataSku'] ) : '';
1904 }
1905
1906 wel_update_item_data( $item_data, $post_id, true );
1907 $item_data = wel_get_item( $post_id, false );
1908
1909 // SKU.
1910 if ( null !== $inputs['itemsku'] ) {
1911
1912 $meta_ids = array();
1913 $codes = array();
1914 $uniq_code = false;
1915 $irreg_code = false;
1916 $irreg_price = false;
1917
1918 $skus = wel_get_skus( $post_id, 'meta_id', false );
1919
1920 foreach ( $inputs['itemsku'] as $mid => $temp ) {
1921 $meta_ids[] = $mid;
1922 }
1923 $meta_ids = array_unique( $meta_ids );
1924 foreach ( $meta_ids as $meta_id ) {
1925
1926 $skucode = isset( $inputs['itemsku'][ $meta_id ]['key'] ) ? trim( $inputs['itemsku'][ $meta_id ]['key'] ) : '';
1927 $msgsku = apply_filters( 'usces_filter_before_item_save_metadata_sku', null, $post_id, $meta_id, $skucode );
1928 if ( ! empty( $msgsku ) ) {
1929 $message .= $msgsku;
1930 continue;
1931 }
1932
1933 $skucprice = isset( $inputs['itemsku'][ $meta_id ]['cprice'] ) ? trim( $inputs['itemsku'][ $meta_id ]['cprice'] ) : 0;
1934 $skuprice = isset( $inputs['itemsku'][ $meta_id ]['price'] ) ? trim( $inputs['itemsku'][ $meta_id ]['price'] ) : 0;
1935 $skustocknum = isset( $inputs['itemsku'][ $meta_id ]['zaikonum'] ) ? trim( $inputs['itemsku'][ $meta_id ]['zaikonum'] ) : 0;
1936 $skustock = isset( $inputs['itemsku'][ $meta_id ]['zaiko'] ) ? (int) $inputs['itemsku'][ $meta_id ]['zaiko'] : '';
1937 $skuname = isset( $inputs['itemsku'][ $meta_id ]['skudisp'] ) ? trim( $inputs['itemsku'][ $meta_id ]['skudisp'] ) : '';
1938 $skuunit = isset( $inputs['itemsku'][ $meta_id ]['skuunit'] ) ? trim( $inputs['itemsku'][ $meta_id ]['skuunit'] ) : '';
1939 $skugp = isset( $inputs['itemsku'][ $meta_id ]['skugptekiyo'] ) ? (int) $inputs['itemsku'][ $meta_id ]['skugptekiyo'] : 0;
1940 $skusort = isset( $inputs['itemsku'][ $meta_id ]['sort'] ) ? $inputs['itemsku'][ $meta_id ]['sort'] : 0;
1941 $skutaxrate = isset( $inputs['itemsku'][ $meta_id ]['applicable_taxrate'] ) ? $inputs['itemsku'][ $meta_id ]['applicable_taxrate'] : '';
1942
1943 $sku = $skus[ $meta_id ];
1944
1945 $sku['code'] = $skucode;
1946 $sku['name'] = $skuname;
1947 $sku['cprice'] = $skucprice;
1948 $sku['price'] = $skuprice;
1949 $sku['unit'] = $skuunit;
1950 $sku['stocknum'] = $skustocknum;
1951 $sku['stock'] = $skustock;
1952 $sku['gp'] = $skugp;
1953 $sku['sort'] = $skusort;
1954 if ( ! empty( $skutaxrate ) ) {
1955 $sku['taxrate'] = $skutaxrate;
1956 }
1957 $sku = $usces->stripslashes_deep_post( $sku );
1958 $sku = apply_filters( 'usces_filter_item_save_sku_metadata', $sku, $meta_id );
1959
1960 wel_update_sku_data_by_id( $meta_id, $post_id, $sku );
1961
1962 if ( in_array( $skucode, $codes ) ) {
1963 $uniq_code = true;
1964 }
1965
1966 if ( WCUtils::is_blank( $skucode ) ) {
1967 $irreg_code = true;
1968 }
1969
1970 if ( WCUtils::is_blank( $skuprice ) || preg_match( '/[^0-9.]/', $skuprice ) || 1 < substr_count( $skuprice, '.' ) ) {
1971 $irreg_price = true;
1972 }
1973
1974 $codes[] = $skucode;
1975 }
1976
1977 if ( $uniq_code ) {
1978 $message .= __( 'SKU code is duplicated.', 'usces' ) . '<br />';
1979 }
1980 if ( $irreg_code ) {
1981 $message .= __( 'SKU code is invalid.', 'usces' ) . '<br />';
1982 }
1983 if ( $irreg_price ) {
1984 $message .= __( 'SKU of invalid selling price exists.', 'usces' ) . '<br />';
1985 }
1986 }
1987
1988 // OPT.
1989 if ( null !== $inputs['itemopt'] ) {
1990 $meta_ids = array();
1991 $names = array();
1992 $uniq_name = false;
1993 $irreg_name = false;
1994 $irreg_value = false;
1995
1996 $opts = wel_get_opts( $post_id, 'meta_id', false );
1997
1998 foreach ( $inputs['itemopt'] as $mid => $temp ) {
1999 $meta_ids[] = $mid;
2000 }
2001 $meta_ids = array_unique( $meta_ids );
2002 foreach ( $meta_ids as $meta_id ) {
2003
2004 $optname = isset( $inputs['itemopt'][ $meta_id ]['name'] ) ? $inputs['itemopt'][ $meta_id ]['name'] : '';
2005 $optmeans = isset( $inputs['itemopt'][ $meta_id ]['means'] ) ? (int) $inputs['itemopt'][ $meta_id ]['means'] : 0;
2006 $optessential = isset( $inputs['itemopt'][ $meta_id ]['essential'] ) ? $inputs['itemopt'][ $meta_id ]['essential'] : 0;
2007 $optsort = isset( $inputs['itemopt'][ $meta_id ]['sort'] ) ? $inputs['itemopt'][ $meta_id ]['sort'] : 0;
2008 $optvalue = isset( $inputs['itemopt'][ $meta_id ]['value'] ) ? trim( $inputs['itemopt'][ $meta_id ]['value'] ) : '';
2009
2010 $opt = $opts[ $meta_id ];
2011
2012 $opt['name'] = str_replace( "\\", '', $optname );
2013 $opt['value'] = str_replace( "\\", '', $optvalue );
2014 $opt['means'] = $optmeans;
2015 $opt['essential'] = $optessential;
2016 $opt['sort'] = $optsort;
2017
2018 $opt = $usces->stripslashes_deep_post( $opt );
2019
2020 wel_update_opt_data_by_id( $meta_id, $post_id, $opt );
2021
2022 if ( in_array( $optname, $names ) ) {
2023 $uniq_name = true;
2024 }
2025
2026 if ( WCUtils::is_blank( $optname ) ) {
2027 $irreg_name = true;
2028 }
2029
2030 if ( WCUtils::is_blank( $optvalue ) && 1 >= $optmeans ) {
2031 $irreg_value = true;
2032 }
2033
2034 $names[] = $optname;
2035 }
2036
2037 if ( $uniq_name ) {
2038 $message .= __( 'Commodity option option name duplicates exist.', 'usces' ) . '<br />';
2039 }
2040 if ( $irreg_name ) {
2041 $message .= __( 'Commodity option not entered there is the option name.', 'usces' ) . '<br />';
2042 }
2043 if ( $irreg_value ) {
2044 $message .= __( "If you select 'single select' and 'multi-select' the trade option, please enter the select value.", 'usces' ) . '<br />';
2045 }
2046 }
2047
2048 do_action( 'usces_action_save_product', $post_id, $post );
2049 $message = apply_filters( 'usces_filter_save_product_message', $message, $post_id );
2050
2051 if ( $message ) {
2052 $usces->set_action_status( 'error', 'ERROR : ' . $message );
2053 } else {
2054 $usces->set_action_status( 'success', __( 'Registration of the product is complete.', 'usces' ) );
2055 }
2056 }
2057
2058 /**
2059 * Link replace
2060 *
2061 * @param string $para Parameters.
2062 * @return string
2063 */
2064 function usces_link_replace( $para ) {
2065 $str = 'admin.php?page=usces_itemedit&';
2066 $url = preg_replace( '|post\.php\?|i', $str, $para );
2067 return $url;
2068 }
2069
2070 /**
2071 * Count posts
2072 *
2073 * @param string $type Type.
2074 * @param string $perm perm.
2075 * @return object
2076 */
2077 function usces_count_posts( $type = 'post', $perm = '' ) {
2078 global $wpdb;
2079
2080 $user = wp_get_current_user();
2081
2082 $cache_key = $type;
2083
2084 $query = "SELECT post_status, COUNT( * ) AS `num_posts` FROM {$wpdb->posts} WHERE post_type = %s AND post_mime_type = 'item'";
2085 if ( 'readable' == $perm && is_user_logged_in() ) {
2086 if ( ! current_user_can( "read_private_{$type}s" ) ) {
2087 $cache_key .= '_' . $perm . '_' . $user->ID;
2088 $query .= " AND (post_status != 'private' OR ( post_author = '$user->ID' AND post_status = 'private' ))";
2089 }
2090 }
2091 $query .= ' GROUP BY post_status';
2092
2093 $count = wp_cache_get( $cache_key, 'counts' );
2094 if ( false !== $count ) {
2095 $count = $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A );
2096 }
2097
2098 $stats = array();
2099 foreach ( (array) $count as $row_num => $row ) {
2100 $stats[ $row['post_status'] ] = $row['num_posts'];
2101 }
2102
2103 $stats = (object) $stats;
2104 wp_cache_set( $cache_key, $stats, 'counts' );
2105
2106 return $stats;
2107 }
2108
2109 /**
2110 * Custom order meta row
2111 *
2112 * @param string $key Key.
2113 * @param array $entry Entry data.
2114 * @return string
2115 */
2116 function _list_custom_order_meta_row( $key, $entry ) {
2117 $r = '';
2118 $style = '';
2119 $key = esc_attr( $key );
2120
2121 $name = esc_attr( $entry['name'] );
2122 $means = get_option( 'usces_custom_order_select', array() );
2123 $meansoption = '';
2124 foreach ( $means as $meankey => $meanvalue ) {
2125 $meansoption .= '<option value="' . esc_attr( $meankey ) . '"' . selected( $meankey, $entry['means'], false ) . '>' . esc_html( $meanvalue ) . "</option>\n";
2126 }
2127 $essential = ( 1 === (int) $entry['essential'] ) ? ' checked="checked"' : '';
2128 $value = '';
2129 if ( is_array( $entry['value'] ) ) {
2130 foreach ( $entry['value'] as $k => $v ) {
2131 $value .= $v . "\n";
2132 }
2133 }
2134 $value = esc_attr( trim( $value ) );
2135
2136 $r .= "\n\t<tr id=\"csod-{$key}\" class=\"{$style}\">";
2137 $r .= "\n\t\t<td class=\"left\"><div><input type=\"text\" name=\"csod[{$key}][key]\" id=\"csod[{$key}][key]\" class=\"optname\" size=\"20\" value=\"{$key}\" readonly /></div>";
2138 $r .= "\n\t\t<div><input type=\"text\" name=\"csod[{$key}][name]\" id=\"csod[{$key}][name]\" class=\"optname\" size=\"20\" value=\"{$name}\" /></div>";
2139 $r .= "\n\t\t<div class=\"optcheck\"><select name=\"csod[{$key}][means]\" id=\"csod[{$key}][means]\">" . $meansoption . "</select>\n";
2140 $r .= "<input type=\"checkbox\" name=\"csod[{$key}][essential]\" id=\"csod[{$key}][essential]\" value=\"1\"{$essential} /><label for=\"csod[{$key}][essential]\">" . esc_html__( 'Required', 'usces' ) . '</label></div>';
2141 $r .= "\n\t\t<div class=\"submit\"><input type=\"button\" class=\"button\" name=\"del_csod[{$key}]\" id=\"del_csod[{$key}]\" value=\"" . esc_attr__( 'Delete' ) . "\" onclick=\"customField.delOrder('{$key}');\" />";
2142 $r .= "\n\t\t<input type=\"button\" class=\"button\" name=\"upd_csod[{$key}]\" id=\"upd_csod[{$key}]\" value=\"" . esc_attr__( 'Update' ) . "\" onclick=\"customField.updOrder('{$key}');\" /></div>";
2143 $r .= "\n\t\t<div id=\"csod_loading-{$key}\" class=\"meta_submit_loading\"></div>";
2144 $r .= '</td>';
2145 $r .= "\n\t\t<td class=\"item-opt-value\"><textarea name=\"csod[{$key}][value]\" id=\"csod[{$key}][value]\" class=\"optvalue\">{$value}</textarea></td>\n\t</tr>";
2146 return $r;
2147 }
2148
2149 /**
2150 * Has custom field meta
2151 *
2152 * @param string $fieldname Field name.
2153 * @return mixed
2154 */
2155 function usces_has_custom_field_meta( $fieldname ) {
2156 switch ( $fieldname ) {
2157 case 'order':
2158 $field = 'usces_custom_order_field';
2159 break;
2160 case 'customer':
2161 $field = 'usces_custom_customer_field';
2162 break;
2163 case 'delivery':
2164 $field = 'usces_custom_delivery_field';
2165 break;
2166 case 'member':
2167 $field = 'usces_custom_member_field';
2168 break;
2169 case 'admin_member':
2170 $field = 'usces_admin_custom_member_field';
2171 break;
2172 default:
2173 return array();
2174 }
2175 $fields = get_option( $field );
2176 if ( empty( $fields ) ) {
2177 $meta = array();
2178 } elseif ( is_array( $fields ) ) {
2179 $meta = $fields;
2180 } else {
2181 $meta = wel_safe_unserialize( $fields );
2182 }
2183 return $meta;
2184 }
2185
2186 /**
2187 * Get info ajax
2188 *
2189 * @return void
2190 */
2191 function usces_getinfo_ajax() {
2192 global $wp_version;
2193
2194 $wcex = usces_get_wcex();
2195 $wcex_str = join( ',', array_map(
2196 function( $key, $values ) {
2197 return $key . '-' . $values['version'];
2198 },
2199 array_keys( $wcex ), $wcex )
2200 );
2201
2202 $theme_data = wp_get_theme();
2203 $theme = urlencode( $theme_data->get( 'Name' ) . '-' . $theme_data->get( 'Version' ) );
2204
2205 $args = array(
2206 'timeout' => 30,
2207 'headers' => array( 'Content-Type' => 'application/x-www-form-urlencoded' ),
2208 'body' => array(
2209 'v' => urlencode( USCES_VERSION ),
2210 'wcid' => urlencode( get_option( 'usces_wcid' ) ),
2211 'locale'=> urlencode( get_locale() ),
2212 'theme' => $theme,
2213 'wcex' => urlencode( $wcex_str ),
2214 'wcurl' => urlencode( get_home_url() )
2215 )
2216 );
2217
2218 $response = wp_remote_post( 'https://www.welcart.com/util/welcart_information2.php', $args );
2219
2220 if ( is_wp_error( $response ) ) {
2221 die( 'ERROR' );
2222 }
2223
2224 $body = wp_remote_retrieve_body( $response );
2225 if ( empty( $body ) ) {
2226 die( 'ERROR' );
2227 }
2228
2229 $res = '';
2230 preg_match_all( "/<(title|data)>(.*?)<\/(title|data)>/s", $body, $matches, PREG_SET_ORDER );
2231
2232 foreach ( $matches as $match ) {
2233 switch ( $match[1] ) {
2234 case 'title':
2235 $res .= '<div style="text-align: center;border-bottom: 1px dotted #CCCCCC;width: 80%;margin-bottom: 10px;padding-bottom: 3px; margin-right: auto; margin-left: auto;"><strong>' . $match[2] . '</strong></div><ul>';
2236 break;
2237 case 'data':
2238 $res .= '<li>' . $match[2] . '</li>';
2239 break;
2240 }
2241 }
2242 $res .= '</ul>';
2243
2244 die( $res );
2245 }
2246
2247 /**
2248 * Custom field ajax
2249 */
2250 function custom_field_ajax() {
2251 global $usces;
2252
2253 check_admin_referer( 'custom_field_ajax', 'wc_nonce' );
2254 if ( ! current_user_can( 'wel_manage_setting' ) ) {
2255 wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
2256 }
2257
2258 $_POST = $usces->stripslashes_deep_post( $_POST );
2259 $data = array(
2260 'status' => 'OK',
2261 'msg' => '',
2262 );
2263
2264 if ( 'custom_field_ajax' !== $_POST['action'] ) {
2265 $data['status'] = 'NG';
2266 wp_send_json( $data );
2267 }
2268
2269 switch ( $_POST['field'] ) {
2270 case 'order':
2271 $field = 'usces_custom_order_field';
2272 break;
2273 case 'customer':
2274 $field = 'usces_custom_customer_field';
2275 break;
2276 case 'delivery':
2277 $field = 'usces_custom_delivery_field';
2278 break;
2279 case 'member':
2280 $field = 'usces_custom_member_field';
2281 break;
2282 case 'admin_member':
2283 $field = 'usces_admin_custom_member_field';
2284 break;
2285 default:
2286 $data['status'] = 'NG';
2287 wp_send_json( $data );
2288 }
2289
2290 $meta = usces_has_custom_field_meta( $_POST['field'] );
2291 $dupkey = 0;
2292 $dupname = 0;
2293
2294 if ( isset( $_POST['add'] ) ) {
2295 $newkey = ( isset( $_POST['newkey'] ) ) ? trim( $_POST['newkey'] ) : '';
2296 $newname = ( isset( $_POST['newname'] ) ) ? trim( $_POST['newname'] ) : '';
2297 $newmeans = ( isset( $_POST['newmeans'] ) ) ? $_POST['newmeans'] : 0;
2298 $newessential = ( isset( $_POST['newessential'] ) ) ? $_POST['newessential'] : 0;
2299 $newposition = ( isset( $_POST['newposition'] ) ) ? trim( $_POST['newposition'] ) : '';
2300
2301 if ( 2 === (int) $newmeans || 5 === (int) $newmeans ) { // Text or Textarea.
2302 $newvalue = '';
2303 $nv = $newvalue;
2304 $required_entry = ( ! empty( $newkey ) && ! empty( $newname ) ) ? true : false;
2305 } else {
2306 $newvalue = ( isset( $_POST['newvalue'] ) ) ? explode( "\n", trim( $_POST['newvalue'] ) ) : '';
2307 foreach ( (array) $newvalue as $v ) {
2308 if ( ! WCUtils::is_blank( $v ) ) {
2309 $nv[] = trim( $v );
2310 }
2311 }
2312 $required_entry = ( ( ! empty( $newvalue ) ) && ! empty( $newkey ) && ! empty( $newname ) ) ? true : false;
2313 }
2314
2315 if ( ! array_key_exists( $newkey, $meta ) ) {
2316 if ( $required_entry ) {
2317 $meta[ $newkey ]['name'] = $newname;
2318 $meta[ $newkey ]['means'] = $newmeans;
2319 $meta[ $newkey ]['essential'] = $newessential;
2320 $meta[ $newkey ]['value'] = $nv;
2321 if ( ! WCUtils::is_blank( $newposition ) ) {
2322 $meta[ $newkey ]['position'] = $newposition;
2323 } elseif ( ( 'admin_member' === $_POST['field'] ) && WCUtils::is_blank( $newposition ) ) {
2324 $meta[ $newkey ]['position'] = 'fax_after';
2325 }
2326 update_option( $field, $meta );
2327 }
2328 } else {
2329 $dupkey = 1;
2330 }
2331
2332 } elseif ( isset( $_POST['update'] ) ) {
2333 $key = ( isset( $_POST['key'] ) ) ? trim( $_POST['key'] ) : '';
2334 $name = ( isset( $_POST['name'] ) ) ? trim( $_POST['name'] ) : '';
2335 $means = ( isset( $_POST['means'] ) ) ? $_POST['means'] : 0;
2336 $essential = ( isset( $_POST['essential'] ) ) ? $_POST['essential'] : 0;
2337 $position = ( isset( $_POST['position'] ) ) ? trim( $_POST['position'] ) : '';
2338
2339 if ( 2 === (int) $means || 5 === (int) $means ) { // Text or Textarea.
2340 $value = '';
2341 $nv = $value;
2342 $required_entry = ( ! empty( $key ) && ! empty( $name ) ) ? true : false;
2343
2344 } else {
2345 $value = ( isset( $_POST['value'] ) ) ? explode( "\n", trim( $_POST['value'] ) ) : '';
2346 foreach ( (array) $value as $v ) {
2347 if ( ! WCUtils::is_blank( $v ) ) {
2348 $nv[] = trim( $v );
2349 }
2350 }
2351 $required_entry = ( ( ! empty( $value ) ) && ! empty( $key ) && ! empty( $name ) ) ? true : false;
2352 }
2353
2354 if ( $required_entry ) {
2355 $meta[ $key ]['name'] = $name;
2356 $meta[ $key ]['means'] = $means;
2357 $meta[ $key ]['essential'] = $essential;
2358 $meta[ $key ]['value'] = $nv;
2359 if ( ! WCUtils::is_blank( $position ) ) {
2360 $meta[ $key ]['position'] = $position;
2361 }
2362 update_option( $field, $meta );
2363 }
2364
2365 } elseif ( isset( $_POST['delete'] ) ) {
2366 $key = ( isset( $_POST['key'] ) ) ? trim( $_POST['key'] ) : '';
2367 unset( $meta[ $key ] );
2368 update_option( $field, $meta );
2369 }
2370
2371 $r = '';
2372 switch ( $_POST['field'] ) {
2373 case 'order':
2374 foreach ( $meta as $key => $entry ) {
2375 $r .= _list_custom_order_meta_row( $key, $entry );
2376 }
2377 break;
2378 case 'customer':
2379 foreach ( $meta as $key => $entry ) {
2380 $r .= _list_custom_customer_meta_row( $key, $entry );
2381 }
2382 break;
2383 case 'delivery':
2384 foreach ( $meta as $key => $entry ) {
2385 $r .= _list_custom_delivery_meta_row( $key, $entry );
2386 }
2387 break;
2388 case 'member':
2389 foreach ( $meta as $key => $entry ) {
2390 $r .= _list_custom_member_meta_row( $key, $entry );
2391 }
2392 break;
2393 case 'admin_member':
2394 foreach ( $meta as $key => $entry ) {
2395 $r .= _list_admin_custom_member_meta_row( $key, $entry );
2396 }
2397 break;
2398 }
2399
2400 $data['list'] = $r;
2401 $data['dupkey'] = $dupkey;
2402 $data['dupname'] = $dupname;
2403 wp_send_json( $data );
2404 }
2405
2406 /**
2407 * List custom customer meta row
2408 *
2409 * @param string $key Key.
2410 * @param array $entry Entry data.
2411 * @return string
2412 */
2413 function _list_custom_customer_meta_row( $key, $entry ) {
2414 $r = '';
2415 $style = '';
2416 $key = esc_attr( $key );
2417
2418 $name = esc_attr( $entry['name'] );
2419 $means = get_option( 'usces_custom_customer_select', array() );
2420 $meansoption = '';
2421 foreach ( $means as $meankey => $meanvalue ) {
2422 $meansoption .= '<option value="' . esc_attr( $meankey ) . '"' . selected( $meankey, $entry['means'], false ) . '>' . esc_html( $meanvalue ) . "</option>\n";
2423 }
2424 $essential = ( 1 === (int) $entry['essential'] ) ? ' checked="checked"' : '';
2425 $value = '';
2426 if ( is_array( $entry['value'] ) ) {
2427 foreach ( $entry['value'] as $k => $v ) {
2428 $value .= $v . "\n";
2429 }
2430 }
2431 $value = esc_attr( trim( $value ) );
2432 $positions = get_option( 'usces_custom_field_position_select', array() );
2433 $positionsoption = '';
2434 foreach ( $positions as $poskey => $posvalue ) {
2435 $positionsoption .= '<option value="' . esc_attr( $poskey ) . '"' . selected( $poskey, $entry['position'], false ) . '>' . esc_attr( $posvalue ) . "</option>\n";
2436 }
2437
2438 $r .= "\n\t<tr id=\"cscs-{$key}\" class=\"{$style}\">";
2439 $r .= "\n\t\t<td class=\"left\"><div><input type=\"text\" name=\"cscs[{$key}][key]\" id=\"cscs[{$key}][key]\" class=\"optname\" size=\"20\" value=\"{$key}\" readonly /></div>";
2440 $r .= "\n\t\t<div><input type=\"text\" name=\"cscs[{$key}][name]\" id=\"cscs[{$key}][name]\" class=\"optname\" size=\"20\" value=\"{$name}\" /></div>";
2441 $r .= "\n\t\t<div class=\"optcheck\"><select name=\"cscs[{$key}][means]\" id=\"cscs[{$key}][means]\">" . $meansoption . "</select>\n";
2442 $r .= "<input type=\"checkbox\" name=\"cscs[{$key}][essential]\" id=\"cscs[{$key}][essential]\" value=\"1\"{$essential} /><label for=\"cscs[{$key}][essential]\">" . esc_html__( 'Required', 'usces' ) . "</label>\n";
2443 $r .= "<select name=\"cscs[{$key}][position]\" id=\"cscs[{$key}][position]\">" . $positionsoption . '</select></div>';
2444 $r .= "\n\t\t<div class=\"submit\"><input type=\"button\" class=\"button\" name=\"del_cscs[{$key}]\" id=\"del_cscs[{$key}]\" value=\"" . esc_attr__( 'Delete' ) . "\" onclick=\"customField.delCustomer('{$key}');\" />";
2445 $r .= "\n\t\t<input type=\"button\" class=\"button\" name=\"upd_cscs[{$key}]\" id=\"upd_cscs[{$key}]\" value=\"" . esc_attr__( 'Update' ) . "\" onclick=\"customField.updCustomer('{$key}');\" /></div>";
2446 $r .= "\n\t\t<div id=\"cscs_loading-{$key}\" class=\"meta_submit_loading\"></div>";
2447 $r .= '</td>';
2448 $r .= "\n\t\t<td class=\"item-opt-value\"><textarea name=\"cscs[{$key}][value]\" id=\"cscs[{$key}][value]\" class=\"optvalue\">{$value}</textarea></td>\n\t</tr>";
2449 return $r;
2450 }
2451
2452 /**
2453 * List custom delivery meta row
2454 *
2455 * @param string $key Key.
2456 * @param array $entry Entry data.
2457 * @return string
2458 */
2459 function _list_custom_delivery_meta_row( $key, $entry ) {
2460 $r = '';
2461 $style = '';
2462 $key = esc_attr( $key );
2463
2464 $name = esc_attr( $entry['name'] );
2465 $means = get_option( 'usces_custom_delivery_select', array() );
2466 $meansoption = '';
2467 foreach ( $means as $meankey => $meanvalue ) {
2468 $meansoption .= '<option value="' . esc_attr( $meankey ) . '"' . selected( $meankey, $entry['means'], false ) . '>' . esc_html( $meanvalue ) . "</option>\n";
2469 }
2470 $essential = ( 1 === (int) $entry['essential'] ) ? ' checked="checked"' : '';
2471 $value = '';
2472 if ( is_array( $entry['value'] ) ) {
2473 foreach ( $entry['value'] as $k => $v ) {
2474 $value .= $v . "\n";
2475 }
2476 }
2477 $value = esc_attr( trim( $value ) );
2478 $positions = get_option( 'usces_custom_field_position_select', array() );
2479 $positionsoption = '';
2480 foreach ( $positions as $poskey => $posvalue ) {
2481 $positionsoption .= '<option value="' . esc_attr( $poskey ) . '"' . selected( $poskey, $entry['position'], false ) . '>' . esc_attr( $posvalue ) . "</option>\n";
2482 }
2483
2484 $r .= "\n\t<tr id=\"csde-{$key}\" class=\"{$style}\">";
2485 $r .= "\n\t\t<td class=\"left\"><div><input type=\"text\" name=\"csde[{$key}][key]\" id=\"csde[{$key}][key]\" class=\"optname\" size=\"20\" value=\"{$key}\" readonly /></div>";
2486 $r .= "\n\t\t<div><input type=\"text\" name=\"csde[{$key}][name]\" id=\"csde[{$key}][name]\" class=\"optname\" size=\"20\" value=\"{$name}\" /></div>";
2487 $r .= "\n\t\t<div class=\"optcheck\"><select name=\"csde[{$key}][means]\" id=\"csde[{$key}][means]\">" . $meansoption . "</select>\n";
2488 $r .= "<input type=\"checkbox\" name=\"csde[{$key}][essential]\" id=\"csde[{$key}][essential]\" value=\"1\"{$essential} /><label for=\"csde[{$key}][essential]\">" . esc_html__( 'Required', 'usces' ) . "</label>\n";
2489 $r .= "<select name=\"csde[{$key}][position]\" id=\"csde[{$key}][position]\">" . $positionsoption . '</select></div>';
2490 $r .= "\n\t\t<div class=\"submit\"><input type=\"button\" class=\"button\" name=\"del_csde[{$key}]\" id=\"del_csde[{$key}]\" value=\"" . esc_attr__( 'Delete' ) . "\" onclick=\"customField.delDelivery('{$key}');\" />";
2491 $r .= "\n\t\t<input type=\"button\" class=\"button\" name=\"upd_csde[{$key}]\" id=\"upd_csde[{$key}]\" value=\"" . esc_attr__( 'Update' ) . "\" onclick=\"customField.updDelivery('{$key}');\" /></div>";
2492 $r .= "\n\t\t<div id=\"csde_loading-{$key}\" class=\"meta_submit_loading\"></div>";
2493 $r .= '</td>';
2494 $r .= "\n\t\t<td class=\"item-opt-value\"><textarea name=\"csde[{$key}][value]\" id=\"csde[{$key}][value]\" class=\"optvalue\">{$value}</textarea></td>\n\t</tr>";
2495 return $r;
2496 }
2497
2498 /**
2499 * List custom member meta row
2500 *
2501 * @param string $key Key.
2502 * @param array $entry Entry data.
2503 * @return string
2504 */
2505 function _list_custom_member_meta_row( $key, $entry ) {
2506 $r = '';
2507 $style = '';
2508 $key = esc_attr( $key );
2509
2510 $name = esc_attr( $entry['name'] );
2511 $means = get_option( 'usces_custom_member_select', array() );
2512 $meansoption = '';
2513 foreach ( $means as $meankey => $meanvalue ) {
2514 $meansoption .= '<option value="' . esc_attr( $meankey ) . '"' . selected( $meankey, $entry['means'], false ) . '>' . esc_html( $meanvalue ) . "</option>\n";
2515 }
2516 $essential = ( 1 === (int) $entry['essential'] ) ? ' checked="checked"' : '';
2517 $value = '';
2518 if ( is_array( $entry['value'] ) ) {
2519 foreach ( $entry['value'] as $k => $v ) {
2520 $value .= $v . "\n";
2521 }
2522 }
2523 $value = esc_attr( trim( $value ) );
2524 $positions = get_option( 'usces_custom_field_position_select', array() );
2525 $positionsoption = '';
2526 foreach ( $positions as $poskey => $posvalue ) {
2527 $positionsoption .= '<option value="' . esc_attr( $poskey ) . '"' . selected( $poskey, $entry['position'], false ) . '>' . esc_attr( $posvalue ) . "</option>\n";
2528 }
2529 $r .= "\n\t<tr id=\"csmb-{$key}\" class=\"{$style}\">";
2530 $r .= "\n\t\t<td class=\"left\"><div><input type=\"text\" name=\"csmb[{$key}][key]\" id=\"csmb[{$key}][key]\" class=\"optname\" size=\"20\" value=\"{$key}\" readonly /></div>";
2531 $r .= "\n\t\t<div><input type=\"text\" name=\"csmb[{$key}][name]\" id=\"csmb[{$key}][name]\" class=\"optname\" size=\"20\" value=\"{$name}\" /></div>";
2532 $r .= "\n\t\t<div class=\"optcheck\"><select name=\"csmb[{$key}][means]\" id=\"csmb[{$key}][means]\">" . $meansoption . "</select>\n";
2533 $r .= "<input type=\"checkbox\" name=\"csmb[{$key}][essential]\" id=\"csmb[{$key}][essential]\" value=\"1\"{$essential} /><label for=\"csmb[{$key}][essential]\">" . esc_html__( 'Required', 'usces' ) . "</label>\n";
2534 $r .= "<select name=\"csmb[{$key}][position]\" id=\"csmb[{$key}][position]\">" . $positionsoption . '</select></div>';
2535 $r .= "\n\t\t<div class=\"submit\"><input type=\"button\" class=\"button\" name=\"del_csmb[{$key}]\" id=\"del_csmb[{$key}]\" value=\"" . esc_attr__( 'Delete' ) . "\" onclick=\"customField.delMember('{$key}');\" />";
2536 $r .= "\n\t\t<input type=\"button\" class=\"button\" name=\"upd_csmb[{$key}]\" id=\"upd_csmb[{$key}]\" value=\"" . esc_attr__( 'Update' ) . "\" onclick=\"customField.updMember('{$key}');\" /></div>";
2537 $r .= "\n\t\t<div id=\"csmb_loading-{$key}\" class=\"meta_submit_loading\"></div>";
2538 $r .= '</td>';
2539 $r .= "\n\t\t<td class=\"item-opt-value\"><textarea name=\"csmb[{$key}][value]\" id=\"csmb[{$key}][value]\" class=\"optvalue\">{$value}</textarea></td>\n\t</tr>";
2540 return $r;
2541 }
2542
2543 /**
2544 * List admin custom member meta row
2545 *
2546 * @param string $key Key.
2547 * @param array $entry Entry data.
2548 * @return string
2549 */
2550 function _list_admin_custom_member_meta_row( $key, $entry ) {
2551 $r = '';
2552 $style = '';
2553 $key = esc_attr( $key );
2554
2555 $name = esc_attr( $entry['name'] );
2556 $means = get_option( 'usces_custom_member_select', array() );
2557 $meansoption = '';
2558 foreach ( $means as $meankey => $meanvalue ) {
2559 $meansoption .= '<option value="' . esc_attr( $meankey ) . '"' . selected( $meankey, $entry['means'], false ) . '>' . esc_html( $meanvalue ) . "</option>\n";
2560 }
2561 $essential = ( 1 === (int) $entry['essential'] ) ? ' checked="checked"' : '';
2562 $value = '';
2563 if ( is_array( $entry['value'] ) ) {
2564 foreach ( $entry['value'] as $k => $v ) {
2565 $value .= $v . "\n";
2566 }
2567 }
2568 $value = esc_attr( trim( $value ) );
2569 $r .= "\n\t<tr id=\"admb-{$key}\" class=\"{$style}\">";
2570 $r .= "\n\t\t<td class=\"left\"><div><input type=\"text\" name=\"admb[{$key}][key]\" id=\"admb[{$key}][key]\" class=\"optname\" size=\"20\" value=\"{$key}\" readonly /></div>";
2571 $r .= "\n\t\t<div><input type=\"text\" name=\"admb[{$key}][name]\" id=\"admb[{$key}][name]\" class=\"optname\" size=\"20\" value=\"{$name}\" /></div>";
2572 $r .= "\n\t\t<div class=\"optcheck\"><select name=\"admb[{$key}][means]\" id=\"admb[{$key}][means]\">" . $meansoption . "</select>\n";
2573 $r .= "<input type=\"checkbox\" name=\"admb[{$key}][essential]\" id=\"admb[{$key}][essential]\" value=\"1\"{$essential} /><label for=\"admb[{$key}][essential]\">" . esc_html__( 'Required', 'usces' ) . "</label>\n";
2574 $r .= '</div>';
2575 $r .= "\n\t\t<div class=\"submit\"><input type=\"button\" class=\"button\" name=\"del_admb[{$key}]\" id=\"del_admb[{$key}]\" value=\"" . esc_attr__( 'Delete' ) . "\" onclick=\"customField.delAdmb('{$key}');\" />";
2576 $r .= "\n\t\t<input type=\"button\" class=\"button\" name=\"upd_admb[{$key}]\" id=\"upd_admb[{$key}]\" value=\"" . esc_attr__( 'Update' ) . "\" onclick=\"customField.updAdmb('{$key}');\" /></div>";
2577 $r .= "\n\t\t<div id=\"admb_loading-{$key}\" class=\"meta_submit_loading\"></div>";
2578 $r .= '</td>';
2579 $r .= "\n\t\t<td class=\"item-opt-value\"><textarea name=\"admb[{$key}][value]\" id=\"admb[{$key}][value]\" class=\"optvalue\">{$value}</textarea></td>\n\t</tr>";
2580 return $r;
2581 }
2582
2583 /**
2584 * Change State/Prefecture
2585 */
2586 function change_states_ajax() {
2587 global $usces, $usces_states;
2588
2589 $_POST = $usces->stripslashes_deep_post( $_POST );
2590
2591 $c = $_POST['country'];
2592 $res = '';
2593 $prefs = get_usces_states( $c );
2594 if ( is_array( $prefs ) && 0 < count( $prefs ) ) {
2595 foreach ( (array) $prefs as $state ) {
2596 $res .= '<option value="' . $state . '">' . $state . '</option>';
2597 }
2598 } else {
2599 die( 'error' );
2600 }
2601 die( $res );
2602 }
2603
2604 /**
2605 * State/Prefecture
2606 *
2607 * @param string $country Country.
2608 * @return array
2609 */
2610 function get_usces_states( $country ) {
2611 global $usces, $usces_states;
2612
2613 $states = array();
2614 $prefs = wel_safe_maybe_unserialize( $usces->options['province'] );
2615 if ( ! isset( $prefs[ $country ] ) || empty( $prefs[ $country ] ) ) {
2616 if ( $country == $usces->options['system']['base_country'] ) {
2617 foreach ( (array) $prefs as $state ) {
2618 if ( ! is_array( $state ) ) {
2619 array_push( $states, esc_html( $state ) );
2620 }
2621 }
2622 if ( 0 === count( $states ) ) {
2623 if ( ! empty( $usces_states[ $country ] ) ) {
2624 $prefs = $usces_states[ $country ];
2625 if ( is_array( $prefs ) ) {
2626 $states = array_map( 'esc_html', $prefs );
2627 }
2628 }
2629 }
2630 } else {
2631 if ( ! empty( $usces_states[ $country ] ) ) {
2632 $prefs = $usces_states[ $country ];
2633 if ( is_array( $prefs ) ) {
2634 $states = array_map( 'esc_html', $prefs );
2635 }
2636 }
2637 }
2638 } else {
2639 $states = is_array( $prefs[ $country ] ) ? array_map( 'esc_html', $prefs[ $country ] ) : esc_html( $prefs[ $country ] );
2640 }
2641 return $states;
2642 }
2643
2644 /**
2645 * Target market countries
2646 */
2647 function target_market_ajax() {
2648 global $usces;
2649
2650 $_POST = $usces->stripslashes_deep_post( $_POST );
2651 $response = array();
2652 $target = explode( ',', $_POST['target'] );
2653 foreach ( (array) $target as $country ) {
2654 $prefs = get_usces_states( $country );
2655 if ( is_array( $prefs ) && ! empty( $prefs ) ) {
2656 $pos = strpos( $prefs[0], '--' );
2657 if ( false !== $pos ) {
2658 array_shift( $prefs );
2659 }
2660 $response[] = $country . ',' . implode( "\n", $prefs );
2661 } else {
2662 $response[] = $country;
2663 }
2664 }
2665 wp_send_json( $response );
2666 }
2667
2668 /**
2669 * Admin ajax
2670 */
2671 function usces_admin_ajax() {
2672 switch ( $_POST['mode'] ) {
2673 case 'options_backup':
2674 check_admin_referer( 'options_backup', 'wc_nonce' );
2675 $options = get_option( 'usces' );
2676 $res = true;
2677 if ( is_array( $options ) ) {
2678 $usces_backup_date = current_time( 'mysql' );
2679 update_option( 'usces_backup', $options );
2680 update_option( 'usces_backup_date', $usces_backup_date );
2681 $res = $usces_backup_date;
2682 } else {
2683 $res = false;
2684 }
2685 die( $res );
2686 break;
2687 case 'options_restore':
2688 check_admin_referer( 'options_restore', 'wc_nonce' );
2689 $options = get_option( 'usces_backup' );
2690 $res = true;
2691 if ( is_array( $options ) ) {
2692 update_option( 'usces', $options );
2693 } else {
2694 $res = false;
2695 }
2696 die( $res );
2697 break;
2698 }
2699 do_action( 'usces_action_admin_ajax' );
2700 }
2701
2702 /**
2703 * Order amount recalculation
2704 *
2705 * @param int $order_id Order ID.
2706 * @param int $mem_id Member ID.
2707 * @param int $post_id Post ID.
2708 * @param float $price Price.
2709 * @param int $quant Quantity.
2710 * @param array $cart_id Cart ID.
2711 * @param int $use_point Use point.
2712 * @param float $shipping_charge Shipping charge.
2713 * @param float $cod_fee Fee.
2714 * @param float $discount Discount.
2715 * @param string $change_taxrate Change applicable tax rate.
2716 */
2717 function usces_order_recalculation( $order_id, $mem_id, $post_id, $price, $quant, $cart_id, $use_point, $shipping_charge, $cod_fee, $discount = 0, $change_taxrate = '' ) {
2718 global $usces;
2719
2720 $data = array();
2721 $res = 'ok';
2722
2723 $cart = array();
2724 $post_id_count = ( is_array( $post_id ) ) ? count( $post_id ) : 0;
2725 for ( $i = 0; $i < $post_id_count; $i++ ) {
2726 if ( $post_id[ $i ] ) {
2727 $cart[] = array(
2728 'post_id' => $post_id[ $i ],
2729 'price' => (float) $price[ $i ],
2730 'quantity' => (float) $quant[ $i ],
2731 );
2732 }
2733 }
2734
2735 if ( 'change' === $change_taxrate ) {
2736 if ( usces_is_reduced_taxrate() ) {
2737 $usces_tax = Welcart_Tax::get_instance();
2738 $usces_tax->set_order_condition_reduced_taxrate( $order_id );
2739 for ( $i = 0; $i < $post_id_count; $i++ ) {
2740 if ( $cart_id[ $i ] ) {
2741 $taxrate = usces_get_ordercart_meta( 'taxrate', $cart_id[ $i ] );
2742 if ( ! $taxrate ) {
2743 $ordercart = usces_get_ordercartdata_row( $cart_id[ $i ] );
2744 $sku['taxrate'] = $usces_tax->get_sku_applicable_taxrate( $post_id[ $i ], $ordercart['sku_code'] );
2745 $usces_tax->set_ordercart_applicable_taxrate( $cart_id[ $i ], $sku );
2746 }
2747 }
2748 }
2749 $data['status'] = $res;
2750 $data['tax_mode'] = 'reduced';
2751 wp_send_json( $data );
2752 }
2753 $condition = $usces->get_condition();
2754 } else {
2755 if ( ! empty( $order_id ) ) {
2756 $condition = usces_get_order_condition( $order_id );
2757 } else {
2758 $condition = $usces->get_condition();
2759 }
2760 }
2761
2762 $tax_display = ( isset( $condition['tax_display'] ) ) ? $condition['tax_display'] : usces_get_tax_display();
2763 $member_system = ( isset( $condition['membersystem_state'] ) ) ? $condition['membersystem_state'] : $usces->options['membersystem_state'];
2764 $member_system_point = ( isset( $condition['membersystem_point'] ) ) ? $condition['membersystem_point'] : $usces->options['membersystem_point'];
2765 $tax_mode = ( isset( $condition['tax_mode'] ) ) ? $condition['tax_mode'] : usces_get_tax_mode();
2766 $tax_target = ( isset( $condition['tax_target'] ) ) ? $condition['tax_target'] : usces_get_tax_target();
2767 $point_coverage = ( isset( $condition['point_coverage'] ) ) ? $condition['point_coverage'] : usces_point_coverage();
2768 $tax_method = ( isset( $condition['tax_method'] ) ) ? $condition['tax_method'] : $usces->options['tax_method'];
2769
2770 $total_items_price = 0;
2771 foreach ( $cart as $cart_row ) {
2772 $total_items_price += $cart_row['price'] * $cart_row['quantity'];
2773 }
2774 $meminfo = $usces->get_member_info( $mem_id );
2775
2776 if ( empty( $discount ) || 'NaN' == $discount ) {
2777 $discount = 0;
2778 }
2779 if ( 'change' == $change_taxrate ) {
2780 $discount = 0;
2781 if ( isset( $condition['display_mode'] ) && 'Promotionsale' === $condition['display_mode'] ) {
2782 if ( isset( $condition['campaign_privilege'] ) && 'discount' === $condition['campaign_privilege'] ) {
2783 if ( 0 === (int) $condition['campaign_category'] ) {
2784 $discount = (float) sprintf( '%.3f', $total_items_price * (float) $condition['privilege_discount'] / 100 );
2785 } else {
2786 foreach ( $cart as $cart_row ) {
2787 if ( in_category( (int) $condition['campaign_category'], $cart_row['post_id'] ) ) {
2788 $discount += (float) sprintf( '%.3f', $cart_row['price'] * $cart_row['quantity'] * (float) $condition['privilege_discount'] / 100 );
2789 }
2790 }
2791 }
2792 }
2793 }
2794 if ( 0 != $discount ) {
2795 $decimal = $usces->get_currency_decimal();
2796 if ( 0 == $decimal ) {
2797 $discount = ceil( $discount );
2798 } else {
2799 $decipad = (int) str_pad( '1', $decimal + 1, '0', STR_PAD_RIGHT );
2800 $discount = ceil( $discount * $decipad ) / $decipad;
2801 }
2802 $discount = $discount * -1;
2803 }
2804 }
2805 $discount = apply_filters( 'usces_filter_order_discount_recalculation', $discount, $cart, $condition, $order_id );
2806
2807 $point = 0;
2808 if ( empty( $use_point ) || 'NaN' == $use_point ) {
2809 $use_point = 0;
2810 }
2811 if ( 'activate' === $member_system && 'activate' === $member_system_point && ! empty( $meminfo['ID'] ) ) {
2812 if ( isset( $condition['display_mode'] ) && 'Promotionsale' === $condition['display_mode'] ) {
2813 if ( isset( $condition['campaign_privilege'] ) && 'discount' === $condition['campaign_privilege'] ) {
2814 foreach ( $cart as $cart_row ) {
2815 $cats = $usces->get_post_term_ids( $cart_row['post_id'], 'category' );
2816 if ( ! in_array( $condition['campaign_category'], $cats ) ) {
2817 $product = wel_get_product( $cart_row['post_id'] );
2818 $rate = (float) $product['itemPointrate'];
2819 $price = $cart_row['price'] * $cart_row['quantity'];
2820 $point = (float) sprintf( '%.3f', $point + ( $price * $rate / 100 ) );
2821 }
2822 }
2823 } elseif ( isset( $condition['campaign_privilege'] ) && 'point' === $condition['campaign_privilege'] ) {
2824 foreach ( $cart as $cart_row ) {
2825 $product = wel_get_product( $cart_row['post_id'] );
2826 $rate = (float) $product['itemPointrate'];
2827 $price = $cart_row['price'] * $cart_row['quantity'];
2828 $cats = $usces->get_post_term_ids( $cart_row['post_id'], 'category' );
2829 if ( in_array( $condition['campaign_category'], $cats ) ) {
2830 $point = sprintf( '%.3f', $point + ( $price * $rate / 100 * (float) $condition['privilege_point'] ) );
2831 } else {
2832 $point = sprintf( '%.3f', $point + ( $price * $rate / 100 ) );
2833 }
2834 }
2835 }
2836 } else {
2837 foreach ( $cart as $cart_row ) {
2838 $product = wel_get_product( $cart_row['post_id'] );
2839 $rate = (float) $product['itemPointrate'];
2840 $price = $cart_row['price'] * $cart_row['quantity'];
2841 $point = sprintf( '%.3f', $point + ( $price * $rate / 100 ) );
2842 }
2843 }
2844
2845 if ( 0 < $use_point ) {
2846 $point = (float) sprintf( '%.3f', $point - ( $point * (int) $use_point / $total_items_price ) );
2847 $point = ceil( $point );
2848 if ( 0 > $point ) {
2849 $point = 0;
2850 }
2851 } else {
2852 if ( 0 < $point ) {
2853 $point = ceil( $point );
2854 }
2855 }
2856 }
2857 $point = apply_filters( 'usces_filter_set_point_recalculation', $point, $condition, $cart, $meminfo, $use_point, $order_id );
2858
2859 $total_price = $total_items_price - $use_point + $discount + $shipping_charge + $cod_fee;
2860 if ( $total_price < 0 ) {
2861 $total_price = 0;
2862 }
2863 $total_price = apply_filters( 'usces_filter_set_cart_fees_total_price', $total_price, $total_items_price, $use_point, $discount, $shipping_charge, $cod_fee ); // Deprecated.
2864 $total_price = apply_filters( 'usces_filter_order_total_price_recalculation', $total_price, $total_items_price, $use_point, $discount, $shipping_charge, $cod_fee, $cart, $order_id );
2865 $materials = compact( 'total_items_price', 'shipping_charge', 'discount', 'cod_fee', 'use_point', 'condition' );
2866 if ( 'activate' === $tax_display ) {
2867 if ( 'include' === $tax_mode ) {
2868 $tax = 0;
2869 $include_tax = usces_internal_tax( $materials, 'return' );
2870 } else {
2871 // $tax = $usces->getTax( $total_price, $materials );
2872 if ( 1 === (int) $point_coverage ) {
2873 if ( 'products' === $tax_target ) {
2874 $total = (float) $total_items_price + (float) $discount;
2875 } else {
2876 $total = (float) $total_items_price + (float) $discount + (float) $shipping_charge + (float) $cod_fee;
2877 }
2878 } else {
2879 if ( 'products' === $tax_target ) {
2880 $total = (float) $total_items_price + (float) $discount;
2881 } else {
2882 if ( empty( $use_point ) ) {
2883 $use_point = 0;
2884 }
2885 $total = (float) $total_items_price + (float) $discount - (int) $use_point + (float) $shipping_charge + (float) $cod_fee;
2886 }
2887 }
2888 $tax = (float) sprintf( '%.3f', (float) $total * (float) $condition['tax_rate'] / 100 );
2889 $tax = usces_tax_rounding_off( $tax, $tax_method );
2890 $include_tax = 0;
2891 }
2892 } else {
2893 $tax = 0;
2894 $include_tax = 0;
2895 }
2896 $total_full_price = $total_price + $tax;
2897 $total_full_price = apply_filters( 'usces_filter_set_cart_fees_total_full_price', $total_full_price, $total_items_price, $use_point, $discount, $shipping_charge, $cod_fee ); // Deprecated.
2898 $total_full_price = apply_filters( 'usces_filter_order_total_full_price_recalculation', $total_full_price, $total_items_price, $use_point, $discount, $shipping_charge, $cod_fee, $cart, $order_id );
2899
2900 $data['status'] = $res;
2901 $data['tax_mode'] = 'standard';
2902 $data['discount'] = $discount;
2903 $data['tax'] = usces_crform( $tax, false, false, 'return', false );
2904 $data['include_tax'] = ( 0 < $include_tax ) ? '(' . usces_crform( $include_tax, false, false, 'return', true ) . ')' : '';
2905 $data['point'] = $point;
2906 $data['total_full_price'] = usces_crform( $total_full_price, false, false, 'return', false );
2907 wp_send_json( $data );
2908 }
2909
2910 /**
2911 * Order amount recalculation ( for Reduced tax rate )
2912 *
2913 * @param int $order_id Order ID.
2914 * @param int $mem_id Member ID.
2915 * @param int $post_id Post ID.
2916 * @param float $price Price.
2917 * @param int $quant Quantity.
2918 * @param array $cart_id Cart ID.
2919 * @param int $use_point Use point.
2920 * @param float $shipping_charge Shipping charge.
2921 * @param float $cod_fee Fee.
2922 * @param float $discount_standard Standard tax rate discount.
2923 * @param float $discount_reduced Reduced tax rate discount.
2924 * @param string $change_taxrate Change applicable tax rate.
2925 * @return void
2926 */
2927 function usces_order_recalculation_reduced( $order_id, $mem_id, $post_id, $price, $quant, $cart_id, $use_point, $shipping_charge, $cod_fee, $discount_standard = 0, $discount_reduced = 0, $change_taxrate = '' ) {
2928 global $usces;
2929
2930 $usces_tax = Welcart_Tax::get_instance();
2931
2932 $data = array();
2933 $res = 'ok';
2934
2935 if ( 'change' === $change_taxrate ) {
2936 $condition = $usces->get_condition();
2937 $usces_tax->set_order_condition_reduced_taxrate( $order_id );
2938 } else {
2939 if ( ! empty( $order_id ) ) {
2940 $condition = usces_get_order_condition( $order_id );
2941 } else {
2942 $condition = $usces->get_condition();
2943 }
2944 }
2945
2946 $tax_rate = ( isset( $condition['tax_rate'] ) ) ? (float) $condition['tax_rate'] : (float) $usces->options['tax_rate'];
2947 $tax_rate_reduced = ( isset( $condition['tax_rate_reduced'] ) ) ? (float) $condition['tax_rate_reduced'] : (float) $usces->options['tax_rate_reduced'];
2948 $tax_display = ( isset( $condition['tax_display'] ) ) ? $condition['tax_display'] : usces_get_tax_display();
2949 $member_system = ( isset( $condition['membersystem_state'] ) ) ? $condition['membersystem_state'] : $usces->options['membersystem_state'];
2950 $member_system_point = ( isset( $condition['membersystem_point'] ) ) ? $condition['membersystem_point'] : $usces->options['membersystem_point'];
2951 $tax_mode = ( isset( $condition['tax_mode'] ) ) ? $condition['tax_mode'] : usces_get_tax_mode();
2952 $tax_target = ( isset( $condition['tax_target'] ) ) ? $condition['tax_target'] : usces_get_tax_target();
2953 $point_coverage = ( isset( $condition['point_coverage'] ) ) ? $condition['point_coverage'] : usces_point_coverage();
2954 $tax_method = ( isset( $condition['tax_method'] ) ) ? $condition['tax_method'] : $usces->options['tax_method'];
2955
2956 $cart = array();
2957 $post_id_count = ( is_array( $post_id ) ) ? count( $post_id ) : 0;
2958 for ( $i = 0; $i < $post_id_count; $i++ ) {
2959 if ( $post_id[ $i ] ) {
2960 $ordercart = usces_get_ordercartdata_row( $cart_id[ $i ] );
2961 $applicable_taxrate = ( 'change' === $change_taxrate ) ? $usces_tax->get_sku_applicable_taxrate( $post_id[ $i ], $ordercart['sku_code'] ) : $usces_tax->get_ordercart_applicable_taxrate( $cart_id[ $i ], $post_id[ $i ], $ordercart['sku_code'] );
2962 $cart[] = array(
2963 'post_id' => $post_id[ $i ],
2964 'sku_code' => $ordercart['sku_code'],
2965 'price' => (float) $price[ $i ],
2966 'quantity' => (float) $quant[ $i ],
2967 'taxrate' => $applicable_taxrate,
2968 );
2969 }
2970 }
2971
2972 $subtotal_standard = 0;
2973 $subtotal_reduced = 0;
2974 foreach ( $cart as $cart_row ) {
2975 if ( 'reduced' === $cart_row['taxrate'] ) {
2976 $subtotal_reduced += (float) $cart_row['price'] * (float) $cart_row['quantity'];
2977 } else {
2978 $subtotal_standard += (float) $cart_row['price'] * (float) $cart_row['quantity'];
2979 }
2980 }
2981 $total_items_price = $subtotal_standard + $subtotal_reduced;
2982 $meminfo = $usces->get_member_info( $mem_id );
2983
2984 if ( 'change' === $change_taxrate ) {
2985 $discount = 0;
2986 $discount_standard = 0;
2987 $discount_reduced = 0;
2988 if ( isset( $condition['display_mode'] ) && 'Promotionsale' === $condition['display_mode'] ) {
2989 if ( isset( $condition['campaign_privilege'] ) && 'discount' === $condition['campaign_privilege'] ) {
2990 if ( 0 === (int) $condition['campaign_category'] ) {
2991 $discount_standard = (float) sprintf( '%.3f', (float) $subtotal_standard * (float) $condition['privilege_discount'] / 100 );
2992 $discount_reduced = (float) sprintf( '%.3f', (float) $subtotal_reduced * (float) $condition['privilege_discount'] / 100 );
2993 } else {
2994 foreach ( $cart as $cart_row ) {
2995 if ( in_category( (int) $condition['campaign_category'], $cart_row['post_id'] ) ) {
2996 $items_discount = (float) sprintf( '%.3f', (float) $cart_row['price'] * (float) $cart_row['quantity'] * (float) $condition['privilege_discount'] / 100 );
2997 if ( 'reduced' === $cart_row['taxrate'] ) {
2998 $discount_reduced += $items_discount;
2999 } else {
3000 $discount_standard += $items_discount;
3001 }
3002 }
3003 }
3004 }
3005 if ( 0 != $discount_standard || 0 != $discount_reduced ) {
3006 $decimal = $usces->get_currency_decimal();
3007 if ( 0 == $decimal ) {
3008 $discount_standard = ceil( $discount_standard );
3009 $discount_reduced = ceil( $discount_reduced );
3010 } else {
3011 $decipad = (int) str_pad( '1', $decimal + 1, '0', STR_PAD_RIGHT );
3012 $discount_standard = ceil( $discount_standard * $decipad ) / $decipad;
3013 $discount_reduced = ceil( $discount_reduced * $decipad ) / $decipad;
3014 }
3015 $discount_standard *= -1;
3016 $discount_reduced *= -1;
3017 $discount = $discount_standard + $discount_reduced;
3018 }
3019 }
3020 }
3021 }
3022 $discount = $discount_standard + $discount_reduced;
3023 $discount = apply_filters( 'usces_filter_order_discount_recalculation', $discount, $cart, $condition, $order_id );
3024
3025 $point = 0;
3026 if ( empty( $use_point ) || 'NaN' == $use_point ) {
3027 $use_point = 0;
3028 }
3029 if ( 'activate' === $member_system && 'activate' === $member_system_point && ! empty( $meminfo['ID'] ) ) {
3030 if ( isset( $condition['display_mode'] ) && 'Promotionsale' === $condition['display_mode'] ) {
3031 if ( isset( $condition['campaign_privilege'] ) && 'discount' === $condition['campaign_privilege'] ) {
3032 foreach ( $cart as $cart_row ) {
3033 $cats = $usces->get_post_term_ids( $cart_row['post_id'], 'category' );
3034 if ( ! in_array( $condition['campaign_category'], $cats ) ) {
3035 $product = wel_get_product( $cart_row['post_id'] );
3036 $rate = (float) $product['itemPointrate'];
3037 $price = $cart_row['price'] * $cart_row['quantity'];
3038 $point = (float) sprintf( '%.3f', $point + ( $price * $rate / 100 ) );
3039 }
3040 }
3041 } elseif ( isset( $condition['campaign_privilege'] ) && 'point' === $condition['campaign_privilege'] ) {
3042 foreach ( $cart as $cart_row ) {
3043 $product = wel_get_product( $cart_row['post_id'] );
3044 $rate = (float) $product['itemPointrate'];
3045 $price = $cart_row['price'] * $cart_row['quantity'];
3046 $cats = $usces->get_post_term_ids( $cart_row['post_id'], 'category' );
3047 if ( in_array( $condition['campaign_category'], $cats ) ) {
3048 $point = sprintf( '%.3f', $point + ( $price * $rate / 100 * (float) $condition['privilege_point'] ) );
3049 } else {
3050 $point = sprintf( '%.3f', $point + ( $price * $rate / 100 ) );
3051 }
3052 }
3053 }
3054 } else {
3055 foreach ( $cart as $cart_row ) {
3056 $product = wel_get_product( $cart_row['post_id'] );
3057 $rate = (float) $product['itemPointrate'];
3058 $price = $cart_row['price'] * $cart_row['quantity'];
3059 $point = sprintf( '%.3f', $point + ( $price * $rate / 100 ) );
3060 }
3061 }
3062
3063 if ( 0 < $use_point ) {
3064 $point = (float) sprintf( '%.3f', $point - ( $point * (int) $use_point / $total_items_price ) );
3065 $point = ceil( $point );
3066 if ( 0 > $point ) {
3067 $point = 0;
3068 }
3069 } else {
3070 if ( 0 < $point ) {
3071 $point = ceil( $point );
3072 }
3073 }
3074 }
3075 $point = apply_filters( 'usces_filter_set_point_recalculation', $point, $condition, $cart, $meminfo, $use_point, $order_id );
3076
3077 $total_price = $total_items_price - $use_point + $discount + $shipping_charge + $cod_fee;
3078 if ( $total_price < 0 ) {
3079 $total_price = 0;
3080 }
3081 $total_price = apply_filters( 'usces_filter_set_cart_fees_total_price', $total_price, $total_items_price, $use_point, $discount, $shipping_charge, $cod_fee ); // Deprecated.
3082 $total_price = apply_filters( 'usces_filter_order_total_price_recalculation', $total_price, $total_items_price, $use_point, $discount, $shipping_charge, $cod_fee, $cart, $order_id );
3083
3084 $tax = 0;
3085 $tax_standard = 0;
3086 $tax_reduced = 0;
3087 $include_tax = 0;
3088 if ( 'activate' === $tax_display ) {
3089 if ( 'all' === $tax_target ) {
3090 if ( 0 < $shipping_charge ) {
3091 $subtotal_standard += (float) $shipping_charge;
3092 }
3093 if ( 0 < $cod_fee ) {
3094 $subtotal_standard += (float) $cod_fee;
3095 }
3096 }
3097
3098 if ( 'include' === $tax_mode ) {
3099 if ( 0 < $subtotal_standard ) {
3100 $tax_standard = (float) sprintf( '%.3f', ( (float) $subtotal_standard + (float) $discount_standard ) * $tax_rate / ( 100 + $tax_rate ) );
3101 }
3102 if ( 0 < $subtotal_reduced ) {
3103 $tax_reduced = (float) sprintf( '%.3f', ( (float) $subtotal_reduced + (float) $discount_reduced ) * $tax_rate_reduced / ( 100 + $tax_rate_reduced ) );
3104 }
3105 } else {
3106 if ( 0 < $subtotal_standard ) {
3107 $tax_standard = (float) sprintf( '%.3f', ( (float) $subtotal_standard + (float) $discount_standard ) * $tax_rate / 100 );
3108 }
3109 if ( 0 < $subtotal_reduced ) {
3110 $tax_reduced = (float) sprintf( '%.3f', ( (float) $subtotal_reduced + (float) $discount_reduced ) * $tax_rate_reduced / 100 );
3111 }
3112 }
3113
3114 $tax_standard = usces_tax_rounding_off( $tax_standard, $tax_method );
3115 $tax_reduced = usces_tax_rounding_off( $tax_reduced, $tax_method );
3116
3117 $materials = compact( 'total_items_price', 'shipping_charge', 'discount', 'cod_fee', 'use_point', 'cart' );
3118 if ( 'include' === $tax_mode ) {
3119 $include_tax = $tax_standard + $tax_reduced;
3120 $total_full_price = $total_price;
3121 } else {
3122 $tax = apply_filters( 'usces_filter_order_tax_recalculation', $tax_standard + $tax_reduced, $materials );
3123 $total_full_price = $total_price + $tax;
3124 }
3125 } else {
3126 $total_full_price = $total_price;
3127 }
3128 $total_full_price = apply_filters( 'usces_filter_set_cart_fees_total_full_price', $total_full_price, $total_items_price, $use_point, $discount, $shipping_charge, $cod_fee ); // Deprecated.
3129 $total_full_price = apply_filters( 'usces_filter_order_total_full_price_recalculation', $total_full_price, $total_items_price, $use_point, $discount, $shipping_charge, $cod_fee, $cart, $order_id );
3130
3131 $data['status'] = $res;
3132 $data['tax_mode'] = 'reduced';
3133 $data['discount'] = $discount;
3134 $data['tax'] = $tax;
3135 $data['point'] = $point;
3136 $data['total_full_price'] = $total_full_price;
3137 $data['subtotal_standard'] = $subtotal_standard;
3138 $data['subtotal_reduced'] = $subtotal_reduced;
3139 //if ( 'change' == $change_taxrate ) {
3140 $data['discount_standard'] = $discount_standard;
3141 $data['discount_reduced'] = $discount_reduced;
3142 //}
3143 $data['tax_standard'] = $tax_standard;
3144 $data['tax_reduced'] = $tax_reduced;
3145 $data['include_tax'] = $include_tax;
3146 wp_send_json( $data );
3147 }
3148
3149 /**
3150 * Application of reduced tax rate
3151 *
3152 * @param array $sku SKU data.
3153 * @return void
3154 */
3155 function usces_sku_meta_row_reduced_taxrate( $sku ) {
3156 global $usces;
3157
3158 if ( $usces->is_reduced_taxrate() ) :
3159 $standard = $usces->options['tax_rate'];
3160 $reduced = $usces->options['tax_rate_reduced'];
3161 $taxrate = ( isset( $sku['taxrate'] ) ) ? $sku['taxrate'] : '';
3162 ?>
3163 <select id="itemsku[<?php echo esc_attr( $sku['meta_id'] ); ?>][applicable_taxrate]" name="itemsku[<?php echo esc_attr( $sku['meta_id'] ); ?>][applicable_taxrate]" class="sku_applicable_taxrate" >
3164 <option value="standard"<?php selected( $taxrate, 'reduced' ); ?>><?php esc_html_e( 'Standard tax rate', 'usces' ); ?>(<?php echo esc_html( $standard ); ?>%)</option>
3165 <option value="reduced"<?php selected( $taxrate, 'reduced' ); ?>><?php esc_html_e( 'Reduced tax rate', 'usces' ); ?>(<?php echo esc_html( $reduced ); ?>%)</option>
3166 </select>
3167 <?php
3168 endif;
3169 }
3170
3171 /**
3172 * Application of reduced tax rate
3173 */
3174 function usces_newsku_meta_row_reduced_taxrate() {
3175 global $usces;
3176
3177 if ( $usces->is_reduced_taxrate() ) :
3178 $standard = $usces->options['tax_rate'];
3179 $reduced = $usces->options['tax_rate_reduced'];
3180 ?>
3181 <select id="newsku_applicable_taxrate" name="newsku_applicable_taxrate" class="newsku_applicable_taxrate" >
3182 <option value="standard"><?php esc_html_e( 'Standard tax rate', 'usces' ); ?>(<?php echo esc_html( $standard ); ?>%)</option>
3183 <option value="reduced"><?php esc_html_e( 'Reduced tax rate', 'usces' ); ?>(<?php echo esc_html( $reduced ); ?>%)</option>
3184 </select>
3185 <?php
3186 endif;
3187 }
3188
3189 /**
3190 * When deleting product data from the trash bin, also delete "_item", "_sku", and "_opt".
3191 *
3192 * @param int $post_id Post ID.
3193 */
3194 function usces_delete_all_item_data( $post_id ) {
3195 $action = filter_input( INPUT_GET, 'action', FILTER_SANITIZE_SPECIAL_CHARS );
3196
3197 if ( ! empty( $action ) && 'delete' === $action && usces_is_item( $post_id ) ) {
3198 wel_delete_all_sku_data( $post_id );
3199 wel_delete_all_opt_data( $post_id );
3200 wel_delete_item_data( $post_id );
3201 }
3202 }
3203