Improve checks for running cache plugin

This commit is contained in:
Tetrakern 2024-01-29 17:08:56 +01:00
parent f39a259516
commit ccad4d272d
20 changed files with 71 additions and 56 deletions

View File

@ -33,10 +33,12 @@ if ( ! function_exists( 'fictioneer_caching_active' ) ) {
*
* @since 4.0.0
*
* @param string|null $context Context of the check. Currently unused.
*
* @return boolean Either true (active) or false (inactive or not installed).
*/
function fictioneer_caching_active() {
function fictioneer_caching_active( $context = null ) {
// Check early
if ( get_option( 'fictioneer_enable_cache_compatibility' ) ) {
return true;
@ -85,7 +87,7 @@ if ( ! function_exists( 'fictioneer_private_caching_active' ) ) {
function fictioneer_enable_shortcode_transients() {
// Check constant and caching status
$bool = FICTIONEER_SHORTCODE_TRANSIENT_EXPIRATION > -1 && ! fictioneer_caching_active();
$bool = FICTIONEER_SHORTCODE_TRANSIENT_EXPIRATION > -1 && ! fictioneer_caching_active( 'shortcodes' );
// Filter
$bool = apply_filters( 'fictioneer_filter_enable_shortcode_transients', $bool );
@ -503,7 +505,7 @@ function fictioneer_toggle_refresh_hooks( $add = true ) {
}
}
if ( FICTIONEER_CACHE_PURGE_ASSIST && fictioneer_caching_active() ) {
if ( FICTIONEER_CACHE_PURGE_ASSIST && fictioneer_caching_active( 'purge_assist' ) ) {
fictioneer_toggle_refresh_hooks();
}
@ -636,6 +638,7 @@ if ( ! function_exists( 'fictioneer_track_chapter_and_story_updates' ) ) {
// Refresh cached HTML output
delete_transient( 'fictioneer_story_chapter_list_' . $story_id );
delete_post_meta( $story_id, 'fictioneer_story_chapter_index_html' );
// Delete cached stories total word count
delete_transient( 'fictioneer_stories_total_word_count' );

View File

@ -989,7 +989,9 @@ if ( ! function_exists( 'fictioneer_get_chapter_list_items' ) ) {
function fictioneer_get_chapter_list_items( $story_id, $data, $current_index ) {
// Meta cache?
if ( ! fictioneer_caching_active() ) {
$cache_plugin_active = fictioneer_caching_active( 'chapter_list_items' );
if ( ! $cache_plugin_active ) {
$last_story_update = get_post_modified_time( 'U', true, $story_id );
$meta_cache = get_post_meta( $story_id, 'fictioneer_story_chapter_index_html', true );
@ -1051,8 +1053,8 @@ if ( ! function_exists( 'fictioneer_get_chapter_list_items' ) ) {
// Capture
$html = ob_get_clean();
// Meta-cache for next time
if ( ! fictioneer_caching_active() ) {
// Update meta cache
if ( ! $cache_plugin_active ) {
update_post_meta(
$story_id,
'fictioneer_story_chapter_index_html',

View File

@ -104,7 +104,7 @@ function fictioneer_do_fast_ajax() {
// Skip cache checks
if ( ! function_exists( 'fictioneer_caching_active' ) ) {
function fictioneer_caching_active() { return false; };
function fictioneer_caching_active( $context = null ) { return false; };
}
if ( ! function_exists( 'fictioneer_private_caching_active' ) ) {

View File

@ -67,7 +67,7 @@ if ( ! function_exists( 'fictioneer_get_card_list' ) ) {
// Default card arguments
$the_card_args = array(
'cache' => fictioneer_caching_active() && ! fictioneer_private_caching_active()
'cache' => fictioneer_caching_active( 'card_args' ) && ! fictioneer_private_caching_active()
);
// Merge with optional arguments

View File

@ -304,7 +304,7 @@ if ( ! function_exists( 'fictioneer_get_seo_title' ) ) {
return esc_html( _x( 'Archive', 'SEO fallback archive title.', 'fictioneer' ) );
}
// Meta cache for title?
// Meta cache for title (purged on update)?
$meta_cache = get_post_meta( $post_id, 'fictioneer_seo_title_cache', true );
if ( ! empty( $meta_cache ) && ! $skip_cache ) {
@ -459,7 +459,7 @@ if ( ! function_exists( 'fictioneer_get_seo_description' ) ) {
return esc_html( sprintf( __( 'Archived posts on %s.', 'fictioneer' ), get_bloginfo( 'name' ) ) );
}
// Meta cache for description?
// Meta cache for description (purged on update)?
$meta_cache = get_post_meta( $post_id, 'fictioneer_seo_description_cache', true );
if ( ! empty( $meta_cache ) && ! $skip_cache ) {
@ -548,7 +548,7 @@ if ( ! function_exists( 'fictioneer_get_seo_image' ) ) {
$image_id = $default_id;
}
// Meta cache for image? Except for site default, which can globally change!
// Meta cache for image (purged on update)? Except for site default, which can globally change!
if ( ! $use_default ) {
$meta_cache = get_post_meta( $post_id, 'fictioneer_seo_og_image_cache', true );

View File

@ -338,7 +338,7 @@ function fictioneer_root_attributes() {
$conditions = array(
'data-age-confirmation' => get_option( 'fictioneer_enable_site_age_confirmation' ),
'data-caching-active' => fictioneer_caching_active(),
'data-caching-active' => fictioneer_caching_active( 'root_attribute' ),
'data-ajax-submit' => get_option( 'fictioneer_enable_ajax_comment_submit', false ),
'data-force-child-theme' => ! FICTIONEER_THEME_SWITCH,
'data-public-caching' => get_option( 'fictioneer_enable_public_cache_compatibility', false ),
@ -401,7 +401,7 @@ function fictioneer_add_classes_to_body( $classes ) {
}
// Browsers
if ( ! fictioneer_caching_active() ) {
if ( ! fictioneer_caching_active( 'devise_body_classes' ) ) {
$includes['is-iphone'] = $GLOBALS['is_iphone'];
$includes['is-chrome'] = $GLOBALS['is_chrome'];
$includes['is-safari'] = $GLOBALS['is_safari'];

View File

@ -261,7 +261,7 @@ if ( ! function_exists( 'fictioneer_get_story_data' ) ) {
return false;
}
// Meta cache?
// Meta cache (purged on update)?
if ( FICTIONEER_ENABLE_STORY_DATA_META_CACHE ) {
$meta_cache = get_post_meta( $story_id, 'fictioneer_story_data_collection', true );
}
@ -275,7 +275,7 @@ if ( ! function_exists( 'fictioneer_get_story_data' ) ) {
// Time to refresh comment count?
$comment_count_delay = ( $meta_cache['comment_count_timestamp'] ?? 0 ) + FICTIONEER_STORY_COMMENT_COUNT_TIMEOUT;
$refresh_comments = $comment_count_delay < time() ||
( $args['refresh_comment_count'] ?? 0 ) || fictioneer_caching_active();
( $args['refresh_comment_count'] ?? 0 ) || fictioneer_caching_active( 'story_data_refresh_comment_count' );
// Refresh comment count
if ( $refresh_comments ) {
@ -430,11 +430,15 @@ if ( ! function_exists( 'fictioneer_get_author_statistics' ) ) {
return false;
}
// Meta cache?
$meta_cache = $author->fictioneer_author_statistics;
$cache_plugin_active = fictioneer_caching_active( 'author_statistics' );
if ( $meta_cache && ( $meta_cache['valid_until'] ?? 0 ) > time() ) {
return $meta_cache;
// Meta cache?
if ( ! $cache_plugin_active ) {
$meta_cache = $author->fictioneer_author_statistics;
if ( $meta_cache && ( $meta_cache['valid_until'] ?? 0 ) > time() ) {
return $meta_cache;
}
}
// Get stories
@ -509,7 +513,9 @@ if ( ! function_exists( 'fictioneer_get_author_statistics' ) ) {
);
// Update meta cache
fictioneer_update_user_meta( $author_id, 'fictioneer_author_statistics', $result );
if ( ! $cache_plugin_active ) {
fictioneer_update_user_meta( $author_id, 'fictioneer_author_statistics', $result );
}
// Done
return $result;
@ -533,10 +539,14 @@ if ( ! function_exists( 'fictioneer_get_collection_statistics' ) ) {
function fictioneer_get_collection_statistics( $collection_id ) {
// Meta cache?
$meta_cache = get_post_meta( $collection_id, 'fictioneer_collection_statistics_cache', true );
$cache_plugin_active = fictioneer_caching_active( 'collection_statistics' );
if ( ! empty( $meta_cache ) && ( $meta_cache['valid_until'] ?? 0 ) > time() ) {
return $meta_cache;
if ( ! $cache_plugin_active ) {
$meta_cache = get_post_meta( $collection_id, 'fictioneer_collection_statistics', true );
if ( $meta_cache && ( $meta_cache['valid_until'] ?? 0 ) > time() ) {
return $meta_cache;
}
}
// Setup
@ -620,7 +630,9 @@ if ( ! function_exists( 'fictioneer_get_collection_statistics' ) ) {
);
// Update meta cache
update_post_meta( $collection_id, 'fictioneer_collection_statistics_cache', $statistics );
if ( ! $cache_plugin_active ) {
update_post_meta( $collection_id, 'fictioneer_collection_statistics', $statistics );
}
// Done
return $statistics;

View File

@ -1139,7 +1139,7 @@ function fictioneer_gate_unpublished_content() {
// 404 if access is not allowed
if (
fictioneer_caching_active() &&
fictioneer_caching_active( 'get_unpublished_content' ) &&
$post->post_status !== 'publish' &&
! fictioneer_verify_unpublish_access( $post->ID )
) {

View File

@ -405,7 +405,7 @@ function fictioneer_ajax_submit_comment() {
$html = trim( $html );
// Purge cache if necessary
if ( fictioneer_caching_active() && ! get_option( 'fictioneer_enable_ajax_comments' ) ) {
if ( fictioneer_caching_active( 'ajax_comment_submit' ) && ! get_option( 'fictioneer_enable_ajax_comments' ) ) {
fictioneer_purge_post_cache( $post_id );
}

View File

@ -284,7 +284,7 @@ function fictioneer_comment_post( $comment_id, $comment_approved, $commentdata )
}
// Purge cache of post/page the comment is for
if ( fictioneer_caching_active() && isset( $commentdata['comment_post_ID'] ) ) {
if ( fictioneer_caching_active( 'comment_post' ) && isset( $commentdata['comment_post_ID'] ) ) {
fictioneer_purge_post_cache( $commentdata['comment_post_ID'] );
}
@ -529,7 +529,7 @@ if ( ! function_exists( 'fictioneer_unsubscribe_from_comment' ) ) {
function fictioneer_comment_edit( $comment_ID, $data ) {
// Purge cache of post/page the comment is for
if ( fictioneer_caching_active() && isset( $data['comment_post_ID'] ) ) {
if ( fictioneer_caching_active( 'comment_edit' ) && isset( $data['comment_post_ID'] ) ) {
fictioneer_purge_post_cache( $data['comment_post_ID'] );
}
}

View File

@ -608,7 +608,7 @@ function fictioneer_ajax_moderate_comment() {
// Send result
if ( $result ) {
// Clear cache if necessary
if ( fictioneer_caching_active() ) {
if ( fictioneer_caching_active( 'ajax_comment_moderation' ) ) {
fictioneer_purge_post_cache( $comment->comment_post_ID );
}
@ -714,7 +714,7 @@ function fictioneer_ajax_report_comment() {
}
// Purge cache
if ( fictioneer_caching_active() ) {
if ( fictioneer_caching_active( 'ajax_report_comment' ) ) {
fictioneer_purge_post_cache( $comment->comment_post_ID );
}

View File

@ -368,7 +368,7 @@ if ( ! function_exists( 'fictioneer_theme_comment' ) ) {
$flag_classes = '';
// Flags
$is_caching = fictioneer_caching_active();
$cache_plugin_active = fictioneer_caching_active( 'theme_comment' );
$is_owner = ! empty( $comment_user_id ) && $comment_user_id == $current_user->ID;
$is_approved = $comment->comment_approved;
$is_offensive = get_comment_meta( $comment->comment_ID, 'fictioneer_marked_offensive', true );
@ -404,7 +404,7 @@ if ( ! function_exists( 'fictioneer_theme_comment' ) ) {
}
// No individual flags for cached comments unless loaded via AJAX or caches are per user
if ( ! wp_doing_ajax() && $is_caching && ! fictioneer_private_caching_active() ) {
if ( ! wp_doing_ajax() && $cache_plugin_active && ! fictioneer_private_caching_active() ) {
$is_flagged_by_current_user = false;
$flag_classes = '_dubious';
}
@ -608,7 +608,7 @@ if ( ! function_exists( 'fictioneer_theme_comment' ) ) {
// COMMENT EDIT
// =============================================================================
?>
<?php if ( ( $can_edit && $is_editable ) || ( $is_caching && ! $is_deleted_by_owner) ) : ?>
<?php if ( ( $can_edit && $is_editable ) || ( $cache_plugin_active && ! $is_deleted_by_owner) ) : ?>
<div class="fictioneer-comment__edit" hidden>
<textarea class="comment-inline-edit-content"><?php echo $comment->comment_content; ?></textarea>
</div>
@ -698,7 +698,7 @@ if ( ! function_exists( 'fictioneer_theme_comment' ) ) {
// caching is active. In either case, the button will be made visible by JS.
// =============================================================================
?>
<?php if ( ( $can_edit && $is_editable ) || $is_caching ) : ?>
<?php if ( ( $can_edit && $is_editable ) || $cache_plugin_active ) : ?>
<button
class="fictioneer-comment__edit-toggle comment-quick-button hide-on-edit tooltipped hide-if-logged-out hide-on-ajax"
type="button"
@ -714,7 +714,7 @@ if ( ! function_exists( 'fictioneer_theme_comment' ) ) {
// active. In either case, the button will be made visible by JS.
// =============================================================================
?>
<?php if ( $is_owner || $is_caching ) : ?>
<?php if ( $is_owner || $cache_plugin_active ) : ?>
<button
class="fictioneer-comment__delete comment-quick-button hide-on-edit tooltipped hide-if-logged-out hide-on-ajax"
type="button"

View File

@ -30,7 +30,7 @@ function fictioneer_chapters_list( $args ) {
<?php
// Card arguments
$card_args = array(
'cache' => fictioneer_caching_active() && ! fictioneer_private_caching_active(),
'cache' => fictioneer_caching_active( 'card_args' ) && ! fictioneer_private_caching_active(),
'order' => $args['order'] ?? 'desc',
'orderby' => $args['orderby'] ?? 'modified',
'ago' => $args['ago'] ?? 0

View File

@ -30,7 +30,7 @@ function fictioneer_collections_list( $args ) {
<?php
// Card arguments
$card_args = array(
'cache' => fictioneer_caching_active() && ! fictioneer_private_caching_active(),
'cache' => fictioneer_caching_active( 'card_args' ) && ! fictioneer_private_caching_active(),
'order' => $args['order'] ?? 'desc',
'orderby' => $args['orderby'] ?? 'modified',
'ago' => $args['ago'] ?? 0
@ -197,7 +197,7 @@ function fictioneer_collection_featured_list( $args ) {
$card_args = array( 'show_type' => true );
// Cached?
if ( fictioneer_caching_active() && ! fictioneer_private_caching_active() ) {
if ( fictioneer_caching_active( 'card_args' ) && ! fictioneer_private_caching_active() ) {
$card_args['cache'] = true;
}

View File

@ -91,7 +91,7 @@ function fictioneer_post_featured_list( $post_id ) {
$card_args = array( 'show_type' => true );
// Cached?
if ( fictioneer_caching_active() && ! fictioneer_private_caching_active() ) {
if ( fictioneer_caching_active( 'card_args' ) && ! fictioneer_private_caching_active() ) {
$card_args['cache'] = true;
}

View File

@ -30,7 +30,7 @@ function fictioneer_recommendations_list( $args ) {
<?php
// Card arguments
$card_args = array(
'cache' => fictioneer_caching_active() && ! fictioneer_private_caching_active(),
'cache' => fictioneer_caching_active( 'card_args' ) && ! fictioneer_private_caching_active(),
'order' => $args['order'] ?? 'desc',
'orderby' => $args['orderby'] ?? 'modified',
'ago' => $args['ago'] ?? 0

View File

@ -106,7 +106,7 @@ function fictioneer_stories_list( $args ) {
<?php
// Card arguments
$card_args = array(
'cache' => fictioneer_caching_active() && ! fictioneer_private_caching_active(),
'cache' => fictioneer_caching_active( 'card_args' ) && ! fictioneer_private_caching_active(),
'order' => $args['order'] ?? 'desc',
'orderby' => $args['orderby'] ?? 'modified',
'ago' => $args['ago'] ?? 0
@ -494,13 +494,16 @@ function fictioneer_story_chapters( $args ) {
return;
}
// Check for cached chapters output
$chapters_html = FICTIONEER_CHAPTER_LIST_TRANSIENTS ?
get_transient( 'fictioneer_story_chapter_list_' . $args['story_id'] ) : null;
$cache_plugin_active = fictioneer_caching_active( 'story_chapter_list' );
if ( ! empty( $chapters_html ) ) {
echo $chapters_html;
return;
// Check for cached chapters output
if ( FICTIONEER_CHAPTER_LIST_TRANSIENTS && ! $cache_plugin_active ) {
$transient_cache = get_transient( 'fictioneer_story_chapter_list_' . $args['story_id'] );
if ( $transient_cache ) {
echo $transient_cache;
return;
}
}
// Setup
@ -709,7 +712,7 @@ function fictioneer_story_chapters( $args ) {
echo $chapters_html;
// Cache for next time (24 hours)
if ( FICTIONEER_CHAPTER_LIST_TRANSIENTS ) {
if ( FICTIONEER_CHAPTER_LIST_TRANSIENTS && ! $cache_plugin_active ) {
set_transient( 'fictioneer_story_chapter_list_' . $story_id, $chapters_html, 86400 );
}
}

View File

@ -261,7 +261,7 @@
<a class="button button--secondary disabled" href="<?php echo fictioneer_tool_action( 'fictioneer_fix_posts' ); ?>"><?php _e( 'Fix Posts', 'fictioneer' ); ?></a>
</div>
<?php if ( fictioneer_caching_active() ) : ?>
<?php if ( fictioneer_caching_active( 'fictioneer_settings_tab_tools' ) ) : ?>
<hr>

View File

@ -53,18 +53,13 @@ $hook_args = array(
// Setup
$type = get_post_type();
$card_args = array(
'cache' => fictioneer_caching_active() && ! fictioneer_private_caching_active(),
'cache' => fictioneer_caching_active( 'card_args' ) && ! fictioneer_private_caching_active(),
'show_type' => true,
'order' => $order,
'orderby' => $orderby,
'ago' => $ago
);
// Cached?
if ( fictioneer_caching_active() && ! fictioneer_private_caching_active() ) {
$card_args['cache'] = true;
}
// Special conditions for chapters...
if ( $type == 'fcn_chapter' ) {
if (

View File

@ -114,7 +114,7 @@ $hook_args['is_advanced_search'] = $is_advanced_search;
$card_args = array( 'show_type' => true );
// Cached?
if ( fictioneer_caching_active() && ! fictioneer_private_caching_active() ) {
if ( fictioneer_caching_active( 'card_args' ) && ! fictioneer_private_caching_active() ) {
$card_args['cache'] = true;
}