Created
October 11, 2019 14:07
-
-
Save petenelson/f314edc59a422fdf8e940fdd45c03050 to your computer and use it in GitHub Desktop.
WordPress Cache Incrementor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Gets or updates the cache incrementor for a post type. | |
* | |
* @param string $post_type The post type. | |
* @param bool $update Whether to force an incrementor update. | |
* @return string | |
*/ | |
function post_type_cache_incrementor( $post_type, $update = false ) { | |
$group = 'post_type_cache_incrementor'; | |
if ( $update ) { | |
wp_cache_delete( $post_type, $group ); | |
} | |
$incrementor = wp_cache_get( $post_type, $group ); | |
if ( false === $incrementor ) { | |
$incrementor = current_time( 'timestamp' ); | |
wp_cache_set( $post_type, $incrementor, $group, HOUR_IN_SECONDS ); | |
} | |
return $incrementor; | |
} | |
/** | |
* Updates the cache incrementor for a post type. | |
* | |
* @param int|WP_Post $post The post ID or object. | |
* @return void | |
*/ | |
function update_post_type_cache_incrementor( $post ) { | |
// Check autosave. | |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { | |
return; | |
} | |
$post = get_post( $post ); | |
if ( is_a( $post, '\WP_Post' ) ) { | |
post_type_cache_incrementor( $post->post_type, true ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment