From 38dc6da10c6fc05d9bdb4091948b59d9354ed187 Mon Sep 17 00:00:00 2001 From: Tetrakern <26898880+Tetrakern@users.noreply.github.com> Date: Mon, 2 Sep 2024 02:05:05 +0200 Subject: [PATCH] Add Transient cache to index page templates --- singular-authors-advanced.php | 67 +++++++++++++--------- singular-authors.php | 63 +++++++++++++-------- singular-index-advanced.php | 103 +++++++++++++++++++--------------- singular-index.php | 95 ++++++++++++++++++------------- 4 files changed, 194 insertions(+), 134 deletions(-) diff --git a/singular-authors-advanced.php b/singular-authors-advanced.php index 460d2c09..aa1a006c 100644 --- a/singular-authors-advanced.php +++ b/singular-authors-advanced.php @@ -10,43 +10,58 @@ // Setup $post_id = get_the_ID(); +$sorted_authors = []; +$cached = fictioneer_caching_active( 'author_index_advanced' ); // Header get_header(); -// Query authors with published posts -$authors = fictioneer_get_publishing_authors( array( 'fields' => array( 'ID', 'display_name', 'user_nicename' ) ) ); +// Transient cache? +$transient = $cached ? 0 : get_transient( 'fictioneer_author_index_advanced_' . $post_id ); -// Sort authors -$sorted_authors = []; +if ( $transient ) { + $sorted_authors = $transient; +} -if ( ! empty( $authors ) ) { - // Loop through authors... - foreach ( $authors as $author ) { - // Relevant data - $first_char = mb_strtolower( mb_substr( $author->display_name, 0, 1, 'UTF-8' ), 'UTF-8' ); +// ... if not cached +if ( empty( $sorted_authors ) ) { + // Query authors with published posts + $authors = fictioneer_get_publishing_authors( array( 'fields' => array( 'ID', 'display_name', 'user_nicename' ) ) ); - // Normalize for numbers and other non-alphabetical characters - if ( ! preg_match( '/\p{L}/u', $first_char ) ) { - $first_char = '#'; // Group under '#' + // Sort authors + if ( ! empty( $authors ) ) { + // Loop through authors... + foreach ( $authors as $author ) { + // Relevant data + $first_char = mb_strtolower( mb_substr( $author->display_name, 0, 1, 'UTF-8' ), 'UTF-8' ); + + // Normalize for numbers and other non-alphabetical characters + if ( ! preg_match( '/\p{L}/u', $first_char ) ) { + $first_char = '#'; // Group under '#' + } + + // Add index if necessary + if ( ! isset( $sorted_authors[ $first_char ] ) ) { + $sorted_authors[ $first_char ] = []; + } + + $sorted_authors[ $first_char ][] = array( + 'id' => $author->ID, + 'name' => $author->display_name, + 'link' => get_author_posts_url( $author->ID, $author->user_nicename ), + 'story_count' => count_user_posts( $author->ID, 'fcn_story', true ), + 'chapter_count' => count_user_posts( $author->ID, 'fcn_chapter', true ) + ); } - // Add index if necessary - if ( ! isset( $sorted_authors[ $first_char ] ) ) { - $sorted_authors[ $first_char ] = []; - } + // Sort by index + ksort( $sorted_authors ); - $sorted_authors[ $first_char ][] = array( - 'id' => $author->ID, - 'name' => $author->display_name, - 'link' => get_author_posts_url( $author->ID, $author->user_nicename ), - 'story_count' => count_user_posts( $author->ID, 'fcn_story', true ), - 'chapter_count' => count_user_posts( $author->ID, 'fcn_chapter', true ) - ); + // Cache as Transient + if ( ! $cached ) { + set_transient( 'fictioneer_author_index_advanced_' . $post_id, $sorted_authors, 12 * HOUR_IN_SECONDS ); + } } - - // Sort by index - ksort( $sorted_authors ); } // Last key diff --git a/singular-authors.php b/singular-authors.php index ba34e758..23eaa6e8 100644 --- a/singular-authors.php +++ b/singular-authors.php @@ -10,41 +10,56 @@ // Setup $post_id = get_the_ID(); +$sorted_authors = []; +$cached = fictioneer_caching_active( 'author_index' ); // Header get_header(); -// Query authors with published posts -$authors = fictioneer_get_publishing_authors( array( 'fields' => array( 'ID', 'display_name', 'user_nicename' ) ) ); +// Transient cache? +$transient = $cached ? 0 : get_transient( 'fictioneer_author_index_' . $post_id ); -// Sort authors -$sorted_authors = []; +if ( $transient ) { + $sorted_authors = $transient; +} -if ( ! empty( $authors ) ) { - // Loop through authors... - foreach ( $authors as $author ) { - // Relevant data - $first_char = mb_strtolower( mb_substr( $author->display_name, 0, 1, 'UTF-8' ), 'UTF-8' ); +// ... if not cached +if ( empty( $sorted_authors ) ) { + // Query authors with published posts + $authors = fictioneer_get_publishing_authors( array( 'fields' => array( 'ID', 'display_name', 'user_nicename' ) ) ); - // Normalize for numbers and other non-alphabetical characters - if ( ! preg_match( '/\p{L}/u', $first_char ) ) { - $first_char = '#'; // Group under '#' + // Sort authors + if ( ! empty( $authors ) ) { + // Loop through authors... + foreach ( $authors as $author ) { + // Relevant data + $first_char = mb_strtolower( mb_substr( $author->display_name, 0, 1, 'UTF-8' ), 'UTF-8' ); + + // Normalize for numbers and other non-alphabetical characters + if ( ! preg_match( '/\p{L}/u', $first_char ) ) { + $first_char = '#'; // Group under '#' + } + + // Add index if necessary + if ( ! isset( $sorted_authors[ $first_char ] ) ) { + $sorted_authors[ $first_char ] = []; + } + + $sorted_authors[ $first_char ][] = array( + 'id' => $author->ID, + 'name' => $author->display_name, + 'link' => get_author_posts_url( $author->ID, $author->user_nicename ) + ); } - // Add index if necessary - if ( ! isset( $sorted_authors[ $first_char ] ) ) { - $sorted_authors[ $first_char ] = []; - } + // Sort by index + ksort( $sorted_authors ); - $sorted_authors[ $first_char ][] = array( - 'id' => $author->ID, - 'name' => $author->display_name, - 'link' => get_author_posts_url( $author->ID, $author->user_nicename ) - ); + // Cache as Transient + if ( ! $cached ) { + set_transient( 'fictioneer_author_index_' . $post_id, $sorted_authors, 12 * HOUR_IN_SECONDS ); + } } - - // Sort by index - ksort( $sorted_authors ); } // Last key diff --git a/singular-index-advanced.php b/singular-index-advanced.php index 4b819ca8..e16fbafa 100644 --- a/singular-index-advanced.php +++ b/singular-index-advanced.php @@ -10,63 +10,78 @@ // Setup $post_id = get_the_ID(); +$sorted_stories = []; +$cached = fictioneer_caching_active( 'story_index_advanced' ); // Header get_header(); -// Query all stories -$args = array( - 'post_type' => 'fcn_story', - 'post_status' => ['publish'], - 'posts_per_page' => -1, - 'order' => 'ASC', - 'orderby' => 'name', - 'update_post_term_cache' => false, // Improve performance - 'no_found_rows' => true // Improve performance -); +// Transient cache? +$transient = $cached ? 0 : get_transient( 'fictioneer_story_index_advanced_' . $post_id ); -$stories = new WP_Query( $args ); +if ( $transient ) { + $sorted_stories = $transient; +} -// Sort stories -$sorted_stories = []; +// ... if not cached +if ( empty( $sorted_stories ) ) { + // Query all stories + $args = array( + 'post_type' => 'fcn_story', + 'post_status' => ['publish'], + 'posts_per_page' => -1, + 'order' => 'ASC', + 'orderby' => 'name', + 'update_post_term_cache' => false, // Improve performance + 'no_found_rows' => true // Improve performance + ); -if ( $stories->have_posts() ) { - // Loop through posts... - foreach ( $stories->posts as $story ) { - $story_id = $story->ID; + $stories = new WP_Query( $args ); - // Skip hidden - if ( get_post_meta( $story_id, 'fictioneer_story_hidden', true ) ) { - continue; + // Sort stories + if ( $stories->have_posts() ) { + // Loop through posts... + foreach ( $stories->posts as $story ) { + $story_id = $story->ID; + + // Skip hidden + if ( get_post_meta( $story_id, 'fictioneer_story_hidden', true ) ) { + continue; + } + + // Relevant data + $title = trim( fictioneer_get_safe_title( $story_id, 'story_index' ) ); + $first_char = mb_strtolower( mb_substr( $title, 0, 1, 'UTF-8' ), 'UTF-8' ); + + // Normalize for numbers and other non-alphabetical characters + if ( ! preg_match( '/\p{L}/u', $first_char ) ) { + $first_char = '#'; // Group under '#' + } + + // Add index if necessary + if ( ! isset( $sorted_stories[ $first_char ] ) ) { + $sorted_stories[ $first_char ] = []; + } + + $sorted_stories[ $first_char ][] = array( + 'id' => $story_id, + 'title' => $title, + 'link' => get_post_meta( $story_id, 'fictioneer_story_redirect_link', true ) ?: get_permalink( $story_id ), + 'date' => get_the_date( $story_id ), + 'total_words' => fictioneer_get_story_word_count( $story_id ), + 'rating' => get_post_meta( $story_id, 'fictioneer_story_rating', true ), + 'status' => get_post_meta( $story_id, 'fictioneer_story_status', true ) + ); } - // Relevant data - $title = trim( fictioneer_get_safe_title( $story_id, 'story_index' ) ); - $first_char = mb_strtolower( mb_substr( $title, 0, 1, 'UTF-8' ), 'UTF-8' ); + // Sort by index + ksort( $sorted_stories ); - // Normalize for numbers and other non-alphabetical characters - if ( ! preg_match( '/\p{L}/u', $first_char ) ) { - $first_char = '#'; // Group under '#' + // Cache as Transient + if ( ! $cached ) { + set_transient( 'fictioneer_story_index_advanced_' . $post_id, $sorted_stories, 12 * HOUR_IN_SECONDS ); } - - // Add index if necessary - if ( ! isset( $sorted_stories[ $first_char ] ) ) { - $sorted_stories[ $first_char ] = []; - } - - $sorted_stories[ $first_char ][] = array( - 'id' => $story_id, - 'title' => $title, - 'link' => get_post_meta( $story_id, 'fictioneer_story_redirect_link', true ) ?: get_permalink( $story_id ), - 'date' => get_the_date( $story_id ), - 'total_words' => fictioneer_get_story_word_count( $story_id ), - 'rating' => get_post_meta( $story_id, 'fictioneer_story_rating', true ), - 'status' => get_post_meta( $story_id, 'fictioneer_story_status', true ) - ); } - - // Sort by index - ksort( $sorted_stories ); } // Last key diff --git a/singular-index.php b/singular-index.php index f88557aa..d09f53e0 100644 --- a/singular-index.php +++ b/singular-index.php @@ -10,59 +10,74 @@ // Setup $post_id = get_the_ID(); +$sorted_stories = []; +$cached = fictioneer_caching_active( 'story_index' ); // Header get_header(); -// Query all stories -$args = array( - 'post_type' => 'fcn_story', - 'post_status' => ['publish'], - 'posts_per_page' => -1, - 'order' => 'ASC', - 'orderby' => 'name', - 'update_post_term_cache' => false, // Improve performance - 'no_found_rows' => true // Improve performance -); +// Transient cache? +$transient = $cached ? 0 : get_transient( 'fictioneer_story_index_' . $post_id ); -$stories = new WP_Query( $args ); +if ( $transient ) { + $sorted_stories = $transient; +} -// Sort stories -$sorted_stories = []; +// ... if not cached +if ( empty( $sorted_stories ) ) { + // Query all stories + $args = array( + 'post_type' => 'fcn_story', + 'post_status' => ['publish'], + 'posts_per_page' => -1, + 'order' => 'ASC', + 'orderby' => 'name', + 'update_post_term_cache' => false, // Improve performance + 'no_found_rows' => true // Improve performance + ); -if ( $stories->have_posts() ) { - // Loop through posts... - foreach ( $stories->posts as $story ) { - $story_id = $story->ID; + $stories = new WP_Query( $args ); - // Skip hidden - if ( get_post_meta( $story_id, 'fictioneer_story_hidden', true ) ) { - continue; + // Sort stories + if ( $stories->have_posts() ) { + // Loop through posts... + foreach ( $stories->posts as $story ) { + $story_id = $story->ID; + + // Skip hidden + if ( get_post_meta( $story_id, 'fictioneer_story_hidden', true ) ) { + continue; + } + + // Relevant data + $title = trim( fictioneer_get_safe_title( $story_id, 'story_index' ) ); + $first_char = mb_strtolower( mb_substr( $title, 0, 1, 'UTF-8' ), 'UTF-8' ); + + // Normalize for numbers and other non-alphabetical characters + if ( ! preg_match( '/\p{L}/u', $first_char ) ) { + $first_char = '#'; // Group under '#' + } + + // Add index if necessary + if ( ! isset( $sorted_stories[ $first_char ] ) ) { + $sorted_stories[ $first_char ] = []; + } + + $sorted_stories[ $first_char ][] = array( + 'id' => $story_id, + 'title' => $title, + 'link' => get_post_meta( $story_id, 'fictioneer_story_redirect_link', true ) ?: get_permalink( $story_id ) + ); } - // Relevant data - $title = trim( fictioneer_get_safe_title( $story_id, 'story_index' ) ); - $first_char = mb_strtolower( mb_substr( $title, 0, 1, 'UTF-8' ), 'UTF-8' ); + // Sort by index + ksort( $sorted_stories ); - // Normalize for numbers and other non-alphabetical characters - if ( ! preg_match( '/\p{L}/u', $first_char ) ) { - $first_char = '#'; // Group under '#' + // Cache as Transient + if ( ! $cached ) { + set_transient( 'fictioneer_story_index_' . $post_id, $sorted_stories, 12 * HOUR_IN_SECONDS ); } - - // Add index if necessary - if ( ! isset( $sorted_stories[ $first_char ] ) ) { - $sorted_stories[ $first_char ] = []; - } - - $sorted_stories[ $first_char ][] = array( - 'id' => $story_id, - 'title' => $title, - 'link' => get_post_meta( $story_id, 'fictioneer_story_redirect_link', true ) ?: get_permalink( $story_id ) - ); } - - // Sort by index - ksort( $sorted_stories ); } // Last key