/**
 * 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);
}
Beispiel #2
0
/**
 * Function to return the footer links of the results page.
 *
 * @param string $search_query
 * @param int $numrows
 * @param int $limit
 * @return string
 */
function get_bsearch_footer($search_query, $numrows, $limit)
{
    $match_range = get_bsearch_range($numrows, $limit);
    $page = $match_range[0];
    $pages = intval($numrows / $limit);
    // Number of results pages.
    if ($numrows % $limit) {
        $pages++;
        // If remainder so add one page
    }
    $search_query = urlencode($search_query);
    $output = '<p class="bsearch_footer">';
    if (0 != $page) {
        // Don't show back link if current page is first page.
        $back_page = $page - $limit;
        $output .= "<a href=\"" . home_url() . "/?s={$search_query}&limit={$limit}&bpaged={$back_page}\">&laquo; ";
        $output .= __('Previous', BSEARCH_LOCAL_NAME);
        $output .= "</a>    \n";
    }
    $pagination_range = 4;
    // Number of pagination elements
    for ($i = 1; $i <= $pages; $i++) {
        // loop through each page and give link to it.
        $current = $match_range[0] / $limit + 1;
        // Current page number.
        if ($i >= $current + $pagination_range && $i < $pages) {
            if ($i == $current + $pagination_range) {
                $output .= '&hellip;&nbsp;';
            }
            continue;
        }
        if ($i < $current - $pagination_range + 1 && $i < $pages) {
            continue;
        }
        $ppage = $limit * ($i - 1);
        if ($ppage == $page) {
            $output .= "<b>{$i}</b>\n";
            // If current page don't give link, just text.
        } else {
            $output .= "<a href=\"" . home_url() . "/?s={$search_query}&limit={$limit}&bpaged={$ppage}\">{$i}</a> \n";
        }
    }
    if (!(($page + $limit) / $limit >= $pages) && $pages != 1) {
        // If last page don't give next link.
        $next_page = $page + $limit;
        $output .= "    <a href=\"" . home_url() . "/?s={$search_query}&limit={$limit}&bpaged={$next_page}\">";
        $output .= __('Next', BSEARCH_LOCAL_NAME);
        $output .= " &raquo;</a>";
    }
    $output .= '</p>';
    return apply_filters('get_bsearch_footer', $output);
}