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

153 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 * @package Polylang
4 */
5
6 /**
7 * A class to manipulate the language query var in WP_Query
8 *
9 * @since 2.2
10 */
11 class PLL_Query {
12 /**
13 * Constructor
14 *
15 * @since 2.2
16 *
17 * @param array $query Reference to the WP_Query object
18 * @param object $model
19 */
20 public function __construct( &$query, &$model ) {
21 $this->query = &$query;
22 $this->model = &$model;
23 }
24
25 /**
26 * Check if translated taxonomy is queried
27 * Compatible with nested queries introduced in WP 4.1
28 *
29 * @see https://wordpress.org/support/topic/tax_query-bug
30 *
31 * @since 1.7
32 *
33 * @param array $tax_queries
34 * @return bool
35 */
36 protected function have_translated_taxonomy( $tax_queries ) {
37 if ( is_array( $tax_queries ) ) {
38 foreach ( $tax_queries as $tax_query ) {
39 if ( isset( $tax_query['taxonomy'] ) && $this->model->is_translated_taxonomy( $tax_query['taxonomy'] ) && ! ( isset( $tax_query['operator'] ) && 'NOT IN' === $tax_query['operator'] ) ) {
40 return true;
41 }
42
43 // Nested queries
44 elseif ( is_array( $tax_query ) && $this->have_translated_taxonomy( $tax_query ) ) {
45 return true;
46 }
47 }
48 }
49
50 return false;
51 }
52
53 /**
54 * Get queried taxonomies
55 *
56 * @since 2.2
57 *
58 * @return array queried taxonomies
59 */
60 public function get_queried_taxonomies() {
61 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();
62 }
63
64 /**
65 * Sets the language in query
66 * Optimized for ( needs ) WP 3.5+
67 *
68 * @since 2.2
69 *
70 * @param object $lang
71 */
72 public function set_language( $lang ) {
73 // Defining directly the tax_query ( rather than setting 'lang' avoids transforming the query by WP )
74 $lang_query = array(
75 'taxonomy' => 'language',
76 'field' => 'term_taxonomy_id', // Since WP 3.5
77 'terms' => $lang->term_taxonomy_id,
78 'operator' => 'IN',
79 );
80
81 $tax_query = &$this->query->query_vars['tax_query'];
82
83 if ( isset( $tax_query['relation'] ) && 'OR' === $tax_query['relation'] ) {
84 $tax_query = array(
85 $lang_query,
86 array( $tax_query ),
87 'relation' => 'AND',
88 );
89 } elseif ( is_array( $tax_query ) ) {
90 // The tax query is expected to be *always* an array, but it seems that 3rd parties fill it with a string
91 // Causing a fatal error if we don't check it.
92 // See https://wordpress.org/support/topic/fatal-error-2947/
93 $tax_query[] = $lang_query;
94 } elseif ( empty( $tax_query ) ) {
95 // Supposing the tax query has been wrongly filled with an empty string
96 $tax_query = array( $lang_query );
97 }
98 }
99
100 /**
101 * Add the language in query after it has checked that it won't conflict with other query vars
102 *
103 * @since 2.2
104 *
105 * @param object $lang Language
106 */
107 public function filter_query( $lang ) {
108 $qvars = &$this->query->query_vars;
109
110 if ( ! isset( $qvars['lang'] ) ) {
111 $taxonomies = array_intersect( $this->model->get_translated_taxonomies(), get_taxonomies( array( '_builtin' => false ) ) );
112
113 foreach ( $taxonomies as $tax ) {
114 $tax = get_taxonomy( $tax );
115 if ( ! empty( $qvars[ $tax->query_var ] ) ) {
116 return;
117 }
118 }
119
120 if ( ! empty( $qvars['tax_query'] ) && $this->have_translated_taxonomy( $qvars['tax_query'] ) ) {
121 return;
122 }
123
124 // Filter queries according to the requested language
125 if ( ! empty( $lang ) ) {
126 $taxonomies = $this->get_queried_taxonomies();
127
128 if ( $taxonomies && ( empty( $qvars['post_type'] ) || 'any' === $qvars['post_type'] ) ) {
129 foreach ( $taxonomies as $taxonomy ) {
130 $tax_object = get_taxonomy( $taxonomy );
131 if ( $this->model->is_translated_post_type( $tax_object->object_type ) ) {
132 $this->set_language( $lang );
133 break;
134 }
135 }
136 } elseif ( empty( $qvars['post_type'] ) || $this->model->is_translated_post_type( $qvars['post_type'] ) ) {
137 $this->set_language( $lang );
138 }
139 }
140 } else {
141 // Do not filter untranslatable post types such as nav_menu_item
142 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'] ) ) ) {
143 unset( $qvars['lang'] );
144 }
145
146 // Unset 'all' query var (mainly for admin language filter).
147 if ( isset( $qvars['lang'] ) && 'all' === $qvars['lang'] ) {
148 unset( $qvars['lang'] );
149 }
150 }
151 }
152 }
153