Extend chapter groups and add fictioneer_filter_chapter_group filter

This commit is contained in:
Tetrakern 2024-10-05 20:55:25 +02:00
parent 7aba027729
commit 997ff71b78
2 changed files with 55 additions and 9 deletions

View File

@ -211,6 +211,37 @@ Filters the default chapter formatting settings. The passed array is empty becau
---
### `apply_filters( 'fictioneer_filter_chapter_group', $group, $group_index, $story_id )`
Filters the array of grouped chapter data in the `fictioneer_story_chapters()` function before it is rendered in the chapter list on story pages.
**$group:**
* $group (string) Name of the group.
* $toggle_icon (string) CSS classes for the group toggle icon (Font Awesome).
* $data (array) Array of chapter post data in render order.
* $id (int) Post ID
* $story_id (int) Story ID.
* $status (string) Status of the post.
* $link (string) Permalink of the post. Can be empty.
* $timestamp (string) Publish date of the post (Unix).
* $password (boolean) Whether the post has a password.
* $list_date (string) Publish date formatted for lists.
* $grid_date (string) Publish date formatted for grids.
* $icon (string) CSS classes for the chapter icon (Font Awesome).
* $text_icon (string) Text icon (if any).
* $prefix (string) Prefix for the title (if any).
* $title (string) Title of the post (never empty).
* $list_title (string) Alternative title of the post (used on small cards).
* $words (int) Word count of the post.
* $warning (string) Warning note of the post (if any).
* $count (int) Number of items in the group to be rendered.
* $classes (array) Array of CSS classes to be added to the group button.
**Parameters:**
* $group_index (int) The current group index in render order, starting at 1.
* $story_id (int) Story ID.
---
### `apply_filters( 'fictioneer_filter_chapter_icon', $icon_html, $chapter_id, $story_id )`
Filters the HTML of the chapter icon before it is rendered. The display hierarchy is password icon > scheduled icon > text icon > chapter icon.

View File

@ -555,7 +555,13 @@ function fictioneer_story_chapters( $args ) {
if ( ! array_key_exists( $group_key, $chapter_groups ) ) {
$chapter_groups[ $group_key ] = array(
'group' => $group,
'data' => []
'toggle_icon' => 'fa-solid fa-chevron-down',
'data' => [],
'count' => 0,
'classes' => array(
'_group-' . sanitize_title( $group ),
"_story-{$story_id}"
)
);
}
@ -564,7 +570,7 @@ function fictioneer_story_chapters( $args ) {
'story_id' => $story_id,
'status' => $post->post_status,
'link' => in_array( $post->post_status, $allowed_permalinks ) ? get_permalink( $post->ID ) : '',
'timestamp' => get_the_time( 'c' ),
'timestamp' => get_the_time( 'U' ),
'password' => ! empty( $post->post_password ),
'list_date' => get_the_date( '', $post ),
'grid_date' => get_the_time( get_option( 'fictioneer_subitem_date_format', "M j, 'y" ) ?: "M j, 'y" ),
@ -576,6 +582,8 @@ function fictioneer_story_chapters( $args ) {
'words' => fictioneer_get_word_count( $chapter_id ),
'warning' => get_post_meta( $chapter_id, 'fictioneer_chapter_warning', true )
);
$chapter_groups[ $group_key ]['count'] += 1;
}
// Reset postdata
@ -593,25 +601,32 @@ function fictioneer_story_chapters( $args ) {
$has_groups = count( $chapter_groups ) > 1 && get_option( 'fictioneer_enable_chapter_groups' );
foreach ( $chapter_groups as $group ) {
$group_index++;
$group = apply_filters( 'fictioneer_filter_chapter_group', $group, $group_index, $story_id );
$index = 0;
$group_chapter_count = count( $group['data'] );
$reverse_order = 99999;
$group_item_count = count( $group['data'] );
$chapter_folding = ! $disable_folding && ! get_option( 'fictioneer_disable_chapter_collapsing' );
$chapter_folding = $chapter_folding && count( $group['data'] ) >= FICTIONEER_CHAPTER_FOLDING_THRESHOLD * 2 + 3;
$aria_label = __( 'Toggle chapter group: %s', 'fictioneer' );
$group_index++;
// Start HTML ---> ?>
<div class="chapter-group <?php echo implode( ' ', $group_classes ); ?>" data-folded="true">
<?php if ( $has_groups ) : ?>
<button
class="chapter-group__name"
class="chapter-group__name <?php echo implode( ' ', $group['classes'] ?? [] ); ?>"
aria-label="<?php echo esc_attr( sprintf( $aria_label, $group['group'] ) ); ?>"
data-item-count="<?php echo esc_attr( $group_item_count ); ?>"
data-group-index="<?php echo esc_attr( $group_index ); ?>"
tabindex="0"
>
<i class="fa-solid fa-chevron-down chapter-group__heading-icon"></i>
<span><?php echo $group['group']; ?></span>
<i class="<?php echo $group['toggle_icon']; ?> chapter-group__heading-icon"></i>
<span data-item-count="<?php echo esc_attr( $group_item_count ); ?>"><?php
echo $group['group'];
?></span>
</button>
<?php endif; ?>
@ -623,7 +638,7 @@ function fictioneer_story_chapters( $args ) {
// Must account for extra toggle row and start at 1
$is_folded = $chapter_folding && $index > FICTIONEER_CHAPTER_FOLDING_THRESHOLD &&
$index < ( $group_chapter_count + 2 - FICTIONEER_CHAPTER_FOLDING_THRESHOLD );
$index < ( $group_item_count + 2 - FICTIONEER_CHAPTER_FOLDING_THRESHOLD );
if ( $is_folded ) {
$extra_classes .= ' _foldable';
@ -640,7 +655,7 @@ function fictioneer_story_chapters( $args ) {
<?php
printf(
__( 'Show %s more', 'fictioneer' ),
$group_chapter_count - FICTIONEER_CHAPTER_FOLDING_THRESHOLD * 2
$group_item_count - FICTIONEER_CHAPTER_FOLDING_THRESHOLD * 2
);
?>
</button>