PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.6.9.3
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.6.9.3
4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 All 139 releases
learnpress / inc / custom-post-types / order.php

order.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.1.6.9.3, at inc/custom-post-types/order.php

1,027 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 * @class LP_Order_Post_Type
4 *
5 * @author ThimPress
6 * @package LearnPress/Classes
7 * @version 1.0.1
8 */
9
10 if ( ! class_exists( 'LP_Order_Post_Type' ) ) {
11 final class LP_Order_Post_Type extends LP_Abstract_Post_Type {
12 /**
13 * Type of post
14 *
15 * @var string
16 */
17 protected $_post_type = LP_ORDER_CPT;
18 /**
19 * @var null
20 */
21 protected static $_instance = null;
22
23 /**
24 * LP_Order_Post_Type constructor.
25 *
26 * @param $post_type
27 */
28 public function __construct() {
29 add_action( 'init', array( $this, 'register_post_statues' ) );
30 add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
31 add_action( 'admin_init', array( $this, 'remove_box' ) );
32 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
33 // add_action( 'transition_post_status', array( $this, 'restore_order' ), 1, 3 );
34 add_filter( 'wp_untrash_post_status', array( $this, 'restore_status_order' ), 11, 3 );
35 // add_action( 'save_post', array( $this, 'recount_enrolled_users' ), 11, 3 );
36
37 add_filter( 'admin_footer', array( $this, 'admin_footer' ) );
38
39 // $this->add_map_method( 'save', 'save_order' );
40
41 // Hungkv => Fix error child order not show in trash
42 // add_filter( 'wp_count_posts', array( $this, 'filter_count_posts' ), 100, 3 );
43 add_filter( 'views_edit-lp_order', array( $this, 'filter_views' ) );
44 // add_filter( 'posts_where_paged', array( $this, 'filter_orders' ) );
45
46 parent::__construct();
47 }
48
49 /**
50 * Re-count enrolled users to the courses in current order
51 * is being changed status
52 *
53 * @param int $post_id
54 * @since 3.0.10
55 * @editor tungnx
56 * @reason not use
57 */
58 /*
59 public function recount_enrolled_users( int $post_id ) {
60 $order = learn_press_get_order( $post_id );
61 $curd = new LP_Course_CURD();
62 $items = $order->get_items();
63
64 if ( $items ) {
65 foreach ( $items as $item ) {
66 if ( ! isset( $item['course_id'] ) ) {
67 continue;
68 }
69
70 $course_id = $item['course_id'];
71 LP_Repair_Database::instance()->sync_course_orders( $course_id );
72 $count = $curd->count_enrolled_users_by_orders( $course_id );
73 update_post_meta( $course_id, 'count_enrolled_users', $count );
74 }
75 }
76 }*/
77
78 /**
79 * Filter the counts of posts when wp counting orders by statuses.
80 * Maybe there are some orders are created for multiple users,
81 * and each user in main order will be assigned to a separated
82 * order with post_parent is ID of main order. And, we do not
83 * want to show these orders in the list.
84 *
85 * @param array $counts
86 * @param string $type
87 * @param string $perm
88 *
89 * @return array|object
90 */
91 public function filter_count_posts( $counts, $type, $perm ) {
92 if ( LP_ORDER_CPT === $type ) {
93 $cache_key = 'lp-' . _count_posts_cache_key( $type, $perm );
94
95 $counts = LP_Object_Cache::get( $cache_key, 'counts' );
96
97 if ( false !== $counts ) {
98 return $counts;
99 }
100
101 global $wpdb;
102 $query = "
103 SELECT post_status, COUNT( ID ) AS num_posts
104 FROM {$wpdb->posts}
105 WHERE post_type = %s
106 AND post_parent = %d
107 ";
108
109 if ( 'readable' == $perm && is_user_logged_in() ) {
110 $post_type_object = get_post_type_object( $type );
111 if ( ! current_user_can( $post_type_object->cap->read_private_posts ) ) {
112 $query .= $wpdb->prepare(
113 " AND (post_status != 'private' OR ( post_author = %d AND post_status = 'private' ))",
114 get_current_user_id()
115 );
116 }
117 }
118 $query .= ' GROUP BY post_status';
119 $query = $wpdb->prepare( $query, $type, 0 );
120 $results = (array) $wpdb->get_results( $query, ARRAY_A );
121 $counts = array_fill_keys( get_post_stati(), 0 );
122
123 foreach ( $results as $row ) {
124 $counts[ $row['post_status'] ] = $row['num_posts'];
125 }
126
127 $counts = (object) $counts;
128 LP_Object_Cache::set( $cache_key, $counts, 'counts' );
129 }
130
131 return $counts;
132 }
133
134 /**
135 * Unset value in 'mine' key in views of LP Orders.
136 * The 'mine' is present in some case when 'user_posts_count'
137 * is not the same with total posts then wp add it to the views
138 * of WP Posts List table.
139 *
140 * @param array $views
141 *
142 * @return mixed
143 */
144 public function filter_views( $views ) {
145 if ( isset( $views['mine'] ) ) {
146 unset( $views['mine'] );
147 }
148
149 return $views;
150 }
151
152 /**
153 * Filter to hide orders are created by one order for multiple users.
154 *
155 * @param string $where
156 *
157 * @return string
158 */
159 public function filter_orders( string $where ): string {
160 if ( ! $this->is_page_list_posts_on_backend() ) {
161 return $where;
162 }
163
164 global $wpdb;
165
166 if ( isset( $_REQUEST['parent'] ) ) {
167 $where .= sprintf( ' AND post_parent = %d ', absint( $_REQUEST['parent'] ) );
168 } else {
169 // $where .= $wpdb->prepare( " AND (post_parent = 0 OR {$wpdb->posts}.ID IN( SELECT post_parent FROM {$wpdb->posts} X WHERE X.post_parent <> 0 AND X.post_type = %s) )", LP_ORDER_CPT );
170 $where .= ' AND post_parent = 0 ';
171 }
172
173 return $where;
174 }
175
176 public function enqueue_scripts() {
177 if ( get_post_type() != $this->_post_type ) {
178 return;
179 }
180 wp_enqueue_script( 'user-suggest' );
181 }
182
183 /**
184 * Restore user course item when the order is stored (usually from trash).
185 *
186 * @param string $new
187 * @param string $old
188 * @param WP_Post $post
189 *
190 * @editor tungnx
191 * @modify 4.1.3 - commnet - not use
192 */
193 /*
194 public function restore_order( $new, $old, $post ) {
195
196 if ( ! ( 'trash' === $old ) ) {
197 return;
198 }
199
200 $order = learn_press_get_order( $post->ID );
201 if ( ! $order ) {
202 return;
203 }
204
205 $user_item_data = get_post_meta( $post->ID, '_lp_user_data', true );
206 if ( ! $user_item_data ) {
207 return;
208 }
209
210 $items = $order->get_items();
211 if ( ! $items ) {
212 return;
213 }
214
215 $users = $order->get_users();
216 if ( ! $users ) {
217 return;
218 }
219
220 // Restore child order if current order is for multi users
221 $child_orders = $order->get_child_orders();
222 if ( $order->is_multi_users() && $child_orders ) {
223 foreach ( $child_orders as $child_order ) {
224 wp_untrash_post( $child_order );
225 }
226 }
227
228 $user_curd = new LP_User_CURD();
229
230 foreach ( $user_item_data as $user_item_id => $data ) {
231 $item_course = $user_curd->get_user_item_by_id( $user_item_id );
232
233 if ( ! $item_course ) {
234 continue;
235 }
236
237 $order_status = $order->get_order_status();
238 $last_status = ( $order_status != '' && $order_status != 'completed' ) ? 'pending' : 'in-progress';
239 $user_curd->update_user_item_status( $user_item_id, $last_status );
240 // Restore data
241 $user_curd->update_user_item_by_id(
242 $user_item_id,
243 $data
244 );
245 }
246
247 // Delete data
248 delete_post_meta( $post->ID, '_lp_user_data' );
249 }*/
250
251 /**
252 * Restore user course item when the order is stored (usually from trash).
253 *
254 * @param string $new_status
255 * @param int $post_id
256 * @param string $previous_status
257 * @return string
258 */
259 public function restore_status_order( string $new_status, int $post_id, string $previous_status ): string {
260 if ( LP_ORDER_CPT != get_post_type( $post_id ) ) {
261 return $new_status;
262 }
263
264 return $previous_status;
265 }
266
267 /**
268 * @param LP_Order $order
269 * @param array $user_ids
270 * @param bool $trigger_action
271 *
272 * @throws Exception
273 * @editor tungnx
274 * @reason comment - not use
275 */
276 /*
277 protected function _update_child( $order, $user_ids, $trigger_action = false ) {
278 $new_orders = array();
279 $child_orders = $order->get_child_orders( true );
280
281 if ( $child_orders ) {
282 foreach ( $child_orders as $child_id ) {
283 $child_order = learn_press_get_order( $child_id );
284 $child_order_user_id = $child_order->get_user( 'id' );
285
286 if ( ! in_array( $child_order_user_id, $user_ids ) ) {
287 wp_delete_post( $child_id );
288 continue;
289 }
290
291 $order->cln_items( $child_order->get_id() );
292 $new_orders[ $child_order_user_id ] = $child_order;
293 }
294 }
295
296 foreach ( $user_ids as $uid ) {
297 if ( empty( $new_orders[ $uid ] ) ) {
298 $new_order = $order->cln();
299 $new_orders[ $uid ] = $new_order;
300 } else {
301 $new_order = $new_orders[ $uid ];
302 }
303
304 $old_status = get_post_status( $new_order->get_id() );
305 $new_order->set_order_date( $order->get_order_date( 'edit' ) );
306 $new_order->set_parent_id( $order->get_id() );
307 $new_order->set_user_id( $uid );
308 $new_order->set_total( $order->get_total() );
309 $new_order->set_subtotal( $order->get_subtotal() );
310
311 $new_order->set_status( learn_press_get_request( 'order-status' ) );
312 $new_order->save();
313 $new_status = get_post_status( $new_order->get_id() );
314
315 if ( ( $new_status == $old_status ) && $trigger_action ) {
316 $status = str_replace( 'lp-', '', $new_status );
317 $old_status = str_replace( 'lp-', '', $new_status );
318 do_action( 'learn-press/order/status-' . $status, $new_order->get_id(), $status );
319 do_action( 'learn-press/order/status-' . $old_status . '-to-' . $status, $new_order->get_id() );
320 do_action( 'learn-press/order/status-changed', $new_order->get_id(), $status, $old_status );
321 }
322 }
323 }*/
324
325 /**
326 * Save order post.
327 *
328 * @param int $post_id
329 * @param WP_Post $post
330 * @throws Exception
331 * @editor tungnx
332 * @version 1.0.1
333 */
334 public function save( int $post_id, WP_Post $post ) {
335 global $action;
336
337 if ( wp_is_post_revision( $post_id ) ) {
338 return;
339 }
340
341 if ( $action == 'editpost' ) {
342 $order = learn_press_get_order( $post_id );
343 if ( ! $order ) {
344 return;
345 }
346
347 $created_via = $order->get_created_via();
348 if ( empty( $created_via ) ) {
349 $order->set_created_via( 'manual' );
350 }
351
352 if ( isset( $_POST['order-customer'] ) ) {
353 $user_id = LP_Helper::sanitize_params_submitted( $_POST['order-customer'] );
354 $order->set_user_id( $user_id );
355 }
356
357 $order->set_status( learn_press_get_request( 'order-status' ) );
358 $order->save();
359 }
360 }
361
362 /**
363 * Remove unused boxes
364 */
365 public function remove_box() {
366 remove_post_type_support( LP_ORDER_CPT, 'title' );
367 remove_post_type_support( LP_ORDER_CPT, 'editor' );
368 }
369
370 public function admin_footer() {
371 if ( ! $this->is_page_list_posts_on_backend() ) {
372 return;
373 }
374 ?>
375 <script type="text/javascript">
376 jQuery(function ($) {
377 $('#post-search-input').prop('placeholder',
378 '<?php esc_attr_e( 'Order number, course name etc...', 'learnpress' ); ?>').css('width', 300)
379 })
380 </script>
381 <?php
382 }
383
384 /**
385 * Hook to filter LP orders by some conditions.
386 *
387 * @param string $where
388 *
389 * @return mixed
390 */
391 public function posts_where_paged( $where ) {
392 global $wpdb, $wp_query;
393 if ( is_admin() && $this->is_page_list_posts_on_backend() &&
394 ( ! isset( $wp_query->query['post_status'] ) || ! $wp_query->query['post_status'] ) ) {
395 $statuses = array_keys( learn_press_get_register_order_statuses() );
396 $search = "{$wpdb->posts}.post_status = 'publish' ";
397 $tmps = array( $search );
398 $tmp = "{$wpdb->posts}.post_status = %s ";
399 foreach ( $statuses as $status ) {
400 $tmps[] = $wpdb->prepare( $tmp, $status );
401 }
402 $replace = implode( ' OR ', $tmps );
403 $where = str_replace( $search, $replace, $where );
404 }
405
406 if ( ! $this->is_page_list_posts_on_backend() || ! $this->_is_search() ) {
407 return $where;
408 }
409
410 // filter by user id
411 preg_match( "#{$wpdb->posts}\.post_author IN\s*\((\d+)\)#", $where, $matches );
412 if ( ! empty( $matches ) && isset( $matches[1] ) ) {
413 $author_id = intval( $matches[1] );
414 $author_id_str = $wpdb->prepare( '%"%d"%', $author_id );
415 // $sql = ' ( pm1.meta_value = %d OR pm1.meta_value LIKE %s)';
416
417 $sql = " {$wpdb->posts}.ID IN ( SELECT
418 IF( p.post_parent >0, p.post_parent, p.ID)
419 FROM
420 {$wpdb->posts} AS p
421 INNER JOIN
422 {$wpdb->postmeta} m ON p.ID = m.post_id and p.post_type = %s
423 AND m.meta_key = %s AND (meta_value = %d OR meta_value like %s )
424 )
425 ";
426
427 $sql = $wpdb->prepare( $sql, array( LP_ORDER_CPT, '_user_id', $author_id, $author_id_str ) );
428 $where = str_replace( $matches[0], $sql, $where );
429 }
430
431 $s = $wp_query->get( 's' );
432
433 if ( $s ) {
434 $s = '%' . $wpdb->esc_like( $s ) . '%';
435 preg_match( "#{$wpdb->posts}\.post_title LIKE#", $where, $matches2 );
436 $sql = " {$wpdb->posts}.ID IN (
437 SELECT
438 IF( p.post_parent >0, p.post_parent, p.ID)
439 FROM
440 {$wpdb->posts} AS p
441 INNER JOIN
442 {$wpdb->postmeta} m ON p.ID = m.post_id and p.post_type = %s
443 AND m.meta_key = %s
444 INNER JOIN
445 {$wpdb->users} u on m.meta_value = u.ID
446 WHERE
447 u.user_login LIKE %s
448 OR u.user_nicename LIKE %s
449 OR u.user_email LIKE %s
450 OR u.display_name LIKE %s
451 OR {$wpdb->posts}.ID LIKE %s
452 ) ";
453 $sql = $wpdb->prepare( $sql, array( LP_ORDER_CPT, '_user_id', $s, $s, $s, $s, $s ) );
454 // print_r($sql);die('ccc');
455 // search order via course name
456 $sql .= ' OR ' . $wpdb->prepare(
457 " {$wpdb->posts}.ID IN (
458 SELECT DISTINCT order_id FROM {$wpdb->learnpress_order_items} loi
459 INNER JOIN {$wpdb->learnpress_order_itemmeta} loim ON loi.order_item_id = loim.learnpress_order_item_id AND loim.meta_key LIKE %s
460 WHERE `order_item_name` LIKE %s OR loim.meta_value LIKE %s
461 )",
462 array( '_course_id', $s, $s )
463 );
464 if ( ! empty( $matches2 ) && isset( $matches2[0] ) ) {
465 $sql .= $wpdb->prepare( ' OR loi.order_item_name LIKE %s', $s );
466 $where = str_replace( $matches2[0], $sql . ' OR ' . $matches2[0], $where );
467 } else {
468 $where .= ' AND ' . $sql;
469 }
470 }
471
472 return $where;
473 }
474
475 public function posts_fields( $fields ) {
476 global $wp_query;
477
478 if ( ! $this->is_page_list_posts_on_backend() || ! $this->_is_search() ) {
479 return $fields;
480 }
481 $fields .= ', uu.ID as user_ID, uu.display_name as user_display_name';
482
483 return $fields;
484 }
485
486 public function posts_orderby( $orderby ) {
487 global $wpdb;
488
489 if ( ! $this->is_page_list_posts_on_backend() ) {
490 return $orderby;
491 }
492 global $wpdb;
493
494 $order = $this->get_order_sort();
495
496 switch ( $this->get_order_by() ) {
497 case 'title':
498 $orderby = "{$wpdb->posts}.ID {$order}";
499 break;
500 case 'student':
501 $orderby = "uu.user_login {$order}";
502 break;
503 case 'date':
504 $orderby = "{$wpdb->posts}.post_date {$order}";
505 break;
506 case 'order_total':
507 $orderby = " pm2.meta_value {$order}";
508 break;
509 }
510
511 return $orderby;
512 }
513
514 public function posts_join_paged( $join ) {
515 global $wpdb, $wp_query;
516 if ( ! $this->is_page_list_posts_on_backend() ) {
517 return $join;
518 }
519
520 $s = $wp_query->get( 's' );
521 if ( $s ) {
522 $join .= " INNER JOIN {$wpdb->learnpress_order_items} loi ON {$wpdb->posts}.ID = loi.order_id";
523 }
524
525 if ( isset( $_REQUEST['author'] ) ) {
526 $join .= " INNER JOIN {$wpdb->postmeta} pm1 ON {$wpdb->posts}.ID = pm1.post_id AND pm1.meta_key = '_user_id'";
527 $join .= " INNER JOIN {$wpdb->postmeta} pm2 ON {$wpdb->posts}.ID = pm2.post_id AND pm2.meta_key = '_order_total'";
528 $join .= " LEFT JOIN {$wpdb->users} uu ON pm1.meta_value = uu.ID";
529 }
530
531 return $join;
532 }
533
534 /**
535 * Make our custom columns can be sortable
536 *
537 * @param $columns
538 *
539 * @return mixed
540 */
541 public function sortable_columns( $columns ) {
542 $columns['order_student'] = 'student';
543 $columns['order_date'] = 'date';
544 $columns['order_total'] = 'order_total';
545
546 return $columns;
547 }
548
549 public function update_status() {
550 $order_id = ! empty( $_REQUEST['order_id'] ) ? absint( $_REQUEST['order_id'] ) : 0;
551 $status = ! empty( $_REQUEST['status'] ) ? LP_Helper::sanitize_params_submitted( $_REQUEST['status'] ) : 'Pending';
552
553 learn_press_update_order_status( $order_id, $status );
554
555 wp_send_json(
556 array(
557 'status' => $status,
558 'class' => sanitize_title( $status ),
559 )
560 );
561 }
562
563 /**
564 * Custom row's actions.
565 *
566 * @param array $actions
567 * @param WP_Post $post
568 *
569 * @return mixed
570 * @since 2.1.7
571 */
572 public function row_actions( $actions, $post ) {
573 if ( ! empty( $actions['inline hide-if-no-js'] ) ) {
574 unset( $actions['inline hide-if-no-js'] );
575 }
576 if ( ! empty( $actions['edit'] ) ) {
577 $actions['edit'] = preg_replace( '/>(.*?)<\/a>/', '>' . __( 'View Order', 'learnpress' ) . '</a>', $actions['edit'] );
578 }
579
580 $order = learn_press_get_order( $post->ID );
581 if ( $order->is_multi_users() ) {
582 $actions['child-orders'] = sprintf(
583 '<a href="%s">%s</a>',
584 esc_url_raw(
585 add_query_arg(
586 array(
587 'post_type' => LP_ORDER_CPT,
588 'parent' => $post->ID,
589 ),
590 admin_url( 'edit.php' )
591 )
592 ),
593 __( 'View child orders', 'learnpress' )
594 );
595 }
596
597 return $actions;
598 }
599
600 /**
601 * re-order the orders by newest
602 *
603 * @param $wp_query
604 * @editor tungnx
605 * @reason comment this function - because default sort by id
606 *
607 * @return mixed
608 */
609 public function pre_get_posts( $wp_query ) {
610 if ( is_admin() && isset( $wp_query->query['post_type'] ) && LP_ORDER_CPT == $wp_query->query['post_type'] ) {
611 $wp_query->set( 'orderby', 'date' );
612 $wp_query->set( 'order', 'desc' );
613 }
614
615 return $wp_query;
616 }
617
618 /**
619 *
620 */
621 public function columns_head( $existing ) {
622
623 // Remove Checkbox - adding it back below
624 if ( isset( $existing['cb'] ) ) {
625 $check = $existing['cb'];
626 unset( $existing['cb'] );
627 }
628
629 // Remove Title - adding it back below
630 if ( isset( $existing['title'] ) ) {
631 unset( $existing['title'] );
632 }
633
634 // Remove Format
635 if ( isset( $existing['format'] ) ) {
636 unset( $existing['format'] );
637 }
638
639 // Remove Author
640 if ( isset( $existing['author'] ) ) {
641 unset( $existing['author'] );
642 }
643
644 // Remove Comments
645 if ( isset( $existing['comments'] ) ) {
646 unset( $existing['comments'] );
647 }
648
649 // Remove Date
650 if ( isset( $existing['date'] ) ) {
651 unset( $existing['date'] );
652 }
653
654 // Remove Builder
655 if ( isset( $existing['builder_layout'] ) ) {
656 unset( $existing['builder_layout'] );
657 }
658
659 add_filter( 'the_title', array( $this, 'order_title' ), 5, 2 );
660
661 $columns['cb'] = '<input type="checkbox" />';
662 $columns['title'] = esc_html__( 'Order', 'learnpress' );
663 $columns['order_student'] = esc_html__( 'Student', 'learnpress' );
664 $columns['order_items'] = esc_html__( 'Purchased', 'learnpress' );
665 $columns['order_date'] = esc_html__( 'Date', 'learnpress' );
666 $columns['order_total'] = esc_html__( 'Total', 'learnpress' );
667 $columns['order_status'] = '<span class="status_head tips" data-tip="' . esc_attr__( 'Status', 'learnpress' ) . '">' . esc_attr__( 'Status', 'learnpress' ) . '</span>';
668
669 $columns = array_merge( $columns, $existing );
670
671 return $columns;
672 }
673
674 public function order_title( $title, $post_id ) {
675 $order = learn_press_get_order( $post_id );
676
677 if ( $order ) {
678 $title = $order->get_order_number();
679 }
680
681 return $title;
682 }
683
684 /**
685 * Render column data
686 *
687 * @param string
688 * @param int
689 */
690 public function columns_content( $column, $post_id = 0 ) {
691 global $post;
692 $the_order = learn_press_get_order( $post->ID );
693
694 switch ( $column ) {
695 case 'order_student':
696 $user_ids = $the_order->get_users();
697 if ( $user_ids ) {
698 $outputs = array();
699 foreach ( $user_ids as $user_id ) {
700 if ( get_user_by( 'id', $user_id ) ) {
701 $user = learn_press_get_user( $user_id );
702 $outputs[] = sprintf(
703 '<a href="user-edit.php?user_id=%d">%s (%s)</a><span>%s</span>',
704 $user_id,
705 $user->get_data( 'user_login' ),
706 $user->get_data( 'display_name' ),
707 $user->get_data( 'user_email' )
708 );
709 } else {
710 if ( sizeof( $user_ids ) == 1 ) {
711 $outputs[] = $the_order->get_customer_name();
712 }
713 }
714 }
715 echo join( ', ', $outputs );
716 } else {
717 echo esc_html__( '(Guest)', 'learnpress' );
718 }
719 break;
720 case 'order_status':
721 $icon = '';
722 switch ( $the_order->get_status() ) {
723 case 'pending':
724 $icon = '<i class="fas fa-flag"></i>';
725 break;
726 case 'processing':
727 $icon = '<i class="far fa-clock"></i>';
728 break;
729 case 'completed':
730 $icon = '<i class="far fa-check-circle"></i>';
731 break;
732 case 'failed':
733 $icon = '<i class="far fa-times-circle"></i>';
734 break;
735 case 'cancelled':
736 $icon = '<i class="fas fa-ban"></i>';
737 break;
738 }
739
740 $icon = apply_filters( 'learn-press/order-status-icon', $icon, $the_order->get_status() );
741 $label = learn_press_get_order_status_label( $the_order->get_id() );
742 echo sprintf(
743 '<span class="learn-press-tooltip %s" data-tooltip="%s">%s %s</span>',
744 $the_order->get_status(),
745 $label,
746 $icon,
747 $label
748 );
749 break;
750 case 'order_date':
751 $t_time = get_the_time( 'Y/m/d g:i:s a' );
752 $m_time = $post->post_date;
753 $time = get_post_time( 'G', true, $post );
754
755 $time_diff = current_time( 'timestamp' ) - $time;
756
757 if ( $time_diff > 0 && $time_diff < DAY_IN_SECONDS ) {
758 $h_time = sprintf( __( '%s ago', 'learnpress' ), human_time_diff( $time ) );
759 } else {
760 $h_time = mysql2date( 'Y/m/d', $m_time );
761 }
762
763 echo '<abbr title="' . esc_attr( $t_time ) . '">' . esc_html( apply_filters( 'learn_press_order_column_time', $h_time, $the_order ) ) . '</abbr>';
764
765 break;
766 case 'order_items':
767 $links = array();
768 $items = $the_order->get_items();
769 $count = sizeof( $items );
770
771 foreach ( $items as $item ) {
772 if ( empty( $item['course_id'] ) || get_post_type( $item['course_id'] ) !== LP_COURSE_CPT ) {
773 $links[] = apply_filters( 'learn-press/order-item-not-course-id', esc_html__( 'Course does not exist', 'learnpress' ), $item );
774 } elseif ( get_post_status( $item['course_id'] ) !== 'publish' ) {
775 $links[] = get_the_title( $item['course_id'] ) . sprintf( ' (#%d - %s)', $item['course_id'], esc_html__( 'Deleted', 'learnpress' ) );
776 } else {
777 $link = '<a href="' . get_the_permalink( $item['course_id'] ) . '">' . get_the_title( $item['course_id'] ) . ' (#' . $item['course_id'] . ')' . '</a>';
778 if ( $count > 1 ) {
779 $link = sprintf( '<li>%s</li>', $link );
780 }
781 $links[] = apply_filters( 'learn-press/order-item-link', $link, $item );
782
783 }
784 }
785
786 if ( $count > 1 ) {
787 echo sprintf( '<ol>%s</ol>', join( '', $links ) );
788 } elseif ( 1 == $count ) {
789 echo join( '', $links );
790 } else {
791 echo esc_html__( '(No item)', 'learnpress' );
792 }
793 break;
794 case 'order_total':
795 echo wp_kses_post( $the_order->get_formatted_order_total() );
796 $title = $the_order->get_payment_method_title();
797
798 if ( $title ) {
799 ?>
800 <div class="payment-method-title">
801 <?php echo wp_kses_post( $the_order->order_total == 0 ? $title : sprintf( __( 'Pay via <strong>%s</strong>', 'learnpress' ), apply_filters( 'learn-press/order-payment-method-title', $title, $the_order ), $the_order ) ); ?>
802 </div>
803 <?php
804 }
805 break;
806 }
807 }
808
809 private function _is_search() {
810 return is_search();
811 }
812
813 /**
814 * Register order post type
815 */
816 public function args_register_post_type(): array {
817 return array(
818 'labels' => array(
819 'name' => __( 'Orders', 'learnpress' ),
820 'menu_name' => __( 'Orders', 'learnpress' ),
821 'singular_name' => __( 'Order', 'learnpress' ),
822 'add_new_item' => __( 'Add New Order', 'learnpress' ),
823 'edit_item' => __( 'Order Details', 'learnpress' ),
824 'all_items' => __( 'Orders', 'learnpress' ),
825 'view_item' => __( 'View Order', 'learnpress' ),
826 'add_new' => __( 'Add New', 'learnpress' ),
827 'update_item' => __( 'Update Order', 'learnpress' ),
828 'search_items' => __( 'Search Orders', 'learnpress' ),
829 'not_found' => __( 'No order found', 'learnpress' ),
830 'not_found_in_trash' => __( 'No order found in Trash', 'learnpress' ),
831 ),
832 'public' => false,
833 'show_ui' => true,
834 'show_in_nav_menus' => false,
835 'show_in_admin_bar' => false,
836 'publicly_queryable' => false,
837 'show_in_menu' => 'learn_press',
838 'map_meta_cap' => true,
839 'capability_type' => LP_ORDER_CPT,
840 'hierarchical' => true,
841 'rewrite' => array(
842 'slug' => LP_ORDER_CPT,
843 'hierarchical' => true,
844 'with_front' => true,
845 ),
846 'supports' => array(
847 'title',
848 'custom-fields',
849 ),
850 );
851 }
852
853 /**
854 * Remove some unwanted metaboxes
855 */
856 public static function register_metabox() {
857 // Remove Publish metabox
858 remove_meta_box( 'submitdiv', LP_ORDER_CPT, 'side' );
859 remove_meta_box( 'commentstatusdiv', LP_ORDER_CPT, 'normal' );
860 }
861
862 // /**
863 // * Order details view.
864 // *
865 // * @param WP_Post $post
866 // */
867 // public static function order_details( $post ) {
868 // learn_press_admin_view( 'meta-boxes/order/details.php', array( 'order' => new LP_Order( $post ) ) );
869 // }
870
871 // /**
872 // * Order actions view.
873 // *
874 // * @param WP_Post $post
875 // */
876 // public static function order_actions( $post ) {
877 // learn_press_admin_view( 'meta-boxes/order/actions.php', array( 'order' => new LP_Order( $post ) ) );
878 // }
879
880 public function preparing_to_trash_order( $post_id ) {
881 if ( LP_ORDER_CPT != learn_press_get_post_type( $post_id ) ) {
882 return;
883 }
884 }
885
886 /**
887 * Register new post status for order
888 */
889 public function register_post_statues() {
890 $statuses = learn_press_get_register_order_statuses();
891 foreach ( $statuses as $status => $args ) {
892 register_post_status( $status, $args );
893 }
894 }
895
896 public static function instance() {
897 if ( ! self::$_instance ) {
898 self::$_instance = new self();
899 }
900
901 return self::$_instance;
902 }
903
904 /**
905 * Order export view.
906 *
907 * @param WP_Post $post
908 *
909 * @throws Exception
910 * @since 3.2.7.8
911 *
912 * @author hungkv
913 */
914 // public static function order_exports( $post ) {
915 // learn_press_admin_view( 'meta-boxes/order/exports-invoice.php', array( 'order' => new LP_Order( $post ) ) );
916 // }
917
918 /**
919 * Before post deleted
920 *
921 * @author tungnx
922 * @since 4.1.4
923 * @version 1.0.0
924 */
925 public function before_delete( int $order_id ) {
926 $lp_order_db = LP_Order_DB::getInstance();
927 $lp_user_items_db = LP_User_Items_DB::getInstance();
928
929 try {
930 $order = learn_press_get_order( $order_id );
931
932 if ( ! $order ) {
933 return;
934 }
935
936 $order_item_ids = $lp_order_db->get_order_item_ids( $order_id );
937
938 if ( empty( $order_item_ids ) ) {
939 return;
940 }
941
942 $user_ids = $order->get_users();
943
944 foreach ( $user_ids as $user_id ) {
945 delete_user_meta( $user_id, 'orders' );
946
947 foreach ( $order->get_item_ids() as $course_id ) {
948 // Check this order is the latest by user and course_id
949 $last_order_id = $lp_order_db->get_last_lp_order_id_of_user_course( $user_id, $course_id );
950 if ( $last_order_id && $last_order_id != $order->get_id() ) {
951 continue;
952 }
953
954 $lp_user_items_db->delete_user_items_old( $user_id, $course_id );
955 }
956 }
957
958 // Delete lp_order_item, lp_order_itemmeta
959 $filter_delete = new LP_Order_Filter();
960 $filter_delete->order_item_ids = $order_item_ids;
961 $lp_order_db->delete_order_item( $filter_delete );
962 $lp_order_db->delete_order_itemmeta( $filter_delete );
963 // End
964
965 // $this->remove_user_items_by_order_id( $order_id );
966
967 if ( ! empty( $data['child'] ) ) {
968 // $this->remove_child_orders( $data['child'] );
969 }
970 } catch ( Throwable $e ) {
971 error_log( $e->getMessage() . '>' . __FILE__ );
972 }
973 }
974
975 /**
976 * Action delete Order
977 *
978 * @param int $order_id
979 * @author tungnx
980 * @since 4.1.4
981 * @version 1.0.0
982 */
983 public function deleted_post( int $order_id ) {
984
985 }
986
987 public function meta_boxes() {
988 return array(
989 'order_details' => array(
990 'title' => esc_html__( 'Order Details', 'learnpress' ),
991 'callback' => function( $post ) {
992 learn_press_admin_view( 'meta-boxes/order/details.php', array( 'order' => new LP_Order( $post ) ) );
993 },
994 'context' => 'normal',
995 'priority' => 'high',
996 ),
997 'submitdiv' => array(
998 'title' => esc_html__( 'Order Actions', 'learnpress' ),
999 'callback' => function( $post ) {
1000 learn_press_admin_view( 'meta-boxes/order/actions.php', array( 'order' => new LP_Order( $post ) ) );
1001 },
1002 'context' => 'side',
1003 'priority' => 'high',
1004 ),
1005 'order_exports' => array(
1006 'title' => esc_html__( 'Order Exports', 'learnpress' ),
1007 'callback' => function( $post ) {
1008 learn_press_admin_view( 'meta-boxes/order/exports-invoice.php', array( 'order' => new LP_Order( $post ) ) );
1009 },
1010 'context' => 'side',
1011 'priority' => 'high',
1012 ),
1013 );
1014 }
1015 }
1016
1017 // end LP_Order_Post_Type
1018
1019 $order_post_type = LP_Order_Post_Type::instance();
1020
1021 // Todo: Nhamdv see to rewrite
1022 // $order_post_type
1023 // ->add_meta_box( 'order_details', esc_html__( 'Order Details', 'learnpress' ), 'order_details', 'normal', 'high' )
1024 // ->add_meta_box( 'submitdiv', esc_html__( 'Order Actions', 'learnpress' ), 'order_actions', 'side', 'high' )
1025 // ->add_meta_box( 'order_export', esc_html__( 'Order Exports', 'learnpress' ), 'order_exports', 'side', 'high' );
1026 }
1027