Add fictioneer_get_word_count helper

This commit is contained in:
Tetrakern 2023-11-30 17:10:19 +01:00
parent 082337c53c
commit 36aff1f54a
13 changed files with 38 additions and 16 deletions

View File

@ -220,7 +220,7 @@ if ( ! function_exists( 'fictioneer_api_get_story_node' ) ) {
$chapter['published'] = get_post_time( 'U', true );
$chapter['modified'] = get_post_modified_time( 'U', true );
$chapter['protected'] = post_password_required();
$chapter['words'] = intval( get_post_meta( $chapter_id, '_word_count', true ) );
$chapter['words'] = fictioneer_get_word_count( $chapter_id );
$chapter['nonChapter'] = ! empty( $no_chapter );
if ( ! empty( $rating ) ) {

View File

@ -1003,7 +1003,7 @@ function fictioneer_shortcode_chapter_list( $attr ) {
$icon = fictioneer_get_icon_field( 'fictioneer_chapter_icon', $chapter_id );
$text_icon = get_post_meta( $chapter_id, 'fictioneer_chapter_text_icon', true );
$prefix = get_post_meta( $chapter_id, 'fictioneer_chapter_prefix', true );
$words = get_post_meta( $chapter_id, '_word_count', true );
$words = fictioneer_get_word_count( $chapter_id );;
$title = fictioneer_get_safe_title( $chapter_id );
// Start HTML ---> ?>

View File

@ -323,7 +323,7 @@ if ( ! function_exists( 'fictioneer_get_story_data' ) ) {
// Do not count non-chapters...
if ( ! get_post_meta( $chapter->ID, 'fictioneer_chapter_no_chapter', true ) ) {
$chapter_count += 1;
$word_count += (int) get_post_meta( $chapter->ID, '_word_count', true );
$word_count += fictioneer_get_word_count( $chapter->ID );
}
// ... but they are still listed!
@ -336,7 +336,7 @@ if ( ! function_exists( 'fictioneer_get_story_data' ) ) {
}
// Add story word count
$word_count += (int) get_post_meta( $story_id, '_word_count', true );
$word_count += fictioneer_get_word_count( $story_id );
// Prepare result
$result = array(
@ -463,7 +463,7 @@ if ( ! function_exists( 'fictioneer_get_author_statistics' ) ) {
$comment_count = 0;
foreach ( $chapters as $chapter ) {
$word_count += (int) get_post_meta( $chapter->ID, '_word_count', true );
$word_count += fictioneer_get_word_count( $chapter->ID );
$comment_count += get_comments_number( $chapter );
}
@ -812,6 +812,28 @@ if ( ! function_exists( 'fictioneer_get_story_chapters' ) ) {
}
}
if ( ! function_exists( 'fictioneer_get_word_count' ) ) {
/**
* Wrapper for get_post_meta() to get word count
*
* @since Fictioneer 5.8.2
*
* @param int $post_id Optional. The ID of the post the field belongs to.
* Defaults to current post ID.
*
* @return int The word count or 0.
*/
function fictioneer_get_word_count( $post_id = null ) {
// Setup
$words = get_post_meta( $post_id ?? get_the_ID(), '_word_count', true );
$words = (int) $words;
// Always return an integer greater or equal 0
return $words > 0 ? $words : 0;
}
}
if ( ! function_exists( 'fictioneer_get_content_field' ) ) {
/**
* Wrapper for get_post_meta() with content filers applied

View File

@ -65,7 +65,7 @@ if ( ! function_exists( 'fictioneer_build_chapter_schema' ) ) {
$schema = fictioneer_get_schema_node_root();
$story_id = get_post_meta( $post_id, 'fictioneer_chapter_story', true );
$image_data = fictioneer_get_schema_primary_image( $post_id );
$word_count = intval( get_post_meta( $post_id, '_word_count', true ) );
$word_count = fictioneer_get_word_count( $post_id );
$page_description = fictioneer_get_seo_description( $post_id );
$page_title = fictioneer_get_seo_title( $post_id, array( 'skip_cache' => true ) );

View File

@ -191,7 +191,7 @@ $show_type = $args['show_type'] ?? false;
$footer_items['words'] = '<i class="card-footer-icon fa-solid fa-font" title="' .
esc_attr__( 'Words', 'fictioneer' ) . '"></i> ' .
fictioneer_shorten_number( get_post_meta( get_the_ID(), '_word_count', true ) );
fictioneer_shorten_number( fictioneer_get_word_count( get_the_ID() ) );
if ( ( $args['orderby'] ?? 0 ) === 'date' ) {
$footer_items['publish_date'] = '<i class="card-footer-icon fa-solid fa-clock" title="' .

View File

@ -79,7 +79,7 @@ if ( ! empty( $items ) ) {
case 'fcn_chapter':
if ( ! in_array( $item->ID, $processed_ids ) ) {
$chapter_count += 1;
$word_count += (int) get_post_meta( $item->ID, '_word_count', true );
$word_count += fictioneer_get_word_count( $item->ID );
$processed_ids[] = $item->ID;
}
break;
@ -118,7 +118,7 @@ if ( ! empty( $items ) ) {
if ( ! in_array( $chapter->ID, $processed_ids ) ) {
$chapter_count += 1;
$word_count += (int) get_post_meta( $chapter->ID, '_word_count', true );
$word_count += fictioneer_get_word_count( $chapter->ID );
$processed_ids[] = $chapter->ID;
}
}

View File

@ -135,7 +135,7 @@ $is_sticky = FICTIONEER_ENABLE_STICKY_CARDS &&
<?php
printf(
'%1$s<span class="hide-below-480"> %2$s</span><span class="separator-dot">&#8196;&bull;&#8196;</span>%3$s',
fictioneer_shorten_number( get_post_meta( $chapter->ID, '_word_count', true ) ),
fictioneer_shorten_number( fictioneer_get_word_count( $chapter->ID ) ),
__( 'Words', 'fictioneer' ),
strtotime( '-1 days' ) < strtotime( get_the_date( '', $chapter->ID ) ) ?
__( 'New', 'fictioneer' ) : get_the_time( FICTIONEER_CARD_STORY_LI_DATE, $chapter->ID )

View File

@ -72,7 +72,7 @@ if ( ! empty( $args['featured_list'] ) ) {
! get_post_meta( $chapter->ID, 'fictioneer_chapter_hidden', true ) &&
! get_post_meta( $chapter->ID, 'fictioneer_chapter_no_chapter', true )
) {
$word_count += (int) get_post_meta( $chapter->ID, '_word_count', true );
$word_count += fictioneer_get_word_count( $chapter->ID );
}
}
}

View File

@ -180,7 +180,7 @@ remove_filter( 'posts_where', 'fictioneer_exclude_protected_posts' );
<?php
printf(
_x( '%1$s Words on %2$s', 'Small card: {n} Words on {Date}.', 'fictioneer' ),
fictioneer_shorten_number( get_post_meta( $post->ID, '_word_count', true ) ),
fictioneer_shorten_number( fictioneer_get_word_count( $post->ID ) ),
get_the_time( FICTIONEER_LATEST_CHAPTERS_FOOTER_DATE )
);
?>

View File

@ -204,7 +204,7 @@ remove_filter( 'posts_where', 'fictioneer_exclude_protected_posts' );
$footer_items['words'] = '<i class="card-footer-icon fa-solid fa-font" title="' .
esc_attr__( 'Words', 'fictioneer' ) . '"></i> ' .
fictioneer_shorten_number( get_post_meta( $post->ID, '_word_count', true ) );
fictioneer_shorten_number( fictioneer_get_word_count( $post->ID ) );
if ( $args['orderby'] == 'modified' ) {
$footer_items['modified_date'] = '<i class="card-footer-icon fa-regular fa-clock" title="' .

View File

@ -198,7 +198,7 @@ remove_filter( 'posts_where', 'fictioneer_exclude_protected_posts' );
</div>
<div class="card__right">
<?php
echo fictioneer_shorten_number( get_post_meta( $chapter_id, '_word_count', true ) );
echo fictioneer_shorten_number( fictioneer_get_word_count( $chapter_id ) );
echo '<span class="separator-dot">&#8196;&bull;&#8196;</span>';
echo get_the_date( FICTIONEER_LATEST_UPDATES_LI_DATE, $chapter_id )
?>

View File

@ -195,7 +195,7 @@ remove_filter( 'posts_where', 'fictioneer_exclude_protected_posts' );
</div>
<div class="card__right">
<?php
echo fictioneer_shorten_number( get_post_meta( $chapter_id, '_word_count', true ) );
echo fictioneer_shorten_number( fictioneer_get_word_count( $chapter_id ) );
echo '<span class="separator-dot">&#8196;&bull;&#8196;</span>';
echo get_the_date( FICTIONEER_LATEST_UPDATES_LI_DATE, $chapter_id )
?>

View File

@ -206,7 +206,7 @@ $disable_folding = get_post_meta( $story_id, 'fictioneer_story_disable_collapse'
'prefix' => get_post_meta( $chapter_id, 'fictioneer_chapter_prefix', true ),
'title' => fictioneer_get_safe_title( $chapter_id ),
'list_title' => get_post_meta( $chapter_id, 'fictioneer_chapter_list_title', true ),
'words' => get_post_meta( $chapter_id, '_word_count', true ),
'words' => fictioneer_get_word_count( $chapter_id ),
'warning' => get_post_meta( $chapter_id, 'fictioneer_chapter_warning', true )
);
}