Ticket #7394: 7394.3.diff
File 7394.3.diff, 7.3 KB (added by , 11 years ago) |
---|
-
src/wp-includes/functions.php
3703 3703 } 3704 3704 3705 3705 /** 3706 * Used internally to tidy up the search terms.3707 *3708 * @access private3709 * @since 2.9.03710 *3711 * @param string $t3712 * @return string3713 */3714 function _search_terms_tidy($t) {3715 return trim($t, "\"'\n\r ");3716 }3717 3718 /**3719 3706 * Returns true. 3720 3707 * 3721 3708 * Useful for returning true to filters easily. … … 4042 4029 } 4043 4030 4044 4031 /** 4032 * Check if the terms are suitable for searching. 4033 * 4034 * Includes array of stopwords (terms) that are excluded from the separate term matching when searching for posts. 4035 * The list of English stopwords is the approximate search engines list. MySQL has a much longer default list of full-text stopwords. 4036 * 4037 * @since 3.6.0 4038 * @access private 4039 * 4040 * @param array Terms to check 4041 * @return array 4042 */ 4043 function _check_search_terms($terms) { 4044 $strtolower_func = function_exists( 'mb_strtolower' ) ? 'mb_strtolower' : 'strtolower'; 4045 $checked = $stopwords = array(); 4046 4047 $_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).' ) ); 4048 4049 foreach( $_words as $word ) { 4050 $word = trim( $word, "\r\n\t " ); 4051 if ( ! $word ) 4052 continue; 4053 $stopwords[] = $word; 4054 } 4055 4056 $stopwords = apply_filters( 'wp_search_stopwords', $stopwords ); 4057 4058 foreach ( $terms as $term ) { 4059 // keep before/after spaces when term is for exact match 4060 if ( preg_match( '/^".+"$/', $term ) ) 4061 $term = trim( $term, "\"'" ); 4062 else 4063 $term = trim( $term, "\"' " ); 4064 4065 // \p{L} matches a single letter that is not a Chinese, Japanese, etc. char 4066 if ( ! $term || preg_match( '/^\p{L}$/u', $term ) ) 4067 continue; 4068 4069 if ( in_array( $strtolower_func( $term ), $stopwords, true ) ) 4070 continue; 4071 4072 $checked[] = $term; 4073 } 4074 4075 return $checked; 4076 } 4077 4078 /** 4045 4079 * Load the auth check for monitoring whether the user is still logged in. 4046 4080 * 4047 4081 * Can be disabled with remove_action( 'admin_enqueue_scripts', 'wp_auth_check_load' ); -
src/wp-includes/query.php
2230 2230 } 2231 2231 2232 2232 // If a search pattern is specified, load the posts that match 2233 if ( !empty($q['s']) ) { 2233 // Sanity check: search string shouldn't be more than 1600 characters. 2234 // See ticket #21688 for more info. 2235 if ( ! empty( $q['s'] ) && strlen( $q['s'] ) < 1600 ) { 2234 2236 // added slashes screw with quote grouping when done early, so done later 2235 2237 $q['s'] = stripslashes($q['s']); 2236 2238 if ( empty( $_GET['s'] ) && $this->is_main_query() ) 2237 2239 $q['s'] = urldecode($q['s']); 2240 // there are no line breaks in <input /> fields 2241 $q['s'] = str_replace( array("\r", "\n"), '', $q['s'] ); 2242 $num_all_terms = 1; 2238 2243 if ( !empty($q['sentence']) ) { 2239 2244 $q['search_terms'] = array($q['s']); 2240 2245 } else { 2241 preg_match_all('/".*?("|$)|((?<=[\r\n\t ",+])|^)[^\r\n\t ",+]+/', $q['s'], $matches); 2242 $q['search_terms'] = array_map('_search_terms_tidy', $matches[0]); 2246 if ( preg_match_all( '/".*?("|$)|((?<=[\t ",+])|^)[^\t ",+]+/', $q['s'], $matches ) ) { 2247 $num_all_terms = count( $matches[0] ); 2248 $q['search_terms'] = _check_search_terms( $matches[0] ); 2249 // if the search string has only short terms or stopwords, or is 10+ terms long, match it as sentence 2250 if ( empty( $q['search_terms'] ) || count( $q['search_terms'] ) > 9 ) 2251 $q['search_terms'] = array( $q['s'] ); 2252 } else { 2253 $q['search_terms'] = array( $q['s'] ); 2254 } 2243 2255 } 2256 2244 2257 $n = !empty($q['exact']) ? '' : '%'; 2245 2258 $searchand = ''; 2246 foreach( (array) $q['search_terms'] as $term ) { 2259 $search_orderby_title = array(); 2260 foreach ( $q['search_terms'] as $term ) { 2247 2261 $term = esc_sql( like_escape( $term ) ); 2262 if ( $n ) 2263 $search_orderby_title[] = "$wpdb->posts.post_title LIKE '%$term%'"; 2264 2248 2265 $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))"; 2249 2266 $searchand = ' AND '; 2250 2267 } … … 3666 3683 else 3667 3684 $post_type = 'post'; 3668 3685 3686 // Order search results by relevance when another "orderby" is not specified in the query 3687 if ( ! empty( $search_orderby_title ) && empty( $q['orderby'] ) ) { 3688 $search_orderby = ''; 3689 // Allow plugins to override sorting on 'post_title' and/or 'post_content'. 3690 // Passing an empty array would disable sorting. 3691 $search_orderby_on = array( 'post_title', 'post_content' ); 3692 $search_orderby_on = apply_filters( 'posts_search_orderby_on', $search_orderby_on, $this ); 3693 3694 if ( $num_all_terms > 1 ) { 3695 $num_terms = count( $search_orderby_title ); 3696 $search_orderby_s = esc_sql( like_escape( $q['s'] ) ); 3697 3698 if ( in_array( 'post_title', $search_orderby_on, true ) ) { 3699 $search_orderby = '(CASE '; 3700 // sentence match in 'post_title' 3701 $search_orderby .= "WHEN $wpdb->posts.post_title LIKE '%{$search_orderby_s}%' THEN 1 "; 3702 3703 // sanity limit, sort as sentence when more than 6 terms 3704 // (few searches are longer than 6 terms and most titles are not) 3705 if ( $num_terms < 7 ) { 3706 // all words in title 3707 $search_orderby .= 'WHEN ' . implode( ' AND ', $search_orderby_title ) . ' THEN 2 '; 3708 // any word in title, not needed when $num_terms == 1 3709 if ( $num_terms > 1 ) 3710 $search_orderby .= 'WHEN ' . implode( ' OR ', $search_orderby_title ) . ' THEN 3 '; 3711 } 3712 3713 // sentence match in 'post_content' 3714 if ( in_array( 'post_content', $search_orderby_on, true ) ) 3715 $search_orderby .= "WHEN $wpdb->posts.post_content LIKE '%{$search_orderby_s}%' THEN 4 "; 3716 3717 $search_orderby .= 'ELSE 5 END)'; 3718 } elseif ( in_array( 'post_content', $search_orderby_on, true ) ) { 3719 // Not sorting on 'post_title', order by sentence matches in 'post_content' 3720 $search_orderby = "$wpdb->posts.post_content LIKE '%{$search_orderby_s}%' DESC"; 3721 } 3722 } else { 3723 // single word or sentence search 3724 if ( in_array( 'post_title', $search_orderby_on, true ) ) 3725 $search_orderby = reset( $search_orderby_title ) . ' DESC'; 3726 } 3727 // Allow plugins to add/remove/modify the 'order by' for the search section of the database query 3728 $search_orderby = apply_filters( 'posts_search_orderby', $search_orderby, $this ); 3729 if ( $search_orderby ) 3730 $orderby = $orderby ? $search_orderby . ', ' . $orderby : $search_orderby; 3731 } 3732 3669 3733 if ( is_array( $post_type ) ) { 3670 3734 if ( count( $post_type ) > 1 ) 3671 3735 return; -
src/wp-includes/deprecated.php
3317 3317 function _save_post_hook() {} 3318 3318 3319 3319 /** 3320 * Formerly used internally to tidy up the search terms. 3321 * 3322 * @access private 3323 * @since 2.9.0 3324 * @deprecated 3.6.0 3325 */ 3326 function _search_terms_tidy( $t ) { 3327 _deprecated_function( __FUNCTION__, '3.5', '' ); 3328 return trim( $t, "\"'\n\r " ); 3329 } 3330 3331 /** 3320 3332 * Check if the installed version of GD supports particular image type 3321 3333 * 3322 3334 * @since 2.9.0