Add helper to get chapter story ID

This would be useful if I ever decide to use the parent ID field instead if meta.
This commit is contained in:
Tetrakern 2024-10-26 02:03:23 +02:00
parent 3f84aa6585
commit 4b41dc825c
24 changed files with 60 additions and 42 deletions

View File

@ -58,7 +58,7 @@ if ( $post_id ) {
$story_id = $post_id;
break;
case 'fcn_chapter':
$story_id = get_post_meta( get_the_ID(), 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( get_the_ID() );
break;
}

View File

@ -333,3 +333,21 @@ function fictioneer_allow_falsy_story_hidden( $allowed ) {
if ( get_option( 'fictioneer_disable_extended_story_list_meta_queries' ) ) {
add_filter( 'fictioneer_filter_falsy_meta_allow_list', 'fictioneer_allow_falsy_story_hidden' );
}
// =============================================================================
// GET CHAPTER STORY ID
// =============================================================================
/**
* Returns ID of the chapter story or null
*
* @since 5.26.9
*
* @param int $chapter_id Chapter ID.
*
* @return int|null Story ID or null if not set.
*/
function fictioneer_get_chapter_story_id( $chapter_id ) {
return get_post_meta( $chapter_id, 'fictioneer_chapter_story', true );
}

View File

@ -653,7 +653,7 @@ if ( ! function_exists( 'fictioneer_get_small_card_thumbnail' ) ) {
function fictioneer_get_small_card_thumbnail( $post_id, $args = [] ) {
// Setup
$parent_id = $args['parent_id'] ?? get_post_meta( $post_id, 'fictioneer_chapter_story', true );
$parent_id = $args['parent_id'] ?? fictioneer_get_chapter_story_id( $post_id );
$landscape_image_id = get_post_meta( $post_id, 'fictioneer_landscape_image', true );
$thumbnail_size = ( $args['vertical'] ?? 0 ) ? ( $args['size'] ?? 'large' ) : 'snippet';
$thumbnail_args = array(
@ -900,7 +900,7 @@ if ( ! function_exists( 'fictioneer_get_subscribe_options' ) ) {
// Setup
$post_id = $post_id ? $post_id : get_the_ID();
$post_type = get_post_type( $post_id );
$story_id = get_post_meta( $post_id, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $post_id );
$author_id = $author_id ? $author_id : get_the_author_meta( 'ID' );
$patreon_link = get_post_meta( $post_id, 'fictioneer_patreon_link', true );
$kofi_link = get_post_meta( $post_id, 'fictioneer_kofi_link', true );
@ -1565,7 +1565,7 @@ if ( ! function_exists( 'fictioneer_get_rss_link' ) ) {
$feed = esc_url( add_query_arg( 'story_id', $post_id, home_url( 'feed/rss-chapters' ) ) );
break;
case 'fcn_chapter':
$story_id = get_post_meta( $post_id, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $post_id );
if ( $story_id ) {
$feed = esc_url( add_query_arg( 'story_id', $story_id, home_url( 'feed/rss-chapters' ) ) );
};
@ -1972,7 +1972,7 @@ function fictioneer_get_support_links( $post_id = null, $parent_id = null, $auth
// Get story ID if chapter and parent ID not given
if ( $parent_id === null && get_post_type( $post_id ) == 'fcn_chapter' ) {
$parent_id = get_post_meta( $post_id, 'fictioneer_chapter_story', true );
$parent_id = fictioneer_get_chapter_story_id( $post_id );
}
// Iterate over keys of interest

View File

@ -112,7 +112,7 @@ function fictioneer_post_comment_to_discord( $comment_id, $comment_approved ) {
}
// Is chapter with story?
$story_id = get_post_meta( $post->ID, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $post->ID );
if ( $story_id ) {
$message['embeds'][0]['footer'] = array(
@ -324,7 +324,7 @@ function fictioneer_post_chapter_to_discord( $post_id, $post, $update ) {
);
// Story?
$story_id = get_post_meta( $post->ID, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $post->ID );
$story_status = get_post_status( $story_id );
$story_title = get_the_title( $story_id );
$story_url = get_permalink( $story_id );

View File

@ -84,7 +84,7 @@ if ( ! function_exists( 'fictioneer_seo_fields' ) ) {
// ... if not found, look for parent thumbnail...
if ( ! $seo_og_image_display && $post->post_type === 'fcn_chapter' ) {
$story_id = get_post_meta( $post->ID, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $post->ID );
if ( $story_id && has_post_thumbnail( $story_id ) ) {
$seo_og_image_display = get_the_post_thumbnail_url( $story_id );
@ -629,7 +629,7 @@ if ( ! function_exists( 'fictioneer_get_seo_image' ) ) {
// Try story thumbnail if chapter...
if ( ! $image_id && get_post_type( $post_id ) == 'fcn_chapter' ) {
$story_id = get_post_meta( $post_id, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $post_id );
$image_id = has_post_thumbnail( $story_id ) ? get_post_thumbnail_id( $story_id ) : $image_id;
}
@ -690,7 +690,7 @@ function fictioneer_output_head_seo() {
$post_id = get_queried_object_id(); // In archives and search, this is the first post
$post_type = get_post_type(); // In archives and search, this is the first post
$post_author = $post->post_author ?? 0;
$chapter_story_id = get_post_meta( $post_id, 'fictioneer_chapter_story', true );
$chapter_story_id = fictioneer_get_chapter_story_id( $post_id );
$canonical_url = wp_get_canonical_url(); // Only on singular pages
// Flags

View File

@ -495,7 +495,7 @@ if ( ! function_exists( 'fictioneer_refresh_post_caches' ) ) {
// Purge parent story (if any)...
if ( $post_type == 'fcn_chapter' ) {
$story_id = get_post_meta( $post_id, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $post_id );
if ( ! empty( $story_id ) ) {
$chapters = fictioneer_get_story_chapter_ids( $story_id );
@ -740,7 +740,7 @@ if ( ! function_exists( 'fictioneer_track_chapter_and_story_updates' ) ) {
// Get story ID from post or parent story (if any)
$post_type = get_post_type( $post_id ); // Not all hooks get the $post object!
$story_id = $post_type == 'fcn_story' ? $post_id : get_post_meta( $post_id, 'fictioneer_chapter_story', true );
$story_id = $post_type == 'fcn_story' ? $post_id : fictioneer_get_chapter_story_id( $post_id );
// Delete cached statistics for stories
delete_transient( 'fictioneer_stories_statistics' );
@ -812,7 +812,7 @@ function fictioneer_rebuild_story_data_collection( $post_id, $post ) {
// Story or chapter?
if ( $post->post_type === 'fcn_chapter' ) {
$story_id = get_post_meta( $post_id, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $post_id );
if ( $story_id ) {
fictioneer_get_story_data( $story_id );
@ -1399,7 +1399,7 @@ function fictioneer_delete_cached_story_card_after_update( $post_id ) {
// Chapter?
if ( get_post_type( $post_id ) === 'fcn_chapter' ) {
$story_id = get_post_meta( $post_id, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $post_id );
if ( $story_id ) {
fictioneer_delete_cached_story_card( $story_id );
@ -1426,7 +1426,7 @@ function fictioneer_delete_cached_story_card_by_comment( $comment_id ) {
$comment = get_comment( $comment_id );
if ( $comment ) {
$story_id = get_post_meta( $comment->comment_post_ID, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $comment->comment_post_ID );
if ( $story_id ) {
fictioneer_delete_cached_story_card( $story_id );

View File

@ -26,7 +26,7 @@ function fictioneer_update_modified_date_on_story_for_chapter( $post_id ) {
}
// Setup
$story_id = get_post_meta( $post_id, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $post_id );
// No linked story found
if ( empty( $story_id ) || ! get_post_status( $story_id ?? 0 ) ) {
@ -187,7 +187,7 @@ function fictioneer_log_story_chapter_status_changes( $new_status, $old_status,
// Story?
$post_id = $post->ID;
$story_id = get_post_meta( $post_id, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $post_id );
if ( empty( $story_id ) ) {
return;
@ -254,7 +254,7 @@ function fictioneer_remove_chapter_from_story( $chapter_id ) {
}
// Story?
$story_id = get_post_meta( $chapter_id, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $chapter_id );
if ( empty( $story_id ) ) {
return;
@ -334,7 +334,7 @@ function fictioneer_chapter_future_to_publish( $new_status, $old_status, $post )
}
// Update fictioneer_chapters_added field of story (if any)
$story_id = get_post_meta( $post->ID, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $post->ID );
if ( $story_id ) {
update_post_meta( $story_id, 'fictioneer_chapters_added', $post->post_date_gmt );
@ -418,7 +418,7 @@ function fictioneer_increment_story_comment_count( $comment_id ) {
return;
}
$story_id = get_post_meta( $comment->comment_post_ID, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $comment->comment_post_ID );
$story_data = $story_id ? fictioneer_get_story_data( $story_id ) : null;
// Increment comment count (will be recounted at some later point)
@ -444,7 +444,7 @@ function fictioneer_decrement_story_comment_count( $comment_id ) {
return;
}
$story_id = get_post_meta( $comment->comment_post_ID, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $comment->comment_post_ID );
$story_data = $story_id ? fictioneer_get_story_data( $story_id ) : null;
// Decrement comment count (will be recounted at some later point)

View File

@ -2745,7 +2745,7 @@ function fictioneer_render_chapter_data_metabox( $post ) {
$nonce = wp_create_nonce( "chapter_meta_data_{$post->ID}" ); // Accounts for manual wp_update_post() calls!
$post_author_id = get_post_field( 'post_author', $post->ID );
$current_story_id = get_post_meta( $post->ID, 'fictioneer_chapter_story', true );
$current_story_id = fictioneer_get_chapter_story_id( $post->ID );
$output = [];
// --- Add fields ------------------------------------------------------------
@ -3027,7 +3027,7 @@ function fictioneer_save_chapter_metaboxes( $post_id ) {
// Story
if ( isset( $_POST['fictioneer_chapter_story'] ) ) {
$story_id = fictioneer_validate_id( $_POST['fictioneer_chapter_story'], 'fcn_story' );
$current_story_id = absint( get_post_meta( $post_id, 'fictioneer_chapter_story', true ) );
$current_story_id = absint( fictioneer_get_chapter_story_id( $post_id ) );
if ( $current_story_id && $story_id !== $current_story_id ) {
// Make sure the chapter is not in the list of the wrong story
@ -4811,7 +4811,7 @@ add_filter( 'manage_fcn_chapter_posts_columns', 'fictioneer_add_posts_column_cha
function fictioneer_manage_posts_column_chapter_story( $column_name, $post_id ) {
if (
$column_name === 'fictioneer_chapter_story' &&
( $story_id = get_post_meta( $post_id, 'fictioneer_chapter_story', true ) )
( $story_id = fictioneer_get_chapter_story_id( $post_id ) )
) {
static $titles = [];

View File

@ -553,7 +553,7 @@ function fictioneer_bypass_password( $required, $post ) {
// Check unlocked posts
if ( $user && $required ) {
$story_id = get_post_meta( $post->ID, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $post->ID );
$unlocks = get_user_meta( $user->ID, 'fictioneer_post_unlocks', true ) ?: [];
$unlocks = is_array( $unlocks ) ? $unlocks : [];

View File

@ -1216,7 +1216,7 @@ function fictioneer_shortcode_chapter_list( $attr ) {
// Setup
$chapter_query->the_post();
$chapter_id = get_the_ID();
$chapter_story_id = get_post_meta( $chapter_id, 'fictioneer_chapter_story', true );
$chapter_story_id = fictioneer_get_chapter_story_id( $chapter_id );
// Skip not visible chapters
if ( get_post_meta( $chapter_id, 'fictioneer_chapter_hidden', true ) ) {

View File

@ -295,7 +295,7 @@ if ( get_option( 'fictioneer_rewrite_chapter_permalinks' ) ) {
function fictioneer_rewrite_chapter_permalink( $post_link, $post ) {
// Only for chapters...
if ( $post->post_type === 'fcn_chapter' ) {
$story_id = get_post_meta( $post->ID, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $post->ID );
if ( $story_id ) {
// Chapter with story

View File

@ -405,7 +405,7 @@ function fictioneer_output_rss( $post_id = null ) {
}
// RSS Feed if chapter...
$story_id = get_post_meta( $post_id, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $post_id );
if ( $post_type == 'fcn_chapter' && ! empty( $story_id ) && ! $skip_to_default ) {
$title = sprintf(
@ -1306,7 +1306,7 @@ function fictioneer_gate_unpublished_content() {
// 404 chapter of unpublished story
if ( $post->post_type === 'fcn_chapter' ) {
$story_id = get_post_meta( $post->ID, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $post->ID );
if (
! empty( $story_id ) &&

View File

@ -2238,7 +2238,7 @@ if ( ! function_exists( 'fictioneer_is_commenting_disabled' ) ) {
// Check parent story if chapter...
if ( get_post_type( $post_id ) === 'fcn_chapter' ) {
$story_id = get_post_meta( $post_id, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $post_id );
if ( $story_id ) {
return get_post_meta( $story_id, 'fictioneer_disable_commenting', true ) == true;

View File

@ -28,7 +28,7 @@ function fictioneer_add_fiction_css() {
}
if ( get_post_type( $post_id ) == 'fcn_chapter' ) {
$story_id = get_post_meta( $post_id, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $post_id );
if ( ! empty( $story_id ) ) {
$custom_css .= get_post_meta( $story_id, 'fictioneer_story_css', true );

View File

@ -285,7 +285,7 @@ function fictioneer_mobile_lists_panel() {
$output = [];
// Chapters?
if ( $post_type === 'fcn_chapter' && get_post_meta( get_the_ID(), 'fictioneer_chapter_story', true ) && ! is_search() ) {
if ( $post_type === 'fcn_chapter' && fictioneer_get_chapter_story_id( get_the_ID() ) && ! is_search() ) {
$output['chapters'] = sprintf(
'<button class="mobile-menu__frame-button" data-click-action="open-dialog-modal" data-click-target="#fictioneer-chapter-index-dialog"><i class="fa-solid fa-caret-right mobile-menu__item-icon"></i> %s</button>',
__( 'Chapters', 'fictioneer' )

View File

@ -33,7 +33,7 @@ function fictioneer_refresh_chapter_schema( $post_id, $post ) {
}
// Setup
$story_id = get_post_meta( $post_id, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $post_id );
// Rebuild schema
fictioneer_build_chapter_schema( $post_id );
@ -69,7 +69,7 @@ if ( ! function_exists( 'fictioneer_build_chapter_schema' ) ) {
// Setup
$post = get_post( $post_id );
$schema = fictioneer_get_schema_node_root();
$story_id = get_post_meta( $post_id, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $post_id );
$image_data = fictioneer_get_schema_primary_image( $post_id );
$word_count = fictioneer_get_word_count( $post_id );
$page_description = fictioneer_get_seo_description( $post_id );

View File

@ -334,7 +334,7 @@ function fictioneer_ajax_get_follows_notifications() {
), $chapter->ID
);
$chapter_timestamp = get_post_timestamp( $chapter->ID ) * 1000; // Compatible with Date.now() in JavaScript
$story_id = get_post_meta( $chapter->ID, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $chapter->ID );
$new = $user_follows['seen'] < $chapter_timestamp ? '_new' : '';
// Start HTML ---> ?>

View File

@ -171,7 +171,7 @@ if ( $splide ) {
// Chapter story?
if ( $post->post_type === 'fcn_chapter' ) {
$story_id = get_post_meta( $post_id, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $post_id );
}
// Extra classes

View File

@ -20,7 +20,7 @@ defined( 'ABSPATH' ) OR exit;
// Setup
$post_id = $post->ID;
$title = fictioneer_get_safe_title( $post_id, 'card-chapter' );
$story_id = get_post_meta( $post_id, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $post_id );
$story_post = get_post( $story_id );
$story_unpublished = get_post_status( $story_id ) !== 'publish';
$story_data = $story_id ? fictioneer_get_story_data( $story_id, false ) : null; // Does not refresh comment count!

View File

@ -154,7 +154,7 @@ if ( $splide ) {
<?php
// Setup
$post_id = $post->ID;
$story_id = get_post_meta( $post_id, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $post_id );
if ( get_post_status( $story_id ) !== 'publish' ) {
continue;

View File

@ -159,7 +159,7 @@ if ( $splide ) {
<?php
// Setup
$post_id = $post->ID;
$story_id = get_post_meta( $post_id, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $post_id );
$story = $story_id ? fictioneer_get_story_data( $story_id, false ) : null; // Does not refresh comment count!
if ( get_post_status( $story_id ) !== 'publish' || ! $story ) {

View File

@ -159,7 +159,7 @@ if ( $splide ) {
<?php
// Setup
$post_id = $post->ID;
$story_id = get_post_meta( $post_id, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $post_id );
if ( get_post_status( $story_id ) !== 'publish' ) {
continue;

View File

@ -178,7 +178,7 @@ if ( $args['min_width'] ) {
break;
case 'fcn_chapter':
$list_title = get_post_meta( $post_id, 'fictioneer_chapter_list_title', true );
$story_id = get_post_meta( $post_id, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $post_id );
if ( empty( $landscape_image_id ) ) {
$landscape_image_id = get_post_meta( $story_id, 'fictioneer_landscape_image', true );

View File

@ -49,7 +49,7 @@ get_header(
$age_rating = get_post_meta( $post_id, 'fictioneer_chapter_rating', true );
$this_breadcrumb = [ $title, get_the_permalink() ];
$story_id = get_post_meta( $post_id, 'fictioneer_chapter_story', true );
$story_id = fictioneer_get_chapter_story_id( $post_id );
$story_data = null;
$story_post = null;