+
+ do_action( 'parse_tax_query', $this );
+ }
+
+ /**
+ * Generate SQL for the WHERE clause based on passed search terms.
+ *
+ * @since 3.7.0
+ *
+ * @global wpdb $wpdb
+ * @param array $q Query variables.
+ * @return string WHERE clause.
+ */
+ protected function parse_search( &$q ) {
+ global $wpdb;
+
+ $search = '';
+
+ // added slashes screw with quote grouping when done early, so done later
+ $q['s'] = stripslashes( $q['s'] );
+ if ( empty( $_GET['s'] ) && $this->is_main_query() )
+ $q['s'] = urldecode( $q['s'] );
+ // there are no line breaks in <input /> fields
+ $q['s'] = str_replace( array( "\r", "\n" ), '', $q['s'] );
+ $q['search_terms_count'] = 1;
+ if ( ! empty( $q['sentence'] ) ) {
+ $q['search_terms'] = array( $q['s'] );
+ } else {
+ if ( preg_match_all( '/".*?("|$)|((?<=[\t ",+])|^)[^\t ",+]+/', $q['s'], $matches ) ) {
+ $q['search_terms_count'] = count( $matches[0] );
+ $q['search_terms'] = $this->parse_search_terms( $matches[0] );
+ // if the search string has only short terms or stopwords, or is 10+ terms long, match it as sentence
+ if ( empty( $q['search_terms'] ) || count( $q['search_terms'] ) > 9 )
+ $q['search_terms'] = array( $q['s'] );
+ } else {
+ $q['search_terms'] = array( $q['s'] );
+ }
+ }
+
+ $n = ! empty( $q['exact'] ) ? '' : '%';
+ $searchand = '';
+ $q['search_orderby_title'] = array();
+ foreach ( $q['search_terms'] as $term ) {
+ $term = like_escape( esc_sql( $term ) );
+ if ( $n )
+ $q['search_orderby_title'][] = "$wpdb->posts.post_title LIKE '%$term%'";
+
+ $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
+ $searchand = ' AND ';
+ }
+
+ if ( ! empty( $search ) ) {
+ $search = " AND ({$search}) ";
+ if ( ! is_user_logged_in() )
+ $search .= " AND ($wpdb->posts.post_password = '') ";
+ }
+
+ return $search;
+ }
+
+ /**
+ * Check if the terms are suitable for searching.
+ *
+ * Uses an array of stopwords (terms) that are excluded from the separate
+ * term matching when searching for posts. The list of English stopwords is
+ * the approximate search engines list, and is translatable.
+ *
+ * @since 3.7.0
+ *
+ * @param array Terms to check.
+ * @return array Terms that are not stopwords.
+ */
+ protected function parse_search_terms( $terms ) {
+ $strtolower = function_exists( 'mb_strtolower' ) ? 'mb_strtolower' : 'strtolower';
+ $checked = array();
+
+ $stopwords = $this->get_search_stopwords();
+
+ foreach ( $terms as $term ) {
+ // keep before/after spaces when term is for exact match
+ if ( preg_match( '/^".+"$/', $term ) )
+ $term = trim( $term, "\"'" );
+ else
+ $term = trim( $term, "\"' " );
+
+ // Avoid single A-Z.
+ if ( ! $term || ( 1 === strlen( $term ) && preg_match( '/^[a-z]$/i', $term ) ) )
+ continue;
+
+ if ( in_array( call_user_func( $strtolower, $term ), $stopwords, true ) )
+ continue;
+
+ $checked[] = $term;
+ }
+
+ return $checked;
+ }
+
+ /**
+ * Retrieve stopwords used when parsing search terms.
+ *
+ * @since 3.7.0
+ *
+ * @return array Stopwords.
+ */
+ protected function get_search_stopwords() {
+ if ( isset( $this->stopwords ) )
+ return $this->stopwords;
+
+ /* translators: This is a comma-separated list of very common words that should be excluded from a search,
+ * like a, an, and the. These are usually called "stopwords". You should not simply translate these individual
+ * words into your language. Instead, look for and provide commonly accepted stopwords in your language.
+ */
+ $words = explode( ',', _x( 'about,an,are,as,at,be,by,com,for,from,how,in,is,it,of,on,or,that,the,this,to,was,what,when,where,who,will,with,www',
+ 'Comma-separated list of search stopwords in your language' ) );
+
+ $stopwords = array();
+ foreach( $words as $word ) {
+ $word = trim( $word, "\r\n\t " );
+ if ( $word )
+ $stopwords[] = $word;
+ }
+
+ /**
+ * Filter stopwords used when parsing search terms.
+ *
+ * @since 3.7.0
+ *
+ * @param array $stopwords Stopwords.
+ */
+ $this->stopwords = apply_filters( 'wp_search_stopwords', $stopwords );
+ return $this->stopwords;
+ }
+
+ /**
+ * Generate SQL for the ORDER BY condition based on passed search terms.
+ *
+ * @global wpdb $wpdb
+ * @param array $q Query variables.
+ * @return string ORDER BY clause.
+ */
+ protected function parse_search_order( &$q ) {
+ global $wpdb;
+
+ $search_orderby = '';
+
+ if ( $q['search_terms_count'] > 1 ) {
+ $num_terms = count( $q['search_orderby_title'] );
+ $search_orderby_s = like_escape( esc_sql( $q['s'] ) );
+
+ $search_orderby = '(CASE ';
+ // sentence match in 'post_title'
+ $search_orderby .= "WHEN $wpdb->posts.post_title LIKE '%{$search_orderby_s}%' THEN 1 ";
+
+ // sanity limit, sort as sentence when more than 6 terms
+ // (few searches are longer than 6 terms and most titles are not)
+ if ( $num_terms < 7 ) {
+ // all words in title
+ $search_orderby .= 'WHEN ' . implode( ' AND ', $q['search_orderby_title'] ) . ' THEN 2 ';
+ // any word in title, not needed when $num_terms == 1
+ if ( $num_terms > 1 )
+ $search_orderby .= 'WHEN ' . implode( ' OR ', $q['search_orderby_title'] ) . ' THEN 3 ';
+ }
+
+ // sentence match in 'post_content'
+ $search_orderby .= "WHEN $wpdb->posts.post_content LIKE '%{$search_orderby_s}%' THEN 4 ";
+ $search_orderby .= 'ELSE 5 END)';
+ } else {
+ // single word or sentence search
+ $search_orderby = reset( $q['search_orderby_title'] ) . ' DESC';
+ }
+
+ return $search_orderby;