Skip to content

fix(importer): resolve nav_menu widget term_id from slug hint#458

Merged
selul merged 1 commit intodevelopmentfrom
fix/nav-menu-widget-slug-resolution
Apr 23, 2026
Merged

fix(importer): resolve nav_menu widget term_id from slug hint#458
selul merged 1 commit intodevelopmentfrom
fix/nav-menu-widget-slug-resolution

Conversation

@selul
Copy link
Copy Markdown
Contributor

@selul selul commented Apr 23, 2026

Summary

Nav-menu widgets imported through TPC lose their menu after a cross-site import: the source payload carries nav_menu => <source_term_id> and Slug_Mapping::rewrite_value() only rewrites URL strings, so the integer passes through and wp_nav_menu(['menu' => N]) silently matches nothing. The target sidebar ends up with an empty .widget_nav_menu div.

This PR adds a general pre-import resolution layer for ID-by-reference widget settings:

  • New private Widgets_Importer::resolve_known_references() consumes a _ti_nav_menu_slug hint, looks up the menu on the target site via wp_get_nav_menu_object(), rewrites nav_menu to the freshly-imported term_id, and strips the hint so it never leaks into widget_nav_menu.
  • Unresolved slugs fall through non-fatally (leaves the stale id in place) so a failed menu import never aborts the widgets step.
  • New ti_tpc_widget_pre_import filter exposes the same pipeline to third-party exporters for future ID-by-reference widgets (products, custom taxonomies, etc.).
  • Runs after Slug_Mapping::rewrite_value() and before the "identical settings already exist" dedup, so reruns stay idempotent.

Companion exporter change (stamping _ti_nav_menu_slug) lives in the consumer (Demo Data Exporter). This PR does not introduce any dependency on the exporter - absent the hint, behaviour is unchanged.

Test plan

  • tests/widgets-import-test.php covers:
    • _ti_nav_menu_slug resolves to the target term_id and is stripped.
    • Unresolvable slug keeps the stale id and still strips the hint.
    • ti_tpc_widget_pre_import fires with (widget, id_base, instance_id, sidebar_id) and its mutation is persisted.
  • php -l clean on all touched files.
  • Run existing phpunit suite: vendor/bin/phpunit.
  • Manual: reprovision a Neve-based subsite from an exporter that emits _ti_nav_menu_slug and confirm Footer Two renders the imported menu and widget_nav_menu stores the target term_id with no leftover _ti_nav_menu_slug key.

Made with Cursor

Widget instances exported from another site reference menus by
numeric term_id (e.g. `nav_menu` => 3), which never match on the
target after a fresh import. `Slug_Mapping::rewrite_value()` only
rewrites URL strings, so the integer passes through and
`wp_nav_menu(['menu' => 3])` silently renders nothing.

Add a pre-import resolver that consumes a `_ti_nav_menu_slug` hint
(carried by companion exporters), looks the slug up on the target
site via `wp_get_nav_menu_object()`, rewrites `nav_menu` to the
fresh term_id, and strips the hint so it never persists into
`widget_nav_menu`. Unresolved slugs are non-fatal.

Expose the resolution step as a `ti_tpc_widget_pre_import` filter
so future ID-by-reference widgets (taxonomies, products) can plug
in without further core changes.

Covered by `tests/widgets-import-test.php` with three cases:
successful resolution, unresolved-slug fallback, and filter-hook
invocation.

Made-with: Cursor
@pirate-bot pirate-bot added the pr-checklist-incomplete The Pull Request checklist is incomplete. (automatic label) label Apr 23, 2026
@pirate-bot
Copy link
Copy Markdown
Collaborator

Plugin build for 82d1298 is ready 🛎️!

@selul selul merged commit 9087799 into development Apr 23, 2026
6 of 8 checks passed
@selul selul deleted the fix/nav-menu-widget-slug-resolution branch April 23, 2026 10:28
@pirate-bot
Copy link
Copy Markdown
Collaborator

🎉 This PR is included in version 1.2.27 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

@pirate-bot pirate-bot added the released Indicate that an issue has been resolved and released in a particular version of the product. label Apr 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-checklist-incomplete The Pull Request checklist is incomplete. (automatic label) released Indicate that an issue has been resolved and released in a particular version of the product.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants