WordPress.org

Make WordPress Core

Opened 15 months ago

Closed 11 months ago

Last modified 10 months 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 15 months ago.
23265.diff (385 bytes) - added by scribu 15 months ago.

Download all attachments as: .zip

Change History (13)

comment:2 scribu15 months ago

  • Description modified (diff)

comment:3 follow-up: toscho15 months 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 scribu15 months 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

scribu15 months ago

scribu15 months ago

comment:5 scribu15 months 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 wonderboymusic15 months 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

comment:7 in reply to: ↑ 3 rmccue15 months 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 ryan11 months ago

  • Milestone changed from 3.6 to Future Release

comment:9 nacin11 months 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 SergeyBiryukov11 months ago

  • Milestone changed from Future Release to 3.6

comment:11 nacin10 months ago

In 1294/tests:

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

Note: See TracTickets for help on using tickets.