This repository was archived by the owner on Jan 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphoneticbleepfilter.class.php
More file actions
348 lines (284 loc) · 11 KB
/
phoneticbleepfilter.class.php
File metadata and controls
348 lines (284 loc) · 11 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
class PhoneticBleepFilter extends BleepFilter
{
public function __construct(){
$bleep_filter_content = get_option('bleep_filter_content');
$bleep_filter_content_rss = get_option('bleep_filter_content_rss');
$bleep_filter_comment = get_option('bleep_filter_comment');
$bleep_filter_comment_rss = get_option('bleep_filter_comment_rss');
$bleep_filter_bbpress = get_option('bleep_filter_bbpress');
if ( ! is_admin() ) {
/* Check which settings are toggled on */
if($bleep_filter_content == 'on'){
add_filter( 'the_content' , array( $this, 'word_filter' ) , 50 );
add_filter( 'the_excerpt' , array( $this, 'word_filter' ), 50 );
add_filter( 'the_title' , array( $this, 'word_filter' ), 50 );
}
if($bleep_filter_content_rss == 'on'){
add_filter( 'the_content_rss' , array( $this, 'word_filter' ) , 50 );
add_filter( 'the_excerpt_rss' , array( $this, 'word_filter' ) , 50 );
add_filter( 'the_title_rss' , array( $this, 'word_filter' ) , 50 );
}
if($bleep_filter_comment == 'on'){
add_filter( 'comment_text' , array( $this, 'word_filter' ), 50);
add_filter( 'comment_excerpt' , array( $this, 'word_filter' ), 50);
}
if($bleep_filter_comment_rss == 'on'){
add_filter( 'comment_text_rss' , array( $this, 'word_filter' ), 50 );
add_filter( 'comment_excerpt_rss' , array( $this, 'word_filter' ), 50);
}
/* bbPress specific filtering (only if bbPress is present) */
if( class_exists('bbPress') and $bleep_filter_bbpress == 'on' ) {
add_filter( 'bbp_get_topic_content', array( $this, 'word_filter' ), 50 );
add_filter( 'bbp_get_topic_title', array( $this, 'word_filter' ), 50 );
add_filter( 'bbp_get_reply_content', array( $this, 'word_filter' ), 50 );
add_filter( 'bbp_get_reply_title', array( $this, 'word_filter' ), 50 );
}
/* Load Filtered Words and Exceptions */
$this->alert_words = $this->get_words();
$this->exception_words = $this->get_exceptions();
}
}
/* Grabs filtered words as array */
public function get_words(){
$alert_words = array();
$replace_words = array();
$the_posts = get_posts(array('post_type' => 'bleep_filter_words','numberposts' => -1));
foreach($the_posts as $post){
$value = get_post_meta( $post->ID, 'bleep_replacement', true );
array_push($alert_words, $post->post_title);
$replace_words[$post->post_title] = esc_attr( $value );
}
$this->replace_words = $replace_words;
return $alert_words;
}
/* Grabs word exceptions as array */
function get_exceptions(){
$exception_words = array();
$the_posts = get_posts(array('post_type' => 'bleep_exception','numberposts' => -1));
foreach($the_posts as $post){
array_push($exception_words, $post->post_name);
}
return $exception_words;
}
/* Gets before and after text formatting
$formatting[0] is used for before text formatting
$formatting[1] is used for adter text formatting
$formatting[2] is used as replacement string for string_replace
*/
public function filterFormatting($bleep_filter_format){
$formatting = array();
switch($bleep_filter_format){
case 'erase':
$formatting[0] = '';
$formatting[1] = '';
break;
case 'blackout':
$formatting[0] = "<span class='bleep_filter_blackout'>";
$formatting[1] = '</span>';
break;
case 'blackout_erase':
$formatting[0] = "<span class='bleep_filter_blackout_erase'>";
$formatting[1] = '</span>';
break;
case 'strikeout':
$formatting[0] = "<span class='bleep_filter_strikeout'>";
$formatting[1] = '</span>';
break;
case 'bleep':
$formatting[0] = '';
$formatting[1] = '';
break;
case 'asterisk':
$formatting[0] = '';
$formatting[1] = '';
$formatting[2] = '*';
break;
case 'asterisk_first':
$formatting[0] = '';
$formatting[1] = '';
$formatting[2] = 'z';
break;
default:
$formatting[0] = '';
$formatting[1] = '';
break;
}
return $formatting;
}
/* Filters filtered words */
function word_filter($comment){
/* Intensity refers to the similarity match in the metaphone.
A higher intensity would mean a more exact match is required.
A lower intensity would catch the most words but setting too
low may cause unwanted words to be filtered out */
$intensity = 5;
$bleep_filter_format = get_option('bleep_filter_format');
$words_formatting = $this->filterFormatting($bleep_filter_format);
$word_before = $words_formatting[0];
$word_after = $words_formatting[1];
$words_format = $words_formatting[2];
/* The post / comment text */
$words = $comment;
$words = preg_replace('/\s+/', ' ',$words);
$word_count = str_word_count($words,0,'0123456789');
$words_explode = explode(' ',$words);
$words_metaphone = '';
$alert_words_metaphone = array();
/* Loop through all words to filter out and create a metaphone array */
foreach($this->alert_words as $alert_phrase){
$alert_phrase_count = str_word_count($alert_phrase,1);
$alert_phrase_metaphone = '';
$new_alert_phrase = '';
if($alert_phrase_count >= 1){
foreach($alert_phrase_count as $alert_phrase_value){
$new_alert_phrase .= metaphone($alert_phrase_value).' ';
}
array_push($alert_words_metaphone,rtrim($new_alert_phrase));
}
else{
$alert_phrase_explode = explode(' ',$alert_phrase);
foreach($alert_phrase_explode as $alert_word){
$new_alert_phrase .= ' '.metaphone($alert_word);
}
array_push($alert_words_metaphone,ltrim($new_alert_phrase));
}
}
/* Create metaphone for each word in the post */
foreach($words_explode as $word){
$metaphone_word = metaphone($word);
if(trim($metaphone_word) === ''){
$metaphone_word = $word;
}
$words_metaphone .= ' '.$metaphone_word;
}
$words_metaphone = ltrim($words_metaphone);
/* Loop through metaphone words to filter */
foreach($alert_words_metaphone as $alert_word_index => $alert_word_metaphone){
$pattern = '/'.$alert_word_metaphone.'/';
/* Find all metaphone matchs containg the metaphone of the word to filter */
preg_match_all($pattern, $words_metaphone, $matches, PREG_OFFSET_CAPTURE);
foreach($matches as $match=>$m){
$string = '';
$char_start = '';
$string_len = '';
if($m){
foreach($m as $ma){
$string = $ma[0];
$string_len = strlen($string);
$char_start = $ma[1]+1;
if($string_len != ''){
$meta_substr = substr($words_metaphone,0,$char_start);
/* finds all words in the found matches array */
if(str_word_count($meta_substr) >= 0){
/* finds out the starting word placement in the metaphone array */
$words_start = str_word_count($meta_substr,0,'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ');
/* finds out the end word placement in the metaphone array */
$words_end = $words_start + (str_word_count($alert_word_metaphone,0,'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ')-1);
$words_start--;
$words_end--;
$phrase_word_count = $words_end - $words_start + 1;
$phrase_words = '';
$start_word_test = strtolower(trim(strip_tags($words_explode[$words_start])));
$start_word_test = preg_replace('/[^A-Za-z0-9\-]/', '', $start_word_test);
$start_word_metaphone_test = metaphone($start_word_test);
$alert_word_test = strtolower(trim($this->alert_words[$alert_word_index]));
$alert_word_metaphone_test = metaphone($alert_word_test);
$found = 0;
if($phrase_word_count > 1){
for($i=0;$i<$phrase_word_count;$i++){
$phrase_words .= metaphone(strtolower(trim(strip_tags($words_explode[$words_start+$i])))). ' ';
}
$phrase_words = rtrim($phrase_words);
if($phrase_words == $ma[0]){
$found = 1;
}
}
/* compares metaphone word against the metaphone filter word and returns a percent match */
similar_text($start_word_metaphone_test,$alert_word_metaphone_test,$p);
if($found !== 1){
$subject = preg_replace('/(.)\1{2,}/', '$1',$start_word_test);
$pattern = '/'.$alert_word_test.'/';
preg_match($pattern, $subject, $matches);
$found = count($matches[0]);
}
if(($p == 100) || ($found == 1)){
$exception_found = false;
if($found == 0){
if(strlen($alert_word_metaphone_test) <= 3){
similar_text($start_word_test,$alert_word_test,$p2);
if( (($p+$p2)/2) < 85){
$exception_found = true;
}
}
}
}
else{
$exception_found = true;
}
if($exception_found !== true){
/* check to see if the match is an exception word */
foreach($this->exception_words as $exception){
if($start_word_test === $exception){
$exception_found = true;
}
}
}
/* filter the word if the exception is not found */
if($exception_found === false){
$replace = null;
for($i = $words_start; $i <= $words_end; $i++){
$words_explode[$i] = strip_tags($words_explode[$i]);
}
if($bleep_filter_format == 'erase'){
$words_explode[$words_start] = '';
}
if($bleep_filter_format == 'blackout_erase'){
$replace = ' ';
}
if($words_format){
if($bleep_filter_format == 'asterisk'){
$replace = '*';
}
}
if($bleep_filter_format == 'bleep'){
$bleeps = array('!','@','#','$','%','&','*','?');
$bleeps_count = count($bleeps);
for($i = $words_start; $i <= $words_end; $i++){
$word_len = strlen($words_explode[$i]);
for($a = 0; $a < $word_len; $a++){
$bleep = $bleeps[rand(0,$bleeps_count)];
$words_explode[$i] = substr_replace($words_explode[$i],$bleep,$a,1);
}
}
}
//$alert_word_test = trim($alert_word_test);
if($this->replace_words[$alert_word_test] != ''){
$replace = $this->replace_words[$alert_word_test];
$words_explode[$words_start] = str_replace($words_explode[$words_start], $replace, 0);
}
if($replace != null){
for($i = $words_start; $i <= $words_end; $i++){
$words_explode[$i] = preg_replace('/[\S]/', "$replace", $words_explode[$i]);
}
}
if($this->replace_words[$alert_word_test] === ''){
$words_explode[$words_start] = $word_before.$words_explode[$words_start];
$words_explode[$words_end] = $words_explode[$words_end].$word_after;
}
}
}
}
}
}
}
}
$show_words = '';
/* loop through the post words and return the filtered text */
foreach($words_explode as $show_word){
$show_words .= $show_word . ' ';
}
return rtrim($show_words);
}
}
?>