Add fictioneer_get_theme_cache_dir()

This commit is contained in:
Tetrakern 2024-08-28 19:18:18 +02:00
parent ee64b3cbdf
commit 5c7756fe9c
8 changed files with 50 additions and 51 deletions

View File

@ -1567,7 +1567,6 @@ define( 'CONSTANT_NAME', value );
| FICTIONEER_DISCORD_EMBED_COLOR | string | Color code for Discord notifications. Default `'9692513'`.
| FICTIONEER_TRUNCATION_ELLIPSIS | string | Appended to truncated strings. Default `…`.
| FICTIONEER_AGE_CONFIRMATION_REDIRECT | string | Redirect URL if a visitor reject the age confirmation. Default `https://search.brave.com/`.
| FICTIONEER_CACHE_DIR | string | Server path to the theme cache directory.
| FICTIONEER_COMMENTCODE_TTL | integer | How long guests can see their private/unapproved comments in _seconds_. Default `600`.
| FICTIONEER_AJAX_TTL | integer | How long to cache certain AJAX requests locally in _milliseconds_. Default `60000`.
| FICTIONEER_AJAX_LOGIN_TTL | integer | How long to cache AJAX authentications locally in _milliseconds_. Default `15000`.

View File

@ -130,11 +130,6 @@ if ( ! defined( 'FICTIONEER_DEFAULT_CHAPTER_ICON' ) ) {
define( 'FICTIONEER_DEFAULT_CHAPTER_ICON', 'fa-solid fa-book' );
}
// String: Path to cache directory (without trailing slash)
if ( ! defined( 'FICTIONEER_CACHE_DIR' ) ) {
define( 'FICTIONEER_CACHE_DIR', WP_CONTENT_DIR . '/themes/fictioneer/cache' );
}
/*
* Date Strings
*/

View File

