Adds check for term before capability check #2230
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description of the Change
The
action_sync_on_object_update
method that's hooked intoset_object_terms
needs to be able to handle an array of term slugs. The reason being is thatwp_set_object_terms()
's second parameter can bestring|int|array
. See docs. When a string is passed,wp_set_object_terms
wraps it in an array, thus becoming an array of slugs that's later passed toset_object_terms
. Using theterm_exists()
function here provides some flexibility since it accepts ID, slug, or name. Without this check, when a slug is passed intowp_set_object_terms()
(Polylang does this when setting a language) it will throw a php Notice "Trying to get property 'term_id' of non-object" in SyncManager.php:110. This can be replicated with Polylang and ElasticPress installed with the Terms Feature enabled. From there, creating a new post will give you a php notice noted above.I don't think there is a current issue files for this but please let me know if I'm incorrect and I'll link the two.
Alternate Designs
NA
Benefits
No php Notices
Possible Drawbacks
Extra database queries in the
term_exists()
function but I think this a cleaner approach than various type checks.Verification Process
I made this change in my local environment and ran several check/tests updating and adding new post content.
Checklist:
Applicable Issues
NA
Changelog Entry
Added a check for term exists to get the term id by slug, id or name before a capabilities check is done.