Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Author, Author Bio, Author Name: Add a fallback for Author Archive Template #55451

Merged
merged 5 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/block-library/src/avatar/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
5 changes: 3 additions & 2 deletions packages/block-library/src/post-author-biography/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
*/
function render_block_core_post_author_biography( $attributes, $content, $block ) {
if ( ! isset( $block->context['postId'] ) ) {
return '';
$author_id = get_query_var( 'author' );
} else {
$author_id = get_post_field( 'post_author', $block->context['postId'] );
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Now this conditional is more than a simple guard clause, perhaps it should test for the positive scenario? Sort of like the code style guidelines recommend for ternary operators.

I don't feel strongly about this but it might be good to have all three locations logic match at least 🤷

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review!

I rewrote the two places by 8151386. If I rewrite this part using the ternary operator, nested conditional statements will occur, so I think it's fine as is, but what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies if I wasn't clear with my suggestion. I didn't mean that you needed to change these if/else statements to ternaries, just that we should check the positive case first. Like what is suggested for when you do use ternary operators.

This is such a minor detail, don't feel you need to keep changing the code over it.

Below is a quick diff to illustrate what I was originally suggesting:

Example diff
diff --git a/packages/block-library/src/post-author-biography/index.php b/packages/block-library/src/post-author-biography/index.php
index 6c4cdc301e..23f5f16cbc 100644
--- a/packages/block-library/src/post-author-biography/index.php
+++ b/packages/block-library/src/post-author-biography/index.php
@@ -14,7 +14,11 @@
  * @return string Returns the rendered post author biography block.
  */
 function render_block_core_post_author_biography( $attributes, $content, $block ) {
-	$author_id = isset( $block->context['postId'] ) ? get_post_field( 'post_author', $block->context['postId'] ) : get_query_var( 'author' );
+	if ( 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 '';
diff --git a/packages/block-library/src/post-author-name/index.php b/packages/block-library/src/post-author-name/index.php
index 92d37f70f9..a5fadfcbf3 100644
--- a/packages/block-library/src/post-author-name/index.php
+++ b/packages/block-library/src/post-author-name/index.php
@@ -14,7 +14,11 @@
  * @return string Returns the rendered post author name block.
  */
 function render_block_core_post_author_name( $attributes, $content, $block ) {
-	$author_id = isset( $block->context['postId'] ) ? get_post_field( 'post_author', $block->context['postId'] ) : get_query_var( 'author' );
+	if ( 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 '';

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I mistakenly thought that I should use ternary operators itself. I understand what you mean 😅


$author_id = get_post_field( 'post_author', $block->context['postId'] );
if ( empty( $author_id ) ) {
return '';
}
Expand Down
5 changes: 3 additions & 2 deletions packages/block-library/src/post-author-name/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
*/
function render_block_core_post_author_name( $attributes, $content, $block ) {
if ( ! isset( $block->context['postId'] ) ) {
return '';
$author_id = get_query_var( 'author' );
} else {
$author_id = get_post_field( 'post_author', $block->context['postId'] );
}

$author_id = get_post_field( 'post_author', $block->context['postId'] );
if ( empty( $author_id ) ) {
return '';
}
Expand Down
Loading