Repository navigation
Expand file tree
/
Copy pathOrderActivitiesModel.php
More file actions
210 lines (190 loc) · 5.26 KB
/
Copy pathOrderActivitiesModel.php
File metadata and controls
210 lines (190 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/**
* Order Model
*
* @package Tutor\Models
* @author Themeum <support@themeum.com>
* @link https://themeum.com
* @since 3.0.0
*/
namespace Tutor\Models;
use Tutor\Helpers\DateTimeHelper;
use Tutor\Helpers\QueryHelper;
/**
* OrderActivitiesModel Class
*
* @since 3.0.0
*/
class OrderActivitiesModel {
/**
* Order Meta keys for history, refund & comment
*
* @since 3.0.0
*
* @var string
*/
const META_KEY_HISTORY = 'history';
const META_KEY_REFUND = 'refund';
const META_KEY_PARTIALLY_REFUND = 'partially-refund';
const META_KEY_COMMENT = 'comment';
const META_KEY_CANCEL_REASON = 'cancel-reason';
/**
* Order meta table name
*
* @since 3.0.0
*
* @var string
*/
private $table_name = 'tutor_ordermeta';
/**
* Resolve props & dependencies
*
* @since 3.0.0
*/
public function __construct() {
global $wpdb;
$this->table_name = $wpdb->prefix . $this->table_name;
}
/**
* Get table name with wp prefix
*
* @since 3.0.0
*
* @return string
*/
public function get_table_name() {
return $this->table_name;
}
/**
* Get all Meta keys
*
* @since 3.0.0
*
* @return array
*/
public static function get_meta_keys() {
return array(
self::META_KEY_HISTORY => __( 'History', 'tutor' ),
self::META_KEY_REFUND => __( 'Refund', 'tutor' ),
self::META_KEY_COMMENT => __( 'Comment', 'tutor' ),
);
}
/**
* Retrieve order activities by order ID.
*
* This function fetches all order activities from the 'tutor_ordermeta' table
* based on the given order ID and the 'history' meta key. It uses a helper
* function from the QueryHelper class to perform the database query.
*
* If no order activities are found, the function returns an empty array.
* Otherwise, it decodes the JSON-encoded meta values and returns them as an array.
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param int $order_id The ID of the order to retrieve activities for.
*
* @since 3.0.0
*
* @return array An array of order activities, each decoded from its JSON representation.
*/
public function get_order_activities( $order_id ) {
global $wpdb;
// Retrieve order activities for the given order ID from the 'tutor_ordermeta' table.
$meta_keys = array(
self::META_KEY_COMMENT,
self::META_KEY_HISTORY,
self::META_KEY_REFUND,
self::META_KEY_PARTIALLY_REFUND,
);
$order_activities = QueryHelper::get_all(
$this->table_name,
array(
'order_id' => $order_id,
'meta_key' => $meta_keys,
),
'id'
);
if ( empty( $order_activities ) ) {
return array();
}
$response = array();
try {
foreach ( $order_activities as &$activity ) {
$values = new \stdClass();
if ( tutor_is_json( $activity->meta_value ) ) {
$values = json_decode( $activity->meta_value );
} else {
$values->message = $activity->meta_value;
}
$values->id = (int) $activity->id;
$values->created_at_readable = DateTimeHelper::get_gmt_to_user_timezone_date( $activity->created_at_gmt );
$values->type = $activity->meta_key;
$response[] = $values;
}
} catch ( \Throwable $th ) {
// Log error message with file info.
error_log( $th->getMessage() . ' in ' . $th->getFile() . ' at line ' . $th->getLine() );
}
unset( $activity );
return $response;
}
/**
* Add order metadata.
*
* Inserts metadata into the 'tutor_ordermeta' table based on the provided data object.
*
* @since 3.0.0
*
* @param object $data An object containing order metadata:
* - $data->order_id (int) The ID of the order.
* - $data->meta_key (string) The meta key for the metadata.
* - $data->meta_value (string) The meta value(JSON) for the metadata.
*
* @return int The ID of the inserted row on success, or 0 on failure.
*/
public function add_order_meta( object $data ) {
$current_time = current_time( 'mysql', true );
$current_user_id = get_current_user_id();
return QueryHelper::insert(
$this->table_name,
array(
'order_id' => $data->order_id,
'meta_key' => $data->meta_key,
'meta_value' => $data->meta_value,
'created_at_gmt' => $current_time,
'created_by' => $current_user_id,
'updated_at_gmt' => $current_time,
'updated_by' => $current_user_id,
)
);
}
/**
* Update order metadata.
*
* Updates metadata in the 'tutor_ordermeta' table based on the provided data object.
*
* @since 3.0.0
*
* @param object $data An object containing order metadata:
* - $data->order_id (int) The ID of the order.
* - $data->meta_key (string) The meta key for the metadata.
* - $data->meta_value (string) The meta value(JSON) for the metadata.
*
* @return bool True on success, false on failure.
*/
public function update_order_meta( object $data ) {
return QueryHelper::update(
$this->table_name,
array(
'meta_key' => $data->meta_key,
'meta_value' => $data->meta_value,
'updated_at_gmt' => current_time( 'mysql', true ),
'updated_by' => get_current_user_id(),
),
array(
'id' => $data->id,
'order_id' => $data->order_id,
)
);
}
}