@ -27,7 +27,7 @@ function fictioneer_watch_for_customizer_updates() {
fictioneer_clear_all_cached_partials();
// Files
$bundled_fonts = FICTIONEER_CACHE_DIR . '/bundled-fonts.css';
$bundled_fonts = fictioneer_get_theme_cache_dir( 'watch_for_customizer_updates' ) . '/bundled-fonts.css';
if ( file_exists( $bundled_fonts ) ) {
unlink( $bundled_fonts );

View File

@ -466,7 +466,7 @@ function fictioneer_get_font_data() {
function fictioneer_build_bundled_fonts() {
// Setup
$base_fonts = WP_CONTENT_DIR . '/themes/fictioneer/css/fonts-base.css';
$bundled_fonts = FICTIONEER_CACHE_DIR . '/bundled-fonts.css';
$bundled_fonts = fictioneer_get_theme_cache_dir( 'build_bundled_fonts' ) . '/bundled-fonts.css';
$fonts = fictioneer_get_font_data();
$disabled_fonts = get_option( 'fictioneer_disabled_fonts', [] );
$disabled_fonts = is_array( $disabled_fonts ) ? $disabled_fonts : [];
@ -476,11 +476,6 @@ function fictioneer_build_bundled_fonts() {
// Apply filters
$fonts = apply_filters( 'fictioneer_filter_pre_build_bundled_fonts', $fonts );
// Make sure directory exists
if ( ! is_dir( FICTIONEER_CACHE_DIR ) ) {
mkdir( FICTIONEER_CACHE_DIR, 0755, true );
}
// Build
if ( file_exists( $base_fonts ) ) {
$css = file_get_contents( $base_fonts );
@ -621,7 +616,7 @@ function fictioneer_get_theme_color( $mod, $default = null ) {
function fictioneer_build_customize_css( $context = null ) {
// --- Setup -----------------------------------------------------------------
$file_path = FICTIONEER_CACHE_DIR . '/customize.css';
$file_path = fictioneer_get_theme_cache_dir( 'build_customize_css' ) . '/customize.css';
$site_width = (int) get_theme_mod( 'site_width', 960 );
$header_image_style = get_theme_mod( 'header_image_style', 'default' );
$header_style = get_theme_mod( 'header_style', 'default' );
@ -634,12 +629,7 @@ function fictioneer_build_customize_css( $context = null ) {
$css = '';
if ( $context === 'preview' ) {
$file_path = FICTIONEER_CACHE_DIR . '/customize-preview.css';
}
// Make sure directory exists
if ( ! is_dir( FICTIONEER_CACHE_DIR ) ) {
mkdir( FICTIONEER_CACHE_DIR, 0755, true );
$file_path = fictioneer_get_theme_cache_dir( 'preview' ) . '/customize-preview.css';
}
// --- Assets ----------------------------------------------------------------

View File

@ -914,7 +914,7 @@ function fictioneer_get_cache_salt() {
function fictioneer_create_html_cache_directory( $dir = null ) {
// Setup
$default_dir = FICTIONEER_CACHE_DIR . '/html/';
$default_dir = fictioneer_get_theme_cache_dir( 'create_html_cache_directory' ) . '/html/';
$dir = $dir ?? $default_dir;
$result = true;
@ -975,7 +975,7 @@ function fictioneer_get_cached_partial( $slug, $identifier = '', $expiration = n
// Setup
$args_hash = md5( serialize( $args ) . $identifier . fictioneer_get_cache_salt() );
$static_file = $slug . ( $name ? "-{$name}" : '' ) . "-{$args_hash}.html";
$path = FICTIONEER_CACHE_DIR . '/html/' . $static_file;
$path = fictioneer_get_theme_cache_dir( 'get_cached_partial' ) . '/html/' . $static_file;
// Make sure directory exists and handle failure
if ( ! fictioneer_create_html_cache_directory( dirname( $path ) ) ) {
@ -1025,7 +1025,7 @@ function fictioneer_clear_all_cached_partials() {
$done = true;
// Setup
$cache_dir = FICTIONEER_CACHE_DIR . '/html/';
$cache_dir = fictioneer_get_theme_cache_dir( 'clear_all_cached_partials' ) . '/html/';
// Regenerate cache salt
fictioneer_generate_cache_salt();
@ -1083,7 +1083,7 @@ function fictioneer_get_static_content( $more_link_text = \null, $strip_teaser =
// Setup
$hash = md5( $post->ID . fictioneer_get_cache_salt() );
$dir = FICTIONEER_CACHE_DIR . '/html/' . substr( $hash, 0, 2 );
$dir = fictioneer_get_theme_cache_dir( 'get_static_content' ) . '/html/' . substr( $hash, 0, 2 );
$path = "{$dir}/{$hash}_{$post->ID}.html";
// Make sure directory exists and handle failure
@ -1142,7 +1142,7 @@ function fictioneer_the_static_content( $more_link_text = \null, $strip_teaser =
function fictioneer_clear_cached_content( $post_id ) {
// Setup
$hash = md5( $post_id . fictioneer_get_cache_salt() );
$dir = FICTIONEER_CACHE_DIR . '/html/' . substr( $hash, 0, 2 );
$dir = fictioneer_get_theme_cache_dir( 'clear_cached_content' ) . '/html/' . substr( $hash, 0, 2 );
$path = "{$dir}/{$hash}_{$post_id}.html";
// Delete file

View File

@ -972,29 +972,25 @@ function fictioneer_look_for_issues() {
global $wpdb;
// Setup
$dynamic_scripts_path = FICTIONEER_CACHE_DIR . '/dynamic-scripts.js';
$bundled_fonts_path = FICTIONEER_CACHE_DIR . '/bundled-fonts.css';
$customize_css_path = FICTIONEER_CACHE_DIR . '/customize.css';
$cache_dir = fictioneer_get_theme_cache_dir( 'looking_for_issues' );
$dynamic_scripts_path = $cache_dir . '/dynamic-scripts.js';
$bundled_fonts_path = $cache_dir . '/bundled-fonts.css';
$customize_css_path = $cache_dir . '/customize.css';
$issues = [];
// Make sure directory exists
if ( ! is_dir( FICTIONEER_CACHE_DIR ) ) {
mkdir( FICTIONEER_CACHE_DIR, 0755, true );
}
// Cache directory set up?
if ( ! is_dir( FICTIONEER_CACHE_DIR ) ) {
if ( ! is_dir( $cache_dir ) ) {
$issues[] = sprintf(
__( '<code>%s</code> directory could not be found or created.', 'fictioneer' ),
FICTIONEER_CACHE_DIR
$cache_dir
);
} else {
$permissions = substr( sprintf( '%o', fileperms( FICTIONEER_CACHE_DIR ) ), -4 );
$permissions = substr( sprintf( '%o', fileperms( $cache_dir ) ), -4 );
if ( $permissions !== '0755' ) {
$issues[] = sprintf(
__( '<code>%s</code> directory permissions are not 755.', 'fictioneer' ),
FICTIONEER_CACHE_DIR
$cache_dir
);
}
}
@ -1013,7 +1009,7 @@ function fictioneer_look_for_issues() {
$issues[] = sprintf(
__( '<strong>%s</strong> could not be found or created in the <code>%s</code> directory.', 'fictioneer' ),
'dynamic-scripts.js',
FICTIONEER_CACHE_DIR
$cache_dir
);
} else {
$permissions = substr( sprintf( '%o', fileperms( $dynamic_scripts_path ) ), -4 );
@ -1031,7 +1027,7 @@ function fictioneer_look_for_issues() {
$issues[] = sprintf(
__( '<strong>%s</strong> could not be found or created in the <code>%s</code> directory.', 'fictioneer' ),
'bundled-fonts.css',
FICTIONEER_CACHE_DIR
$cache_dir
);
} else {
$permissions = substr( sprintf( '%o', fileperms( $bundled_fonts_path ) ), -4 );
@ -1049,7 +1045,7 @@ function fictioneer_look_for_issues() {
$issues[] = sprintf(
__( '<strong>%s</strong> could not be found or created in the <code>%s</code> directory.', 'fictioneer' ),
'customize.css',
FICTIONEER_CACHE_DIR
$cache_dir
);
} else {
$permissions = substr( sprintf( '%o', fileperms( $customize_css_path ) ), -4 );

View File

@ -22,6 +22,30 @@ function fictioneer_get_theme_cache_uri( $context = null ) {
);
}
/**
* Returns directory path of the theme cache
*
* @since 5.23.1
*
* @param string|null $context The context of the call. Default null.
*
* @return string Path of the theme directory.
*/
function fictioneer_get_theme_cache_dir( $context = null ) {
$dir = apply_filters(
'fictioneer_filter_cache_dir',
WP_CONTENT_DIR . '/themes/fictioneer/cache',
$context
);
if ( ! is_dir( $dir ) ) {
mkdir( $dir, 0755, true );
}
return $dir;
}
// =============================================================================
// LEGACY CLEANUP
// =============================================================================
@ -306,7 +330,7 @@ function fictioneer_purge_caches_after_update() {
fictioneer_regenerate_cache_bust();
// Delete cached files
$files = glob( trailingslashit( FICTIONEER_CACHE_DIR ) . '*' );
$files = glob( trailingslashit( fictioneer_get_theme_cache_dir( 'purge_caches_after_update' ) ) . '*' );
foreach ( $files as $file ) {
if ( is_file( $file ) ) {
@ -958,7 +982,7 @@ add_action( 'wp_enqueue_scripts', 'fictioneer_style_queue' );
function fictioneer_output_customize_css() {
// Setup
$file_path = FICTIONEER_CACHE_DIR . '/customize.css';
$file_path = fictioneer_get_theme_cache_dir( 'output_customize_css' ) . '/customize.css';
// Create file if it does not exist
if ( ! file_exists( $file_path ) ) {
@ -988,7 +1012,7 @@ if ( ! is_customize_preview() ) {
function fictioneer_output_customize_preview_css() {
// Setup
$file_path = FICTIONEER_CACHE_DIR . '/customize-preview.css';
$file_path = fictioneer_get_theme_cache_dir( 'output_customize_preview_css' ) . '/customize-preview.css';
// Create file if it does not exist
fictioneer_build_customize_css( 'preview' );
@ -1091,7 +1115,7 @@ if ( ! function_exists( 'fictioneer_add_font_awesome' ) ) {
function fictioneer_build_dynamic_scripts() {
// --- Setup -----------------------------------------------------------------
$file_path = FICTIONEER_CACHE_DIR . '/dynamic-scripts.js';
$file_path = fictioneer_get_theme_cache_dir( 'build_dynamic_scripts' ) . '/dynamic-scripts.js';
$last_version = get_transient( 'fictioneer_dynamic_scripts_version' );
$scripts = '';
@ -1100,11 +1124,6 @@ function fictioneer_build_dynamic_scripts() {
return;
}
// Make sure directory exists
if ( ! is_dir( FICTIONEER_CACHE_DIR ) ) {
mkdir( FICTIONEER_CACHE_DIR, 0755, true );
}
// --- AJAX Settings ---------------------------------------------------------
$scripts .= "var fictioneer_ajax = " . json_encode( array(
@ -1543,7 +1562,7 @@ if ( ! function_exists( 'fictioneer_output_head_fonts' ) ) {
fictioneer_output_critical_fonts();
// Setup
$bundled_fonts = FICTIONEER_CACHE_DIR . '/bundled-fonts.css';
$bundled_fonts = fictioneer_get_theme_cache_dir( 'output_head_fonts' ) . '/bundled-fonts.css';
$last_built_timestamp = get_option( 'fictioneer_bundled_fonts_timestamp', '123456789' );
$cache_bust = "?timestamp={$last_built_timestamp}";
$loading_pattern = fictioneer_get_async_css_loading_pattern();

View File

@ -470,7 +470,7 @@ function fictioneer_purge_theme_caches() {
fictioneer_delete_transients_like( 'fictioneer_' );
// Delete cached files
$files = glob( trailingslashit( FICTIONEER_CACHE_DIR ) . '*' );
$files = glob( trailingslashit( fictioneer_get_theme_cache_dir( 'purge_theme_caches' ) ) . '*' );
foreach ( $files as $file ) {
if ( is_file( $file ) ) {