Add and use indexed chapter IDs to story data

This is in case the query is filtered and suddenly contains non-published chapters that would cause 404 if accessed.
This commit is contained in:
Tetrakern 2024-04-23 02:47:23 +02:00
parent 1a2b736e18
commit 8ea32feec2
4 changed files with 38 additions and 23 deletions

View File

@ -1123,12 +1123,14 @@ if ( ! function_exists( 'fictioneer_get_chapter_micro_menu' ) ) {
*
* @since 5.0.0
* @since 5.9.4 - Removed output buffer.
* @since 5.14.0 - Added indexed chapter IDs.
*
* @param WP_Post|null $args['story_post'] Optional. Post object of the story.
* @param int $args['chapter_id'] The chapter ID.
* @param array $args['chapter_ids'] IDs of visible chapters in the same story or empty array.
* @param int|boolean $args['prev_index'] Index of previous chapter or false if outside bounds.
* @param int|boolean $args['next_index'] Index of next chapter or false if outside bounds.
* @param WP_Post|null $args['story_post'] Optional. Post object of the story.
* @param int $args['chapter_id'] The chapter ID.
* @param array $args['chapter_ids'] IDs of visible chapters in the same story or empty array.
* @param array $args['indexed_chapter_ids'] IDs of accessible chapters in the same story or empty array.
* @param int|boolean $args['prev_index'] Index of previous chapter or false if outside bounds.
* @param int|boolean $args['next_index'] Index of next chapter or false if outside bounds.
*
* @return string The chapter micro menu HTML.
*/
@ -1184,8 +1186,8 @@ if ( ! function_exists( 'fictioneer_get_chapter_micro_menu' ) ) {
if ($args['prev_index'] !== false) {
$micro_menu['previous'] = sprintf(
'<a href="%s" title="%s" class="micro-menu__item micro-menu__previous previous" tabindex="-1"><i class="fa-solid fa-caret-left"></i></a>',
get_permalink( $args['chapter_ids'][ $args['prev_index'] ] ),
esc_attr( get_the_title( $args['chapter_ids'][ $args['prev_index'] ] ) )
get_permalink( $args['indexed_chapter_ids'][ $args['prev_index'] ] ),
esc_attr( get_the_title( $args['indexed_chapter_ids'][ $args['prev_index'] ] ) )
);
}
@ -1199,8 +1201,8 @@ if ( ! function_exists( 'fictioneer_get_chapter_micro_menu' ) ) {
if ($args['next_index']) {
$micro_menu['next'] = sprintf(
'<a href="%s" title="%s" class="micro-menu__item micro-menu__next next" tabindex="-1"><i class="fa-solid fa-caret-right"></i></a>',
get_permalink( $args['chapter_ids'][ $args['next_index'] ] ),
esc_attr( get_the_title( $args['chapter_ids'][ $args['next_index'] ] ) )
get_permalink( $args['indexed_chapter_ids'][ $args['next_index'] ] ),
esc_attr( get_the_title( $args['indexed_chapter_ids'][ $args['next_index'] ] ) )
);
}

View File

@ -330,6 +330,7 @@ if ( ! function_exists( 'fictioneer_get_story_data' ) ) {
$word_count = 0;
$comment_count = 0;
$visible_chapter_ids = [];
$indexed_chapter_ids = [];
// Assign correct icon
if ( $status != 'Ongoing' ) {
@ -362,6 +363,11 @@ if ( ! function_exists( 'fictioneer_get_story_data' ) ) {
// ... but they are still listed!
$visible_chapter_ids[] = $chapter->ID;
// Indexed chapters (accounts for custom filers)
if ( $chapter->post_status === 'publish' ) {
$indexed_chapter_ids[] = $chapter->ID;
}
}
// Count ALL comments
@ -390,6 +396,7 @@ if ( ! function_exists( 'fictioneer_get_story_data' ) ) {
'rating' => get_post_meta( $story_id, 'fictioneer_story_rating', true ),
'rating_letter' => get_post_meta( $story_id, 'fictioneer_story_rating', true )[0],
'chapter_ids' => $visible_chapter_ids,
'indexed_chapter_ids' => $indexed_chapter_ids,
'last_modified' => get_the_modified_time( 'U', $story_id ),
'comment_count' => $comment_count,
'comment_count_timestamp' => time(),

View File

@ -240,11 +240,13 @@ add_action( 'fictioneer_chapter_actions_top_left', 'fictioneer_chapter_resize_bu
* Outputs the HTML for the chapter navigation buttons
*
* @since 5.0.0
* @since 5.14.0 - Added indexed chapter IDs.
*
* @param array $args['chapter_ids'] IDs of visible chapters in the same story or empty array.
* @param int|boolean $args['prev_index'] Index of previous chapter or false if outside bounds.
* @param int|boolean $args['next_index'] Index of next chapter or false if outside bounds.
* @param string $location Either 'top' or 'bottom'.
* @param array $args['chapter_ids'] IDs of visible chapters in the same story or empty array.
* @param array $args['indexed_chapter_ids'] IDs of accessible chapters in the same story or empty array.
* @param int|boolean $args['prev_index'] Index of previous chapter or false if outside bounds.
* @param int|boolean $args['next_index'] Index of next chapter or false if outside bounds.
* @param string $location Either 'top' or 'bottom'.
*/
function fictioneer_chapter_nav_buttons( $args, $location ) {
@ -257,7 +259,7 @@ function fictioneer_chapter_nav_buttons( $args, $location ) {
// Start HTML ---> ?>
<?php if ( $args['prev_index'] !== false ) : ?>
<a href="<?php echo get_permalink( $args['chapter_ids'][ $args['prev_index'] ] ); ?>" title="<?php echo get_the_title( $args['chapter_ids'][ $args['prev_index'] ] ); ?>" class="button _secondary _navigation _prev"><?php echo fcntr( 'previous' ); ?></a>
<a href="<?php echo get_permalink( $args['indexed_chapter_ids'][ $args['prev_index'] ] ); ?>" title="<?php echo get_the_title( $args['indexed_chapter_ids'][ $args['prev_index'] ] ); ?>" class="button _secondary _navigation _prev"><?php echo fcntr( 'previous' ); ?></a>
<?php endif; ?>
<?php if ( $location === 'top' ) : ?>
<a href="#bottom" data-block="center" aria-label="<?php _e( 'Scroll to bottom of the chapter', 'fictioneer' ); ?>" name="top" class="anchor button _secondary tooltipped" data-tooltip="<?php esc_attr_e( 'Scroll to bottom', 'fictioneer' ); ?>"><i class="fa-solid fa-caret-down"></i></a>
@ -265,7 +267,7 @@ function fictioneer_chapter_nav_buttons( $args, $location ) {
<a href="#top" data-block="center" aria-label="<?php _e( 'Scroll to top of the chapter', 'fictioneer' ); ?>" name="bottom" class="anchor button _secondary tooltipped" data-tooltip="<?php esc_attr_e( 'Scroll to top', 'fictioneer' ); ?>"><i class="fa-solid fa-caret-up"></i></a>
<?php endif; ?>
<?php if ( $args['next_index'] ) : ?>
<a href="<?php echo get_permalink( $args['chapter_ids'][ $args['next_index'] ] ); ?>" title="<?php echo get_the_title( $args['chapter_ids'][ $args['next_index'] ] ); ?>" class="button _secondary _navigation _next"><?php echo fcntr( 'next' ); ?></a>
<a href="<?php echo get_permalink( $args['indexed_chapter_ids'][ $args['next_index'] ] ); ?>" title="<?php echo get_the_title( $args['indexed_chapter_ids'][ $args['next_index'] ] ); ?>" class="button _secondary _navigation _next"><?php echo fcntr( 'next' ); ?></a>
<?php endif; ?>
<?php // <--- End HTML
}
@ -593,11 +595,12 @@ add_action( 'fictioneer_chapter_after_content', 'fictioneer_chapter_support_link
*
* @since 5.0.0
*
* @param WP_Post|null $args['story_post'] Optional. Post object of the story.
* @param int $args['chapter_id'] The chapter ID.
* @param array $args['chapter_ids'] IDs of visible chapters in the same story or empty array.
* @param int|boolean $args['prev_index'] Index of previous chapter or false if outside bounds.
* @param int|boolean $args['next_index'] Index of next chapter or false if outside bounds.
* @param WP_Post|null $args['story_post'] Optional. Post object of the story.
* @param int $args['chapter_id'] The chapter ID.
* @param array $args['chapter_ids'] IDs of visible chapters in the same story or empty array.
* @param array $args['indexed_chapter_ids'] IDs of accessible chapters in the same story or empty array.
* @param int|boolean $args['prev_index'] Index of previous chapter or false if outside bounds.
* @param int|boolean $args['next_index'] Index of next chapter or false if outside bounds.
*/
function fictioneer_chapter_micro_menu( $args ) {

View File

@ -47,6 +47,7 @@ get_header( null, $header_args );
<?php
// Setup
$chapter_ids = [];
$indexed_chapters = [];
$password_class = ! empty( $post->post_password ) ? 'password' : '';
$title = fictioneer_get_safe_title( $post->ID, 'single-chapter' );
$age_rating = get_post_meta( $post->ID, 'fictioneer_chapter_rating', true );
@ -64,6 +65,7 @@ get_header( null, $header_args );
if ( $story_post ) {
$story_data = fictioneer_get_story_data( $story_id, false ); // Does not refresh comment count!
$chapter_ids = $story_data['chapter_ids'];
$indexed_chapters = $story_data['indexed_chapter_ids'] ?? $chapter_ids;
if ( empty( $age_rating ) ) {
$age_rating = $story_data['rating'];
@ -71,7 +73,7 @@ get_header( null, $header_args );
}
// Chapter navigation
$current_index = array_search( $post->ID, $chapter_ids );
$current_index = array_search( $post->ID, $indexed_chapters );
$prev_index = $current_index - 1;
$next_index = $current_index + 1;
@ -84,13 +86,14 @@ get_header( null, $header_args );
'chapter_title' => $title,
'chapter_password' => $post->post_password,
'chapter_ids' => $chapter_ids,
'indexed_chapter_ids' => $indexed_chapters,
'current_index' => $current_index,
'prev_index' => $prev_index >= 0 ? $prev_index : false,
'next_index' => isset( $chapter_ids[ $next_index ] ) ? $next_index : false
'next_index' => isset( $indexed_chapters[ $next_index ] ) ? $next_index : false
);
?>
<?php if ( $story_post && $chapter_ids ): ?>
<?php if ( $story_post && $indexed_chapters ): ?>
<div id="story-chapter-list" class="hidden" data-story-id="<?php echo $story_id; ?>">
<ul data-current-id="<?php echo $post->ID; ?>"><?php
echo fictioneer_get_chapter_list_items( $story_id, $story_data, $current_index );