From 189e23b084f857e71884173660172d62d8f566af Mon Sep 17 00:00:00 2001 From: Aki Hamano <54422211+t-hamano@users.noreply.github.com> Date: Tue, 20 Feb 2024 14:03:46 +0900 Subject: [PATCH] Author, Author Bio, Author Name: Add a fallback for Author Archive Template (#55451) * Author, Author Bio, Author Name: Add a fallback for Author Archive Template * Use ternary operators * Use positive conditionals * Fix typo Co-authored-by: t-hamano Co-authored-by: aaronrobertshaw --- packages/block-library/src/avatar/index.php | 13 ++++++++++++- .../src/post-author-biography/index.php | 7 ++++--- .../block-library/src/post-author-name/index.php | 7 ++++--- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/packages/block-library/src/avatar/index.php b/packages/block-library/src/avatar/index.php index d404fb81ca3576..303b35458eb1ae 100644 --- a/packages/block-library/src/avatar/index.php +++ b/packages/block-library/src/avatar/index.php @@ -31,7 +31,18 @@ function render_block_core_avatar( $attributes, $content, $block ) { : ''; if ( ! isset( $block->context['commentId'] ) ) { - $author_id = isset( $attributes['userId'] ) ? $attributes['userId'] : get_post_field( 'post_author', $block->context['postId'] ); + if ( isset( $attributes['userId'] ) ) { + $author_id = $attributes['userId']; + } elseif ( isset( $block->context['postId'] ) ) { + $author_id = get_post_field( 'post_author', $block->context['postId'] ); + } else { + $author_id = get_query_var( 'author' ); + } + + if ( empty( $author_id ) ) { + return ''; + } + $author_name = get_the_author_meta( 'display_name', $author_id ); // translators: %s is the Author name. $alt = sprintf( __( '%s Avatar' ), $author_name ); diff --git a/packages/block-library/src/post-author-biography/index.php b/packages/block-library/src/post-author-biography/index.php index 3ea6ecbd9b46a1..23f5f16cbcf450 100644 --- a/packages/block-library/src/post-author-biography/index.php +++ b/packages/block-library/src/post-author-biography/index.php @@ -14,11 +14,12 @@ * @return string Returns the rendered post author biography block. */ function render_block_core_post_author_biography( $attributes, $content, $block ) { - if ( ! isset( $block->context['postId'] ) ) { - return ''; + if ( isset( $block->context['postId'] ) ) { + $author_id = get_post_field( 'post_author', $block->context['postId'] ); + } else { + $author_id = get_query_var( 'author' ); } - $author_id = get_post_field( 'post_author', $block->context['postId'] ); if ( empty( $author_id ) ) { return ''; } diff --git a/packages/block-library/src/post-author-name/index.php b/packages/block-library/src/post-author-name/index.php index c5c6d142e06ddf..a5fadfcbf33c1c 100644 --- a/packages/block-library/src/post-author-name/index.php +++ b/packages/block-library/src/post-author-name/index.php @@ -14,11 +14,12 @@ * @return string Returns the rendered post author name block. */ function render_block_core_post_author_name( $attributes, $content, $block ) { - if ( ! isset( $block->context['postId'] ) ) { - return ''; + if ( isset( $block->context['postId'] ) ) { + $author_id = get_post_field( 'post_author', $block->context['postId'] ); + } else { + $author_id = get_query_var( 'author' ); } - $author_id = get_post_field( 'post_author', $block->context['postId'] ); if ( empty( $author_id ) ) { return ''; }