[wp-trac] [WordPress Trac] #57923: Deprecated notice fired by wp_set_object_terms() with PHP 8.1
WordPress Trac
noreply at wordpress.org
Wed Mar 15 12:30:57 UTC 2023
#57923: Deprecated notice fired by wp_set_object_terms() with PHP 8.1
-------------------------------------------+---------------------
Reporter: Chouby | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: 6.3
Component: Taxonomy | Version:
Severity: normal | Resolution:
Keywords: php81 needs-patch 2nd-opinion | Focuses:
-------------------------------------------+---------------------
Comment (by costdev):
Thanks for the extra information @Chouby!
Given that:
- The documentation in "More Information" explicitly states that `null`
can be passed.
- At least 74 that pass `null` for `$terms`, and the documentation
currently mentions passing `null`.
I think that officially accepting `null` is the backward compatible
approach here. The documentation, and loose checking, has somewhat backed
us into a corner here.
Based on this, I think we have a few options:
1. Implicitly accept `null`, by:
a. Adding `null !== $terms` to the condition
[https://github.com/WordPress/WordPress/blob/1bf93a87a449a9476d1efd93a0452d7d6ceb3808
/wp-includes/taxonomy.php#L2728-L2730 here].
i. This would avoid the deprecation notice, as the `foreach()` uses
`(array) $terms`, converting `null` to `array()` just for the `foreach()`
loop.
ii. This would avoid changing the type of `$terms` passed by the
`wp_set_object_terms` action, protecting possible explicit `null` checks
by extenders.
2. Or, explicitly accept `null`, by:
a. Adding `null` to the list of types for `$terms`.
b. Adding `if ( empty( $terms ) ) { $terms = array(); }` before line
2728.
i. This would cover more values than `null` but, as far I know, none
of those values are valid Term IDs/slugs, and it would be consistent with
the current documentation: "Passing an empty value will remove all related
terms."
ii. This would change the type of `$terms` passed by the
`wp_set_object_terms` action, which would now pass `$terms` as `array()`,
instead of the current behaviour of `null`.
3. Or, a mixture of both:
a. Adding `null` to the list of types for `$terms`.
b. Adding `null !== $terms` to the condition
[https://github.com/WordPress/WordPress/blob/1bf93a87a449a9476d1efd93a0452d7d6ceb3808
/wp-includes/taxonomy.php#L2728-L2730 here].
i. This would prevent the deprecation notice, and prevent changing the
type of `$terms` passed by the `wp_set_object_terms` action.
At the moment, I'm leaning towards option 3.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/57923#comment:11>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list