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