Making WordPress.org


Ignore:
Timestamp:
06/06/2024 04:13:41 AM (20 months ago)
Author:
dd32
Message:

Plugin Directory: Search: Improve the search code for phrase matching.

This does not add support for proper phrase matching in the search, but rather corrects the code to properly handle Jetpack Phrase search mode.

Previously most of the customizations in our search code was being skipped, as the structure of the Jetpack ES query was in an unexpected form.

See #2642.

File:
1 edited

Legend:

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

    r13721 r13778  
    975975        // Sanitize / cleanup the search query a little bit.
    976976        if ( $wp_query->is_search() ) {
    977             $s = $wp_query->get( 's' );
     977            $s = wp_unslash( $wp_query->get( 's' ) );
    978978            $s = urldecode( $s );
    979979
     
    988988            }
    989989
    990             // Trim off special characters, only allowing wordy characters at the end of searches.
    991             $s = preg_replace( '!(\W+)$!iu', '', $s );
    992             // ..and whitespace
     990            // Trim whitespace
    993991            $s = trim( $s );
    994992
    995             $wp_query->set( 's', $s );
     993            // If we're searching for a phrase, only trim non-quotey+wordy characters.
     994            if ( str_starts_with( $s, '"' ) || str_starts_with( $s, "'" ) ) {
     995                $s = preg_replace( '!(\s*[^\'"\w]+)$!iu', '', $s );
     996            } else {
     997                // If we're searching for a word, trim all non-wordy characters.
     998                $s = preg_replace( '!(\s*\W+)$!iu', '', $s );
     999            }
     1000
     1001            $wp_query->set( 's', wp_slash( $s ) );
    9961002
    9971003            // If the search is in the block directory, require that.
Note: See TracChangeset for help on using the changeset viewer.