PluginProbe
Polylang / trunk
Polylang vtrunk
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
← All changes | src/frontend/frontend-auto-translate.php +21 -45 3.8.8 → trunk View file →
@@ -76,9 +76,9 @@
76 76 public function translate_included_ids_in_query( $query ) {
77 77 global $wpdb;
78 78 $qv = &$query->query_vars;
79 79
80 - if ( $query->is_main_query() || isset( $qv['lang'] ) || ( ! empty( $qv['post_type'] ) && ! $this->model->is_translated_post_type( $qv['post_type'] ) ) ) {
80 + if ( $query->is_main_query() || isset( $qv['lang'] ) || empty( $this->curlang ) || ( ! empty( $qv['post_type'] ) && ! $this->model->is_translated_post_type( $qv['post_type'] ) ) ) {
81 81 return;
82 82 }
83 83
84 84 // /!\ always keep untranslated as is
@@ -97,12 +97,12 @@
97 97 // Category_name
98 98 $arr = array();
99 99 if ( ! empty( $qv['category_name'] ) ) {
100 100 foreach ( explode( ',', $qv['category_name'] ) as $slug ) {
101 - $arr[] = $this->get_translated_term_by( 'slug', $slug, 'category' );
101 + $arr[] = $this->model->term->get_by( 'slug', $slug, $this->curlang, 'category' );
102 102 }
103 103
104 - $qv['category_name'] = implode( ',', $arr );
104 + $qv['category_name'] = implode( ',', array_filter( $arr ) );
105 105 }
106 106
107 107 // Array of term ids
108 108 foreach ( array( 'category__and', 'category__in', 'category__not_in', 'tag__and', 'tag__in', 'tag__not_in' ) as $key ) {
@@ -130,12 +130,12 @@
130 130 foreach ( array( 'tag_slug__and', 'tag_slug__in' ) as $key ) {
131 131 $arr = array();
132 132 if ( ! empty( $qv[ $key ] ) ) {
133 133 foreach ( $qv[ $key ] as $slug ) {
134 - $arr[] = $this->get_translated_term_by( 'slug', $slug, 'post_tag' );
134 + $arr[] = $this->model->term->get_by( 'slug', $slug, $this->curlang, 'post_tag' );
135 135 }
136 136
137 - $qv[ $key ] = $arr;
137 + $qv[ $key ] = array_filter( $arr );
138 138 }
139 139 }
140 140
141 141 // Custom taxonomies
@@ -253,8 +253,12 @@
253 253 * @param array $tax_queries An array of tax queries.
254 254 * @return array Translated tax queries.
255 255 */
256 256 protected function translate_tax_query_recursive( $tax_queries ) {
257 + if ( empty( $this->curlang ) ) {
258 + return $tax_queries;
259 + }
260 +
257 261 foreach ( $tax_queries as $key => $q ) {
258 262 if ( ! is_array( $q ) ) {
259 263 continue;
260 264 }
@@ -261,10 +265,11 @@
261 265
262 266 if ( isset( $q['taxonomy'], $q['terms'] ) && $this->model->is_translated_taxonomy( $q['taxonomy'] ) ) {
263 267 $arr = array();
264 268 $field = isset( $q['field'] ) && in_array( $q['field'], array( 'slug', 'name' ) ) ? $q['field'] : 'term_id';
265 - foreach ( (array) $q['terms'] as $t ) {
266 - $arr[] = $this->get_translated_term_by( $field, $t, $q['taxonomy'] );
269 + foreach ( (array) $q['terms'] as $term ) {
270 + $translated = $this->model->term->get_by( $field, $term, $this->curlang, $q['taxonomy'] );
271 + $arr[] = $translated ?: $term;
267 272 }
268 273
269 274 $tax_queries[ $key ]['terms'] = $arr;
270 275 } else {
@@ -276,41 +281,8 @@
276 281 return $tax_queries;
277 282 }
278 283
279 284 /**
280 - * Translates a term given one field.
281 - *
282 - * @since 2.3.3
283 - *
284 - * @param string $field Either 'slug', 'name', 'term_id', or 'term_taxonomy_id'
285 - * @param string|int $term Search for this term value
286 - * @param string $taxonomy Taxonomy name.
287 - * @return string|int Translated term slug, name, term_id or term_taxonomy_id
288 - */
289 - protected function get_translated_term_by( $field, $term, $taxonomy ) {
290 - if ( 'term_id' === $field ) {
291 - if ( $tr_id = $this->get_term( $term ) ) {
292 - return $tr_id;
293 - }
294 - } else {
295 - $terms = get_terms( array( 'taxonomy' => $taxonomy, $field => $term, 'lang' => '' ) );
296 -
297 - if ( ! empty( $terms ) && is_array( $terms ) ) {
298 - $t = reset( $terms );
299 - if ( ! $t instanceof WP_Term ) {
300 - return $term;
301 - }
302 - $tr_id = $this->get_term( $t->term_id );
303 -
304 - if ( ! is_wp_error( $tr = get_term( $tr_id, $taxonomy ) ) ) {
305 - return $tr->$field;
306 - }
307 - }
308 - }
309 - return $term;
310 - }
311 -
312 - /**
313 285 * Translates a list of term slugs provided either as an array or a string
314 286 * with slugs separated by a comma or a '+'.
315 287 *
316 288 * @since 3.2.8
@@ -328,15 +300,19 @@
328 300 $sep = strpos( $query_var, ',' ) !== false ? ',' : '+'; // Two possible separators.
329 301 $slugs = explode( $sep, $query_var );
330 302 }
331 303
332 - foreach ( $slugs as &$slug ) {
333 - if ( ! is_string( $slug ) ) {
334 - // We got an unexpected query var, let return it unchanged.
335 - return $query_var;
304 + if ( ! empty( $this->curlang ) ) {
305 + foreach ( $slugs as $key => $slug ) {
306 + if ( ! is_string( $slug ) ) {
307 + // We got an unexpected query var, let return it unchanged.
308 + return $query_var;
309 + }
310 + $slugs[ $key ] = $this->model->term->get_by( 'slug', $slug, $this->curlang, $taxonomy );
336 311 }
337 - $slug = $this->get_translated_term_by( 'slug', $slug, $taxonomy );
338 312 }
313 +
314 + $slugs = array_filter( $slugs );
339 315
340 316 if ( ! empty( $sep ) ) {
341 317 return implode( $sep, $slugs );
342 318 }