WordPress.org

Making WordPress.org

Changeset 3499


Ignore:
Timestamp:
06/20/2016 01:04:32 PM (4 years ago)
Author:
dd32
Message:

Plugin Directory: Allow for the custom query logic to be applied to all WP_Query instances, not just the main query.
Also removes a weird SQL hack to remove the browse query var from queries, and replaces it with an equally weird closure hack.

See #1584

File:
1 edited

Legend:

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

    r3498 r3499  
    416416     */
    417417    public function use_plugins_in_query( $wp_query ) {
    418         if ( is_admin() || ! $wp_query->is_main_query() ) {
     418        if ( is_admin() ) {
    419419            return;
    420420        }
     
    429429        }
    430430
    431         switch ( get_query_var( 'browse' ) ) {
     431        switch ( $wp_query->query_vars['browse'] ) {
    432432            case 'favorites':
    433433                $favorites_user = wp_get_current_user();
     
    451451                }
    452452
    453                 add_filter( 'posts_where', array( $this, 'pre_get_posts_sql_browse' ) );
    454453                break;
    455454
    456455            case 'new':
    457             case 'popular':
    458                 add_filter( 'posts_where', array( $this, 'pre_get_posts_sql_browse' ) );
     456                $wp_query->query_vars['orderby']  = 'post_modified';
    459457                break;
    460458        }
    461459
    462         if ( $wp_query->is_archive() && ! $wp_query->is_tax( 'plugin_section', 'new' ) && empty( $wp_query->query_vars['orderby'] ) ) {
     460        if ( isset( $wp_query->query['browse'] ) ) {
     461            if ( 'beta' != $wp_query->query['browse'] && 'featured' != $wp_query->query['browse'] ) {
     462                unset( $wp_query->query_vars['browse'] );
     463
     464                add_filter( 'the_posts', function( $posts, $wp_query ) {
     465                    // Fix the queried object for the archive view
     466                    if ( ! $wp_query->queried_object && isset( $wp_query->query['browse'] ) ) {
     467                        $wp_query->query_vars['browse'] = $wp_query->query['browse'];
     468                        $wp_query->queried_object = get_term_by( 'slug', $wp_query->query['browse'], 'plugin_section' );
     469                    }
     470                    return $posts;
     471                }, 10, 2 );
     472
     473            }
     474        }
     475
     476        if ( empty( $wp_query->query_vars['orderby'] ) ) {
    463477            $wp_query->query_vars['orderby']  = 'meta_value_num';
    464478            $wp_query->query_vars['meta_key'] = '_active_installs';
    465479        }
    466     }
    467 
    468     /**
    469      * Callback to remove the requirement for plugins to be tagged with the requested
    470      * plugin_section term.
    471      *
    472      * Used for archives like `popular` or `favorites`, that all active plugins are a part of.
    473      *
    474      * @ignore
    475      *
    476      * @param string $where WHERE clause.
    477      * @return string
    478      */
    479     public function pre_get_posts_sql_browse( $where ) {
    480         global $wpdb;
    481 
    482         remove_filter( 'posts_where', array( $this, 'pre_get_posts_sql_browse' ) );
    483 
    484         $term = get_term_by( 'slug', get_query_var( 'browse' ), 'plugin_section' );
    485 
    486         if ( $term instanceof \WP_Term ) {
    487             $where = str_replace( " AND ( \n  {$wpdb->term_relationships}.term_taxonomy_id IN ({$term->term_id})\n)", '', $where );
    488         }
    489 
    490         return $where;
    491480    }
    492481
Note: See TracChangeset for help on using the changeset viewer.