Making WordPress.org

Ticket #496: 496-limit-found-posts.diff

File 496-limit-found-posts.diff, 1.5 KB (added by tellyworth, 7 years ago)

Limit browse results to 200 pages.

  • class-plugin-directory.php

     
    3333                add_filter( 'post_type_link', array( $this, 'filter_post_type_link' ), 10, 2 );
    3434                add_filter( 'term_link', array( $this, 'filter_term_link' ), 10, 2 );
    3535                add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
     36                add_filter( 'found_posts', array( $this, 'filter_found_posts' ), 10, 2 );
    3637                add_filter( 'rest_api_allowed_post_types', array( $this, 'filter_allowed_post_types' ) );
    3738                add_filter( 'pre_update_option_jetpack_options', array( $this, 'filter_jetpack_options' ) );
    3839                add_action( 'template_redirect', array( $this, 'prevent_canonical_for_plugins' ), 9 );
     
    803804        }
    804805
    805806        /**
     807         * Filter to limit the total number of found posts in browse queries.
     808         * Stops search crawlers from paginating through the entire DB.
     809         */
     810        public function filter_found_posts( $found_posts, $wp_query ) {
     811                if ( isset( $wp_query->query['browse'] ) && in_array( 'plugin', $wp_query->query_vars['post_type'] ) )
     812                        return min( $found_posts, 200 * $wp_query->query_vars['posts_per_page'] ); // 200 pages
     813                return $found_posts;
     814        }
     815
     816        /**
    806817         * Filter to bypass caching for options critical to Jetpack sync to work around race conditions and other unidentified bugs.
    807818         * If this works and becomes a permanent solution, it probably belongs elsewhere.
    808819         */