PluginProbe
Polylang / 2.0.3
Polylang v2.0.3
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 +9 -200 2.82.0.3 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 *
@@ -30,30 +27,10 @@
30 27
31 28 // Filters the get_pages function according to the current language
32 29 add_filter( 'get_pages', array( $this, 'get_pages' ), 10, 2 );
33 30
34 - // Rewrites next and previous post links to filter them by language
35 - add_filter( 'get_previous_post_join', array( $this, 'posts_join' ), 10, 5 );
36 - add_filter( 'get_next_post_join', array( $this, 'posts_join' ), 10, 5 );
37 - add_filter( 'get_previous_post_where', array( $this, 'posts_where' ), 10, 5 );
38 - add_filter( 'get_next_post_where', array( $this, 'posts_where' ), 10, 5 );
39 -
40 31 // Converts the locale to a valid W3C locale
41 32 add_filter( 'language_attributes', array( $this, 'language_attributes' ) );
42 -
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 - // Translate the site title in emails sent to users
47 - add_filter( 'password_change_email', array( $this, 'translate_user_email' ) );
48 - add_filter( 'email_change_email', array( $this, 'translate_user_email' ) );
49 -
50 - // Translates the privacy policy page
51 - add_filter( 'option_wp_page_for_privacy_policy', array( $this, 'translate_page_for_privacy_policy' ), 20 ); // Since WP 4.9.6
52 - add_filter( 'map_meta_cap', array( $this, 'fix_privacy_policy_page_editing' ), 10, 4 );
53 -
54 - // Personal data exporter
55 - add_filter( 'wp_privacy_personal_data_exporters', array( $this, 'register_personal_data_exporter' ), 0 ); // Since WP 4.9.6
56 33 }
57 34
58 35 /**
59 36 * Get the language to filter a comments query
@@ -148,25 +125,22 @@
148 125 if ( ! empty( $args['number'] ) && ! $once ) {
149 126 $once = true; // avoid infinite loop
150 127
151 128 $r = array(
152 - 'lang' => 0, // So this query is not filtered
129 + 'lang' => 0, // So this query is not filtered
153 130 'numberposts' => -1,
154 131 'nopaging' => true,
155 132 'post_type' => $args['post_type'],
156 133 'fields' => 'ids',
157 - 'tax_query' => array(
158 - array(
159 - 'taxonomy' => 'language',
160 - 'field' => 'term_taxonomy_id', // Since WP 3.5
161 - 'terms' => $language->term_taxonomy_id,
162 - 'operator' => 'NOT IN',
163 - ),
164 - ),
134 + 'tax_query' => array( array(
135 + 'taxonomy' => 'language',
136 + 'field' => 'term_taxonomy_id', // Since WP 3.5
137 + 'terms' => $language->term_taxonomy_id,
138 + 'operator' => 'NOT IN',
139 + ) ),
165 140 );
166 141
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 ) );
142 + $args['exclude'] = array_merge( $args['exclude'], get_posts( $r ) );
169 143 $pages = get_pages( $args );
170 144 }
171 145
172 146 $ids = wp_list_pluck( $pages, 'ID' );
@@ -179,10 +153,8 @@
179 153 if ( ! in_array( $page->ID, $ids ) ) {
180 154 unset( $pages[ $key ] );
181 155 }
182 156 }
183 -
184 - $pages = array_values( $pages ); // In case 3rd parties suppose the existence of $pages[0]
185 157 }
186 158
187 159 // Not done by WP but extremely useful for performance when manipulating taxonomies
188 160 update_object_term_cache( $ids, $args['post_type'] );
@@ -191,40 +163,8 @@
191 163 return $pages;
192 164 }
193 165
194 166 /**
195 - * Modifies the sql request for get_adjacent_post to filter by the current language
196 - *
197 - * @since 0.1
198 - *
199 - * @param string $sql The JOIN clause in the SQL.
200 - * @param bool $in_same_term Whether post should be in a same taxonomy term.
201 - * @param array $excluded_terms Array of excluded term IDs.
202 - * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true.
203 - * @param WP_Post $post WP_Post object.
204 - * @return string modified JOIN clause
205 - */
206 - public function posts_join( $sql, $in_same_term, $excluded_terms, $taxonomy = '', $post = null ) {
207 - return $this->model->is_translated_post_type( $post->post_type ) && ! empty( $this->curlang ) ? $sql . $this->model->post->join_clause( 'p' ) : $sql;
208 - }
209 -
210 - /**
211 - * Modifies the sql request for wp_get_archives and get_adjacent_post to filter by the current language
212 - *
213 - * @since 0.1
214 - *
215 - * @param string $sql The WHERE clause in the SQL.
216 - * @param bool $in_same_term Whether post should be in a same taxonomy term.
217 - * @param array $excluded_terms Array of excluded term IDs.
218 - * @param string $taxonomy Taxonomy. Used to identify the term used when `$in_same_term` is true.
219 - * @param WP_Post $post WP_Post object.
220 - * @return string modified WHERE clause
221 - */
222 - public function posts_where( $sql, $in_same_term, $excluded_terms, $taxonomy = '', $post = null ) {
223 - return $this->model->is_translated_post_type( $post->post_type ) && ! empty( $this->curlang ) ? $sql . $this->model->post->where_clause( $this->curlang ) : $sql;
224 - }
225 -
226 - /**
227 167 * Converts WordPress locale to valid W3 locale in html language attributes
228 168 *
229 169 * @since 1.8
230 170 *
@@ -231,141 +171,10 @@
231 171 * @param string $output language attributes
232 172 * @return string
233 173 */
234 174 public function language_attributes( $output ) {
235 - if ( $language = $this->model->get_language( is_admin() ? get_user_locale() : get_locale() ) ) {
175 + if ( $language = $this->model->get_language( get_locale() ) ) {
236 176 $output = str_replace( '"' . get_bloginfo( 'language' ) . '"', '"' . $language->get_locale( 'display' ) . '"', $output );
237 177 }
238 178 return $output;
239 - }
240 -
241 - /**
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 - * Translates the site title in emails sent to the user (change email, reset password)
268 - * It is necessary to filter the email because WP evaluates the site title before calling switch_to_locale()
269 - *
270 - * @since 2.1.3
271 - *
272 - * @param array $email
273 - * @return array
274 - */
275 - public function translate_user_email( $email ) {
276 - $blog_name = wp_specialchars_decode( pll__( get_option( 'blogname' ) ), ENT_QUOTES );
277 - $email['subject'] = sprintf( $email['subject'], $blog_name );
278 - $email['message'] = str_replace( '###SITENAME###', $blog_name, $email['message'] );
279 - return $email;
280 - }
281 -
282 - /**
283 - * Translates the privacy policy page, on both frontend and admin
284 - *
285 - * @since 2.3.6
286 - *
287 - * @param int $id Privacy policy page id
288 - * @return int
289 - */
290 - public function translate_page_for_privacy_policy( $id ) {
291 - return empty( $this->curlang ) ? $id : $this->model->post->get( $id, $this->curlang );
292 - }
293 -
294 - /**
295 - * Prevents edit and delete links for the translations of the privacy policy page for non admin
296 - *
297 - * @since 2.3.7
298 - *
299 - * @param array $caps The user's actual capabilities.
300 - * @param string $cap Capability name.
301 - * @param int $user_id The user ID.
302 - * @param array $args Adds the context to the cap. The category id.
303 - * @return array
304 - */
305 - public function fix_privacy_policy_page_editing( $caps, $cap, $user_id, $args ) {
306 - if ( in_array( $cap, array( 'edit_page', 'edit_post', 'delete_page', 'delete_post' ) ) ) {
307 - $privacy_page = get_option( 'wp_page_for_privacy_policy' );
308 - if ( $privacy_page && array_intersect( $args, $this->model->post->get_translations( $privacy_page ) ) ) {
309 - $caps = array_merge( $caps, map_meta_cap( 'manage_privacy_options', $user_id ) );
310 - }
311 - }
312 -
313 - return $caps;
314 - }
315 -
316 - /**
317 - * Register our personal data exporter
318 - *
319 - * @since 2.3.6
320 - *
321 - * @param array $exporters Personal data exporters
322 - * @retun array
323 - */
324 - public function register_personal_data_exporter( $exporters ) {
325 - $exporters[] = array(
326 - 'exporter_friendly_name' => __( 'Translated user descriptions', 'polylang' ),
327 - 'callback' => array( $this, 'user_data_exporter' ),
328 - );
329 - return $exporters;
330 - }
331 -
332 - /**
333 - * Export translated user description as WP exports only the description in the default language
334 - *
335 - * @since 2.3.6
336 - *
337 - * @param string $email_address User email address
338 - * @return array Personal data
339 - */
340 - public function user_data_exporter( $email_address ) {
341 - $email_address = trim( $email_address );
342 - $data_to_export = array();
343 - $user_data_to_export = array();
344 -
345 - if ( $user = get_user_by( 'email', $email_address ) ) {
346 - foreach ( $this->model->get_languages_list() as $lang ) {
347 - if ( $lang->slug !== $this->options['default_lang'] && $value = get_user_meta( $user->ID, 'description_' . $lang->slug, true ) ) {
348 - $user_data_to_export[] = array(
349 - /* translators: %s is a language native name */
350 - 'name' => sprintf( __( 'User description - %s', 'polylang' ), $lang->name ),
351 - 'value' => $value,
352 - );
353 - }
354 - }
355 -
356 - if ( ! empty( $user_data_to_export ) ) {
357 - $data_to_export[] = array(
358 - 'group_id' => 'user',
359 - 'group_label' => __( 'User', 'polylang' ),
360 - 'item_id' => "user-{$user->ID}",
361 - 'data' => $user_data_to_export,
362 - );
363 - }
364 - }
365 -
366 - return array(
367 - 'data' => $data_to_export,
368 - 'done' => true,
369 - );
370 179 }
371 180 }