Add Transient cache to index page templates

This commit is contained in:
Tetrakern 2024-09-02 02:05:05 +02:00
parent bbc7695e2e
commit 38dc6da10c
4 changed files with 194 additions and 134 deletions

View File

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

View File

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

View File

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

View File

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