stopwords.php
11.7 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<?php
/**
* /lib/stopwords.php
*
* @package Relevanssi
* @author Mikko Saari
* @license https://wordpress.org/about/gpl/ GNU General Public License
* @see https://www.relevanssi.com/
*/
/**
* Reads automatically the correct stopwords for the current language set in
* WPLANG.
*
* The stopwords are first read from the wp_relevanssi_stopwords database table
* (which is where they were stored before they were moved to an option), but
* if the table is empty (as it will be in new installations), the stopwords are
* read from the stopword file for the current language (defaulting to en_US).
*
* @global object $wpdb The WordPress database interface.
* @global array $relevanssi_variables The global Relevanssi variables array.
*
* @param boolean $verbose If true, output results. Default false.
* @param string $stopword_table Name of the stopword table to use. Default
* empty, which means the default table.
*
* @return string Result: 'database' for reading from database, 'file' for
* reading from file, 'no_file' for non-existing file, 'file_error' for file
* with non-acceptable data.
*/
function relevanssi_populate_stopwords( $verbose = false, string $stopword_table = '' ) {
global $relevanssi_variables, $wpdb;
if ( empty( $stopword_table ) ) {
$stopword_table = $relevanssi_variables['stopword_table'];
}
$stopwords_from_table = $wpdb->get_col( "SELECT * FROM $stopword_table" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
if ( count( $stopwords_from_table ) > 1 ) {
array_walk( $stopwords_from_table, 'relevanssi_add_single_stopword' );
$verbose && printf(
"<div id='message' class='updated fade'><p>%s</p></div>",
esc_html__( 'Added stopwords from the database.', 'relevanssi' )
);
return 'database';
}
$language = relevanssi_get_current_language();
$stopword_file = $relevanssi_variables['plugin_dir']
. 'stopwords/stopwords.' . $language;
if ( ! file_exists( $stopword_file ) ) {
$verbose && printf(
"<div id='message' class='updated fade'><p>%s</p></div>",
sprintf(
// Translators: %s is the language code.
esc_html__(
"The stopword file for the language '%s' doesn't exist.",
'relevanssi'
),
esc_html( $language )
)
);
return 'no_file';
}
$stopwords = array();
include $stopword_file; // Contains the stopwords in the $stopwords array.
if ( ! is_array( $stopwords ) ) {
$verbose && printf(
"<div id='message' class='updated fade'><p>%s</p></div>",
esc_html__(
"Couldn't read the stopwords from the file.",
'relevanssi'
)
);
return 'file_error';
}
array_walk( $stopwords, 'relevanssi_add_single_stopword' );
$verbose && printf(
"<div id='message' class='updated fade'><p>%s</p></div>",
esc_html__( 'Added stopwords from the stopword file.', 'relevanssi' )
);
return 'file';
}
/**
* Fetches the list of stopwords in the current language.
*
* Gets the list of stopwords from the relevanssi_stopwords option using the
* current language.
*
* @return array An array of stopwords; if nothing is found, returns an empty
* array.
*/
function relevanssi_fetch_stopwords() {
$current_language = relevanssi_get_current_language();
$stopwords_array = get_option( 'relevanssi_stopwords', array() );
$stopwords = isset( $stopwords_array[ $current_language ] ) ? $stopwords_array[ $current_language ] : '';
$stopword_list = $stopwords ? explode( ',', $stopwords ) : array();
return $stopword_list;
}
/**
* Adds a stopword to the list of stopwords.
*
* @param string $term The stopword that is added.
* @param boolean $verbose If true, print out notices. Default true.
*
* @return boolean True, if success; false otherwise.
*/
function relevanssi_add_stopword( $term, $verbose = true ) {
if ( empty( $term ) ) {
return false;
}
$total_stopwords = 0;
$successfully_added = 0;
$terms = explode( ',', $term );
if ( count( $terms ) > 1 ) {
$total_stopwords = count( $terms );
$successfully_added = array_reduce(
$terms,
function ( $counter, $term ) {
$success = relevanssi_add_single_stopword( trim( $term ) );
$success && $counter++;
return $counter;
},
0
);
$verbose &&
printf(
"<div id='message' class='updated fade'><p>%s</p></div>",
sprintf(
// translators: %1$d is the successful entries, %2$d is the total entries.
esc_html__(
'Successfully added %1$d/%2$d terms to stopwords!',
'relevanssi'
),
intval( $successfully_added ),
intval( $total_stopwords )
)
);
return boolval( $successfully_added );
}
// Add to stopwords.
$success = relevanssi_add_single_stopword( $term );
$term = esc_html( $term );
$verbose && $success && printf(
"<div id='message' class='updated fade'><p>%s</p></div>",
sprintf(
// Translators: %s is the stopword.
esc_html__( "Term '%s' added to stopwords!", 'relevanssi' ),
esc_html( stripslashes( $term ) )
)
);
$verbose && ! $success && printf(
"<div id='message' class='updated fade'><p>%s</p></div>",
sprintf(
// Translators: %s is the stopword.
esc_html__( "Couldn't add term '%s' to stopwords!", 'relevanssi' ),
esc_html( stripslashes( $term ) )
)
);
return $success;
}
/**
* Adds a single stopword to the stopword table.
*
* @global object $wpdb The WP database interface.
* @global array $relevanssi_variables The global Relevanssi variables.
*
* @param string $term The term to add.
*
* @return boolean True if success, false if not.
*/
function relevanssi_add_single_stopword( $term ) {
if ( empty( $term ) ) {
return false;
}
$stopwords = relevanssi_fetch_stopwords();
$term = stripslashes( relevanssi_strtolower( $term ) );
if ( in_array( $term, $stopwords, true ) ) {
return false;
}
$stopwords[] = $term;
$success = relevanssi_update_stopwords( $stopwords );
if ( ! $success ) {
return false;
}
relevanssi_delete_term_from_all_posts( $term );
return true;
}
/**
* Updates the current language stopwords in the stopwords option.
*
* Fetches the stopwords option, replaces the current language stopwords with
* the parameter array and updates the option.
*
* @param array $stopwords An array of stopwords.
*
* @return boolean The return value from update_option().
*/
function relevanssi_update_stopwords( $stopwords ) {
$current_language = relevanssi_get_current_language();
$stopwords_option = get_option( 'relevanssi_stopwords', array() );
$stopwords_option[ $current_language ] = implode( ',', array_filter( $stopwords ) );
return update_option(
'relevanssi_stopwords',
$stopwords_option
);
}
/**
* Deletes a term from all posts in the database, language considered.
*
* If Polylang or WPML are used, deletes the term only from the posts matching
* the current language.
*
* @param string $term The term to delete.
*/
function relevanssi_delete_term_from_all_posts( $term ) {
global $wpdb, $relevanssi_variables;
if ( function_exists( 'pll_languages_list' ) ) {
$term_id = relevanssi_get_language_term_taxonomy_id(
relevanssi_get_current_language()
);
$wpdb->query(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"DELETE FROM {$relevanssi_variables['relevanssi_table']}
WHERE term=%s
AND doc IN (
SELECT object_id
FROM $wpdb->term_relationships
WHERE term_taxonomy_id = %d
)",
$term,
$term_id
)
);
return;
}
if ( function_exists( 'icl_object_id' ) && ! function_exists( 'pll_is_translated_post_type' ) ) {
$language = relevanssi_get_current_language( false );
$wpdb->query(
$wpdb->prepare(
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"DELETE FROM {$relevanssi_variables['relevanssi_table']}
WHERE term=%s
AND doc IN (
SELECT DISTINCT(element_id)
FROM {$wpdb->prefix}icl_translations
WHERE language_code = %s
)",
$term,
$language
)
);
return;
}
// No language defined, just remove from the index.
$wpdb->query(
$wpdb->prepare(
'DELETE FROM ' . $relevanssi_variables['relevanssi_table'] . // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
' WHERE term=%s',
$term
)
);
}
/**
* Removes all stopwords in specific language.
*
* Empties the relevanssi_stopwords option for particular language.
*
* @param boolean $verbose If true, print out notice. Default true.
* @param string $language The language code of stopwords. If empty, removes
* the stopwords for the current language.
*
* @return boolean True, if able to remove the options.
*/
function relevanssi_remove_all_stopwords( $verbose = true, $language = false ) {
if ( ! $language ) {
$language = relevanssi_get_current_language();
}
$stopwords = get_option( 'relevanssi_stopwords', array() );
unset( $stopwords[ $language ] );
$success = update_option( 'relevanssi_stopwords', $stopwords );
$verbose && $success && printf(
"<div id='message' class='updated fade'><p>%s</p></div>",
esc_html__(
'All stopwords removed! Remember to re-index.',
'relevanssi'
)
);
$verbose && ! $success && printf(
"<div id='message' class='updated fade'><p>%s</p></div>",
esc_html__(
"There was a problem, and stopwords couldn't be removed.",
'relevanssi'
)
);
return $success;
}
/**
* Removes a single stopword.
*
* @global object $wpdb The WP database interface.
* @global array $relevanssi_variables The global Relevanssi variables.
*
* @param string $term The stopword to remove.
* @param boolean $verbose If true, print out a notice. Default true.
*
* @return boolean True if success, false if not.
*/
function relevanssi_remove_stopword( $term, $verbose = true ) {
$stopwords = relevanssi_fetch_stopwords();
$term = stripslashes( $term );
$stopwords = array_filter(
$stopwords,
function ( $stopword ) use ( $term ) {
return $stopword !== $term;
}
);
$success = relevanssi_update_stopwords( $stopwords );
$verbose && $success &&
printf(
"<div id='message' class='updated fade'><p>%s</p></div>",
sprintf(
// Translators: %s is the stopword.
esc_html__(
"Term '%s' removed from stopwords! Re-index to get it back to index.",
'relevanssi'
),
esc_html( stripslashes( $term ) )
)
);
$verbose && ! $success &&
printf(
"<div id='message' class='updated fade'><p>%s</p></div>",
sprintf(
// Translators: %s is the stopword.
esc_html__(
"Couldn't remove term '%s' from stopwords!",
'relevanssi'
),
esc_html( stripslashes( $term ) )
)
);
return $success;
}
/**
* Helper function to remove stopwords from an array.
*
* Removes all stopwords from an array of terms. If body stopwords are
* available, those will also be removed. The terms must be in the array values.
*
* @param array $terms An array of terms to clean out.
*
* @return array An array of terms with stopwords removed.
*/
function relevanssi_remove_stopwords_from_array( $terms ) {
$stopword_list = relevanssi_fetch_stopwords();
if ( function_exists( 'relevanssi_fetch_body_stopwords' ) ) {
$stopword_list = array_merge( $stopword_list, relevanssi_fetch_body_stopwords() );
}
$terms_without_stops = array_diff( $terms, $stopword_list );
return $terms_without_stops;
}
/**
* Updates the relevanssi_stopwords setting from a simple string to an array
* that is required for multilingual stopwords.
*/
function relevanssi_update_stopwords_setting() {
$stopwords = get_option( 'relevanssi_stopwords' );
if ( is_object( $stopwords ) ) {
$array_stopwords = (array) $stopwords;
update_option( 'relevanssi_stopwords', $array_stopwords );
return;
}
$current_language = relevanssi_get_current_language();
$array_stopwords[ $current_language ] = $stopwords;
update_option( 'relevanssi_stopwords', $array_stopwords );
}