Permalink
Browse files

feature(hooks): a new function to clear all callbacks for a given hook

fixes #5972
  • Loading branch information...
jdalsem committed Jun 26, 2015
1 parent dea29b2 commit bd5117636c901ea47e07a9f7cefce3ae5854cccd
@@ -3,11 +3,11 @@
/**
* WARNING: API IN FLUX. DO NOT USE DIRECTLY.
*
*
* Use the elgg_* versions instead.
*
*
* @access private
*
*
* @package Elgg.Core
* @subpackage Hooks
* @since 1.9.0
@@ -110,6 +110,19 @@ public function unregisterHandler($name, $type, $callback) {
return false;
}
/**
* Clears all handlers for a specific hook
*
* @param string $name
* @param string $type
*
* @return void
* @access private
*/
public function clearHandlers($name, $type) {
unset($this->registrations[$name][$type]);
}
/**
* Returns all registered handlers as array(
@@ -121,7 +134,7 @@ public function unregisterHandler($name, $type, $callback) {
* )
* )
* )
*
*
* @access private
* @return array
*/
@@ -142,7 +155,7 @@ public function getAllHandlers() {
/**
* Does the hook have a handler?
*
*
* @param string $name The name of the hook
* @param string $type The type of the hook
* @return boolean
View
@@ -725,6 +725,19 @@ function elgg_unregister_plugin_hook_handler($hook, $entity_type, $callback) {
_elgg_services()->hooks->unregisterHandler($hook, $entity_type, $callback);
}
/**
* Clears all callback registrations for a plugin hook.
*
* @param string $hook The name of the hook
* @param string $type The type of the hook
*
* @return void
* @since 2.0
*/
function elgg_clear_plugin_hook_handlers($hook, $type) {
_elgg_services()->hooks->clearHandlers($hook, $type);
}
/**
* Trigger a Plugin Hook and run all handler callbacks registered to that hook:type.
*
@@ -72,6 +72,27 @@ public function testCanUnregisterHandlers() {
// check unregistering things that aren't registered
$this->assertFalse($this->mock->unregisterHandler('foo', 'bar', 'not_valid'));
}
public function testCanClearMultipleHandlersAtOnce() {
$o = new HooksRegistrationServiceTest_invokable();
$this->mock->registerHandler('foo', 'bar', 'callback1');
$this->mock->registerHandler('foo', 'baz', 'callback1', 10);
$this->mock->registerHandler('foo', 'bar', 'callback2', 100);
$this->mock->registerHandler('foo', 'bar', 'callback2', 150);
$expected = [
'foo' => [
'baz' => [
10 => ['callback1'],
]
]
];
// clearHandlers should remove everything registrered for 'foo', 'bar', but not 'foo', 'baz'
$this->mock->clearHandlers('foo', 'bar');
$this->assertSame($expected, $this->mock->getAllHandlers());
}
public function testOnlyOneHandlerUnregistered() {
$this->mock->registerHandler('foo', 'bar', 'callback');

0 comments on commit bd51176

Please sign in to comment.