Skip to content

Instantly share code, notes, and snippets.

@daronspence
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save daronspence/73e06e037f699ca771dd to your computer and use it in GitHub Desktop.

Select an option

Save daronspence/73e06e037f699ca771dd to your computer and use it in GitHub Desktop.
Remove actions/filters from anonymous class instance
// All of your classes in /acf/forms/ are called anonymously
// so it's near impossible to filter them out without this jargon. :(
// example of won't work, but should
remove_filter('in_widget_form', array('acf_form_widget', 'edit_widget'), 10 );
// See http://wordpress.stackexchange.com/questions/137688/remove-actions-filters-added-via-anonymous-functions
function acfw_remove_object_filter( $tag, $class, $method = NULL, $priority = NULL ) {
$filters = $GLOBALS['wp_filter'][ $tag ];
if ( empty ( $filters ) ) {
return;
}
foreach ( $filters as $p => $filter ) :
if ( ! is_null($priority) && ( (int) $priority !== (int) $p ) ) continue;
$remove = FALSE;
foreach ( $filter as $identifier => $function ) {
$function = $function['function'];
if ( is_array( $function )
&& ( is_a( $function[0], $class ) || ( is_array( $function )
&& $function[0] === $class ) ) ) :
$remove = ( $method && ( $method === $function[1] ) );
elseif ( $function instanceof Closure && $class === 'Closure' ) :
$remove = TRUE;
endif;
if ( $remove ) {
unset( $GLOBALS['wp_filter'][$tag][$p][$identifier] );
}
}
endforeach;
}
@elliotcondon
Copy link
Copy Markdown

Hey mate,

Just to clarify, the issue is that the form files (forms/widget.php) instantiates the class, but doesn't assign it to an available variable?

I'm guessing you are trying to unhook one of the filters/actions from the class.

ACF has quite a few of these 'forms' classes, not to mention 'core', 'admin', etc...

The reason I call them anonymously is to avoid adding them all the $acf object, which would become large, and may become a performance burden? I'm assuming this, and haven't tested it.

@elliotcondon
Copy link
Copy Markdown

What do you think would be a good solution for the issue?

  • Add all the classes to the $acf object as variables. eg: $acf->form_widget
  • Perhaps add them to the $GLOBALS array?

@elliotcondon
Copy link
Copy Markdown

Just had a look through the woocommerce plugin to see if they had a good solution, but they are doing the same as ACF...

@elliotcondon
Copy link
Copy Markdown

I'm guessing this is todo with the widget customizer bug you emailed me about. I'll be reviewing and debugging the problem tomorrow, so hopefully I've got a solution for it shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment