Check taxonomy registration before showing category/tag panels#112
Check taxonomy registration before showing category/tag panels#112
Conversation
Use `is_object_in_taxonomy()` to verify the category and tag taxonomies are registered for the post type before checking capabilities or querying terms. Previously, `get_taxonomy()` returned the taxonomy object even after `unregister_taxonomy_for_object_type()`, so the panels would still appear. Fixes #111
There was a problem hiding this comment.
Pull request overview
This PR fixes Press This sidebar taxonomy panels incorrectly appearing when the category and/or post_tag taxonomies are no longer registered for the active post type (e.g., via unregister_taxonomy_for_object_type()), by ensuring the backend only reports capabilities and term data when the taxonomy is actually associated with the post type.
Changes:
- Gate taxonomy capability checks behind
is_object_in_taxonomy()for the chosen$post_type. - Skip the
get_categories()query entirely whencategoryisn’t registered for the post type. - Ensure
canAssignCategories,canEditCategories, andcanAssignTagsreflect taxonomy registration state, allowing existing JS guards to hide panels correctly.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Get taxonomy capabilities (only if the taxonomy is registered for this post type). | ||
| $categories_tax = is_object_in_taxonomy( $post_type, 'category' ) ? get_taxonomy( 'category' ) : false; | ||
| $tag_tax = is_object_in_taxonomy( $post_type, 'post_tag' ) ? get_taxonomy( 'post_tag' ) : false; | ||
| $can_assign_cats = $categories_tax && current_user_can( $categories_tax->cap->assign_terms ); | ||
| $can_edit_cats = $categories_tax && current_user_can( $categories_tax->cap->edit_terms ); | ||
| $can_assign_tags = $tag_tax && current_user_can( $tag_tax->cap->assign_terms ); |
There was a problem hiding this comment.
Test coverage: this change fixes a regression where unregister_taxonomy_for_object_type() should hide Tags/Categories panels by making canAssign* false and skipping category term loading. There are PHP unit tests in tests/php/, but none currently exercise WP_Press_This_Plugin::html() output for this scenario; please add a regression test that unregisters post_tag/category for the selected post type and asserts the generated pressThisData has canAssignTags/canAssignCategories false and categories empty when appropriate.
Verify that pressThisData reflects taxonomy registration state: - categories data is populated when category taxonomy is registered - canAssignTags is false when post_tag is unregistered - canAssignCategories, canEditCategories, and categories are empty when category is unregistered - All taxonomy caps are false and categories empty when both taxonomies are unregistered Fixes #111
Summary
is_object_in_taxonomy()to verify category and tag taxonomies are registered for the post type before checking capabilities or querying termsget_categories()query entirely when the category taxonomy isn't registeredunregister_taxonomy_for_object_type( 'post_tag', 'post' )but the Tags panel still appearsNo JS changes needed — the existing
canAssignCategories,canAssignTags, andcategories.lengthguards already handle the frontend correctly once the PHP passes the right values.Fixes #111
See https://wordpress.org/support/topic/issues-with-the-updated-press-this/
Test plan
unregister_taxonomy_for_object_type( 'post_tag', 'post' )and verify the Tags panel is hiddenunregister_taxonomy_for_object_type( 'category', 'post' )and verify the Categories panel is hiddenpress_this_post_typefilter that has neither taxonomy registered