Last active
May 3, 2021 13:31
-
-
Save xavier-bs/434770c66269b74de3ef862642e9255c to your computer and use it in GitHub Desktop.
Fonctionnalities for WP >= 4.7 to remove filter/action defined by a callback function, a static or dynamic method from a class,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Function remove_filter_x(): Remove filter/action defined by a callback function, a static or dynamic method from a class | |
* WP version >= 4.7 | |
* @author Xavier Birnie-Scott <xavierbirniescott@yahoo.fr> | |
* @version 1.0 [2020-12-18] Short version | |
* @see https://make.wordpress.org/core/2016/09/08/wp_hook-next-generation-actions-and-filters/ | |
* @see https://murviel-info-beziers.com/supprimer-filtre-action-methode-classe-wordpress/ | |
* @param array $args: [ | |
* 'hook' hook name, mandatory | |
* 'class' class name | |
* 'method' method name | |
* 'function' function name | |
* 'priority' 10 by default if omitted | |
* ] | |
* ($class and $method) or $function is mandatory | |
* @return bool true if success, false otherwise | |
*/ | |
function remove_filter_x( $args ) { | |
$default = [ | |
'hook' => false, | |
'class' => false, | |
'method' => false, | |
'function' => false, | |
'priority' => 10, | |
]; | |
extract( wp_parse_args( $args, $default ) ); | |
global $wp_filter; | |
// Checks if hook is not missing | |
if( ! $hook ) return false; | |
// Checks if it's a valid hook | |
if( empty( $wp_filter[$hook] ) ) return false; | |
// Checks if hook exists with that priority | |
if( empty( $wp_filter[$hook]->callbacks[$priority] ) ) return false; | |
$unset = false; | |
foreach( $wp_filter[$hook]->callbacks[$priority] as $key => $arr ) { | |
// Function | |
if( is_string( $arr['function'] ) ) { | |
if( $function == $arr['function'] ) { | |
$result['function'] = $arr['function']; | |
unset( $wp_filter[$hook]->callbacks[$priority][$key] ); | |
$unset = true; | |
break; | |
} | |
} | |
// Static method | |
else if( is_array( $arr['function'][0] ) ) { | |
if( $arr['function'][0] == $class && $arr['function'][1] == $method ) { | |
unset( $wp_filter[$hook]->callbacks[$priority][$key] ); | |
$unset = true; | |
break; | |
} | |
} | |
// Dynamic method | |
else if( is_object( $arr['function'][0] ) ) { | |
if( get_class( $arr['function'][0] ) == $class && $arr['function'][1] == $method ) { | |
unset( $wp_filter[$hook]->callbacks[$priority][$key] ); | |
$unset = true; | |
break; | |
} | |
} | |
} | |
return $unset; | |
} | |
/** | |
* Function remove_filter_ctrl(): Remove filter/action defined by a callback function, a static or dynamic method from a class, with error control. | |
* WP version >= 4.7 | |
* @author Xavier Birnie-Scott <xavierbirniescott@yahoo.fr> | |
* @version 1.0 [2020-12-18] with error control | |
* @see https://make.wordpress.org/core/2016/09/08/wp_hook-next-generation-actions-and-filters/ | |
* @param array $args: [ | |
* 'hook' hook name, mandatory | |
* 'class' class name | |
* 'method' method name | |
* 'function' function name | |
* 'priority' 10 by default if omitted | |
* 'textdomain' text domain wp default if omitted | |
* ] | |
* ($class and $method) or $function is mandatory | |
* @return WP_Error (invalid or bug with message) | |
* or array ['hook', | |
* 'priority', | |
* 'key', (callback_id) | |
* 'type', (function, static or dynamic) | |
* 'function' or 'class' and 'method'] | |
*/ | |
function remove_filter_ctrl( $args ) { | |
$default = [ | |
'hook' => false, | |
'class' => false, | |
'method' => false, | |
'function' => false, | |
'priority' => 10, | |
'textdomain' => 'default', | |
]; | |
extract( wp_parse_args( $args, $default ) ); | |
global $wp_filter; | |
$error = new WP_Error; | |
// Checks if hook is not missing | |
if( ! $hook ) { | |
return $error->add( 'invalid', __( 'hook property is mandatory', $textdomain ) ); | |
} | |
// Checks if it's a valid hook | |
if( empty( $wp_filter[$hook] ) ) { | |
return $error->add( 'invalid', sprintf( __( '%s hook seems invalid', $textdomain ), $hook ) ); | |
} | |
// Checks if hook exists with that priority | |
if( empty( $wp_filter[$hook]->callbacks[$priority] ) ) { | |
return $error->add( 'priority', sprintf( __( 'There is no %s hook with priority %d', $textdomain ), $hook, $priority ) ); | |
} | |
$type = $unset = false; | |
foreach( $wp_filter[$hook]->callbacks[$priority] as $key => $arr ) { | |
$result['hook'] = $hook; | |
$result['priority'] = $priority; | |
$result['key'] = $key; | |
// Function | |
if( is_string( $arr['function'] ) ) { | |
$result['type'] = 'function'; | |
$type = 'function'; | |
if( $function == $arr['function'] ) { | |
$result['function'] = $arr['function']; | |
unset( $wp_filter[$hook]->callbacks[$priority][$key] ); | |
$unset = true; | |
break; | |
} | |
} | |
// Static method | |
else if( is_array( $arr['function'][0] ) ) { | |
$result['type'] = 'static'; | |
$type = 'static'; | |
if( $arr['function'][0] == $class && $arr['function'][1] == $method ) { | |
$result['class'] = $class; | |
$result['method'] = $method; | |
unset( $wp_filter[$hook]->callbacks[$priority][$key] ); | |
$unset = true; | |
break; | |
} | |
} | |
// Dynamic method | |
else if( is_object( $arr['function'][0] ) ) { | |
$result['type'] = 'dynamic'; | |
$type = 'dynamic'; | |
if( get_class( $arr['function'][0] ) == $class && $arr['function'][1] == $method ) { | |
$result['class'] = $class; | |
$result['method'] = $method; | |
unset( $wp_filter[$hook]->callbacks[$priority][$key] ); | |
$unset = true; | |
break; | |
} | |
} | |
} | |
if( ! $unset ) { | |
if( $type == 'function' ) { | |
return $error->add( 'invalid', sprintf( __( 'The %s function with the priority %d on the %s hook was not found', $textdomain ), $function, $priority, $hook ) ); | |
} | |
else if( $type == 'static' ) { | |
return $error->add( 'invalid', sprintf( __( 'The %s class and the %s static method with the priority %d on the %s hook was not found', $textdomain ), $class, $method, $priority, $hook ) ); | |
} | |
else if( $type == 'dynamic' ) { | |
return $error->add( 'invalid', sprintf( __( 'The %s class and the %s dynamic method with the priority %d on the %s hook was not found', $textdomain ), $class, $method, $priority, $hook ) ); | |
} | |
else { | |
return $error->add( 'bug', sprintf( __( 'The function is not able to remove the %s hook with priority %d for some unknown reason', $textdomain ), $hook, $priority ) ); | |
} | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment