PluginProbe
Polylang / 2.3.6
Polylang v2.3.6
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 +42 -109 3.0.22.3.6 View file →
@@ -1,8 +1,5 @@
1 1 <?php
2 -/**
3 - * @package Polylang
4 - */
5 2
6 3 /**
7 4 * Setup filters common to admin and frontend
8 5 *
@@ -8,35 +5,11 @@
8 5 *
9 6 * @since 1.4
10 7 */
11 8 class PLL_Filters {
12 - /**
13 - * Stores the plugin options.
14 - *
15 - * @var array
16 - */
17 - public $options;
9 + public $links_model, $model, $options, $curlang;
18 10
19 11 /**
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
35 - */
36 - public $curlang;
37 -
38 - /**
39 12 * Constructor: setups filters
40 13 *
41 14 * @since 1.4
42 15 *
@@ -47,13 +20,8 @@
47 20 $this->model = &$polylang->model;
48 21 $this->options = &$polylang->options;
49 22 $this->curlang = &$polylang->curlang;
50 23
51 - // Deletes our cache for sticky posts when the list is updated.
52 - add_action( 'update_option_sticky_posts', array( $this, 'delete_sticky_posts_cache' ) );
53 - add_action( 'add_option_sticky_posts', array( $this, 'delete_sticky_posts_cache' ) );
54 - add_action( 'delete_option_sticky_posts', array( $this, 'delete_sticky_posts_cache' ) );
55 -
56 24 // Filters the comments according to the current language
57 25 add_action( 'parse_comment_query', array( $this, 'parse_comment_query' ) );
58 26 add_filter( 'comments_clauses', array( $this, 'comments_clauses' ), 10, 2 );
59 27
@@ -77,9 +45,8 @@
77 45 add_filter( 'email_change_email', array( $this, 'translate_user_email' ) );
78 46
79 47 // Translates the privacy policy page
80 48 add_filter( 'option_wp_page_for_privacy_policy', array( $this, 'translate_page_for_privacy_policy' ), 20 ); // Since WP 4.9.6
81 - add_filter( 'map_meta_cap', array( $this, 'fix_privacy_policy_page_editing' ), 10, 4 );
82 49
83 50 // Personal data exporter
84 51 add_filter( 'wp_privacy_personal_data_exporters', array( $this, 'register_personal_data_exporter' ), 0 ); // Since WP 4.9.6
85 52 }
@@ -84,28 +51,17 @@
84 51 add_filter( 'wp_privacy_personal_data_exporters', array( $this, 'register_personal_data_exporter' ), 0 ); // Since WP 4.9.6
85 52 }
86 53
87 54 /**
88 - * Deletes the cache for multilingual sticky posts.
55 + * Get the language to filter a comments query
89 56 *
90 - * @since 2.8.4
91 - *
92 - * @return void
93 - */
94 - public function delete_sticky_posts_cache() {
95 - wp_cache_delete( 'sticky_posts', 'options' );
96 - }
97 -
98 - /**
99 - * Get the language to filter a comments query.
100 - *
101 57 * @since 2.0
102 58 *
103 - * @param WP_Comment_Query $query WP_Comment_Query object.
104 - * @return PLL_Language|false The language to use in the filter, false otherwise.
59 + * @param object $query
60 + * @return object|bool the language(s) to use in the filter, false otherwise
105 61 */
106 62 protected function get_comments_queried_language( $query ) {
107 - // Don't filter comments if comment ids or post ids are specified.
63 + // Don't filter comments if comment ids or post ids are specified
108 64 $plucked = wp_array_slice_assoc( $query->query_vars, array( 'comment__in', 'parent', 'post_id', 'post__in', 'post_parent' ) );
109 65 $fields = array_filter( $plucked );
110 66 if ( ! empty( $fields ) ) {
111 67 return false;
@@ -110,9 +66,9 @@
110 66 if ( ! empty( $fields ) ) {
111 67 return false;
112 68 }
113 69
114 - // Don't filter comments if a non translated post type is specified.
70 + // Don't filter comments if a non translated post type is specified
115 71 if ( ! empty( $query->query_vars['post_type'] ) && ! $this->model->is_translated_post_type( $query->query_vars['post_type'] ) ) {
116 72 return false;
117 73 }
118 74
@@ -119,34 +75,32 @@
119 75 return empty( $query->query_vars['lang'] ) ? $this->curlang : $this->model->get_language( $query->query_vars['lang'] );
120 76 }
121 77
122 78 /**
123 - * Adds a language dependent cache domain when querying comments.
124 - * Useful as the 'lang' parameter is not included in cache key by WordPress.
125 - * Needed since WP 4.6 as comments have been added to persistent cache. See #36906, #37419.
79 + * Adds language dependent cache domain when querying comments
80 + * Useful as the 'lang' parameter is not included in cache key by WordPress
81 + * Needed since WP 4.6 as comments have been added to persistent cache. See #36906, #37419
126 82 *
127 83 * @since 2.0
128 84 *
129 - * @param WP_Comment_Query $query WP_Comment_Query object.
130 - * @return void
85 + * @param object $query
131 86 */
132 87 public function parse_comment_query( $query ) {
133 - $lang = $this->get_comments_queried_language( $query );
134 - if ( $lang ) {
135 - $key = '_' . $lang->slug;
88 + if ( $lang = $this->get_comments_queried_language( $query ) ) {
89 + $key = '_' . ( is_array( $lang ) ? implode( ',', $lang ) : $this->model->get_language( $lang )->slug );
136 90 $query->query_vars['cache_domain'] = empty( $query->query_vars['cache_domain'] ) ? 'pll' . $key : $query->query_vars['cache_domain'] . $key;
137 91 }
138 92 }
139 93
140 94 /**
141 - * Filters the comments according to the current language.
142 - * Used by the recent comments widget and admin language filter.
95 + * Filters the comments according to the current language
96 + * Used by the recent comments widget and admin language filter
143 97 *
144 98 * @since 0.2
145 99 *
146 - * @param string[] $clauses SQL clauses.
147 - * @param WP_Comment_Query $query WP_Comment_Query object.
148 - * @return string[] Modified $clauses.
100 + * @param array $clauses sql clauses
101 + * @param object $query WP_Comment_Query object
102 + * @return array modified $clauses
149 103 */
150 104 public function comments_clauses( $clauses, $query ) {
151 105 global $wpdb;
152 106
@@ -152,9 +106,9 @@
152 106
153 107 $lang = $this->get_comments_queried_language( $query );
154 108
155 109 if ( ! empty( $lang ) ) {
156 - // If this clause is not already added by WP.
110 + // If this clause is not already added by WP
157 111 if ( ! strpos( $clauses['join'], '.ID' ) ) {
158 112 $clauses['join'] .= " JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
159 113 }
160 114
@@ -164,15 +118,15 @@
164 118 return $clauses;
165 119 }
166 120
167 121 /**
168 - * Filters get_pages() per language.
122 + * Filters get_pages per language
169 123 *
170 124 * @since 1.4
171 125 *
172 - * @param WP_Post[] $pages An array of pages already queried.
173 - * @param array $args Array of get_pages() arguments.
174 - * @return WP_Post[] Modified list of pages.
126 + * @param array $pages an array of pages already queried
127 + * @param array $args get_pages arguments
128 + * @return array modified list of pages
175 129 */
176 130 public function get_pages( $pages, $args ) {
177 131 if ( isset( $args['lang'] ) && empty( $args['lang'] ) ) {
178 132 return $pages;
@@ -190,9 +144,9 @@
190 144 if ( ! empty( $args['number'] ) && ! $once ) {
191 145 $once = true; // avoid infinite loop
192 146
193 147 $r = array(
194 - 'lang' => 0, // So this query is not filtered
148 + 'lang' => 0, // So this query is not filtered
195 149 'numberposts' => -1,
196 150 'nopaging' => true,
197 151 'post_type' => $args['post_type'],
198 152 'fields' => 'ids',
@@ -233,36 +187,36 @@
233 187 return $pages;
234 188 }
235 189
236 190 /**
237 - * Modifies the sql request for get_adjacent_post to filter by the current language.
191 + * Modifies the sql request for get_adjacent_post to filter by the current language
238 192 *
239 193 * @since 0.1
240 194 *
241 195 * @param string $sql The JOIN clause in the SQL.
242 196 * @param bool $in_same_term Whether post should be in a same taxonomy term.
243 - * @param int[] $excluded_terms Array of excluded term IDs.
197 + * @param array $excluded_terms Array of excluded term IDs.
244 198 * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true.
245 199 * @param WP_Post $post WP_Post object.
246 - * @return string Modified JOIN clause.
200 + * @return string modified JOIN clause
247 201 */
248 - public function posts_join( $sql, $in_same_term, $excluded_terms, $taxonomy, $post ) {
202 + public function posts_join( $sql, $in_same_term, $excluded_terms, $taxonomy = '', $post = null ) {
249 203 return $this->model->is_translated_post_type( $post->post_type ) && ! empty( $this->curlang ) ? $sql . $this->model->post->join_clause( 'p' ) : $sql;
250 204 }
251 205
252 206 /**
253 - * Modifies the sql request for wp_get_archives and get_adjacent_post to filter by the current language.
207 + * Modifies the sql request for wp_get_archives and get_adjacent_post to filter by the current language
254 208 *
255 209 * @since 0.1
256 210 *
257 211 * @param string $sql The WHERE clause in the SQL.
258 212 * @param bool $in_same_term Whether post should be in a same taxonomy term.
259 - * @param int[] $excluded_terms Array of excluded term IDs.
213 + * @param array $excluded_terms Array of excluded term IDs.
260 214 * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true.
261 215 * @param WP_Post $post WP_Post object.
262 - * @return string Modified WHERE clause.
216 + * @return string modified WHERE clause
263 217 */
264 - public function posts_where( $sql, $in_same_term, $excluded_terms, $taxonomy, $post ) {
218 + public function posts_where( $sql, $in_same_term, $excluded_terms, $taxonomy = '', $post = null ) {
265 219 return $this->model->is_translated_post_type( $post->post_type ) && ! empty( $this->curlang ) ? $sql . $this->model->post->where_clause( $this->curlang ) : $sql;
266 220 }
267 221
268 222 /**
@@ -273,14 +227,15 @@
273 227 * @param string $output language attributes
274 228 * @return string
275 229 */
276 230 public function language_attributes( $output ) {
277 - if ( $language = $this->model->get_language( is_admin() ? get_user_locale() : get_locale() ) ) {
231 + if ( $language = $this->model->get_language( get_locale() ) ) {
278 232 $output = str_replace( '"' . get_bloginfo( 'language' ) . '"', '"' . $language->get_locale( 'display' ) . '"', $output );
279 233 }
280 234 return $output;
281 235 }
282 236
237 +
283 238 /**
284 239 * Prevents deleting all the translations of the default category
285 240 *
286 241 * @since 2.1
@@ -305,15 +260,15 @@
305 260 return $caps;
306 261 }
307 262
308 263 /**
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().
264 + * Translates the site title in emails sent to the user (change email, reset password)
265 + * It is necessary to filter the email because WP evaluates the site title before calling switch_to_locale()
311 266 *
312 267 * @since 2.1.3
313 268 *
314 - * @param string[] $email Email contents.
315 - * @return string[] Translated email contents.
269 + * @param array $email
270 + * @return array
316 271 */
317 272 public function translate_user_email( $email ) {
318 273 $blog_name = wp_specialchars_decode( pll__( get_option( 'blogname' ) ), ENT_QUOTES );
319 274 $email['subject'] = sprintf( $email['subject'], $blog_name );
@@ -333,36 +288,14 @@
333 288 return empty( $this->curlang ) ? $id : $this->model->post->get( $id, $this->curlang );
334 289 }
335 290
336 291 /**
337 - * Prevents edit and delete links for the translations of the privacy policy page for non admin
338 - *
339 - * @since 2.3.7
340 - *
341 - * @param array $caps The user's actual capabilities.
342 - * @param string $cap Capability name.
343 - * @param int $user_id The user ID.
344 - * @param array $args Adds the context to the cap. The category id.
345 - * @return array
346 - */
347 - public function fix_privacy_policy_page_editing( $caps, $cap, $user_id, $args ) {
348 - if ( in_array( $cap, array( 'edit_page', 'edit_post', 'delete_page', 'delete_post' ) ) ) {
349 - $privacy_page = get_option( 'wp_page_for_privacy_policy' );
350 - if ( $privacy_page && array_intersect( $args, $this->model->post->get_translations( $privacy_page ) ) ) {
351 - $caps = array_merge( $caps, map_meta_cap( 'manage_privacy_options', $user_id ) );
352 - }
353 - }
354 -
355 - return $caps;
356 - }
357 -
358 - /**
359 292 * Register our personal data exporter
360 293 *
361 294 * @since 2.3.6
362 295 *
363 296 * @param array $exporters Personal data exporters
364 - * @return array
297 + * @retun array
365 298 */
366 299 public function register_personal_data_exporter( $exporters ) {
367 300 $exporters[] = array(
368 301 'exporter_friendly_name' => __( 'Translated user descriptions', 'polylang' ),
@@ -379,12 +312,12 @@
379 312 * @param string $email_address User email address
380 313 * @return array Personal data
381 314 */
382 315 public function user_data_exporter( $email_address ) {
383 - $email_address = trim( $email_address );
384 - $data_to_export = array();
385 - $user_data_to_export = array();
316 + $email_address = trim( $email_address );
386 317
318 + $data_to_export = array();
319 +
387 320 if ( $user = get_user_by( 'email', $email_address ) ) {
388 321 foreach ( $this->model->get_languages_list() as $lang ) {
389 322 if ( $lang->slug !== $this->options['default_lang'] && $value = get_user_meta( $user->ID, 'description_' . $lang->slug, true ) ) {
390 323 $user_data_to_export[] = array(
@@ -397,9 +330,9 @@
397 330
398 331 if ( ! empty( $user_data_to_export ) ) {
399 332 $data_to_export[] = array(
400 333 'group_id' => 'user',
401 - 'group_label' => __( 'User', 'polylang' ),
334 + 'group_label' => __( 'User' ),
402 335 'item_id' => "user-{$user->ID}",
403 336 'data' => $user_data_to_export,
404 337 );
405 338 }