Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better match the behaviour of Advanced Post Cache to WP_Query when setting found_posts #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions advanced-post-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function __construct() {
add_action( 'switch_blog', array( $this, 'setup_for_blog' ), 10, 2 );

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( '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

Expand Down Expand Up @@ -115,8 +115,9 @@ function posts_request( $sql, $query ) {

$this->cache_key = md5( $sql ); // init
$this->all_post_ids = wp_cache_get( $this->cache_key, $this->cache_group );
if ( 'NA' !== $this->found_posts )
if ( 'NO_FOUND_ROWS' !== $this->found_posts ) {
$this->found_posts = wp_cache_get( "{$this->cache_key}_found", $this->cache_group );
}

if ( $this->all_post_ids xor $this->found_posts )
$this->cache_func = 'wp_cache_set';
Expand All @@ -137,11 +138,14 @@ function posts_request( $sql, $query ) {
}
}

$this->cached_posts = array_filter( $this->cached_posts );
// wp_cache_get_multi returns `false` if either the group or the key wasn't found
if ( !empty( $this->cached_posts ) ) {
$this->cached_posts = array_filter( $this->cached_posts );

foreach ( $this->cached_posts as $post ) {
if ( !empty( $post ) )
$this->cached_post_ids[] = $post->ID;
foreach ( $this->cached_posts as $post ) {
if ( !empty( $post ) )
$this->cached_post_ids[] = $post->ID;
}
}
$uncached_post_ids = array_diff( $this->all_post_ids, $this->cached_post_ids );

Expand All @@ -164,8 +168,11 @@ function posts_results( $posts, $query ) {

if ( $this->found_posts && is_array( $this->all_post_ids ) ) { // is cached
$collated_posts = array();
foreach ( $this->cached_posts as $post )
$posts[] = $post;
if( !empty( $this->cached_posts ) ) {
foreach ( $this->cached_posts as $post ){
$posts[] = $post;
}
}

foreach ( $posts as $post ) {
$loc = array_search( $post->ID, $this->all_post_ids );
Expand All @@ -190,17 +197,21 @@ function posts_results( $posts, $query ) {
}

/**
* If $limits is empty, WP_Query never calls the found_rows stuff, so we set $this->found_rows to 'NA'
* If $limits is empty, WP_Query never calls the found_rows stuff, so we set $this->found_posts to 'NO_LIMITS'
*/
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
if ( isset( $query->query_vars['no_found_rows'] ) && $query->query_vars['no_found_rows'] ) {
$this->found_posts = 'NO_FOUND_ROWS';
} elseif ( empty( $limits ) ) {
$this->found_posts = 'NO_LIMITS';
} else {
$this->found_posts = false; // re-init
}

return $limits;
}

Expand Down