Make WordPress Core

Ticket #21688: 21688-5.patch

File 21688-5.patch, 4.6 KB (added by azaozz, 11 years ago)
  • wp-includes/default-filters.php

     
    192192add_filter( 'editable_slug',            'urldecode'                           );
    193193add_filter( 'editable_slug',            'esc_textarea'                        );
    194194add_filter( 'nav_menu_meta_box_object', '_wp_nav_menu_meta_box_object'        );
     195add_filter( 'wp_search_stopwords',      'wp_search_stopwords'                 );
    195196
    196197// Actions
    197198add_action( 'wp_head',             'wp_enqueue_scripts',              1     );
  • wp-includes/functions.php

     
    36373637        return true;
    36383638}
    36393639
     3640/**
     3641 * Common stopwords (terms) that are excluded from the separate term matching when searching for posts.
     3642 * The list of English stopwords is the approximate search engines list. MySQL has a much longer default list of full-text stopwords.
     3643 *
     3644 * @since 3.5.0
     3645 *
     3646 * @param array optional Terms to exclude when searching
     3647 * @return array
     3648 */
     3649function wp_search_stopwords( $in = array() ) {
     3650        $_words = explode( ',', _x('about,are,com,for,from,how,that,the,this,was,what,when,where,who,will,with,www', 'Comma separated list of common words to exclude when searching.') );
     3651        $words = array();
     3652
     3653        foreach( $_words as $word ) {
     3654                $word = trim($word, "\r\n\t ");
     3655                if ( !$word )
     3656                        continue;
     3657                $words[] = $word;
     3658        }
     3659
     3660        return array_merge( $in, $words );
     3661}
     3662
     3663/**
     3664 * Check if the term is suitable for searching.
     3665 *
     3666 * @access private
     3667 * @since 3.5.0
     3668 *
     3669 * @param string Term to check
     3670 * @return string
     3671 */
     3672function _check_search_term($term) {
     3673        static $strtolower_func, $stopwords;
     3674
     3675        if ( empty( $term ) )
     3676                return '';
     3677
     3678        // keep spaces when term is for exact match
     3679        if ( preg_match('/^".+"$/', $term) )
     3680                $term = trim($term, "\"'");
     3681        else
     3682                $term = trim($term, "\"' ");
     3683
     3684        if ( !$strtolower_func ) {
     3685                $strtolower_func = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
     3686                $stopwords = apply_filters( 'wp_search_stopwords', array() );
     3687        }
     3688
     3689        if ( empty( $term{2} ) ) {
     3690                // $term is too short, remove it if it doesn't contain numbers and is lower case
     3691                if ( !preg_match( '/[0-9]/', $term ) && $strtolower_func($term) == $term  )
     3692                        $term = '';
     3693        }
     3694
     3695        if ( $term && in_array( $strtolower_func($term), $stopwords, true ) )
     3696                $term = '';
     3697
     3698        return $term;
     3699}
     3700
  • wp-includes/query.php

     
    21732173                        }
    21742174                }
    21752175
    2176                 // If a search pattern is specified, load the posts that match
    2177                 if ( !empty($q['s']) ) {
     2176                // If a search pattern is specified, load the posts that match.
     2177                // Sanity check: search string shouldn't be more than 1600 characters.
     2178                // See ticket #21688 for more info.
     2179                if ( !empty($q['s']) && strlen($q['s']) < 1600 ) {
    21782180                        // added slashes screw with quote grouping when done early, so done later
    21792181                        $q['s'] = stripslashes($q['s']);
    21802182                        if ( empty( $_GET['s'] ) && $this->is_main_query() )
    21812183                                $q['s'] = urldecode($q['s']);
     2184                        // there are no line breaks in <input /> fields
     2185                        $q['s'] = str_replace( array("\r", "\n"), '', $q['s'] );
    21822186                        if ( !empty($q['sentence']) ) {
    21832187                                $q['search_terms'] = array($q['s']);
    21842188                        } else {
    2185                                 preg_match_all('/".*?("|$)|((?<=[\r\n\t ",+])|^)[^\r\n\t ",+]+/', $q['s'], $matches);
    2186                                 $q['search_terms'] = array_map('_search_terms_tidy', $matches[0]);
     2189                                if ( preg_match_all('/".*?("|$)|((?<=[\t ",+])|^)[^\t ",+]+/', $q['s'], $matches) ) {
     2190                                        $q['search_terms'] = array();
     2191
     2192                                        if ( !empty($matches[0]) ) {
     2193                                                foreach ( $matches[0] as $term ) {
     2194                                                        $term = _check_search_term($term);
     2195                                                        if ( $term )
     2196                                                                $q['search_terms'][] = $term;
     2197                                                }
     2198                                        }
     2199
     2200                                        // if the search string has only short terms or stopwords, or is more than 10 terms long, match it as sentence
     2201                                        if ( empty($q['search_terms']) || count($q['search_terms']) > 10 )
     2202                                                $q['search_terms'] = array($q['s']);
     2203                                } else {
     2204                                        $q['search_terms'] = array($q['s']);
     2205                                }
    21872206                        }
     2207
    21882208                        $n = !empty($q['exact']) ? '' : '%';
    21892209                        $searchand = '';
    2190                         foreach( (array) $q['search_terms'] as $term ) {
     2210                        foreach( $q['search_terms'] as $term ) {
    21912211                                $term = esc_sql( like_escape( $term ) );
    21922212                                $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
    21932213                                $searchand = ' AND ';