PluginProbe
Polylang / 3.7.7
Polylang v3.7.7
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 | include/filters.php +189 -99 2.93.7.7 View file →
@@ -8,18 +8,44 @@
8 8 *
9 9 * @since 1.4
10 10 */
11 11 class PLL_Filters {
12 - public $links_model, $model, $options, $curlang;
12 + /**
13 + * Stores the plugin options.
14 + *
15 + * @var array
16 + */
17 + public $options;
13 18
14 19 /**
20 + * @var PLL_Model
21 + */
22 + public $model;
23 +
24 + /**
25 + * Instance of a child class of PLL_Links_Model.
26 + *
27 + * @var PLL_Links_Model
28 + */
29 + public $links_model;
30 +
31 + /**
32 + * Current language.
33 + *
34 + * @var PLL_Language|null
35 + */
36 + public $curlang;
37 +
38 + /**
15 39 * Constructor: setups filters
16 40 *
17 41 * @since 1.4
18 42 *
19 - * @param object $polylang
43 + * @param object $polylang The Polylang object.
20 44 */
21 45 public function __construct( &$polylang ) {
46 + global $wp_version;
47 +
22 48 $this->links_model = &$polylang->links_model;
23 49 $this->model = &$polylang->model;
24 50 $this->options = &$polylang->options;
25 51 $this->curlang = &$polylang->curlang;
@@ -33,9 +59,13 @@
33 59 add_action( 'parse_comment_query', array( $this, 'parse_comment_query' ) );
34 60 add_filter( 'comments_clauses', array( $this, 'comments_clauses' ), 10, 2 );
35 61
36 62 // Filters the get_pages function according to the current language
37 - add_filter( 'get_pages', array( $this, 'get_pages' ), 10, 2 );
63 + if ( version_compare( $wp_version, '6.3-alpha', '<' ) ) {
64 + // Backward compatibility with WP < 6.3.
65 + add_filter( 'get_pages', array( $this, 'get_pages' ), 10, 2 );
66 + }
67 + add_filter( 'get_pages_query_args', array( $this, 'get_pages_query_args' ), 10, 2 );
38 68
39 69 // Rewrites next and previous post links to filter them by language
40 70 add_filter( 'get_previous_post_join', array( $this, 'posts_join' ), 10, 5 );
41 71 add_filter( 'get_next_post_join', array( $this, 'posts_join' ), 10, 5 );
@@ -41,14 +71,11 @@
41 71 add_filter( 'get_next_post_join', array( $this, 'posts_join' ), 10, 5 );
42 72 add_filter( 'get_previous_post_where', array( $this, 'posts_where' ), 10, 5 );
43 73 add_filter( 'get_next_post_where', array( $this, 'posts_where' ), 10, 5 );
44 74
45 - // Converts the locale to a valid W3C locale
75 + // Converts the locale to a valid W3C locale.
46 76 add_filter( 'language_attributes', array( $this, 'language_attributes' ) );
47 77
48 - // Prevents deleting all the translations of the default category
49 - add_filter( 'map_meta_cap', array( $this, 'fix_delete_default_category' ), 10, 4 );
50 -
51 78 // Translate the site title in emails sent to users
52 79 add_filter( 'password_change_email', array( $this, 'translate_user_email' ) );
53 80 add_filter( 'email_change_email', array( $this, 'translate_user_email' ) );
54 81
@@ -57,8 +84,11 @@
57 84 add_filter( 'map_meta_cap', array( $this, 'fix_privacy_policy_page_editing' ), 10, 4 );
58 85
59 86 // Personal data exporter
60 87 add_filter( 'wp_privacy_personal_data_exporters', array( $this, 'register_personal_data_exporter' ), 0 ); // Since WP 4.9.6
88 +
89 + // Fix for `term_exists()`.
90 + add_filter( 'term_exists_default_query_args', array( $this, 'term_exists_default_query_args' ), 0, 3 ); // Since WP 6.0.0.
61 91 }
62 92
63 93 /**
64 94 * Deletes the cache for multilingual sticky posts.
@@ -63,8 +93,10 @@
63 93 /**
64 94 * Deletes the cache for multilingual sticky posts.
65 95 *
66 96 * @since 2.8.4
97 + *
98 + * @return void
67 99 */
68 100 public function delete_sticky_posts_cache() {
69 101 wp_cache_delete( 'sticky_posts', 'options' );
70 102 }
@@ -69,69 +101,88 @@
69 101 wp_cache_delete( 'sticky_posts', 'options' );
70 102 }
71 103
72 104 /**
73 - * Get the language to filter a comments query
105 + * Get the language to filter a comments query.
74 106 *
75 107 * @since 2.0
108 + * @since 3.1 Always returns an array. Renamed from get_comments_queried_language().
76 109 *
77 - * @param object $query
78 - * @return object|bool the language(s) to use in the filter, false otherwise
110 + * @param WP_Comment_Query $query WP_Comment_Query object.
111 + * @return PLL_Language[] The languages to use in the filter.
79 112 */
80 - protected function get_comments_queried_language( $query ) {
81 - // Don't filter comments if comment ids or post ids are specified
113 + protected function get_comments_queried_languages( $query ) {
114 + // Don't filter comments if comment ids or post ids are specified.
82 115 $plucked = wp_array_slice_assoc( $query->query_vars, array( 'comment__in', 'parent', 'post_id', 'post__in', 'post_parent' ) );
83 116 $fields = array_filter( $plucked );
84 117 if ( ! empty( $fields ) ) {
85 - return false;
118 + return array();
86 119 }
87 120
88 - // Don't filter comments if a non translated post type is specified
121 + // Don't filter comments if a non translated post type is specified.
89 122 if ( ! empty( $query->query_vars['post_type'] ) && ! $this->model->is_translated_post_type( $query->query_vars['post_type'] ) ) {
90 - return false;
123 + return array();
91 124 }
92 125
93 - return empty( $query->query_vars['lang'] ) ? $this->curlang : $this->model->get_language( $query->query_vars['lang'] );
126 + // If comments are queried with a 'lang' parameter, keeps only language codes.
127 + if ( isset( $query->query_vars['lang'] ) ) {
128 + $languages = is_string( $query->query_vars['lang'] ) ? explode( ',', $query->query_vars['lang'] ) : $query->query_vars['lang'];
129 + if ( is_array( $languages ) ) {
130 + $languages = array_map( array( $this->model, 'get_language' ), $languages );
131 + return array_filter( $languages );
132 + }
133 + }
134 +
135 + if ( ! empty( $this->curlang ) ) {
136 + return array( $this->curlang );
137 + }
138 +
139 + return array();
94 140 }
95 141
96 142 /**
97 - * Adds language dependent cache domain when querying comments
98 - * Useful as the 'lang' parameter is not included in cache key by WordPress
99 - * Needed since WP 4.6 as comments have been added to persistent cache. See #36906, #37419
143 + * Adds a language dependent cache domain when querying comments.
144 + * Useful as the 'lang' parameter is not included in cache key by WordPress.
145 + * Needed since WP 4.6 as comments have been added to persistent cache. See #36906, #37419.
100 146 *
101 147 * @since 2.0
102 148 *
103 - * @param object $query
149 + * @param WP_Comment_Query $query WP_Comment_Query object.
150 + * @return void
104 151 */
105 152 public function parse_comment_query( $query ) {
106 - if ( $lang = $this->get_comments_queried_language( $query ) ) {
107 - $key = '_' . ( is_array( $lang ) ? implode( ',', $lang ) : $this->model->get_language( $lang )->slug );
153 + $lang = $this->get_comments_queried_languages( $query );
154 + if ( ! empty( $lang ) ) {
155 + $lang = wp_list_pluck( $lang, 'slug' );
156 + $key = '_' . implode( ',', $lang );
108 157 $query->query_vars['cache_domain'] = empty( $query->query_vars['cache_domain'] ) ? 'pll' . $key : $query->query_vars['cache_domain'] . $key;
109 158 }
110 159 }
111 160
112 161 /**
113 - * Filters the comments according to the current language
114 - * Used by the recent comments widget and admin language filter
162 + * Filters the comments according to the current language.
163 + * Used by the recent comments widget and admin language filter.
115 164 *
116 165 * @since 0.2
117 166 *
118 - * @param array $clauses sql clauses
119 - * @param object $query WP_Comment_Query object
120 - * @return array modified $clauses
167 + * @param string[] $clauses SQL clauses.
168 + * @param WP_Comment_Query $query WP_Comment_Query object.
169 + * @return string[] Modified $clauses.
121 170 */
122 171 public function comments_clauses( $clauses, $query ) {
123 172 global $wpdb;
124 173
125 - $lang = $this->get_comments_queried_language( $query );
174 + $lang = $this->get_comments_queried_languages( $query );
126 175
127 176 if ( ! empty( $lang ) ) {
128 - // If this clause is not already added by WP
129 - if ( ! strpos( $clauses['join'], '.ID' ) ) {
177 + $lang = wp_list_pluck( $lang, 'slug' );
178 +
179 + // If this clause is not already added by WP.
180 + if ( ! preg_match( "#JOIN\s+{$wpdb->posts}\s+ON\s+(({$wpdb->posts}\.)?ID|({$wpdb->comments}\.)?comment_post_ID)\s*=#", $clauses['join'] ) ) {
130 181 $clauses['join'] .= " JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
131 182 }
132 183
133 - $clauses['join'] .= $this->model->post->join_clause();
184 + $clauses['join'] .= $this->model->post->join_clause();
134 185 $clauses['where'] .= $this->model->post->where_clause( $lang );
135 186 }
136 187 return $clauses;
137 188 }
@@ -136,15 +187,15 @@
136 187 return $clauses;
137 188 }
138 189
139 190 /**
140 - * Filters get_pages per language
191 + * Filters get_pages() per language.
141 192 *
142 193 * @since 1.4
143 194 *
144 - * @param array $pages an array of pages already queried
145 - * @param array $args get_pages arguments
146 - * @return array modified list of pages
195 + * @param WP_Post[] $pages An array of pages already queried.
196 + * @param array $args Array of get_pages() arguments.
197 + * @return WP_Post[] Modified list of pages.
147 198 */
148 199 public function get_pages( $pages, $args ) {
149 200 if ( isset( $args['lang'] ) && empty( $args['lang'] ) ) {
150 201 return $pages;
@@ -157,38 +208,23 @@
157 208 }
158 209
159 210 static $once = false;
160 211
161 - // Obliged to redo the get_pages query if we want to get the right number
162 212 if ( ! empty( $args['number'] ) && ! $once ) {
163 - $once = true; // avoid infinite loop
213 + // We are obliged to redo the get_pages() query if we want to get the right number.
214 + $once = true; // Avoid infinite loop.
164 215
165 - $r = array(
166 - 'lang' => 0, // So this query is not filtered
167 - 'numberposts' => -1,
168 - 'nopaging' => true,
169 - 'post_type' => $args['post_type'],
170 - 'fields' => 'ids',
171 - 'tax_query' => array(
172 - array(
173 - 'taxonomy' => 'language',
174 - 'field' => 'term_taxonomy_id', // Since WP 3.5
175 - 'terms' => $language->term_taxonomy_id,
176 - 'operator' => 'NOT IN',
177 - ),
178 - ),
179 - );
180 -
181 - // Take care that 'exclude' argument accepts integer or strings too
182 - $args['exclude'] = array_merge( wp_parse_id_list( $args['exclude'] ), get_posts( $r ) );
183 - $pages = get_pages( $args );
216 + // Take care that 'exclude' argument accepts integer or strings too.
217 + $args['exclude'] = array_merge( wp_parse_id_list( $args['exclude'] ), $this->get_related_page_ids( $language, 'NOT IN', $args ) ); // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude
218 + $numbered_pages = get_pages( $args );
219 + $pages = ! $numbered_pages ? $pages : $numbered_pages;
184 220 }
185 221
186 222 $ids = wp_list_pluck( $pages, 'ID' );
187 223
188 - // Filters the queried list of pages by language
189 224 if ( ! $once ) {
190 - $ids = array_intersect( $ids, $this->model->post->get_objects_in_language( $language ) );
225 + // Filters the queried list of pages by language.
226 + $ids = array_intersect( $ids, $this->get_related_page_ids( $language, 'IN', $args ) );
191 227
192 228 foreach ( $pages as $key => $page ) {
193 229 if ( ! in_array( $page->ID, $ids ) ) {
194 230 unset( $pages[ $key ] );
@@ -194,47 +230,95 @@
194 230 unset( $pages[ $key ] );
195 231 }
196 232 }
197 233
198 - $pages = array_values( $pages ); // In case 3rd parties suppose the existence of $pages[0]
234 + $pages = array_values( $pages ); // In case 3rd parties suppose the existence of $pages[0].
199 235 }
200 236
201 - // Not done by WP but extremely useful for performance when manipulating taxonomies
237 + // Not done by WP but extremely useful for performance when manipulating taxonomies.
202 238 update_object_term_cache( $ids, $args['post_type'] );
203 239
204 - $once = false; // In case get_pages is called another time
240 + $once = false; // In case get_pages() is called another time.
205 241 return $pages;
206 242 }
207 243
208 244 /**
209 - * Modifies the sql request for get_adjacent_post to filter by the current language
245 + * Filters the WP_Query in get_pages() per language.
210 246 *
247 + * @since 3.4.3
248 + *
249 + * @param array $query_args Array of arguments passed to WP_Query.
250 + * @param array $parsed_args Array of get_pages() arguments.
251 + * @return array Array of arguments passed to WP_Query with the language.
252 + */
253 + public function get_pages_query_args( $query_args, $parsed_args ) {
254 + if ( isset( $parsed_args['lang'] ) ) {
255 + $query_args['lang'] = $parsed_args['lang'];
256 + }
257 +
258 + return $query_args;
259 + }
260 +
261 + /**
262 + * Get page ids related to a get_pages() in or not in a given language.
263 + *
264 + * @since 3.2
265 + *
266 + * @param PLL_Language $language The language to use in the relationship
267 + * @param string $relation 'IN' or 'NOT IN'.
268 + * @param array $args Array of get_pages() arguments.
269 + * @return int[]
270 + */
271 + protected function get_related_page_ids( $language, $relation, $args ) {
272 + $r = array(
273 + 'lang' => '', // Ensure this query is not filtered.
274 + 'numberposts' => -1,
275 + 'nopaging' => true,
276 + 'post_type' => $args['post_type'],
277 + 'post_status' => $args['post_status'],
278 + 'fields' => 'ids',
279 + 'tax_query' => array(
280 + array(
281 + 'taxonomy' => 'language',
282 + 'field' => 'term_taxonomy_id', // Since WP 3.5.
283 + 'terms' => $language->get_tax_prop( 'language', 'term_taxonomy_id' ),
284 + 'operator' => $relation,
285 + ),
286 + ),
287 + );
288 +
289 + return get_posts( $r );
290 + }
291 +
292 + /**
293 + * Modifies the sql request for get_adjacent_post to filter by the current language.
294 + *
211 295 * @since 0.1
212 296 *
213 297 * @param string $sql The JOIN clause in the SQL.
214 298 * @param bool $in_same_term Whether post should be in a same taxonomy term.
215 - * @param array $excluded_terms Array of excluded term IDs.
299 + * @param int[] $excluded_terms Array of excluded term IDs.
216 300 * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true.
217 301 * @param WP_Post $post WP_Post object.
218 - * @return string modified JOIN clause
302 + * @return string Modified JOIN clause.
219 303 */
220 - public function posts_join( $sql, $in_same_term, $excluded_terms, $taxonomy = '', $post = null ) {
304 + public function posts_join( $sql, $in_same_term, $excluded_terms, $taxonomy, $post ) {
221 305 return $this->model->is_translated_post_type( $post->post_type ) && ! empty( $this->curlang ) ? $sql . $this->model->post->join_clause( 'p' ) : $sql;
222 306 }
223 307
224 308 /**
225 - * Modifies the sql request for wp_get_archives and get_adjacent_post to filter by the current language
309 + * Modifies the sql request for wp_get_archives and get_adjacent_post to filter by the current language.
226 310 *
227 311 * @since 0.1
228 312 *
229 313 * @param string $sql The WHERE clause in the SQL.
230 314 * @param bool $in_same_term Whether post should be in a same taxonomy term.
231 - * @param array $excluded_terms Array of excluded term IDs.
315 + * @param int[] $excluded_terms Array of excluded term IDs.
232 316 * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true.
233 317 * @param WP_Post $post WP_Post object.
234 - * @return string modified WHERE clause
318 + * @return string Modified WHERE clause.
235 319 */
236 - public function posts_where( $sql, $in_same_term, $excluded_terms, $taxonomy = '', $post = null ) {
320 + public function posts_where( $sql, $in_same_term, $excluded_terms, $taxonomy, $post ) {
237 321 return $this->model->is_translated_post_type( $post->post_type ) && ! empty( $this->curlang ) ? $sql . $this->model->post->where_clause( $this->curlang ) : $sql;
238 322 }
239 323
240 324 /**
@@ -245,37 +329,15 @@
245 329 * @param string $output language attributes
246 330 * @return string
247 331 */
248 332 public function language_attributes( $output ) {
249 - if ( $language = $this->model->get_language( is_admin() ? get_user_locale() : get_locale() ) ) {
250 - $output = str_replace( '"' . get_bloginfo( 'language' ) . '"', '"' . $language->get_locale( 'display' ) . '"', $output );
251 - }
252 - return $output;
253 - }
333 + $language = $this->model->get_language( determine_locale() );
254 334
255 - /**
256 - * Prevents deleting all the translations of the default category
257 - *
258 - * @since 2.1
259 - *
260 - * @param array $caps The user's actual capabilities.
261 - * @param string $cap Capability name.
262 - * @param int $user_id The user ID.
263 - * @param array $args Adds the context to the cap. The category id.
264 - * @return array
265 - */
266 - public function fix_delete_default_category( $caps, $cap, $user_id, $args ) {
267 - if ( 'delete_term' === $cap ) {
268 - $term = get_term( reset( $args ) ); // Since WP 4.4, we can get the term to get the taxonomy
269 - if ( $term instanceof WP_Term ) {
270 - $default_cat = get_option( 'default_' . $term->taxonomy );
271 - if ( $default_cat && array_intersect( $args, $this->model->term->get_translations( $default_cat ) ) ) {
272 - $caps[] = 'do_not_allow';
273 - }
274 - }
335 + if ( ! $language ) {
336 + return $output;
275 337 }
276 338
277 - return $caps;
339 + return str_replace( '"' . get_bloginfo( 'language' ) . '"', '"' . $language->get_locale( 'display' ) . '"', $output );
278 340 }
279 341
280 342 /**
281 343 * Translates the site title in emails sent to the user (change email, reset password)
@@ -282,10 +344,10 @@
282 344 * It is necessary to filter the email because WP evaluates the site title before calling switch_to_locale()
283 345 *
284 346 * @since 2.1.3
285 347 *
286 - * @param array $email
287 - * @return array
348 + * @param string[] $email Email contents.
349 + * @return string[] Translated email contents.
288 350 */
289 351 public function translate_user_email( $email ) {
290 352 $blog_name = wp_specialchars_decode( pll__( get_option( 'blogname' ) ), ENT_QUOTES );
291 353 $email['subject'] = sprintf( $email['subject'], $blog_name );
@@ -332,9 +394,9 @@
332 394 *
333 395 * @since 2.3.6
334 396 *
335 397 * @param array $exporters Personal data exporters
336 - * @retun array
398 + * @return array
337 399 */
338 400 public function register_personal_data_exporter( $exporters ) {
339 401 $exporters[] = array(
340 402 'exporter_friendly_name' => __( 'Translated user descriptions', 'polylang' ),
@@ -357,9 +419,9 @@
357 419 $user_data_to_export = array();
358 420
359 421 if ( $user = get_user_by( 'email', $email_address ) ) {
360 422 foreach ( $this->model->get_languages_list() as $lang ) {
361 - if ( $lang->slug !== $this->options['default_lang'] && $value = get_user_meta( $user->ID, 'description_' . $lang->slug, true ) ) {
423 + if ( ! $lang->is_default && $value = get_user_meta( $user->ID, 'description_' . $lang->slug, true ) ) {
362 424 $user_data_to_export[] = array(
363 425 /* translators: %s is a language native name */
364 426 'name' => sprintf( __( 'User description - %s', 'polylang' ), $lang->name ),
365 427 'value' => $value,
@@ -380,6 +442,34 @@
380 442 return array(
381 443 'data' => $data_to_export,
382 444 'done' => true,
383 445 );
446 + }
447 +
448 + /**
449 + * Filters default query arguments for checking if a term exists.
450 + * In `term_exists()`, WP 6.0 uses `get_terms()`, which is filtered by language by Polylang.
451 + * This filter prevents `term_exists()` to be filtered by language.
452 + *
453 + * @since 3.2
454 + *
455 + * @param array $defaults An array of arguments passed to get_terms().
456 + * @param int|string $term The term to check. Accepts term ID, slug, or name.
457 + * @param string $taxonomy The taxonomy name to use. An empty string indicates the search is against all taxonomies.
458 + * @return array
459 + */
460 + public function term_exists_default_query_args( $defaults, $term, $taxonomy ) {
461 + if ( ! empty( $taxonomy ) && ! $this->model->is_translated_taxonomy( $taxonomy ) ) {
462 + return $defaults;
463 + }
464 +
465 + if ( ! is_array( $defaults ) ) {
466 + $defaults = array();
467 + }
468 +
469 + if ( ! isset( $defaults['lang'] ) ) {
470 + $defaults['lang'] = '';
471 + }
472 +
473 + return $defaults;
384 474 }
385 475 }