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;
}
示例#2
0
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;
}