| @@ -63,9 +63,9 @@ | ||
| 63 | 63 | * @see https://wordpress.org/support/topic/tax_query-bug |
| 64 | 64 | * |
| 65 | 65 | * @since 1.7 |
| 66 | 66 | * |
| 67 | - * @param array $tax_queries | |
| 67 | + * @param array $tax_queries An array of tax queries. | |
| 68 | 68 | * @return bool |
| 69 | 69 | */ |
| 70 | 70 | protected function have_translated_taxonomy( $tax_queries ) { |
| 71 | 71 | if ( is_array( $tax_queries ) ) { |
| @@ -91,9 +91,9 @@ | ||
| 91 | 91 | * |
| 92 | 92 | * @return array queried taxonomies |
| 93 | 93 | */ |
| 94 | 94 | public function get_queried_taxonomies() { |
| 95 | - 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(); | |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | /** |
| 99 | 99 | * Sets the language in query. |
| @@ -99,18 +99,28 @@ | ||
| 99 | 99 | * Sets the language in query. |
| 100 | 100 | * Optimized for (and requires) WP 3.5+. |
| 101 | 101 | * |
| 102 | 102 | * @since 2.2 |
| 103 | + * @since 3.3 Accepts now an array of languages. | |
| 103 | 104 | * |
| 104 | - * @param PLL_Language $lang Language object. | |
| 105 | + * @param PLL_Language|PLL_Language[] $languages Language object(s). | |
| 105 | 106 | * @return void |
| 106 | 107 | */ |
| 107 | - public function set_language( $lang ) { | |
| 108 | - // 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). | |
| 109 | 119 | $lang_query = array( |
| 110 | 120 | 'taxonomy' => 'language', |
| 111 | 121 | 'field' => 'term_taxonomy_id', // Since WP 3.5 |
| 112 | - 'terms' => $lang->term_taxonomy_id, | |
| 122 | + 'terms' => $tt_ids, | |
| 113 | 123 | 'operator' => 'IN', |
| 114 | 124 | ); |
| 115 | 125 | |
| 116 | 126 | $tax_query = &$this->query->query_vars['tax_query']; |
| @@ -117,9 +127,9 @@ | ||
| 117 | 127 | |
| 118 | 128 | if ( isset( $tax_query['relation'] ) && 'OR' === $tax_query['relation'] ) { |
| 119 | 129 | $tax_query = array( |
| 120 | 130 | $lang_query, |
| 121 | - array( $tax_query ), | |
| 131 | + $tax_query, | |
| 122 | 132 | 'relation' => 'AND', |
| 123 | 133 | ); |
| 124 | 134 | } elseif ( is_array( $tax_query ) ) { |
| 125 | 135 | // The tax query is expected to be *always* an array, but it seems that 3rd parties fill it with a string |
| @@ -136,9 +146,9 @@ | ||
| 136 | 146 | * Adds the language in the query after it has checked that it won't conflict with other query vars. |
| 137 | 147 | * |
| 138 | 148 | * @since 2.2 |
| 139 | 149 | * |
| 140 | - * @param PLL_Language $lang Language. | |
| 150 | + * @param PLL_Language|false $lang Language. | |
| 141 | 151 | * @return void |
| 142 | 152 | */ |
| 143 | 153 | public function filter_query( $lang ) { |
| 144 | 154 | $qvars = &$this->query->query_vars; |
| @@ -173,8 +183,10 @@ | ||
| 173 | 183 | $this->set_language( $lang ); |
| 174 | 184 | } |
| 175 | 185 | } |
| 176 | 186 | } else { |
| 187 | + $this->maybe_set_language_for_or_relation(); | |
| 188 | + | |
| 177 | 189 | // Do not filter untranslatable post types such as nav_menu_item |
| 178 | 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'] ) ) ) { |
| 179 | 191 | unset( $qvars['lang'] ); |
| 180 | 192 | } |
| @@ -182,7 +194,41 @@ | ||
| 182 | 194 | // Unset 'all' query var (mainly for admin language filter). |
| 183 | 195 | if ( isset( $qvars['lang'] ) && 'all' === $qvars['lang'] ) { |
| 184 | 196 | unset( $qvars['lang'] ); |
| 185 | 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(). | |
| 186 | 232 | } |
| 187 | 233 | } |
| 188 | 234 | } |