forked from msaari/relevanssi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
relevanssi.php
462 lines (396 loc) · 16.5 KB
/
relevanssi.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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
<?php
/*
Plugin Name: Relevanssi
Plugin URI: http://www.relevanssi.com/
Description: This plugin replaces WordPress search with a relevance-sorting search.
Version: 3.5.9.1
Author: Mikko Saari
Author URI: http://www.mikkosaari.fi/
*/
/* Copyright 2017 Mikko Saari (email: mikko@mikkosaari.fi)
This file is part of Relevanssi, a search plugin for WordPress.
Relevanssi is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Relevanssi is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Relevanssi. If not, see <http://www.gnu.org/licenses/>.
*/
// For debugging purposes
//error_reporting(E_ALL);
//ini_set("display_errors", 1);
//define('WP-DEBUG', true);
global $wpdb;
//$wpdb->show_errors();
define('RELEVANSSI_PREMIUM', false);
global $relevanssi_variables;
$relevanssi_variables['relevanssi_table'] = $wpdb->prefix . "relevanssi";
$relevanssi_variables['stopword_table'] = $wpdb->prefix . "relevanssi_stopwords";
$relevanssi_variables['log_table'] = $wpdb->prefix . "relevanssi_log";
$relevanssi_variables['title_boost_default'] = 5;
$relevanssi_variables['comment_boost_default'] = 0.75;
$relevanssi_variables['post_type_weight_defaults']['post_tag'] = 0.75;
$relevanssi_variables['post_type_weight_defaults']['category'] = 0.75;
$relevanssi_variables['post_type_index_defaults'] = array('post', 'page');
$relevanssi_variables['database_version'] = 5;
$relevanssi_variables['file'] = __FILE__;
$relevanssi_variables['plugin_dir'] = plugin_dir_path(__FILE__);
require_once('lib/init.php');
require_once('lib/interface.php');
require_once('lib/indexing.php');
require_once('lib/stopwords.php');
require_once('lib/search.php');
require_once('lib/excerpts-highlights.php');
require_once('lib/shortcodes.php');
require_once('lib/common.php');
function relevanssi_didyoumean($query, $pre, $post, $n = 5, $echo = true) {
global $wpdb, $relevanssi_variables, $wp_query;
$total_results = $wp_query->found_posts;
if ($total_results > $n) return;
$q = "SELECT query, count(query) as c, AVG(hits) as a FROM " . $relevanssi_variables['log_table'] . " WHERE hits > 1 GROUP BY query ORDER BY count(query) DESC";
$q = apply_filters('relevanssi_didyoumean_query', $q);
$data = $wpdb->get_results($q);
$distance = -1;
$closest = "";
foreach ($data as $row) {
if ($row->c < 2) break;
$lev = levenshtein($query, $row->query);
if ($lev < $distance || $distance < 0) {
if ($row->a > 0) {
$distance = $lev;
$closest = $row->query;
if ($lev == 1) break; // get the first with distance of 1 and go
}
}
}
$result = null;
if ($distance > 0) {
$url = get_bloginfo('url');
$url = esc_attr(add_query_arg(array(
's' => urlencode($closest)
), $url ));
$url = apply_filters('relevanssi_didyoumean_url', $url, $query, $closest);
$closest = htmlspecialchars($closest);
$result = apply_filters('relevanssi_didyoumean_suggestion', "$pre<a href='$url'>$closest</a>$post");
if ($echo) echo $result;
}
return $result;
}
function relevanssi_check_old_data() {
if (!function_exists('get_current_screen')) return;
$screen = get_current_screen();
if ($screen->base != 'settings_page_relevanssi-premium/relevanssi') return;
if (is_admin()) {
// Version 3.3 removes the cache feature
$cache = get_option('relevanssi_enable_cache', 'nothing');
if ($cache != 'nothing') {
global $wpdb;
$relevanssi_cache = $wpdb->prefix . "relevanssi_cache";
$relevanssi_excerpt_cache = $wpdb->prefix . "relevanssi_excerpt_cache";
$wpdb->query("DROP TABLE $relevanssi_cache");
$wpdb->query("DROP TABLE $relevanssi_excerpt_cache");
delete_option('relevanssi_enable_cache');
delete_option('relevanssi_cache_seconds');
wp_clear_scheduled_hook('relevanssi_truncate_cache');
}
// Version 3.1.4 combined taxonomy indexing options
$inctags = get_option('relevanssi_include_tags', 'nothing');
if ($inctags == 'on') {
$taxonomies = get_option('relevanssi_index_taxonomies_list');
if (!is_array($taxonomies)) $taxonomies = array();
$taxonomies[] = 'post_tag';
update_option('relevanssi_index_taxonomies_list', $taxonomies);
delete_option('relevanssi_include_tags');
}
$inccats = get_option('relevanssi_include_cats', 'nothing');
if ($inccats == 'on') {
$taxonomies = get_option('relevanssi_index_taxonomies_list');
if (!is_array($taxonomies)) $taxonomies = array();
$taxonomies[] = 'category';
update_option('relevanssi_index_taxonomies_list', $taxonomies);
delete_option('relevanssi_include_cats');
}
$custom = get_option('relevanssi_custom_taxonomies', 'nothing');
if ($custom != 'nothing') {
$cts = explode(",", $custom);
$taxonomies = get_option('relevanssi_index_taxonomies_list');
if (!is_array($taxonomies)) $taxonomies = array();
foreach ($cts as $taxonomy) {
$taxonomy = trim($taxonomy);
$taxonomies[] = $taxonomy;
}
update_option('relevanssi_index_taxonomies_list', $taxonomies);
delete_option('relevanssi_custom_taxonomies');
}
$limit = get_option('relevanssi_throttle_limit');
if (empty($limit)) update_option('relevanssi_throttle_limit', 500);
global $wpdb, $relevanssi_variables;
if ($relevanssi_variables['database_version'] == 3) {
$res = $wpdb->query("SHOW INDEX FROM " . $relevanssi_variables['relevanssi_table'] . " WHERE Key_name = 'typeitem'");
if ($res == 0) $wpdb->query("ALTER TABLE " . $relevanssi_variables['relevanssi_table'] . " ADD INDEX `typeitem` (`type`, `item`)");
}
// Version 3.0 removed relevanssi_tag_boost
$tag_boost = get_option('relevanssi_tag_boost', 'nothing');
if ($tag_boost != 'nothing') {
$post_type_weights = get_option('relevanssi_post_type_weights');
if (!is_array($post_type_weights)) {
$post_type_weights = array();
}
$post_type_weights['post_tag'] = $tag_boost;
delete_option('relevanssi_tag_boost');
update_option('relevanssi_post_type_weights', $post_type_weights);
}
$index_type = get_option('relevanssi_index_type', 'nothing');
if ($index_type != 'nothing') {
// Delete unused options from versions < 3
$post_types = get_option('relevanssi_index_post_types');
if (!is_array($post_types)) $post_types = array();
switch ($index_type) {
case "posts":
array_push($post_types, 'post');
break;
case "pages":
array_push($post_types, 'page');
break;
case 'public':
if (function_exists('get_post_types')) {
$pt_1 = get_post_types(array('exclude_from_search' => '0'));
$pt_2 = get_post_types(array('exclude_from_search' => false));
foreach (array_merge($pt_1, $pt_2) as $type) {
array_push($post_types, $type);
}
}
break;
case "both": // really should be "everything"
$pt = get_post_types();
foreach ($pt as $type) {
array_push($post_types, $type);
}
break;
}
$attachments = get_option('relevanssi_index_attachments');
if ('on' == $attachments) array_push($post_types, 'attachment');
$custom_types = get_option('relevanssi_custom_types');
$custom_types = explode(',', $custom_types);
if (is_array($custom_types)) {
foreach ($custom_types as $type) {
$type = trim($type);
if (substr($type, 0, 1) != '-') {
array_push($post_types, $type);
}
}
}
update_option('relevanssi_index_post_types', $post_types);
delete_option('relevanssi_index_type');
delete_option('relevanssi_index_attachments');
delete_option('relevanssi_custom_types');
}
}
}
function _relevanssi_install() {
global $relevanssi_variables;
add_option('relevanssi_title_boost', $relevanssi_variables['title_boost_default']);
add_option('relevanssi_comment_boost', $relevanssi_variables['comment_boost_default']);
add_option('relevanssi_admin_search', 'off');
add_option('relevanssi_highlight', 'strong');
add_option('relevanssi_txt_col', '#ff0000');
add_option('relevanssi_bg_col', '#ffaf75');
add_option('relevanssi_css', 'text-decoration: underline; text-color: #ff0000');
add_option('relevanssi_class', 'relevanssi-query-term');
add_option('relevanssi_excerpts', 'on');
add_option('relevanssi_excerpt_length', '30');
add_option('relevanssi_excerpt_type', 'words');
add_option('relevanssi_excerpt_allowable_tags', '');
add_option('relevanssi_log_queries', 'off');
add_option('relevanssi_log_queries_with_ip', 'off');
add_option('relevanssi_cat', '0');
add_option('relevanssi_excat', '0');
add_option('relevanssi_extag', '0');
add_option('relevanssi_index_fields', '');
add_option('relevanssi_exclude_posts', ''); //added by OdditY
add_option('relevanssi_hilite_title', ''); //added by OdditY
add_option('relevanssi_highlight_docs', 'off');
add_option('relevanssi_highlight_comments', 'off');
add_option('relevanssi_index_comments', 'none'); //added by OdditY
add_option('relevanssi_show_matches', '');
add_option('relevanssi_show_matches_text', '(Search hits: %body% in body, %title% in title, %categories% in categories, %tags% in tags, %taxonomies% in other taxonomies, %comments% in comments. Score: %score%)');
add_option('relevanssi_fuzzy', 'sometimes');
add_option('relevanssi_indexed', '');
add_option('relevanssi_expand_shortcodes', 'on');
add_option('relevanssi_custom_taxonomies', '');
add_option('relevanssi_index_author', '');
add_option('relevanssi_implicit_operator', 'OR');
add_option('relevanssi_omit_from_logs', '');
add_option('relevanssi_synonyms', '');
add_option('relevanssi_index_excerpt', 'off');
add_option('relevanssi_index_limit', '500');
add_option('relevanssi_disable_or_fallback', 'off');
add_option('relevanssi_respect_exclude', 'on');
add_option('relevanssi_min_word_length', '3');
add_option('relevanssi_wpml_only_current', 'on');
add_option('relevanssi_word_boundaries', 'on');
add_option('relevanssi_default_orderby', 'relevance');
add_option('relevanssi_db_version', '0');
add_option('relevanssi_post_type_weights', $relevanssi_variables['post_type_weight_defaults']);
add_option('relevanssi_throttle', 'on');
add_option('relevanssi_throttle_limit', '500');
add_option('relevanssi_index_post_types', $relevanssi_variables['post_type_index_defaults']);
add_option('relevanssi_index_taxonomies_list', array());
relevanssi_create_database_tables($relevanssi_variables['database_version']);
}
if (function_exists('register_uninstall_hook')) {
register_uninstall_hook(__FILE__, 'relevanssi_uninstall');
// this doesn't seem to work
}
function relevanssi_get_post($id) {
global $relevanssi_post_array;
if (isset($relevanssi_post_array[$id])) {
$post = $relevanssi_post_array[$id];
}
else {
$post = get_post($id);
}
return $post;
}
function relevanssi_remove_doc($id) {
global $wpdb, $relevanssi_variables;
$D = get_option( 'relevanssi_doc_count');
$q = "DELETE FROM " . $relevanssi_variables['relevanssi_table'] . " WHERE doc=$id";
$wpdb->query($q);
$rows_updated = $wpdb->query($q);
if($rows_updated && $rows_updated > 0) {
update_option('relevanssi_doc_count', $D - $rows_updated);
}
}
/*****
* Interface functions
*/
function relevanssi_form_tag_weight($post_type_weights) {
$label = __("Tag weight:", 'relevanssi');
$value = $post_type_weights['post_tag'];
echo <<<EOH
<tr>
<td>
$label
</td>
<td>
<input type='text' name='relevanssi_weight_post_tag' id='relevanssi_weight_post_tag' size='4' value='$value' />
</td>
<td> </td>
</tr>
EOH;
$label = __("Category weight:", 'relevanssi');
$value = $post_type_weights['category'];
echo <<<EOH
<tr>
<td>
$label
</td>
<td>
<input type='text' id='relevanssi_weight_category' name='relevanssi_weight_category' size='4' value='$value' />
</td>
<td> </td>
</tr>
EOH;
}
function relevanssi_sidebar() {
$tweet = 'http://twitter.com/home?status=' . urlencode("I'm using Relevanssi, a better search for WordPress. http://wordpress.org/extend/plugins/relevanssi/ #relevanssi #wordpress");
if (function_exists("plugins_url")) {
global $wp_version;
if (version_compare($wp_version, '2.8dev', '>' )) {
$facebooklogo = plugins_url('facebooklogo.jpg', __FILE__);
}
else {
$facebooklogo = plugins_url('relevanssi/facebooklogo.jpg');
}
}
else {
// We can't check, so let's assume something sensible
$facebooklogo = '/wp-content/plugins/relevanssi/facebooklogo.jpg';
}
echo <<<EOH
<div class="postbox-container" style="width:20%; margin-top: 35px; margin-left: 15px;">
<div class="metabox-holder">
<div class="meta-box-sortables" style="min-height: 0">
<div id="relevanssi_buy" class="postbox">
EOH;
printf('<h3 class="hndle"><span>%s!</span></h3>', __('Buy Relevanssi Premium', 'relevanssi'));
echo <<<EOH
<div class="inside">
<p>
EOH;
_e('Do you want more features? Support Relevanssi development? Get a better search experience for your users?', 'relevanssi');
echo "</p>";
printf('<p><strong>%s</strong> ', __('Go Premium!', 'relevanssi'));
printf(__('Buy Relevanssi Premium. See <a href="%s">feature comparison</a> and <a href="%s">license prices</a>.', 'relevanssi'), 'https://www.relevanssi.com/features/?utm_source=plugin&utm_medium=link&utm_campaign=features', 'https://www.relevanssi.com/buy-premium/?utm_source=plugin&utm_medium=link&utm_campaign=license');
echo "</p>";
printf('<p><strong><a href="https://www.relevanssi.com/buy-premium/?utm_source=plugin&utm_medium=link&utm_campaign=license">%s »</a></strong></p>', __('Buy Premium now', 'relevanssi'));
printf('<p>' . __('Use the coupon %s to get 20%% off the price (valid through 2017).', 'relevanssi') . '</p>', '<strong>FREE2017</strong>');
echo <<<EOH
</div>
</div>
</div>
<div class="meta-box-sortables" style="min-height: 0">
<div id="relevanssi_premium" class="postbox">
EOH;
printf('<h3 class="hndle"><span>%s</span></h3>', __('Some Premium features', 'relevanssi'));
echo <<<EOH
<div class="inside">
EOH;
printf('<p>%s</p>', __('With Relevanssi Premium, you would have more options:', 'relevanssi'));
printf('– %s<br />', __('Internal link anchors are search terms for the target posts, if you wish', 'relevanssi'));
printf('– %s<br />', __('Hiding Relevanssi branding from the User Searches page on a client installation', 'relevanssi'));
printf('– %s<br />', __('Adjust weights separately for each post type and taxonomy', 'relevanssi'));
printf('– %s<br />', __('Give extra weight to recent posts', 'relevanssi'));
printf('– %s<br />', __('Make Relevanssi understand thousand separators to handle big numbers better', 'relevanssi'));
printf('– %s<br />', __('Index and search any columns in the wp_posts database', 'relevanssi'));
printf('– %s<br />', __('Index and search user profile pages', 'relevanssi'));
printf('– %s<br />', __('Index and search taxonomy term pages', 'relevanssi'));
printf('– %s<br />', __('Import and export options', 'relevanssi'));
printf('– %s<br />', __('WP CLI commands', 'relevanssi'));
printf('– %s<br />', __('And more!', 'relevanssi'));
echo <<<EOH
</p>
</div>
</div>
</div>
<div class="meta-box-sortables" style="min-height: 0">
<div id="relevanssi_facebook" class="postbox">
EOH;
printf('<h3 class="hndle"><span>%s</span></h3>', __('Relevanssi on Facebook', 'relevanssi'));
echo <<<EOH
<div class="inside">
<div style="float: left; margin-right: 5px"><img src="$facebooklogo" width="45" height="43" alt="Facebook" /></div>
EOH;
printf('<p>' . __('<a href="%s">Check out the Relevanssi page on Facebook</a> for news and updates about Relevanssi.', 'relevanssi') . '</p>', 'https://www.facebook.com/relevanssi');
echo <<<EOH
</div>
</div>
</div>
<div class="meta-box-sortables" style="min-height: 0">
<div id="relevanssi_help" class="postbox">
EOH;
printf('<h3 class="hndle"><span>%s</span></h3>', __('Help and support', 'relevanssi'));
echo '<div class="inside">';
printf('<p>%s</p>', __('For Relevanssi support, see:', 'relevanssi'));
printf('<p>– <a href="http://wordpress.org/tags/relevanssi?forum_id=10">%s</a><br />', __('WordPress.org forum', 'relevanssi'));
printf('– <a href="https://www.relevanssi.com/category/knowledge-base/?utm_source=plugin&utm_medium=link&utm_campaign=kb">%s</a></p>', __('Knowledge base', 'relevanssi'));
echo <<<EOH
</div>
</div>
</div>
</div>
</div>
EOH;
}
/**
* Wrapper function for Premium compatibility.
*/
function relevanssi_install() {
_relevanssi_install();
}
?>