/** * Get the matches for the search term. * * @since 1.2 * * @param string $search_info Search terms array * @param bool $bydate Sort by date? * @return array Search results */ function get_bsearch_matches($search_query, $bydate) { global $wpdb, $bsearch_settings; // if there are two items in $search_info, the string has been broken into separate terms that // are listed at $search_info[1]. The cleaned-up version of $search_query is still at the zero index. // This is when fulltext is disabled, and we search using LIKE $search_info = get_bsearch_terms($search_query); // Get search transient $search_query_transient = 'bs_' . preg_replace('/[^A-Za-z0-9\\-]/', '', str_replace(' ', '', $search_query)); /** * Filter name of the search transient * * @since 2.1.0 * * @param string $search_query_transient Transient name * @param array $search_query Search query */ $search_query_transient = apply_filters('bsearch_transient_name', $search_query_transient, $search_query); $search_query_transient = substr($search_query_transient, 0, 40); // Name of the transient limited to 40 chars $matches = get_transient($search_query_transient); if ($matches) { if (isset($matches['search_query'])) { if ($matches['search_query'] == $search_query) { $results = $matches[0]; /** * Filter array holding the search results * * @since 1.2 * * @param object $matches Search results object * @param array $search_info Search query */ return apply_filters('get_bsearch_matches', $matches, $search_info); } } } // If no transient is set if (!isset($results)) { $sql = bsearch_sql_prepare($search_info, $bsearch_settings['boolean_mode'], $bydate); $results = $wpdb->get_results($sql); } // If no results are found then force BOOLEAN mode if (!$results) { $sql = bsearch_sql_prepare($search_info, 1, $bydate); $results = $wpdb->get_results($sql); } // If no results are found then force LIKE mode if (!$results) { // strip out all the fancy characters that fulltext would use $search_query = addslashes_gpc($search_query); $search_query = preg_replace('/, +/', ' ', $search_query); $search_query = str_replace(',', ' ', $search_query); $search_query = str_replace('"', ' ', $search_query); $search_query = trim($search_query); $search_words = explode(' ', $search_query); $s_array[0] = $search_query; // Save original query at [0] $s_array[1] = $search_words; // Save array of terms at [1] $search_info = $s_array; $sql = bsearch_sql_prepare($search_info, 0, $bydate); $results = $wpdb->get_results($sql); } $matches[0] = $results; $matches['search_query'] = $search_query; if ($bsearch_settings['cache']) { // Set search transient set_transient($search_query_transient, $matches, 7200); } /** * Described in better-search.php */ return apply_filters('get_bsearch_matches', $matches, $search_info); }
/** * Get the matches for the search term. * * @param mixed $search_info Search terms array * @param mixed $bydate Sort by date? * @return array Search results */ function get_bsearch_matches($search_info, $bydate) { global $wpdb, $bsearch_settings; parse_str($bsearch_settings['post_types'], $post_types); // Save post types in $post_types variable $n = '%'; // if there are two items in $search_info, the string has been broken into separate terms that // are listed at $search_info[1]. The cleaned-up version of $search_query is still at the zero index. // This is when fulltext is disabled, and we search using LIKE $search_info = get_bsearch_terms(); if (count($search_info) > 1) { $search_terms = $search_info[1]; $args = array($n . $search_terms[0] . $n, $n . $search_terms[0] . $n); $sql = "SELECT ID, 0 AS score FROM " . $wpdb->posts . " WHERE ("; $sql .= "((post_title LIKE '%s') OR (post_content LIKE '%s'))"; for ($i = 1; $i < count($search_terms); $i = $i + 1) { $sql .= " AND ((post_title LIKE '%s') OR (post_content LIKE '%s'))"; $args[] = $n . $search_terms[$i] . $n; $args[] = $n . $search_terms[$i] . $n; } $sql .= " OR (post_title LIKE '%s') OR (post_content LIKE '%s')"; $args[] = $n . $search_info[0] . $n; $args[] = $n . $search_info[0] . $n; $sql .= ") AND post_status = 'publish' "; $sql .= "AND ( "; $multiple = false; foreach ($post_types as $post_type) { if ($multiple) { $sql .= ' OR '; } $sql .= " post_type = '%s' "; $multiple = true; $args[] = $post_type; // Add the post types to the $args array } $sql .= " ) "; $sql .= "ORDER BY post_date DESC "; } else { $boolean_mode = $bsearch_settings['boolean_mode'] ? ' IN BOOLEAN MODE' : ''; $args = array($search_info[0], $bsearch_settings['weight_title'], $search_info[0], $bsearch_settings['weight_content'], $search_info[0]); $sql = "SELECT ID, "; $sql .= "(MATCH(post_title) AGAINST ('%s' {$boolean_mode} ) * %d ) + "; $sql .= "(MATCH(post_content) AGAINST ('%s' {$boolean_mode} ) * %d ) "; $sql .= "AS score FROM " . $wpdb->posts . " WHERE MATCH (post_title,post_content) AGAINST ('%s' {$boolean_mode} ) AND post_status = 'publish' "; $sql .= "AND ( "; $multiple = false; foreach ($post_types as $post_type) { if ($multiple) { $sql .= ' OR '; } $sql .= " post_type = '%s' "; $multiple = true; $args[] = $post_type; // Add the post types to the $args array } $sql .= " ) "; if ($bydate) { $sql .= "ORDER BY post_date DESC "; } else { $sql .= "ORDER BY score DESC "; } } $matches[0] = $wpdb->get_results($wpdb->prepare($sql, $args)); $matches[1] = $sql; return apply_filters('get_bsearch_matches', $matches); }