-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
61467 - Filter metadata api for site ( network ) meta. #8161
base: trunk
Are you sure you want to change the base?
Conversation
Introduce filters for get, update, add, and delete site metadata, specific to multisite installations. These functions extend metadata API compatibility while marking their use as deprecated in favor of network options functions. Warnings are added to guide developers toward preferred alternatives.
The $unique parameter was passed but not used in add_network_option. This change cleans up the function call for consistency and clarity. It aligns with the intended usage of add_network_option.
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.
@spacedmonkey I like the overall idea behind this, routing the site
metadata function calls through to the corresponding *_network_option()
function. However, I think we can improve the approach a little:
- I think all the warnings and messages related to deprecation are unnecessary.
- We should always either use the corresponding
*_network_option()
function or return a specific short-circuit value, but we should never return$current
here, because that would create inconsistent behavior where sometimes the regular metadata logic would still run.
if ( ! is_multisite() ) { | ||
_doing_it_wrong( 'get_metadata', __( 'This function is only available in multisite installations.' ), '6.8.0' ); | ||
return false; | ||
} |
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.
On non-multisite, get_network_option()
still works and falls through to get_option()
. So it would make sense to me to align with that. We could remove this condition since get_network_option()
already handles it.
_doing_it_wrong( 'get_metadata', __( 'The $single parameter is not supported in this function.' ), '6.8.0' ); | ||
return $current; |
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.
While it is correct that ! $single
does not work when using with network options, I don't think we should allow the function to pass through when $single
is false
. I think we have two options here:
- Either return an empty array because it's not valid.
- Or return an array with just the option value as the sole element.
Here is an example for the 2nd option:
_doing_it_wrong( 'get_metadata', __( 'The $single parameter is not supported in this function.' ), '6.8.0' ); | |
return $current; | |
$option_value = get_network_option( $object_id, $meta_key, false ); | |
if ( false !== $option_value ) { | |
return array( $option_value ); | |
} | |
return array(); |
return $current; | ||
} | ||
|
||
_doing_it_wrong( 'get_metadata', __( 'This function is deprecated. Use get_network_option() instead.' ), '6.8.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 my overarching comment, I don't think all those warnings and deprecation messages are needed. We can make this work without them.
if ( ! is_multisite() ) { | ||
_doing_it_wrong( 'update_metadata', __( 'This function is only available in multisite installations.' ), '6.8.0' ); | ||
return false; | ||
} | ||
|
||
_doing_it_wrong( 'update_metadata', __( 'This function is deprecated. Use update_network_option() instead.' ), '6.8.0' ); | ||
|
||
return update_network_option( $object_id, $meta_key, $meta_value ); |
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.
See above, I think this could simply become:
if ( ! is_multisite() ) { | |
_doing_it_wrong( 'update_metadata', __( 'This function is only available in multisite installations.' ), '6.8.0' ); | |
return false; | |
} | |
_doing_it_wrong( 'update_metadata', __( 'This function is deprecated. Use update_network_option() instead.' ), '6.8.0' ); | |
return update_network_option( $object_id, $meta_key, $meta_value ); | |
return update_network_option( $object_id, $meta_key, $meta_value ); |
if ( ! is_multisite() ) { | ||
_doing_it_wrong( 'add_metadata', __( 'This function is only available in multisite installations.' ), '6.8.0' ); | ||
return false; | ||
} |
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.
See above: We can remove this check and call add_network_option()
as it works outside of Multisite too.
if ( ! $unique ) { | ||
_doing_it_wrong( 'get_metadata', __( 'The $unique parameter is not supported in this function.' ), '6.8.0' ); | ||
return $current; | ||
} |
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.
I think we can make this a bit safer: If $unique
is false
, this is only a problem if there is already a value for that option. Also, we shouldn't return $current
as that would allow the function to proceed with its regular metadata logic, which would not be aligned with the changes this PR otherwise makes.
For example we could do this:
if ( ! $unique ) { | |
_doing_it_wrong( 'get_metadata', __( 'The $unique parameter is not supported in this function.' ), '6.8.0' ); | |
return $current; | |
} | |
if ( ! $unique ) { | |
$option_value = get_network_option( $object_id, $meta_key, false ); | |
if ( false !== $option_value ) { | |
return false; | |
} | |
} |
return $current; | ||
} | ||
|
||
_doing_it_wrong( 'add_metadata', __( 'This function is deprecated. Use add_network_option() instead.' ), '6.8.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.
See above, these deprecation warnings are not necessary, and they would be confusing because those functions are actually not deprecated.
if ( ! is_multisite() ) { | ||
_doing_it_wrong( 'delete_metadata', __( 'This function is only available in multisite installations.' ), '6.8.0' ); | ||
return false; | ||
} | ||
|
||
if ( $delete_all ) { | ||
_doing_it_wrong( 'delete_metadata', __( 'The $delete_all parameter is not supported in this function.' ), '6.8.0' ); | ||
return $current; | ||
} | ||
|
||
if ( $meta_value ) { | ||
_doing_it_wrong( 'delete_metadata', __( 'The $meta_value parameter is not supported in this function.' ), '6.8.0' ); | ||
return $current; | ||
} | ||
|
||
_doing_it_wrong( 'delete_metadata', __( 'This function is deprecated. Use delete_network_option() instead.' ), '6.8.0' ); | ||
|
||
return delete_network_option( $object_id, $meta_key ); |
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.
Based on the above feedback, this can be simplified to:
if ( ! is_multisite() ) { | |
_doing_it_wrong( 'delete_metadata', __( 'This function is only available in multisite installations.' ), '6.8.0' ); | |
return false; | |
} | |
if ( $delete_all ) { | |
_doing_it_wrong( 'delete_metadata', __( 'The $delete_all parameter is not supported in this function.' ), '6.8.0' ); | |
return $current; | |
} | |
if ( $meta_value ) { | |
_doing_it_wrong( 'delete_metadata', __( 'The $meta_value parameter is not supported in this function.' ), '6.8.0' ); | |
return $current; | |
} | |
_doing_it_wrong( 'delete_metadata', __( 'This function is deprecated. Use delete_network_option() instead.' ), '6.8.0' ); | |
return delete_network_option( $object_id, $meta_key ); | |
if ( $delete_all ) { | |
return false; | |
} | |
if ( $meta_value ) { | |
$option_value = get_network_option( $object_id, $meta_key, false ); | |
if ( $meta_value !== $option_value ) { | |
return false; | |
} | |
} | |
return delete_network_option( $object_id, $meta_key ); |
Add filters for site metadata handling in multisite setups
Introduce filters for get, update, add, and delete site metadata, specific to multisite installations. These functions extend metadata API compatibility while marking their use as deprecated in favor of network options functions. Warnings are added to guide developers toward preferred alternatives.
Trac ticket: https://core.trac.wordpress.org/ticket/61467
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.