Skip to content

Comments: Add default args to Walker_Comment methods to prevent PHP warnings#11110

Open
dan-zakirov wants to merge 3 commits intoWordPress:trunkfrom
dan-zakirov:trac-56539
Open

Comments: Add default args to Walker_Comment methods to prevent PHP warnings#11110
dan-zakirov wants to merge 3 commits intoWordPress:trunkfrom
dan-zakirov:trac-56539

Conversation

@dan-zakirov
Copy link

@dan-zakirov dan-zakirov commented Mar 2, 2026

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

  • Added get_args() helper method with a single list of defaults matching wp_list_comments(), including dynamic format detection via current_theme_supports()
  • All 7 affected methods now use $this->get_args( $args ) instead of duplicated defaults
  • Added PHPUnit tests to verify no warnings when methods are called with empty args

Notes

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.

@github-actions
Copy link

github-actions bot commented Mar 2, 2026

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 props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props alexodiy, westonruter.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link

github-actions bot commented Mar 2, 2026

Hi @dan-zakirov! 👋

Thank you for your contribution to WordPress! 💖

It looks like this is your first pull request to wordpress-develop. Here are a few things to be aware of that may help you out!

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,
The WordPress Project

@github-actions
Copy link

github-actions bot commented Mar 2, 2026

Test using WordPress Playground

The 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

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

$args = wp_parse_args(
$args,
array(
'style' => 'ul',
Copy link
Member

@westonruter westonruter Mar 3, 2026

Choose a reason for hiding this comment

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

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 );

Copy link
Author

Choose a reason for hiding this comment

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

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',
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
'format' => 'xhtml',
'format' => current_theme_supports( 'html5', 'comment-list' ) ? 'html5' : 'xhtml',

Copy link
Author

@dan-zakirov dan-zakirov Mar 3, 2026

Choose a reason for hiding this comment

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

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants