Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/tutor-pro/gift-course/EventHandler.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
/**
3
+
* Handle Gift order events
4
+
*
5
+
* @package TutorPro\GiftCourse
6
+
* @author Themeum <support@themeum.com>
7
+
* @link https://themeum.com
8
+
* @since 3.8.0
9
+
*/
10
+
11
+
namespace TutorPro\GiftCourse;
12
+
13
+
use AllowDynamicProperties;
14
+
use Tutor\Ecommerce\Ecommerce;
15
+
use Tutor\Helpers\DateTimeHelper;
16
+
use Tutor\Models\CartItemModel;
17
+
18
+
/**
19
+
* Handle events for Gift Course
20
+
*
21
+
* @since 3.8.0
22
+
*/
23
+
#[AllowDynamicProperties]
24
+
class EventHandler {
25
+
26
+
/**
27
+
* Register hooks
28
+
*
29
+
* @since 3.8.0
30
+
*/
31
+
public function __construct() {
32
+
add_action( 'woocommerce_checkout_create_order_line_item', array( $this, 'woo_save_gift_data_to_order_items' ), 10, 4 );
33
+
add_action( 'woocommerce_order_status_changed', array( $this, 'handle_order_status_update' ), 10, 3 );
34
+
add_filter( 'tutor_is_gift_item', array( $this, 'check_gift_item' ), 10, 2 );
35
+
add_filter( 'tutor_before_order_create', array( $this, 'native_save_gift_data_to_order_items' ) );
36
+
add_action( 'tutor_order_payment_status_changed', array( $this, 'handle_order_status_update' ), 10, 3 );
37
+
add_filter( 'woocommerce_order_item_get_formatted_meta_data', array( $this, 'hide_tutor_gift_meta_on_thankyou' ), 10, 2 );
38
+
add_action( 'woocommerce_create_refund', array( $this, 'woo_restrict_refund' ), 10, 2 );
39
+
add_action( 'tutor_before_initiate_refund', array( $this, 'native_restrict_refund' ) );
40
+
add_filter( 'tutor_order_item_meta', array( $this, 'filter_meta_value_for_display' ) );
41
+
}
42
+
43
+
/**
44
+
* Save gift data with order items
45
+
*
46
+
* @since 3.8.0
47
+
*
48
+
* @param object $item WC product item.
49
+
* @param string $cart_item_key Cart key.
50
+
* @param array $values Cart data.
51
+
* @param object $order WC Order.
52
+
*
53
+
* @return void
54
+
*/
55
+
public function woo_save_gift_data_to_order_items( $item, $cart_item_key, $values, $order ) {
56
+
$monetize_by = $this->get_monetize_by();
57
+
if ( 'wc' !== $monetize_by ) {
58
+
return;
59
+
}
60
+
61
+
$is_gift_course_in_cart = ( new WooGiftProceeder() )->is_gift_course_in_cart( $values );
62
+
if ( $is_gift_course_in_cart ) {
63
+
$gift_data = $values[ GiftCourse::GIFT_DATA_META ];
64
+
if ( $gift_data ) {
65
+
$gift_data->order_id = $order->get_id();
66
+
$gift_data->monetize_by = $monetize_by;
67
+
68
+
$item->add_meta_data( GiftCourse::GIFT_FLAG_META, true );
69
+
$item->add_meta_data( GiftCourse::GIFT_DATA_META, $gift_data, true );
70
+
$item->add_meta_data( GiftCourse::GIFT_STATUS_META, GiftCourse::GIFT_STATUS_PROCESSING, true );
71
+
}
72
+
}
73
+
}
74
+
75
+
/**
76
+
* Save gift data with order items
77
+
*
78
+
* @since 3.8.0
79
+
*
80
+
* @param array $order_data Native order data.
81
+
*
82
+
* @return array
83
+
*/
84
+
public function native_save_gift_data_to_order_items( $order_data ) {
85
+
if ( tutor_utils()->count( $order_data ) ) {
86
+
$items = $order_data['items'];
87
+
if ( tutor_utils()->count( $items ) ) {
88
+
foreach ( $items as $key => $item ) {
89
+
$is_gift_item = ( new NativeGiftProceeder() )->is_gift_course_in_cart( $item['item_id'] );
90
+
if ( $is_gift_item ) {
91
+
$cart_item = ( new CartItemModel() )->get_row( array( 'course_id' => $item['item_id'] ) );
92
+
if ( $cart_item ) {
93
+
$gift_data = json_decode( $cart_item->item_details );
94
+
if ( ! empty( $gift_data ) ) {
95
+
$gift_data->order_id = $order_data['order_id'] ?? 0;
96
+
$gift_data->monetize_by = Ecommerce::MONETIZE_BY;
97
+
98
+
// Add gift meta data.
99
+
$item['meta_data'] = array(
100
+
array(
101
+
'meta_key' => GiftCourse::GIFT_FLAG_META,
102
+
'meta_value' => true,
103
+
),
104
+
array(
105
+
'meta_key' => GiftCourse::GIFT_DATA_META,
106
+
'meta_value' => $gift_data,
107
+
),
108
+
array(
109
+
'meta_key' => GiftCourse::GIFT_STATUS_META,
110
+
'meta_value' => GiftCourse::GIFT_STATUS_PROCESSING,
111
+
),
112
+
);
113
+
114
+
$items[ $key ] = $item;
115
+
}
116
+
}
117
+
}
118
+
}
119
+
120
+
$order_data['items'] = $items;
121
+
}
122
+
}
123
+
124
+
return $order_data;
125
+
}
126
+
127
+
/**
128
+
* Send emails & schedule gift data on order status update
129
+
*
130
+
* @since 3.8.0
131
+
*
132
+
* @param int $order_id Order id.
133
+
* @param string $status_from Order from status.
134
+
* @param string $status_to Order to status.
135
+
*
136
+
* @return void
137
+
*/
138
+
public function handle_order_status_update( $order_id, $status_from, $status_to ) {
139
+
// For native status_to is payment status.
140
+
$is_tutor_monetization = tutor_utils()->is_monetize_by_tutor();
141
+
if ( $is_tutor_monetization && 'paid' !== $status_to ) {
142
+
return;
143
+
}
144
+
145
+
try {
146
+
$proceeder = GiftCourse::get_gift_proceeder();
147
+
$order = $proceeder->get_order( $order_id );
148
+
149
+
if ( ! $order || 'completed' !== $order->order_status ) {
150
+
return;
151
+
}
152
+
153
+
$order_items = $order->items;
154
+
// If order is not gift course order.
155
+
if ( ! $order_items ) {
156
+
return;
157
+
}
158
+
159
+
foreach ( $order_items as $item ) {
160
+
try {
161
+
$gift_data = $proceeder->get_gift_data( $item );
162
+
if ( $gift_data ) {
163
+
// Set the order & item id.
164
+
$gift_data->order_id = $order_id;
165
+
166
+
// Primary is for native & id is for wc.
167
+
$gift_data->order_item_id = $is_tutor_monetization ? $item->primary_id : $item->get_id();
168
+
( new GiftMailer( $gift_data, $proceeder ) )->send_emails();
169
+
170
+
$can_process = time() >= strtotime( $gift_data->scheduled_at_gmt );
171
+
try {
172
+
( new GiftScheduler() )->schedule( $gift_data, $can_process ? GiftScheduler::STATUS_PROCESSED : GiftScheduler::STATUS_SCHEDULED );
173
+
} catch ( \Throwable $th ) {
174
+
tutor_log( $th );
175
+
}
176
+
}
177
+
} catch ( \Throwable $th ) {
178
+
tutor_log( $th );
179
+
}
180
+
}
181
+
} catch ( \Throwable $th ) {
182
+
tutor_log( $th );
183
+
}
184
+
}
185
+
186
+
/**
187
+
* Check if the item is gift course item
188
+
*
189
+
* @since 3.8.0
190
+
*
191
+
* @param bool $is_gift_item Default value.
192
+
* @param object|int $item Item id or item object.
193
+
*
194
+
* @return bool
195
+
*/
196
+
public function check_gift_item( bool $is_gift_item, $item ) {
197
+
return GiftCourse::is_gift_item( $item );
198
+
}
199
+
200
+
/**
201
+
* Refund restriction added for the WC order when item is gift
202
+
*
203
+
* @since 3.8.0
204
+
*
205
+
* @param WC_Order_Refund $refund Refund object.
206
+
* @param array $args Refund args.
207
+
*
208
+
* @return mixed
209
+
*/
210
+
public function woo_restrict_refund( $refund, $args ) {
211
+
if ( 'wc' === $this->get_monetize_by() ) {
212
+
return GiftCourse::get_gift_proceeder()->restrict_refund( $refund, $args );
213
+
}
214
+
}
215
+
216
+
/**
217
+
* Restrict refund if gift received
218
+
*
219
+
* @since 3.8.0
220
+
*
221
+
* @param object $order Order data.
222
+
*
223
+
* @throws \Exception If gift already received.
224
+
*
225
+
* @return mixed
226
+
*/
227
+
public function native_restrict_refund( $order ) {
228
+
return GiftCourse::get_gift_proceeder()->restrict_refund( $order );
229
+
}
230
+
231
+
/**
232
+
* Get monetize by
233
+
*
234
+
* @since 3.8.0
235
+
*
236
+
* @return mixed
237
+
*/
238
+
private function get_monetize_by() {
239
+
return tutor_utils()->get_option( 'monetize_by' );
240
+
}
241
+
242
+
/**
243
+
* Remove gift course meta keys from the thank you on
244
+
* frontend
245
+
*
246
+
* @since 3.8.0
247
+
*
248
+
* @param array $formatted_meta Array of meta.
249
+
* @param array $item Order item.
250
+
*
251
+
* @return array
252
+
*/
253
+
public function hide_tutor_gift_meta_on_thankyou( $formatted_meta, $item ) {
254
+
$meta_keys_to_hide = array(
255
+
'tutor_gift',
256
+
'tutor_gift_status',
257
+
'tutor_gift_sent_at',
258
+
'tutor_gift_received_at',
259
+
);
260
+
261
+
foreach ( $formatted_meta as $key => $meta ) {
262
+
if ( in_array( $meta->key, $meta_keys_to_hide ) ) {
263
+
if ( is_admin() ) {
264
+
if ( 'tutor_gift' === $meta->key ) {
265
+
unset( $formatted_meta[ $key ] );
266
+
} elseif ( in_array( $meta->key, array( GiftCourse::GIFT_SENT_AT_META, GiftCourse::GIFT_RECEIVED_AT_META ) ) ) {
267
+
$meta->display_key = ucfirst( str_replace( '_', ' ', $meta->key ) );
268
+
$meta->display_value = DateTimeHelper::get_gmt_to_user_timezone_date( $meta->value );
269
+
270
+
$formatted_meta[ $key ] = $meta;
271
+
} else {
272
+
$meta->display_key = ucfirst( str_replace( '_', ' ', $meta->key ) );
273
+
274
+
$formatted_meta[ $key ] = $meta;
275
+
}
276
+
} else {
277
+
unset( $formatted_meta[ $key ] );
278
+
}
279
+
}
280
+
}
281
+
282
+
return $formatted_meta;
283
+
}
284
+
285
+
/**
286
+
* Update the gift item meta to display on the admin side
287
+
*
288
+
* @since 3.8.0
289
+
*
290
+
* @param mixed $item_meta Order item meta.
291
+
*
292
+
* @return array
293
+
*/
294
+
public function filter_meta_value_for_display( $item_meta ) {
295
+
if ( tutor_utils()->count( $item_meta ) ) {
296
+
$hide_keys = array(
297
+
GiftCourse::GIFT_FLAG_META,
298
+
GiftCourse::GIFT_DATA_META,
299
+
);
300
+
301
+
$gift_time_array = array(
302
+
GiftCourse::GIFT_SENT_AT_META,
303
+
GiftCourse::GIFT_RECEIVED_AT_META,
304
+
);
305
+
306
+
foreach ( $item_meta as $key => $meta ) {
307
+
if ( in_array( $meta->meta_key, $hide_keys ) ) {
308
+
unset( $item_meta[ $key ] );
309
+
} else {
310
+
if ( in_array( $meta->meta_key, $gift_time_array ) ) {
311
+
if ( $meta->meta_value ) {
312
+
$meta->meta_value = DateTimeHelper::get_gmt_to_user_timezone_date( $meta->meta_value );
313
+
}
314
+
} else {
315
+
$meta->meta_value = trim( ucfirst( str_replace( '_', ' ', $meta->meta_value ) ) );
316
+
}
317
+
318
+
$update_key = trim( str_replace( array( '_', 'tutor' ), array( ' ', ' ' ), $meta->meta_key ) );
319
+
$meta->meta_key = ucfirst( $update_key );
320
+
321
+
// Update the value.
322
+
$item_meta[ $key ] = $meta;
323
+
}
324
+
}
325
+
}
326
+
327
+
$reset = array_values( $item_meta );
328
+
return $reset;
329
+
}
330
+
}
331
+