Skip to content

Commit

Permalink
Merge pull request #18 from Automattic/add/filtering-for-post-types
Browse files Browse the repository at this point in the history
Allow plugins to filter post_types from being cached by advanced_post_cache
  • Loading branch information
westi committed Nov 14, 2019
2 parents 8b72249 + e5d1247 commit c46b2d9
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions advanced-post-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ function __construct() {

add_action( 'switch_blog', array( $this, 'setup_for_blog' ), 10, 2 );

add_filter( 'posts_request', array( &$this, 'posts_request' ) ); // Short circuits if cached
add_filter( 'posts_results', array( &$this, 'posts_results' ) ); // Collates if cached, primes cache if not
add_filter( 'posts_request', array( &$this, 'posts_request' ), 10, 2 ); // Short circuits if cached
add_filter( 'posts_results', array( &$this, 'posts_results' ), 10, 2 ); // Collates if cached, primes cache if not

add_filter( 'post_limits_request', array( &$this, 'post_limits_request' ), 999, 2 ); // Checks to see if we need to worry about found_posts

add_filter( 'found_posts_query', array( &$this, 'found_posts_query' ) ); // Short circuits if cached
add_filter( 'found_posts', array( &$this, 'found_posts' ) ); // Reads from cache if cached, primes cache if not
add_filter( 'found_posts_query', array( &$this, 'found_posts_query' ), 10, 2 ); // Short circuits if cached
add_filter( 'found_posts', array( &$this, 'found_posts' ), 10, 2 ); // Reads from cache if cached, primes cache if not
}

function setup_for_blog( $new_blog_id = false, $previous_blog_id = false ) {
Expand Down Expand Up @@ -106,9 +106,13 @@ function flush_cache() {
* If cached: Return query of needed post IDs.
* Otherwise: Returns query unchanged.
*/
function posts_request( $sql ) {
function posts_request( $sql, $query ) {
global $wpdb;

if ( apply_filters( 'advanced_post_cache_skip_for_post_type', false, $query->get( 'post_type' ) ) ) {
return $sql;
}

$this->cache_key = md5( $sql ); // init
$this->all_post_ids = wp_cache_get( $this->cache_key, $this->cache_group );
if ( 'NA' !== $this->found_posts )
Expand Down Expand Up @@ -153,7 +157,11 @@ function posts_request( $sql ) {
* If cached: Collates posts returned by SQL query with posts that are already cached. Orders correctly.
* Otherwise: Primes cache with data for current posts WP_Query.
*/
function posts_results( $posts ) {
function posts_results( $posts, $query ) {
if ( apply_filters( 'advanced_post_cache_skip_for_post_type', false, $query->get( 'post_type' ) ) ) {
return $posts;
}

if ( $this->found_posts && is_array( $this->all_post_ids ) ) { // is cached
$collated_posts = array();
foreach ( $this->cached_posts as $post )
Expand Down Expand Up @@ -185,6 +193,10 @@ function posts_results( $posts ) {
* If $limits is empty, WP_Query never calls the found_rows stuff, so we set $this->found_rows to 'NA'
*/
function post_limits_request( $limits, $query ) {
if ( apply_filters( 'advanced_post_cache_skip_for_post_type', false, $query->get( 'post_type' ) ) ) {
return $limits;
}

if ( empty( $limits ) || ( isset( $query->query_vars['no_found_rows'] ) && $query->query_vars['no_found_rows'] ) )
$this->found_posts = 'NA';
else
Expand All @@ -196,7 +208,11 @@ function post_limits_request( $limits, $query ) {
* If cached: Blanks SELECT FOUND_ROWS() query. This data is already stored in cache.
* Otherwise: Returns query unchanged.
*/
function found_posts_query( $sql ) {
function found_posts_query( $sql, $query ) {
if ( apply_filters( 'advanced_post_cache_skip_for_post_type', false, $query->get( 'post_type' ) ) ) {
return $sql;
}

if ( $this->found_posts && is_array( $this->all_post_ids ) ) // is cached
return '';
return $sql;
Expand All @@ -206,7 +222,11 @@ function found_posts_query( $sql ) {
* If cached: Returns cached result of FOUND_ROWS() query.
* Otherwise: Returs result unchanged
*/
function found_posts( $found_posts ) {
function found_posts( $found_posts, $query ) {
if ( apply_filters( 'advanced_post_cache_skip_for_post_type', false, $query->get( 'post_type' ) ) ) {
return $found_posts;
}

if ( $this->found_posts && is_array( $this->all_post_ids ) ) // is cached
return (int) $this->found_posts;

Expand Down

0 comments on commit c46b2d9

Please sign in to comment.