Make WordPress Core

Ticket #7394: 7394.4.diff

File 7394.4.diff, 7.3 KB (added by DrewAPicture, 11 years ago)

Baseline refresh at r25605

  • src/wp-includes/deprecated.php

     
    33173317function _save_post_hook() {}
    33183318
    33193319/**
     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 */
     3326function _search_terms_tidy( $t ) {
     3327        _deprecated_function( __FUNCTION__, '3.5', '' );
     3328        return trim( $t, "\"'\n\r " );
     3329}
     3330
     3331/**
    33203332 * Check if the installed version of GD supports particular image type
    33213333 *
    33223334 * @since 2.9.0
  • src/wp-includes/functions.php

     
    37033703}
    37043704
    37053705/**
    3706  * Used internally to tidy up the search terms.
    3707  *
    3708  * @access private
    3709  * @since 2.9.0
    3710  *
    3711  * @param string $t
    3712  * @return string
    3713  */
    3714 function _search_terms_tidy($t) {
    3715         return trim($t, "\"'\n\r ");
    3716 }
    3717 
    3718 /**
    37193706 * Returns true.
    37203707 *
    37213708 * Useful for returning true to filters easily.
     
    40424029}
    40434030
    40444031/**
     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 */
     4043function _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/**
    40454079 * Load the auth check for monitoring whether the user is still logged in.
    40464080 *
    40474081 * Can be disabled with remove_action( 'admin_enqueue_scripts', 'wp_auth_check_load' );
  • src/wp-includes/query.php

     
    22332233                }
    22342234
    22352235                // If a search pattern is specified, load the posts that match
    2236                 if ( !empty($q['s']) ) {
     2236                // Sanity check: search string shouldn't be more than 1600 characters.
     2237                // See ticket #21688 for more info.
     2238                if ( ! empty( $q['s'] ) && strlen( $q['s'] ) < 1600 ) {
    22372239                        // added slashes screw with quote grouping when done early, so done later
    22382240                        $q['s'] = stripslashes($q['s']);
    22392241                        if ( empty( $_GET['s'] ) && $this->is_main_query() )
    22402242                                $q['s'] = urldecode($q['s']);
     2243                        // there are no line breaks in <input /> fields
     2244                        $q['s'] = str_replace( array("\r", "\n"), '', $q['s'] );
     2245                        $num_all_terms = 1;
    22412246                        if ( !empty($q['sentence']) ) {
    22422247                                $q['search_terms'] = array($q['s']);
    22432248                        } else {
    2244                                 preg_match_all('/".*?("|$)|((?<=[\r\n\t ",+])|^)[^\r\n\t ",+]+/', $q['s'], $matches);
    2245                                 $q['search_terms'] = array_map('_search_terms_tidy', $matches[0]);
     2249                                if ( preg_match_all( '/".*?("|$)|((?<=[\t ",+])|^)[^\t ",+]+/', $q['s'], $matches ) ) {
     2250                                        $num_all_terms = count( $matches[0] );
     2251                                        $q['search_terms'] = _check_search_terms( $matches[0] );
     2252                                        // if the search string has only short terms or stopwords, or is 10+ terms long, match it as sentence
     2253                                        if ( empty( $q['search_terms'] ) || count( $q['search_terms'] ) > 9 )
     2254                                                $q['search_terms'] = array( $q['s'] );
     2255                                } else {
     2256                                        $q['search_terms'] = array( $q['s'] );
     2257                                }
    22462258                        }
     2259
    22472260                        $n = !empty($q['exact']) ? '' : '%';
    22482261                        $searchand = '';
    2249                         foreach( (array) $q['search_terms'] as $term ) {
     2262                        $search_orderby_title = array();
     2263                        foreach ( $q['search_terms'] as $term ) {
    22502264                                $term = esc_sql( like_escape( $term ) );
     2265                                if ( $n )
     2266                                        $search_orderby_title[] = "$wpdb->posts.post_title LIKE '%$term%'";
     2267
    22512268                                $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
    22522269                                $searchand = ' AND ';
    22532270                        }
     
    24582475                                $orderby .= " {$q['order']}";
    24592476                }
    24602477
     2478                // Order search results by relevance when another "orderby" is not specified in the query
     2479                if ( ! empty( $search_orderby_title ) && empty( $q['orderby'] ) ) {
     2480                        $search_orderby = '';
     2481                        // Allow plugins to override sorting on 'post_title' and/or 'post_content'.
     2482                        // Passing an empty array would disable sorting.
     2483                        $search_orderby_on = array( 'post_title', 'post_content' );
     2484                        $search_orderby_on = apply_filters( 'posts_search_orderby_on', $search_orderby_on, $this );
     2485
     2486                        if ( $num_all_terms > 1 ) {
     2487                                $num_terms = count( $search_orderby_title );
     2488                                $search_orderby_s = esc_sql( like_escape( $q['s'] ) );
     2489
     2490                                if ( in_array( 'post_title', $search_orderby_on, true ) ) {
     2491                                        $search_orderby = '(CASE ';
     2492                                        // sentence match in 'post_title'
     2493                                        $search_orderby .= "WHEN $wpdb->posts.post_title LIKE '%{$search_orderby_s}%' THEN 1 ";
     2494
     2495                                        // sanity limit, sort as sentence when more than 6 terms
     2496                                        // (few searches are longer than 6 terms and most titles are not)
     2497                                        if ( $num_terms < 7 ) {
     2498                                                // all words in title
     2499                                                $search_orderby .= 'WHEN ' . implode( ' AND ', $search_orderby_title ) . ' THEN 2 ';
     2500                                                // any word in title, not needed when $num_terms == 1
     2501                                                if ( $num_terms > 1 )
     2502                                                        $search_orderby .= 'WHEN ' . implode( ' OR ', $search_orderby_title ) . ' THEN 3 ';
     2503                                        }
     2504
     2505                                        // sentence match in 'post_content'
     2506                                        if ( in_array( 'post_content', $search_orderby_on, true ) )
     2507                                                $search_orderby .= "WHEN $wpdb->posts.post_content LIKE '%{$search_orderby_s}%' THEN 4 ";
     2508
     2509                                        $search_orderby .= 'ELSE 5 END)';
     2510                                } elseif ( in_array( 'post_content', $search_orderby_on, true ) ) {
     2511                                        // Not sorting on 'post_title', order by sentence matches in 'post_content'
     2512                                        $search_orderby = "$wpdb->posts.post_content LIKE '%{$search_orderby_s}%' DESC";
     2513                                }
     2514                        } else {
     2515                                // single word or sentence search
     2516                                if ( in_array( 'post_title', $search_orderby_on, true ) )
     2517                                        $search_orderby = reset( $search_orderby_title ) . ' DESC';
     2518                        }
     2519                        // Allow plugins to add/remove/modify the 'order by' for the search section of the database query
     2520                        $search_orderby = apply_filters( 'posts_search_orderby', $search_orderby, $this );
     2521                        if ( $search_orderby )
     2522                                $orderby = $orderby ? $search_orderby . ', ' . $orderby : $search_orderby;
     2523                }
     2524
    24612525                if ( is_array( $post_type ) && count( $post_type ) > 1 ) {
    24622526                        $post_type_cap = 'multiple_post_type';
    24632527                } else {