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 +136 -74 3.03.7.7 View file →
@@ -30,9 +30,9 @@
30 30
31 31 /**
32 32 * Current language.
33 33 *
34 - * @var PLL_Language
34 + * @var PLL_Language|null
35 35 */
36 36 public $curlang;
37 37
38 38 /**
@@ -39,11 +39,13 @@
39 39 * Constructor: setups filters
40 40 *
41 41 * @since 1.4
42 42 *
43 - * @param object $polylang
43 + * @param object $polylang The Polylang object.
44 44 */
45 45 public function __construct( &$polylang ) {
46 + global $wp_version;
47 +
46 48 $this->links_model = &$polylang->links_model;
47 49 $this->model = &$polylang->model;
48 50 $this->options = &$polylang->options;
49 51 $this->curlang = &$polylang->curlang;
@@ -57,9 +59,13 @@
57 59 add_action( 'parse_comment_query', array( $this, 'parse_comment_query' ) );
58 60 add_filter( 'comments_clauses', array( $this, 'comments_clauses' ), 10, 2 );
59 61
60 62 // Filters the get_pages function according to the current language
61 - 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 );
62 68
63 69 // Rewrites next and previous post links to filter them by language
64 70 add_filter( 'get_previous_post_join', array( $this, 'posts_join' ), 10, 5 );
65 71 add_filter( 'get_next_post_join', array( $this, 'posts_join' ), 10, 5 );
@@ -65,14 +71,11 @@
65 71 add_filter( 'get_next_post_join', array( $this, 'posts_join' ), 10, 5 );
66 72 add_filter( 'get_previous_post_where', array( $this, 'posts_where' ), 10, 5 );
67 73 add_filter( 'get_next_post_where', array( $this, 'posts_where' ), 10, 5 );
68 74
69 - // Converts the locale to a valid W3C locale
75 + // Converts the locale to a valid W3C locale.
70 76 add_filter( 'language_attributes', array( $this, 'language_attributes' ) );
71 77
72 - // Prevents deleting all the translations of the default category
73 - add_filter( 'map_meta_cap', array( $this, 'fix_delete_default_category' ), 10, 4 );
74 -
75 78 // Translate the site title in emails sent to users
76 79 add_filter( 'password_change_email', array( $this, 'translate_user_email' ) );
77 80 add_filter( 'email_change_email', array( $this, 'translate_user_email' ) );
78 81
@@ -81,8 +84,11 @@
81 84 add_filter( 'map_meta_cap', array( $this, 'fix_privacy_policy_page_editing' ), 10, 4 );
82 85
83 86 // Personal data exporter
84 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.
85 91 }
86 92
87 93 /**
88 94 * Deletes the cache for multilingual sticky posts.
@@ -98,26 +104,40 @@
98 104 /**
99 105 * Get the language to filter a comments query.
100 106 *
101 107 * @since 2.0
108 + * @since 3.1 Always returns an array. Renamed from get_comments_queried_language().
102 109 *
103 - * @param WP_Comment_Query $query WP_Comment_Query object.
104 - * @return PLL_Language|false The language 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.
105 112 */
106 - protected function get_comments_queried_language( $query ) {
113 + protected function get_comments_queried_languages( $query ) {
107 114 // Don't filter comments if comment ids or post ids are specified.
108 115 $plucked = wp_array_slice_assoc( $query->query_vars, array( 'comment__in', 'parent', 'post_id', 'post__in', 'post_parent' ) );
109 116 $fields = array_filter( $plucked );
110 117 if ( ! empty( $fields ) ) {
111 - return false;
118 + return array();
112 119 }
113 120
114 121 // Don't filter comments if a non translated post type is specified.
115 122 if ( ! empty( $query->query_vars['post_type'] ) && ! $this->model->is_translated_post_type( $query->query_vars['post_type'] ) ) {
116 - return false;
123 + return array();
117 124 }
118 125
119 - 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();
120 140 }
121 141
122 142 /**
123 143 * Adds a language dependent cache domain when querying comments.
@@ -129,11 +149,12 @@
129 149 * @param WP_Comment_Query $query WP_Comment_Query object.
130 150 * @return void
131 151 */
132 152 public function parse_comment_query( $query ) {
133 - $lang = $this->get_comments_queried_language( $query );
134 - if ( $lang ) {
135 - $key = '_' . $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 );
136 157 $query->query_vars['cache_domain'] = empty( $query->query_vars['cache_domain'] ) ? 'pll' . $key : $query->query_vars['cache_domain'] . $key;
137 158 }
138 159 }
139 160
@@ -149,17 +170,19 @@
149 170 */
150 171 public function comments_clauses( $clauses, $query ) {
151 172 global $wpdb;
152 173
153 - $lang = $this->get_comments_queried_language( $query );
174 + $lang = $this->get_comments_queried_languages( $query );
154 175
155 176 if ( ! empty( $lang ) ) {
177 + $lang = wp_list_pluck( $lang, 'slug' );
178 +
156 179 // If this clause is not already added by WP.
157 - if ( ! strpos( $clauses['join'], '.ID' ) ) {
180 + if ( ! preg_match( "#JOIN\s+{$wpdb->posts}\s+ON\s+(({$wpdb->posts}\.)?ID|({$wpdb->comments}\.)?comment_post_ID)\s*=#", $clauses['join'] ) ) {
158 181 $clauses['join'] .= " JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
159 182 }
160 183
161 - $clauses['join'] .= $this->model->post->join_clause();
184 + $clauses['join'] .= $this->model->post->join_clause();
162 185 $clauses['where'] .= $this->model->post->where_clause( $lang );
163 186 }
164 187 return $clauses;
165 188 }
@@ -185,38 +208,23 @@
185 208 }
186 209
187 210 static $once = false;
188 211
189 - // Obliged to redo the get_pages query if we want to get the right number
190 212 if ( ! empty( $args['number'] ) && ! $once ) {
191 - $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.
192 215
193 - $r = array(
194 - 'lang' => 0, // So this query is not filtered
195 - 'numberposts' => -1,
196 - 'nopaging' => true,
197 - 'post_type' => $args['post_type'],
198 - 'fields' => 'ids',
199 - 'tax_query' => array(
200 - array(
201 - 'taxonomy' => 'language',
202 - 'field' => 'term_taxonomy_id', // Since WP 3.5
203 - 'terms' => $language->term_taxonomy_id,
204 - 'operator' => 'NOT IN',
205 - ),
206 - ),
207 - );
208 -
209 - // Take care that 'exclude' argument accepts integer or strings too
210 - $args['exclude'] = array_merge( wp_parse_id_list( $args['exclude'] ), get_posts( $r ) );
211 - $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;
212 220 }
213 221
214 222 $ids = wp_list_pluck( $pages, 'ID' );
215 223
216 - // Filters the queried list of pages by language
217 224 if ( ! $once ) {
218 - $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 ) );
219 227
220 228 foreach ( $pages as $key => $page ) {
221 229 if ( ! in_array( $page->ID, $ids ) ) {
222 230 unset( $pages[ $key ] );
@@ -222,19 +230,67 @@
222 230 unset( $pages[ $key ] );
223 231 }
224 232 }
225 233
226 - $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].
227 235 }
228 236
229 - // 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.
230 238 update_object_term_cache( $ids, $args['post_type'] );
231 239
232 - $once = false; // In case get_pages is called another time
240 + $once = false; // In case get_pages() is called another time.
233 241 return $pages;
234 242 }
235 243
236 244 /**
245 + * Filters the WP_Query in get_pages() per language.
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 + /**
237 293 * Modifies the sql request for get_adjacent_post to filter by the current language.
238 294 *
239 295 * @since 0.1
240 296 *
@@ -273,42 +329,20 @@
273 329 * @param string $output language attributes
274 330 * @return string
275 331 */
276 332 public function language_attributes( $output ) {
277 - if ( $language = $this->model->get_language( is_admin() ? get_user_locale() : get_locale() ) ) {
278 - $output = str_replace( '"' . get_bloginfo( 'language' ) . '"', '"' . $language->get_locale( 'display' ) . '"', $output );
279 - }
280 - return $output;
281 - }
333 + $language = $this->model->get_language( determine_locale() );
282 334
283 - /**
284 - * Prevents deleting all the translations of the default category
285 - *
286 - * @since 2.1
287 - *
288 - * @param array $caps The user's actual capabilities.
289 - * @param string $cap Capability name.
290 - * @param int $user_id The user ID.
291 - * @param array $args Adds the context to the cap. The category id.
292 - * @return array
293 - */
294 - public function fix_delete_default_category( $caps, $cap, $user_id, $args ) {
295 - if ( 'delete_term' === $cap ) {
296 - $term = get_term( reset( $args ) ); // Since WP 4.4, we can get the term to get the taxonomy
297 - if ( $term instanceof WP_Term ) {
298 - $default_cat = get_option( 'default_' . $term->taxonomy );
299 - if ( $default_cat && array_intersect( $args, $this->model->term->get_translations( $default_cat ) ) ) {
300 - $caps[] = 'do_not_allow';
301 - }
302 - }
335 + if ( ! $language ) {
336 + return $output;
303 337 }
304 338
305 - return $caps;
339 + return str_replace( '"' . get_bloginfo( 'language' ) . '"', '"' . $language->get_locale( 'display' ) . '"', $output );
306 340 }
307 341
308 342 /**
309 - * Translates the site title in emails sent to the user (change email, reset password).
310 - * It is necessary to filter the email because WP evaluates the site title before calling switch_to_locale().
343 + * Translates the site title in emails sent to the user (change email, reset password)
344 + * It is necessary to filter the email because WP evaluates the site title before calling switch_to_locale()
311 345 *
312 346 * @since 2.1.3
313 347 *
314 348 * @param string[] $email Email contents.
@@ -385,9 +419,9 @@
385 419 $user_data_to_export = array();
386 420
387 421 if ( $user = get_user_by( 'email', $email_address ) ) {
388 422 foreach ( $this->model->get_languages_list() as $lang ) {
389 - 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 ) ) {
390 424 $user_data_to_export[] = array(
391 425 /* translators: %s is a language native name */
392 426 'name' => sprintf( __( 'User description - %s', 'polylang' ), $lang->name ),
393 427 'value' => $value,
@@ -408,6 +442,34 @@
408 442 return array(
409 443 'data' => $data_to_export,
410 444 'done' => true,
411 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;
412 474 }
413 475 }