Пример #1
0
/**
 * Gets the search results.
 *
 * @since	1.2
 *
 * @param	string     $search_query	Search term.
 * @param	int|string $limit			Maximum number of search results.
 * @return	string     Search results
 */
function get_bsearch_results($search_query = '', $limit = '')
{
    global $wpdb, $bsearch_settings;
    if (!$limit) {
        $limit = isset($_GET['limit']) ? intval($_GET['limit']) : $bsearch_settings['limit'];
        // Read from GET variable.
    }
    $bydate = isset($_GET['bydate']) ? intval($_GET['bydate']) : 0;
    // Order by date or by score?
    $topscore = 0;
    $matches = get_bsearch_matches($search_query, $bydate);
    // Fetch the search results for the search term stored in $search_query.
    $searches = $matches[0];
    // 0 index contains the search results always.
    if ($searches) {
        foreach ($searches as $search) {
            if ($topscore < $search->score) {
                $topscore = $search->score;
            }
        }
        $numrows = count($searches);
    } else {
        $numrows = 1;
    }
    $match_range = get_bsearch_range($numrows, $limit);
    $searches = array_slice($searches, $match_range[0], $match_range[1] - $match_range[0] + 1);
    // Extract the elements for the page from the complete results array.
    $output = '';
    /* Lets start printing the results */
    if ('' != $search_query) {
        if ($searches) {
            $output .= get_bsearch_header($search_query, $numrows, $limit);
            $search_query = preg_quote($search_query, '/');
            $keys = explode(' ', str_replace(array("'", "\"", "&quot;"), "", $search_query));
            foreach ($searches as $search) {
                $score = $search->score;
                $search = get_post($search->ID);
                $post_title = get_the_title($search->ID);
                /* Highlight the search terms in the title */
                if ($bsearch_settings['highlight']) {
                    $post_title = preg_replace('/(' . implode('|', $keys) . ')/iu', '<span class="bsearch_highlight">$1</span>', $post_title);
                }
                $output .= '<h2><a href="' . get_permalink($search->ID) . '" rel="bookmark">' . $post_title . '</a></h2>';
                $output .= '<p>';
                $output .= '<span class="bsearch_score">' . get_bsearch_score($search, $score, $topscore) . '</span>';
                $before = __('Posted on: ', 'better-search');
                $output .= '<span class="bsearch_date">' . get_bsearch_date($search, __('Posted on: ', 'better-search')) . '</span>';
                $output .= '</p>';
                $output .= '<p>';
                if ($bsearch_settings['include_thumb']) {
                    $output .= '<p class="bsearch_thumb">' . get_the_post_thumbnail($search->ID, 'thumbnail') . '</p>';
                }
                $excerpt = get_bsearch_excerpt($search->ID, $bsearch_settings['excerpt_length']);
                /* Highlight the search terms in the excerpt */
                if ($bsearch_settings['highlight']) {
                    $excerpt = preg_replace('/(' . implode('|', $keys) . ')/iu', '<span class="bsearch_highlight">$1</span>', $excerpt);
                }
                $output .= '<span class="bsearch_excerpt">' . $excerpt . '</span>';
                $output .= '</p>';
            }
            //end of foreach loop
            $output .= get_bsearch_footer($search_query, $numrows, $limit);
        } else {
            $output .= '<p>';
            $output .= __('No results.', 'better-search');
            $output .= '</p>';
        }
    } else {
        $output .= '<p>';
        $output .= __('Please type in your search terms. Use descriptive words since this search is intelligent.', 'better-search');
        $output .= '</p>';
    }
    if ($bsearch_settings['show_credit']) {
        $output .= '<hr /><p style="text-align:center">';
        $output .= __('Powered by ', 'better-search');
        $output .= '<a href="https://webberzone.com/plugins/better-search/">Better Search plugin</a></p>';
    }
    /**
     * Filter formatted string with search results
     *
     * @since	1.2
     *
     * @param	string	$output			Formatted results
     * @param	string	$search_query	Search query
     * @param	int		$limit			Number of results per page
     */
    return apply_filters('get_bsearch_results', $output, $search_query, $limit);
}
Пример #2
0
/**
 * Gets the search results
 *
 * @param string $search_query Search term
 * @param int|string $limit Maximum number of search results
 * @return string Search results
 */
function get_bsearch_results($search_query = '', $limit)
{
    global $wpdb, $bsearch_settings;
    if (!$limit) {
        $limit = isset($_GET['limit']) ? intval($_GET['limit']) : $bsearch_settings['limit'];
        // Read from GET variable
    }
    $bydate = isset($_GET['bydate']) ? intval($_GET['bydate']) : 0;
    // Order by date or by score?
    $topscore = 0;
    $matches = get_bsearch_matches($search_query, $bydate);
    // Fetch the search results for the search term stored in $search_query
    $searches = $matches[0];
    // 0 index contains the search results always
    if ($searches) {
        foreach ($searches as $search) {
            if ($topscore < $search->score) {
                $topscore = $search->score;
            }
        }
        $numrows = count($searches);
    } else {
        $numrows = 1;
    }
    $match_range = get_bsearch_range($numrows, $limit);
    $searches = array_slice($searches, $match_range[0], $match_range[1] - $match_range[0] + 1);
    // Extract the elements for the page from the complete results array
    $output = '';
    // Lets start printing the results
    if ('' != $search_query) {
        if ($searches) {
            $output .= get_bsearch_header($search_query, $numrows, $limit);
            foreach ($searches as $search) {
                $score = $search->score;
                $search = get_post($search->ID);
                $post_title = get_the_title($search->ID);
                $output .= '<h2><a href="' . get_permalink($search->ID) . '" rel="bookmark">' . $post_title . '</a></h2>';
                $output .= '<p>';
                $output .= '<span class="bsearch_score">' . get_bsearch_score($search, $score, $topscore) . '</span>';
                $before = __('Posted on: ', BSEARCH_LOCAL_NAME);
                $output .= '<span class="bsearch_date">' . get_bsearch_date($search, __('Posted on: ', BSEARCH_LOCAL_NAME)) . '</span>';
                $output .= '</p>';
                $output .= '<p>';
                if ($bsearch_settings['include_thumb']) {
                    $output .= '<p class="bsearch_thumb">' . get_the_post_thumbnail($search->ID, 'thumbnail') . '</p>';
                }
                $output .= '<span class="bsearch_excerpt">' . get_bsearch_excerpt($search->ID, $bsearch_settings['excerpt_length']) . '</span>';
                // This displays the post excerpt / creates it. Replace with $output .= $content; to use content instead of excerpt
                $output .= '</p>';
            }
            //end of foreach loop
            $output .= get_bsearch_footer($search_query, $numrows, $limit);
        } else {
            $output .= '<p>';
            $output .= __('No results.', BSEARCH_LOCAL_NAME);
            $output .= '</p>';
        }
    } else {
        $output .= '<p>';
        $output .= __('Please type in your search terms. Use descriptive words since this search is intelligent.', BSEARCH_LOCAL_NAME);
        $output .= '</p>';
    }
    if ($bsearch_settings['show_credit']) {
        $output .= '<hr /><p style="text-align:center">';
        $output .= __('Powered by ', BSEARCH_LOCAL_NAME);
        $output .= '<a href="http://ajaydsouza.com/wordpress/plugins/better-search/">Better Search plugin</a></p>';
    }
    return apply_filters('get_bsearch_results', $output);
}