Make WordPress Core

Opened 12 years ago

Closed 12 years ago

Last modified 11 years ago

#23265 closed enhancement (fixed)

Canonical callback representation

Reported by: scribu's profile scribu Owned by: nacin's profile 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 12 years ago.
23265.diff (385 bytes) - added by scribu 12 years ago.

Download all attachments as: .zip

Change History (13)

#2 @scribu
12 years ago

  • Description modified (diff)

#3 follow-up: @toscho
12 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.

#4 @scribu
12 years ago

It seems the string syntax was added even earlier:

// Type 4: Static class method call (As of PHP 5.2.3)
call_user_func('MyClass::myCallbackMethod');

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

@scribu
12 years ago

@scribu
12 years ago

#5 @scribu
12 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.

#6 @wonderboymusic
12 years ago

I looked at the same-ish thing here #21267

Here was my patch:
https://core.trac.wordpress.org/attachment/ticket/21267/filter-callback-keys.diff

Main problem is closures

#7 in reply to: ↑ 3 @rmccue
12 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.

#8 @ryan
12 years ago

  • Milestone changed from 3.6 to Future Release

#9 @nacin
12 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.

#10 @SergeyBiryukov
12 years ago

  • Milestone changed from Future Release to 3.6

#11 @nacin
11 years ago

In 1294/tests:

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

Note: See TracTickets for help on using tickets.