Add meta cache for chapter index HTML

This was a potentially expensive query executed on each chapter page.
This commit is contained in:
Tetrakern 2024-01-27 20:43:20 +01:00
parent fae2dda4db
commit 3740947d8f
4 changed files with 49 additions and 13 deletions

View File

@ -978,11 +978,32 @@ if ( ! function_exists( 'fictioneer_get_chapter_list_items' ) ) {
* Returns the HTML for chapter list items with icon and link
*
* @since 5.0.0
* @since 5.9.3 - Added meta field caching.
*
* @return string List of chapter.
* @param int $story_id ID of the story.
* @param array $data Prepared data of the story.
* @param int $current_index Index in chapter ID array.
*
* @return string HTML list of chapters.
*/
function fictioneer_get_chapter_list_items( $story_id, $data, $current_index ) {
// Meta-cache?
if ( ! fictioneer_caching_active() ) {
$last_story_update = get_post_modified_time( 'U', true, $story_id );
$cache = get_post_meta( $story_id, 'fictioneer_story_chapter_index_html', true );
if ( $cache && is_array( $cache ) && array_key_exists( 'html', $cache ) ) {
// ... still up-to-date and valid?
if (
( $cache['timestamp'] ?? 0 ) == $last_story_update &&
( $cache['valid_until'] ?? 0 ) > time()
) {
return $cache['html'];
}
}
}
// Setup
$chapters = fictioneer_get_story_chapter_posts( $story_id );
$hide_icons = get_post_meta( $story_id, 'fictioneer_story_hide_chapter_icons', true ) || get_option( 'fictioneer_hide_chapter_icons' );
@ -1009,17 +1030,12 @@ if ( ! function_exists( 'fictioneer_get_chapter_list_items' ) ) {
);
}
// CSS classes
if ( $current_index == array_search( $chapter->ID, $data['chapter_ids'] ) ) {
$classes[] = 'current-chapter';
}
if ( ! empty( $chapter->post_password ) ) {
$classes[] = 'has-password';
}
// Start HTML ---> ?>
<li class="<?php echo implode( ' ', $classes ); ?>">
<li class="<?php echo implode( ' ', $classes ); ?>" data-id="<?php echo $chapter->ID; ?>">
<a href="<?php echo "{$relative_path}?p={$chapter->ID}"; ?>">
<?php if ( empty( $text_icon ) && ! $hide_icons ) : ?>
<i class="<?php echo fictioneer_get_icon_field( 'fictioneer_chapter_icon', $chapter->ID ); ?>"></i>
@ -1032,7 +1048,20 @@ if ( ! function_exists( 'fictioneer_get_chapter_list_items' ) ) {
<?php // <--- End HTML
}
return ob_get_clean();
// Capture
$html = ob_get_clean();
// Meta-cache for next time
if ( ! fictioneer_caching_active() ) {
update_post_meta(
$story_id,
'fictioneer_story_chapter_index_html',
array( 'html' => $html, 'timestamp' => $last_story_update, 'valid_until' => time() + HOUR_IN_SECONDS )
);
}
// Return
return $html;
}
}

File diff suppressed because one or more lines are too long

View File

@ -87,7 +87,9 @@ get_header( null, $header_args );
<?php if ( $story_post && $chapter_ids ): ?>
<div id="story-chapter-list" class="hidden" data-story-id="<?php echo $story_id; ?>">
<ul><?php echo fictioneer_get_chapter_list_items( $story_id, $story_data, $current_index ); ?></ul>
<ul data-current-id="<?php echo $post->ID; ?>"><?php
echo fictioneer_get_chapter_list_items( $story_id, $story_data, $current_index );
?></ul>
</div>
<?php endif; ?>

View File

@ -16,14 +16,19 @@ var /** @type {Object} */ fcn_cssVars = getComputedStyle(document.documentElemen
/** @type {Boolean} */ fcn_isLoggedIn = fcn_theBody.classList.contains('logged-in'),
/** @type {Number} */ fcn_viewportWidth,
/** @type {Boolean} */ fcn_mediaMinTablet,
/** @type {HTMLElement} */ fcn_chapterList = _$('#story-chapter-list > ul')?.cloneNode(true);
/** @type {HTMLElement} */ fcn_chapterList = _$('#story-chapter-list > ul');
// Translation functions
const { __, _x, _n, sprintf } = wp.i18n;
// Remove chapter list from DOM to improve performance (add when needed)
// =============================================================================
// CHAPTER INDEX
// =============================================================================
if (fcn_chapterList) {
_$('#story-chapter-list > ul').remove();
fcn_chapterList = fcn_chapterList.cloneNode(true);
_$$$('story-chapter-list').remove();
fcn_chapterList.querySelector(`[data-id="${fcn_chapterList.dataset.currentId}"]`)?.classList.add('current-chapter');
}
// =============================================================================