Opened 3 years ago
Last modified 20 months ago
#54028 new defect (bug)
Fix improper use of the hooks API
Reported by: | azaozz | Owned by: | |
---|---|---|---|
Milestone: | Future Release | Priority: | normal |
Severity: | normal | Version: | 5.8 |
Component: | General | Keywords: | has-patch |
Focuses: | docs | Cc: |
Description
Using lambda functions as callbacks for add_action()
and add_filter()
is not supported as it breaks part of the WP Hooks API. It prevents the removal of such callbacks, remove_action()
and remove_filter()
stop working.
It seems there are few cases of such breakage in core:
- https://github.com/WordPress/wordpress-develop/blob/master/src/wp-admin/edit-form-blocks.php#L32
- https://github.com/WordPress/wordpress-develop/blob/master/src/wp-admin/options-privacy.php#L21
- https://github.com/WordPress/wordpress-develop/blob/master/src/wp-admin/privacy-policy-guide.php#L20
- https://github.com/WordPress/wordpress-develop/blob/master/src/wp-includes/script-loader.php#L2448
- https://github.com/WordPress/wordpress-develop/blob/master/src/wp-includes/sitemaps/class-wp-sitemaps-renderer.php#L254
(and possibly other).
Generally hooks should be added to either wp-admin/includes/admin-filters.php
or wp-includes/default-filters.php
as much as possible. It's also acceptable to add them from functions or "template" files. However hooks should never be added from "include" files (for security reasons), and lambda callbacks are not acceptable.
Attachments (1)
Change History (16)
This ticket was mentioned in PR #1634 on WordPress/wordpress-develop by donmhico.
3 years ago
#2
- Keywords has-patch added; needs-patch removed
This PR converts the lambda functions that adds body classes in the following pages:
- wp-admin/post-new.php and wp-admin/post-new.php?post_type=page - https://github.com/WordPress/wordpress-develop/blob/master/src/wp-admin/edit-form-blocks.php#L32
- wp-admin/options-privacy.php - https://github.com/WordPress/wordpress-develop/blob/master/src/wp-admin/options-privacy.php#L21
- wp-admin/options-privacy.php?tab=policyguide https://github.com/WordPress/wordpress-develop/blob/master/src/wp-admin/privacy-policy-guide.php#L20
New filters are as follows
body_class_edit_form_blocks
body_class_options_privacy
Trac ticket: https://core.trac.wordpress.org/ticket/54028
#3
@
3 years ago
Nice catch @azaozz!
I've converted the filters in the following files:
- https://github.com/WordPress/wordpress-develop/blob/master/src/wp-admin/edit-form-blocks.php#L32
- https://github.com/WordPress/wordpress-develop/blob/master/src/wp-admin/options-privacy.php#L21
- https://github.com/WordPress/wordpress-develop/blob/master/src/wp-admin/privacy-policy-guide.php#L20
I'll try to work on the other files as time permits.
#5
@
3 years ago
- Milestone changed from 5.9 to 6.0
With 5.9 Beta 1 happening in less than 4 hours, moving this ticket to 6.0.
#6
@
3 years ago
@donmhico I have time to finish up this ticket if that's okay with you.
I've found a few other instances that need to be updated as well.
Here's the list I have so far (including instances mentioned above)
add_filter()
https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-admin/edit-form-blocks.php#L32
https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-admin/options-privacy.php#L21
https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-admin/privacy-policy-guide.php#L20
https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/script-loader.php#L2460
https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/sitemaps/class-wp-sitemaps-renderer.php#L254
(This one is in a docblock, but it's a bad example)
https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/theme.php#L1999
add_action()
https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/block-supports/layout.php#L194
https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/block-supports/elements.php#L76
#8
@
3 years ago
Here's one more instance of lambda function hooked to add_action
: https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/block-supports/duotone.php#L459-L467
This ticket was mentioned in PR #2401 on WordPress/wordpress-develop by johnregan3.
3 years ago
#9
Trac ticket: https://core.trac.wordpress.org/ticket/54028
Fixes/updates hooks using lambda functions.
The list below is taken from the trac ticket.
Note that the two unaddressed issues below are lambda functions using use
to pass data into the filter, and it would take serious work to correct these issues that may be beyond the scope of this ticket, which I believe is generally to "clean things up." I'm definitely open to advice on how to approach fixing these.
add_filter()
- x [wp-admin/edit-form-blocks.php#L36]
- x [wp-admin/poptions-privacy.php#L21]
- x [wp-admin/privacy-policy-guide.php#L20]
- https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/script-loader.php#L2492 [wp-includes/script-loader.php#L2492]
- x [wp-includes/sitemaps/class-wp-sitemaps-renderer.php#L254]
Bad example in docblock
add_action()
- https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/block-supports/duotone.php#L550 [wp-includes/block-supports/duotone.php#L550]
Mentioned in ticket, but no longer valid
Props to @donmhico and their PR.
#11
@
3 years ago
I requested changes on a few Docblocks issues in the above PR, otherwise the patch looks good to me
This ticket was mentioned in Slack in #core by costdev. View the logs.
2 years ago
#13
@
2 years ago
- Milestone changed from 6.0 to Future Release
Per the discussion in the bug scrub, I'm moving this ticket to Future Release as this still needs architectural decisions before it can proceed.
Related: #51506, #49264, #53616, #50117
Would probably be good to add some docs explaining how things work to avoid this in the future.