PluginProbe
Polylang / 3.6.7
Polylang v3.6.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
polylang / include / filters-sanitization.php

filters-sanitization.php in Polylang 3.6.7, at include/filters-sanitization.php

107 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Polylang
4 */
5
6 /**
7 * Setup specific filters useful for sanitization.
8 *
9 * Extract from PLL_Admin_Filters to be able to use in a REST API context.
10 *
11 * @since 2.9
12 */
13 class PLL_Filters_Sanitization {
14 /**
15 * Language used for the sanitization depending on the context.
16 *
17 * @var string $locale
18 */
19 public $locale;
20
21 /**
22 * Constructor: setups filters and actions.
23 *
24 * @since 2.9
25 *
26 * @param string $locale Locale code of the language.
27 */
28 public function __construct( $locale ) {
29 $this->locale = $locale;
30
31 // We need specific filters for some languages like German and Danish
32 $specific_locales = array( 'da_DK', 'de_DE', 'de_DE_formal', 'de_CH', 'de_CH_informal', 'ca', 'sr_RS', 'bs_BA' );
33 if ( in_array( $locale, $specific_locales ) ) {
34 add_filter( 'sanitize_title', array( $this, 'sanitize_title' ), 10, 3 );
35 add_filter( 'sanitize_user', array( $this, 'sanitize_user' ), 10, 3 );
36 }
37 }
38
39 /**
40 * Retrieve the locale code of the language.
41 *
42 * @since 2.0
43 *
44 * @return string
45 */
46 public function get_locale() {
47 return $this->locale;
48 }
49
50 /**
51 * Maybe fix the result of sanitize_title() in case the languages include German or Danish
52 * Without this filter, if language of the title being sanitized is different from the language
53 * used for the admin interface and if one this language is German or Danish, some specific
54 * characters such as ä, ö, ü, ß are incorrectly sanitized.
55 *
56 * All the process is done by the remove_accents() WordPress function based on the locale value
57 *
58 * @link https://github.com/WordPress/WordPress/blob/5.5.1/wp-includes/formatting.php#L1920-L1944
59 *
60 * @since 2.0
61 *
62 * @param string $title Sanitized title.
63 * @param string $raw_title The title prior to sanitization.
64 * @param string $context The context for which the title is being sanitized.
65 * @return string
66 */
67 public function sanitize_title( $title, $raw_title, $context ) {
68 static $once = false;
69
70 if ( ! $once && 'save' == $context && ! empty( $title ) ) {
71 $once = true;
72 add_filter( 'locale', array( $this, 'get_locale' ), 20 ); // After the filter for the admin interface
73 $title = sanitize_title( $raw_title, '', $context );
74 remove_filter( 'locale', array( $this, 'get_locale' ), 20 );
75 $once = false;
76 }
77 return $title;
78 }
79
80 /**
81 * Maybe fix the result of sanitize_user() in case the languages include German or Danish
82 *
83 * All the process is done by the remove_accents() WordPress function based on the locale value
84 *
85 * @link https://github.com/WordPress/WordPress/blob/5.5-branch/wp-includes/formatting.php#L1920-L1944
86 *
87 * @since 2.0
88 *
89 * @param string $username Sanitized username.
90 * @param string $raw_username The username prior to sanitization.
91 * @param bool $strict Whether to limit the sanitization to specific characters. Default false.
92 * @return string
93 */
94 public function sanitize_user( $username, $raw_username, $strict ) {
95 static $once = false;
96
97 if ( ! $once ) {
98 $once = true;
99 add_filter( 'locale', array( $this, 'get_locale' ), 20 ); // After the filter for the admin interface
100 $username = sanitize_user( $raw_username, $strict );
101 remove_filter( 'locale', array( $this, 'get_locale' ), 20 );
102 $once = false;
103 }
104 return $username;
105 }
106 }
107