/wp-content/plugins/polylang/include/filters.php

https://gitlab.com/hop23typhu/bryepoxy · PHP · 180 lines · 89 code · 26 blank · 65 comment · 16 complexity · 9498d6b0e60081d2c2ff058d4b53afc5 MD5 · raw file

  1. <?php
  2. /**
  3. * Setup filters common to admin and frontend
  4. *
  5. * @since 1.4
  6. */
  7. class PLL_Filters {
  8. public $links_model, $model, $options, $curlang;
  9. /**
  10. * Constructor: setups filters
  11. *
  12. * @since 1.4
  13. *
  14. * @param object $polylang
  15. */
  16. public function __construct( &$polylang ) {
  17. $this->links_model = &$polylang->links_model;
  18. $this->model = &$polylang->model;
  19. $this->options = &$polylang->options;
  20. $this->curlang = &$polylang->curlang;
  21. // Filters the comments according to the current language
  22. add_action( 'parse_comment_query', array( $this, 'parse_comment_query' ) );
  23. add_filter( 'comments_clauses', array( $this, 'comments_clauses' ), 10, 2 );
  24. // Filters the get_pages function according to the current language
  25. add_filter( 'get_pages', array( $this, 'get_pages' ), 10, 2 );
  26. // Converts the locale to a valid W3C locale
  27. add_filter( 'language_attributes', array( $this, 'language_attributes' ) );
  28. }
  29. /**
  30. * Get the language to filter a comments query
  31. *
  32. * @since 2.0
  33. *
  34. * @param object $query
  35. * @return object|bool the language(s) to use in the filter, false otherwise
  36. */
  37. protected function get_comments_queried_language( $query ) {
  38. // Don't filter comments if comment ids or post ids are specified
  39. $plucked = wp_array_slice_assoc( $query->query_vars, array( 'comment__in', 'parent', 'post_id', 'post__in', 'post_parent' ) );
  40. $fields = array_filter( $plucked );
  41. if ( ! empty( $fields ) ) {
  42. return false;
  43. }
  44. // Don't filter comments if a non translated post type is specified
  45. if ( ! empty( $query->query_vars['post_type'] ) && ! $this->model->is_translated_post_type( $query->query_vars['post_type'] ) ) {
  46. return false;
  47. }
  48. return empty( $query->query_vars['lang'] ) ? $this->curlang : $this->model->get_language( $query->query_vars['lang'] );
  49. }
  50. /**
  51. * Adds language dependent cache domain when querying comments
  52. * Useful as the 'lang' parameter is not included in cache key by WordPress
  53. * Needed since WP 4.6 as comments have been added to persistent cache. See #36906, #37419
  54. *
  55. * @since 2.0
  56. *
  57. * @param object $query
  58. */
  59. public function parse_comment_query( $query ) {
  60. if ( $lang = $this->get_comments_queried_language( $query ) ) {
  61. $key = '_' . ( is_array( $lang ) ? implode( ',', $lang ) : $this->model->get_language( $lang )->slug );
  62. $query->query_vars['cache_domain'] = empty( $query->query_vars['cache_domain'] ) ? 'pll' . $key : $query->query_vars['cache_domain'] . $key;
  63. }
  64. }
  65. /**
  66. * Filters the comments according to the current language
  67. * Used by the recent comments widget and admin language filter
  68. *
  69. * @since 0.2
  70. *
  71. * @param array $clauses sql clauses
  72. * @param object $query WP_Comment_Query object
  73. * @return array modified $clauses
  74. */
  75. public function comments_clauses( $clauses, $query ) {
  76. global $wpdb;
  77. $lang = $this->get_comments_queried_language( $query );
  78. if ( ! empty( $lang ) ) {
  79. // If this clause is not already added by WP
  80. if ( ! strpos( $clauses['join'], '.ID' ) ) {
  81. $clauses['join'] .= " JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
  82. }
  83. $clauses['join'] .= $this->model->post->join_clause();
  84. $clauses['where'] .= $this->model->post->where_clause( $lang );
  85. }
  86. return $clauses;
  87. }
  88. /**
  89. * Filters get_pages per language
  90. *
  91. * @since 1.4
  92. *
  93. * @param array $pages an array of pages already queried
  94. * @param array $args get_pages arguments
  95. * @return array modified list of pages
  96. */
  97. public function get_pages( $pages, $args ) {
  98. if ( isset( $args['lang'] ) && empty( $args['lang'] ) ) {
  99. return $pages;
  100. }
  101. $language = empty( $args['lang'] ) ? $this->curlang : $this->model->get_language( $args['lang'] );
  102. if ( empty( $language ) || empty( $pages ) || ! $this->model->is_translated_post_type( $args['post_type'] ) ) {
  103. return $pages;
  104. }
  105. static $once = false;
  106. // Obliged to redo the get_pages query if we want to get the right number
  107. if ( ! empty( $args['number'] ) && ! $once ) {
  108. $once = true; // avoid infinite loop
  109. $r = array(
  110. 'lang' => 0, // So this query is not filtered
  111. 'numberposts' => -1,
  112. 'nopaging' => true,
  113. 'post_type' => $args['post_type'],
  114. 'fields' => 'ids',
  115. 'tax_query' => array( array(
  116. 'taxonomy' => 'language',
  117. 'field' => 'term_taxonomy_id', // Since WP 3.5
  118. 'terms' => $language->term_taxonomy_id,
  119. 'operator' => 'NOT IN',
  120. ) ),
  121. );
  122. $args['exclude'] = array_merge( $args['exclude'], get_posts( $r ) );
  123. $pages = get_pages( $args );
  124. }
  125. $ids = wp_list_pluck( $pages, 'ID' );
  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. foreach ( $pages as $key => $page ) {
  130. if ( ! in_array( $page->ID, $ids ) ) {
  131. unset( $pages[ $key ] );
  132. }
  133. }
  134. }
  135. // Not done by WP but extremely useful for performance when manipulating taxonomies
  136. update_object_term_cache( $ids, $args['post_type'] );
  137. $once = false; // In case get_pages is called another time
  138. return $pages;
  139. }
  140. /**
  141. * Converts WordPress locale to valid W3 locale in html language attributes
  142. *
  143. * @since 1.8
  144. *
  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. }
  152. return $output;
  153. }
  154. }