Make WordPress Core

Opened 8 years ago

Closed 11 months ago

#38116 closed enhancement (wontfix)

Introduce is_callable check into apply_filters function to avoid failure of call_user_func_array causing all filters to fail.

Reported by: garrett-eclipse's profile garrett-eclipse Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Plugins Keywords: has-patch
Focuses: Cc:

Description

Hello,

Just wanted to suggest an improvement for the apply_filters function to avoid failures introduced by custom code or plugins that invalidly set an add_filter without providing the scope or misspelling the function.

Currently if a filter is applied incorrectly... in the case I came across I had a custom plugin which I missed wrapping the array($this,) around the function name. This caused an error in debug for call_user_func_array expecting valid callback. Due to this NULL is returned and all other filters are basically ignored.

Would be nice to do an is_callable check before execution of the call_user_func_array so as to skip any broken add_filters applied. I'd say still log an error so people can troubleshoot and find out why their filter failed, but would be nicer than returning null.

Thank you

Change History (9)

#1 @desrosj
6 years ago

  • Component changed from General to Plugins

#2 follow-up: @SergeyBiryukov
5 years ago

#49002 was marked as a duplicate.

#3 in reply to: ↑ 2 @sjoerdlinders
5 years ago

Replying to SergeyBiryukov:

#49002 was marked as a duplicate.

With the solution to solve the problem in file: class-wp-hooks.php

public function apply_filters( $value, $args ) {
    ...
    foreach ( $this->callbacks[ $priority ] as $the_ ) {
        if( ! $this->doing_action ) {
            $args[ 0 ] = $value;
        }

        // Check if callable function or method exists.
        if ( !isset( $the_['function'] ) || !is_callable( $the_['function'] ) ) continue;

        ....
    }
Last edited 5 years ago by sjoerdlinders (previous) (diff)

#4 @johnbillion
5 years ago

  • Keywords needs-patch added
  • Type changed from defect (bug) to enhancement
  • Version 4.6.1 deleted

Silently discarding an invalid callback isn't a good idea because it turns a visible error into an invisible one.

PHP errors exist for a reason and I think this is a valid case for an error. It's unfortunate that subsequent callbacks receive a null value, so if this guard condition is going to be put into place it needs to trigger a PHP error too.

This will need performance profiling too to ensure there's minimal performance impact.

This ticket was mentioned in Slack in #core-php by dlh. View the logs.


4 years ago

This ticket was mentioned in PR #4131 on WordPress/wordpress-develop by @tabrisrp.


22 months ago
#7

  • Keywords has-patch added; needs-patch removed

Add check for the callback function value passed to apply_filters, to determine if the value is empty or not callable. Trigger a _doing_it_wrong() call if an invalid value is passed to make sure the error is not silenced.

Trac ticket: https://core.trac.wordpress.org/ticket/38116

#8 @johnbillion
11 months ago

#60235 was marked as a duplicate.

#9 @johnbillion
11 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

I'm going to make an executive decision and close this for the same reason as #51894. An invalid callback is a clear developer error and should not be ignored. The fact that what used to be a PHP warning is now a fatal error is unfortunate but valid, and it does make it much more visible that a problem has occurred.

Note: See TracTickets for help on using tickets.