Comments: Add default args to Walker_Comment methods to prevent PHP warnings#11110
Comments: Add default args to Walker_Comment methods to prevent PHP warnings#11110dan-zakirov wants to merge 3 commits intoWordPress:trunkfrom
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Hi @dan-zakirov! 👋 Thank you for your contribution to WordPress! 💖 It looks like this is your first pull request to No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description. Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making. More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook. Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook. If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook. The Developer Hub also documents the various coding standards that are followed:
Thank you, |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
| $args = wp_parse_args( | ||
| $args, | ||
| array( | ||
| 'style' => 'ul', |
There was a problem hiding this comment.
Instead of replicating the default args in multiple plays, why not create a helper method like:
protected function get_args( array $args ): array {
return array_merge(
array(
/* single list of defaults goes here */
),
$args
);
} Then each method can just do:
$args = $this->get_args( $args );There was a problem hiding this comment.
Good point, thanks. Done — refactored to a get_args() helper as suggested. All 7 methods now use $this->get_args( $args ) with a single list of defaults.
Walker_Comment methods access $args keys (style, short_ping, format, avatar_size, max_depth) without checking if they exist. When called with an empty $args array, this produces PHP warnings on PHP 8.0+. Added wp_parse_args() with defaults matching wp_list_comments() at the top of each affected method: start_lvl, end_lvl, start_el, end_el, ping, comment, html5_comment. Added PHPUnit tests to verify no warnings are produced when Walker_Comment methods are called with an empty args array. Trac: https://core.trac.wordpress.org/ticket/56539
- Replaced duplicated wp_parse_args() calls with a single get_args() helper - All 7 methods now use $this->get_args( $args ) for consistency - Single source of defaults: style, avatar_size, format, short_ping, max_depth Affected: src/wp-includes/class-walker-comment.php
| array( | ||
| 'style' => 'ul', | ||
| 'avatar_size' => 32, | ||
| 'format' => 'xhtml', |
There was a problem hiding this comment.
| 'format' => 'xhtml', | |
| 'format' => current_theme_supports( 'html5', 'comment-list' ) ? 'html5' : 'xhtml', |
There was a problem hiding this comment.
Thanks! Already applied manually in the latest commit.
…ts() - Replaced hardcoded 'xhtml' with current_theme_supports() check - Matches the format default logic used in wp_list_comments() Affected: src/wp-includes/class-walker-comment.php
Trac Ticket
https://core.trac.wordpress.org/ticket/56539
Summary
Walker_Comment methods access $args keys (style, short_ping, format, avatar_size, max_depth) without verifying they exist. When called with an empty $args array (which is the documented default), this produces PHP warnings on PHP 8.0+.
Changes
get_args()helper method with a single list of defaults matchingwp_list_comments(), including dynamicformatdetection viacurrent_theme_supports()$this->get_args( $args )instead of duplicated defaultsNotes
This is a cleaner alternative to PR #4126, which mixes the fix with unrelated whitespace/indentation changes. This PR only adds the defaults with no other modifications.