Improve co-author handling

This commit is contained in:
Tetrakern 2023-11-30 12:49:45 +01:00
parent 84a098871a
commit c8b142367c
4 changed files with 35 additions and 16 deletions

View File

@ -54,16 +54,25 @@ if ( ! function_exists( 'fictioneer_api_get_story_node' ) ) {
$node['coAuthors'] = [];
foreach ( $co_author_ids as $co_id ) {
if ( $co_id != $author_id ) {
$co_author_url = get_the_author_meta( 'user_url', $co_id );
$co_author_node = [];
$co_author_name = get_the_author_meta( 'display_name', $co_id );
$co_author_node['name'] = get_the_author_meta( 'display_name', $co_id );
if ( ! empty( $co_author_url ) ) $co_author_node['url'] = $co_author_url;
if ( $co_id != $author_id && ! empty( $co_author_name ) ) {
$co_author_url = get_the_author_meta( 'user_url', $co_id );
$co_author_node = array(
'name' => $co_author_name
);
if ( ! empty( $co_author_url ) ) {
$co_author_node['url'] = $co_author_url;
}
$node['coAuthors'][] = $co_author_node;
}
}
if ( empty( $node['coAuthors'] ) ) {
unset( $node['coAuthors'] );
}
}
// Content
@ -186,11 +195,13 @@ if ( ! function_exists( 'fictioneer_api_get_story_node' ) ) {
$chapter['coAuthors'] = [];
foreach ( $co_author_ids as $co_id ) {
if ( $co_id != $author_id ) {
$co_author_url = get_the_author_meta( 'user_url', $co_id );
$co_author_node = [];
$co_author_name = get_the_author_meta( 'display_name', $co_id );
$co_author_node['name'] = get_the_author_meta( 'display_name', $co_id );
if ( $co_id != $author_id && ! empty( $co_author_name ) ) {
$co_author_url = get_the_author_meta( 'user_url', $co_id );
$co_author_node = array(
'name' => $co_author_name
);
if ( ! empty( $co_author_url ) ) {
$co_author_node['url'] = $co_author_url;
@ -199,6 +210,10 @@ if ( ! function_exists( 'fictioneer_api_get_story_node' ) ) {
$chapter['coAuthors'][] = $co_author_node;
}
}
if ( empty( $chapter['coAuthors'] ) ) {
unset( $chapter['coAuthors'] );
}
}
// Chapter meta

View File

@ -564,6 +564,7 @@ if ( ! function_exists( 'fictioneer_get_story_author_nodes' ) ) {
function fictioneer_get_story_author_nodes( $story_id ) {
// Setup
$all_authors = fictioneer_get_post_author_ids( $story_id );
$all_authors = is_array( $all_authors ) ? $all_authors : [];
// Return author nodes
return fictioneer_get_multi_author_nodes( $all_authors );

View File

@ -347,7 +347,7 @@ if ( ! function_exists( 'fictioneer_add_epub_chapters' ) ) {
$templateDoc->xmlStandalone = false;
// Abort if...
if ( empty( $chapters ) ) {
if ( ! is_array( $chapters ) || empty( $chapters ) ) {
fictioneer_epub_return_and_exit();
}
@ -1050,7 +1050,7 @@ function fictioneer_generate_epub() {
$image_list = [];
// Build list of authors
if ( ! empty( $co_authors ) ) {
if ( is_array( $co_authors ) && ! empty( $co_authors ) ) {
foreach ( $co_authors as $co_author_id ) {
$co_author_name = get_the_author_meta( 'display_name', intval( $co_author_id ) );

View File

@ -675,7 +675,7 @@ function fictioneer_output_head_seo() {
$article_author_url = empty( $article_author_url ) ? get_author_posts_url( $post_author ) : $article_author_url;
// Prepare author array with either URL (required) or name (better than nothing)
$all_authors = empty( $article_author_url ) ? [$article_author] : [$article_author_url];
$all_authors = empty( $article_author_url ) ? [ $article_author ] : [ $article_author_url ];
// Get co-authors (if any)
$co_authors = $post_type == 'fcn_story' ?
@ -683,13 +683,16 @@ function fictioneer_output_head_seo() {
fictioneer_get_field( 'fictioneer_chapter_co_authors', $post_id ) ?? [];
// Add co-authors URL or name
if ( ! empty( $co_authors ) ) {
if ( is_array( $co_authors ) && ! empty( $co_authors ) ) {
foreach ( $co_authors as $co_author_id ) {
$co_author_name = get_the_author_meta( 'display_name', intval( $co_author_id ) );
$co_author_url = get_the_author_meta( 'url', intval( $co_author_id ) );
$co_author_name = get_the_author_meta( 'display_name', $co_author_id );
$co_author_url = get_the_author_meta( 'url', $co_author_id );
$co_author_url = empty( $co_author_url ) ? get_author_posts_url( $co_author_id ) : $co_author_url;
$author_item = empty( $co_author_url ) ? $co_author_name : $co_author_url;
if ( ! empty( $author_item ) && ! in_array( $author_item, $all_authors ) ) $all_authors[] = $author_item;
if ( ! empty( $author_item ) && ! in_array( $author_item, $all_authors ) ) {
$all_authors[] = $author_item;
}
}
}