PLL_Filters

Setup filters common to admin and frontend.

Defined (1)

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

/include/filters.php  
  1. class PLL_Filters { 
  2. public $links_model, $model, $options, $curlang; 
  3.  
  4. /** 
  5. * Constructor: setups filters 
  6. * @since 1.4 
  7. * @param object $polylang 
  8. */ 
  9. public function __construct( &$polylang ) { 
  10. $this->links_model = &$polylang->links_model; 
  11. $this->model = &$polylang->model; 
  12. $this->options = &$polylang->options; 
  13. $this->curlang = &$polylang->curlang; 
  14.  
  15. // Filters the comments according to the current language 
  16. add_action( 'parse_comment_query', array( $this, 'parse_comment_query' ) ); 
  17. add_filter( 'comments_clauses', array( $this, 'comments_clauses' ), 10, 2 ); 
  18.  
  19. // Filters the get_pages function according to the current language 
  20. add_filter( 'get_pages', array( $this, 'get_pages' ), 10, 2 ); 
  21.  
  22. // Converts the locale to a valid W3C locale 
  23. add_filter( 'language_attributes', array( $this, 'language_attributes' ) ); 
  24.  
  25. // Prevents deleting all the translations of the default category 
  26. add_filter( 'map_meta_cap', array( $this, 'fix_delete_default_category' ), 10, 4 ); 
  27.  
  28. // Translate the site title in emails sent to users 
  29. add_filter( 'password_change_email', array( $this, 'translate_user_email' ) ); 
  30. add_filter( 'email_change_email', array( $this, 'translate_user_email' ) ); 
  31.  
  32. /** 
  33. * Get the language to filter a comments query 
  34. * @since 2.0 
  35. * @param object $query 
  36. * @return object|bool the language(s) to use in the filter, false otherwise 
  37. */ 
  38. protected function get_comments_queried_language( $query ) { 
  39. // Don't filter comments if comment ids or post ids are specified 
  40. $plucked = wp_array_slice_assoc( $query->query_vars, array( 'comment__in', 'parent', 'post_id', 'post__in', 'post_parent' ) ); 
  41. $fields = array_filter( $plucked ); 
  42. if ( ! empty( $fields ) ) { 
  43. return false; 
  44.  
  45. // Don't filter comments if a non translated post type is specified 
  46. if ( ! empty( $query->query_vars['post_type'] ) && ! $this->model->is_translated_post_type( $query->query_vars['post_type'] ) ) { 
  47. return false; 
  48.  
  49. return empty( $query->query_vars['lang'] ) ? $this->curlang : $this->model->get_language( $query->query_vars['lang'] ); 
  50.  
  51. /** 
  52. * Adds language dependent cache domain when querying comments 
  53. * Useful as the 'lang' parameter is not included in cache key by WordPress 
  54. * Needed since WP 4.6 as comments have been added to persistent cache. See #36906, #37419 
  55. * @since 2.0 
  56. * @param object $query 
  57. */ 
  58. public function parse_comment_query( $query ) { 
  59. if ( $lang = $this->get_comments_queried_language( $query ) ) { 
  60. $key = '_' . ( is_array( $lang ) ? implode( ', ', $lang ) : $this->model->get_language( $lang )->slug ); 
  61. $query->query_vars['cache_domain'] = empty( $query->query_vars['cache_domain'] ) ? 'pll' . $key : $query->query_vars['cache_domain'] . $key; 
  62.  
  63. /** 
  64. * Filters the comments according to the current language 
  65. * Used by the recent comments widget and admin language filter 
  66. * @since 0.2 
  67. * @param array $clauses sql clauses 
  68. * @param object $query WP_Comment_Query object 
  69. * @return array modified $clauses 
  70. */ 
  71. public function comments_clauses( $clauses, $query ) { 
  72. global $wpdb; 
  73.  
  74. $lang = $this->get_comments_queried_language( $query ); 
  75.  
  76. if ( ! empty( $lang ) ) { 
  77. // If this clause is not already added by WP 
  78. if ( ! strpos( $clauses['join'], '.ID' ) ) { 
  79. $clauses['join'] .= " JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID"; 
  80.  
  81. $clauses['join'] .= $this->model->post->join_clause(); 
  82. $clauses['where'] .= $this->model->post->where_clause( $lang ); 
  83. return $clauses; 
  84.  
  85. /** 
  86. * Filters get_pages per language 
  87. * @since 1.4 
  88. * @param array $pages an array of pages already queried 
  89. * @param array $args get_pages arguments 
  90. * @return array modified list of pages 
  91. */ 
  92. public function get_pages( $pages, $args ) { 
  93. if ( isset( $args['lang'] ) && empty( $args['lang'] ) ) { 
  94. return $pages; 
  95.  
  96. $language = empty( $args['lang'] ) ? $this->curlang : $this->model->get_language( $args['lang'] ); 
  97.  
  98. if ( empty( $language ) || empty( $pages ) || ! $this->model->is_translated_post_type( $args['post_type'] ) ) { 
  99. return $pages; 
  100.  
  101. static $once = false; 
  102.  
  103. // Obliged to redo the get_pages query if we want to get the right number 
  104. if ( ! empty( $args['number'] ) && ! $once ) { 
  105. $once = true; // avoid infinite loop 
  106.  
  107. $r = array( 
  108. 'lang' => 0, // So this query is not filtered 
  109. 'numberposts' => -1,  
  110. 'nopaging' => true,  
  111. 'post_type' => $args['post_type'],  
  112. 'fields' => 'ids',  
  113. 'tax_query' => array( array( 
  114. 'taxonomy' => 'language',  
  115. 'field' => 'term_taxonomy_id', // Since WP 3.5 
  116. 'terms' => $language->term_taxonomy_id,  
  117. 'operator' => 'NOT IN',  
  118. ) ),  
  119. ); 
  120.  
  121. $args['exclude'] = array_merge( $args['exclude'], get_posts( $r ) ); 
  122. $pages = get_pages( $args ); 
  123.  
  124. $ids = wp_list_pluck( $pages, 'ID' ); 
  125.  
  126. // Filters the queried list of pages by language 
  127. if ( ! $once ) { 
  128. $ids = array_intersect( $ids, $this->model->post->get_objects_in_language( $language ) ); 
  129.  
  130. foreach ( $pages as $key => $page ) { 
  131. if ( ! in_array( $page->ID, $ids ) ) { 
  132. unset( $pages[ $key ] ); 
  133.  
  134. $pages = array_values( $pages ); // In case 3rd parties suppose the existence of $pages[0] 
  135.  
  136. // Not done by WP but extremely useful for performance when manipulating taxonomies 
  137. update_object_term_cache( $ids, $args['post_type'] ); 
  138.  
  139. $once = false; // In case get_pages is called another time 
  140. return $pages; 
  141.  
  142. /** 
  143. * Converts WordPress locale to valid W3 locale in html language attributes 
  144. * @since 1.8 
  145. * @param string $output language attributes 
  146. * @return string 
  147. */ 
  148. public function language_attributes( $output ) { 
  149. if ( $language = $this->model->get_language( get_locale() ) ) { 
  150. $output = str_replace( '"' . get_bloginfo( 'language' ) . '"', '"' . $language->get_locale( 'display' ) . '"', $output ); 
  151. return $output; 
  152.  
  153.  
  154. /** 
  155. * Prevents deleting all the translations of the default category 
  156. * @since 2.1 
  157. * @param array $caps The user's actual capabilities. 
  158. * @param string $cap Capability name. 
  159. * @param int $user_id The user ID. 
  160. * @param array $args Adds the context to the cap. The category id. 
  161. * @return array 
  162. */ 
  163. public function fix_delete_default_category( $caps, $cap, $user_id, $args ) { 
  164. if ( 'delete_term' === $cap && array_intersect( $args, $this->model->term->get_translations( get_option( 'default_category' ) ) ) ) { 
  165. $caps[] = 'do_not_allow'; 
  166.  
  167. return $caps; 
  168.  
  169. /** 
  170. * Translates the site title in emails sent to the user (change email, reset password) 
  171. * It is necessary to filter the email because WP evaluates the site title before calling switch_to_locale() 
  172. * @since 2.1.3 
  173. * @param array $email 
  174. * @return array 
  175. */ 
  176. function translate_user_email( $email ) { 
  177. $blog_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); 
  178. $email['subject'] = sprintf( $email['subject'], $blog_name ); 
  179. $email['message'] = str_replace( '###SITENAME###', $blog_name, $email['message'] ); 
  180. return $email;