forked from gocodebox/lifterlms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.llms.query.user.postmeta.php
271 lines (207 loc) · 5.79 KB
/
class.llms.query.user.postmeta.php
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
defined( 'ABSPATH' ) || exit;
/**
* LifterLMS User Postmeta Query
* @since 3.15.0
* @version 3.15.0
*/
class LLMS_Query_User_Postmeta extends LLMS_Database_Query {
/**
* Identify the extending query
* @var string
*/
protected $id = 'user_postmeta';
/**
* Retrieve default arguments for a student query
* @return array
* @since 3.15.0
* @version 3.15.0
*/
protected function get_default_args() {
$args = array(
'include_post_children' => true,
'query' => array(),
'query_compare' => 'OR',
'post_id' => array(),
'sort' => array(
'updated_date' => 'DESC',
'meta_id' => 'ASC',
),
'types' => array(),
'user_id' => array(),
);
$args = wp_parse_args( $args, parent::get_default_args() );
return apply_filters( $this->get_filter( 'default_args' ), $args );
}
/**
* Retrieve an array of LLMS_User_Postmetas for the given set of results
* @return array
* @since 3.15.0
* @version 3.15.0
*/
public function get_metas() {
$metas = array();
$results = $this->get_results();
if ( $results ) {
foreach ( $results as $result ) {
$metas[] = new LLMS_User_Postmeta( $result->meta_id );
}
}
if ( $this->get( 'suppress_filters' ) ) {
return $metas;
}
return apply_filters( $this->get_filter( 'get_metas' ), $metas );
}
/**
* Parses data passed to $statuses
* Convert strings to array and ensure resulting array contains only valid statuses
* If no valid statuses, returns to the default
* @return void
* @since 3.15.0
* @version 3.15.0
*/
protected function parse_args() {
// sanitize post & user ids
foreach ( array( 'post_id', 'user_id' ) as $key ) {
$this->arguments[ $key ] = $this->sanitize_id_array( $this->arguments[ $key ] );
}
if ( $this->arguments['include_post_children'] ) {
foreach ( $this->arguments['post_id'] as $id ) {
if ( 'course' !== get_post_type( $id ) ) {
continue;
}
$course = llms_get_post( $id );
$this->arguments['post_id'] = array_merge(
$this->arguments['post_id'],
$this->sanitize_id_array( $course->get_sections( 'ids' ) ),
$this->sanitize_id_array( $course->get_lessons( 'ids' ) ),
$this->sanitize_id_array( $course->get_quizzes() )
);
}
}
if ( $this->arguments['types'] ) {
$all_events = array(
'completion' => array(
'key' => '_is_complete',
'value' => 'yes',
),
'status' => array(
'compare' => 'IS NOT NULL',
'key' => '_status',
),
'achievement' => array(
'compare' => 'IS NOT NULL',
'key' => '_achievement_earned',
),
'certificate' => array(
'compare' => 'IS NOT NULL',
'key' => '_certificate_earned',
),
'email' => array(
'compare' => 'IS NOT NULL',
'key' => '_email_sent',
),
'purchase' => array(
'compare' => 'LIKE',
'key' => '_enrollment_trigger',
'value' => 'order_%',
),
);
if ( is_string( $this->arguments['types'] ) && 'all' === $this->arguments['types'] ) {
$this->arguments['query'] = array_values( $all_events );
} else {
$this->arguments['query'] = array();
if ( ! is_array( $this->arguments['types'] ) ) {
$this->arguments['types'] = array( $this->arguments['types'] );
}
foreach ( $this->arguments['types'] as $type ) {
if ( ! isset( $all_events[ $type ] ) ) {
continue;
}
$this->arguments['query'][] = $all_events[ $type ];
}
}
}// End if().
if ( $this->arguments['query'] ) {
foreach ( $this->arguments['query'] as $i => &$query ) {
// ensure that each query has a compare operator
$query = wp_parse_args( $query, array(
'compare' => '=',
'key' => '',
'value' => '',
) );
$operators = array( '=', '!=', 'LIKE', 'IN', 'NOT IN', 'IS NOT NULL' );
if ( ! in_array( $query['compare'], $operators ) ) {
unset( $this->arguments['query'][ $i ] );
}
}
}
if ( ! in_array( $this->arguments['query_compare'], array( 'AND', 'OR' ) ) ) {
$this->arguments['query_compare'] = 'OR';
}
}
/**
* Prepare the SQL for the query
* @return void
* @since 3.15.0
* @version 3.15.0
*/
protected function preprare_query() {
global $wpdb;
$vars = array();
$vars[] = $this->get_skip();
$vars[] = $this->get( 'per_page' );
$sql = $wpdb->prepare(
"SELECT SQL_CALC_FOUND_ROWS meta_id
FROM {$wpdb->prefix}lifterlms_user_postmeta
{$this->sql_where()}
{$this->sql_orderby()}
LIMIT %d, %d;",
$vars
);
return $sql;
}
/**
* SQL "where" clause for the query
* @return string
* @since 3.15.0
* @version 3.15.0
*/
protected function sql_where() {
global $wpdb;
$sql = 'WHERE 1';
foreach ( array( 'post_id', 'user_id' ) as $key ) {
$ids = $this->get( $key );
if ( $ids ) {
$prepared = implode( ',', $ids );
$sql .= " AND {$key} IN ({$prepared})";
}
}
if ( $this->get( 'query' ) ) {
$sql .= ' AND ( ';
foreach ( $this->get( 'query' ) as $i => $query ) {
if ( 0 !== $i ) {
$sql .= " {$this->get( 'query_compare' )} ";
}
switch ( $query['compare'] ) {
case '=':
case '!=':
case 'LIKE':
$sql .= $wpdb->prepare( "( meta_key = %s AND meta_value {$query['compare']} %s )", $query['key'], $query['value'] );
break;
case 'IN';
case 'NOT IN';
$query['value'] = array_map( array( $this, 'escape_and_quote_string' ), $query['value'] );
$vals = implode( ',', $query['value'] );
$sql .= $wpdb->prepare( "( meta_key = %s AND meta_value {$query['compare']} ( {$vals} ) )", $query['key'] );
break;
case 'IS NOT NULL':
$sql .= $wpdb->prepare( "( meta_key = %s AND meta_value {$query['compare']} )", $query['key'] );
break;
}
}
$sql .= ' )';
}// End if().
return apply_filters( $this->get_filter( 'where' ), $sql, $this );
}
}