Add fictioneer_get_story_chapters helper

This commit is contained in:
Tetrakern 2023-11-30 16:21:16 +01:00
parent b65e8ccea1
commit 082337c53c
9 changed files with 41 additions and 27 deletions

View File

@ -382,13 +382,13 @@ if ( ! function_exists( 'fictioneer_refresh_post_caches' ) ) {
$story_id = get_post_meta( $post_id, 'fictioneer_chapter_story', true );
if ( ! empty( $story_id ) ) {
$chapters = get_post_meta( $story_id, 'fictioneer_story_chapters', true );
$chapters = fictioneer_get_story_chapters( $story_id );
delete_post_meta( $story_id, 'fictioneer_story_data_collection' );
fictioneer_purge_post_cache( $story_id );
// ... and associated chapters
if ( is_array( $chapters ) && ! empty( $chapters ) ) {
if ( ! empty( $chapters ) ) {
foreach ( $chapters as $chapter_id ) {
fictioneer_purge_post_cache( $chapter_id );
}
@ -398,9 +398,9 @@ if ( ! function_exists( 'fictioneer_refresh_post_caches' ) ) {
// Purge associated chapters
if ( $post_type == 'fcn_story' ) {
$chapters = get_post_meta( $post_id, 'fictioneer_story_chapters', true );
$chapters = fictioneer_get_story_chapters( $story_id );
if ( is_array( $chapters ) && ! empty( $chapters ) ) {
if ( ! empty( $chapters ) ) {
foreach ( $chapters as $chapter_id ) {
fictioneer_purge_post_cache( $chapter_id );
}

View File

@ -347,7 +347,7 @@ if ( ! function_exists( 'fictioneer_add_epub_chapters' ) ) {
$templateDoc->xmlStandalone = false;
// Abort if...
if ( ! is_array( $chapters ) || empty( $chapters ) ) {
if ( empty( $chapters ) ) {
fictioneer_epub_return_and_exit();
}
@ -1036,7 +1036,7 @@ function fictioneer_generate_epub() {
$story = get_post( $story_id );
$dir = get_template_directory() . '/epubs/';
$folder = "{$story_id}";
$chapters = get_post_meta( $story_id, 'fictioneer_story_chapters', true ) ?: [];
$chapters = fictioneer_get_story_chapters( $story_id );
$author = get_the_author_meta( 'display_name', $story->post_author );
$co_authors = get_post_meta( $story_id, 'fictioneer_story_co_authors', true ) ?: [];
$all_authors = [];

View File

@ -1905,8 +1905,8 @@ function fictioneer_render_story_data_metabox( $post ) {
);
// Chapters
$chapter_ids = get_post_meta( $post->ID, 'fictioneer_story_chapters', true ) ?: [];
$chapter_ids = is_array( $chapter_ids ) ? $chapter_ids : [];
$chapter_ids = fictioneer_get_story_chapters( $post->ID );
$chapters = empty( $chapter_ids ) ? [] : get_posts(
array(
'post_type' => 'fcn_chapter',
@ -2220,8 +2220,7 @@ function fictioneer_save_story_metaboxes( $post_id ) {
// Chapters
if ( isset( $_POST['fictioneer_story_chapters'] ) && current_user_can( 'edit_fcn_stories', $post_id ) ) {
$previous_chapter_ids = get_post_meta( $post_id, 'fictioneer_story_chapters', true );
$previous_chapter_ids = is_array( $previous_chapter_ids ) ? $previous_chapter_ids : [];
$previous_chapter_ids = fictioneer_get_story_chapters( $post_id );
$chapter_ids = $_POST['fictioneer_story_chapters'];
$chapter_ids = is_array( $chapter_ids ) ? $chapter_ids : [ $chapter_ids ];
@ -2942,12 +2941,7 @@ function fictioneer_append_chapter_to_story( $post_id, $story_id ) {
}
// Get current story chapters
$story_chapters = get_post_meta( $story_id, 'fictioneer_story_chapters', true );
// Prepare chapter array if null (or broken)
if ( ! is_array( $story_chapters ) ) {
$story_chapters = [];
}
$story_chapters = fictioneer_get_story_chapters( $story_id );
// Append chapter (if not already included) and save to database
if ( ! in_array( $post_id, $story_chapters ) ) {

View File

@ -289,8 +289,8 @@ function fictioneer_remove_chapter_from_story( $chapter_id ) {
}
// Check chapter list
$chapters = get_post_meta( $story_id, 'fictioneer_story_chapters', true ) ?: [];
$previous = get_post_meta( $story_id, 'fictioneer_story_chapters', true ) ?: [];
$chapters = fictioneer_get_story_chapters( $story_id );
$previous = $chapters;
if ( empty( $chapters ) || ! in_array( $chapter_id, $chapters ) ) {
return;

View File

@ -270,7 +270,7 @@ if ( ! function_exists( 'fictioneer_get_story_data' ) ) {
}
// Setup
$chapters = get_post_meta( $story_id, 'fictioneer_story_chapters', true ) ?: [];
$chapters = fictioneer_get_story_chapters( $story_id );
$tags = get_the_tags( $story_id );
$fandoms = get_the_terms( $story_id, 'fcn_fandom' );
$characters = get_the_terms( $story_id, 'fcn_character' );
@ -791,6 +791,27 @@ if ( ! function_exists( 'fictioneer_is_editor' ) ) {
// GET META FIELDS
// =============================================================================
if ( ! function_exists( 'fictioneer_get_story_chapters' ) ) {
/**
* Wrapper for get_post_meta() to get story chapters
*
* @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 array Array of post IDs or an empty array.
*/
function fictioneer_get_story_chapters( $post_id = null ) {
// Setup
$chapter_ids = get_post_meta( $post_id ?? get_the_ID(), 'fictioneer_story_chapters', true );
// Always return an array
return is_array( $chapter_ids ) ? $chapter_ids : [];
}
}
if ( ! function_exists( 'fictioneer_get_content_field' ) ) {
/**
* Wrapper for get_post_meta() with content filers applied

View File

@ -31,10 +31,10 @@ function fictioneer_refresh_story_schema( $post_id, $post ) {
fictioneer_build_story_schema( $post_id );
// Get chapters of story
$chapters = get_post_meta( $post_id, 'fictioneer_story_chapters', true );
$chapters = fictioneer_get_story_chapters( $post_id );
// Rebuild chapter schemas (if any)
if ( is_array( $chapters ) ) {
if ( ! empty( $chapters ) ) {
foreach ( $chapters as $chapter_id ) {
fictioneer_build_chapter_schema( $chapter_id );
}
@ -67,7 +67,7 @@ if ( ! function_exists( 'fictioneer_build_story_schema' ) ) {
$schema = fictioneer_get_schema_node_root();
$image_data = fictioneer_get_schema_primary_image( $post_id );
$rating = get_post_meta( $post_id, 'fictioneer_story_rating', true );
$chapters = get_post_meta( $post_id, 'fictioneer_story_chapters', true );
$chapters = fictioneer_get_story_chapters( $post_id );
$page_title = fictioneer_get_seo_title( $post_id, array( 'skip_cache' => true ) );
$default_description = fictioneer_first_paragraph_as_excerpt( fictioneer_get_content_field( 'fictioneer_story_short_description', $post_id ) );
$chapter_count = 0;
@ -79,7 +79,7 @@ if ( ! function_exists( 'fictioneer_build_story_schema' ) ) {
);
// Collect visible chapters
if ( is_array( $chapters ) ) {
if ( ! empty( $chapters ) ) {
foreach ( $chapters as $chapter_id ) {
if (
! get_post_meta( $chapter_id, 'fictioneer_chapter_no_chapter', true ) &&

View File

@ -85,7 +85,7 @@ if ( ! empty( $items ) ) {
break;
case 'fcn_story':
$story_count += 1;
$chapter_ids = get_post_meta( $item->ID, 'fictioneer_story_chapters', true ) ?: [];
$chapter_ids = fictioneer_get_story_chapters( $item->ID );
// Try to rescue an empty description by using one from a story...
if ( empty( $description ) ) {

View File

@ -150,7 +150,7 @@ $disable_folding = get_post_meta( $story_id, 'fictioneer_story_disable_collapse'
<section id="chapters" class="story__tab-page _current story__chapters" data-order="asc" data-view="list">
<?php
$chapters = get_post_meta( $story_id, 'fictioneer_story_chapters', true ) ?: []; // Already prepared!
$chapters = fictioneer_get_story_chapters( $story_id ); // Already prepared!
$chapter_groups = [];
// Loop and prepare groups

View File

@ -25,8 +25,7 @@ if ( ! $story_id || $is_hidden || post_password_required( $story_id ) ) {
// Get story data
$story = fictioneer_get_story_data( $story_id, false ); // Does not refresh comment count!
$chapters = get_post_meta( $story_id, 'fictioneer_story_chapters', true );
$chapters = is_array( $chapters ) ? $chapters : [];
$chapters = fictioneer_get_story_chapters( $story_id );
// Feed title
$title = sprintf(