+
+ /**
+ * Fires after taxonomy-related query vars have been parsed.
+ *
+ * @since 3.7.0
+ *
+ * @param WP_Query $this The WP_Query instance.
+ */
+ 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 ) {
+ if ( $n ) {
+ $like = '%' . $wpdb->esc_like( $term ) . '%';
+ $q['search_orderby_title'][] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $like );
+ }
+
+ $like = $n . $wpdb->esc_like( $term ) . $n;
+ $search .= $wpdb->prepare( "{$searchand}(($wpdb->posts.post_title LIKE %s) OR ($wpdb->posts.post_content LIKE %s))", $like, $like );
+ $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 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;
+
+ if ( $q['search_terms_count'] > 1 ) {
+ $num_terms = count( $q['search_orderby_title'] );
+ $like = '%' . $wpdb->esc_like( $q['s'] ) . '%';
+
+ $search_orderby = '(CASE ';
+ // sentence match in 'post_title'
+ $search_orderby .= $wpdb->prepare( "WHEN $wpdb->posts.post_title LIKE %s THEN 1 ", $like );
+
+ // 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 .= $wpdb->prepare( "WHEN $wpdb->posts.post_content LIKE %s THEN 4 ", $like );
+ $search_orderby .= 'ELSE 5 END)';
+ } else {
+ // single word or sentence search
+ $search_orderby = reset( $q['search_orderby_title'] ) . ' DESC';
+ }
+
+ return $search_orderby;
+ }
+
+ /**
+ * If the passed orderby value is allowed, convert the alias to a
+ * properly-prefixed orderby value.
+ *
+ * @since 4.0.0
+ * @access protected
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
+ * @param string $orderby Alias for the field to order by.
+ * @return string|false Table-prefixed value to used in the ORDER clause. False otherwise.
+ */
+ protected function parse_orderby( $orderby ) {
+ global $wpdb;
+
+ // Used to filter values.
+ $allowed_keys = array(
+ 'post_name', 'post_author', 'post_date', 'post_title', 'post_modified',
+ 'post_parent', 'post_type', 'name', 'author', 'date', 'title', 'modified',
+ 'parent', 'type', 'ID', 'menu_order', 'comment_count', 'rand',
+ );
+
+ $primary_meta_key = '';
+ $primary_meta_query = false;
+ $meta_clauses = $this->meta_query->get_clauses();
+ if ( ! empty( $meta_clauses ) ) {
+ $primary_meta_query = reset( $meta_clauses );
+
+ if ( ! empty( $primary_meta_query['key'] ) ) {
+ $primary_meta_key = $primary_meta_query['key'];
+ $allowed_keys[] = $primary_meta_key;
+ }
+
+ $allowed_keys[] = 'meta_value';
+ $allowed_keys[] = 'meta_value_num';
+ $allowed_keys = array_merge( $allowed_keys, array_keys( $meta_clauses ) );
+ }
+
+ if ( ! in_array( $orderby, $allowed_keys ) ) {
+ return false;
+ }
+
+ switch ( $orderby ) {
+ case 'post_name':
+ case 'post_author':
+ case 'post_date':
+ case 'post_title':
+ case 'post_modified':
+ case 'post_parent':
+ case 'post_type':
+ case 'ID':
+ case 'menu_order':
+ case 'comment_count':
+ $orderby_clause = "$wpdb->posts.{$orderby}";
+ break;
+ case 'rand':
+ $orderby_clause = 'RAND()';
+ break;
+ case $primary_meta_key:
+ case 'meta_value':
+ if ( ! empty( $primary_meta_query['type'] ) ) {
+ $orderby_clause = "CAST({$primary_meta_query['alias']}.meta_value AS {$primary_meta_query['cast']})";
+ } else {
+ $orderby_clause = "{$primary_meta_query['alias']}.meta_value";
+ }
+ break;
+ case 'meta_value_num':
+ $orderby_clause = "{$primary_meta_query['alias']}.meta_value+0";
+ break;
+ default:
+ if ( array_key_exists( $orderby, $meta_clauses ) ) {
+ // $orderby corresponds to a meta_query clause.
+ $meta_clause = $meta_clauses[ $orderby ];
+ $orderby_clause = "CAST({$meta_clause['alias']}.meta_value AS {$meta_clause['cast']})";
+ } else {
+ // Default: order by post field.
+ $orderby_clause = "$wpdb->posts.post_" . sanitize_key( $orderby );
+ }
+
+ break;
+ }
+
+ return $orderby_clause;
+ }
+
+ /**
+ * Parse an 'order' query variable and cast it to ASC or DESC as necessary.
+ *
+ * @since 4.0.0
+ * @access protected
+ *
+ * @param string $order The 'order' query variable.
+ * @return string The sanitized 'order' query variable.
+ */
+ protected function parse_order( $order ) {
+ if ( ! is_string( $order ) || empty( $order ) ) {
+ return 'DESC';
+ }
+
+ if ( 'ASC' === strtoupper( $order ) ) {
+ return 'ASC';
+ } else {
+ return 'DESC';
+ }