PluginProbe
Polylang / 3.1.4
Polylang v3.1.4
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 +102 -68 2.73.1.4 View file →
@@ -1,5 +1,8 @@
1 1 <?php
2 +/**
3 + * @package Polylang
4 + */
2 5
3 6 /**
4 7 * Setup filters common to admin and frontend
5 8 *
@@ -5,11 +8,35 @@
5 8 *
6 9 * @since 1.4
7 10 */
8 11 class PLL_Filters {
9 - public $links_model, $model, $options, $curlang;
12 + /**
13 + * Stores the plugin options.
14 + *
15 + * @var array
16 + */
17 + public $options;
10 18
11 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
35 + */
36 + public $curlang;
37 +
38 + /**
12 39 * Constructor: setups filters
13 40 *
14 41 * @since 1.4
15 42 *
@@ -20,8 +47,13 @@
20 47 $this->model = &$polylang->model;
21 48 $this->options = &$polylang->options;
22 49 $this->curlang = &$polylang->curlang;
23 50
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 +
24 56 // Filters the comments according to the current language
25 57 add_action( 'parse_comment_query', array( $this, 'parse_comment_query' ) );
26 58 add_filter( 'comments_clauses', array( $this, 'comments_clauses' ), 10, 2 );
27 59
@@ -36,11 +68,8 @@
36 68
37 69 // Converts the locale to a valid W3C locale
38 70 add_filter( 'language_attributes', array( $this, 'language_attributes' ) );
39 71
40 - // Prevents deleting all the translations of the default category
41 - add_filter( 'map_meta_cap', array( $this, 'fix_delete_default_category' ), 10, 4 );
42 -
43 72 // Translate the site title in emails sent to users
44 73 add_filter( 'password_change_email', array( $this, 'translate_user_email' ) );
45 74 add_filter( 'email_change_email', array( $this, 'translate_user_email' ) );
46 75
@@ -52,64 +81,94 @@
52 81 add_filter( 'wp_privacy_personal_data_exporters', array( $this, 'register_personal_data_exporter' ), 0 ); // Since WP 4.9.6
53 82 }
54 83
55 84 /**
56 - * Get the language to filter a comments query
85 + * Deletes the cache for multilingual sticky posts.
57 86 *
87 + * @since 2.8.4
88 + *
89 + * @return void
90 + */
91 + public function delete_sticky_posts_cache() {
92 + wp_cache_delete( 'sticky_posts', 'options' );
93 + }
94 +
95 + /**
96 + * Get the language to filter a comments query.
97 + *
58 98 * @since 2.0
99 + * @since 3.1 Always returns an array. Renamed from get_comments_queried_language().
59 100 *
60 - * @param object $query
61 - * @return object|bool the language(s) to use in the filter, false otherwise
101 + * @param WP_Comment_Query $query WP_Comment_Query object.
102 + * @return PLL_Language[] The languages to use in the filter.
62 103 */
63 - protected function get_comments_queried_language( $query ) {
64 - // Don't filter comments if comment ids or post ids are specified
104 + protected function get_comments_queried_languages( $query ) {
105 + // Don't filter comments if comment ids or post ids are specified.
65 106 $plucked = wp_array_slice_assoc( $query->query_vars, array( 'comment__in', 'parent', 'post_id', 'post__in', 'post_parent' ) );
66 107 $fields = array_filter( $plucked );
67 108 if ( ! empty( $fields ) ) {
68 - return false;
109 + return array();
69 110 }
70 111
71 - // Don't filter comments if a non translated post type is specified
112 + // Don't filter comments if a non translated post type is specified.
72 113 if ( ! empty( $query->query_vars['post_type'] ) && ! $this->model->is_translated_post_type( $query->query_vars['post_type'] ) ) {
73 - return false;
114 + return array();
74 115 }
75 116
76 - return empty( $query->query_vars['lang'] ) ? $this->curlang : $this->model->get_language( $query->query_vars['lang'] );
117 + // If comments are queried with a 'lang' parameter, keeps only language codes.
118 + if ( isset( $query->query_vars['lang'] ) ) {
119 + $languages = is_string( $query->query_vars['lang'] ) ? explode( ',', $query->query_vars['lang'] ) : $query->query_vars['lang'];
120 + if ( is_array( $languages ) ) {
121 + $languages = array_map( array( $this->model, 'get_language' ), $languages );
122 + return array_filter( $languages );
123 + }
124 + }
125 +
126 + if ( ! empty( $this->curlang ) ) {
127 + return array( $this->curlang );
128 + }
129 +
130 + return array();
77 131 }
78 132
79 133 /**
80 - * Adds language dependent cache domain when querying comments
81 - * Useful as the 'lang' parameter is not included in cache key by WordPress
82 - * Needed since WP 4.6 as comments have been added to persistent cache. See #36906, #37419
134 + * Adds a language dependent cache domain when querying comments.
135 + * Useful as the 'lang' parameter is not included in cache key by WordPress.
136 + * Needed since WP 4.6 as comments have been added to persistent cache. See #36906, #37419.
83 137 *
84 138 * @since 2.0
85 139 *
86 - * @param object $query
140 + * @param WP_Comment_Query $query WP_Comment_Query object.
141 + * @return void
87 142 */
88 143 public function parse_comment_query( $query ) {
89 - if ( $lang = $this->get_comments_queried_language( $query ) ) {
90 - $key = '_' . ( is_array( $lang ) ? implode( ',', $lang ) : $this->model->get_language( $lang )->slug );
144 + $lang = $this->get_comments_queried_languages( $query );
145 + if ( ! empty( $lang ) ) {
146 + $lang = wp_list_pluck( $lang, 'slug' );
147 + $key = '_' . implode( ',', $lang );
91 148 $query->query_vars['cache_domain'] = empty( $query->query_vars['cache_domain'] ) ? 'pll' . $key : $query->query_vars['cache_domain'] . $key;
92 149 }
93 150 }
94 151
95 152 /**
96 - * Filters the comments according to the current language
97 - * Used by the recent comments widget and admin language filter
153 + * Filters the comments according to the current language.
154 + * Used by the recent comments widget and admin language filter.
98 155 *
99 156 * @since 0.2
100 157 *
101 - * @param array $clauses sql clauses
102 - * @param object $query WP_Comment_Query object
103 - * @return array modified $clauses
158 + * @param string[] $clauses SQL clauses.
159 + * @param WP_Comment_Query $query WP_Comment_Query object.
160 + * @return string[] Modified $clauses.
104 161 */
105 162 public function comments_clauses( $clauses, $query ) {
106 163 global $wpdb;
107 164
108 - $lang = $this->get_comments_queried_language( $query );
165 + $lang = $this->get_comments_queried_languages( $query );
109 166
110 167 if ( ! empty( $lang ) ) {
111 - // If this clause is not already added by WP
168 + $lang = wp_list_pluck( $lang, 'slug' );
169 +
170 + // If this clause is not already added by WP.
112 171 if ( ! strpos( $clauses['join'], '.ID' ) ) {
113 172 $clauses['join'] .= " JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
114 173 }
115 174
@@ -119,15 +178,15 @@
119 178 return $clauses;
120 179 }
121 180
122 181 /**
123 - * Filters get_pages per language
182 + * Filters get_pages() per language.
124 183 *
125 184 * @since 1.4
126 185 *
127 - * @param array $pages an array of pages already queried
128 - * @param array $args get_pages arguments
129 - * @return array modified list of pages
186 + * @param WP_Post[] $pages An array of pages already queried.
187 + * @param array $args Array of get_pages() arguments.
188 + * @return WP_Post[] Modified list of pages.
130 189 */
131 190 public function get_pages( $pages, $args ) {
132 191 if ( isset( $args['lang'] ) && empty( $args['lang'] ) ) {
133 192 return $pages;
@@ -160,10 +219,10 @@
160 219 ),
161 220 ),
162 221 );
163 222
164 - // Take care that 'exclude' argument accepts integer or strings too
165 - $args['exclude'] = array_merge( wp_parse_id_list( $args['exclude'] ), get_posts( $r ) );
223 + // Take care that 'exclude' argument accepts integer or strings too.
224 + $args['exclude'] = array_merge( wp_parse_id_list( $args['exclude'] ), get_posts( $r ) ); // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude
166 225 $pages = get_pages( $args );
167 226 }
168 227
169 228 $ids = wp_list_pluck( $pages, 'ID' );
@@ -188,36 +247,36 @@
188 247 return $pages;
189 248 }
190 249
191 250 /**
192 - * Modifies the sql request for get_adjacent_post to filter by the current language
251 + * Modifies the sql request for get_adjacent_post to filter by the current language.
193 252 *
194 253 * @since 0.1
195 254 *
196 255 * @param string $sql The JOIN clause in the SQL.
197 256 * @param bool $in_same_term Whether post should be in a same taxonomy term.
198 - * @param array $excluded_terms Array of excluded term IDs.
257 + * @param int[] $excluded_terms Array of excluded term IDs.
199 258 * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true.
200 259 * @param WP_Post $post WP_Post object.
201 - * @return string modified JOIN clause
260 + * @return string Modified JOIN clause.
202 261 */
203 - public function posts_join( $sql, $in_same_term, $excluded_terms, $taxonomy = '', $post = null ) {
262 + public function posts_join( $sql, $in_same_term, $excluded_terms, $taxonomy, $post ) {
204 263 return $this->model->is_translated_post_type( $post->post_type ) && ! empty( $this->curlang ) ? $sql . $this->model->post->join_clause( 'p' ) : $sql;
205 264 }
206 265
207 266 /**
208 - * Modifies the sql request for wp_get_archives and get_adjacent_post to filter by the current language
267 + * Modifies the sql request for wp_get_archives and get_adjacent_post to filter by the current language.
209 268 *
210 269 * @since 0.1
211 270 *
212 271 * @param string $sql The WHERE clause in the SQL.
213 272 * @param bool $in_same_term Whether post should be in a same taxonomy term.
214 - * @param array $excluded_terms Array of excluded term IDs.
273 + * @param int[] $excluded_terms Array of excluded term IDs.
215 274 * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true.
216 275 * @param WP_Post $post WP_Post object.
217 - * @return string modified WHERE clause
276 + * @return string Modified WHERE clause.
218 277 */
219 - public function posts_where( $sql, $in_same_term, $excluded_terms, $taxonomy = '', $post = null ) {
278 + public function posts_where( $sql, $in_same_term, $excluded_terms, $taxonomy, $post ) {
220 279 return $this->model->is_translated_post_type( $post->post_type ) && ! empty( $this->curlang ) ? $sql . $this->model->post->where_clause( $this->curlang ) : $sql;
221 280 }
222 281
223 282 /**
@@ -235,40 +294,15 @@
235 294 return $output;
236 295 }
237 296
238 297 /**
239 - * Prevents deleting all the translations of the default category
240 - *
241 - * @since 2.1
242 - *
243 - * @param array $caps The user's actual capabilities.
244 - * @param string $cap Capability name.
245 - * @param int $user_id The user ID.
246 - * @param array $args Adds the context to the cap. The category id.
247 - * @return array
248 - */
249 - public function fix_delete_default_category( $caps, $cap, $user_id, $args ) {
250 - if ( 'delete_term' === $cap ) {
251 - $term = get_term( reset( $args ) ); // Since WP 4.4, we can get the term to get the taxonomy
252 - if ( $term instanceof WP_Term ) {
253 - $default_cat = get_option( 'default_' . $term->taxonomy );
254 - if ( $default_cat && array_intersect( $args, $this->model->term->get_translations( $default_cat ) ) ) {
255 - $caps[] = 'do_not_allow';
256 - }
257 - }
258 - }
259 -
260 - return $caps;
261 - }
262 -
263 - /**
264 298 * Translates the site title in emails sent to the user (change email, reset password)
265 299 * It is necessary to filter the email because WP evaluates the site title before calling switch_to_locale()
266 300 *
267 301 * @since 2.1.3
268 302 *
269 - * @param array $email
270 - * @return array
303 + * @param string[] $email Email contents.
304 + * @return string[] Translated email contents.
271 305 */
272 306 public function translate_user_email( $email ) {
273 307 $blog_name = wp_specialchars_decode( pll__( get_option( 'blogname' ) ), ENT_QUOTES );
274 308 $email['subject'] = sprintf( $email['subject'], $blog_name );
@@ -315,9 +349,9 @@
315 349 *
316 350 * @since 2.3.6
317 351 *
318 352 * @param array $exporters Personal data exporters
319 - * @retun array
353 + * @return array
320 354 */
321 355 public function register_personal_data_exporter( $exporters ) {
322 356 $exporters[] = array(
323 357 'exporter_friendly_name' => __( 'Translated user descriptions', 'polylang' ),