Making WordPress.org

Changeset 12942


Ignore:
Timestamp:
10/11/2023 09:49:06 PM (8 months ago)
Author:
iandunn
Message:

Plugin Directory: Limit pagination to 50 links

Fixes #7300

File:
1 edited

Legend:

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

    r12362 r12942  
    6767
    6868            // add_filter( 'jetpack_search_abort', array( $this, 'log_jetpack_search_abort' ) );
    69            
     69
    7070            // $es_query_args = apply_filters( 'jetpack_search_es_query_args', $es_query_args, $query );
    7171
    7272            add_filter( 'jetpack_search_es_wp_query_args', array( $this, 'jetpack_search_es_wp_query_args' ), 10, 2 );
    73             add_filter( 'jetpack_search_es_query_args', array( $this, 'jetpack_search_es_query_args' ), 10, 2 );           
     73            add_filter( 'jetpack_search_es_query_args', array( $this, 'jetpack_search_es_query_args' ), 10, 2 );
     74            add_filter( 'posts_pre_query', array( $this, 'set_max_num_pages' ), 15, 2 ); // After `Classic_Search::filter__posts_pre_query()`
    7475
    7576            // Load Jetpack Search.
     
    8283            } else {
    8384                // Old(er) Jetpack, load the classic search module, Temporarily.
    84                
     85
    8586                include_once WP_PLUGIN_DIR . '/jetpack/modules/search/class.jetpack-search.php';
    8687                include_once WP_PLUGIN_DIR . '/jetpack/modules/search/class.jetpack-search-helpers.php';
     
    396397    }
    397398
     399    /**
     400     * Limit the number of pagination links to 50.
     401     *
     402     * Jetpack ignores the `max_num_pages` that's set in `WP_Query` args and overrides it in
     403     * `Classic_Search::filter__posts_pre_query()`. When there are more than 1,000 matches, their value causes
     404     * Core's `paginate_links()` to generated 51 links, even though we redirect the user to the homepage when
     405     * page 51+ is requested.
     406     */
     407    function set_max_num_pages( $posts, $query ) {
     408        $post_type = (array) $query->query_vars['post_type'] ?? '';
     409
     410        if ( is_admin() || ! is_search() || ! in_array( 'plugin', $post_type ) ) {
     411            return $posts;
     412        }
     413
     414        if ( $query->max_num_pages > 50 ) {
     415            $query->max_num_pages = 50;
     416        }
     417
     418        return $posts;
     419    }
     420
    398421    public function log_search_es_wp_query_args( $es_wp_query_args, $query ) {
    399422        error_log( '--- ' . __FUNCTION__ . ' ---' );
Note: See TracChangeset for help on using the changeset viewer.