.
*/
// 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_publish'); // 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', 'wp-content/plugins/' . $plugin_dir, $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 "
" . $post->post_excerpt . "
";
}
else {
echo __('There is no excerpt because this is a protected post.');
}
}
function relevanssi_do_excerpt($post, $query) {
$remove_stopwords = false;
$terms = relevanssi_tokenize($query, $remove_stopwords);
$content = apply_filters('the_content', $post->post_content);
$content = apply_filters('relevanssi_excerpt_content', $content, $post, $query);
if ('on' == get_option('relevanssi_expand_shortcodes')) {
if (function_exists("do_shortcode")) {
$content = do_shortcode($content);
}
}
else {
if (function_exists("strip_shortcodes")) {
$content = strip_shortcodes($content);
}
}
$content = relevanssi_strip_invisibles($content); // removes @siu',
'@%s
", $options_txt);
echo '
';
if ('on' == get_option('relevanssi_log_queries')) {
relevanssi_query_log();
}
else {
echo "
Enable query logging to see stats here.
";
}
echo "
";
relevanssi_sidebar();
}
function relevanssi_truncate_logs() {
global $wpdb, $log_table;
$query = "TRUNCATE $log_table";
$wpdb->query($query);
echo "
Logs clear!
";
}
function update_relevanssi_options() {
if (isset($_REQUEST['relevanssi_title_boost'])) {
$boost = floatval($_REQUEST['relevanssi_title_boost']);
update_option('relevanssi_title_boost', $boost);
}
if (isset($_REQUEST['relevanssi_tag_boost'])) {
$boost = floatval($_REQUEST['relevanssi_tag_boost']);
update_option('relevanssi_tag_boost', $boost);
}
if (isset($_REQUEST['relevanssi_comment_boost'])) {
$boost = floatval($_REQUEST['relevanssi_comment_boost']);
update_option('relevanssi_comment_boost', $boost);
}
if (isset($_REQUEST['relevanssi_min_word_length'])) {
$value = intval($_REQUEST['relevanssi_min_word_length']);
if ($value == 0) $value = 3;
update_option('relevanssi_min_word_length', $value);
}
if (isset($_REQUEST['relevanssi_cache_seconds'])) {
$value = intval($_REQUEST['relevanssi_cache_seconds']);
if ($value == 0) $value = 86400;
update_option('relevanssi_cache_seconds', $value);
}
if (!isset($_REQUEST['relevanssi_admin_search'])) {
$_REQUEST['relevanssi_admin_search'] = "off";
}
if (!isset($_REQUEST['relevanssi_excerpts'])) {
$_REQUEST['relevanssi_excerpts'] = "off";
}
if (!isset($_REQUEST['relevanssi_show_matches'])) {
$_REQUEST['relevanssi_show_matches'] = "off";
}
if (!isset($_REQUEST['relevanssi_inccats'])) {
$_REQUEST['relevanssi_inccats'] = "off";
}
if (!isset($_REQUEST['relevanssi_inctags'])) {
$_REQUEST['relevanssi_inctags'] = "off";
}
if (!isset($_REQUEST['relevanssi_index_author'])) {
$_REQUEST['relevanssi_index_author'] = "off";
}
if (!isset($_REQUEST['relevanssi_index_excerpt'])) {
$_REQUEST['relevanssi_index_excerpt'] = "off";
}
if (!isset($_REQUEST['relevanssi_log_queries'])) {
$_REQUEST['relevanssi_log_queries'] = "off";
}
if (!isset($_REQUEST['relevanssi_disable_or_fallback'])) {
$_REQUEST['relevanssi_disable_or_fallback'] = "off";
}
if (!isset($_REQUEST['relevanssi_index_attachments'])) {
$_REQUEST['relevanssi_index_attachments'] = "off";
}
if (!isset($_REQUEST['relevanssi_hilite_title'])) {
$_REQUEST['relevanssi_hilite_title'] = "off";
}
if (!isset($_REQUEST['relevanssi_highlight_docs'])) {
$_REQUEST['relevanssi_highlight_docs'] = "off";
}
if (!isset($_REQUEST['relevanssi_highlight_comments'])) {
$_REQUEST['relevanssi_highlight_comments'] = "off";
}
if (!isset($_REQUEST['relevanssi_expand_shortcodes'])) {
$_REQUEST['relevanssi_expand_shortcodes'] = "off";
}
if (!isset($_REQUEST['relevanssi_enable_cache'])) {
$_REQUEST['relevanssi_enable_cache'] = "off";
}
if (!isset($_REQUEST['relevanssi_respect_exclude'])) {
$_REQUEST['relevanssi_respect_exclude'] = "off";
}
if (!isset($_REQUEST['relevanssi_wpml_only_current'])) {
$_REQUEST['relevanssi_wpml_only_current'] = "off";
}
if (!isset($_REQUEST['relevanssi_word_boundaries'])) {
$_REQUEST['relevanssi_word_boundaries'] = "off";
}
if (isset($_REQUEST['relevanssi_excerpt_length'])) {
$value = intval($_REQUEST['relevanssi_excerpt_length']);
if ($value != 0) {
update_option('relevanssi_excerpt_length', $value);
}
}
if (isset($_REQUEST['relevanssi_synonyms'])) {
$linefeeds = array("\r\n", "\n", "\r");
$value = str_replace($linefeeds, ";", $_REQUEST['relevanssi_synonyms']);
$value = stripslashes($value);
update_option('relevanssi_synonyms', $value);
}
if (isset($_REQUEST['relevanssi_show_matches'])) update_option('relevanssi_show_matches', $_REQUEST['relevanssi_show_matches']);
if (isset($_REQUEST['relevanssi_show_matches_text'])) {
$value = $_REQUEST['relevanssi_show_matches_text'];
$value = str_replace('"', "'", $value);
update_option('relevanssi_show_matches_text', $value);
}
if (isset($_REQUEST['relevanssi_admin_search'])) update_option('relevanssi_admin_search', $_REQUEST['relevanssi_admin_search']);
if (isset($_REQUEST['relevanssi_excerpts'])) update_option('relevanssi_excerpts', $_REQUEST['relevanssi_excerpts']);
if (isset($_REQUEST['relevanssi_excerpt_type'])) update_option('relevanssi_excerpt_type', $_REQUEST['relevanssi_excerpt_type']);
if (isset($_REQUEST['relevanssi_log_queries'])) update_option('relevanssi_log_queries', $_REQUEST['relevanssi_log_queries']);
if (isset($_REQUEST['relevanssi_highlight'])) update_option('relevanssi_highlight', $_REQUEST['relevanssi_highlight']);
if (isset($_REQUEST['relevanssi_highlight_docs'])) update_option('relevanssi_highlight_docs', $_REQUEST['relevanssi_highlight_docs']);
if (isset($_REQUEST['relevanssi_highlight_comments'])) update_option('relevanssi_highlight_comments', $_REQUEST['relevanssi_highlight_comments']);
if (isset($_REQUEST['relevanssi_txt_col'])) update_option('relevanssi_txt_col', $_REQUEST['relevanssi_txt_col']);
if (isset($_REQUEST['relevanssi_bg_col'])) update_option('relevanssi_bg_col', $_REQUEST['relevanssi_bg_col']);
if (isset($_REQUEST['relevanssi_css'])) update_option('relevanssi_css', $_REQUEST['relevanssi_css']);
if (isset($_REQUEST['relevanssi_class'])) update_option('relevanssi_class', $_REQUEST['relevanssi_class']);
if (isset($_REQUEST['relevanssi_cat'])) update_option('relevanssi_cat', $_REQUEST['relevanssi_cat']);
if (isset($_REQUEST['relevanssi_excat'])) update_option('relevanssi_excat', $_REQUEST['relevanssi_excat']);
if (isset($_REQUEST['relevanssi_index_type'])) update_option('relevanssi_index_type', $_REQUEST['relevanssi_index_type']);
if (isset($_REQUEST['relevanssi_custom_types'])) update_option('relevanssi_custom_types', $_REQUEST['relevanssi_custom_types']);
if (isset($_REQUEST['relevanssi_custom_taxonomies'])) update_option('relevanssi_custom_taxonomies', $_REQUEST['relevanssi_custom_taxonomies']);
if (isset($_REQUEST['relevanssi_index_fields'])) update_option('relevanssi_index_fields', $_REQUEST['relevanssi_index_fields']);
if (isset($_REQUEST['relevanssi_expst'])) update_option('relevanssi_exclude_posts', $_REQUEST['relevanssi_expst']); //added by OdditY
if (isset($_REQUEST['relevanssi_inctags'])) update_option('relevanssi_include_tags', $_REQUEST['relevanssi_inctags']); //added by OdditY
if (isset($_REQUEST['relevanssi_hilite_title'])) update_option('relevanssi_hilite_title', $_REQUEST['relevanssi_hilite_title']); //added by OdditY
if (isset($_REQUEST['relevanssi_index_comments'])) update_option('relevanssi_index_comments', $_REQUEST['relevanssi_index_comments']); //added by OdditY
if (isset($_REQUEST['relevanssi_inccats'])) update_option('relevanssi_include_cats', $_REQUEST['relevanssi_inccats']);
if (isset($_REQUEST['relevanssi_index_author'])) update_option('relevanssi_index_author', $_REQUEST['relevanssi_index_author']);
if (isset($_REQUEST['relevanssi_index_excerpt'])) update_option('relevanssi_index_excerpt', $_REQUEST['relevanssi_index_excerpt']);
if (isset($_REQUEST['relevanssi_fuzzy'])) update_option('relevanssi_fuzzy', $_REQUEST['relevanssi_fuzzy']);
if (isset($_REQUEST['relevanssi_expand_shortcodes'])) update_option('relevanssi_expand_shortcodes', $_REQUEST['relevanssi_expand_shortcodes']);
if (isset($_REQUEST['relevanssi_implicit_operator'])) update_option('relevanssi_implicit_operator', $_REQUEST['relevanssi_implicit_operator']);
if (isset($_REQUEST['relevanssi_omit_from_logs'])) update_option('relevanssi_omit_from_logs', $_REQUEST['relevanssi_omit_from_logs']);
if (isset($_REQUEST['relevanssi_index_limit'])) update_option('relevanssi_index_limit', $_REQUEST['relevanssi_index_limit']);
if (isset($_REQUEST['relevanssi_index_attachments'])) update_option('relevanssi_index_attachments', $_REQUEST['relevanssi_index_attachments']);
if (isset($_REQUEST['relevanssi_disable_or_fallback'])) update_option('relevanssi_disable_or_fallback', $_REQUEST['relevanssi_disable_or_fallback']);
if (isset($_REQUEST['relevanssi_respect_exclude'])) update_option('relevanssi_respect_exclude', $_REQUEST['relevanssi_respect_exclude']);
if (isset($_REQUEST['relevanssi_enable_cache'])) update_option('relevanssi_enable_cache', $_REQUEST['relevanssi_enable_cache']);
if (isset($_REQUEST['relevanssi_wpml_only_current'])) update_option('relevanssi_wpml_only_current', $_REQUEST['relevanssi_wpml_only_current']);
if (isset($_REQUEST['relevanssi_word_boundaries'])) update_option('relevanssi_word_boundaries', $_REQUEST['relevanssi_word_boundaries']);
if (isset($_REQUEST['relevanssi_default_orderby'])) update_option('relevanssi_default_orderby', $_REQUEST['relevanssi_default_orderby']);
}
function relevanssi_add_stopword($term) {
global $wpdb, $relevanssi_table, $stopword_table;
if ('' == $term) return; // do not add empty $term to stopwords - added by renaissancehack
$n = 0;
$s = 0;
$terms = explode(',', $term);
if (count($terms) > 1) {
foreach($terms as $term) {
$n++;
$term = trim($term);
$success = relevanssi_add_single_stopword($term);
if ($success) $s++;
}
printf(__("
Successfully added %d/%d terms to stopwords!
", "relevanssi"), $s, $n);
}
else {
// add to stopwords
$success = relevanssi_add_single_stopword($term);
if ($success) {
printf(__("
Term '%s' added to stopwords!
", "relevanssi"), $term);
}
else {
printf(__("
Couldn't add term '%s' to stopwords!
", "relevanssi"), $term);
}
}
}
function relevanssi_add_single_stopword($term) {
global $wpdb, $relevanssi_table, $stopword_table;
if ('' == $term) return;
$q = $wpdb->prepare("INSERT INTO $stopword_table (stopword) VALUES (%s)", $term);
$success = $wpdb->query($q);
if ($success) {
// remove from index
$q = $wpdb->prepare("DELETE FROM $relevanssi_table WHERE term=%s", $term);
$wpdb->query($q);
return true;
}
else {
return false;
}
}
function relevanssi_remove_all_stopwords() {
global $wpdb, $stopword_table;
$q = $wpdb->prepare("TRUNCATE $stopword_table");
$success = $wpdb->query($q);
printf(__("
Stopwords removed! Remember to re-index.
", "relevanssi"), $term);
}
function relevanssi_remove_stopword($term) {
global $wpdb, $stopword_table;
$q = $wpdb->prepare("DELETE FROM $stopword_table WHERE stopword = '$term'");
$success = $wpdb->query($q);
if ($success) {
printf(__("
Term '%s' removed from stopwords! Re-index to get it back to index.
", "relevanssi"), $term);
}
else {
printf(__("
Couldn't remove term '%s' from stopwords!
", "relevanssi"), $term);
}
}
function relevanssi_common_words() {
global $wpdb, $relevanssi_table, $wp_version;
echo "
";
echo "
" . __("25 most common words in the index", 'relevanssi') . "
";
echo "
" . __("These words are excellent stopword material. A word that appears in most of the posts in the database is quite pointless when searching. This is also an easy way to create a completely new stopword list, if one isn't available in your language. Click the icon after the word to add the word to the stopword list. The word will also be removed from the index, so rebuilding the index is not necessary.", 'relevanssi') . "
";
$words = $wpdb->get_results("SELECT COUNT(DISTINCT(doc)) as cnt, term
FROM $relevanssi_table GROUP BY term ORDER BY cnt DESC LIMIT 25");
echo '
";
echo "
";
}
function relevanssi_query_log() {
global $log_table, $wpdb;
$lead = __("Here you can see the 20 most common user search queries, how many times those
queries were made and how many results were found for those queries.", 'relevanssi');
echo "
$lead
";
echo "
";
relevanssi_date_queries(1, __("Today and yesterday", 'relevanssi'));
echo '
';
echo "
";
relevanssi_date_queries(7, __("Last 7 days", 'relevanssi'));
echo '
';
echo "
";
relevanssi_date_queries(30, __("Last 30 days", 'relevanssi'));
echo '
';
echo '
';
echo '
' . __("Unsuccessful Queries", 'relevanssi') . '
';
echo "
";
relevanssi_date_queries(1, __("Today and yesterday", 'relevanssi'), 'bad');
echo '
';
echo "
";
relevanssi_date_queries(7, __("Last 7 days", 'relevanssi'), 'bad');
echo '
';
echo "
";
relevanssi_date_queries(30, __("Last 30 days", 'relevanssi'), 'bad');
echo '
';
if ( current_user_can('manage_options') ) {
echo '
';
echo <<
Reset Logs
EOR;
}
echo " ";
}
function relevanssi_date_queries($d, $title, $version = 'good') {
global $wpdb, $log_table;
if ($version == 'good')
$queries = $wpdb->get_results("SELECT COUNT(DISTINCT(id)) as cnt, query, hits
FROM $log_table
WHERE TIMESTAMPDIFF(DAY, time, NOW()) <= $d
GROUP BY query
ORDER BY cnt DESC
LIMIT 20");
if ($version == 'bad')
$queries = $wpdb->get_results("SELECT COUNT(DISTINCT(id)) as cnt, query, hits
FROM $log_table
WHERE TIMESTAMPDIFF(DAY, time, NOW()) <= $d
AND hits = 0
GROUP BY query
ORDER BY time DESC
LIMIT 20");
if (count($queries) > 0) {
echo "
Enter a word here to add it to the list of stopwords. The word will automatically be removed from the index, so re-indexing is not necessary. You can enter many words at the same time, separate words with commas.", 'relevanssi');
?>