Making WordPress.org

Changeset 9363


Ignore:
Timestamp:
12/19/2019 08:03:44 AM (5 years ago)
Author:
dd32
Message:

Theme Directory: Prioritise exact-match theme searches in search results, and allow an exact-match slug/title to bypass the last two years restriction.

Fixes #2939, #4920.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory
Files:
2 edited

Legend:

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

    r8999 r9363  
    450450        $cache_key = sanitize_key( __METHOD__ . ':' . get_locale() . ':' . md5( serialize( $this->request ) . serialize( $this->fields ) ) );
    451451        if ( false !== ( $this->response = wp_cache_get( $cache_key, $this->cache_group ) ) && empty( $this->request->cache_buster ) ) {
    452             return;
     452        //  return;
    453453        }
    454454
     
    487487        }
    488488
    489         wp_cache_set( $cache_key, $this->response, $this->cache_group, $this->cache_life );
     489        //wp_cache_set( $cache_key, $this->response, $this->cache_group, $this->cache_life );
    490490    }
    491491
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/query-modifications.php

    r9188 r9363  
    136136        ! in_array( $query->query_vars['browse'], array( 'favorites', 'new', 'updated' ) )
    137137    ) {
    138         add_filter( 'posts_clauses', 'wporg_themes_prioritize_translated_posts_clauses' );
    139     }
    140 
     138        add_filter( 'posts_clauses', 'wporg_themes_prioritize_translated_posts_clauses', 11 );
     139    }
     140
     141    if ( $query->is_search() ) {
     142        add_filter( 'posts_clauses', 'wporg_themes_prioritize_exact_matches_clauses', 10, 2 );
     143    }
    141144}
    142145add_action( 'pre_get_posts', 'wporg_themes_pre_get_posts' );
     
    160163
    161164/**
     165 * Filters SQL clauses, to prioritise exact theme slug matches.
     166 */
     167function wporg_themes_prioritize_exact_matches_clauses( $clauses, $query ) {
     168    global $wpdb;
     169
     170    // Override the post_modified check to allow matching when an exact title/slug match is found.
     171    $clauses['where'] = preg_replace_callback(
     172        "!{$wpdb->posts}.post_modified > '(.+?)'!is",
     173        function( $m ) use ( $query ) {
     174            global $wpdb;
     175
     176            return $wpdb->prepare(
     177                "( {$wpdb->posts}.post_modified > %s OR {$wpdb->posts}.post_name = %s OR {$wpdb->posts}.post_title = %s )",
     178                $m[1],
     179                $query->get('s'),
     180                $query->get('s')
     181            );
     182        },
     183        $clauses['where']
     184    );
     185
     186    // Prioritize exact-match slug/titles in search results
     187    $clauses['orderby'] = $wpdb->prepare(
     188        "( {$wpdb->posts}.post_name = %s OR {$wpdb->posts}.post_title = %s ) DESC, ",
     189        $query->get( 's' ),
     190        $query->get( 's' )
     191    ) . $clauses['orderby'];
     192
     193    return $clauses;
     194}
     195
     196/**
    162197 * Handle proper 404 errors for requests.
    163198 */
Note: See TracChangeset for help on using the changeset viewer.