PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.1.6.9.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.1.6.9.1
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 / databases / class-lp-order-db.php

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

153 lines 3.2 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 $user_id
30 * @param int $course_id
31 *
32 * @return null|string
33 * @since 4.1.4
34 * @author tungnx
35 * @version 1.0.0
36 */
37 public function get_last_lp_order_id_of_user_course( int $user_id, int $course_id ) {
38 $user_id_str = $this->wpdb->prepare( '%"%d"%', $user_id );
39
40 $query = $this->wpdb->prepare(
41 "SELECT p.ID FROM {$this->tb_posts} as p
42 INNER join {$this->tb_postmeta} pm on p.ID = pm.post_id
43 INNER join {$this->tb_lp_order_items} as oi on p.ID = oi.order_id
44 INNER join {$this->tb_lp_order_itemmeta} as oim on oim.learnpress_order_item_id = oi.order_item_id
45 WHERE post_type = %s
46 AND pm.meta_key = %s
47 AND (pm.meta_value = %d OR pm.meta_value LIKE '%s')
48 AND oim.meta_key = %s
49 AND oim.meta_value = %d
50 ORDER BY p.ID DESC
51 LIMIT 1
52 ",
53 LP_ORDER_CPT,
54 '_user_id',
55 $user_id,
56 $user_id_str,
57 '_course_id',
58 $course_id
59 );
60
61 return $this->wpdb->get_var( $query );
62 }
63
64 /**
65 * Get order_item_ids by order_id
66 *
67 * @param int $order_id
68 * @author tungnx
69 * @since 4.1.4
70 * @version 1.0.0
71 * @return array
72 */
73 public function get_order_item_ids( int $order_id ): array {
74 $query = $this->wpdb->prepare(
75 "SELECT order_item_id FROM $this->tb_lp_order_items
76 WHERE order_id = %d
77 ",
78 $order_id
79 );
80
81 return $this->wpdb->get_col( $query );
82 }
83
84 /**
85 * Delete row IN order_item_ids
86 *
87 * @param LP_Order_Filter $filter
88 * @author tungnx
89 * @since 4.1.4
90 * @version 1.0.0
91 * @return bool|int
92 * @throws Exception
93 */
94 public function delete_order_item( LP_Order_Filter $filter ) {
95 // Check valid user.
96 if ( ! current_user_can( 'administrator' ) ) {
97 throw new Exception( __( 'User invalid!', 'learnpress' ) );
98 }
99
100 if ( empty( $filter->order_item_ids ) ) {
101 return 1;
102 }
103
104 $where = 'WHERE 1=1 ';
105
106 $where .= $this->wpdb->prepare(
107 'AND order_item_id IN(' . LP_Helper::db_format_array( $filter->order_item_ids, '%d' ) . ')',
108 $filter->order_item_ids
109 );
110
111 return $this->wpdb->query(
112 "DELETE FROM {$this->tb_lp_order_items}
113 {$where}
114 "
115 );
116 }
117
118 /**
119 * Delete row IN order_item_ids
120 *
121 * @param LP_Order_Filter $filter
122 * @author tungnx
123 * @since 4.1.4
124 * @version 1.0.0
125 * @return bool|int
126 * @throws Exception
127 */
128 public function delete_order_itemmeta( LP_Order_Filter $filter ) {
129 // Check valid user.
130 if ( ! current_user_can( 'administrator' ) ) {
131 throw new Exception( __( 'User invalid!', 'learnpress' ) );
132 }
133
134 if ( empty( $filter->order_item_ids ) ) {
135 return 1;
136 }
137
138 $where = 'WHERE 1=1 ';
139
140 $where .= $this->wpdb->prepare(
141 'AND learnpress_order_item_id IN(' . LP_Helper::db_format_array( $filter->order_item_ids, '%d' ) . ')',
142 $filter->order_item_ids
143 );
144
145 return $this->wpdb->query(
146 "DELETE FROM {$this->tb_lp_order_itemmeta}
147 {$where}
148 "
149 );
150 }
151 }
152
153