/
relevanssi.php
3804 lines (3149 loc) · 130 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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
Plugin Name: Relevanssi
Plugin URI: http://www.relevanssi.com/
Description: This plugin replaces WordPress search with a relevance-sorting search.
Version: 2.9.14
Author: Mikko Saari
Author URI: http://www.mikkosaari.fi/
*/
/* Copyright 2011 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();
register_activation_hook(__FILE__,'relevanssi_install');
add_action('admin_menu', 'relevanssi_menu');
add_filter('the_posts', 'relevanssi_query');
add_action('save_post', 'relevanssi_edit'); // thanks to Brian D Gajus
add_action('delete_post', 'relevanssi_delete');
add_action('comment_post', 'relevanssi_comment_index'); //added by OdditY
add_action('edit_comment', 'relevanssi_comment_edit'); //added by OdditY
add_action('delete_comment', 'relevanssi_comment_remove'); //added by OdditY
// BEGIN added by renaissancehack
// *_page and *_post hooks do not trigger on attachments
add_action('delete_attachment', 'relevanssi_delete');
add_action('add_attachment', 'relevanssi_publish');
add_action('edit_attachment', 'relevanssi_edit');
// When a post status changes, check child posts that inherit their status from parent
add_action('transition_post_status', 'relevanssi_update_child_posts',99,3);
// END added by renaissancehack
add_action('init', 'relevanssi_init');
add_filter('relevanssi_hits_filter', 'relevanssi_wpml_filter');
add_filter('relevanssi_remove_punctuation', 'relevanssi_remove_punct');
add_filter('relevanssi_post_ok', 'relevanssi_default_post_ok');
$plugin_dir = basename(dirname(__FILE__));
load_plugin_textdomain('relevanssi', false, $plugin_dir);
global $wpSearch_low;
global $wpSearch_high;
global $relevanssi_table;
global $stopword_table;
global $log_table;
global $relevanssi_cache;
global $relevanssi_excerpt_cache;
global $stopword_list;
global $title_boost_default;
global $tag_boost_default;
global $comment_boost_default;
global $relevanssi_hits;
$wpSearch_low = 0;
$wpSearch_high = 0;
$relevanssi_table = $wpdb->prefix . "relevanssi";
$stopword_table = $wpdb->prefix . "relevanssi_stopwords";
$log_table = $wpdb->prefix . "relevanssi_log";
$relevanssi_cache = $wpdb->prefix . "relevanssi_cache";
$relevanssi_excerpt_cache = $wpdb->prefix . "relevanssi_excerpt_cache";
$title_boost_default = 5;
$tag_boost_default = 0.75;
$comment_boost_default = 0.75;
function relevanssi_menu() {
add_options_page(
'Relevanssi',
'Relevanssi',
'manage_options',
__FILE__,
'relevanssi_options'
);
add_dashboard_page(
__('User searches', 'relevanssi'),
__('User searches', 'relevanssi'),
'edit_pages',
__FILE__,
'relevanssi_search_stats'
);
}
function relevanssi_init() {
isset($_POST['index']) ? $index = true : $index = false;
if (!get_option('relevanssi_indexed') && !$index) {
function relevanssi_warning() {
echo "<div id='relevanssi-warning' class='updated fade'><p><strong>"
. sprintf(__('Relevanssi needs attention: Remember to build the index (you can do it at <a href="%1$s">the settings page</a>), otherwise searching won\'t work.'), "options-general.php?page=relevanssi/relevanssi.php")
. "</strong></p></div>";
}
add_action('admin_notices', 'relevanssi_warning');
}
if (!function_exists('mb_internal_encoding')) {
function relevanssi_mb_warning() {
echo "<div id='relevanssi-warning' class='updated fade'><p><strong>"
. "Multibyte string functions are not available. Relevanssi won't work without them."
. "Please install (or ask your host to install) the mbstring extension."
. "</strong></p></div>";
}
add_action('admin_notices', 'relevanssi_mb_warning');
}
if (!wp_next_scheduled('relevanssi_truncate_cache')) {
wp_schedule_event(time(), 'daily', 'relevanssi_truncate_cache');
add_action('relevanssi_truncate_cache', 'relevanssi_truncate_cache');
}
return;
}
function relevanssi_truncate_cache($all = false) {
global $relevanssi_cache, $relevanssi_excerpt_cache, $wpdb;
if ($all) {
$query = "TRUNCATE TABLE $relevanssi_excerpt_cache";
$wpdb->query($query);
$query = "TRUNCATE TABLE $relevanssi_cache";
}
else {
$time = get_option('relevanssi_cache_seconds', 172800);
$query = "DELETE FROM $relevanssi_cache
WHERE UNIX_TIMESTAMP() - UNIX_TIMESTAMP(tstamp) > $time";
// purge all expired cache data
}
$wpdb->query($query);
}
function relevanssi_didyoumean($query, $pre, $post, $n = 5) {
global $wpdb, $log_table, $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 $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
}
}
}
if ($distance > 0) {
$url = get_bloginfo('url');
$url = esc_attr(add_query_arg(array(
's' => urlencode($closest)
), $url ));
echo "$pre<a href='$url'>$closest</a>$post";
}
}
// BEGIN added by renaissancehack
function relevanssi_update_child_posts($new_status, $old_status, $post) {
// called by 'transition_post_status' action hook when a post is edited/published/deleted
// and calls appropriate indexing function on child posts/attachments
global $wpdb;
$index_statuses = array('publish', 'private', 'draft', 'pending', 'future');
if (($new_status == $old_status)
|| (in_array($new_status, $index_statuses) && in_array($old_status, $index_statuses))
|| (in_array($post->post_type, array('attachment', 'revision')))) {
return;
}
$q = "SELECT * FROM $wpdb->posts WHERE post_parent=$post->ID AND post_type!='revision'";
$child_posts = $wpdb->get_results($q);
if ($child_posts) {
if (!in_array($new_status, $index_statuses)) {
foreach ($child_posts as $post) {
relevanssi_delete($post->ID);
}
} else {
foreach ($child_posts as $post) {
relevanssi_publish($post->ID);
}
}
}
}
// END added by renaissancehack
function relevanssi_edit($post) {
// Check if the post is public
global $wpdb;
$post_status = get_post_status($post);
if ('auto-draft' == $post_status) return;
// BEGIN added by renaissancehack
// if post_status is "inherit", get post_status from parent
if ($post_status == 'inherit') {
$post_type = $wpdb->get_var("SELECT post_type FROM $wpdb->posts WHERE ID=$post");
$post_status = $wpdb->get_var("SELECT p.post_status FROM $wpdb->posts p, $wpdb->posts c WHERE c.ID=$post AND c.post_parent=p.ID");
}
// END added by renaissancehack
$index_statuses = array('publish', 'private', 'draft', 'pending', 'future');
if (!in_array($post_status, $index_statuses)) {
// The post isn't supposed to be indexed anymore, remove it from index
relevanssi_remove_doc($post);
}
else {
relevanssi_publish($post);
}
}
function relevanssi_purge_excerpt_cache($post) {
global $wpdb, $relevanssi_excerpt_cache;
$wpdb->query("DELETE FROM $relevanssi_excerpt_cache WHERE post = $post");
}
function relevanssi_delete($post) {
relevanssi_remove_doc($post);
relevanssi_purge_excerpt_cache($post);
}
function relevanssi_publish($post) {
global $relevanssi_publish_doc;
$post_status = get_post_status($post);
if ('auto-draft' == $post_status) return;
$custom_fields = relevanssi_get_custom_fields();
relevanssi_index_doc($post, true, $custom_fields);
}
function relevanssi_install() {
global $wpdb, $relevanssi_table, $stopword_table, $title_boost_default,
$tag_boost_default, $comment_boost_default;
add_option('relevanssi_title_boost', $title_boost_default);
add_option('relevanssi_tag_boost', $tag_boost_default);
add_option('relevanssi_comment_boost', $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', '450');
add_option('relevanssi_excerpt_type', 'chars');
add_option('relevanssi_log_queries', 'off');
add_option('relevanssi_cat', '0');
add_option('relevanssi_excat', '0');
add_option('relevanssi_index_type', 'both');
add_option('relevanssi_index_fields', '');
add_option('relevanssi_exclude_posts', ''); //added by OdditY
add_option('relevanssi_include_tags', 'on'); //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_include_cats', '');
add_option('relevanssi_show_matches', '');
add_option('relevanssi_show_matches_txt', '(Search hits: %body% in body, %title% in title, %tags% in tags, %comments% in comments. Score: %score%)');
add_option('relevanssi_fuzzy', 'sometimes');
add_option('relevanssi_indexed', '');
add_option('relevanssi_expand_shortcodes', 'on');
add_option('relevanssi_custom_types', '');
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', '');
add_option('relevanssi_index_limit', '500');
add_option('relevanssi_index_attachments', '');
add_option('relevanssi_disable_or_fallback', 'off');
add_option('relevanssi_respect_exclude', 'on');
add_option('relevanssi_cache_seconds', '172800');
add_option('relevanssi_enable_cache', 'off');
add_option('relevanssi_min_word_length', 3);
add_option('relevanssi_wpml_only_current', 'on');
add_option('relevanssi_word_boundaries', 'on');
add_option('relevanssi_hidesponsor', 'false');
add_option('relevanssi_default_orderby', 'relevance');
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
$charset_collate = '';
if (!empty($wpdb->charset))
$charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
if (!empty($wpdb->collate))
$charset_collate .= " COLLATE $wpdb->collate";
$relevanssi_table = $wpdb->prefix . "relevanssi";
$stopword_table = $wpdb->prefix . "relevanssi_stopwords";
$log_table = $wpdb->prefix . "relevanssi_log";
$relevanssi_cache = $wpdb->prefix . 'relevanssi_cache';
$relevanssi_excerpt_cache = $wpdb->prefix . 'relevanssi_excerpt_cache';
if($wpdb->get_var("SHOW TABLES LIKE '$relevanssi_table'") != $relevanssi_table) {
$sql = "CREATE TABLE " . $relevanssi_table . " (id mediumint(9) NOT NULL AUTO_INCREMENT, "
. "doc bigint(20) NOT NULL, "
. "term varchar(50) NOT NULL, "
. "tf mediumint(9) NOT NULL DEFAULT '0', "
. "title tinyint(1) NOT NULL, "
. "UNIQUE KEY id (id)) $charset_collate;";
dbDelta($sql);
$sql = "CREATE INDEX terms ON $relevanssi_table (term(20))";
$wpdb->query($sql);
}
if($wpdb->get_var("SHOW TABLES LIKE '$stopword_table'") != $stopword_table) {
$sql = "CREATE TABLE " . $stopword_table . " (stopword varchar(50) NOT NULL, "
. "UNIQUE KEY stopword (stopword)) $charset_collate;";
dbDelta($sql);
}
if ($wpdb->get_var("SELECT COUNT(*) FROM $stopword_table WHERE 1") < 1) {
relevanssi_populate_stopwords();
}
if($wpdb->get_var("SHOW TABLES LIKE '$log_table'") != $log_table) {
$sql = "CREATE TABLE " . $log_table . " (id mediumint(9) NOT NULL AUTO_INCREMENT, "
. "query varchar(200) NOT NULL, "
. "hits mediumint(9) NOT NULL, "
. "time timestamp NOT NULL, "
. "UNIQUE KEY id (id)) $charset_collate;";
dbDelta($sql);
}
if($wpdb->get_var("SHOW TABLES LIKE '$relevanssi_cache'") != $relevanssi_cache) {
$sql = "CREATE TABLE " . $relevanssi_cache . " (param varchar(32) NOT NULL, "
. "hits text NOT NULL, "
. "tstamp timestamp NOT NULL, "
. "UNIQUE KEY param (param)) $charset_collate;";
dbDelta($sql);
}
if($wpdb->get_var("SHOW TABLES LIKE '$relevanssi_excerpt_cache'") != $relevanssi_excerpt_cache) {
$sql = "CREATE TABLE " . $relevanssi_excerpt_cache . " (query varchar(100) NOT NULL, "
. "post mediumint(9) NOT NULL, "
. "excerpt text NOT NULL, "
. "UNIQUE (query, post)) $charset_collate;";
dbDelta($sql);
}
}
if (function_exists('register_uninstall_hook')) {
register_uninstall_hook(__FILE__, 'relevanssi_uninstall');
// this doesn't seem to work
}
function relevanssi_uninstall() {
global $wpdb, $relevanssi_table, $log_table, $stopword_table;
delete_option('relevanssi_title_boost');
delete_option('relevanssi_tag_boost');
delete_option('relevanssi_comment_boost');
delete_option('relevanssi_admin_search');
delete_option('relevanssi_highlight');
delete_option('relevanssi_txt_col');
delete_option('relevanssi_bg_col');
delete_option('relevanssi_css');
delete_option('relevanssi_excerpts');
delete_option('relevanssi_excerpt_length');
delete_option('relevanssi_excerpt_type');
delete_option('relevanssi_log_queries');
delete_option('relevanssi_excat');
delete_option('relevanssi_cat');
delete_option('relevanssi_index_type');
delete_option('revelanssi_index_fields');
delete_option('relevanssi_exclude_posts'); //added by OdditY
delete_option('relevanssi_include_tags'); //added by OdditY
delete_option('relevanssi_hilite_title'); //added by OdditY
delete_option('relevanssi_index_comments'); //added by OdditY
delete_option('relevanssi_include_cats');
delete_option('relevanssi_show_matches');
delete_option('relevanssi_show_matches_text');
delete_option('relevanssi_fuzzy');
delete_option('relevanssi_indexed');
delete_option('relevanssi_expand_shortcodes');
delete_option('relevanssi_custom_types');
delete_option('relevanssi_custom_taxonomies');
delete_option('relevanssi_index_author');
delete_option('relevanssi_implicit_operator');
delete_option('relevanssi_omit_from_logs');
delete_option('relevanssi_synonyms');
delete_option('relevanssi_index_excerpt');
delete_option('relevanssi_highlight_docs');
delete_option('relevanssi_highlight_comments');
delete_option('relevanssi_index_limit');
delete_option('relevanssi_index_attachments');
delete_option('relevanssi_disable_or_fallback');
delete_option('relevanssi_respect_exclude');
delete_option('relevanssi_cache_seconds');
delete_option('relevanssi_enable_cache');
delete_option('relevanssi_min_word_length');
delete_option('relevanssi_wpml_only_current');
delete_option('relevanssi_word_boundaries');
delete_option('relevanssi_hidesponsor');
delete_option('relevanssi_default_orderby');
wp_clear_scheduled_hook('relevanssi_truncate_cache');
$relevanssi_table = $wpdb->prefix . "relevanssi";
$stopword_table = $wpdb->prefix . "relevanssi_stopwords";
$log_table = $wpdb->prefix . "relevanssi_log";
$relevanssi_cache = $wpdb->prefix . 'relevanssi_cache';
$relevanssi_excerpt_cache = $wpdb->prefix . 'relevanssi_excerpt_cache';
if($wpdb->get_var("SHOW TABLES LIKE '$stopword_table'") == $stopword_table) {
$sql = "DROP TABLE $stopword_table";
$wpdb->query($sql);
}
if($wpdb->get_var("SHOW TABLES LIKE '$relevanssi_table'") == $relevanssi_table) {
$sql = "DROP TABLE $relevanssi_table";
$wpdb->query($sql);
}
if($wpdb->get_var("SHOW TABLES LIKE '$log_table'") == $log_table) {
$sql = "DROP TABLE $log_table";
$wpdb->query($sql);
}
if($wpdb->get_var("SHOW TABLES LIKE '$relevanssi_cache'") == $relevanssi_cache) {
$sql = "DROP TABLE $relevanssi_cache";
$wpdb->query($sql);
}
if($wpdb->get_var("SHOW TABLES LIKE '$relevanssi_excerpt_cache'") == $relevanssi_excerpt_cache) {
$sql = "DROP TABLE $relevanssi_excerpt_cache";
$wpdb->query($sql);
}
echo '<div id="message" class="updated fade"><p>' . __("Data wiped clean, you can now delete the plugin.", "relevanssi") . '</p></div>';
}
//Added by OdditY ->
function relevanssi_comment_edit($comID) {
relevanssi_comment_index($comID,$action="update");
}
function relevanssi_comment_remove($comID) {
relevanssi_comment_index($comID,$action="remove");
}
function relevanssi_comment_index($comID,$action="add") {
global $wpdb;
$comtype = get_option("relevanssi_index_comments");
switch ($comtype) {
case "all":
// all (incl. customs, track-&pingbacks)
break;
case "normal":
// normal (excl. customs, track-&pingbacks)
$restriction=" AND comment_type='' ";
break;
default:
// none (don't index)
return ;
}
switch ($action) {
case "update":
//(update) comment status changed:
$cpostID = $wpdb->get_var("SELECT comment_post_ID FROM $wpdb->comments WHERE comment_ID='$comID'".$restriction);
break;
case "remove":
//(remove) approved comment will be deleted (if not approved, its not in index):
$cpostID = $wpdb->get_var("SELECT comment_post_ID FROM $wpdb->comments WHERE comment_ID='$comID' AND comment_approved='1'".$restriction);
if($cpostID!=NULL) {
//empty comment_content & reindex, then let WP delete the empty comment
$wpdb->query("UPDATE $wpdb->comments SET comment_content='' WHERE comment_ID='$comID'");
}
break;
default:
// (add) new comment:
$cpostID = $wpdb->get_var("SELECT comment_post_ID FROM $wpdb->comments WHERE comment_ID='$comID' AND comment_approved='1'".$restriction);
break;
}
if($cpostID!=NULL) relevanssi_publish($cpostID);
}
//Added by OdditY END <-
function relevanssi_populate_stopwords() {
global $wpdb, $stopword_table;
include('stopwords');
if (is_array($stopwords) && count($stopwords) > 0) {
foreach ($stopwords as $word) {
$q = $wpdb->prepare("INSERT IGNORE INTO $stopword_table (stopword) VALUES (%s)", trim($word));
$wpdb->query($q);
}
}
}
function relevanssi_fetch_stopwords() {
global $wpdb, $stopword_list, $stopword_table;
if (count($stopword_list) < 1) {
$results = $wpdb->get_results("SELECT stopword FROM $stopword_table");
foreach ($results as $word) {
$stopword_list[] = $word->stopword;
}
}
return $stopword_list;
}
add_filter('query_vars', 'relevanssi_query_vars');
function relevanssi_query_vars($qv) {
$qv[] = 'cats';
$qv[] = 'post_types';
return $qv;
}
function relevanssi_query($posts, $query = false) {
$admin_search = get_option('relevanssi_admin_search');
($admin_search == 'on') ? $admin_search = true : $admin_search = false;
global $relevanssi_active;
$search_ok = true; // we will search!
if (!is_search()) {
$search_ok = false; // no, we can't
}
if (is_search() && is_admin()) {
$search_ok = false; // but if this is an admin search, reconsider
if ($admin_search) $search_ok = true; // yes, we can search!
}
if ($relevanssi_active) {
$search_ok = false; // Relevanssi is already in action
}
if ($search_ok) {
global $wp_query;
$posts = relevanssi_do_query($wp_query);
}
return $posts;
}
function relevanssi_do_query(&$query) {
// this all is basically lifted from Kenny Katzgrau's wpSearch
// thanks, Kenny!
global $wpSearch_low;
global $wpSearch_high;
global $relevanssi_active;
$relevanssi_active = true;
$posts = array();
$q = trim(stripslashes(mb_strtolower($query->query_vars["s"])));
$cat = false;
if (isset($query->query_vars["cat"])) {
$cat = $query->query_vars["cat"];
}
if (isset($query->query_vars["cats"])) {
$cat = $query->query_vars["cats"];
}
if (!$cat) {
$cat = get_option('relevanssi_cat');
if (0 == $cat) {
$cat = false;
}
}
$tag = false;
if (isset($query->query_vars["tag"])) {
$tag = $query->query_vars["tag"];
}
if (isset($query->query_vars["tags"])) {
$tag = $query->query_vars["tags"];
}
$tax = false;
$tax_term = false;
if (isset($query->query_vars["taxonomy"])) {
$tax = $query->query_vars["taxonomy"];
$tax_term = $query->query_vars["term"];
}
if (!isset($excat)) {
$excat = get_option('relevanssi_excat');
if (0 == $excat) {
$excat = false;
}
}
$post_type = false;
if (isset($query->query_vars["post_type"]) && $query->query_vars["post_type"] != 'any') {
$post_type = $query->query_vars["post_type"];
}
if (isset($query->query_vars["post_types"])) {
$post_type = $query->query_vars["post_types"];
}
$author = false;
if (isset($query->query_vars["author"])) {
$author = $query->query_vars["author"];
}
$expids = get_option("relevanssi_exclude_posts");
if (is_admin()) {
// in admin search, search everything
$cat = null;
$excat = null;
$expids = null;
$tag = null;
$tax = null;
$tax_term = null;
}
$operator = get_option("relevanssi_implicit_operator");
// Add synonyms
// This is done here so the new terms will get highlighting
if ("OR" == $operator) {
// Synonyms are only used in OR queries
$synonym_data = get_option('relevanssi_synonyms');
if ($synonym_data) {
$synonyms = array();
$pairs = explode(";", $synonym_data);
foreach ($pairs as $pair) {
$parts = explode("=", $pair);
$key = trim($parts[0]);
$value = trim($parts[1]);
$synonyms[$key][$value] = true;
}
if (count($synonyms) > 0) {
$new_terms = array();
$terms = array_keys(relevanssi_tokenize($q, false)); // remove stopwords is false here
foreach ($terms as $term) {
if (in_array($term, array_keys($synonyms))) {
$new_terms = array_merge($new_terms, array_keys($synonyms[$term]));
}
}
if (count($new_terms) > 0) {
foreach ($new_terms as $new_term) {
$q .= " $new_term";
}
}
}
}
}
$cache = get_option('relevanssi_enable_cache');
$cache == 'on' ? $cache = true : $cache = false;
if ($cache) {
$params = md5(serialize(array($q, $cat, $excat, $expids, $post_type, $tax, $tax_term, $operator, $tag, $author)));
$return = relevanssi_fetch_hits($params);
if (!$return) {
$return = relevanssi_search($q, $cat, $excat, $expids, $post_type, $tax, $tax_term, $operator, $tag, $author);
$return_ser = serialize($return);
relevanssi_store_hits($params, $return_ser);
}
}
else {
$return = relevanssi_search($q, $cat, $excat, $expids, $post_type, $tax, $tax_term, $operator, $tag, $author);
}
$hits = $return['hits'];
$q = $return['query'];
$filter_data = array($hits, $q);
$hits_filters_applied = apply_filters('relevanssi_hits_filter', $filter_data);
$hits = $hits_filters_applied[0];
$query->found_posts = sizeof($hits);
$query->max_num_pages = ceil(sizeof($hits) / $query->query_vars["posts_per_page"]);
$update_log = get_option('relevanssi_log_queries');
if ('on' == $update_log) {
relevanssi_update_log($q, sizeof($hits));
}
$make_excerpts = get_option('relevanssi_excerpts');
if (is_paged()) {
$wpSearch_low = ($query->query_vars['paged'] - 1) * $query->query_vars["posts_per_page"];
}
else {
$wpSearch_low = 0;
}
$wpSearch_high = $wpSearch_low + $query->query_vars["posts_per_page"] - 1;
if ($wpSearch_high > sizeof($hits)) $wpSearch_high = sizeof($hits) - 1;
for ($i = $wpSearch_low; $i <= $wpSearch_high; $i++) {
if (isset($hits[intval($i)])) {
$post = $hits[intval($i)];
}
else {
continue;
}
if ($post == NULL) {
// apparently sometimes you can get a null object
continue;
}
//Added by OdditY - Highlight Result Title too ->
if("on" == get_option('relevanssi_hilite_title')){
if (function_exists('qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage')) {
$post->post_title = strip_tags(qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($post->post_title));
}
else {
$post->post_title = strip_tags($post->post_title);
}
$highlight = get_option('relevanssi_highlight');
if ("none" != $highlight) {
if (!is_admin()) {
$post->post_title = relevanssi_highlight_terms($post->post_title, $q);
}
}
}
// OdditY end <-
if ('on' == $make_excerpts) {
if ($cache) {
$post->post_excerpt = relevanssi_fetch_excerpt($post->ID, $q);
if ($post->post_excerpt == null) {
$post->post_excerpt = relevanssi_do_excerpt($post, $q);
relevanssi_store_excerpt($post->ID, $q, $post->post_excerpt);
}
}
else {
$post->post_excerpt = relevanssi_do_excerpt($post, $q);
}
if ('on' == get_option('relevanssi_show_matches')) {
$post->post_excerpt .= relevanssi_show_matches($return, $post->ID);
}
}
$post->relevance_score = round($return['scores'][$post->ID], 2);
$posts[] = $post;
}
$query->posts = $posts;
$query->post_count = count($posts);
return $posts;
}
function relevanssi_fetch_excerpt($post, $query) {
global $wpdb, $relevanssi_excerpt_cache;
$query = mysql_real_escape_string($query);
$excerpt = $wpdb->get_var("SELECT excerpt FROM $relevanssi_excerpt_cache WHERE post = $post AND query = '$query'");
if (!$excerpt) return null;
return $excerpt;
}
function relevanssi_store_excerpt($post, $query, $excerpt) {
global $wpdb, $relevanssi_excerpt_cache;
$query = mysql_real_escape_string($query);
$excerpt = mysql_real_escape_string($excerpt);
$wpdb->query("INSERT INTO $relevanssi_excerpt_cache (post, query, excerpt)
VALUES ($post, '$query', '$excerpt')
ON DUPLICATE KEY UPDATE excerpt = '$excerpt'");
}
function relevanssi_fetch_hits($param) {
global $wpdb, $relevanssi_cache;
$time = get_option('relevanssi_cache_seconds', 172800);
$hits = $wpdb->get_var("SELECT hits FROM $relevanssi_cache WHERE param = '$param' AND UNIX_TIMESTAMP() - UNIX_TIMESTAMP(tstamp) < $time");
if ($hits) {
return unserialize($hits);
}
else {
return null;
}
}
function relevanssi_store_hits($param, $data) {
global $wpdb, $relevanssi_cache;
$param = mysql_real_escape_string($param);
$data = mysql_real_escape_string($data);
$wpdb->query("INSERT INTO $relevanssi_cache (param, hits)
VALUES ('$param', '$data')
ON DUPLICATE KEY UPDATE hits = '$data'");
}
// thanks to rvencu
function relevanssi_wpml_filter($data) {
$use_filter = get_option('relevanssi_wpml_only_current');
if ('on' == $use_filter) {
//save current blog language
$lang = get_bloginfo('language');
$filtered_hits = array();
foreach ($data[0] as $hit) {
if (isset($hit->blog_id)) {
switch_to_blog($hit->blog_id);
}
global $sitepress;
if (function_exists('icl_object_id') && $sitepress->is_translated_post_type($hit->post_type)) {
if ($hit->ID == icl_object_id($hit->ID, $hit->post_type,false,ICL_LANGUAGE_CODE))
$filtered_hits[] = $hit;
}
// if there is no WPML but the target blog has identical language with current blog,
// we use the hits. Note en-US is not identical to en-GB!
elseif (get_bloginfo('language') == $lang) {
$filtered_hits[] = $hit;
}
if (isset($hit->blog_id)) {
restore_current_blog();
}
}
return array($filtered_hits, $data[1]);
}
return $data;
}
/**
* Function by Matthew Hood http://my.php.net/manual/en/function.sort.php#75036
*/
function objectSort(&$data, $key, $dir = 'desc') {
$dir = strtolower($dir);
for ($i = count($data) - 1; $i >= 0; $i--) {
$swapped = false;
for ($j = 0; $j < $i; $j++) {
if ('asc' == $dir) {
if ($data[$j]->$key > $data[$j + 1]->$key) {
$tmp = $data[$j];
$data[$j] = $data[$j + 1];
$data[$j + 1] = $tmp;
$swapped = true;
}
}
else {
if ($data[$j]->$key < $data[$j + 1]->$key) {
$tmp = $data[$j];
$data[$j] = $data[$j + 1];
$data[$j + 1] = $tmp;
$swapped = true;
}
}
}
if (!$swapped) return;
}
}
function relevanssi_show_matches($data, $hit) {
isset($data['body_matches'][$hit]) ? $body = $data['body_matches'][$hit] : $body = 0;
isset($data['title_matches'][$hit]) ? $title = $data['title_matches'][$hit] : $title = 0;
isset($data['tag_matches'][$hit]) ? $tag = $data['tag_matches'][$hit] : $tag = 0;
isset($data['comment_matches'][$hit]) ? $comment = $data['comment_matches'][$hit] : $comment = 0;
isset($data['scores'][$hit]) ? $score = round($data['scores'][$hit], 2) : $score = 0;
isset($data['term_hits'][$hit]) ? $term_hits_a = $data['term_hits'][$hit] : $term_hits_a = array();
arsort($term_hits_a);
$term_hits = "";
$total_hits = 0;
foreach ($term_hits_a as $term => $hits) {
$term_hits .= " $term: $hits";
$total_hits += $hits;
}
$text = get_option('relevanssi_show_matches_text');
$replace_these = array("%body%", "%title%", "%tags%", "%comments%", "%score%", "%terms%", "%total%");
$replacements = array($body, $title, $tag, $comment, $score, $term_hits, $total_hits);
$result = " " . str_replace($replace_these, $replacements, $text);
return $result;
}
function relevanssi_update_log($query, $hits) {
if(isset($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT'] == "Mediapartners-Google")
return;
global $wpdb, $log_table;
if (get_option('relevanssi_omit_from_logs')) {
$user = wp_get_current_user();
$omit = explode(",", get_option('relevanssi_omit_from_logs'));
if (in_array($user->ID, $omit)) return;
}
$q = $wpdb->prepare("INSERT INTO $log_table (query, hits) VALUES (%s, %d)", $query, intval($hits));
$wpdb->query($q);
}
// This is my own magic working.
function relevanssi_search($q, $cat = NULL, $excat = NULL, $expost = NULL, $post_type = NULL, $taxonomy = NULL, $taxonomy_term = NULL, $operator = "AND", $tag = NULL, $author = NULL) {
global $relevanssi_table, $wpdb;
$values_to_filter = array(
'q' => $q,
'cat' => $cat,
'tag' => $tag,
'excat' => $excat,
'expost' => $expost,
'post_type' => $post_type,
'taxonomy' => $taxonomy,
'taxonomy_term' => $taxonomy_term,
'operator' => $operator,
'author' => $author,
);
$filtered_values = apply_filters( 'relevanssi_search_filters', $values_to_filter );
$q = $filtered_values['q'];
$cat = $filtered_values['cat'];
$excat = $filtered_values['excat'];
$tag = $filtered_values['tag'];
$expost = $filtered_values['expost'];
$post_type = $filtered_values['post_type'];
$taxonomy = $filtered_values['taxonomy'];
$taxonomy_term = $filtered_values['taxonomy_term'];
$operator = $filtered_values['operator'];
$author = $filtered_values['author'];
$hits = array();
$custom_cat = NULL;
$o_cat = $cat;
$o_excat = $excat;
$o_expost = $expost;
$o_tag = $tag;
$o_post_type = $post_type;
$o_taxonomy = $taxonomy;
$o_taxonomy_term = $taxonomy_term;
$o_author = $author;
if ("custom" == $cat) {
$custom_field = "custom";
$post_ids = array();
$results = $wpdb->get_results("SELECT post_id FROM $wpdb->postmeta WHERE meta_key='$custom_field'");
foreach ($results as $row) {
$post_ids[] = $row->post_id;
}
$custom_cat = implode(",", $post_ids);
$cat = "";
}
else if ($cat) {
$cats = explode(",", $cat);
$inc_term_tax_ids = array();
$ex_term_tax_ids = array();
foreach ($cats as $t_cat) {
$exclude = false;
if ($t_cat < 0) {
// Negative category, ie. exclusion
$exclude = true;
$t_cat = substr($t_cat, 1); // strip the - sign.