forked from gocodebox/lifterlms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.llms.query.php
302 lines (234 loc) · 7.34 KB
/
class.llms.query.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
/**
* Query base class
* Handles queries and endpoints.
*
* @since 1.0.0
* @version 3.33.0
*/
defined( 'ABSPATH' ) || exit;
/**
* LLMS_Query class
*
* @since 1.0.0
* @since 3.31.0 Deprecated `add_query_vars() method and added sanitizing functions when accessing `$_GET` vars.
* @since 3.33.0 Added catalog secondary sorting by `post_title` when the primary sort is `menu_order`.
*/
class LLMS_Query {
/**
* Query var
*
* @var array
*/
public $query_vars = array();
/**
* Constructor
*
* @since 1.0.0
* @version 3.28.2
*
* @return void
*/
public function __construct() {
add_action( 'init', array( $this, 'add_endpoints' ) );
if ( ! is_admin() ) {
add_filter( 'query_vars', array( $this, 'set_query_vars' ), 0 );
add_action( 'parse_request', array( $this, 'parse_request' ), 0 );
}
$this->init_query_vars();
add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
}
/**
* Add Query Endpoints
*
* @since 1.0.0
* @since 3.28.2 Handle dashboard tab pagination via a rewrite rule.
*
* @return void
*/
public function add_endpoints() {
foreach ( $this->get_query_vars() as $key => $var ) {
add_rewrite_endpoint( $var, EP_PAGES );
}
global $wp_rewrite;
foreach ( LLMS_Student_Dashboard::get_tabs() as $id => $tab ) {
if ( ! empty( $tab['paginate'] ) ) {
$regex = sprintf( '(.?.+?)/%1$s/%2$s/?([0-9]{1,})/?$', $tab['endpoint'], $wp_rewrite->pagination_base );
$redirect = sprintf( 'index.php?pagename=$matches[1]&%s=$matches[3]&paged=$matches[2]', $tab['endpoint'] );
add_rewrite_rule( $regex, $redirect, 'top' );
}
}
}
/**
* Get query variables
*
* @since Unknown
*
* @return array
*/
public function get_query_vars() {
return apply_filters( 'llms_get_endpoints', $this->query_vars );
}
/**
* Get a taxonomy query that filters out courses & memberships based on catalog / search visibility settings
*
* @since 3.6.0
*
* @param array $query Existing taxonomy query from the global $wp_query.
* @return array
*/
private function get_tax_query( $query = array() ) {
if ( ! is_array( $query ) ) {
$query = array(
'relation' => 'AND',
);
}
$terms = wp_list_pluck(
get_terms( array(
'taxonomy' => 'llms_product_visibility',
'hide_empty' => false,
) ),
'term_taxonomy_id',
'name'
);
$not_in = ( is_search() ) ? array( $terms['hidden'], $terms['catalog'] ) : array( $terms['hidden'], $terms['search'] );
$query[] = array(
'field' => 'term_taxonomy_id',
'operator' => 'NOT IN',
'taxonomy' => 'llms_product_visibility',
'terms' => $not_in,
);
return $query;
}
/**
* Init queries
*
* @since Unknown
*
* @return void
*/
public function init_query_vars() {
$this->query_vars = array(
'confirm-payment' => get_option( 'lifterlms_myaccount_confirm_payment_endpoint', 'confirm-payment' ),
'lost-password' => get_option( 'lifterlms_myaccount_lost_password_endpoint', 'lost-password' ),
);
}
/**
* Parse the request for query variables
*
* @since unknown
* @since 3.31.0 sanitize and unslash `$_GET` vars.
*
* @return void
*/
public function parse_request() {
global $wp;
foreach ( $this->get_query_vars() as $key => $var ) {
if ( isset( $_GET[ $var ] ) ) {
$wp->query_vars[ $key ] = sanitize_text_field( wp_unslash( $_GET[ $var ] ) );
} elseif ( isset( $wp->query_vars[ $var ] ) ) {
$wp->query_vars[ $key ] = $wp->query_vars[ $var ];
}
}
}
/**
* Sets the WP_Query variables for "post_type" on LifterLMS custom taxonomy archive pages for Courses and Memberships
*
* @since 1.4.4 Moved from LLMS_Post_Types.
* @since 3.16.8
* @since 3.33.0 Added `post_title` as a secondary sort when the primary sort is `menu_order`
*
* @param WP_Query $query Main WP_Query Object.
* @return void
*/
public function pre_get_posts( $query ) {
$modify_tax_query = false;
if ( ! is_admin() && $query->is_main_query() ) {
if ( is_search() ) {
$modify_tax_query = true;
}
if ( is_tax( array( 'course_cat', 'course_tag', 'course_difficulty', 'course_track', 'membership_tag', 'membership_cat' ) ) ) {
$query->set( 'post_type', array( 'course', 'llms_membership' ) );
$modify_tax_query = true;
}
if ( is_post_type_archive( 'course' ) || $query->get( 'page_id' ) == llms_get_page_id( 'courses' ) || is_tax( array( 'course_cat', 'course_tag', 'course_difficulty', 'course_track' ) ) ) {
$query->set( 'posts_per_page', get_option( 'lifterlms_shop_courses_per_page', 10 ) );
$sorting = explode( ',', get_option( 'lifterlms_shop_ordering', 'menu_order,ASC' ) );
$orderby = empty( $sorting[0] ) ? 'menu_order' : $sorting[0];
if ( 'menu_order' === $orderby ) {
$orderby .= ' post_title';
}
$order = empty( $sorting[1] ) ? 'ASC' : $sorting[1];
$query->set( 'orderby', apply_filters( 'llms_courses_orderby', $orderby ) );
$query->set( 'order', apply_filters( 'llms_courses_order', $order ) );
$modify_tax_query = true;
} elseif ( is_post_type_archive( 'llms_membership' ) || $query->get( 'page_id' ) == llms_get_page_id( 'memberships' ) || is_tax( array( 'membership_tag', 'membership_cat' ) ) ) {
$query->set( 'posts_per_page', get_option( 'lifterlms_memberships_per_page', 10 ) );
$sorting = explode( ',', get_option( 'lifterlms_memberships_ordering', 'menu_order,ASC' ) );
$orderby = empty( $sorting[0] ) ? 'menu_order' : $sorting[0];
if ( 'menu_order' === $orderby ) {
$orderby .= ' post_title';
}
$order = empty( $sorting[1] ) ? 'ASC' : $sorting[1];
$query->set( 'orderby', apply_filters( 'llms_memberships_orderby', $orderby ) );
$query->set( 'order', apply_filters( 'llms_memberships_order', $order ) );
$modify_tax_query = true;
}
// remove action when finished
remove_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
}// End if().
if ( $modify_tax_query ) {
$query->set( 'tax_query', $this->get_tax_query( $query->get( 'tax_query' ) ) );
}
}
/**
* Set query variables
*
* @since Unknown
*
* @param array $vars WP query variables available for query.
* @return array
*/
public function set_query_vars( $vars ) {
foreach ( $this->get_query_vars() as $key => $var ) {
$vars[] = $key;
}
return $vars;
}
/**
* Handles setting the "paged" variable on Student Dashboard endpoints
* which utilize page/{n} style pagination
*
* @since 3.14.0
* @deprecated 3.28.2 $paged automatically set via add_rewrite_rule() in $this->add_endpoints() method.
*
* @return void
*/
public function set_dashboard_pagination() {
llms_deprecated_function( 'LLMS_Query::set_dashboard_pagination()', '3.28.2' );
$tab = LLMS_Student_Dashboard::get_current_tab( 'slug' );
$var = get_query_var( $tab );
if ( $var ) {
global $wp_rewrite;
$paged = explode( '/', $var );
// this should work on localized sites
if ( $wp_rewrite->pagination_base === $paged[0] ) {
set_query_var( 'paged', $paged[1] );
}
}
}
/**
* Add query variables
*
* @since 1.0.0
* @deprecated 3.31.0 Use LLMS_Query::set_query_vars() instead.
*
* @param array $vars WP query variables available for query.
* @return array
*/
public function add_query_vars( $vars ) {
llms_deprecated_function( 'LLMS_Query::add_query_vars()', '3.31.0', 'LLMS_Query::set_query_vars()' );
return $this->set_query_vars( $vars );
}
}
return new LLMS_Query();