Make WordPress Core

Ticket #7394: 7394.2.diff

File 7394.2.diff, 7.2 KB (added by DrewAPicture, 12 years ago)

Refresh at r24174

  • wp-includes/deprecated.php

     
    33293329function _save_post_hook() {}
    33303330
    33313331/**
     3332 * Formerly used internally to tidy up the search terms.
     3333 *
     3334 * @access private
     3335 * @since 2.9.0
     3336 * @deprecated 3.6.0
     3337 */
     3338function _search_terms_tidy( $t ) {
     3339        _deprecated_function( __FUNCTION__, '3.5', '' );
     3340        return trim( $t, "\"'\n\r " );
     3341}
     3342
     3343/**
    33323344 * Check if the installed version of GD supports particular image type
    33333345 *
    33343346 * @since 2.9.0
  • wp-includes/functions.php

     
    35613561}
    35623562
    35633563/**
    3564  * Used internally to tidy up the search terms.
    3565  *
    3566  * @access private
    3567  * @since 2.9.0
    3568  *
    3569  * @param string $t
    3570  * @return string
    3571  */
    3572 function _search_terms_tidy($t) {
    3573         return trim($t, "\"'\n\r ");
    3574 }
    3575 
    3576 /**
    35773564 * Returns true.
    35783565 *
    35793566 * Useful for returning true to filters easily.
     
    38873874}
    38883875
    38893876/**
     3877 * Check if the terms are suitable for searching.
     3878 *
     3879 * Includes array of stopwords (terms) that are excluded from the separate term matching when searching for posts.
     3880 * The list of English stopwords is the approximate search engines list. MySQL has a much longer default list of full-text stopwords.
     3881 *
     3882 * @since 3.6.0
     3883 * @access private
     3884 *
     3885 * @param array Terms to check
     3886 * @return array
     3887 */
     3888function _check_search_terms($terms) {
     3889        $strtolower_func = function_exists( 'mb_strtolower' ) ? 'mb_strtolower' : 'strtolower';
     3890        $checked = $stopwords = array();
     3891
     3892        $_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).' ) );
     3893
     3894        foreach( $_words as $word ) {
     3895                $word = trim( $word, "\r\n\t " );
     3896                if ( ! $word )
     3897                        continue;
     3898                $stopwords[] = $word;
     3899        }
     3900
     3901        $stopwords = apply_filters( 'wp_search_stopwords', $stopwords );
     3902
     3903        foreach ( $terms as $term ) {
     3904                // keep before/after spaces when term is for exact match
     3905                if ( preg_match( '/^".+"$/', $term ) )
     3906                        $term = trim( $term, "\"'" );
     3907                else
     3908                        $term = trim( $term, "\"' " );
     3909
     3910                // \p{L} matches a single letter that is not a Chinese, Japanese, etc. char
     3911                if ( ! $term || preg_match( '/^\p{L}$/u', $term ) )
     3912                        continue;
     3913
     3914                if ( in_array( $strtolower_func( $term ), $stopwords, true ) )
     3915                        continue;
     3916
     3917                $checked[] = $term;
     3918        }
     3919
     3920        return $checked;
     3921}
     3922
     3923/**
    38903924 * Load the auth check for monitoring whether the user is still logged in.
    38913925 * Can be disabled with remove_action( 'admin_init', 'wp_auth_check_load' );
    38923926 *
  • wp-includes/query.php

     
    21862186                }
    21872187
    21882188                // If a search pattern is specified, load the posts that match
    2189                 if ( !empty($q['s']) ) {
     2189                // Sanity check: search string shouldn't be more than 1600 characters.
     2190                // See ticket #21688 for more info.
     2191                if ( ! empty( $q['s'] ) && strlen( $q['s'] ) < 1600 ) {
    21902192                        // added slashes screw with quote grouping when done early, so done later
    21912193                        $q['s'] = stripslashes($q['s']);
    21922194                        if ( empty( $_GET['s'] ) && $this->is_main_query() )
    21932195                                $q['s'] = urldecode($q['s']);
     2196                        // there are no line breaks in <input /> fields
     2197                        $q['s'] = str_replace( array("\r", "\n"), '', $q['s'] );
     2198                        $num_all_terms = 1;
    21942199                        if ( !empty($q['sentence']) ) {
    21952200                                $q['search_terms'] = array($q['s']);
    21962201                        } else {
    2197                                 preg_match_all('/".*?("|$)|((?<=[\r\n\t ",+])|^)[^\r\n\t ",+]+/', $q['s'], $matches);
    2198                                 $q['search_terms'] = array_map('_search_terms_tidy', $matches[0]);
     2202                                if ( preg_match_all( '/".*?("|$)|((?<=[\t ",+])|^)[^\t ",+]+/', $q['s'], $matches ) ) {
     2203                                        $num_all_terms = count( $matches[0] );
     2204                                        $q['search_terms'] = _check_search_terms( $matches[0] );
     2205                                        // if the search string has only short terms or stopwords, or is 10+ terms long, match it as sentence
     2206                                        if ( empty( $q['search_terms'] ) || count( $q['search_terms'] ) > 9 )
     2207                                                $q['search_terms'] = array( $q['s'] );
     2208                                } else {
     2209                                        $q['search_terms'] = array( $q['s'] );
     2210                                }
    21992211                        }
     2212
    22002213                        $n = !empty($q['exact']) ? '' : '%';
    22012214                        $searchand = '';
    2202                         foreach( (array) $q['search_terms'] as $term ) {
     2215                        $search_orderby_title = array();
     2216                        foreach ( $q['search_terms'] as $term ) {
    22032217                                $term = esc_sql( like_escape( $term ) );
     2218                                if ( $n )
     2219                                        $search_orderby_title[] = "$wpdb->posts.post_title LIKE '%$term%'";
     2220
    22042221                                $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
    22052222                                $searchand = ' AND ';
    22062223                        }
     
    35903607                else
    35913608                        $post_type = 'post';
    35923609
     3610                // Order search results by relevance when another "orderby" is not specified in the query
     3611                if ( ! empty( $search_orderby_title ) && empty( $q['orderby'] ) ) {
     3612                        $search_orderby = '';
     3613                        // Allow plugins to override sorting on 'post_title' and/or 'post_content'.
     3614                        // Passing an empty array would disable sorting.
     3615                        $search_orderby_on = array( 'post_title', 'post_content' );
     3616                        $search_orderby_on = apply_filters( 'posts_search_orderby_on', $search_orderby_on, $this );
     3617
     3618                        if ( $num_all_terms > 1 ) {
     3619                                $num_terms = count( $search_orderby_title );
     3620                                $search_orderby_s = esc_sql( like_escape( $q['s'] ) );
     3621
     3622                                if ( in_array( 'post_title', $search_orderby_on, true ) ) {
     3623                                        $search_orderby = '(CASE ';
     3624                                        // sentence match in 'post_title'
     3625                                        $search_orderby .= "WHEN $wpdb->posts.post_title LIKE '%{$search_orderby_s}%' THEN 1 ";
     3626
     3627                                        // sanity limit, sort as sentence when more than 6 terms
     3628                                        // (few searches are longer than 6 terms and most titles are not)
     3629                                        if ( $num_terms < 7 ) {
     3630                                                // all words in title
     3631                                                $search_orderby .= 'WHEN ' . implode( ' AND ', $search_orderby_title ) . ' THEN 2 ';
     3632                                                // any word in title, not needed when $num_terms == 1
     3633                                                if ( $num_terms > 1 )
     3634                                                        $search_orderby .= 'WHEN ' . implode( ' OR ', $search_orderby_title ) . ' THEN 3 ';
     3635                                        }
     3636
     3637                                        // sentence match in 'post_content'
     3638                                        if ( in_array( 'post_content', $search_orderby_on, true ) )
     3639                                                $search_orderby .= "WHEN $wpdb->posts.post_content LIKE '%{$search_orderby_s}%' THEN 4 ";
     3640
     3641                                        $search_orderby .= 'ELSE 5 END)';
     3642                                } elseif ( in_array( 'post_content', $search_orderby_on, true ) ) {
     3643                                        // Not sorting on 'post_title', order by sentence matches in 'post_content'
     3644                                        $search_orderby = "$wpdb->posts.post_content LIKE '%{$search_orderby_s}%' DESC";
     3645                                }
     3646                        } else {
     3647                                // single word or sentence search
     3648                                if ( in_array( 'post_title', $search_orderby_on, true ) )
     3649                                        $search_orderby = reset( $search_orderby_title ) . ' DESC';
     3650                        }
     3651                        // Allow plugins to add/remove/modify the 'order by' for the search section of the database query
     3652                        $search_orderby = apply_filters( 'posts_search_orderby', $search_orderby, $this );
     3653                        if ( $search_orderby )
     3654                                $orderby = $orderby ? $search_orderby . ', ' . $orderby : $search_orderby;
     3655                }
     3656
    35933657                if ( is_array( $post_type ) ) {
    35943658                        if ( count( $post_type ) > 1 )
    35953659                                return;