Making WordPress.org

Changeset 13744


Ignore:
Timestamp:
05/22/2024 06:20:58 PM ( 4 months ago)
Author:
ryelle
Message:

Page Limiter: Update hook for setting max page & found posts so it applies to cached queries.

See https://github.com/WordPress/wordpress.org/pull/322

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/mu-plugins/pub/wporg-page-limiter.php

    r8224 r13744  
    thirty-one thirty-one         add_action( 'parse_query', [ $this, 'parse_query' ], 100 );
    thirty-two thirty-two
    thirty-three           // Limit WP_Query::max_num_pages to self::MAX_PAGES
    thirty-four           add_filter( 'found_posts', [ $this, 'found_posts' ], 100, 2 );
      thirty-three         add_filter( 'posts_results', [ $this, 'set_found_posts_max_pages' ], 100, 2 );
    thirty-five thirty-four
    thirty-six thirty-five         // BbPress Forum Topic pagination
     
    sixty-three sixty-two
    sixty-four sixty-three     // Make WordPress think there's only 50 pages
    sixty-five       public function found_posts( $found_ posts, $query ) {
      sixty-four     public function set_found_posts_max_pages( $ posts, $query ) {
    sixty-six sixty-five         if ( ! $query->is_main_query() ) {
    sixty-seven               return $ found_ posts;
      sixty-six             return $ posts;
    sixty-eight sixty-seven         }
    sixty-nine sixty-eight
     
    seventy-two seventy-one         $max_posts      = self::MAX_PAGES * $posts_per_page;
    seventy-three seventy-two
    seventy-four           return min(
    seventy-five               $found_posts,
    seventy-six               $max_posts
    seventy-seven           );
      seventy-three         $query->original_found_posts = $query->found_posts;
      seventy-four         $query->found_posts = min( $query->found_posts, $max_posts );
      seventy-five
      seventy-six         // Update the max-pages value too.
      seventy-seven         $query->max_num_pages = (int) ceil( $query->found_posts / $posts_per_page );
      seventy-eight
      seventy-nine         return $posts;
    seventy-eight eighty     }
    seventy-nine eighty-one
Note: See TracChangeset for help on using the changeset viewer.