Repository navigation
Content Types: Add checkbox for deletion of terms - #78629
im3dabasia wants to merge 2 commits into
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
ntsekouras
left a comment
There was a problem hiding this comment.
Thanks for the PR! I've left some initial comments to iterate.
| $response = parent::delete_item( $request ); | ||
| $taxonomy_slug = $existing instanceof WP_Post ? $existing->post_name : ''; | ||
|
|
||
| // Delete all terms belonging to this taxonomy before removing the |
There was a problem hiding this comment.
If we deleted the terms and then the taxonomy deletion failed, it would be a way bigger problem than the current one (orphaned terms).
| // Delete all terms belonging to this taxonomy before removing the | ||
| // record, so no orphaned rows are left in wp_terms / wp_term_taxonomy. | ||
| // Note: only active (published) taxonomies are registered in-memory; | ||
| // terms under inactive taxonomies are not cleaned up here. |
There was a problem hiding this comment.
In order to address this feature properly, we should also handle the inactive entities, even though the active/inactive feature might also be removed later..
Maybe a way for this would be to to register on the fly the inactive taxonomy in order to properly delete terms? 🤔
| // record, so no orphaned rows are left in wp_terms / wp_term_taxonomy. | ||
| // Note: only active (published) taxonomies are registered in-memory; | ||
| // terms under inactive taxonomies are not cleaned up here. | ||
| if ( $request['delete_terms'] && '' !== $taxonomy_slug && taxonomy_exists( $taxonomy_slug ) ) { |
There was a problem hiding this comment.
Any request param (e.g. delete_terms) handled by the controller should be properly declared as one in the controller. We shouldn't read from $request like that.
| @@ -150,6 +154,16 @@ function DeleteTaxonomyModal( { | |||
| items[ 0 ].title.raw | |||
| ) } | |||
| </Text> | |||
| <CheckboxControl | |||
There was a problem hiding this comment.
Looks like we're not clear in the interface that this only applies to active taxonomies. Perhaps this checkbox should only appear for active taxonomies?
| // record, so no orphaned rows are left in wp_terms / wp_term_taxonomy. | ||
| // Note: only active (published) taxonomies are registered in-memory; | ||
| // terms under inactive taxonomies are not cleaned up here. | ||
| if ( $request['delete_terms'] && '' !== $taxonomy_slug && taxonomy_exists( $taxonomy_slug ) ) { |
There was a problem hiding this comment.
Hmm, so we're limiting deletion of terms just to active taxonomies. But what happens to the terms of taxonomies that were once active and were deactivated later? Won't these remain orphans forever as well? How are we handling them?
| // record, so no orphaned rows are left in wp_terms / wp_term_taxonomy. | ||
| // Note: only active (published) taxonomies are registered in-memory; | ||
| // terms under inactive taxonomies are not cleaned up here. | ||
| if ( $request['delete_terms'] && '' !== $taxonomy_slug && taxonomy_exists( $taxonomy_slug ) ) { |
There was a problem hiding this comment.
Could be useful to have some tests for the deletion of terms, especially ones that handle various cases (active/inactive taxonomies for example).
| ); | ||
| if ( is_array( $term_ids ) ) { | ||
| foreach ( $term_ids as $term_id ) { | ||
| wp_delete_term( (int) $term_id, $taxonomy_slug ); |
There was a problem hiding this comment.
How are we handling term deletion errors?
| // record, so no orphaned rows are left in wp_terms / wp_term_taxonomy. | ||
| // Note: only active (published) taxonomies are registered in-memory; | ||
| // terms under inactive taxonomies are not cleaned up here. | ||
| if ( $request['delete_terms'] && '' !== $taxonomy_slug && taxonomy_exists( $taxonomy_slug ) ) { |
There was a problem hiding this comment.
Also it doesn't seem like we're handling permissions correctly, specifically: can the current user delete terms? The endpoint will only check if the user can delete these posts (wp_user_taxonomy) but not terms from that taxonomy - it needs a delete_terms or similar cap check and should most likely be handled at the controller level.
| @@ -150,6 +154,16 @@ function DeleteTaxonomyModal( { | |||
| items[ 0 ].title.raw | |||
| ) } | |||
| </Text> | |||
| <CheckboxControl | |||
There was a problem hiding this comment.
Also we don't seem to be checking if the current user is authorized to delete terms from these taxonomies.
| ); | ||
| if ( is_array( $term_ids ) ) { | ||
| foreach ( $term_ids as $term_id ) { | ||
| wp_delete_term( (int) $term_id, $taxonomy_slug ); |
There was a problem hiding this comment.
Also imagine this is deleting thousands of terms. With all term and post hooks and cache cleanup that fire here, this might be disastrous - might make sense to consider batching. Another alternative is doing it asynchronously outside of the main request (a wp cron job or something), since it may not necessarily make sense to block the main request.
| { force: true }, | ||
| { | ||
| force: true, | ||
| ...( deleteTerms ? { delete_terms: true } : {} ), |
There was a problem hiding this comment.
This may be tricky. Maybe the user didn't mean to delete terms for all taxonomies that they're deleting? Are we clear enough that this is happening?
| ? __( 'Also delete all terms in these taxonomies' ) | ||
| : __( 'Also delete all terms in this taxonomy' ) |
There was a problem hiding this comment.
This is actually a pretty destructive checkbox. Should we think about how to make this a bit more "dangerous" and find a way for the user to double-confirm that this is the intent?
|
Thanks for your work! I'm closing though since the experiment was removed here: #81340. |
What?
Part of #77600
This PR adds a checkbox to delete terms associated with a taxonomy when the taxonomy/taxonomies are deleted in the UI.
Why?
Cleans up database with orphan terms in the db.
How?
Introduced
delete_termsa extra key in the API, which when passed assumes we want to delete the terms in the DB .Testing Instructions
Screenshots or screencast
Before
Screen.Recording.2026-05-25.at.5.50.35.PM.mov
After
Screen.Recording.2026-05-25.at.5.47.45.PM.mov