-
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.
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 should do that. currently, get_metadata( 'site', 1, 'site_url' )
in a single site, would result false, as it should. It is start returning the option, than that could be considered a breaking change.
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 don't think it's a breaking change to return another value - the function is expected to return anything, so returning anything is not a breaking change. If you argue that way, even using get_network_option()
here at all would be a breaking change.
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 don't think it's a breaking change to return another value - the function is expected to return anything, so returning anything is not a breaking change. If you argue that way, even using
get_network_option()
here at all would be a breaking change.
I am confused by this feedback. Currently if I called get_metadata( 'site', 1, 'site_url' )
, what happens if is try to load data from the sitemeta table. If table doesn't exist, it will result false. If we use get_network_option
it will load from option the response of the function call goes from false to option value. That is a different and breaking change for the code.
_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(); |
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.
If we do this, it would a breaking change. If for some reason you were using network options to store multiple values in the same key, the other keys that become in accessible for this change. My hope is that the doing it wrong, will tell developers to stop doing this and we can change this in the future.
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.
This would only be a breaking change if someone was actually using this to store multiple values - which I doubt is common. Probably the most common usage of the metadata functions for network options is via WP-CLI.
I'm on board with not changing the return value here for now to first only warn developers about this. But the warning should only appear if there are actually multiple values stored. The problem is not that someone is calling this function with $single = false
- the problem is when there are multiple values stored for what should be a single network option.
So we would need to conditionally trigger a warning via another filter, once the value has been retrieved, and only if it's multiple values.
Even more important would be to flag this in the filter callback for add_metadata
: If there's already a value stored and you call this function, it should not be allowed to store multiple values.
But here we need basically the same warning, in order to account for data that was already stored before this change.
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.
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 really want to push home to developers, that if you are doing this, you are doing this wrong. But I still want the code to work. If developers know what they are doing and don't care, they can always unhook these filters and do use the metadata api as is.
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 agree with you that some of these warnings make sense. But using the metadata functions for network options is not necessarily wrong. It may not be recommended, but for the most part it works.
There is only one thing that is wrong: Nobody should be able to store multiple meta values for a single meta key in sitemeta
, since it's intended for network options, and those are restricted to one value per key. So I think this is the only aspect where developers would be doing something critically wrong and should be warned about.
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.
It IS wrong, as caches ( specially if external object caches ) values for options are not updated / created / delete. It also does not run through the existing filters and actions for network options. I think there are serious issues and should be pushing developers to not do this.
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 ); |
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 disagree, that the doing it wrong messages are not needed. I think we want to deprecate the miss use of metadata api for network options.
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.
I think should do that. currently, get_metadata( 'site', 1, 'site_url' )
in a single site, would result false, as it should. It is start returning the option, than that could be considered a breaking change.
_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.
If we do this, it would a breaking change. If for some reason you were using network options to store multiple values in the same key, the other keys that become in accessible for this change. My hope is that the doing it wrong, will tell developers to stop doing this and we can change this in the future.
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.
I really want to push home to developers, that if you are doing this, you are doing this wrong. But I still want the code to work. If developers know what they are doing and don't care, they can always unhook these filters and do use the metadata api as is.
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.