From e8bae220e6f64c4da38d1d19b3ae9d9de5a756d6 Mon Sep 17 00:00:00 2001
From: Garvin Hicking <blog@garv.in>
Date: Thu, 22 May 2014 11:44:25 +0200
Subject: [PATCH] A proposal on how 2k11 could utilize pre-event hooks. Now
 per-event so that the function name no longer clashes with child-themes that
 need to include the 2k11 config.inc.php.

---
 docs/NEWS                     |  4 ++++
 include/plugin_api.inc.php    | 15 +++++++++++++++
 templates/2k11/config.inc.php | 21 +++++++++------------
 3 files changed, 28 insertions(+), 12 deletions(-)

diff --git a/docs/NEWS b/docs/NEWS
index 1efefe40..30a45b8b 100644
--- a/docs/NEWS
+++ b/docs/NEWS
@@ -4,6 +4,10 @@
 Version 2.0-beta3 ()
 ------------------------------------------------------------------------
 
+    * Changed 2k11's config.inc.php file to provide a more stable
+      call of event hooks so that other templates can also hook
+      their own events.
+      
     * Changed JS for category filtering and its reset button to be a
       reusable function, which is now also used in the list of
       installable plugins.
diff --git a/include/plugin_api.inc.php b/include/plugin_api.inc.php
index dd97ee52..29d57d15 100644
--- a/include/plugin_api.inc.php
+++ b/include/plugin_api.inc.php
@@ -1096,6 +1096,16 @@ class serendipity_plugin_api
             $apifunc($event_name, $bag, $eventData, $addData);
         }
         
+        // Function names cannot contain ":" etc, so if we ever have event looks like "backend:js" this
+        // needs to be replaced to "backend_js". The real event name is passed as a function argument
+        // These specific per-hook functions are utilized for theme's config.inc.php files
+        // that act as an engine for other themes.
+        $safe_event_name = preg_replace('@[^a-z0-9_]+@i', '_', $event_name);
+        if (function_exists('serendipity_plugin_api_pre_event_hook_' . $safe_event_name)) {
+            $apifunc = 'serendipity_plugin_api_pre_event_hook_' . $safe_event_name;
+            $apifunc($event_name, $bag, $eventData, $addData);
+        }
+
         if (is_array($plugins)) {
             // foreach() operates on copies of values, but we want to operate on references, so we use while()
             @reset($plugins);
@@ -1124,6 +1134,11 @@ class serendipity_plugin_api
                 $apifunc($event_name, $bag, $eventData, $addData);
             }
 
+            if (function_exists('serendipity_plugin_api_event_hook_' . $safe_event_name)) {
+                $apifunc = 'serendipity_plugin_api_event_hook_' . $safe_event_name;
+                $apifunc($event_name, $bag, $eventData, $addData);
+            }
+
         }
 
         return true;
diff --git a/templates/2k11/config.inc.php b/templates/2k11/config.inc.php
index 845c2429..fdd4851c 100644
--- a/templates/2k11/config.inc.php
+++ b/templates/2k11/config.inc.php
@@ -105,13 +105,15 @@ $template_global_config = array('navigation' => true);
 $template_loaded_config = serendipity_loadThemeOptions($template_config, $serendipity['smarty_vars']['template_option'], true);
 serendipity_loadGlobalThemeOptions($template_config, $template_loaded_config, $template_global_config);
 
-function serendipity_plugin_api_pre_event_hook($event, &$bag, &$eventData, &$addData) {
-    // Check what Event is coming in, only react to those we want.
-    switch($event) {
-        case 'js':
-            // always add newlines to the end of last element, in case of other plugins using this hook and
-            // always start at line Col 1, to populate the (virtual) serendipity.js file
-            echo "
+// 2k11 shall be a re-usable frontend theme that other templates can inherit (through "Engine: 2k11" in their info.txt)
+// If those themes use a custom config.inc.php file, they may need to declare their own pre-event-hooks. 
+// Since serendipity_plugin_api_pre_event_hook() is the advertised method for template authors to hook into
+// 2k11 cannot declare this on its own. We rather use per-event hook functions now, which templates other than 2k11
+// (or other custom engines) should not use.
+function serendipity_plugin_api_pre_event_hook_js($event, &$bag, &$eventData, &$addData) {
+    // always add newlines to the end of last element, in case of other plugins using this hook and
+    // always start at line Col 1, to populate the (virtual) serendipity.js file
+    echo "
 jQuery(function() { 
     jQuery('input[type=\"url\"]').change(function() {
         if (this.value != '' && ! (this.value.substr(0,7) == 'http://' || this.value.substr(0,8) == 'https://')) {
@@ -119,11 +121,6 @@ jQuery(function() {
         }
     });
 })\n\n";
-            break;
-            
-        return true;
-        break;
-    }
 }
 
 if ($_SESSION['serendipityUseTemplate']) {