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 +20 -43 3.8.4 → 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,9 +97,10 @@
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 + $translated = $this->model->term->get_by( 'slug', $slug, $this->curlang, 'category' );
102 + $arr[] = $translated ?: $slug;
102 103 }
103 104
104 105 $qv['category_name'] = implode( ',', $arr );
105 106 }
@@ -130,9 +131,10 @@
130 131 foreach ( array( 'tag_slug__and', 'tag_slug__in' ) as $key ) {
131 132 $arr = array();
132 133 if ( ! empty( $qv[ $key ] ) ) {
133 134 foreach ( $qv[ $key ] as $slug ) {
134 - $arr[] = $this->get_translated_term_by( 'slug', $slug, 'post_tag' );
135 + $translated = $this->model->term->get_by( 'slug', $slug, $this->curlang, 'post_tag' );
136 + $arr[] = $translated ?: $slug;
135 137 }
136 138
137 139 $qv[ $key ] = $arr;
138 140 }
@@ -253,8 +255,12 @@
253 255 * @param array $tax_queries An array of tax queries.
254 256 * @return array Translated tax queries.
255 257 */
256 258 protected function translate_tax_query_recursive( $tax_queries ) {
259 + if ( empty( $this->curlang ) ) {
260 + return $tax_queries;
261 + }
262 +
257 263 foreach ( $tax_queries as $key => $q ) {
258 264 if ( ! is_array( $q ) ) {
259 265 continue;
260 266 }
@@ -261,10 +267,11 @@
261 267
262 268 if ( isset( $q['taxonomy'], $q['terms'] ) && $this->model->is_translated_taxonomy( $q['taxonomy'] ) ) {
263 269 $arr = array();
264 270 $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'] );
271 + foreach ( (array) $q['terms'] as $term ) {
272 + $translated = $this->model->term->get_by( $field, $term, $this->curlang, $q['taxonomy'] );
273 + $arr[] = $translated ?: $term;
267 274 }
268 275
269 276 $tax_queries[ $key ]['terms'] = $arr;
270 277 } else {
@@ -276,41 +283,8 @@
276 283 return $tax_queries;
277 284 }
278 285
279 286 /**
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 287 * Translates a list of term slugs provided either as an array or a string
314 288 * with slugs separated by a comma or a '+'.
315 289 *
316 290 * @since 3.2.8
@@ -328,14 +302,17 @@
328 302 $sep = strpos( $query_var, ',' ) !== false ? ',' : '+'; // Two possible separators.
329 303 $slugs = explode( $sep, $query_var );
330 304 }
331 305
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;
306 + if ( ! empty( $this->curlang ) ) {
307 + foreach ( $slugs as $key => $slug ) {
308 + if ( ! is_string( $slug ) ) {
309 + // We got an unexpected query var, let return it unchanged.
310 + return $query_var;
311 + }
312 + $translated = $this->model->term->get_by( 'slug', $slug, $this->curlang, $taxonomy );
313 + $slugs[ $key ] = $translated ?: $slug;
336 314 }
337 - $slug = $this->get_translated_term_by( 'slug', $slug, $taxonomy );
338 315 }
339 316
340 317 if ( ! empty( $sep ) ) {
341 318 return implode( $sep, $slugs );