-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathquery.php
More file actions
139 lines (120 loc) · 3.81 KB
/
query.php
File metadata and controls
139 lines (120 loc) · 3.81 KB
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
<?php
/**
* Set up query modifications.
*/
namespace WordPressdotorg\Theme\Learn_2024\Query;
add_action( 'pre_get_posts', __NAMESPACE__ . '\add_language_to_archive_queries' );
add_action( 'pre_get_posts', __NAMESPACE__ . '\add_excluded_to_lesson_archive_query' );
add_action( 'pre_get_posts', __NAMESPACE__ . '\filter_search_queries_by_post_type' );
add_filter( 'request', __NAMESPACE__ . '\handle_all_level_query' );
/**
* Modify the query by adding meta query for language if set.
*
* @param WP_Query $query The query object.
*/
function add_language_to_archive_queries( $query ) {
// Ensure this code runs only for the main query on archive pages and search results.
if ( ! is_admin() && $query->is_main_query() && ( $query->is_archive() || $query->is_search() ) ) {
if ( isset( $_GET['language'] ) && is_array( $_GET['language'] ) ) {
$languages = array_map( 'sanitize_text_field', $_GET['language'] );
$meta_query = array( 'relation' => 'OR' );
$meta_query[] = array(
'key' => 'language',
'value' => $languages,
'compare' => 'IN',
);
// If 'en_US' is included, include posts with no language defined
// as this is the default value for the meta field.
if ( in_array( 'en_US', $languages ) ) {
$meta_query[] = array(
'key' => 'language',
'compare' => 'NOT EXISTS',
);
}
$query->set( 'meta_query', $meta_query );
}
}
}
/**
* Modify the query by adding meta query for excluding the lesson from the archive if set.
*
* @param WP_Query $query The query object.
*/
function add_excluded_to_lesson_archive_query( $query ) {
// Ensure this code runs only for the main query on lesson archive pages and search results.
if ( ! is_admin() && $query->is_main_query() && ( $query->is_archive( 'lesson' ) || $query->is_search() ) ) {
$meta_query = $query->get( 'meta_query', array() );
$tax_query = $query->get( 'tax_query', array() );
$exclude_lessons_by_post_meta = array(
'relation' => 'OR',
array(
'key' => '_lesson_archive_excluded',
'compare' => 'NOT EXISTS',
),
array(
'key' => '_lesson_archive_excluded',
'value' => 'excluded',
'compare' => '!=',
),
);
// If there's an existing meta query, wrap both in an AND relation
if ( ! empty( $meta_query ) ) {
$meta_query = array(
'relation' => 'AND',
$meta_query,
$exclude_lessons_by_post_meta,
);
} else {
$meta_query = $exclude_lessons_by_post_meta;
}
$exclude_lessons_by_taxonomy = array(
'taxonomy' => 'show',
'field' => 'slug',
'terms' => 'hidden',
'operator' => 'NOT IN',
);
// If there's an existing tax query, add the new condition
if ( ! empty( $tax_query ) ) {
$tax_query['relation'] = 'AND';
$tax_query[] = $exclude_lessons_by_taxonomy;
} else {
$tax_query = array( $exclude_lessons_by_taxonomy );
}
$query->set( 'meta_query', $meta_query );
$query->set( 'tax_query', $tax_query );
}
}
/**
* Filter search queries by post type.
* Only include courses and lessons in search results unless post_type is set, eg. for an archive search.
*
* @param WP_Query $query The query object.
* @return WP_Query The modified query object.
*/
function filter_search_queries_by_post_type( $query ) {
if ( ! is_admin() && $query->is_search() && $query->is_main_query() ) {
if ( ! $query->get( 'post_type' ) ) {
$query->set( 'post_type', array( 'course', 'lesson' ) );
}
}
return $query;
}
/**
* Modify the request.
*
* Update the query_vars to reset 'all' to an empty string.
*
* @param array $query_vars The array of requested query variables.
*
* @return array
*/
function handle_all_level_query( $query_vars ) {
if ( is_admin() ) {
return $query_vars;
}
$level = $query_vars['wporg_lesson_level'] ?? '';
if ( 'all' === $level ) {
$query_vars['wporg_lesson_level'] = '';
}
return $query_vars;
}