PLL_Frontend_Auto_Translate

Auto translates the posts and terms ids useful for example for themes querying a specific cat.

Defined (1)

The class is defined in the following location(s).

/frontend/frontend-auto-translate.php  
  1. class PLL_Frontend_Auto_Translate { 
  2. public $model, $curlang; 
  3.  
  4. /** 
  5. * constructor 
  6. * @since 1.1 
  7. * @param object $polylang 
  8. */ 
  9. public function __construct( &$polylang ) { 
  10. $this->model = &$polylang->model; 
  11. $this->curlang = &$polylang->curlang; 
  12.  
  13. add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) ); // after main Polylang filter 
  14. add_filter( 'get_terms_args', array( $this, 'get_terms_args' ), 10, 2 ); 
  15.  
  16. /** 
  17. * helper function to get the translated post in the current language 
  18. * since 1.8 
  19. * @param int $post_id 
  20. * @return int 
  21. */ 
  22. protected function get_post( $post_id ) { 
  23. return $this->model->post->get( $post_id, $this->curlang ); 
  24.  
  25. /** 
  26. * helper function to get the translated term in the current language 
  27. * since 1.8 
  28. * @param int $term_id 
  29. * @return int 
  30. */ 
  31. protected function get_term( $term_id ) { 
  32. return $this->model->term->get( $term_id, $this->curlang ); 
  33.  
  34. /** 
  35. * filters posts query to automatically translate included ids 
  36. * @since 1.1 
  37. * @param object $query WP_Query object 
  38. */ 
  39. public function pre_get_posts( $query ) { 
  40. global $wpdb; 
  41. $qv = &$query->query_vars; 
  42.  
  43. if ( $query->is_main_query() || isset( $qv['lang'] ) || ( ! empty( $qv['post_type'] ) && ! $this->model->is_translated_post_type( $qv['post_type'] ) ) ) { 
  44. return; 
  45.  
  46. // /!\ always keep untranslated as is 
  47.  
  48. // term ids separated by a comma 
  49. $arr = array(); 
  50. if ( ! empty( $qv['cat'] ) ) { 
  51. foreach ( explode( ', ', $qv['cat'] ) as $cat ) { 
  52. $tr = $this->get_term( abs( $cat ) ); 
  53. $arr[] = $cat < 0 ? -$tr : $tr; 
  54.  
  55. $qv['cat'] = implode( ', ', $arr ); 
  56.  
  57. // category_name 
  58. $arr = array(); 
  59. if ( ! empty( $qv['category_name'] ) ) { 
  60. foreach ( explode( ', ', $qv['category_name'] ) as $slug ) { 
  61. $arr[] = ( ( $cat = wpcom_vip_get_category_by_slug( $slug ) ) && ( $tr_id = $this->get_term( $cat->term_id ) ) && ! is_wp_error( $tr = get_category( $tr_id ) ) ) ? $tr->slug : $slug; 
  62.  
  63. $qv['category_name'] = implode( ', ', $arr ); 
  64.  
  65. // array of term ids 
  66. foreach ( array( 'category__and', 'category__in', 'category__not_in', 'tag__and', 'tag__in', 'tag__not_in' ) as $key ) { 
  67. $arr = array(); 
  68. if ( ! empty( $qv[ $key ] ) ) { 
  69. foreach ( $qv[ $key ] as $cat ) { 
  70. $arr[] = ( $tr = $this->get_term( $cat ) ) ? $tr : $cat; 
  71. $qv[ $key ] = $arr; 
  72.  
  73. // tag 
  74. $arr = array(); 
  75. if ( ! empty( $qv['tag'] ) ) { 
  76. $sep = strpos( $qv['tag'], ', ' ) !== false ? ', ' : '+'; // two possible separators for tag slugs 
  77. foreach ( explode( $sep, $qv['tag'] ) as $slug ) { 
  78. $arr[] = ( ( $tag = wpcom_vip_get_term_by( 'slug', $slug, 'post_tag' ) ) && ( $tr_id = $this->get_term( $tag->term_id ) ) && ! is_wp_error( $tr = get_tag( $tr_id ) ) ) ? $tr->slug : $slug; 
  79.  
  80. $qv['tag'] = implode( $sep, $arr ); 
  81.  
  82. // tag_id can only take one id 
  83. if ( ! empty( $qv['tag_id'] ) && $tr_id = $this->get_term( $qv['tag_id'] ) ) { 
  84. $qv['tag_id'] = $tr_id; 
  85.  
  86. // array of tag slugs 
  87. foreach ( array( 'tag_slug__and', 'tag_slug__in' ) as $key ) { 
  88. $arr = array(); 
  89. if ( ! empty( $qv[ $key ] ) ) { 
  90. foreach ( $qv[ $key ] as $slug ) { 
  91. $arr[] = ( ( $tag = wpcom_vip_get_term_by( 'slug', $slug, 'post_tag' ) ) && ( $tr_id = $this->get_term( $tag->term_id ) ) && ! is_wp_error( $tr = get_tag( $tr_id ) ) ) ? $tr->slug : $slug; 
  92.  
  93. $qv[ $key ] = $arr; 
  94.  
  95. // custom taxonomies 
  96. // according to codex, this type of query is deprecated as of WP 3.1 but it does not appear in WP 3.5 source code 
  97. foreach ( array_intersect( $this->model->get_translated_taxonomies(), get_taxonomies( array( '_builtin' => false ) ) ) as $taxonomy ) { 
  98. $tax = get_taxonomy( $taxonomy ); 
  99. $arr = array(); 
  100. if ( ! empty( $qv[ $tax->query_var ] ) ) { 
  101. $sep = strpos( $qv[ $tax->query_var ], ', ' ) !== false ? ', ' : '+'; // two possible separators 
  102. foreach ( explode( $sep, $qv[ $tax->query_var ] ) as $slug ) { 
  103. $arr[] = ( ( $tag = wpcom_vip_get_term_by( 'slug', $slug, $taxonomy ) ) && ( $tr_id = $this->get_term( $tag->term_id ) ) && ! is_wp_error( $tr = get_term( $tr_id, $taxonomy ) ) ) ? $tr->slug : $slug; 
  104.  
  105. $qv[ $tax->query_var ] = implode( $sep, $arr ); 
  106.  
  107. // tax_query since WP 3.1 
  108. if ( ! empty( $qv['tax_query'] ) && is_array( $qv['tax_query'] ) ) { 
  109. $qv['tax_query'] = $this->translate_tax_query_recursive( $qv['tax_query'] ); 
  110.  
  111. // p, page_id, post_parent can only take one id 
  112. foreach ( array( 'p', 'page_id', 'post_parent' ) as $key ) { 
  113. if ( ! empty( $qv[ $key ] ) && $tr_id = $this->get_post( $qv[ $key ] ) ) { 
  114. $qv[ $key ] = $tr_id; 
  115.  
  116. // name, pagename can only take one slug 
  117. foreach ( array( 'name', 'pagename' ) as $key ) { 
  118. if ( ! empty( $qv[ $key ] ) ) { 
  119. // no function to get post by name except get_posts itself 
  120. $post_type = empty( $qv['post_type'] ) ? 'post' : $qv['post_type']; 
  121. $id = $wpdb->get_var( $wpdb->prepare( "SELECT ID from $wpdb->posts WHERE post_type=%s AND post_name=%s", $post_type, $qv[ $key ] ) ); 
  122. $qv[ $key ] = ( $id && ( $tr_id = $this->get_post( $id ) ) && $tr = get_post( $tr_id ) ) ? $tr->post_name : $qv[ $key ]; 
  123.  
  124. // array of post ids 
  125. // post_parent__in & post_parent__not_in since WP 3.6 
  126. foreach ( array( 'post__in', 'post__not_in', 'post_parent__in', 'post_parent__not_in' ) as $key ) { 
  127. $arr = array(); 
  128. if ( ! empty( $qv[ $key ] ) ) { 
  129. // post__in used by the 2 functions below 
  130. // useless to filter them as output is already in the right language and would result in performance loss 
  131. foreach ( debug_backtrace() as $trace ) { 
  132. if ( in_array( $trace['function'], array( 'wp_nav_menu', 'gallery_shortcode' ) ) ) { 
  133. return; 
  134.  
  135. foreach ( $qv[ $key ] as $p ) { 
  136. $arr[] = ( $tr = $this->get_post( $p ) ) ? $tr : $p; 
  137.  
  138. $qv[ $key ] = $arr; 
  139.  
  140. /** 
  141. * filters terms query to automatically translate included ids 
  142. * @since 1.1.1 
  143. * @param array $args 
  144. * @param array $taxonomies 
  145. * @return array modified $args 
  146. */ 
  147. public function get_terms_args( $args, $taxonomies ) { 
  148. if ( ! empty( $args['include'] ) && $this->model->is_translated_taxonomy( $taxonomies ) ) { 
  149. foreach ( wp_parse_id_list( $args['include'] ) as $id ) { 
  150. $arr[] = ( $tr = $this->get_term( $id ) ) ? $tr : $id; 
  151.  
  152. $args['include'] = $arr; 
  153. return $args; 
  154.  
  155. /** 
  156. * translates tax queries 
  157. * compatible with nested tax queries introduced in WP 4.1 
  158. * @since 1.7 
  159. * @param array $tax_queries 
  160. * @return array translated tax queries 
  161. */ 
  162. protected function translate_tax_query_recursive( $tax_queries ) { 
  163. foreach ( $tax_queries as $key => $q ) { 
  164. if ( isset( $q['taxonomy'], $q['terms'] ) && $this->model->is_translated_taxonomy( $q['taxonomy'] ) ) { 
  165. $arr = array(); 
  166. $field = isset( $q['field'] ) && in_array( $q['field'], array( 'slug', 'name' ) ) ? $q['field'] : 'term_id'; 
  167. foreach ( (array) $q['terms'] as $t ) { 
  168. $arr[] = ( ( $tag = wpcom_vip_get_term_by( $field, $t, $q['taxonomy'] ) ) && ( $tr_id = $this->get_term( $tag->term_id ) ) && ! is_wp_error( $tr = get_term( $tr_id, $q['taxonomy'] ) ) ) ? $tr->$field : $t; 
  169.  
  170. $tax_queries[ $key ]['terms'] = $arr; 
  171.  
  172. // nested queries 
  173. elseif ( is_array( $q ) ) { 
  174. $tax_queries[ $key ] = $this->translate_tax_query_recursive( $q ); 
  175.  
  176. return $tax_queries;