Add fictioneer_filter_simple_chapter_list_item filter

Replaces the fictioneer_filter_chapter_list_item filter.
This commit is contained in:
Tetrakern 2024-10-08 13:31:23 +02:00
parent 94446dcf72
commit 6631184aed
3 changed files with 39 additions and 5 deletions

View File

@ -171,12 +171,12 @@ add_filter( 'fictioneer_filter_safe_title', 'child_modify_chapter_list_title', 1
Or change them completely, if you want even depending on the chapter or associated story. Related to [this issue](https://github.com/Tetrakern/fictioneer/issues/31). Using a filter, you can rebuild the list item HTML to your liking. In the following example, the chapter prefix has been prepended (if there is one).
**References**
* Filter: [fictioneer_filter_chapter_list_item](https://github.com/Tetrakern/fictioneer/blob/main/FILTERS.md#apply_filters-fictioneer_filter_chapter_list_item-item-post-args-)
* Filter: [fictioneer_filter_simple_chapter_list_item](https://github.com/Tetrakern/fictioneer/blob/main/FILTERS.md#apply_filters-fictioneer_filter_simple_chapter_list_item-item-post-args-)
* Include: [_helpers-templates.php](https://github.com/Tetrakern/fictioneer/blob/main/includes/functions/_helpers-templates.php)
```php
/**
* Overwrites the chapter list item string with a prefixed title
* Overwrites the simple chapter list item string with a prefixed title
*
* Note: Warning, this replaces the complete string and should be
* executed early in case there are more, less extreme filters.
@ -190,7 +190,7 @@ Or change them completely, if you want even depending on the chapter or associat
* @return string New HTML list of list item.
*/
function child_prefix_chapter_index_items( $item, $post, $args ) {
function child_prefix_simple_chapter_list_item( $item, $post, $args ) {
$prefix = get_post_meta( $post->ID, 'fictioneer_chapter_prefix', true );
return sprintf(
@ -205,7 +205,7 @@ function child_prefix_chapter_index_items( $item, $post, $args ) {
}
// Priority 1 to execute the filter early
add_filter( 'fictioneer_filter_chapter_list_item', 'child_prefix_chapter_index_items', 1, 3 );
add_filter( 'fictioneer_filter_simple_chapter_list_item', 'child_prefix_simple_chapter_list_item', 1, 3 );
```
## Only show a specific advanced meta field

View File

@ -316,8 +316,11 @@ add_filter( 'fictioneer_filter_simple_chapter_list_items_statuses', 'child_add_s
---
### `apply_filters( 'fictioneer_filter_chapter_list_item', $item, $post, $args )`
### `apply_filters( 'fictioneer_filter_simple_chapter_list_item', $item, $post, $args )`
Filters each list item HTML string used in the chapter index popup and mobile menu section (only visible on chapter pages), build inside the `fictioneer_get_simple_chapter_list_items()` function. Not to be confused with the chapter list shown on story pages. You can either modify the string or build a new one from the given parameters.
**Note:** `fictioneer_filter_chapter_list_item` is deprecated as of 5.25.0.
**Parameters:**
* $item (string) HTML for the list item with icon, ID, link, and title.
* $post (WP_Post) - The chapter post object.
@ -328,6 +331,24 @@ Filters each list item HTML string used in the chapter index popup and mobile me
* $icon (string) HTML for the (text) icon or empty.
* $classes (array) Array of CSS classes or empty.
**Example:**
```php
function child_prefix_simple_chapter_list_item( $item, $post, $args ) {
$prefix = get_post_meta( $post->ID, 'fictioneer_chapter_prefix', true );
return sprintf(
'<li class="%1$s" data-id="%2$s"><a href="%3$s">%4$s<span>%5$s%6$s</span></a></li>',
implode( ' ', $args['classes'] ),
$post->ID,
get_the_permalink( $post->ID ),
$args['icon'],
$prefix ? $prefix . ' ' : '',
$args['list_title'] ?: $args['title']
);
}
add_filter( 'fictioneer_filter_simple_chapter_list_item', 'child_prefix_simple_chapter_list_item', 1, 3 );
```
---
### `apply_filters( 'fictioneer_filter_chapter_micro_menu', $micro_menu, $args )`

View File

@ -1348,7 +1348,8 @@ if ( ! function_exists( 'fictioneer_get_simple_chapter_list_items' ) ) {
$list_title ?: $title
);
$html .= apply_filters(
// Deprecated
$item = apply_filters(
'fictioneer_filter_chapter_list_item',
$item,
$chapter,
@ -1359,6 +1360,18 @@ if ( ! function_exists( 'fictioneer_get_simple_chapter_list_items' ) ) {
'classes' => $classes
)
);
$html .= apply_filters(
'fictioneer_filter_simple_chapter_list_item',
$item,
$chapter,
array(
'title' => $title,
'list_title' => $list_title,
'icon' => $icon,
'classes' => $classes
)
);
}
// Update meta cache