-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Merge sub properties in add_post_type_support().
#10426
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
base: trunk
Are you sure you want to change the base?
Merge sub properties in add_post_type_support().
#10426
Conversation
Previously, calling `add_post_type_support()` multiple times for the same post type and feature with array arguments would overwrite the previous arguments instead of merging them. This prevented plugins and themes from incrementally adding sub-properties to the same feature.
This changes the behavior to merge associative array arguments when the function is called multiple times with the same post type and feature combination.
Example usage:
{{{
add_post_type_support( 'page', 'editor', array(
'default-mode' => 'template-locked',
) );
add_post_type_support( 'page', 'editor', array(
'block-comments' => true,
) );
// Both properties are now preserved instead of only the last one.
}}}
The fix maintains full backwards compatibility by only merging when both the existing and new values are associative arrays. All other calling patterns continue to work as before.
Props fabiankaegy, mamaduka, wildworks.
|
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. |
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
|
IMO, the current behavior is broken, but given everything WP, would this be considered a breaking change? |
Co-authored-by: Mamaduka <[email protected]>
| if ( isset( $_wp_post_type_features[ $post_type ][ $feature ][0] ) | ||
| && is_array( $_wp_post_type_features[ $post_type ][ $feature ][0] ) | ||
| && isset( $args[0] ) | ||
| && is_array( $args[0] ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: This is a bit easier to read and I think is more common formatting for multi-line conditions:
| if ( isset( $_wp_post_type_features[ $post_type ][ $feature ][0] ) | |
| && is_array( $_wp_post_type_features[ $post_type ][ $feature ][0] ) | |
| && isset( $args[0] ) | |
| && is_array( $args[0] ) ) { | |
| if ( | |
| isset( $_wp_post_type_features[ $post_type ][ $feature ][0] ) && | |
| is_array( $_wp_post_type_features[ $post_type ][ $feature ][0] ) && | |
| isset( $args[0] ) && | |
| is_array( $args[0] ) | |
| ) { |
| if ( isset( $_wp_post_type_features[ $post_type ][ $feature ][0] ) | ||
| && is_array( $_wp_post_type_features[ $post_type ][ $feature ][0] ) | ||
| && isset( $args[0] ) | ||
| && is_array( $args[0] ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The is_array( $args[0] ) call here isn't checking whether the arg is an associative array. Shouldn't it also check if ! isset( $args[0][0] )?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or rather: ! array_is_list( $args[0] )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Additionally, what if someone tries to merge an empty array? Should that be allowed? So perhaps the new check here should be: ! array_is_list( $args[0] ) || empty( $args[0] )
| $_wp_post_type_features[ $post_type ][ $feature ] = $args; | ||
| // Check if feature already exists with args and if both are associative arrays that should be merged. | ||
| if ( isset( $_wp_post_type_features[ $post_type ][ $feature ][0] ) | ||
| && is_array( $_wp_post_type_features[ $post_type ][ $feature ][0] ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per below, should this check to make sure it is actually an associative array? I think it should add this check: ! array_is_list( $_wp_post_type_features[ $post_type ][ $feature ][0] )
Previously, calling
add_post_type_support()multiple times for the same post type and feature with array arguments would overwrite the previous arguments instead of merging them. This prevented plugins and themes from incrementally adding sub-properties to the same feature.This changes the behavior to merge associative array arguments when the function is called multiple times with the same post type and feature combination.
Example usage:
The fix maintains full backwards compatibility by only merging when both the existing and new values are associative arrays. All other calling patterns continue to work as before.
Props @fabiankaegy, @Mamaduka, @t-hamano .
Trac ticket: https://core.trac.wordpress.org/ticket/64156#ticket
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.