PluginProbe
Polylang / 2.2.1
Polylang v2.2.1
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.2.1, at include/query.php

168 lines 4.2 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 foreach ( $tax_queries as $tax_query ) {
57 if ( isset( $tax_query['taxonomy'] ) && $this->model->is_translated_taxonomy( $tax_query['taxonomy'] ) && ! ( isset( $tax_query['operator'] ) && 'NOT IN' === $tax_query['operator'] ) ) {
58 return true;
59 }
60
61 // Nested queries
62 elseif ( is_array( $tax_query ) && $this->have_translated_taxonomy( $tax_query ) ) {
63 return true;
64 }
65 }
66
67 return false;
68 }
69
70 /**
71 * Get queried taxonomies
72 *
73 * @since 2.2
74 *
75 * @return array queried taxonomies
76 */
77 public function get_queried_taxonomies() {
78 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();
79 }
80
81 /**
82 * Sets the language in query
83 * Optimized for ( needs ) WP 3.5+
84 *
85 * @since 2.2
86 *
87 * @param object $lang
88 */
89 public function set_language( $lang ) {
90 // Defining directly the tax_query ( rather than setting 'lang' avoids transforming the query by WP )
91 $this->query->query_vars['tax_query'][] = array(
92 'taxonomy' => 'language',
93 'field' => 'term_taxonomy_id', // Since WP 3.5
94 'terms' => $lang->term_taxonomy_id,
95 'operator' => 'IN',
96 );
97 }
98
99 /**
100 * Add the language in query after it has checked that it won't conflict with other query vars
101 *
102 * @since 2.2
103 *
104 * @param object $lang Language
105 */
106 public function filter_query( $lang ) {
107 $qvars = &$this->query->query_vars;
108
109 if ( ! isset( $qvars['lang'] ) ) {
110 // Do not filter the query if the language is already specified in another way
111 foreach ( self::$excludes as $k ) {
112 if ( ! empty( $qvars[ $k ] ) ) {
113 return;
114 }
115 }
116
117 // Specific case for 'cat' as it can contain negative values
118 if ( ! empty( $qvars['cat'] ) ) {
119 foreach ( explode( ',', $qvars['cat'] ) as $cat ) {
120 if ( $cat > 0 ) {
121 return;
122 }
123 }
124 }
125
126 $taxonomies = array_intersect( $this->model->get_translated_taxonomies(), get_taxonomies( array( '_builtin' => false ) ) );
127
128 foreach ( $taxonomies as $tax ) {
129 $tax = get_taxonomy( $tax );
130 if ( ! empty( $qvars[ $tax->query_var ] ) ) {
131 return;
132 }
133 }
134
135 if ( ! empty( $qvars['tax_query'] ) && is_array( $qvars['tax_query'] ) && $this->have_translated_taxonomy( $qvars['tax_query'] ) ) {
136 return;
137 }
138
139 // Filter queries according to the requested language
140 if ( ! empty( $lang ) ) {
141 $taxonomies = $this->get_queried_taxonomies();
142
143 if ( $taxonomies && ( empty( $qvars['post_type'] ) || 'any' === $qvars['post_type'] ) ) {
144 foreach ( $taxonomies as $taxonomy ) {
145 $tax_object = get_taxonomy( $taxonomy );
146 if ( $this->model->is_translated_post_type( $tax_object->object_type ) ) {
147 $this->set_language( $lang );
148 break;
149 }
150 }
151 } elseif ( empty( $qvars['post_type'] ) || $this->model->is_translated_post_type( $qvars['post_type'] ) ) {
152 $this->set_language( $lang );
153 }
154 }
155 } else {
156 // Do not filter untranslatable post types such as nav_menu_item
157 if ( isset( $qvars['post_type'] ) && ! $this->model->is_translated_post_type( $qvars['post_type'] ) ) {
158 unset( $qvars['lang'] );
159 }
160
161 // Unset 'all' query var (mainly for admin language filter).
162 if ( isset( $qvars['lang'] ) && 'all' === $qvars['lang'] ) {
163 unset( $qvars['lang'] );
164 }
165 }
166 }
167 }
168