Skip to content

Commit

Permalink
Make the max posts to query filterable
Browse files Browse the repository at this point in the history
  • Loading branch information
rbcorrales committed Oct 26, 2023
1 parent 8310345 commit e81da46
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions performance/class-media-library-caching.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Media_Library_Caching {
public const CACHE_GROUP = 'mime_types';
public const AVAILABLE_MIME_TYPES_CACHE_KEY = 'vip_available_mime_types_';
public const USING_DEFAULT_MIME_TYPES_CACHE_KEY = 'vip_using_default_mime_types_';
public const MAX_POSTS_TO_QUERY = 100000;
public const MAX_POSTS_TO_QUERY_DEFAULT = 100000; // TODO: benchmark this value.

/**
* Class initialization.
Expand Down Expand Up @@ -63,8 +63,16 @@ public static function get_cached_post_mime_types( $filtered_mime_types, $type )
$mime_types = wp_cache_get( $cache_key, self::CACHE_GROUP );

if ( false === $mime_types ) {
$attachment_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(1) FROM {$wpdb->posts} WHERE post_type = %s", $type ) );
$use_defaults = $attachment_count > self::MAX_POSTS_TO_QUERY;

/**
* Filters the max number of posts to query for dynamic MIME type caching.
*
* @param bool $max_posts_to_query Max number of posts to query.
*/
$max_posts_to_query = apply_filters( 'vip_max_posts_to_query_for_mime_type_caching', self::MAX_POSTS_TO_QUERY_DEFAULT );

$attachment_count = $max_posts_to_query > 0 ? $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(1) FROM {$wpdb->posts} WHERE post_type = %s", $type ) ) : 1;
$use_defaults = $attachment_count > $max_posts_to_query;

if ( $use_defaults ) {
// If there are too many posts to query, use the default mime types.
Expand Down

0 comments on commit e81da46

Please sign in to comment.