PluginProbe
Welcart e-Commerce / 2.11.32
Welcart e-Commerce v2.11.32
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 / includes / product / wel-item-class.php

wel-item-class.php in Welcart e-Commerce 2.11.32, at includes/product/wel-item-class.php

1,315 lines 28.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Welcart item base class
4 *
5 * @package Welcart
6 */
7
8 namespace Welcart;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13
14 /**
15 * Item class
16 *
17 * The Welcart item class handles individual product data.
18 *
19 * @since 2.2.2
20 */
21 class ItemData {
22
23 /**
24 * ID for this object.
25 *
26 * @since 2.2.2
27 * @var int
28 */
29 protected $id = 0;
30
31 /**
32 * Item data format.
33 *
34 * @since 2.2.2
35 * @var array
36 */
37 protected $item_format = array(
38 'post_id' => 0,
39 'itemCode' => null,
40 'itemName' => null,
41 'itemRestriction' => null,
42 'itemPointrate' => 0,
43 'itemGpNum1' => null,
44 'itemGpNum2' => null,
45 'itemGpNum3' => null,
46 'itemGpDis1' => null,
47 'itemGpDis2' => null,
48
49 'itemGpDis3' => null,
50 'itemOrderAcceptable' => 0,
51 'itemShipping' => 0,
52 'itemDeliveryMethod' => array(),
53 'itemShippingCharge' => 0,
54 'itemIndividualSCharge' => null,
55 'item_charging_type' => 0,
56 'item_division' => 'shipped',
57 'dlseller_date' => null,
58 'dlseller_file' => null,
59
60 'dlseller_interval' => null,
61 'dlseller_validity' => null,
62 'dlseller_version' => null,
63 'dlseller_author' => null,
64 'dlseller_purchases' => 0,
65 'dlseller_downloads' => 0,
66 'item_chargingday' => null,
67 'item_frequency' => null,
68 'wcad_regular_unit' => null,
69 'wcad_regular_interval' => null,
70
71 'wcad_regular_frequency' => null,
72 'select_sku_switch' => 0,
73 'select_sku_display' => 0,
74 'select_sku' => array(),
75 'atobarai_propriety' => 0,
76 'atodene_propriety' => 0,
77 'structuredDataSku' => null,
78 'lower_limit' => null,
79 'popularity' => null,
80 'main_price' => null,
81
82 'itemPicts' => null,
83 'itemAdvanced' => array(),
84 );
85
86 /**
87 * Item data placeholders.
88 *
89 * @since 2.7
90 * @var array
91 */
92 protected $placeholders = array(
93 '%d',
94 '%s',
95 '%s',
96 '%s',
97 '%d',
98 '%s',
99 '%s',
100 '%s',
101 '%s',
102 '%s',
103
104 '%s',
105 '%d',
106 '%d',
107 '%s',
108 '%d',
109 '%s',
110 '%d',
111 '%s',
112 '%s',
113 '%s',
114
115 '%s',
116 '%s',
117 '%s',
118 '%s',
119 '%d',
120 '%d',
121 '%s',
122 '%s',
123 '%s',
124 '%s',
125
126 '%s',
127 '%d',
128 '%d',
129 '%s',
130 '%d',
131 '%d',
132 '%s',
133 '%s',
134 '%s',
135 '%s',
136
137 '%s',
138 '%s',
139 );
140
141 /**
142 * Old Item data key.
143 *
144 * @since 2.2.2
145 * @var array
146 */
147 protected $item_old_key = array(
148 'itemCode' => '_itemCode',
149 'itemName' => '_itemName',
150 'itemRestriction' => '_itemRestriction',
151 'itemPointrate' => '_itemPointrate',
152 'itemGpNum1' => '_itemGpNum1',
153 'itemGpNum2' => '_itemGpNum2',
154 'itemGpNum3' => '_itemGpNum3',
155 'itemGpDis1' => '_itemGpDis1',
156 'itemGpDis2' => '_itemGpDis2',
157 'itemGpDis3' => '_itemGpDis3',
158 'itemOrderAcceptable' => '_itemOrderAcceptable',
159 'itemShipping' => '_itemShipping',
160 'itemDeliveryMethod' => '_itemDeliveryMethod',
161 'itemShippingCharge' => '_itemShippingCharge',
162 'itemIndividualSCharge' => '_itemIndividualSCharge',
163 'item_charging_type' => '_item_charging_type',
164 'item_division' => '_item_division',
165 'dlseller_date' => '_dlseller_date',
166 'dlseller_file' => '_dlseller_file',
167 'dlseller_interval' => '_dlseller_interval',
168 'dlseller_validity' => '_dlseller_validity',
169 'dlseller_version' => '_dlseller_version',
170 'dlseller_author' => '_dlseller_author',
171 'dlseller_purchases' => '_dlseller_purchases',
172 'dlseller_downloads' => '_dlseller_downloads',
173 'item_chargingday' => '_item_chargingday',
174 'item_frequency' => '_item_frequency',
175 'wcad_regular_unit' => '_wcad_regular_unit',
176 'wcad_regular_interval' => '_wcad_regular_interval',
177 'wcad_regular_frequency' => '_wcad_regular_frequency',
178 'select_sku_switch' => '_select_sku_switch',
179 'select_sku_display' => '_select_sku_display',
180 'select_sku' => '_select_sku',
181 'atobarai_propriety' => 'atobarai_propriety',
182 'atodene_propriety' => 'atodene_propriety',
183 'structuredDataSku' => '_structuredDataSku',
184 'lower_limit' => 'lower_limit',
185 'popularity' => 'popularity',
186 'main_price' => 'main_price',
187 'itemPicts' => '_itemPicts',
188 'itemAdvanced' => '_itemAdvanced',
189 );
190
191 /**
192 * SKU data format.
193 *
194 * @since 3.0
195 * @var array
196 */
197 protected $sku_format = array(
198 'meta_id' => null,
199 'post_id' => null,
200 'code' => null,
201 'name' => null,
202 'cprice' => null,
203 'price' => null,
204 'stocknum' => null,
205 'stock' => null,
206 'unit' => null,
207 'gp' => null,
208 'taxrate' => null,
209 'size' => null,
210 'weight' => null,
211 'pict_id' => null,
212 'advance' => array(),
213 'paternkey' => null,
214 'sort' => 0,
215 );
216
217 /**
218 * SKU placeholders.
219 *
220 * @since 3.0
221 * @var array
222 */
223 protected $sku_placeholders = array(
224 '%d',
225 '%d',
226 '%s',
227 '%s',
228 '%s',
229 '%s',
230 '%s',
231 '%s',
232 '%s',
233 '%s',
234 '%s',
235 '%s',
236 '%s',
237 '%s',
238 '%s',
239 '%s',
240 '%d',
241 );
242
243 /**
244 * Option data format.
245 *
246 * @since 3.0
247 * @var array
248 */
249 protected $opt_format = array(
250 'meta_id' => null,
251 'post_id' => null,
252 'code' => null,
253 'name' => null,
254 'means' => 0,
255 'essential' => 0,
256 'value' => null,
257 'sort' => 0,
258 );
259
260 /**
261 * Option placeholders.
262 *
263 * @since 3.0
264 * @var array
265 */
266 protected $opt_placeholders = array(
267 '%d',
268 '%d',
269 '%s',
270 '%s',
271 '%s',
272 '%s',
273 '%s',
274 '%d',
275 );
276
277 /**
278 * Exclusion meta key.
279 *
280 * @since 3.0
281 * @var array
282 */
283 protected $exclusion_key = array(
284 '_edit_lock' => null,
285 '_edit_last' => null,
286 );
287
288 /**
289 * Perfect object.
290 *
291 * @since 2.2.2
292 * @var array
293 */
294 protected $product_data = array();
295
296 /**
297 * Post datas for this object. Name value pairs (name + default value).
298 *
299 * @since 2.2.2
300 * @var array
301 */
302 protected $post_data = array();
303
304 /**
305 * Item datas for this object. Name value pairs (name + default value).
306 *
307 * @since 2.2.2
308 * @var array
309 */
310 protected $item_data = array();
311
312 /**
313 * All SKU datas for this object. Name value pairs (name + default value).
314 *
315 * @since 2.2.2
316 * @var array
317 */
318 protected $sku_data = array();
319
320 /**
321 * All option datas for this object. Name value pairs (name + default value).
322 *
323 * @since 2.2.2
324 * @var array
325 */
326 protected $opt_data = array();
327
328 /**
329 * Extra datas for this object. Mainly custom fields(post_meta).
330 *
331 * @since 2.2.2
332 * @var array
333 */
334 protected $ext_data = array();
335
336 /**
337 * Get the item if ID is passed, otherwise the item is empty.
338 * This class should not be instantiated.
339 * The wc_get_product() function should be used instead.
340 *
341 * @param mixed $the_item Post object or post ID of the item.
342 * @param boolean $cache Switch of cache.
343 */
344 public function __construct( $the_item = 0, $cache = true ) {
345
346 $this->item_data = $this->item_format;
347
348 if ( is_numeric( $the_item ) && $the_item > 0 ) {
349 $this->set_id( $the_item );
350 } elseif ( is_object( $the_item ) && isset( $the_item->ID ) && ! empty( $the_item->ID ) ) {
351 $this->set_id( absint( $the_item->ID ) );
352 $this->post_data = $the_item;
353 } else {
354 $this->set_id( 0 );
355 }
356
357 $this->set_data( $this->id, $cache );
358 }
359
360 /**
361 * Set ID.
362 *
363 * @param int $id ID.
364 */
365 public function set_id( $id ) {
366 $this->id = absint( $id );
367 }
368
369 /**
370 * Get ID.
371 */
372 public function get_id() {
373 return $this->id;
374 }
375
376 /**
377 * Set data.
378 *
379 * @since 2.2.2
380 * @param int $post_id post ID of the Item.
381 * @param boolean $cache Switch of cache.
382 */
383 public function set_data( $post_id, $cache = true ) {
384 if ( ! $post_id ) {
385 return $this->item_format;
386 }
387
388 $cache_key = 'wel_product_data_' . $post_id;
389 $product_data = wp_cache_get( $cache_key );
390
391 if ( $cache && is_array( $product_data ) ) {
392
393 foreach ( $product_data as $key => $value ) {
394 if ( array_key_exists( $key, $this->item_format ) ) {
395 $this->item_data[ $key ] = $value;
396 }
397 }
398 $this->post_data = $product_data['_pst'];
399 $this->ext_data = $product_data['_ext'];
400 $this->sku_data = $product_data['_sku'];
401 $this->opt_data = $product_data['_opt'];
402 $this->product_data = $product_data;
403
404 } else {
405
406 if ( ! empty( $this->post_data ) ) {
407 $post_data = $this->post_data;
408 } else {
409 $post_data = get_post( $post_id );
410 }
411
412 if ( null === $post_data ) {
413 return false;
414 }
415
416 $_meta = get_post_meta( $post_id );
417 $temp = array();
418 $item = array();
419
420 foreach ( $_meta as $key => $arr ) {
421
422 $value_arr = array();
423 $value = '';
424 $meta_num = count( $arr );
425 $reserved_key = ltrim( $key, '_' );
426
427 foreach ( $arr as $ind => $v ) {
428 $value_arr[] = @maybe_unserialize( $v );
429 }
430
431 if ( '_iopt_' === $key || '_isku_' === $key ) {
432
433 continue;
434
435 } elseif ( array_key_exists( $reserved_key, $this->item_format ) ) {
436
437 continue;
438
439 } elseif ( ! array_key_exists( $key, $this->exclusion_key ) ) {
440
441 if ( '' === $key || '_' === substr( $key, 0, 1 ) || is_array( maybe_unserialize( $key ) ) ) {
442 continue;
443 }
444 $ac = is_array( $arr ) ? count( $arr ) : null;
445 if ( is_array( $arr ) && 1 < $ac ) {
446 $val = $arr;
447 } elseif ( is_array( $arr ) && 1 === $ac ) {
448 $val = $arr[0];
449 } else {
450 $val = $arr;
451 }
452
453 $this->ext_data[ $key ] = $val;
454 }
455 }
456 $this->item_data = $this->get_item( $cache );
457 $this->opt_data = $this->get_opts( 'sort', $cache );
458 $this->sku_data = $this->get_skus( 'sort', $cache );
459
460 $product_data = $this->item_data;
461 $product_data['ID'] = $post_data->ID;
462 $product_data['_pst'] = $post_data;
463 $product_data['_ext'] = $this->ext_data;
464 $product_data['_sku'] = $this->sku_data;
465 $product_data['_opt'] = $this->opt_data;
466
467 $this->product_data = $product_data;
468
469 wp_cache_set( $cache_key, $product_data );
470 }
471 }
472
473 /**
474 * Returns perfect object.
475 *
476 * @since 2.2.2
477 * @param boolean $cache Switch of cache.
478 * @return array
479 */
480 public function get_product( $cache = true ) {
481 if ( ! isset( $this->id ) ) {
482 return false;
483 }
484
485 if ( ! $cache ) {
486 $this->set_data( $this->id, $cache );
487 }
488 return $this->product_data;
489 }
490
491 /**
492 * Returns item data of this object.
493 *
494 * @since 2.2.2
495 * @param boolean $cache Switch of cache.
496 * @return array
497 */
498 public function get_item( $cache = true ) {
499 global $wpdb, $usces;
500
501 if ( ! isset( $this->id ) ) {
502 return false;
503 }
504
505 $item = array();
506
507 if ( ! isset( $usces->update_db_2_7 ) ) {
508
509 $cache_key = 'wel_get_item_' . $this->id;
510 if ( $cache ) {
511 $res = wp_cache_get( $cache_key );
512 } else {
513 $res = false;
514 }
515
516 if ( false === $res ) {
517
518 $item_table = $wpdb->prefix . 'usces_item';
519
520 $res = $wpdb->get_row(
521 $wpdb->prepare(
522 "SELECT * FROM {$item_table} WHERE post_id = %d",
523 $this->id
524 ),
525 ARRAY_A
526 );
527 if ( null === $res ) {
528 $item = $this->item_format;
529 }
530 wp_cache_set( $cache_key, $res );
531 }
532
533 if ( $res ) {
534
535 foreach ( $res as $key => $value ) {
536
537 $json = is_null( $value ) ? $value : json_decode( $value, true );
538
539 if ( 'itemPicts' === $key ) {
540 if ( 0 === strlen( $value ) ) {
541 $item['itemPicts'] = '';
542 } else {
543 $item['itemPicts'] = explode( ';', $value );
544 }
545 } elseif ( 'itemCode' === $key || 'itemName' === $key ) {
546 $item[ $key ] = $value;
547 } elseif ( null !== $json ) {
548 $item[ $key ] = $json;
549 } else {
550 $item[ $key ] = $value;
551 }
552 }
553 }
554
555 $this->item_data = $item;
556 }
557
558 return $this->item_data;
559 }
560
561 /**
562 * Returns all sku sorted data of this object.
563 *
564 * @since 2.2.2
565 * @param string $keyflag Regenerate skus with sku value as skus index.
566 * @param boolean $cache Switch of cache.
567 * @return array
568 */
569 public function get_skus( $keyflag = 'sort', $cache = true ) {
570 global $wpdb, $usces;
571
572 if ( ! isset( $this->id ) ) {
573 return false;
574 }
575
576 if ( version_compare( USCES_VERSION, '2.7-beta', '>=' ) && ! isset( $usces->update_db_2_7 ) ) {
577
578 $cache_key = 'wel_get_skus_' . $this->id;
579 if ( $cache ) {
580 $res = wp_cache_get( $cache_key );
581 } else {
582 $res = false;
583 }
584
585 if ( false === $res ) {
586
587 $sku_table = $wpdb->prefix . 'usces_skus';
588
589 $res = $wpdb->get_results(
590 $wpdb->prepare(
591 "SELECT * FROM {$sku_table} WHERE post_id = %d",
592 $this->id
593 ),
594 ARRAY_A
595 );
596 if ( null === $res ) {
597 $res = false;
598 }
599 wp_cache_set( $cache_key, $res );
600 }
601
602 $skus = array();
603 if ( false !== $res && is_array( $res ) ) {
604
605 foreach ( $res as $rows ) {
606 $key = isset( $rows[ $keyflag ] ) ? $rows[ $keyflag ] : $rows['sort'];
607 $sku = array();
608 foreach ( $rows as $k => $v ) {
609 if ( 'advance' === $k ) {
610 $json = is_null( $v ) ? $v : json_decode( $v, true );
611 if ( null !== $json ) {
612 $sku[ $k ] = $json;
613 } else {
614 $sku[ $k ] = $v;
615 }
616 } elseif ( 'price' === $k || 'cprice' === $k ) {
617 $sku[ $k ] = floatval( $v );
618 } else {
619 $sku[ $k ] = $v;
620 }
621 }
622 $skus[ $key ] = $sku;
623 }
624 }
625 } else {
626
627 $metas = $this->get_post_meta( '_isku_', $cache );
628 if ( false === $metas ) {
629 $metas = array();
630 }
631 $skus = array();
632
633 foreach ( $metas as $rows ) {
634 $values = unserialize( $rows['meta_value'] );
635 $values['meta_id'] = isset( $rows['meta_id'] ) ? (int) $rows['meta_id'] : '';
636 $key = isset( $values[ $keyflag ] ) ? $values[ $keyflag ] : $values['sort'];
637
638 $new_values = $this->sku_format;
639 foreach ( $values as $k => $v ) {
640 if ( array_key_exists( $k, $this->sku_format ) ) {
641 $new_values[ $k ] = $v;
642 }
643 }
644 $skus[ $key ] = $new_values;
645 }
646 }
647
648 ksort( $skus );
649 return $skus;
650 }
651
652 /**
653 * Returns all option sorted data of this object.
654 *
655 * @since 2.3.3
656 * @param string $keyflag Regenerate option datas with option value as datas index.
657 * @param boolean $cache Switch of cache.
658 * @return array
659 */
660 public function get_opts( $keyflag = 'sort', $cache = true ) {
661 global $wpdb, $usces;
662
663 if ( ! isset( $this->id ) ) {
664 return false;
665 }
666
667 if ( version_compare( USCES_VERSION, '2.7-beta', '>=' ) && ! isset( $usces->update_db_2_7 ) ) {
668
669 $cache_key = 'wel_get_opts_' . $this->id;
670
671 if ( $cache ) {
672 $res = wp_cache_get( $cache_key );
673 } else {
674 $res = false;
675 }
676
677 if ( false === $res ) {
678
679 $opt_table = $wpdb->prefix . 'usces_opts';
680
681 $res = $wpdb->get_results(
682 $wpdb->prepare(
683 "SELECT * FROM {$opt_table} WHERE post_id = %d",
684 $this->id
685 ),
686 ARRAY_A
687 );
688
689 if ( null === $res ) {
690 $res = false;
691 }
692 wp_cache_set( $cache_key, $res );
693 }
694
695 $opts = array();
696 if ( false !== $res && is_array( $res ) ) {
697
698 foreach ( $res as $rows ) {
699 $key = isset( $rows[ $keyflag ] ) ? $rows[ $keyflag ] : $rows['sort'];
700 $opt = array();
701 foreach ( $rows as $k => $v ) {
702 $json = is_null( $v ) ? $v : json_decode( $v, true );
703 if ( null !== $json && ! is_numeric( $v ) ) {
704 $opt[ $k ] = $json;
705 } else {
706 $opt[ $k ] = $v;
707 }
708 }
709 $opts[ $key ] = $opt;
710 }
711 }
712 } else {
713
714 $metas = $this->get_post_meta( '_iopt_', $cache );
715 if ( false === $metas ) {
716 $metas = array();
717 }
718 $opts = array();
719 foreach ( $metas as $rows ) {
720
721 $values = unserialize( $rows['meta_value'] );
722 $key = isset( $values[ $keyflag ] ) ? $values[ $keyflag ] : $values['sort'];
723 $values['meta_id'] = isset( $rows['meta_id'] ) ? (int) $rows['meta_id'] : '';
724 $new_values = $this->opt_format;
725 foreach ( $values as $k => $v ) {
726 if ( array_key_exists( $k, $this->opt_format ) ) {
727 $new_values[ $k ] = $v;
728 }
729 }
730 $opts[ $key ] = $new_values;
731 }
732 }
733
734 ksort( $opts );
735 return $opts;
736 }
737
738 /**
739 * Returns extra data of this object.
740 *
741 * @since 2.3.3
742 * @return array
743 */
744 public function get_ext() {
745 if ( ! isset( $this->id ) ) {
746 return false;
747 }
748
749 return $this->ext_data;
750 }
751
752 /**
753 * Returns meta data by meta key.
754 *
755 * @since 2.2.2
756 * @param string $key Meta key.
757 * @param boolean $cache Switch of cache.
758 * @return array
759 */
760 public function get_post_meta( $key, $cache = true ) {
761 global $wpdb;
762
763 if ( 0 === $this->id ) {
764 return false;
765 }
766
767 $cache_key = 'wel_get_post_meta_' . $this->id . '_' . $key;
768 if ( $cache ) {
769 $meta_data = wp_cache_get( $cache_key );
770 } else {
771 $meta_data = false;
772 }
773
774 if ( false === $meta_data ) {
775 $meta_data = $wpdb->get_results(
776 $wpdb->prepare(
777 "SELECT pm.meta_id, pm.meta_key, pm.meta_value FROM $wpdb->posts AS `post`
778 LEFT JOIN $wpdb->postmeta AS `pm` ON post.ID = pm.post_id
779 WHERE pm.meta_key = %s AND post.ID = %d AND ( post.post_status IN ( %s, %s, %s ) OR post.post_status LIKE %s ) AND (post.post_type = %s OR post.post_type = %s)",
780 $key,
781 $this->id,
782 'publish',
783 'private',
784 'future',
785 '%draft%',
786 'post',
787 'page'
788 ),
789 ARRAY_A
790 );
791
792 if ( empty( $meta_data ) ) {
793 $meta_data = false;
794 }
795 wp_cache_set( $cache_key, $meta_data );
796 }
797
798 if ( false === $meta_data ) {
799 return array();
800 } else {
801 return $meta_data;
802 }
803 }
804
805 /**
806 * Returns post ID by item code.
807 *
808 * @since 2.3.3
809 * @param string $item_code Item code to get post ID.
810 * @param boolean $cache Switch of cache.
811 * @return int Post ID.
812 */
813 public function get_id_by_item_code( $item_code, $cache = true ) {
814 global $wpdb;
815
816 $cache_key = 'wel_post_id_by_item_code_' . $item_code;
817 if ( $cache ) {
818 $post_id = wp_cache_get( $cache_key );
819 } else {
820 $post_id = false;
821 }
822
823 if ( false === $post_id ) {
824
825 if ( version_compare( USCES_VERSION, '2.7-beta', '>=' ) && ! isset( $usces->update_db_2_7 ) ) {
826
827 $table = $wpdb->prefix . 'usces_item';
828 $query = $wpdb->prepare(
829 "SELECT post.ID FROM {$wpdb->posts} AS `post`
830 LEFT JOIN {$table} AS `item` ON post.ID = item.post_id
831 WHERE item.itemCode = %s AND post.post_status IN ( %s, %s, %s, %s ) AND post.post_type = %s
832 LIMIT 1",
833 $item_code,
834 'publish',
835 'private',
836 'draft',
837 'future',
838 'post'
839 );
840
841 } else {
842
843 $query = $wpdb->prepare(
844 "SELECT post.ID FROM $wpdb->posts AS `post`
845 LEFT JOIN $wpdb->postmeta AS `pm` ON post.ID = pm.post_id AND pm.meta_key = %s
846 WHERE pm.meta_value = %s AND post.post_status IN ( %s, %s, %s, %s ) AND post.post_type = %s
847 LIMIT 1",
848 '_itemCode',
849 $item_code,
850 'publish',
851 'private',
852 'draft',
853 'future',
854 'post'
855 );
856 }
857
858 $query = apply_filters( 'usces_filter_get_postidbycode_query', $query, $item_code );
859 $post_id = $wpdb->get_var( $query );
860
861 if ( null !== $post_id ) {
862 wp_cache_set( $cache_key, $post_id );
863 }
864 }
865
866 if ( null === $post_id ) {
867 return false;
868 } else {
869 return $post_id;
870 }
871 }
872
873 /**
874 * Updating product meta information.
875 * In the delete mode, the data is deleted only when there are multiple duplicate data.
876 *
877 * @since 3.0.0
878 * @param string $data Meta data.
879 * @param boolean $delete True if you want to delete before updating.
880 */
881 public function update_item_data( $data, $delete = false ) {
882 global $wpdb;
883
884 if ( 0 === $this->id ) {
885 return false;
886 }
887
888 if ( ! isset( $data['itemCode'] ) || empty( $data['itemCode'] ) ) {
889 return false;
890 }
891
892 $post_id = $this->id;
893 $item_table = $wpdb->prefix . 'usces_item';
894 $new_item = array();
895 $item_format = $this->item_format;
896
897 foreach ( $item_format as $key => $value ) {
898
899 if ( 'itemPicts' === $key ) {
900
901 if ( isset( $data['itemPicts'] ) ) {
902 $new_item[ $key ] = implode( ';', (array) $data[ $key ] );
903 } else {
904 $new_item[ $key ] = implode( ';', (array) $value );
905 }
906 } elseif ( isset( $data[ $key ] ) ) {
907
908 if ( is_array( $data[ $key ] ) ) {
909 $new_item[ $key ] = wp_json_encode( $data[ $key ] );
910 } else {
911 $new_item[ $key ] = $data[ $key ];
912 }
913 } else {
914
915 if ( is_array( $value ) ) {
916 $new_item[ $key ] = wp_json_encode( $value );
917 } else {
918 $new_item[ $key ] = $value;
919 }
920 }
921 }
922
923 $new_item['post_id'] = $post_id;
924 $placeholders = $this->placeholders;
925
926 $res = $wpdb->replace( $item_table, $new_item, $placeholders );
927
928 $this->set_data( $post_id, false );
929
930 return $res;
931 }
932
933 /**
934 * Returns sku data by sku code.
935 *
936 * @since 2.2.2
937 * @param string $sku_code Sku code to get.
938 * @param boolean $cache Switch of cache.
939 * @return array
940 */
941 public function get_sku_by_code( $sku_code, $cache = true ) {
942 $skus = $this->get_skus( 'code', $cache );
943 $sku = isset( $skus[ $sku_code ] ) ? $skus[ $sku_code ] : false;
944 return $sku;
945 }
946
947 /**
948 * Returns sku data by meta id.
949 *
950 * @since 2.2.2
951 * @param string $meta_id Sku meta_id.
952 * @param boolean $cache Switch of cache.
953 * @return array $sku SKU data.
954 */
955 public function get_sku_by_id( $meta_id, $cache = true ) {
956 $skus = $this->get_skus( 'meta_id', $cache );
957 $sku = isset( $skus[ $meta_id ] ) ? $skus[ $meta_id ] : false;
958 return $sku;
959 }
960
961 /**
962 * Updating SKU data by id.
963 *
964 * @since 3.0.0
965 * @param array $sku SKU data.
966 * @return boolean
967 */
968 public function update_sku_data( $sku ) {
969 global $wpdb;
970
971 if ( 0 === $this->id ) {
972 return false;
973 }
974
975 if ( ! isset( $sku['meta_id'] ) || empty( $sku['meta_id'] ) ) {
976 $target = $this->get_sku_by_code( $sku['code'], false );
977 $meta_id = $target['meta_id'];
978 } else {
979 $meta_id = $sku['meta_id'];
980 }
981
982 $new_sku = array();
983 $sku_format = $this->get_sku_format();
984 foreach ( $sku_format as $key => $value ) {
985 if ( array_key_exists( $key, $sku ) ) {
986 if ( is_array( $sku[ $key ] ) ) {
987 $new_sku[ $key ] = wp_json_encode( $sku[ $key ] );
988 } else {
989 $new_sku[ $key ] = $sku[ $key ];
990 }
991 } else {
992 if ( is_array( $value ) ) {
993 $new_sku[ $key ] = wp_json_encode( $value );
994 } else {
995 $new_sku[ $key ] = $value;
996 }
997 }
998 }
999 $new_sku['post_id'] = $this->id;
1000
1001 $table = $wpdb->prefix . 'usces_skus';
1002 $placeholders = $this->sku_placeholders;
1003 $res = $wpdb->replace( $table, $new_sku, $placeholders );
1004
1005 $this->set_data( $this->id, false );
1006
1007 return $res;
1008 }
1009
1010 /**
1011 * Adding SKU data.
1012 *
1013 * @since 3.0.0
1014 * @param array $sku SKU data.
1015 * @return int New meta_id.
1016 */
1017 public function add_sku_data( $sku ) {
1018 global $wpdb;
1019
1020 if ( 0 === $this->id ) {
1021 return false;
1022 }
1023
1024 $new_sku = array();
1025 $sku_format = $this->get_sku_format();
1026 foreach ( $sku_format as $key => $value ) {
1027 if ( array_key_exists( $key, $sku ) ) {
1028 if ( is_array( $sku[ $key ] ) ) {
1029 $new_sku[ $key ] = wp_json_encode( $sku[ $key ] );
1030 } else {
1031 $new_sku[ $key ] = $sku[ $key ];
1032 }
1033 } else {
1034 if ( is_array( $value ) ) {
1035 $new_sku[ $key ] = wp_json_encode( $value );
1036 } else {
1037 $new_sku[ $key ] = $value;
1038 }
1039 }
1040 }
1041 $new_sku['post_id'] = $this->id;
1042
1043 $table = $wpdb->prefix . 'usces_skus';
1044 $placeholders = $this->sku_placeholders;
1045 $wpdb->insert( $table, $new_sku, $placeholders );
1046
1047 $this->set_data( $this->id, false );
1048
1049 return $wpdb->insert_id;
1050 }
1051
1052 /**
1053 * Delete Item data.
1054 *
1055 * @since 3.0.0
1056 * @return boolean Result.
1057 */
1058 public function delete_item_data() {
1059 global $wpdb;
1060
1061 if ( 0 === $this->id ) {
1062 return false;
1063 }
1064
1065 $table = $wpdb->prefix . 'usces_item';
1066 $where = array( 'post_id' => $this->id );
1067 $format = array( '%d' );
1068 $res = $wpdb->delete( $table, $where, $format );
1069
1070 return $res;
1071 }
1072
1073 /**
1074 * Delete SKU data.
1075 *
1076 * @since 3.0.0
1077 * @param int $meta_id Meta id.
1078 * @return boolean Result.
1079 */
1080 public function delete_sku_data( $meta_id ) {
1081 global $wpdb;
1082
1083 if ( 0 === $this->id ) {
1084 return false;
1085 }
1086
1087 $table = $wpdb->prefix . 'usces_skus';
1088 $where = array( 'meta_id' => $meta_id );
1089 $format = array( '%d' );
1090 $res = $wpdb->delete( $table, $where, $format );
1091
1092 return $res;
1093 }
1094
1095 /**
1096 * Delete All SKU data.
1097 *
1098 * @since 3.0.0
1099 * @return boolean Result.
1100 */
1101 public function delete_all_sku_data() {
1102 global $wpdb;
1103
1104 if ( 0 === $this->id ) {
1105 return false;
1106 }
1107
1108 $table = $wpdb->prefix . 'usces_skus';
1109 $where = array( 'post_id' => $this->id );
1110 $format = array( '%d' );
1111 $res = $wpdb->delete( $table, $where, $format );
1112
1113 return $res;
1114 }
1115
1116 /**
1117 * Updating Option data by id.
1118 *
1119 * @since 3.0.0
1120 * @param array $opt Option data.
1121 * @return boolean
1122 */
1123 public function update_opt_data( $opt ) {
1124 global $wpdb;
1125
1126 if ( 0 === $this->id ) {
1127 return false;
1128 }
1129
1130 if ( ! isset( $opt['meta_id'] ) || empty( $opt['meta_id'] ) ) {
1131 return false;
1132 } else {
1133 $meta_id = $opt['meta_id'];
1134 }
1135
1136 $new_opt = array();
1137 $opt_format = $this->get_opt_format();
1138 foreach ( $opt_format as $key => $value ) {
1139 if ( array_key_exists( $key, $opt ) ) {
1140 if ( is_array( $opt[ $key ] ) ) {
1141 $new_opt[ $key ] = wp_json_encode( $opt[ $key ] );
1142 } else {
1143 $new_opt[ $key ] = $opt[ $key ];
1144 }
1145 } else {
1146 if ( is_array( $value ) ) {
1147 $new_opt[ $key ] = wp_json_encode( $value );
1148 } else {
1149 $new_opt[ $key ] = $value;
1150 }
1151 }
1152 }
1153
1154 $new_opt['post_id'] = $this->id;
1155
1156 $table = $wpdb->prefix . 'usces_opts';
1157 $placeholders = $this->opt_placeholders;
1158
1159 $res = $wpdb->replace( $table, $new_opt, $placeholders );
1160
1161 $this->set_data( $this->id, false );
1162
1163 return $res;
1164 }
1165
1166 /**
1167 * Adding option data.
1168 *
1169 * @since 3.0.0
1170 * @param array $opt Option data.
1171 * @return int New meta_id.
1172 */
1173 public function add_opt_data( $opt ) {
1174 global $wpdb;
1175
1176 if ( 0 === $this->id ) {
1177 return false;
1178 }
1179
1180 $new_opt = $this->opt_format;
1181 foreach ( $opt as $key => $value ) {
1182 if ( array_key_exists( $key, $new_opt ) ) {
1183 $new_opt[ $key ] = $value;
1184 }
1185 }
1186
1187 $table = $wpdb->prefix . 'usces_opts';
1188 $placeholders = $this->opt_placeholders;
1189
1190 $new_opt['post_id'] = $this->id;
1191
1192 $res = $wpdb->insert( $table, $new_opt, $placeholders );
1193
1194 $this->set_data( $this->id, false );
1195
1196 return $wpdb->insert_id;
1197 }
1198
1199 /**
1200 * Delete Option data.
1201 *
1202 * @since 3.0.0
1203 * @param int $meta_id Meta id.
1204 * @return boolean Result.
1205 */
1206 public function delete_opt_data( $meta_id ) {
1207 global $wpdb;
1208
1209 if ( 0 === $this->id ) {
1210 return false;
1211 }
1212
1213 $table = $wpdb->prefix . 'usces_opts';
1214 $where = array( 'meta_id' => $meta_id );
1215 $format = array( '%d' );
1216 $res = $wpdb->delete( $table, $where, $format );
1217
1218 return $res;
1219 }
1220
1221 /**
1222 * Delete All Option data.
1223 *
1224 * @since 3.0.0
1225 * @return boolean Result.
1226 */
1227 public function delete_all_opt_data() {
1228 global $wpdb;
1229
1230 if ( 0 === $this->id ) {
1231 return false;
1232 }
1233
1234 $table = $wpdb->prefix . 'usces_opts';
1235 $where = array( 'post_id' => $this->id );
1236 $format = array( '%d' );
1237 $res = $wpdb->delete( $table, $where, $format );
1238
1239 $this->set_data( $this->id, false );
1240
1241 return $res;
1242 }
1243
1244 /**
1245 * Get Item format.
1246 *
1247 * @since 3.0.0
1248 * @return array Format.
1249 */
1250 public function get_item_format() {
1251 return $this->item_format;
1252 }
1253
1254 /**
1255 * Get Item placeholders.
1256 *
1257 * @since 3.0.0
1258 * @return array Format.
1259 */
1260 public function get_item_placeholders() {
1261 return $this->placeholders;
1262 }
1263
1264 /**
1265 * Get Item old key.
1266 *
1267 * @since 3.0.0
1268 * @return array Format.
1269 */
1270 public function get_item_old_key() {
1271 return $this->item_old_key;
1272 }
1273
1274 /**
1275 * Get SKU format.
1276 *
1277 * @since 3.0.0
1278 * @return array Format.
1279 */
1280 public function get_sku_format() {
1281 return $this->sku_format;
1282 }
1283
1284 /**
1285 * Get SKU placeholders.
1286 *
1287 * @since 3.0.0
1288 * @return array Format.
1289 */
1290 public function get_sku_placeholders() {
1291 return $this->sku_placeholders;
1292 }
1293
1294 /**
1295 * Get Option format.
1296 *
1297 * @since 3.0.0
1298 * @return array Format.
1299 */
1300 public function get_opt_format() {
1301 return $this->opt_format;
1302 }
1303
1304 /**
1305 * Get Exclusion key.
1306 *
1307 * @since 3.0.0
1308 * @return array Format.
1309 */
1310 public function get_exclusion_key() {
1311 return $this->exclusion_key;
1312 }
1313
1314 }
1315