Clean up and rename partial caching

This commit is contained in:
Tetrakern 2024-05-19 14:30:44 +02:00
parent 6c6e400383
commit 2f88f6af9a
11 changed files with 128 additions and 128 deletions

View File

@ -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' );
}
?>

View File

@ -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';

View File

@ -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() );
}
}
}

View File

@ -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' );

View File

@ -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() );
}
}
}

View File

@ -137,10 +137,10 @@ function fictioneer_output_modals( $args ) {
// Formatting and suggestions
if ( ! $is_archive && $args['post_type'] == 'fcn_chapter' ) {
?><input id="modal-formatting-toggle" data-target="formatting-modal" type="checkbox" tabindex="-1" class="modal-toggle" autocomplete="off" hidden><?php
fictioneer_get_static_template_part( 'partials/_modal-formatting' );
fictioneer_get_cached_partial( 'partials/_modal-formatting' );
?><input id="modal-tts-settings-toggle" data-target="tts-settings-modal" type="checkbox" tabindex="-1" class="modal-toggle" autocomplete="off" hidden><?php
fictioneer_get_static_template_part( 'partials/_modal-tts-settings' );
fictioneer_get_cached_partial( 'partials/_modal-tts-settings' );
if (
get_option( 'fictioneer_enable_suggestions' ) &&
@ -148,7 +148,7 @@ function fictioneer_output_modals( $args ) {
comments_open()
) {
?><input id="suggestions-modal-toggle" data-target="suggestions-modal" type="checkbox" tabindex="-1" class="modal-toggle" autocomplete="off" hidden><?php
fictioneer_get_static_template_part( 'partials/_modal-suggestions' );
fictioneer_get_cached_partial( 'partials/_modal-suggestions' );
}
}
@ -164,7 +164,7 @@ function fictioneer_output_modals( $args ) {
// Site settings
?><input id="modal-site-settings-toggle" data-target="site-settings-modal" type="checkbox" tabindex="-1" class="modal-toggle" autocomplete="off" hidden><?php
fictioneer_get_static_template_part( 'partials/_modal-site-settings' );
fictioneer_get_cached_partial( 'partials/_modal-site-settings' );
// BBCodes tutorial
if (
@ -174,7 +174,7 @@ function fictioneer_output_modals( $args ) {
! fictioneer_is_commenting_disabled( $args['post_id'] )
) {
?><input id="modal-bbcodes-toggle" data-target="bbcodes-modal" type="checkbox" tabindex="-1" class="modal-toggle" autocomplete="off" hidden><?php
fictioneer_get_static_template_part( 'partials/_modal-bbcodes' );
fictioneer_get_cached_partial( 'partials/_modal-bbcodes' );
}
// Story chapter changelog

View File

@ -192,7 +192,7 @@ if ( get_option( 'fictioneer_enable_follows' ) ) {
*/
function fictioneer_mobile_bookmarks_frame() {
fictioneer_get_static_template_part( 'partials/_template_mobile_bookmark' );
fictioneer_get_cached_partial( 'partials/_template_mobile_bookmark' );
// Start HTML ---> ?>
<div class="mobile-menu__frame" data-frame="bookmarks">

View File

@ -342,7 +342,7 @@ function fictioneer_tools_disable_font() {
fictioneer_build_bundled_fonts();
// Clear cached HTML for good measure
fictioneer_clear_cached_html();
fictioneer_clear_all_cached_partials();
// Log
fictioneer_log(
@ -394,7 +394,7 @@ function fictioneer_tools_enable_font() {
fictioneer_build_bundled_fonts();
// Clear cached HTML for good measure
fictioneer_clear_cached_html();
fictioneer_clear_all_cached_partials();
// Log
fictioneer_log(
@ -477,7 +477,7 @@ function fictioneer_tools_purge_theme_caches() {
}
}
fictioneer_clear_cached_html();
fictioneer_clear_all_cached_partials();
// Cache busting string
fictioneer_regenerate_cache_bust();

View File

@ -171,7 +171,7 @@ function fictioneer_settings_checkbox_added( $option, $value ) {
}
// Clear cached files
fictioneer_clear_cached_html();
fictioneer_clear_all_cached_partials();
// Relay
fictioneer_log_checkbox_update(
@ -199,7 +199,7 @@ function fictioneer_settings_checkbox_updated( $option, $old_value, $value ) {
}
// Clear cached files
fictioneer_clear_cached_html();
fictioneer_clear_all_cached_partials();
// Relay
fictioneer_log_checkbox_update(
@ -372,7 +372,7 @@ function fictioneer_settings_phrase_added( $option, $value ) {
}
// Clear cached files
fictioneer_clear_cached_html();
fictioneer_clear_all_cached_partials();
// Relay
fictioneer_log_phrase_update(
@ -400,7 +400,7 @@ function fictioneer_settings_phrase_updated( $option, $old_value, $value ) {
}
// Clear cached files
fictioneer_clear_cached_html();
fictioneer_clear_all_cached_partials();
// Replay
fictioneer_log_phrase_update(

View File

@ -676,8 +676,8 @@
<?php
fictioneer_settings_label_checkbox(
'fictioneer_enable_static_partials',
__( 'Enable caching of static partials', 'fictioneer' ),
__( 'Caches static partials as HTML files to accelerate rendering. Do not use this together with a cache plugin.', 'fictioneer' )
__( 'Enable caching of partials', 'fictioneer' ),
__( 'Caches parts of the page as static HTML files to accelerate rendering. Do not use this together with a cache plugin.', 'fictioneer' )
);
?>
</div>
@ -885,7 +885,7 @@
fictioneer_settings_textarea(
'fictioneer_comments_notice',
__( 'Notice above comments. Leave empty to hide. HTML allowed.', 'fictioneer' ),
'214px'
'152px'
);
?>
</div>

View File

@ -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 );
?>