Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/tutor/models/OrderItemMetaModel.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
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 +