Ticket #7394: 7394.diff
File 7394.diff, 7.1 KB (added by , 12 years ago) |
---|
-
wp-includes/functions.php
3560 3560 } 3561 3561 3562 3562 /** 3563 * Used internally to tidy up the search terms.3564 *3565 * @access private3566 * @since 2.9.03567 *3568 * @param string $t3569 * @return string3570 */3571 function _search_terms_tidy($t) {3572 return trim($t, "\"'\n\r ");3573 }3574 3575 /**3576 3563 * Returns true. 3577 3564 * 3578 3565 * Useful for returning true to filters easily. … … 3883 3870 */ 3884 3871 function wp_checkdate( $month, $day, $year, $source_date ) { 3885 3872 return apply_filters( 'wp_checkdate', checkdate( $month, $day, $year ), $source_date ); 3873 } 3874 3875 /** 3876 * Check if the terms are suitable for searching. 3877 * 3878 * Includes array of stopwords (terms) that are excluded from the separate term matching when searching for posts. 3879 * The list of English stopwords is the approximate search engines list. MySQL has a much longer default list of full-text stopwords. 3880 * 3881 * @access private 3882 * @since 3.6.0 3883 * 3884 * @param array Terms to check 3885 * @return array 3886 */ 3887 function _check_search_terms($terms) { 3888 $strtolower_func = function_exists( 'mb_strtolower' ) ? 'mb_strtolower' : 'strtolower'; 3889 $checked = $stopwords = array(); 3890 3891 $_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,the,www', 'Comma separated list of common words to exclude when searching (stopwords).' ) ); 3892 3893 foreach( $_words as $word ) { 3894 $word = trim( $word, "\r\n\t " ); 3895 if ( ! $word ) 3896 continue; 3897 $stopwords[] = $word; 3898 } 3899 3900 $stopwords = apply_filters( 'wp_search_stopwords', $stopwords ); 3901 3902 foreach ( $terms as $term ) { 3903 // keep before/after spaces when term is for exact match 3904 if ( preg_match( '/^".+"$/', $term ) ) 3905 $term = trim( $term, "\"'" ); 3906 else 3907 $term = trim( $term, "\"' " ); 3908 3909 // \p{L} matches a single letter that is not a Chinese, Japanese, etc. char 3910 if ( ! $term || preg_match( '/^\p{L}$/u', $term ) ) 3911 continue; 3912 3913 if ( in_array( $strtolower_func( $term ), $stopwords, true ) ) 3914 continue; 3915 3916 $checked[] = $term; 3917 } 3918 3919 return $checked; 3886 3920 } 3921 No newline at end of file -
wp-includes/query.php
2185 2185 } 2186 2186 2187 2187 // If a search pattern is specified, load the posts that match 2188 if ( !empty($q['s']) ) { 2188 // Sanity check: search string shouldn't be more than 1600 characters. 2189 // See ticket #21688 for more info. 2190 if ( ! empty( $q['s'] ) && strlen( $q['s'] ) < 1600 ) { 2189 2191 if ( empty( $_GET['s'] ) && $this->is_main_query() ) 2190 2192 $q['s'] = urldecode($q['s']); 2193 // there are no line breaks in <input /> fields 2194 $q['s'] = str_replace( array("\r", "\n"), '', $q['s'] ); 2195 $num_all_terms = 1; 2191 2196 if ( !empty($q['sentence']) ) { 2192 2197 $q['search_terms'] = array($q['s']); 2193 2198 } else { 2194 preg_match_all('/".*?("|$)|((?<=[\r\n\t ",+])|^)[^\r\n\t ",+]+/', $q['s'], $matches); 2195 $q['search_terms'] = array_map('_search_terms_tidy', $matches[0]); 2199 if ( preg_match_all( '/".*?("|$)|((?<=[\t ",+])|^)[^\t ",+]+/', $q['s'], $matches ) ) { 2200 $num_all_terms = count( $matches[0] ); 2201 $q['search_terms'] = _check_search_terms( $matches[0] ); 2202 // if the search string has only short terms or stopwords, or is 10+ terms long, match it as sentence 2203 if ( empty( $q['search_terms'] ) || count( $q['search_terms'] ) > 9 ) 2204 $q['search_terms'] = array( $q['s'] ); 2205 } else { 2206 $q['search_terms'] = array( $q['s'] ); 2207 } 2196 2208 } 2209 2197 2210 $n = !empty($q['exact']) ? '' : '%'; 2198 2211 $searchand = ''; 2199 foreach( (array) $q['search_terms'] as $term ) { 2212 $search_orderby_title = array(); 2213 foreach ( $q['search_terms'] as $term ) { 2200 2214 $term = esc_sql( like_escape( $term ) ); 2215 if ( $n ) 2216 $search_orderby_title[] = "$wpdb->posts.post_title LIKE '%$term%'"; 2217 2201 2218 $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))"; 2202 2219 $searchand = ' AND '; 2203 2220 } … … 2398 2415 $orderby .= " {$q['order']}"; 2399 2416 } 2400 2417 2418 // Order search results by relevance when another "orderby" is not specified in the query 2419 if ( ! empty( $search_orderby_title ) && empty( $q['orderby'] ) ) { 2420 $search_orderby = ''; 2421 // Allow plugins to override sorting on 'post_title' and/or 'post_content'. 2422 // Passing an empty array would disable sorting. 2423 $search_orderby_on = array( 'post_title', 'post_content' ); 2424 $search_orderby_on = apply_filters( 'posts_search_orderby_on', $search_orderby_on, $this ); 2425 2426 if ( $num_all_terms > 1 ) { 2427 $num_terms = count( $search_orderby_title ); 2428 $search_orderby_s = esc_sql( like_escape( $q['s'] ) ); 2429 2430 if ( in_array( 'post_title', $search_orderby_on, true ) ) { 2431 $search_orderby = '(CASE '; 2432 // sentence match in 'post_title' 2433 $search_orderby .= "WHEN $wpdb->posts.post_title LIKE '%{$search_orderby_s}%' THEN 1 "; 2434 2435 // sanity limit, sort as sentence when more than 6 terms 2436 // (few searches are longer than 6 terms and most titles are not) 2437 if ( $num_terms < 7 ) { 2438 // all words in title 2439 $search_orderby .= 'WHEN ' . implode( ' AND ', $search_orderby_title ) . ' THEN 2 '; 2440 // any word in title, not needed when $num_terms == 1 2441 if ( $num_terms > 1 ) 2442 $search_orderby .= 'WHEN ' . implode( ' OR ', $search_orderby_title ) . ' THEN 3 '; 2443 } 2444 2445 // sentence match in 'post_content' 2446 if ( in_array( 'post_content', $search_orderby_on, true ) ) 2447 $search_orderby .= "WHEN $wpdb->posts.post_content LIKE '%{$search_orderby_s}%' THEN 4 "; 2448 2449 $search_orderby .= 'ELSE 5 END)'; 2450 } elseif ( in_array( 'post_content', $search_orderby_on, true ) ) { 2451 // Not sorting on 'post_title', order by sentence matches in 'post_content' 2452 $search_orderby = "$wpdb->posts.post_content LIKE '%{$search_orderby_s}%' DESC"; 2453 } 2454 } else { 2455 // single word or sentence search 2456 if ( in_array( 'post_title', $search_orderby_on, true ) ) 2457 $search_orderby = reset( $search_orderby_title ) . ' DESC'; 2458 } 2459 // Allow plugins to add/remove/modify the 'order by' for the search section of the database query 2460 $search_orderby = apply_filters( 'posts_search_orderby', $search_orderby, $this ); 2461 if ( $search_orderby ) 2462 $orderby = $orderby ? $search_orderby . ', ' . $orderby : $search_orderby; 2463 } 2464 2401 2465 if ( is_array( $post_type ) ) { 2402 2466 $post_type_cap = 'multiple_post_type'; 2403 2467 } else { -
wp-includes/deprecated.php
3331 3331 function _save_post_hook() {} 3332 3332 3333 3333 /** 3334 * Formerly used internally to tidy up the search terms. 3335 * 3336 * @access private 3337 * @since 2.9.0 3338 * @deprecated 3.6.0 3339 */ 3340 function _search_terms_tidy( $t ) { 3341 _deprecated_function( __FUNCTION__, '3.5', '' ); 3342 return trim( $t, "\"'\n\r " ); 3343 } 3344 3345 /** 3334 3346 * Check if the installed version of GD supports particular image type 3335 3347 * 3336 3348 * @since 2.9.0