Making WordPress.org


Ignore:
Timestamp:
10/29/2014 06:02:03 PM (10 years ago)
Author:
coffee2code
Message:

Code Reference: presume an exact match search request for a function or method if search term ends with '()'. Fixes #686

Falls back to general search if no exact match found.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/functions.php

    r948 r951  
    7474    add_filter( 'post_type_link', __NAMESPACE__ . '\\method_permalink', 10, 2 );
    7575    add_filter( 'term_link', __NAMESPACE__ . '\\taxonomy_permalink', 10, 3 );
     76    add_filter( 'the_posts', __NAMESPACE__ . '\\rerun_empty_exact_search', 10, 2 );
    7677    add_theme_support( 'automatic-feed-links' );
    7778    add_theme_support( 'post-thumbnails' );
     
    237238        $query->set( 'wp-parser-source-file', str_replace( array( '.php', '/' ), array( '-php', '_' ), $query->query['wp-parser-source-file'] ) );
    238239    }
     240
     241    // If user has '()' at end of a search string, assume they want a specific function/method.
     242    if ( $query->is_search() ) {
     243        $s = htmlentities( $query->get( 's' ) );
     244        if ( '()' === substr( $s, -2 ) ) {
     245            // Enable exact search
     246            $query->set( 'exact',     true );
     247            // Modify the search query to omit the parentheses
     248            $query->set( 's',         substr( $s, 0, -2 ) ); // remove '()'
     249            // Restrict search to function-like content
     250            $query->set( 'post_type', array( 'wp-parser-function', 'wp-parser-method' ) );
     251        }
     252    }
     253}
     254
     255/**
     256 * Rerun an exact search with the same criteria except exactness if no posts
     257 * were found.
     258 *
     259 * @access public
     260 *
     261 * @param  array    $posts Array of posts after the main query
     262 * @param  WP_Query $query WP_Query object
     263 * @return array
     264 */
     265function rerun_empty_exact_search( $posts, $query ) {
     266    if ( is_search() && true === $query->get( 'exact' ) && ! $query->found_posts ) {
     267        $query->set( 'exact', false );
     268        $posts = $query->get_posts();
     269    }
     270    return $posts;
    239271}
    240272
Note: See TracChangeset for help on using the changeset viewer.