Experiment: Sync user taxonomies with post types#77997
Conversation
|
Size Change: +3 B (0%) Total Size: 7.95 MB 📦 View Changed
ℹ️ View Unchanged
|
|
Flaky tests detected in dc848a5. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/25438241800
|
tyxla
left a comment
There was a problem hiding this comment.
Nice architectural change, I appreciate the single source of truth 💯
Creation, editing, deletion, changing slugs works great in my testing.
One thing that this PR doesn't cover and we might want to fix (before or after landing it) is the handling of deactivated entities. Consider this scenario:
- Create a "Product Category" taxonomy
- Create a "Product" post type, and select "Product Category" for taxonomies
- Deactivate the "Product Category" taxonomy.
- Go to edit the "Product" post type.
- You'll have issues saving, because the inactive taxonomy will still be selected, but not be considered a valid one for saving.
Also left a few more suggestions/questions, let me know what you think!
| : array(); | ||
| $linked = array(); | ||
| foreach ( self::get_user_taxonomy_ids_with_meta( $item->post_name ) as $tax_id ) { | ||
| $tax_slug = (string) get_post_field( 'post_name', $tax_id ); |
There was a problem hiding this comment.
For each post type item we'll now call:
- one
get_postswithmeta_query(get_user_taxonomy_ids_with_meta) - one
get_post_field('post_name')per linked taxonomy (each is a separate cache/DB hit)
Could be heavy and a potential area for optimization.
There was a problem hiding this comment.
I actually tried using a static cache and decided against it for now. Reasons:
- Adds complexity about flushing the cache in places and keeping in sync
- Storage model might change completely so it's better to keep things simple for now.
There was a problem hiding this comment.
Sounds good to start with. It's just somethng to keep in mind - maybe to explicitly list in the performance concerns/ideas to revisit in the tracking issue.
| * | ||
| * @return array<string, int> | ||
| */ | ||
| private static function get_user_taxonomy_ids_by_slug() { |
There was a problem hiding this comment.
Not for this PR, but just a note: any logic that uses this will need to be double-checked and thoroughly tested once we start planning expansion over existing post types and taxonomies.
|
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. |
53b78e8 to
dc848a5
Compare
Good catch. I'm pretty sure we'll encounter more nuances with the activate/deactivate functionality in other places too.. That said, this is already an issue in trunk and the fix is client side with a few possible approaches. I'll add it to the tracking issue and I think we can land this for now. What do you think? |
tyxla
left a comment
There was a problem hiding this comment.
LGTM, let's go 👍
I agree that the active/inactive scenario should be handled separately.
🚀
| if ( 'wp_user_taxonomy' !== get_post_type( $tax_post_id ) ) { | ||
| return; | ||
| } | ||
| $clean = is_string( $object_type ) ? sanitize_key( $object_type ) : ''; | ||
| if ( '' === $clean ) { | ||
| return; | ||
| } |
There was a problem hiding this comment.
This could be a useful separate helper now that we use it both when attaching and detaching.
There was a problem hiding this comment.
Agreed it could be, but I think we can keep it as is for now. My rationale is that these functions are going to become global (if stabilized) and having a util for those might not worth it. We can revisit of course later.
There was a problem hiding this comment.
Agreed. There are TONS of opportunities to remove duplication between the post types and taxonomies code. Something to polish at a later point. 🚀
Co-authored-by: ntsekouras <ntsekouras@git.wordpress.org> Co-authored-by: tyxla <tyxla@git.wordpress.org>
What?
Part of: #77600
This PR handles the syncing between user post types and taxonomies.
Storage split for
config.taxonomiesThe post type's
config.taxonomiesfield is split across two storage sites:post_content)._wp_user_taxonomy_object_typemeta, which is the single source of truth for the post-type↔user-taxonomy relationship.The client only ever sees a single
config.taxonomiesfield — the controller splits writes back into the two sites and rejoins them on read. Adding a user-taxonomy from the post-types form and adding the same post type from the user-taxonomies form converge on the same meta, so updating either side stays in sync.Slug rename migration
When a post type's slug changes (PUT with
slug), every_wp_user_taxonomy_object_typemeta row pointing at the old slug is rewritten to the new one. Without this, every linked user taxonomy would silently disconnect on rename.Relationship helpers live outside the REST controllers
The post-type↔taxonomy relationship is mediated by module-level helpers in
lib/experimental/content-types/index.php(gutenberg_user_taxonomy_attach_object_type/_detach_object_type/_replace_object_types), not inside either controller.Both controllers need to read and write the same
_wp_user_taxonomy_object_typemeta to keep the two entities in sync. That's a domain concern, not a REST concern — keeping it in shared helpers means the storage shape, sanitization (sanitize_key), andpost_type_existsfiltering live in one place, and either side can be invoked outside the REST flow.Same factoring as WordPress's template helpers (
_build_block_template_result_from_post,_inject_theme_attribute_in_template_part_block) which are called from both REST and non-REST code paths.Testing instructions
album). Confirm it appears in the list.genre) and select thealbumpost type in its associated post types.album—genreshould now appear in its taxonomies list.album→record). Confirm:configin the request body) also migrates correctly.Use of AI Tools
Opus 4.7 with direction, changes and review.