#56577 closed enhancement (fixed)
Add short-circuit filter to `wp_setup_nav_menu_item`
Reported by: | david.binda | Owned by: | azaozz |
---|---|---|---|
Milestone: | 6.3 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Menus | Keywords: | needs-testing-info has-patch commit add-to-field-guide |
Focuses: | Cc: |
Description
While looking into a memory consumption of navigation menus on some blogs with huge menus, I have noticed that to decorate a nav menu item via wp_setup_nav_menu_item
function generates a lot of data.
To decorate a nav menu item of post_type
type, for instance, requires the original object to be fetched in order to grab it's permalink and title only.
However, in case there's a menu consisting of multiple posts and/or pages with large content, the whole original object is much larger than the data which are actually needed.
IMHO, in case of a WordPress install with a persistent object cache backend, it would be beneficial to cache the fully decorated nav menu item (output of the wp_setup_nav_menu_item
) in order to bring that from cache on subsequent requests instead of decorating the nav menu item over and over.
On a site which I've been investigating, the difference in between original objects fetched from memcache in order to decorate the nav menu item vs. obtaining the already decorated and cached nav menu item was 300M vs. 3.19 (yes, the nav menu is huge, consisting of ~1000 items, mostly posts).
However, I don't think that caching the decorated nav menu is something what core should do by default (eg.: it does not make sense w/o persistent object cache backend), so I wonder if it would make sense to add a short-circuit filter to the wp_setup_nav_menu_item
function which would allow plugins to implement the caching layer.
Attachments (4)
Change History (23)
#4
@
20 months ago
- Keywords needs-refresh removed
Removing refresh keyword, as this was addressed with attachment:56577.2.diff.
#5
@
20 months ago
- Keywords needs-unit-tests needs-testing-info added
- Milestone changed from Awaiting Review to 6.3
This filter has a well-defined use case; adding to 6.3 for consideration and further discussion.
It would be great if this patch also included testing steps and unit tests, so I've added those keywords as well.
#6
@
19 months ago
It has been a while since I wrote a unittest for WordPress, but I've made an attempt. Please let me know if I've done it the right way.
#7
@
19 months ago
- Keywords needs-unit-tests removed
56577.2.diff and 56577.unittests.patch look good imho (the second needs some coding standards fixes). Thinking this is ready.
#8
@
19 months ago
- Owner set to azaozz
- Resolution set to fixed
- Status changed from new to closed
In 55867:
#9
@
19 months ago
- Resolution fixed deleted
- Status changed from closed to reopened
Should the filter hook get a @since 6.3.0
?
#10
follow-up:
↓ 11
@
19 months ago
- Resolution set to fixed
- Status changed from reopened to closed
In 55868:
#11
in reply to:
↑ 10
;
follow-up:
↓ 12
@
18 months ago
- Keywords needs-patch 2nd-opinion added; has-patch removed
- Resolution fixed deleted
- Status changed from closed to reopened
Replying to SergeyBiryukov:
Thanks for adding the @since
tag.
Not sure why the description was reverted though. It's not a big deal but generally using the same word (Returning) in different contexts in the same sentence is considered poor usage of the English language. Imho the edited description is better and clearer.
using the
MockAction
class to confirm that the filter runs.
Not sure why the unit test was refactored. Also not sure what is being tested now. The original test was testing the output of the wp_setup_nav_menu_item()
function after the filter was used. The current test doesn't seem to be testing anything?
Think the unit test should be fixed to test the output of wp_setup_nav_menu_item()
after the filter was used.
#12
in reply to:
↑ 11
;
follow-up:
↓ 15
@
18 months ago
Replying to azaozz:
Not sure why the description was reverted though.
The description was adjusted for consistency with all other short-circuit filters in core. There are at least 20 instances of this pattern in core files:
Returning a ... value will (effectively) short-circuit the ..., returning that value instead.
Perhaps it's not ideal and could be improved further, but I find it helpful when documentation uses the same exact sentence structure in the same context.
Not sure why the unit test was refactored. Also not sure what is being tested now.
The test was also adjusted a bit for consistency with existing tests:
Wordpress.org
looks OK intest_orphan_nav_menu_item()
, where it gets replaced withWordPress.org
, but here it seemed unrelated to the purpose of the test and just looked weird to me :)- Newer tests tend to use
MockAction::get_call_count()
orMockAction::get_args()
as a consistent approach to ensure hooks are called in expected places with the expected parameters, instead of attaching callbacks with random data. In this case, I was following [55256] testing thepre_wp_load_alloptions
filter, but this pattern also exists in many other tests.
The original test was testing the output of the
wp_setup_nav_menu_item()
function after the filter was used. The current test doesn't seem to be testing anything?
The current test ensures that wp_setup_nav_menu_item()
applies the pre_wp_setup_nav_menu_item
filter exactly once. I agree though that when testing the output specifically, it might be preferable to use a custom callback instead.
Think the unit test should be fixed to test the output of
wp_setup_nav_menu_item()
after the filter was used.
Fair enough, happy to fix that :)
#13
@
18 months ago
56577.3.diff updates the test to verify the actual output of wp_setup_nav_menu_item()
with the filter applied.
#15
in reply to:
↑ 12
@
18 months ago
- Keywords commit added; 2nd-opinion removed
Replying to SergeyBiryukov:
The description was adjusted for consistency with all other short-circuit filters in core. There are at least 20 instances of this pattern in core files:
Ahh, I see. Makes sense. Thinking we can look at updating/fixing the docs for all of these at some point, seems they can be a bit better.
56577.3.diff looks good imho, thanks for updating the test!
#16
@
18 months ago
Description
DesAdd short-circuit filter to wp_setup_nav_menu_item
Environment
- WordPress: 6.2.2
- PHP: 8.2.0
- Server: Apache/2.4.54 (Unix) mod_fastcgi/mod_fastcgi-SNAP-0910052141 OpenSSL/1.0.2u mod_wsgi/3.5 Python/2.7.18
- Database: mysqli (Server: 5.7.39 / Client: mysqlnd 8.2.0)
- Browser: Chrome 114.0.0.0 (macOS)
- Theme: Twenty Twenty-Three 1.1
- MU-Plugins: None activated
- Plugins:
- WordPress Beta Tester 3.4.1
Steps to Reproduce
- Test 56577.3.diff
Expected Results
- ✅ https://core.trac.wordpress.org/attachment/ticket/56577/56577.3.diff Patch Fixes perfectly
Hi there!
The Patch need to update with document. Properly indent