PluginProbe
Polylang / 2.3.2
Polylang v2.3.2
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
polylang / include / query.php

query.php in Polylang 2.3.2, at include/query.php

170 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * A class to manipulate the language query var in WP_Query
5 *
6 * @since 2.2
7 */
8 class PLL_Query {
9
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 'category__in',
22 'category__and',
23 'post__in',
24 'post_name__in',
25 'tag__in',
26 'tag__and',
27 'tag_slug__in',
28 'tag_slug__and',
29 'post_parent__in',
30 );
31
32 /**
33 * Constructor
34 *
35 * @since 2.2
36 *
37 * @param array $query Reference to the WP_Query object
38 * @param object $model
39 */
40 public function __construct( &$query, &$model ) {
41 $this->query = &$query;
42 $this->model = &$model;
43 }
44
45 /**
46 * Check if translated taxonomy is queried
47 * Compatible with nested queries introduced in WP 4.1
48 * @see https://wordpress.org/support/topic/tax_query-bug
49 *
50 * @since 1.7
51 *
52 * @param array $tax_queries
53 * @return bool
54 */
55 protected function have_translated_taxonomy( $tax_queries ) {
56 if ( is_array( $tax_queries ) ) {
57 foreach ( $tax_queries as $tax_query ) {
58 if ( isset( $tax_query['taxonomy'] ) && $this->model->is_translated_taxonomy( $tax_query['taxonomy'] ) && ! ( isset( $tax_query['operator'] ) && 'NOT IN' === $tax_query['operator'] ) ) {
59 return true;
60 }
61
62 // Nested queries
63 elseif ( is_array( $tax_query ) && $this->have_translated_taxonomy( $tax_query ) ) {
64 return true;
65 }
66 }
67 }
68
69 return false;
70 }
71
72 /**
73 * Get queried taxonomies
74 *
75 * @since 2.2
76 *
77 * @return array queried taxonomies
78 */
79 public function get_queried_taxonomies() {
80 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();
81 }
82
83 /**
84 * Sets the language in query
85 * Optimized for ( needs ) WP 3.5+
86 *
87 * @since 2.2
88 *
89 * @param object $lang
90 */
91 public function set_language( $lang ) {
92 // Defining directly the tax_query ( rather than setting 'lang' avoids transforming the query by WP )
93 $this->query->query_vars['tax_query'][] = array(
94 'taxonomy' => 'language',
95 'field' => 'term_taxonomy_id', // Since WP 3.5
96 'terms' => $lang->term_taxonomy_id,
97 'operator' => 'IN',
98 );
99 }
100
101 /**
102 * Add the language in query after it has checked that it won't conflict with other query vars
103 *
104 * @since 2.2
105 *
106 * @param object $lang Language
107 */
108 public function filter_query( $lang ) {
109 $qvars = &$this->query->query_vars;
110
111 if ( ! isset( $qvars['lang'] ) ) {
112 // Do not filter the query if the language is already specified in another way
113 foreach ( self::$excludes as $k ) {
114 if ( ! empty( $qvars[ $k ] ) ) {
115 return;
116 }
117 }
118
119 // Specific case for 'cat' as it can contain negative values
120 if ( ! empty( $qvars['cat'] ) ) {
121 foreach ( explode( ',', $qvars['cat'] ) as $cat ) {
122 if ( $cat > 0 ) {
123 return;
124 }
125 }
126 }
127
128 $taxonomies = array_intersect( $this->model->get_translated_taxonomies(), get_taxonomies( array( '_builtin' => false ) ) );
129
130 foreach ( $taxonomies as $tax ) {
131 $tax = get_taxonomy( $tax );
132 if ( ! empty( $qvars[ $tax->query_var ] ) ) {
133 return;
134 }
135 }
136
137 if ( ! empty( $qvars['tax_query'] ) && $this->have_translated_taxonomy( $qvars['tax_query'] ) ) {
138 return;
139 }
140
141 // Filter queries according to the requested language
142 if ( ! empty( $lang ) ) {
143 $taxonomies = $this->get_queried_taxonomies();
144
145 if ( $taxonomies && ( empty( $qvars['post_type'] ) || 'any' === $qvars['post_type'] ) ) {
146 foreach ( $taxonomies as $taxonomy ) {
147 $tax_object = get_taxonomy( $taxonomy );
148 if ( $this->model->is_translated_post_type( $tax_object->object_type ) ) {
149 $this->set_language( $lang );
150 break;
151 }
152 }
153 } elseif ( empty( $qvars['post_type'] ) || $this->model->is_translated_post_type( $qvars['post_type'] ) ) {
154 $this->set_language( $lang );
155 }
156 }
157 } else {
158 // Do not filter untranslatable post types such as nav_menu_item
159 if ( isset( $qvars['post_type'] ) && ! $this->model->is_translated_post_type( $qvars['post_type'] ) && ( empty( $qvars['tax_query'] ) || ! $this->have_translated_taxonomy( $qvars['tax_query'] ) ) ) {
160 unset( $qvars['lang'] );
161 }
162
163 // Unset 'all' query var (mainly for admin language filter).
164 if ( isset( $qvars['lang'] ) && 'all' === $qvars['lang'] ) {
165 unset( $qvars['lang'] );
166 }
167 }
168 }
169 }
170