From 2f88f6af9a81be1d45364b06650b3f4dc1784592 Mon Sep 17 00:00:00 2001 From: Tetrakern <26898880+Tetrakern@users.noreply.github.com> Date: Sun, 19 May 2024 14:30:44 +0200 Subject: [PATCH] Clean up and rename partial caching --- footer.php | 4 +- includes/functions/_customizer-settings.php | 2 +- includes/functions/_service-caching.php | 108 +++++++++++++++++- includes/functions/_setup-theme.php | 2 +- includes/functions/_utility.php | 106 ----------------- includes/functions/hooks/_general_hooks.php | 10 +- .../functions/hooks/_mobile_menu_hooks.php | 2 +- .../functions/settings/_settings_actions.php | 6 +- .../functions/settings/_settings_loggers.php | 8 +- .../settings/_settings_page_general.php | 6 +- partials/_bookmarks.php | 2 +- 11 files changed, 128 insertions(+), 128 deletions(-) diff --git a/footer.php b/footer.php index fccecfa5..127b92ec 100644 --- a/footer.php +++ b/footer.php @@ -63,12 +63,12 @@ do_action( 'fictioneer_after_main', $args ); $args['post_type'] == 'fcn_chapter' && ! post_password_required() ) { - fictioneer_get_static_template_part( 'partials/_tts-interface' ); + fictioneer_get_cached_partial( 'partials/_tts-interface' ); } // Render cookie banner HTML if required if ( get_option( 'fictioneer_cookie_banner' ) ) { - fictioneer_get_static_template_part( 'partials/_consent-banner' ); + fictioneer_get_cached_partial( 'partials/_consent-banner' ); } ?> diff --git a/includes/functions/_customizer-settings.php b/includes/functions/_customizer-settings.php index 8fe49d0a..ae1230bb 100644 --- a/includes/functions/_customizer-settings.php +++ b/includes/functions/_customizer-settings.php @@ -24,7 +24,7 @@ function fictioneer_watch_for_customizer_updates() { fictioneer_build_dynamic_scripts(); // Clear cached HTML - fictioneer_clear_cached_html(); + fictioneer_clear_all_cached_partials(); // Files $bundled_fonts = WP_CONTENT_DIR . '/themes/fictioneer/cache/bundled-fonts.css'; diff --git a/includes/functions/_service-caching.php b/includes/functions/_service-caching.php index 3da07c8f..f1ff35bd 100644 --- a/includes/functions/_service-caching.php +++ b/includes/functions/_service-caching.php @@ -181,7 +181,7 @@ if ( ! function_exists( 'fictioneer_purge_all_caches' ) ) { // developer cannot be bothered, neither can I. // Cached HTML by theme - fictioneer_clear_cached_html(); + fictioneer_clear_all_cached_partials(); } } @@ -760,3 +760,109 @@ function fictioneer_purge_nav_menu_transients() { delete_transient( 'fictioneer_footer_menu_html' ); } add_action( 'wp_update_nav_menu', 'fictioneer_purge_nav_menu_transients' ); + +// ============================================================================= +// PARTIAL CACHING (THEME) +// ============================================================================= + +/** + * Get static HTML of cached template partial + * + * @since 5.18.1 + * @see get_template_part() + * + * @param string $slug The slug name for the generic template. + * @param string $identifier Optional. Additional identifier string for the file. Default empty string. + * @param int|null $expiration Optional. Seconds until the cache expires. Default 24 hours in seconds. + * @param string|null $name Optional. The name of the specialized template. + * @param array $args Optional. Additional arguments passed to the template. + */ + +function fictioneer_get_cached_partial( $slug, $identifier = '', $expiration = null, $name = null, $args = [] ) { + // Use default function if... + if ( + ! get_option( 'fictioneer_enable_static_partials' ) || + is_customize_preview() || + fictioneer_caching_active( "get_template_part_for_{$slug}" ) + ) { + get_template_part( $slug, $name, $args ); + + return; + } + + // Setup + $args_hash = md5( serialize( $args ) . $identifier ); + $static_file = $slug . ( $name ? "-{$name}" : '' ) . "-{$args_hash}.html"; + $path = get_template_directory() . '/cache/html/' . $static_file; + + // Make sure directory exists and handle failure + if ( ! file_exists( dirname( $path ) ) ) { + if ( ! mkdir( dirname( $path ), 0755, true ) && ! is_dir( dirname( $path ) ) ) { + get_template_part( $slug, $name, $args ); + return; + } + } + + // Get static file if not expired + if ( file_exists( $path ) ) { + $filemtime = filemtime( $path ); + + if ( time() - $filemtime < ( $expiration ?? DAY_IN_SECONDS ) ) { + echo file_get_contents( $path ); + return; + } + } + + // Generate and cache the new file + ob_start(); + get_template_part( $slug, $name, $args ); + $html = ob_get_clean(); + + if ( file_put_contents( $path, $html ) === false ) { + get_template_part( $slug, $name, $args ); + } else { + echo $html; + } +} + +/** + * Clear static HTML of all cached partials + * + * Note: Only executed once per request to reduce overhead. + * Can therefore be used with impunity. + * + * @since 5.18.1 + */ + +function fictioneer_clear_all_cached_partials() { + // Only run this once per request + static $done = false; + + if ( $done || ! get_option( 'fictioneer_enable_static_partials' ) ) { + return; + } + + $done = true; + + // Setup + $cache_dir = get_template_directory() . '/cache/html/'; + + // Ensure the directory exists + if ( ! file_exists( $cache_dir ) ) { + return; + } + + // Recursively delete files and subdirectories + $files = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator( $cache_dir, RecursiveDirectoryIterator::SKIP_DOTS ), + RecursiveIteratorIterator::CHILD_FIRST + ); + + foreach ( $files as $fileinfo ) { + $todo = ( $fileinfo->isDir() ? 'rmdir' : 'unlink' ); + + if ( ! $todo( $fileinfo->getRealPath() ) ) { + error_log( 'Failed to delete ' . $fileinfo->getRealPath() ); + } + } +} diff --git a/includes/functions/_setup-theme.php b/includes/functions/_setup-theme.php index 7cf12f10..3866bd7d 100644 --- a/includes/functions/_setup-theme.php +++ b/includes/functions/_setup-theme.php @@ -160,7 +160,7 @@ function fictioneer_purge_caches_after_update() { } } - fictioneer_clear_cached_html(); + fictioneer_clear_all_cached_partials(); } add_action( 'fictioneer_after_update', 'fictioneer_purge_caches_after_update' ); diff --git a/includes/functions/_utility.php b/includes/functions/_utility.php index a2e811bc..9ba698eb 100644 --- a/includes/functions/_utility.php +++ b/includes/functions/_utility.php @@ -3055,109 +3055,3 @@ function fictioneer_get_post_patreon_data( $post = null ) { // Return return $data; } - -// ============================================================================= -// GET STATIC TEMPLATE PARTS -// ============================================================================= - -/** - * Get template part from static cache if available - * - * @since 5.18.1 - * @see get_template_part() - * - * @param string $slug The slug name for the generic template. - * @param string $identifier Optional. Additional identifier string for the file. Default empty string. - * @param int|null $expiration Optional. Seconds until the cache expires. Default 24 hours in seconds. - * @param string|null $name Optional. The name of the specialized template. - * @param array $args Optional. Additional arguments passed to the template. - */ - -function fictioneer_get_static_template_part( $slug, $identifier = '', $expiration = null, $name = null, $args = [] ) { - // Use default function if... - if ( - ! get_option( 'fictioneer_enable_static_partials' ) || - is_customize_preview() || - fictioneer_caching_active( "get_template_part_for_{$slug}" ) - ) { - get_template_part( $slug, $name, $args ); - - return; - } - - // Setup - $args_hash = md5( serialize( $args ) . $identifier ); - $static_file = $slug . ( $name ? "-{$name}" : '' ) . "-{$args_hash}.html"; - $path = get_template_directory() . '/cache/html/' . $static_file; - - // Make sure directory exists and handle failure - if ( ! file_exists( dirname( $path ) ) ) { - if ( ! mkdir( dirname( $path ), 0755, true ) && ! is_dir( dirname( $path ) ) ) { - get_template_part( $slug, $name, $args ); - return; - } - } - - // Get static file if not expired - if ( file_exists( $path ) ) { - $filemtime = filemtime( $path ); - - if ( time() - $filemtime < ( $expiration ?? DAY_IN_SECONDS ) ) { - echo file_get_contents( $path ); - return; - } - } - - // Generate and cache the new file - ob_start(); - get_template_part( $slug, $name, $args ); - $html = ob_get_clean(); - - if ( file_put_contents( $path, $html ) === false ) { - get_template_part( $slug, $name, $args ); - } else { - echo $html; - } -} - -/** - * Clear cached static files - * - * Note: Only executed once per request to reduce overhead. - * Can therefore be used with impunity. - * - * @since 5.18.1 - */ - -function fictioneer_clear_cached_html() { - // Only run this once per request - static $done = false; - - if ( $done || ! get_option( 'fictioneer_enable_static_partials' ) ) { - return; - } - - $done = true; - - // Setup - $cache_dir = get_template_directory() . '/cache/html/'; - - // Ensure the directory exists - if ( ! file_exists( $cache_dir ) ) { - return; - } - - // Recursively delete files and subdirectories - $files = new RecursiveIteratorIterator( - new RecursiveDirectoryIterator( $cache_dir, RecursiveDirectoryIterator::SKIP_DOTS ), - RecursiveIteratorIterator::CHILD_FIRST - ); - - foreach ( $files as $fileinfo ) { - $todo = ( $fileinfo->isDir() ? 'rmdir' : 'unlink' ); - - if ( ! $todo( $fileinfo->getRealPath() ) ) { - error_log( 'Failed to delete ' . $fileinfo->getRealPath() ); - } - } -} diff --git a/includes/functions/hooks/_general_hooks.php b/includes/functions/hooks/_general_hooks.php index 333e11e6..18dc3516 100644 --- a/includes/functions/hooks/_general_hooks.php +++ b/includes/functions/hooks/_general_hooks.php @@ -137,10 +137,10 @@ function fictioneer_output_modals( $args ) { // Formatting and suggestions if ( ! $is_archive && $args['post_type'] == 'fcn_chapter' ) { ?> ?>
@@ -885,7 +885,7 @@ fictioneer_settings_textarea( 'fictioneer_comments_notice', __( 'Notice above comments. Leave empty to hide. HTML allowed.', 'fictioneer' ), - '214px' + '152px' ); ?> diff --git a/partials/_bookmarks.php b/partials/_bookmarks.php index 7d7d9f05..6baab85e 100644 --- a/partials/_bookmarks.php +++ b/partials/_bookmarks.php @@ -22,7 +22,7 @@ defined( 'ABSPATH' ) OR exit; $show_empty = isset( $args['show_empty'] ) && $args['show_empty']; $count = isset( $args['count'] ) ? intval( $args['count'] ) : -1; -fictioneer_get_static_template_part( 'partials/_template_bookmark', '', null, null, $args ); +fictioneer_get_cached_partial( 'partials/_template_bookmark', '', null, null, $args ); ?>