Skip to content

Commit

Permalink
Move forward with Tiny cache + clean post
Browse files Browse the repository at this point in the history
  • Loading branch information
szepeviktor committed Jun 1, 2017
1 parent 46f6385 commit 7a37904
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 3 deletions.
2 changes: 1 addition & 1 deletion mu-cache-flush-button/flush-cache-button.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/*
Plugin Name: Fluch cache button (MU)
Plugin Name: Flush cache button (MU)
Version: 0.1.1
Description: Add an admin bar button to flush the object cache.
Author: Viktor Szépe
Expand Down
41 changes: 41 additions & 0 deletions mu-cache-flush-post-button/clean-post-cache-button.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/*
Plugin Name: Clean post cache button (MU)
Version: 0.1.0
Description: Add a post row action to clean the post from the object cache.
Author: Viktor Szépe
Plugin URI: https://github.com/szepeviktor/wordpress-plugin-construction
GitHub Plugin URI: https://github.com/szepeviktor/wordpress-plugin-construction
*/

add_filter( 'post_row_actions', function ( $actions, $post ) {
$post_list_url = admin_url( add_query_arg( array(
'post_type' => $post->post_type,
'clean_post' => $post->ID,
), 'edit.php' ) );

$actions['clean_post'] = sprintf( '<a class="clean" href="%s">%s</a>',
wp_nonce_url( $post_list_url, 'clean_post' ),
__( 'Clean', 'clean-post' )
);

return $actions;
}, 11, 2 );

add_action( 'admin_init', function () {
if ( ! isset( $_GET['clean_post'] ) ) {
return;
}

check_admin_referer( 'clean_post' );

// Clean
$post_id = (int) $_GET['clean_post'];
if ( false !== get_post_status( $post_id ) ) {
clean_post_cache( $post_id );
}

add_action( 'admin_notices', function () {
echo '<div class="notice notice-success is-dismissible"><p>Post cleaned from Object Cache.</p></div>';
} );
} );
58 changes: 56 additions & 2 deletions tiny-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
Plugin name: Tiny cache (MU)
Description: Cache HTML in persistent object cache during the_content() calls.
Version: 0.3.1
Version: 0.4.0
Plugin URI: https://developer.wordpress.org/reference/functions/the_content/
*/

Expand All @@ -16,14 +16,20 @@
the_content();
endif;
if ( function_exists( 'get_the_content_cached' ) ) :
get_the_content_cached();
else :
get_the_content();
endif;
Replace the_content(); instances
--------------------------------
$ find -type f -name "*.php" | xargs -r -L 1 sed -i -e 's|\bthe_content();|the_content_cached();|g'
*/

/**
* Serve cached content or generate and save the content to the object cache.
* Display content from the object cache.
*/
function the_content_cached( $more_link_text = null, $strip_teaser = false ) {

Expand Down Expand Up @@ -72,6 +78,54 @@ function the_content_cached( $more_link_text = null, $strip_teaser = false ) {
}
}

/**
* Retrieve content from the object cache.
*/
function get_the_content_cached( $more_link_text = null, $strip_teaser = false ) {

$post_id = get_the_ID();
// Learned from W3TC Page Cache rules and WP Super Cache rules
if ( ! wp_using_ext_object_cache() // Object cache is unavailable
|| is_user_logged_in() // User is logged in
|| ! ( isset( $_SERVER['REQUEST_METHOD'] ) && 'GET' === $_SERVER['REQUEST_METHOD'] ) // Not a GET request
|| ! $post_id // Not possible to tie content to post ID
|| ! ( defined( 'DONOTCACHEPAGE' ) && DONOTCACHEPAGE ) // DO-NOT-CACHE tag present
) {
return get_the_content( $more_link_text, $strip_teaser );
}

$found = null;
// @TODO Support groups: wp_cache_add_global_groups( 'get_the_content' ) and WP_REDIS_USE_CACHE_GROUPS
$cached = wp_cache_get( $post_id, 'get_the_content', false, $found );

// Cache hit
if ( $found ) {
return $cached;
}

// Cache miss
$save_to_cache = false;
$post = get_post( $post_id );
// Public post
if ( true === is_object( $post ) ) {
if ( 'publish' === $post->post_status && empty( $post->post_password ) ) {
$save_to_cache = true;
}
}

// @TODO Add $more_link_text and $strip_teaser hash to cache key.
$content = get_the_content( $more_link_text, $strip_teaser );
if ( true === $save_to_cache ) {
$message_tpl = '<!-- Cached content generated by Tiny cache on %s -->';
$timestamp = gmdate( 'c' );
$message = sprintf( $message_tpl, esc_html( $timestamp ) );
wp_cache_set( $post_id, $content . $message, 'get_the_content' );
}

return $content;
}


/**
* Save the content to the object cache.
*/
Expand Down

0 comments on commit 7a37904

Please sign in to comment.