| @@ -9,14 +9,24 @@ | ||
| 9 | 9 | * @since 2.2 |
| 10 | 10 | */ |
| 11 | 11 | class PLL_Query { |
| 12 | 12 | /** |
| 13 | + * @var PLL_Model | |
| 14 | + */ | |
| 15 | + public $model; | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * @var WP_Query | |
| 19 | + */ | |
| 20 | + public $query; | |
| 21 | + | |
| 22 | + /** | |
| 13 | 23 | * Constructor |
| 14 | 24 | * |
| 15 | 25 | * @since 2.2 |
| 16 | 26 | * |
| 17 | - * @param array $query Reference to the WP_Query object | |
| 18 | - * @param object $model | |
| 27 | + * @param WP_Query $query Reference to the WP_Query object. | |
| 28 | + * @param PLL_Model $model Instance of PLL_Model. | |
| 19 | 29 | */ |
| 20 | 30 | public function __construct( &$query, &$model ) { |
| 21 | 31 | $this->query = &$query; |
| 22 | 32 | $this->model = &$model; |
| @@ -22,8 +32,32 @@ | ||
| 22 | 32 | $this->model = &$model; |
| 23 | 33 | } |
| 24 | 34 | |
| 25 | 35 | /** |
| 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 | + /** | |
| 26 | 60 | * Check if translated taxonomy is queried |
| 27 | 61 | * Compatible with nested queries introduced in WP 4.1 |
| 28 | 62 | * |
| 29 | 63 | * @see https://wordpress.org/support/topic/tax_query-bug |
| @@ -29,9 +63,9 @@ | ||
| 29 | 63 | * @see https://wordpress.org/support/topic/tax_query-bug |
| 30 | 64 | * |
| 31 | 65 | * @since 1.7 |
| 32 | 66 | * |
| 33 | - * @param array $tax_queries | |
| 67 | + * @param array $tax_queries An array of tax queries. | |
| 34 | 68 | * @return bool |
| 35 | 69 | */ |
| 36 | 70 | protected function have_translated_taxonomy( $tax_queries ) { |
| 37 | 71 | if ( is_array( $tax_queries ) ) { |
| @@ -57,25 +91,36 @@ | ||
| 57 | 91 | * |
| 58 | 92 | * @return array queried taxonomies |
| 59 | 93 | */ |
| 60 | 94 | 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(); | |
| 95 | + return ! empty( $this->query->tax_query->queried_terms ) ? array_keys( wp_list_filter( $this->query->tax_query->queried_terms, array( 'operator' => 'NOT IN' ), 'NOT' ) ) : array(); | |
| 62 | 96 | } |
| 63 | 97 | |
| 64 | 98 | /** |
| 65 | - * Sets the language in query | |
| 66 | - * Optimized for ( needs ) WP 3.5+ | |
| 99 | + * Sets the language in query. | |
| 100 | + * Optimized for (and requires) WP 3.5+. | |
| 67 | 101 | * |
| 68 | 102 | * @since 2.2 |
| 103 | + * @since 3.3 Accepts now an array of languages. | |
| 69 | 104 | * |
| 70 | - * @param object $lang | |
| 105 | + * @param PLL_Language|PLL_Language[] $languages Language object(s). | |
| 106 | + * @return void | |
| 71 | 107 | */ |
| 72 | - public function set_language( $lang ) { | |
| 73 | - // Defining directly the tax_query ( rather than setting 'lang' avoids transforming the query by WP ) | |
| 108 | + public function set_language( $languages ) { | |
| 109 | + if ( ! is_array( $languages ) ) { | |
| 110 | + $languages = array( $languages ); | |
| 111 | + } | |
| 112 | + | |
| 113 | + $tt_ids = array(); | |
| 114 | + foreach ( $languages as $language ) { | |
| 115 | + $tt_ids[] = $language->get_tax_prop( 'language', 'term_taxonomy_id' ); | |
| 116 | + } | |
| 117 | + | |
| 118 | + // Defining directly the tax_query (rather than setting 'lang' avoids transforming the query by WP). | |
| 74 | 119 | $lang_query = array( |
| 75 | 120 | 'taxonomy' => 'language', |
| 76 | 121 | 'field' => 'term_taxonomy_id', // Since WP 3.5 |
| 77 | - 'terms' => $lang->term_taxonomy_id, | |
| 122 | + 'terms' => $tt_ids, | |
| 78 | 123 | 'operator' => 'IN', |
| 79 | 124 | ); |
| 80 | 125 | |
| 81 | 126 | $tax_query = &$this->query->query_vars['tax_query']; |
| @@ -82,9 +127,9 @@ | ||
| 82 | 127 | |
| 83 | 128 | if ( isset( $tax_query['relation'] ) && 'OR' === $tax_query['relation'] ) { |
| 84 | 129 | $tax_query = array( |
| 85 | 130 | $lang_query, |
| 86 | - array( $tax_query ), | |
| 131 | + $tax_query, | |
| 87 | 132 | 'relation' => 'AND', |
| 88 | 133 | ); |
| 89 | 134 | } elseif ( is_array( $tax_query ) ) { |
| 90 | 135 | // The tax query is expected to be *always* an array, but it seems that 3rd parties fill it with a string |
| @@ -97,23 +142,24 @@ | ||
| 97 | 142 | } |
| 98 | 143 | } |
| 99 | 144 | |
| 100 | 145 | /** |
| 101 | - * Add the language in query after it has checked that it won't conflict with other query vars | |
| 146 | + * Adds the language in the query after it has checked that it won't conflict with other query vars. | |
| 102 | 147 | * |
| 103 | 148 | * @since 2.2 |
| 104 | 149 | * |
| 105 | - * @param object $lang Language | |
| 150 | + * @param PLL_Language|false $lang Language. | |
| 151 | + * @return void | |
| 106 | 152 | */ |
| 107 | 153 | public function filter_query( $lang ) { |
| 108 | 154 | $qvars = &$this->query->query_vars; |
| 109 | 155 | |
| 110 | - if ( ! isset( $qvars['lang'] ) ) { | |
| 156 | + if ( ! $this->is_already_filtered( $qvars ) ) { | |
| 111 | 157 | $taxonomies = array_intersect( $this->model->get_translated_taxonomies(), get_taxonomies( array( '_builtin' => false ) ) ); |
| 112 | 158 | |
| 113 | 159 | foreach ( $taxonomies as $tax ) { |
| 114 | - $tax = get_taxonomy( $tax ); | |
| 115 | - if ( ! empty( $qvars[ $tax->query_var ] ) ) { | |
| 160 | + $tax_object = get_taxonomy( $tax ); | |
| 161 | + if ( ! empty( $tax_object ) && ! empty( $qvars[ $tax_object->query_var ] ) ) { | |
| 116 | 162 | return; |
| 117 | 163 | } |
| 118 | 164 | } |
| 119 | 165 | |
| @@ -127,9 +173,9 @@ | ||
| 127 | 173 | |
| 128 | 174 | if ( $taxonomies && ( empty( $qvars['post_type'] ) || 'any' === $qvars['post_type'] ) ) { |
| 129 | 175 | foreach ( $taxonomies as $taxonomy ) { |
| 130 | 176 | $tax_object = get_taxonomy( $taxonomy ); |
| 131 | - if ( $this->model->is_translated_post_type( $tax_object->object_type ) ) { | |
| 177 | + if ( ! empty( $tax_object ) && $this->model->is_translated_post_type( $tax_object->object_type ) ) { | |
| 132 | 178 | $this->set_language( $lang ); |
| 133 | 179 | break; |
| 134 | 180 | } |
| 135 | 181 | } |
| @@ -137,8 +183,10 @@ | ||
| 137 | 183 | $this->set_language( $lang ); |
| 138 | 184 | } |
| 139 | 185 | } |
| 140 | 186 | } else { |
| 187 | + $this->maybe_set_language_for_or_relation(); | |
| 188 | + | |
| 141 | 189 | // Do not filter untranslatable post types such as nav_menu_item |
| 142 | 190 | 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 | 191 | unset( $qvars['lang'] ); |
| 144 | 192 | } |
| @@ -146,7 +194,41 @@ | ||
| 146 | 194 | // Unset 'all' query var (mainly for admin language filter). |
| 147 | 195 | if ( isset( $qvars['lang'] ) && 'all' === $qvars['lang'] ) { |
| 148 | 196 | unset( $qvars['lang'] ); |
| 149 | 197 | } |
| 198 | + } | |
| 199 | + } | |
| 200 | + | |
| 201 | + /** | |
| 202 | + * Sets the language correctly if the current query is a 'OR' relation, | |
| 203 | + * since WordPress merges the language with the other query vars when the relation is OR. | |
| 204 | + * | |
| 205 | + * @since 3.3 | |
| 206 | + * | |
| 207 | + * @return void | |
| 208 | + */ | |
| 209 | + protected function maybe_set_language_for_or_relation() { | |
| 210 | + if ( ! $this->query->tax_query instanceof WP_Tax_Query ) { | |
| 211 | + return; | |
| 212 | + } | |
| 213 | + | |
| 214 | + if ( 'OR' !== $this->query->tax_query->relation ) { | |
| 215 | + return; | |
| 216 | + } | |
| 217 | + | |
| 218 | + if ( ! isset( $this->query->tax_query->queried_terms['language'] ) ) { | |
| 219 | + return; | |
| 220 | + } | |
| 221 | + | |
| 222 | + $langs = $this->query->tax_query->queried_terms['language']['terms']; | |
| 223 | + if ( is_string( $langs ) ) { | |
| 224 | + $langs = explode( ',', $langs ); | |
| 225 | + } | |
| 226 | + $langs = array_map( array( $this->model, 'get_language' ), $langs ); | |
| 227 | + $langs = array_filter( $langs ); | |
| 228 | + | |
| 229 | + if ( ! empty( $langs ) ) { | |
| 230 | + $this->set_language( $langs ); | |
| 231 | + unset( $this->query->query_vars['lang'] ); // Unset the language query var otherwise WordPress would add the language query by slug in WP_Query::parse_tax_query(). | |
| 150 | 232 | } |
| 151 | 233 | } |
| 152 | 234 | } |