PluginProbe
Polylang / 2.0.2
Polylang v2.0.2
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
polylang / include / filters.php

filters.php in Polylang 2.0.2, at include/filters.php

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