Add filters to word counts

This commit is contained in:
Tetrakern 2024-08-28 19:03:43 +02:00
parent 5652447de0
commit ee64b3cbdf
2 changed files with 32 additions and 4 deletions

View File

@ -1634,6 +1634,15 @@ Filters the intermediate output array in the `_story-header.php` partial before
---
### `apply_filters( 'fictioneer_filter_story_word_count', $word_count, $post_id )`
Filters the total word count of a story after sanitization (greater than or equal to 0) and before `fictioneer_multiply_word_count()` is applied, returning a positive integer. The word count is only calculated and updated when a post associated with the story is saved.
**Parameters:**
* $word_count (int) The word count from the `_word_count` meta field.
* $post_id (int|null) The post ID of the story. Unsafe.
---
### `apply_filters( 'fictioneer_filter_subscribe_buttons', $buttons, $post_id, $author_id, $feed )`
Filters the intermediate output array of the `fictioneer_get_subscribe_options( $post_id, $author_id, $feed )` function before it is imploded and returned. Normally accounts for Patreon, Ko-Fi, SubscribeStar, [Feedly](https://feedly.com/), and [Inoreader](https://www.inoreader.com/).
@ -1723,3 +1732,12 @@ Filters the check result of whether the users Patreon data is still valid. Be
* $valid (boolean) Result of the check. True if valid, false if expired.
* $user_id (int) The user the check is for. 0 if not logged in.
* $patreon_tiers (array) The users Patreon tiers. Can be empty.
---
### `apply_filters( 'fictioneer_filter_word_count', $word_count, $post_id )`
Filters the word count of a post after sanitization (greater than or equal to 0) and before `fictioneer_multiply_word_count()` is applied, returning a positive integer. The word count is only calculated and updated when a post is saved.
**Parameters:**
* $word_count (int) The word count from the `fictioneer_story_total_word_count` meta field.
* $post_id (int|null) The post ID. Unsafe.

View File

@ -1135,6 +1135,7 @@ if ( ! function_exists( 'fictioneer_get_word_count' ) ) {
* @since 5.8.2
* @since 5.9.4 - Add logic for optional multiplier.
* @since 5.22.3 - Moved multiplier to fictioneer_multiply_word_count()
* @since 5.23.1 - Added filter and additional sanitization.
*
* @param int $post_id Optional. The ID of the post the field belongs to.
* Defaults to current post ID.
@ -1143,8 +1144,12 @@ if ( ! function_exists( 'fictioneer_get_word_count' ) ) {
*/
function fictioneer_get_word_count( $post_id = null ) {
// Setup
$words = get_post_meta( $post_id ?? get_the_ID(), '_word_count', true );
// Get word count
$words = get_post_meta( $post_id ?? get_the_ID(), '_word_count', true ) ?: 0;
$words = max( 0, intval( $words ) );
// Filter
$words = apply_filters( 'fictioneer_filter_word_count', $words, $post_id );
// Apply multiplier and return
return fictioneer_multiply_word_count( $words );
@ -1156,6 +1161,7 @@ if ( ! function_exists( 'fictioneer_get_story_word_count' ) ) {
* Returns word count for the whole story
*
* @since 5.22.3
* @since 5.23.1 - Added filter and additional sanitization.
*
* @param int $post_id Post ID of the story.
*
@ -1163,8 +1169,12 @@ if ( ! function_exists( 'fictioneer_get_story_word_count' ) ) {
*/
function fictioneer_get_story_word_count( $post_id = null ) {
// Setup
// Get word count
$words = get_post_meta( $post_id, 'fictioneer_story_total_word_count', true ) ?: 0;
$words = max( 0, intval( $words ) );
// Filter
$words = apply_filters( 'fictioneer_filter_story_word_count', $words, $post_id );
// Apply multiplier and return
return fictioneer_multiply_word_count( $words );
@ -1192,7 +1202,7 @@ if ( ! function_exists( 'fictioneer_multiply_word_count' ) ) {
}
// Always return an integer greater or equal 0
return $words > 0 ? $words : 0;
return max( 0, $words );
}
}