Skip to content

Check taxonomy registration before showing category/tag panels#112

Open
kraftbj wants to merge 3 commits intotrunkfrom
fix/taxonomy-registration-check-111
Open

Check taxonomy registration before showing category/tag panels#112
kraftbj wants to merge 3 commits intotrunkfrom
fix/taxonomy-registration-check-111

Conversation

@kraftbj
Copy link
Copy Markdown
Collaborator

@kraftbj kraftbj commented Mar 26, 2026

Summary

  • Use is_object_in_taxonomy() to verify category and tag taxonomies are registered for the post type before checking capabilities or querying terms
  • Skip the get_categories() query entirely when the category taxonomy isn't registered
  • Fixes the case where a site calls unregister_taxonomy_for_object_type( 'post_tag', 'post' ) but the Tags panel still appears

No JS changes needed — the existing canAssignCategories, canAssignTags, and categories.length guards 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

  • Confirm categories and tags panels appear normally with default taxonomy registration
  • Add unregister_taxonomy_for_object_type( 'post_tag', 'post' ) and verify the Tags panel is hidden
  • Add unregister_taxonomy_for_object_type( 'category', 'post' ) and verify the Categories panel is hidden
  • Test with a custom post type via press_this_post_type filter that has neither taxonomy registered

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
@kraftbj kraftbj added this to the 2.0.3 milestone Mar 26, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 when category isn’t registered for the post type.
  • Ensure canAssignCategories, canEditCategories, and canAssignTags reflect 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.

Comment on lines +1512 to +1517
// 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 );
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
@kraftbj kraftbj self-assigned this Mar 26, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Categories and Tags panels show even when taxonomies are unregistered for the post type

2 participants