Making WordPress.org


Ignore:
Timestamp:
05/31/2019 07:51:49 AM (5 years ago)
Author:
dd32
Message:

Plugin Directory: Sanitize some search terms to allow better matching and more cache hits for searches.

See #4469.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/class-plugin-directory.php

    r8894 r8902  
    868868            $wp_query->query_vars['post_status'][] = 'disabled';
    869869            $wp_query->query_vars['post_status']   = array_unique( $wp_query->query_vars['post_status'] );
     870        }
     871
     872        // Sanitize / cleanup the search query a little bit.
     873        if ( $wp_query->is_search() ) {
     874            $s = $wp_query->get( 's' );
     875            $s = urldecode( $s );
     876
     877            // If a URL-like request comes in, reduce to a slug
     878            if ( preg_match( '!^http.+/plugins/([^/]+)(/|$)!i', $s, $m ) ) {
     879                $s = $m[1];
     880            }
     881
     882            // Jetpack Search has a limit, limit to 200char. This is intentionally using ASCII length + Multibyte substr.
     883            if ( strlen( $s ) > 200 ) {
     884                $s = mb_substr( $s, 0, 200 );
     885            }
     886
     887            // Trim off special characters, only allowing wordy characters at the end of searches.s
     888            $s = preg_replace( '!(\W+)$!i', '', $s );
     889            // ..and whitespace
     890            $s = trim( $s );
     891
     892            $wp_query->set( 's', $s );
    870893        }
    871894
Note: See TracChangeset for help on using the changeset viewer.