Skip to content

Add custom taxonomy support to Press This panel#150

Open
faisalahammad wants to merge 1 commit into
WordPress:trunkfrom
faisalahammad:enhancement/9-custom-taxonomies
Open

Add custom taxonomy support to Press This panel#150
faisalahammad wants to merge 1 commit into
WordPress:trunkfrom
faisalahammad:enhancement/9-custom-taxonomies

Conversation

@faisalahammad

Copy link
Copy Markdown

Summary

Custom taxonomies registered for the post type now show up in the Press This panel, alongside Categories and Tags. Previously only Category and Tags were ever shown, even if a site had other taxonomies registered.
Fixes #9

Changes

class-wp-press-this-plugin.php

Before:

$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;
// ... only category/post_tag data ever built for the panel

After:

$custom_taxonomies_data = $this->get_custom_taxonomies_data( $post_type );
// new method enumerates get_object_taxonomies(), excludes category/post_tag,
// filters to show_ui + assign_terms capability, bootstraps terms for hierarchical taxonomies

Why: Category and Tags keep their own dedicated panels, but every other registered taxonomy is now enumerated and sent to the editor, gated the same way Category/Tags already were (registration + capability).

press-this-plugin.php

Before:

// REST /save only ever accepted 'categories' and 'tags' params

After:

'tax_input' => array( 'type' => 'object', 'default' => array() ),
// save handler validates each taxonomy is registered for the post type, has show_ui,
// and the user can assign_terms, before merging into $post_data['tax_input']

Why: Needed a generic way to save terms for an arbitrary taxonomy, with the same gating applied on save as on read, so a taxonomy hidden from the panel can't be written to by crafting a request by hand.

src/components/TaxonomyPanel.js (new)

New component that renders one panel per custom taxonomy: a checkbox tree for hierarchical taxonomies (same interaction as the existing Category panel), or a token field for flat ones (same interaction as Tags).

src/components/PressThisEditor.js, src/App.js

Wired the new taxonomies data through, added taxonomyTerms state per taxonomy, included it in the save payload.

Testing

Test 1: Hierarchical custom taxonomy

  1. Register a hierarchical taxonomy for post (e.g. genre) with some terms.
  2. Open Press This.
  3. Select a term in the new Genre panel and save as draft.
    Result: post saved with the selected term.

Test 2: Flat custom taxonomy

  1. Register a flat taxonomy for post (e.g. rating).
  2. Open Press This, type a new term into the Rating panel, save.
    Result: term is created and assigned to the post.

Test 3: Gating

  1. Register a taxonomy with show_ui => false, or as a user without assign_terms capability for a taxonomy.
    Result: panel doesn't show, and a hand-crafted REST request with that taxonomy in tax_input is silently ignored (no error, term not saved).

Test 4: Category/Tags unaffected

  1. Use Press This as before with no custom taxonomies registered.
    Result: Category and Tags panels behave exactly as before.

Ran the full PHPUnit and Jest suites plus PHPCS/ESLint, all green.

Custom taxonomies registered for the post type now show up in the
Press This sidebar, next to Categories and Tags. Hierarchical
taxonomies get a checkbox tree like Categories, flat ones get a
token field with autocomplete like Tags.

Only taxonomies with show_ui enabled and that the current user can
assign terms for are shown, and the same checks apply again on save
so a request can't sneak terms in for a taxonomy that was never
exposed to the panel. Category and post_tag keep going through their
existing dedicated handling either way.

Added a press_this_taxonomies filter so sites can add or remove
taxonomies from the panel.

Fixes WordPress#9
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.

Support custom taxonomies

1 participant