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.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / models / OrderItemMetaModel.php
tutor / models Last commit date
BaseModel.php 9 months ago BillingModel.php 1 year ago CartItemModel.php 9 months ago CartModel.php 5 months ago CouponModel.php 4 months ago CourseModel.php 3 months ago LessonModel.php 9 months ago OrderActivitiesModel.php 1 year ago OrderItemMetaModel.php 9 months ago OrderItemModel.php 9 months ago OrderMetaModel.php 1 year ago OrderModel.php 2 months ago QuizModel.php 3 months ago UserModel.php 1 year ago WithdrawModel.php 3 weeks ago
OrderItemMetaModel.php
161 lines
1 <?php
2 /**
3 * Order Item Meta Model
4 * Handles CRUD operations for order item meta data.
5 *
6 * @package Tutor\Models
7 * @author Themeum <support@themeum.com>
8 * @link https://themeum.com
9 * @since 3.8.0
10 */
11
12 namespace Tutor\Models;
13
14 use Tutor\Helpers\QueryHelper;
15
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit;
18 }
19
20 /**
21 * Class OrderItemModel
22 */
23 class OrderItemMetaModel {
24
25 /**
26 * Table name.
27 *
28 * @var string
29 */
30 private $table;
31
32 /**
33 * Constructor.
34 */
35 public function __construct() {
36 global $wpdb;
37 $this->table = $wpdb->prefix . 'tutor_order_itemmeta';
38 }
39
40 /**
41 * Add meta for an order item.
42 *
43 * @since 3.8.0
44 *
45 * @param int $item_id Item ID.
46 * @param string $meta_key Meta key.
47 * @param mixed $meta_value Meta value.
48 *
49 * @return int Inserted row ID on success, 0 on failure.
50 */
51 public function add_meta( $item_id, $meta_key, $meta_value ):int {
52 $meta_id = QueryHelper::insert(
53 $this->table,
54 array(
55 'item_id' => $item_id,
56 'meta_key' => $meta_key,
57 'meta_value' => maybe_serialize( $meta_value ),
58 ),
59 );
60
61 return (int) $meta_id;
62 }
63
64 /**
65 * Get meta by item_id and optional meta_key.
66 *
67 * @since 3.8.0
68 *
69 * @param int $item_id Item ID.
70 * @param string|null $meta_key Meta key (optional).
71 * @param bool $single Get a single value or all.
72 *
73 * @return array|false Meta results or false if none.
74 */
75 public function get_meta( int $item_id, $meta_key = null, $single = true ) {
76 $where = array(
77 'item_id' => $item_id,
78 );
79
80 if ( $meta_key ) {
81 $where['meta_key'] = sanitize_key( $meta_key );
82 }
83
84 if ( $single ) {
85 $meta = QueryHelper::get_row(
86 $this->table,
87 $where,
88 'item_id'
89 );
90 if ( $meta ) {
91 $meta->meta_value = maybe_unserialize( $meta->meta_value );
92 }
93 } else {
94 $meta = QueryHelper::get_all(
95 $this->table,
96 $where,
97 'item_id'
98 );
99
100 if ( tutor_utils()->count( $meta ) ) {
101 foreach ( $meta as $row ) {
102 $row->meta_value = maybe_unserialize( $row->meta_value );
103 }
104 }
105 }
106
107 return $meta;
108 }
109
110 /**
111 * Update meta for an order item.
112 *
113 * @since 3.8.0
114 *
115 * @param int $item_id Item ID.
116 * @param string $meta_key Meta key.
117 * @param mixed $meta_value Meta value.
118 *
119 * @return bool True on success, false on failure.
120 */
121 public function update_meta( int $item_id, string $meta_key, $meta_value ): bool {
122 $is_meta_exists = $this->get_meta( $item_id, $meta_key );
123 if ( ! $is_meta_exists ) {
124 return $this->add_meta( $item_id, $meta_key, $meta_value );
125 }
126
127 return QueryHelper::update(
128 $this->table,
129 array(
130 'meta_value' => maybe_serialize( $meta_value ),
131 ),
132 array(
133 'item_id' => $item_id,
134 'meta_key' => $meta_key,
135 ),
136 );
137 }
138
139 /**
140 * Delete meta by item_id and optional meta_key.
141 *
142 * @since 3.8.0
143 *
144 * @param int $item_id Item ID.
145 * @param string|null $meta_key Meta key (optional).
146 *
147 * @return bool True on success, false on failure.
148 */
149 public function delete_meta( $item_id, $meta_key = null ): bool {
150 $where = array(
151 'item_id' => $item_id,
152 );
153
154 if ( $meta_key ) {
155 $where['meta_key'] = $meta_key;
156 }
157
158 return QueryHelper::delete( $this->table, $where );
159 }
160 }
161