Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.x Fix menu location compatibility with WPML #2733

Open
wants to merge 5 commits into
base: 2.x
Choose a base branch
from

Conversation

mcaskill
Copy link
Contributor

@mcaskill mcaskill commented May 3, 2023

Issue

On projects using WPML and that customize the Menu class via the timber/menu/classmap filter, the custom classes won't be used because WPML only translates get_nav_menu_locations() (theme_mod_nav_menu_locations) in specific use cases such as during the filtering of wp_nav_menu_args and when we are in the Admin.

The (translated) menu's term ID won't match the (default language) menu ID assigned to the same location.

This affects MenuFactory and Menu.

Solution

Since the issue affects multiple classes, and based on suggestions:

  • Added a callback on the theme_mod_nav_menu_locations hook in WpmlIntegration to translate the menus mapped to locations.
  • Added methods get_menu_location() and get_menu_locations() to Timber for convenient usage by MenuFactory and Menu.

Impact

Adding an extra hook could affect performance but I should be minor.

Translating the menu IDs of menu locations (theme_mod_nav_menu_locations) could have a more noticeable impact on performance but should also be minor.

Usage Changes

Externally, developers can take advantage of the added hook to fix WPML's edge case.

Timber and developers can rely on the new methods to easily retrieve filtered nav menu locations.

Testing

Unit tests have not added for Timber, nor has the unit tests for MenuFactory, Menu, and WpmlIntegration been updated.

With the custom wpml_object_id_filter() function declared in the tests bootstrap, adding/updating tests will require more time than I have at the moment.

This pull request's functionality has been tested on a client project that has many menu locations and customizes the menus extensively.

@gchtr gchtr added 2.0 Ready for Review Ready for a contrib to take a look at and review/merge labels May 12, 2023
@gchtr gchtr mentioned this pull request May 17, 2023
30 tasks
@nlemoine
Copy link
Member

Hello @mcaskill,

I have mixed feelings about adding another helper class. I'm actually trying to get rid of them.

I don't use WPML, isn't there a way to use WordPress core filters to handle this? get_theme_locations is just a shortcut to get_theme_mod which contains a filter that could do the exact same thing.

Could you give it a try?

@mcaskill
Copy link
Contributor Author

Yeah, I was reluctant to adding another helper class but couldn't think of a quicker solution at the time.

I'll try to refactor this in the next month or so. There should indeed be a way to hook into the theme_mod filter from WpmlIntegration to avoid a helper class.

@gchtr
Copy link
Member

gchtr commented May 23, 2023

It would be great if we could find an easier way to fix this.

Moreover, I think that a fix for this doesn’t have to make it into the first 2.0 release. It looks like we can still solve this with a minor release. To me, it looks like if we find the right filter, this can be fixed without having to introduce a breaking change to Timber. That’s why I’m changing the label to 2.x-Future.

@gchtr gchtr added 2.x Future and removed 2.0 Ready for Review Ready for a contrib to take a look at and review/merge labels May 23, 2023
@mcaskill mcaskill force-pushed the 2.x-wpml-nav-menu-locations branch from 5f3bd19 to 54b8516 Compare June 8, 2023 19:01
@mcaskill
Copy link
Contributor Author

mcaskill commented Jun 8, 2023

I've updated the initial pull request description to reflect the latest proposal.

The one outstanding portion of this proposal is the menu location retrieval methods added to Timber, whether they are necessary or not.

Removed from `MenuFactory` in timber/timber#c59b90e
And allow for shared hooks for customization and third-party integrations.

Helpers:
- `get_menu_location()` to find a location given a menu.
- `get_menu_locations()` to retrieve all locations and menus.

Filters:
- "timber/menu_helper/menu_locations" to filter the locations and menus.

Replaced usage of `get_nav_menu_locations()` with `MenuHelper` equivalents in `Menu` and `MenuFactory`.
Added a callback on the 'timber/menu_helper/menu_locations' hook to translate the menus mapped to locations.

Without this filter, any attempts to retrieve a menu's location will return `null` preventing one from customizing Timber's `Menu` class.

WPML only translates `get_nav_menu_locations()` in specific use cases such as when its filtering `wp_nav_menu_args` and when we are in the Admin.
Replaced callback on the proposed 'timber/menu_helper/menu_locations' hook with the 'theme_mod_nav_menu_locations' hook to translate the menus mapped to locations.

Without this filter, any attempts to retrieve a menu's location will return `null` preventing one from customizing Timber's `Menu` class.

WPML only translates `get_nav_menu_locations()` in specific use cases such as when its filtering `wp_nav_menu_args` and when we are in the Admin.
Changed:
- Moved methods from `MenuHelper` to `Timber` and replaced occurrences of predecessor.
- Removed filter "timber/menu_helper/menu_locations".
@mcaskill mcaskill force-pushed the 2.x-wpml-nav-menu-locations branch from 54b8516 to 7214059 Compare August 2, 2023 19:12
@Levdbas Levdbas added 2.0 and removed 2.x Future labels Dec 13, 2023
@Levdbas Levdbas added this to the 2.0.1 milestone Dec 14, 2023
@Levdbas Levdbas modified the milestones: 2.0.1, 2.1.0 Feb 1, 2024
@Levdbas Levdbas modified the milestones: 2.1.0, 2.2.0 Apr 10, 2024
@Levdbas Levdbas modified the milestones: 2.2.0, 2.3.0 May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants