-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtl-normalize.php
202 lines (173 loc) · 6.22 KB
/
tl-normalize.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
/**
* Plugin Name: Normalizer
* Plugin URI: https://github.com/Zodiac1978/tl-normalizer
* Description: Normalizes content, excerpt, title and comment content to Normalization Form C.
* Version: 1.1.0
* Author: Torsten Landsiedel
* Author URI: http://torstenlandsiedel.de
* License: GPLv2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: normalizer
* Domain Path: /languages
*
* @package WordPress\Normalizer
*/
/**
* Normalize to NFC class
*/
class TLNormalizer {
/**
* Constructor function for TLNormalizer.
*/
public function __construct() {
add_action( 'admin_init', array( $this, 'check_version' ) );
// Don't run anything else in the plugin, if we're on an incompatible PHP.
if ( ! self::compatible_version() ) {
return;
}
add_action( 'init', array( $this, 'init' ), 0 );
// Adding function to all available sanitize filters.
add_filter( 'sanitize_email', array( $this, 'normalize_to_nfc' ) );
add_filter( 'sanitize_file_name', array( $this, 'normalize_to_nfc' ) );
add_filter( 'sanitize_html_class', array( $this, 'normalize_to_nfc' ) );
add_filter( 'sanitize_key', array( $this, 'normalize_to_nfc' ) );
add_filter( 'sanitize_mime_type', array( $this, 'normalize_to_nfc' ) );
add_filter( 'sanitize_sql_orderby', array( $this, 'normalize_to_nfc' ) );
add_filter( 'sanitize_text_field', array( $this, 'normalize_to_nfc' ) );
add_filter( 'sanitize_title', array( $this, 'normalize_to_nfc' ) );
add_filter( 'sanitize_title_for_query', array( $this, 'normalize_to_nfc' ) );
add_filter( 'sanitize_title_with_dashes', array( $this, 'normalize_to_nfc' ) );
add_filter( 'sanitize_user', array( $this, 'normalize_to_nfc' ) );
// Adding function to title, content, comment and excerpt before save to db.
add_filter( 'content_save_pre', array( $this, 'normalize_to_nfc' ) );
add_filter( 'title_save_pre', array( $this, 'normalize_to_nfc' ) );
add_filter( 'pre_comment_content', array( $this, 'normalize_to_nfc' ) );
add_filter( 'excerpt_save_pre', array( $this, 'normalize_to_nfc' ) );
// Adding special function to widget callback.
add_filter( 'widget_update_callback', array( $this, 'normalize_to_nfc_widget' ) );
// Adding function to special case ACF values.
add_filter( 'acf/update_value', array( $this, 'normalize_to_nfc' ) );
// Adding function to output of Beaver Builder/Elementor (before save would be better but is not available).
add_filter( 'fl_builder_render_module_content', array( $this, 'normalize_to_nfc' ) );
add_filter( 'elementor/frontend/the_content', array( $this, 'normalize_to_nfc' ) );
}
/**
* Init when WordPress Initialises.
*
* @since 1.1.0
*/
public function init() {
// Set up localisation.
$this->load_plugin_textdomain();
}
/**
* Load plugin textdomain.
*
* @since 1.1.0
*/
private function load_plugin_textdomain() {
if ( is_admin() ) {
load_plugin_textdomain( 'normalizer', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
}
/**
* The primary sanity check, automatically disable the plugin on activation if it doesn't meet minimum requirements.
*
* @return void
*/
private static function activation_check() {
if ( ! self::compatible_version() ) {
deactivate_plugins( plugin_basename( __FILE__ ) );
wp_die( esc_html__( 'Your PHP version is not 5.3.0 or later or your PHP is missing the required extension (intl).', 'normalizer' ) );
}
}
/**
* The backup sanity check, in case the plugin is activated in a weird way, or the versions change after activation.
*
* @return void
*/
private function check_version() {
if ( ! self::compatible_version() ) {
if ( is_plugin_active( plugin_basename( __FILE__ ) ) ) {
deactivate_plugins( plugin_basename( __FILE__ ) );
add_action( 'admin_notices', array( $this, 'disabled_notice' ) );
if ( isset( $_GET['activate'] ) ) {
unset( $_GET['activate'] );
}
}
}
}
/**
* Show error message.
*
* @return void
*/
private function disabled_notice() {
$error_message = '<div class="notice notice-error is-dismissible">';
$error_message .= '<p><strong>' . esc_html__( 'Plugin deactivated!', 'normalizer' ) . '</strong> ';
$error_message .= esc_html__( 'Your PHP version is not 5.3.0 or later or your PHP is missing the required extension (intl).', 'normalizer' );
$error_message .= '</p></div>';
echo $error_message;
}
/**
* Compatible version check.
*
* @return bool
*/
private static function compatible_version() {
// Add sanity checks for other version requirements here.
if ( ! function_exists( 'normalizer_normalize' ) ) {
return false;
}
return true;
}
/**
* Normalize to NFC
*
* @param string $content Original content.
* @return string $content Normalized content.
*/
public function normalize_to_nfc( $content ) {
/* Why?
*
* For everyone getting this warning from W3C: "Text run is not in Unicode Normalization Form C."
* http://www.w3.org/International/docs/charmod-norm/#choice-of-normalization-form
*
* Requires PHP 5.3+
* Be sure to have the PHP-Normalizer-extension (intl and icu) installed.
* See: http://php.net/manual/en/normalizer.normalize.php
*/
if ( ! normalizer_is_normalized( $content, Normalizer::FORM_C ) ) {
$content = normalizer_normalize( $content, Normalizer::FORM_C );
}
return $content;
}
/**
* Normalize to NFC for Arrays
*
* @param array $instance Orignal array.
* @return array $instance Normalized array.
*/
public function normalize_to_nfc_widget( $instance ) {
/*
* Why?
*
* For everyone getting this warning from W3C: "Text run is not in Unicode Normalization Form C."
* http://www.w3.org/International/docs/charmod-norm/#choice-of-normalization-form
*
* Requires PHP 5.3+
* Be sure to have the PHP-Normalizer-extension (intl and icu) installed.
* See: http://php.net/manual/en/normalizer.normalize.php
*/
foreach ( $instance as $key => $value ) {
if ( ! normalizer_is_normalized( $instance[ $key ], Normalizer::FORM_C ) ) {
$instance[ $key ] = normalizer_normalize( $instance[ $key ], Normalizer::FORM_C );
}
}
return $instance;
}
}
global $normalizer;
$normalizer = new TLNormalizer();
register_activation_hook( __FILE__, array( 'TLNormalizer', 'activation_check' ) );