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.
This commit is contained in:
Garvin Hicking 2014-05-22 11:44:25 +02:00
parent f96f0da3f8
commit e8bae220e6
3 changed files with 28 additions and 12 deletions

View File

@ -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.

View File

@ -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;

View File

@ -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']) {