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

Fix sprintf() arguments #49439

Merged
merged 1 commit into from
Mar 29, 2023
Merged

Fix sprintf() arguments #49439

merged 1 commit into from
Mar 29, 2023

Conversation

janboddez
Copy link
Contributor

@janboddez janboddez commented Mar 29, 2023

Argnum dollar sign seemed to be missing. The 2 in %2s is really a width argument.

What?

Unless the 2nd and 3rd sprintf() arguments (both referenced using %2s) in this line of code really need to have a width of at least 2 chars, there's a typo here, and %2$s and %3$s were meant (and %1$s where %1s was used). See also https://www.php.net/manual/en/function.sprintf.php (compare the argnum$ and width arguments).

Why?

In this case, the bug is mostly harmless, as link targets or author names will almost always have a length of more than 2. Meaning %2s is equivalent to %s and no padding will take place.

But the code is confusing, because it really looks like %2$s was meant instead. It kind of looks like there are more such occurrences in WordPress' code, too, but lets start small.

How?

Added missing dollar sign, fixed 3rd sprintf arg.

Testing Instructions

For most (all?) practical cases, there will be no change in output. But using, e.g., %3s is confusing and may lead to errors in other situations (where %3$s was meant instead), if the argument order is ever changed (like in translated strings).

Argnum dollar sign seemed to be missing. The `2` in `%2s` is width argument.
@janboddez janboddez requested a review from ajitbohra as a code owner March 29, 2023 11:48
@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Mar 29, 2023
@github-actions
Copy link

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @janboddez! In case you missed it, we'd love to have you join us in our Slack community, where we hold regularly weekly meetings open to anyone to coordinate with each other.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@janboddez
Copy link
Contributor Author

Cf. also the post-author-name block, which does get it right:

$author_name = sprintf( '<a href="%1$s" target="%2$s" class="wp-block-post-author-name__link">%3$s</a>', get_author_posts_url( $author_id ), esc_attr( $attributes['linkTarget'] ), $author_name );

@Mamaduka Mamaduka added [Type] Code Quality Issues or PRs that relate to code quality [Block] Post Author Affects the Post Author Block labels Mar 29, 2023
Copy link
Member

@Mamaduka Mamaduka left a comment

Choose a reason for hiding this comment

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

Thank you, @janboddez!

@Mamaduka Mamaduka merged commit b8cd4c9 into WordPress:trunk Mar 29, 2023
@github-actions
Copy link

Congratulations on your first merged pull request, @janboddez! We'd like to credit you for your contribution in the post announcing the next WordPress release, but we can't find a WordPress.org profile associated with your GitHub account. When you have a moment, visit the following URL and click "link your GitHub account" under "GitHub Username" to link your accounts:

https://profiles.wordpress.org/me/profile/edit/

And if you don't have a WordPress.org account, you can create one on this page:

https://login.wordpress.org/register

Kudos!

@github-actions github-actions bot added this to the Gutenberg 15.5 milestone Mar 29, 2023
@ryanwelcher ryanwelcher added the [Type] Bug An existing feature does not function as intended label Mar 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Post Author Affects the Post Author Block First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Type] Bug An existing feature does not function as intended [Type] Code Quality Issues or PRs that relate to code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants