Make WordPress Core

Opened 3 years ago

Closed 2 years ago

Last modified 2 years ago

#23265 closed enhancement (fixed)

Canonical callback representation

Reported by: scribu Owned by: nacin
Milestone: 3.6 Priority: normal
Severity: normal Version:
Component: Plugins Keywords: has-patch
Focuses: Cc:

Description (last modified by scribu)

Since PHP 5.3, you have various ways of passing the same callback:

add_filter( 'the_content', array( 'My_Plugin_Main_Class', 'the_content' ) );

add_filter( 'the_content', 'My_Plugin_Main_Class::the_content' );

add_filter( 'the_content', '\\My_Plugin_Main_Class::the_content' );

Currently, you have to use the exact same syntax in remove_action(), or it won't work.

It would be nice if _wp_filter_build_unique_id() converted all these variations to a canonical representation.

Sprung out of #23259

Attachments (2)

23265.tests.diff (594 bytes) - added by scribu 3 years ago.
23265.diff (385 bytes) - added by scribu 3 years ago.

Download all attachments as: .zip

Change History (13)

comment:2 @scribu3 years ago

  • Description modified (diff)

comment:3 follow-up: @toscho3 years ago

  • Cc info@… added

Another variant:

add_filter( 'the_content', ['Class_Name', 'static_callback_method'] );

But that should be equal to your first example.

comment:4 @scribu3 years ago

It seems the string syntax was added even earlier:

// Type 4: Static class method call (As of PHP 5.2.3)

From http://php.net/manual/en/language.types.callable.php

@scribu3 years ago

@scribu3 years ago

comment:5 @scribu3 years ago

  • Keywords has-patch added
  • Milestone changed from Awaiting Review to 3.6

23265.diff fixes the most common case and has 23265.tests.diff to go with it.

comment:6 @wonderboymusic3 years ago

I looked at the same-ish thing here #21267

Here was my patch:

Main problem is closures

comment:7 in reply to: ↑ 3 @rmccue3 years ago

Replying to toscho:

Another variant:

add_filter( 'the_content', ['Class_Name', 'static_callback_method'] );

But that should be equal to your first example.

Yep, that's just shorthand for an array in 5.4+.

With closures, they're pretty much going to have to be assigned to a variable to do anything with them. The only thing I can think of doing is involving where they were defined via ReflectionFunction, but that seems a little horrible.

comment:8 @ryan2 years ago

  • Milestone changed from 3.6 to Future Release

comment:9 @nacin2 years ago

  • Owner set to nacin
  • Resolution set to fixed
  • Status changed from new to closed

In 24251:

Add a delimiter to _wp_filter_build_unique_id()'s handling of static method callbacks. props scribu, fixes #23265.

comment:10 @SergeyBiryukov2 years ago

  • Milestone changed from Future Release to 3.6

comment:11 @nacin2 years ago

In 1294/tests:

Tests for canonical callback representation. props scribu, see #23265.

Note: See TracTickets for help on using tickets.