Make WordPress Core

Opened 10 years ago

Closed 10 years ago

Last modified 9 years ago

#39007 closed feature request (fixed)

Impossible to access current priority with WP_Hook

Reported by: Rarst Owned by: pento
Priority: normal Milestone: 4.7
Component: Plugins Version: 4.7
Severity: normal Keywords: has-patch has-unit-tests commit dev-reviewed
Cc: Focuses:

Description

While there was never a public API for it, the specifics of implementation had previously allowed to access current priority within a hook via key( $wp_filter[ current_filter() ] ).

This seems to no longer be a case with 4.7 implementation based on WP_Hook which both changes what this expression returns (string callbacks) and seems to hide priority data in private property.

Given that implementation both breaks existing way to do it and doesn't open a new way to it breaks all code that needs access to current priority state without alternative.

Attachments (2)

39007.diff (793 bytes ) - added by pento 10 years ago.
39007.2.diff (2.1 KB ) - added by pento 10 years ago.

Download all attachments as: .zip

Change History (13)

#1 follow-up: @dd32
10 years ago

For reference; Do you know of any public projects which have been utilising this?

#2 in reply to: ↑ 1 @Rarst
10 years ago

Replying to dd32:

For reference; Do you know of any public projects which have been utilising this?

My Laps profiler plugin got exploded by this https://github.com/Rarst/laps

#3 @toscho
10 years ago

There are also some often reused snippets on WordPress Stack Exchange, this one for example: How to force function to run as the last one when saving the post?

@pento
10 years ago

#4 @pento
10 years ago

  • Component GeneralPlugins
  • Keywords has-patch reporter-feedback needs-unit-tests added
  • Milestone Awaiting Review4.7
  • Owner set to pento
  • Status newassigned

While this has never really been supported behaviour (particularly with how broken nested hooks were), I don't think it's unreasonable to explicitly allow access to the currently running priority of the current filter.

@Rarst: How do you feel about 39007.diff? To take your example, you'd use it as: $wp_filter[ current_filter() ]->current_priority().

#5 @Rarst
10 years ago

Not sure about implementation, but API seems to make sense. :)

One question is would current_priority() function make sense to complement current_filter() for procedural access? That would make conditional logic less clunky (if have new function just use it, otherwise do old thing).

#6 @pento
10 years ago

  • Keywords reporter-feedback removed

Adding a current_priority() function seems like it's a bit more than is necessary.

For conditional logic, class_exists( 'WP_Hook' ) is a reasonable test, this is what Batcache uses, for example - it's technically not correct for 4.7 betas that don't have the ::current_priority() method, but I don't think that should be a big issue. :-)

@pento
10 years ago

#7 @pento
10 years ago

  • Keywords has-unit-tests commit added; needs-unit-tests removed

39007.2.diff adds unit tests.

#8 @helen
10 years ago

  • Keywords dev-reviewed added

#9 @pento
10 years ago

  • Resolutionfixed
  • Status assignedclosed

In 39430:

Plugins: Add a current_priority() method to WP_Hook.

This allows plugins to determine the currently running priority of a filter.

Fixes #39007.

#10 @pento
10 years ago

In 39431:

Plugins: Add a current_priority() method to WP_Hook.

This allows plugins to determine the currently running priority of a filter.

Merges [39430] to the 4.7 branch.

Fixes #39007.

#11 @SergeyBiryukov
9 years ago

#33886 was marked as a duplicate.

Note: See TracTickets for help on using tickets.