PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.3.9
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.3.9
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 4.2.1 All 138 releases
learnpress / inc / Databases / class-lp-order-db.php

class-lp-order-db.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.3.9, at inc/Databases/class-lp-order-db.php

170 lines 3.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_DB
4 *
5 * @author tungnx
6 * @since 4.1.4
7 */
8
9 defined( 'ABSPATH' ) || exit();
10
11 class LP_Order_DB extends LP_Database {
12 private static $_instance;
13
14 protected function __construct() {
15 parent::__construct();
16 }
17
18 public static function getInstance() {
19 if ( is_null( self::$_instance ) ) {
20 self::$_instance = new self();
21 }
22
23 return self::$_instance;
24 }
25
26 /**
27 * Get the latest LP Order id by user_id and course_id
28 *
29 * @param int|string $user_id LP_User is int, LP_User_Guest is string
30 * @param int $course_id
31 *
32 * @return null|string
33 * @since 4.1.4
34 * @author tungnx
35 * @version 1.0.2
36 */
37 public function get_last_lp_order_id_of_user_course( $user_id, int $course_id ) {
38 $key_cache = "lp/order/id/last/$user_id/$course_id";
39 $order_id = LP_Cache::cache_load_first( 'get', $key_cache );
40 if ( false !== $order_id ) {
41 return $order_id;
42 }
43
44 if ( ! $user_id || ! $course_id ) {
45 return null;
46 }
47
48 $user_id_str = $this->wpdb->prepare( '%"%d"%', $user_id );
49
50 $query = $this->wpdb->prepare(
51 "SELECT p.ID FROM {$this->tb_posts} as p
52 INNER join {$this->tb_postmeta} pm on p.ID = pm.post_id
53 INNER join {$this->tb_lp_order_items} as oi on p.ID = oi.order_id
54 WHERE post_type = %s
55 AND pm.meta_key = %s
56 AND (pm.meta_value = %s OR pm.meta_value LIKE '%s')
57 AND oi.item_id = %d
58 ORDER BY p.ID DESC
59 LIMIT 1
60 ",
61 LP_ORDER_CPT,
62 '_user_id',
63 $user_id,
64 $user_id_str,
65 $course_id
66 );
67
68 $order_id = $this->wpdb->get_var( $query );
69
70 LP_Cache::cache_load_first( 'set', $key_cache, $order_id );
71
72 return $order_id;
73 }
74
75 /**
76 * Get order_item_ids by order_id
77 *
78 * @param int $order_id
79 * @author tungnx
80 * @since 4.1.4
81 * @version 1.0.0
82 * @return array
83 */
84 public function get_order_item_ids( int $order_id ): array {
85 $query = $this->wpdb->prepare(
86 "SELECT order_item_id FROM $this->tb_lp_order_items
87 WHERE order_id = %d
88 ",
89 $order_id
90 );
91
92 return $this->wpdb->get_col( $query );
93 }
94
95 /**
96 * Delete row IN order_item_ids
97 *
98 * @param LP_Order_Filter $filter
99 * @author tungnx
100 * @since 4.1.4
101 * @version 1.0.0
102 * @return bool|int
103 * @throws Exception
104 */
105 public function delete_order_item( LP_Order_Filter $filter ) {
106 // Check valid user.
107 if ( ! current_user_can( 'administrator' ) ) {
108 throw new Exception( __( 'Invalid user!', 'learnpress' ) );
109 }
110
111 if ( empty( $filter->order_item_ids ) ) {
112 return 1;
113 }
114
115 $where = 'WHERE 1=1 ';
116
117 $where .= $this->wpdb->prepare(
118 'AND order_item_id IN(' . LP_Helper::db_format_array( $filter->order_item_ids, '%d' ) . ')',
119 $filter->order_item_ids
120 );
121
122 return $this->wpdb->query(
123 "DELETE FROM {$this->tb_lp_order_items}
124 {$where}
125 "
126 );
127 }
128
129 /**
130 * Delete row IN order_item_ids
131 *
132 * @param LP_Order_Filter $filter
133 * @author tungnx
134 * @since 4.1.4
135 * @version 1.0.0
136 * @return bool|int
137 * @throws Exception
138 */
139 public function delete_order_itemmeta( LP_Order_Filter $filter ) {
140 // Check valid user.
141 if ( ! current_user_can( 'administrator' ) ) {
142 throw new Exception( __( 'Invalid user!', 'learnpress' ) );
143 }
144
145 if ( empty( $filter->order_item_ids ) ) {
146 return 1;
147 }
148
149 $where = 'WHERE 1=1 ';
150
151 $where .= $this->wpdb->prepare(
152 'AND learnpress_order_item_id IN(' . LP_Helper::db_format_array( $filter->order_item_ids, '%d' ) . ')',
153 $filter->order_item_ids
154 );
155
156 return $this->wpdb->query(
157 "DELETE FROM {$this->tb_lp_order_itemmeta}
158 {$where}
159 "
160 );
161 }
162 public function get_items( LP_Filter $filter, int $order_id = 0 , int &$total_rows = 0 ) {
163 $filter->collection = $this->tb_lp_order_items;
164 $filter->collection_alias = 'oi';
165 $filter->where[] = $this->wpdb->prepare( 'AND oi.order_id=%d', $order_id );
166 $filter->field_count = 'oi.order_item_id';
167 return $this->execute( $filter, $total_rows );
168 }
169 }
170