From 479af90579e7898ded7ed5de966ab03a3be9300a Mon Sep 17 00:00:00 2001
From: alecpl <alec@alec.pl>
Date: Wed, 07 Mar 2012 05:07:57 -0500
Subject: [PATCH] - Hook unregister methods (#1488184)

---
 program/include/rcube_plugin.php     |   31 ++++++++++++++++++++-----------
 program/include/rcube_plugin_api.php |   14 ++++++++++++++
 2 files changed, 34 insertions(+), 11 deletions(-)

diff --git a/program/include/rcube_plugin.php b/program/include/rcube_plugin.php
index 9864749..aeb05af 100644
--- a/program/include/rcube_plugin.php
+++ b/program/include/rcube_plugin.php
@@ -81,7 +81,7 @@
     $this->home = $api->dir . $this->ID;
     $this->urlbase = $api->url . $this->ID . '/';
   }
-  
+
   /**
    * Initialization method, needs to be implemented by the plugin itself
    */
@@ -117,7 +117,7 @@
         'message' => "Failed to load config from $fpath"), true, false);
       return false;
     }
-    
+
     return true;
   }
 
@@ -131,7 +131,18 @@
   {
     $this->api->register_hook($hook, $callback);
   }
-  
+
+  /**
+   * Unregister a callback function for a specific (server-side) hook.
+   *
+   * @param string $hook Hook name
+   * @param mixed  $callback Callback function as string or array with object reference and method name
+   */
+  public function remove_hook($hook, $callback)
+  {
+    $this->api->unregister_hook($hook, $callback);
+  }
+
   /**
    * Load localized texts from the plugins dir
    *
@@ -141,7 +152,7 @@
   public function add_texts($dir, $add2client = false)
   {
     $domain = $this->ID;
-    
+
     $lang = $_SESSION['language'];
     $locdir = slashify(realpath(slashify($this->home) . $dir));
     $texts = array();
@@ -167,7 +178,7 @@
 
       $rcmail = rcmail::get_instance();
       $rcmail->load_language($lang, $add);
-      
+
       // add labels to client
       if ($add2client) {
         $js_labels = is_array($add2client) ? array_map(array($this, 'label_map_callback'), $add2client) : array_keys($add);
@@ -175,7 +186,7 @@
       }
     }
   }
-  
+
   /**
    * Wrapper for rcmail::gettext() adding the plugin ID as domain
    *
@@ -245,7 +256,7 @@
   {
     $this->api->include_stylesheet($this->resource_url($fn));
   }
-  
+
   /**
    * Append a button to a certain container
    *
@@ -260,11 +271,11 @@
       foreach (array('imagepas', 'imageact', 'imagesel') as $key)
         if ($p[$key])
           $p[$key] = $this->api->url . $this->resource_url($p[$key]);
-      
+
       $this->api->add_content($this->api->output->button($p), $container);
     }
   }
-  
+
   /**
    * Generate an absolute URL to the given resource within the current
    * plugin directory
@@ -315,6 +326,4 @@
     return $this->ID.'.'.$key;
   }
 
-
 }
-
diff --git a/program/include/rcube_plugin_api.php b/program/include/rcube_plugin_api.php
index fbf3584..be12f11 100644
--- a/program/include/rcube_plugin_api.php
+++ b/program/include/rcube_plugin_api.php
@@ -230,6 +230,20 @@
         'message' => "Invalid callback function for $hook"), true, false);
   }
 
+  /**
+   * Allow a plugin object to unregister a callback.
+   *
+   * @param string $hook Hook name
+   * @param mixed  $callback String with global function name or array($obj, 'methodname')
+   */
+  public function unregister_hook($hook, $callback)
+  {
+    $callback_id = array_search($callback, $this->handlers[$hook]);
+    if ($callback_id !== false) {
+      unset($this->handlers[$hook][$callback_id]);
+    }
+  }
+
 
   /**
    * Triggers a plugin hook.

--
Gitblit v1.9.1