PluginProbe
Polylang / 2.3.6
Polylang v2.3.6
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
← All changes | include/query.php +61 -56 3.0.12.3.6 View file →
@@ -1,8 +1,5 @@
1 1 <?php
2 -/**
3 - * @package Polylang
4 - */
5 2
6 3 /**
7 4 * A class to manipulate the language query var in WP_Query
8 5 *
@@ -8,17 +5,31 @@
8 5 *
9 6 * @since 2.2
10 7 */
11 8 class PLL_Query {
12 - /**
13 - * @var PLL_Model
14 - */
15 - public $model;
16 9
17 - /**
18 - * @var WP_Query
19 - */
20 - public $query;
10 + static protected $excludes = array(
11 + 'p',
12 + 'post_parent',
13 + 'attachment',
14 + 'attachment_id',
15 + 'name',
16 + 'pagename',
17 + 'page_id',
18 + 'category_name',
19 + 'tag',
20 + 'tag_id',
21 + 'cat',
22 + 'category__in',
23 + 'category__and',
24 + 'post__in',
25 + 'post_name__in',
26 + 'tag__in',
27 + 'tag__and',
28 + 'tag_slug__in',
29 + 'tag_slug__and',
30 + 'post_parent__in',
31 + );
21 32
22 33 /**
23 34 * Constructor
24 35 *
@@ -23,10 +34,10 @@
23 34 * Constructor
24 35 *
25 36 * @since 2.2
26 37 *
27 - * @param WP_Query $query Reference to the WP_Query object.
28 - * @param PLL_Model $model Instance of PLL_Model.
38 + * @param array $query Reference to the WP_Query object
39 + * @param object $model
29 40 */
30 41 public function __construct( &$query, &$model ) {
31 42 $this->query = &$query;
32 43 $this->model = &$model;
@@ -32,35 +43,10 @@
32 43 $this->model = &$model;
33 44 }
34 45
35 46 /**
36 - * Checks if the query already includes a language taxonomy.
37 - *
38 - * @since 3.0
39 - *
40 - * @param array $qvars WP_Query query vars.
41 - * @return bool
42 - */
43 - protected function is_already_filtered( $qvars ) {
44 - if ( isset( $qvars['lang'] ) ) {
45 - return true;
46 - }
47 -
48 - if ( ! empty( $qvars['tax_query'] ) && is_array( $qvars['tax_query'] ) ) {
49 - foreach ( $qvars['tax_query'] as $tax_query ) {
50 - if ( isset( $tax_query['taxonomy'] ) && 'language' === $tax_query['taxonomy'] ) {
51 - return true;
52 - }
53 - }
54 - }
55 -
56 - return false;
57 - }
58 -
59 - /**
60 47 * Check if translated taxonomy is queried
61 48 * Compatible with nested queries introduced in WP 4.1
62 - *
63 49 * @see https://wordpress.org/support/topic/tax_query-bug
64 50 *
65 51 * @since 1.7
66 52 *
@@ -95,15 +81,14 @@
95 81 return isset( $this->query->tax_query->queried_terms ) ? array_keys( wp_list_filter( $this->query->tax_query->queried_terms, array( 'operator' => 'NOT IN' ), 'NOT' ) ) : array();
96 82 }
97 83
98 84 /**
99 - * Sets the language in query.
100 - * Optimized for (and requires) WP 3.5+.
85 + * Sets the language in query
86 + * Optimized for ( needs ) WP 3.5+
101 87 *
102 88 * @since 2.2
103 89 *
104 - * @param PLL_Language $lang Language object.
105 - * @return void
90 + * @param object $lang
106 91 */
107 92 public function set_language( $lang ) {
108 93 // Defining directly the tax_query ( rather than setting 'lang' avoids transforming the query by WP )
109 94 $lang_query = array(
@@ -120,36 +105,56 @@
120 105 $lang_query,
121 106 array( $tax_query ),
122 107 'relation' => 'AND',
123 108 );
124 - } elseif ( is_array( $tax_query ) ) {
125 - // The tax query is expected to be *always* an array, but it seems that 3rd parties fill it with a string
126 - // Causing a fatal error if we don't check it.
127 - // See https://wordpress.org/support/topic/fatal-error-2947/
109 + } else {
128 110 $tax_query[] = $lang_query;
129 - } elseif ( empty( $tax_query ) ) {
130 - // Supposing the tax query has been wrongly filled with an empty string
131 - $tax_query = array( $lang_query );
132 111 }
133 112 }
134 113
135 114 /**
136 - * Adds the language in the query after it has checked that it won't conflict with other query vars.
115 + * Add the language in query after it has checked that it won't conflict with other query vars
137 116 *
138 117 * @since 2.2
139 118 *
140 - * @param PLL_Language $lang Language.
141 - * @return void
119 + * @param object $lang Language
142 120 */
143 121 public function filter_query( $lang ) {
144 122 $qvars = &$this->query->query_vars;
145 123
146 - if ( ! $this->is_already_filtered( $qvars ) ) {
124 + if ( ! isset( $qvars['lang'] ) ) {
125 + /**
126 + * Filter the query vars which disable the language filter in a query
127 + *
128 + * @since 2.3.5
129 + *
130 + * @param array $excludes Query vars excluded from the language filter
131 + * @param object $query WP Query
132 + * @param object $lang Language
133 + */
134 + $excludes = apply_filters( 'pll_filter_query_excluded_query_vars', self::$excludes, $this->query, $lang );
135 +
136 + // Do not filter the query if the language is already specified in another way
137 + foreach ( $excludes as $k ) {
138 + if ( ! empty( $qvars[ $k ] ) ) {
139 + // Specific case for 'cat' as it can contain negative values
140 + if ( 'cat' === $k ) {
141 + foreach ( explode( ',', $qvars['cat'] ) as $cat ) {
142 + if ( $cat > 0 ) {
143 + return;
144 + }
145 + }
146 + } else {
147 + return;
148 + }
149 + }
150 + }
151 +
147 152 $taxonomies = array_intersect( $this->model->get_translated_taxonomies(), get_taxonomies( array( '_builtin' => false ) ) );
148 153
149 154 foreach ( $taxonomies as $tax ) {
150 - $tax_object = get_taxonomy( $tax );
151 - if ( ! empty( $tax_object ) && ! empty( $qvars[ $tax_object->query_var ] ) ) {
155 + $tax = get_taxonomy( $tax );
156 + if ( ! empty( $qvars[ $tax->query_var ] ) ) {
152 157 return;
153 158 }
154 159 }
155 160
@@ -163,9 +168,9 @@
163 168
164 169 if ( $taxonomies && ( empty( $qvars['post_type'] ) || 'any' === $qvars['post_type'] ) ) {
165 170 foreach ( $taxonomies as $taxonomy ) {
166 171 $tax_object = get_taxonomy( $taxonomy );
167 - if ( ! empty( $tax_object ) && $this->model->is_translated_post_type( $tax_object->object_type ) ) {
172 + if ( $this->model->is_translated_post_type( $tax_object->object_type ) ) {
168 173 $this->set_language( $lang );
169 174 break;
170 175 }
171 176 }