Ticket #7394: 7394-4.patch
File 7394-4.patch, 7.3 KB (added by , 12 years ago) |
---|
-
wp-includes/deprecated.php
3328 3328 * @since 2.3.0 3329 3329 * @deprecated 3.5.0 3330 3330 */ 3331 function _save_post_hook() {} 3332 No newline at end of file 3331 function _save_post_hook() {} 3332 3333 /** 3334 * Formerly used internally to tidy up the search terms. 3335 * 3336 * @access private 3337 * @since 2.9.0 3338 * @deprecated 3.5.0 3339 */ 3340 function _search_terms_tidy($t) { 3341 _deprecated_function( __FUNCTION__, '3.5', '' ); 3342 return trim($t, "\"'\n\r "); 3343 } -
wp-includes/functions.php
3470 3470 } 3471 3471 3472 3472 /** 3473 * Used internally to tidy up the search terms.3474 *3475 * @access private3476 * @since 2.9.03477 *3478 * @param string $t3479 * @return string3480 */3481 function _search_terms_tidy($t) {3482 return trim($t, "\"'\n\r ");3483 }3484 3485 /**3486 3473 * Returns true. 3487 3474 * 3488 3475 * Useful for returning true to filters easily. … … 3794 3781 function wp_checkdate( $month, $day, $year, $source_date ) { 3795 3782 return apply_filters( 'wp_checkdate', checkdate( $month, $day, $year ), $source_date ); 3796 3783 } 3784 3785 /** 3786 * Check if the terms are suitable for searching. 3787 * 3788 * Includes array of stopwords (terms) that are excluded from the separate term matching when searching for posts. 3789 * The list of English stopwords is the approximate search engines list. MySQL has a much longer default list of full-text stopwords. 3790 * 3791 * @access private 3792 * @since 3.5.0 3793 * 3794 * @param array Terms to check 3795 * @return array 3796 */ 3797 function _check_search_terms($terms) { 3798 $strtolower_func = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower'; 3799 $checked = $stopwords = array(); 3800 3801 $_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).') ); 3802 3803 foreach( $_words as $word ) { 3804 $word = trim($word, "\r\n\t "); 3805 if ( !$word ) 3806 continue; 3807 $stopwords[] = $word; 3808 } 3809 3810 $stopwords = apply_filters( 'wp_search_stopwords', $stopwords ); 3811 3812 foreach ( $terms as $term ) { 3813 // keep before/after spaces when term is for exact match 3814 if ( preg_match('/^".+"$/', $term) ) 3815 $term = trim($term, "\"'"); 3816 else 3817 $term = trim($term, "\"' "); 3818 3819 // \p{L} matches a single letter that is not a Chinese, Japanese, etc. char 3820 if ( !$term || preg_match( '/^\p{L}$/u', $term ) ) 3821 continue; 3822 3823 if ( in_array( $strtolower_func($term), $stopwords, true ) ) 3824 continue; 3825 3826 $checked[] = $term; 3827 } 3828 3829 return $checked; 3830 } 3831 -
wp-includes/query.php
2180 2180 } 2181 2181 } 2182 2182 2183 // If a search pattern is specified, load the posts that match 2184 if ( !empty($q['s']) ) { 2183 // If a search pattern is specified, load the posts that match. 2184 // Sanity check: search string shouldn't be more than 1600 characters. 2185 // See ticket #21688 for more info. 2186 if ( !empty($q['s']) && strlen($q['s']) < 1600 ) { 2185 2187 // added slashes screw with quote grouping when done early, so done later 2186 2188 $q['s'] = stripslashes($q['s']); 2187 2189 if ( empty( $_GET['s'] ) && $this->is_main_query() ) 2188 2190 $q['s'] = urldecode($q['s']); 2191 // there are no line breaks in <input /> fields 2192 $q['s'] = str_replace( array("\r", "\n"), '', $q['s'] ); 2193 $num_all_terms = 1; 2189 2194 if ( !empty($q['sentence']) ) { 2190 2195 $q['search_terms'] = array($q['s']); 2191 2196 } else { 2192 preg_match_all('/".*?("|$)|((?<=[\r\n\t ",+])|^)[^\r\n\t ",+]+/', $q['s'], $matches); 2193 $q['search_terms'] = array_map('_search_terms_tidy', $matches[0]); 2197 if ( preg_match_all('/".*?("|$)|((?<=[\t ",+])|^)[^\t ",+]+/', $q['s'], $matches) ) { 2198 $num_all_terms = count( $matches[0] ); 2199 $q['search_terms'] = _check_search_terms( $matches[0] ); 2200 // if the search string has only short terms or stopwords, or is 10+ terms long, match it as sentence 2201 if ( empty($q['search_terms']) || count($q['search_terms']) > 9 ) 2202 $q['search_terms'] = array($q['s']); 2203 } else { 2204 $q['search_terms'] = array($q['s']); 2205 } 2194 2206 } 2207 2195 2208 $n = !empty($q['exact']) ? '' : '%'; 2196 2209 $searchand = ''; 2197 foreach( (array) $q['search_terms'] as $term ) { 2210 $search_orderby_title = array(); 2211 foreach ( $q['search_terms'] as $term ) { 2198 2212 $term = esc_sql( like_escape( $term ) ); 2213 2214 if ( $n ) 2215 $search_orderby_title[] = "$wpdb->posts.post_title LIKE '%$term%'"; 2216 2199 2217 $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))"; 2200 2218 $searchand = ' AND '; 2201 2219 } … … 2395 2413 $orderby .= " {$q['order']}"; 2396 2414 } 2397 2415 2416 // Order search results by relevance when another "orderby" is not specified in the query 2417 if ( !empty($search_orderby_title) && empty($q['orderby']) ) { 2418 $search_orderby = ''; 2419 // Allow plugins to override sorting on 'post_title' and/or 'post_content'. 2420 // Passing an empty array would disable sorting. 2421 $search_orderby_on = array('post_title', 'post_content'); 2422 $search_orderby_on = apply_filters_ref_array( 'posts_search_orderby_on', array( $search_orderby_on, &$this ) ); 2423 2424 if ( $num_all_terms > 1 ) { 2425 $num_terms = count($search_orderby_title); 2426 $search_orderby_s = esc_sql( like_escape($q['s']) ); 2427 2428 if ( in_array('post_title', $search_orderby_on, true) ) { 2429 $search_orderby = '(CASE '; 2430 // sentence match in 'post_title' 2431 $search_orderby .= "WHEN $wpdb->posts.post_title LIKE '%{$search_orderby_s}%' THEN 1 "; 2432 2433 // sanity limit, sort as sentence when more than 6 terms 2434 // (few searches are longer than 6 terms and most titles are not) 2435 if ( $num_terms < 7 ) { 2436 // all words in title 2437 $search_orderby .= 'WHEN ' . implode( ' AND ', $search_orderby_title ) . ' THEN 2 '; 2438 // any word in title, not needed when $num_terms == 1 2439 if ( $num_terms > 1 ) 2440 $search_orderby .= 'WHEN ' . implode( ' OR ', $search_orderby_title ) . ' THEN 3 '; 2441 } 2442 2443 // sentence match in 'post_content' 2444 if ( in_array('post_content', $search_orderby_on, true) ) 2445 $search_orderby .= "WHEN $wpdb->posts.post_content LIKE '%{$search_orderby_s}%' THEN 4 "; 2446 2447 $search_orderby .= 'ELSE 5 END)'; 2448 } elseif ( in_array('post_content', $search_orderby_on, true) ) { 2449 // Not sorting on 'post_title', order by sentence matches in 'post_content' 2450 $search_orderby = "$wpdb->posts.post_content LIKE '%{$search_orderby_s}%' DESC"; 2451 } 2452 } else { 2453 // single word or sentence search 2454 if ( in_array('post_title', $search_orderby_on, true) ) 2455 $search_orderby = reset($search_orderby_title) . ' DESC'; 2456 } 2457 // Allow plugins to add/remove/modify the 'order by' for the search section of the database query 2458 $search_orderby = apply_filters_ref_array( 'posts_search_orderby', array( $search_orderby, &$this ) ); 2459 if ( $search_orderby ) 2460 $orderby = $orderby ? $search_orderby . ', ' . $orderby : $search_orderby; 2461 } 2462 2398 2463 if ( is_array( $post_type ) ) { 2399 2464 $post_type_cap = 'multiple_post_type'; 2400 2465 } else {