Ejemplo n.º 1
0
function relevanssi_options()
{
    global $relevanssi_variables;
    if (RELEVANSSI_PREMIUM) {
        $options_txt = __('Relevanssi Premium Search Options', 'relevanssi');
    } else {
        $options_txt = __('Relevanssi Search Options', 'relevanssi');
    }
    printf("<div class='wrap'><h2>%s</h2>", $options_txt);
    if (!empty($_POST)) {
        if (isset($_REQUEST['submit'])) {
            check_admin_referer(plugin_basename($relevanssi_variables['file']), 'relevanssi_options');
            update_relevanssi_options();
        }
        if (isset($_REQUEST['index'])) {
            check_admin_referer(plugin_basename($relevanssi_variables['file']), 'relevanssi_options');
            update_relevanssi_options();
            relevanssi_build_index();
        }
        if (isset($_REQUEST['index_extend'])) {
            check_admin_referer(plugin_basename($relevanssi_variables['file']), 'relevanssi_options');
            update_relevanssi_options();
            relevanssi_build_index(true);
        }
        if (isset($_REQUEST['import_options'])) {
            if (function_exists('relevanssi_import_options')) {
                check_admin_referer(plugin_basename($relevanssi_variables['file']), 'relevanssi_options');
                $options = $_REQUEST['relevanssi_settings'];
                relevanssi_import_options($options);
            }
        }
        if (isset($_REQUEST['search'])) {
            relevanssi_search($_REQUEST['q']);
        }
        if (isset($_REQUEST['dowhat'])) {
            if ("add_stopword" == $_REQUEST['dowhat']) {
                if (isset($_REQUEST['term'])) {
                    check_admin_referer(plugin_basename($relevanssi_variables['file']), 'relevanssi_options');
                    relevanssi_add_stopword($_REQUEST['term']);
                }
            }
        }
        if (isset($_REQUEST['addstopword'])) {
            check_admin_referer(plugin_basename($relevanssi_variables['file']), 'relevanssi_options');
            relevanssi_add_stopword($_REQUEST['addstopword']);
        }
        if (isset($_REQUEST['removestopword'])) {
            check_admin_referer(plugin_basename($relevanssi_variables['file']), 'relevanssi_options');
            relevanssi_remove_stopword($_REQUEST['removestopword']);
        }
        if (isset($_REQUEST['removeallstopwords'])) {
            check_admin_referer(plugin_basename($relevanssi_variables['file']), 'relevanssi_options');
            relevanssi_remove_all_stopwords();
        }
    }
    relevanssi_options_form();
    relevanssi_common_words();
    echo "<div style='clear:both'></div>";
    echo "</div>";
}
Ejemplo n.º 2
0
function relevanssi_options() {
	$options_txt = __('Relevanssi Search Options', 'relevanssi');

	printf("<div class='wrap'><h2>%s</h2>", $options_txt);

	if (isset($_REQUEST['submit'])) {
		update_relevanssi_options();
	}

	if (isset($_REQUEST['index'])) {
		update_relevanssi_options();
		relevanssi_build_index();
	}

	if (isset($_REQUEST['index_extend'])) {
		update_relevanssi_options();
		relevanssi_build_index(true);
	}
	
	if (isset($_REQUEST['search'])) {
		relevanssi_search($_REQUEST['q']);
	}
	
	if (isset($_REQUEST['uninstall'])) {
		relevanssi_uninstall();
	}
	
	if (isset($_REQUEST['dowhat'])) {
		if ("add_stopword" == $_REQUEST['dowhat']) {
			if (isset($_REQUEST['term'])) {
				relevanssi_add_stopword($_REQUEST['term']);
			}
		}
	}

	if (isset($_REQUEST['addstopword'])) {
		relevanssi_add_stopword($_REQUEST['addstopword']);
	}
	
	if (isset($_REQUEST['removestopword'])) {
		relevanssi_remove_stopword($_REQUEST['removestopword']);
	}
	
	relevanssi_options_form();
	
	relevanssi_common_words();
	
	echo "<div style='clear:both'></div>";
	
	echo "</div>";
}
function relevanssi_do_query(&$query)
{
    // this all is basically lifted from Kenny Katzgrau's wpSearch
    // thanks, Kenny!
    global $relevanssi_active;
    $relevanssi_active = true;
    $posts = array();
    if (function_exists('mb_strtolower')) {
        $q = trim(stripslashes(mb_strtolower($query->query_vars["s"])));
    } else {
        $q = trim(stripslashes(strtolower($query->query_vars["s"])));
    }
    $cache = get_option('relevanssi_enable_cache');
    $cache == 'on' ? $cache = true : ($cache = false);
    if (isset($query->query_vars['searchblogs'])) {
        $search_blogs = $query->query_vars['searchblogs'];
        $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"]) && $query->query_vars["post_types"] != 'any') {
            $post_type = $query->query_vars["post_types"];
        }
        if (function_exists('relevanssi_search_multi')) {
            $return = relevanssi_search_multi($q, $search_blogs, $post_type);
        }
    } else {
        $tax_query = array();
        $tax_query_relation = apply_filters('relevanssi_default_tax_query_relation', 'OR');
        if (isset($query->tax_query) && empty($query->tax_query->queries)) {
            // Tax query is empty, let's get rid of it.
            $query->tax_query = null;
        }
        if (isset($query->query_vars['tax_query'])) {
            // This is user-created tax_query array as described in WP Codex
            foreach ($query->query_vars['tax_query'] as $type => $item) {
                if (is_string($type) && $type == 'relation') {
                    $tax_query_relation = $item;
                } else {
                    $tax_query[] = $item;
                }
            }
        } else {
            if (isset($query->tax_query)) {
                // This is the WP-created Tax_Query object, which is different from above
                foreach ($query->tax_query as $type => $item) {
                    if (is_string($type) && $type == 'relation') {
                        $tax_query_relation = $item;
                    }
                    if (is_string($type) && $type == 'queries') {
                        foreach ($item as $tax_query_row) {
                            $tax_query[] = $tax_query_row;
                        }
                    }
                }
            } else {
                $cat = false;
                if (isset($query->query_vars["cats"])) {
                    $cat = $query->query_vars["cats"];
                }
                if (empty($cat)) {
                    $cat = get_option('relevanssi_cat');
                    if (0 == $cat) {
                        $cat = false;
                    }
                }
                if ($cat) {
                    $cat = explode(',', $cat);
                    $tax_query[] = array('taxonomy' => 'category', 'field' => 'id', 'terms' => $cat);
                }
                if (!empty($query->query_vars['category_name']) && empty($query->query_vars['category__in'])) {
                    $cat = explode(',', $query->query_vars['category_name']);
                    $tax_query[] = array('taxonomy' => 'category', 'field' => 'slug', 'terms' => $cat);
                }
                if (!empty($query->query_vars['category__in'])) {
                    $tax_query[] = array('taxonomy' => 'category', 'field' => 'id', 'terms' => $query->query_vars['category__in']);
                }
                if (!empty($query->query_vars['category__not_in'])) {
                    $tax_query[] = array('taxonomy' => 'category', 'field' => 'id', 'terms' => $query->query_vars['category__not_in'], 'operator' => 'NOT IN');
                }
                if (!empty($query->query_vars['category__and'])) {
                    $tax_query[] = array('taxonomy' => 'category', 'field' => 'id', 'terms' => $query->query_vars['category__and'], 'operator' => 'AND', 'include_children' => false);
                }
                $excat = get_option('relevanssi_excat');
                if (isset($excat) && $excat != 0) {
                    $tax_query[] = array('taxonomy' => 'category', 'field' => 'id', 'terms' => $excat, 'operator' => 'NOT IN');
                }
                $tag = false;
                if (isset($query->query_vars["tags"])) {
                    $tag = $query->query_vars["tags"];
                }
                if ($tag) {
                    if (strpos($tag, '+') !== false) {
                        $tag = explode('+', $tag);
                        $operator = 'and';
                    } else {
                        $tag = explode(',', $tag);
                        $operator = 'or';
                    }
                    $tax_query[] = array('taxonomy' => 'post_tag', 'field' => 'id', 'terms' => $tag, 'operator' => $operator);
                }
                if (!empty($query->query_vars['tag_id'])) {
                    $tax_query[] = array('taxonomy' => 'post_tag', 'field' => 'id', 'terms' => $query->query_vars['tag_id']);
                }
                if (!empty($query->query_vars['tag_id'])) {
                    $tax_query[] = array('taxonomy' => 'post_tag', 'field' => 'id', 'terms' => $query->query_vars['tag_id']);
                }
                if (!empty($query->query_vars['tag__in'])) {
                    $tax_query[] = array('taxonomy' => 'post_tag', 'field' => 'id', 'terms' => $query->query_vars['tag__in']);
                }
                if (!empty($query->query_vars['tag__not_in'])) {
                    $tax_query[] = array('taxonomy' => 'post_tag', 'field' => 'id', 'terms' => $query->query_vars['tag__not_in'], 'operator' => 'NOT IN');
                }
                if (!empty($query->query_vars['tag__and'])) {
                    $tax_query[] = array('taxonomy' => 'post_tag', 'field' => 'id', 'terms' => $query->query_vars['tag__and'], 'operator' => 'AND');
                }
                if (!empty($query->query_vars['tag__not_in'])) {
                    $tax_query[] = array('taxonomy' => 'post_tag', 'field' => 'id', 'terms' => $query->query_vars['tag__not_in'], 'operator' => 'NOT IN');
                }
                if (!empty($query->query_vars['tag_slug__in'])) {
                    $tax_query[] = array('taxonomy' => 'post_tag', 'field' => 'slug', 'terms' => $query->query_vars['tag_slug__in']);
                }
                if (!empty($query->query_vars['tag_slug__not_in'])) {
                    $tax_query[] = array('taxonomy' => 'post_tag', 'field' => 'slug', 'terms' => $query->query_vars['tag_slug__not_in'], 'operator' => 'NOT IN');
                }
                if (!empty($query->query_vars['tag_slug__and'])) {
                    $tax_query[] = array('taxonomy' => 'post_tag', 'field' => 'slug', 'terms' => $query->query_vars['tag_slug__and'], 'operator' => 'AND');
                }
                if (isset($query->query_vars["taxonomy"])) {
                    if (function_exists('relevanssi_process_taxonomies')) {
                        $tax_query = relevanssi_process_taxonomies($query->query_vars["taxonomy"], $query->query_vars["term"], $tax_query);
                    } else {
                        if (!empty($query->query_vars["term"])) {
                            $term = $query->query_vars["term"];
                        }
                        $tax_query[] = array('taxonomy' => $query->query_vars["taxonomy"], 'field' => 'slug', 'terms' => $term);
                    }
                }
            }
        }
        $author = false;
        if (!empty($query->query_vars["author"])) {
            $author = explode(',', $query->query_vars["author"]);
        }
        if (!empty($query->query_vars["author_name"])) {
            $author_object = get_user_by('slug', $query->query_vars["author_name"]);
            $author[] = $author_object->ID;
        }
        $post_query = array();
        if (!empty($query->query_vars['post__in'])) {
            $post_query = array('in' => $query->query_vars['post__in']);
        }
        if (!empty($query->query_vars['post__not_in'])) {
            $post_query = array('not in' => $query->query_vars['post__not_in']);
        }
        $meta_query = array();
        $meta_query_relation = apply_filters('relevanssi_default_meta_query_relation', 'AND');
        if (!empty($query->query_vars["meta_query"])) {
            $meta_query = $query->query_vars["meta_query"];
        }
        if (isset($query->query_vars["customfield_key"])) {
            isset($query->query_vars["customfield_value"]) ? $value = $query->query_vars["customfield_value"] : ($value = null);
            $meta_query[] = array('key' => $query->query_vars["customfield_key"], 'value' => $value, 'compare' => '=');
        }
        if (!empty($query->query_vars["meta_key"]) || !empty($query->query_vars["meta_value"]) || !empty($query->query_vars["meta_value_num"])) {
            $value = null;
            if (!empty($query->query_vars["meta_value"])) {
                $value = $query->query_vars["meta_value"];
            }
            if (!empty($query->query_vars["meta_value_num"])) {
                $value = $query->query_vars["meta_value_num"];
            }
            !empty($query->query_vars["meta_compare"]) ? $compare = $query->query_vars["meta_compare"] : ($compare = '=');
            $meta_query[] = array('key' => $query->query_vars["meta_key"], 'value' => $value, 'compare' => $compare);
        }
        $search_blogs = false;
        if (isset($query->query_vars["search_blogs"])) {
            $search_blogs = $query->query_vars["search_blogs"];
        }
        $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"]) && $query->query_vars["post_types"] != 'any') {
            $post_type = $query->query_vars["post_types"];
        }
        if ($post_type == -1) {
            $post_type = false;
        }
        $expids = get_option("relevanssi_exclude_posts");
        if (is_admin()) {
            // in admin search, search everything
            $excat = null;
            $expids = null;
        }
        $operator = "";
        if (function_exists('relevanssi_set_operator')) {
            $operator = relevanssi_set_operator($query);
            $operator = strtoupper($operator);
            // just in case
        }
        if ($operator != "OR" && $operator != "AND") {
            $operator = get_option("relevanssi_implicit_operator");
        }
        isset($query->query_vars['orderby']) ? $orderby = $query->query_vars['orderby'] : ($orderby = null);
        isset($query->query_vars['order']) ? $order = $query->query_vars['order'] : ($order = null);
        // Add synonyms
        // This is done here so the new terms will get highlighting
        if ("OR" == $operator) {
            // Synonyms are only used in OR queries
            $q = relevanssi_add_synonyms($q);
        }
        if ($cache) {
            $params = md5(serialize(array($q, $tax_query, $tax_query_relation, $post_query, $meta_query, $expids, $post_type, $operator, $search_blogs, $author, $orderby, $order)));
            $return = relevanssi_fetch_hits($params);
            if (!$return) {
                $return = relevanssi_search($q, $tax_query, $tax_query_relation, $post_query, $meta_query, $expids, $post_type, $operator, $search_blogs, $author, $orderby, $order);
                $return_ser = serialize($return);
                relevanssi_store_hits($params, $return_ser);
            }
        } else {
            $return = relevanssi_search($q, $tax_query, $tax_query_relation, $post_query, $meta_query, $expids, $post_type, $operator, $search_blogs, $author, $orderby, $order);
        }
    }
    isset($return['hits']) ? $hits = $return['hits'] : ($hits = array());
    isset($return['query']) ? $q = $return['query'] : ($q = "");
    $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);
    if ($query->query_vars["posts_per_page"] == 0) {
        // assume something sensible to prevent "division by zero error";
        $query->query_vars["posts_per_page"] = -1;
    }
    if ($query->query_vars["posts_per_page"] == -1) {
        $query->max_num_pages = sizeof($hits);
    } else {
        $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 ($query->is_admin) {
        $make_excerpts = false;
    }
    if ($query->query_vars['paged'] > 0) {
        $wpSearch_low = ($query->query_vars['paged'] - 1) * $query->query_vars["posts_per_page"];
    } else {
        $wpSearch_low = 0;
    }
    if ($query->query_vars["posts_per_page"] == -1) {
        $wpSearch_high = sizeof($hits);
    } else {
        $wpSearch_high = $wpSearch_low + $query->query_vars["posts_per_page"] - 1;
    }
    if (isset($query->query_vars['offset']) && $query->query_vars['offset'] > 0) {
        $wpSearch_high += $query->query_vars['offset'];
        $wpSearch_low += $query->query_vars['offset'];
    }
    if ($wpSearch_high > sizeof($hits)) {
        $wpSearch_high = sizeof($hits);
    }
    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);
            }
        }
        if (isset($return['scores'][$post->ID])) {
            $post->relevance_score = round($return['scores'][$post->ID], 2);
        }
        $posts[] = $post;
    }
    $query->posts = $posts;
    $query->post_count = count($posts);
    return $posts;
}
Ejemplo n.º 4
0
function relevanssi_options()
{
    $options_txt = __('Relevanssi Search Options', 'relevanssi');
    printf("<div class='wrap'><?php screen_icon(); ?><h2>%s</h2>", $options_txt);
    if (!empty($_REQUEST)) {
        if (isset($_REQUEST['hidesponsor'])) {
            update_option('relevanssi_hidesponsor', 'true');
        }
        if (isset($_REQUEST['submit'])) {
            update_relevanssi_options();
        }
        if (isset($_REQUEST['index'])) {
            update_relevanssi_options();
            relevanssi_build_index();
        }
        if (isset($_REQUEST['index_extend'])) {
            update_relevanssi_options();
            relevanssi_build_index(true);
        }
        if (isset($_REQUEST['search'])) {
            relevanssi_search($_REQUEST['q']);
        }
        if (isset($_REQUEST['uninstall'])) {
            relevanssi_uninstall();
        }
        if (isset($_REQUEST['dowhat'])) {
            if ("add_stopword" == $_REQUEST['dowhat']) {
                if (isset($_REQUEST['term'])) {
                    relevanssi_add_stopword($_REQUEST['term']);
                }
            }
        }
        if (isset($_REQUEST['addstopword'])) {
            relevanssi_add_stopword($_REQUEST['addstopword']);
        }
        if (isset($_REQUEST['removestopword'])) {
            relevanssi_remove_stopword($_REQUEST['removestopword']);
        }
        if (isset($_REQUEST['removeallstopwords'])) {
            relevanssi_remove_all_stopwords();
        }
        if (isset($_REQUEST['truncate'])) {
            $clear_all = true;
            relevanssi_truncate_cache($clear_all);
        }
    }
    relevanssi_options_form();
    relevanssi_common_words();
    echo "<div style='clear:both'></div>";
    echo "</div>";
}