From e8ae17d4aefb0159a784f6a47ae6374c1fd8a126 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Tue, 28 Jul 2015 13:32:18 +0100 Subject: [PATCH] Enhance sidebar display logic 7 bugfixes --- README.md | 3 + core.php | 87 +++++++++++++---------- index.php | 141 ++++++++++++++++++++++++++++---------- module_index.json | 8 +++ modules/extra-sidebar.php | 51 ++++++++++++++ settings.fragment.php | 2 +- 6 files changed, 220 insertions(+), 72 deletions(-) create mode 100644 modules/extra-sidebar.php diff --git a/README.md b/README.md index 217cd52..182893d 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,9 @@ th { text-align: left; } * Allow users to change their passwords * Add auto updating system that doesn't wipe your settings and modules * Make links to non existent pages red + * Optional module support (modules that aren't selected by default in the downloader & builder) + * Add an optional sidebar (as a module maybe?) + * Add redirect pages (as a module?) * .... (open an issue if you have any suggestions!) --Starbeamrainbowlabs diff --git a/core.php b/core.php index e819d54..e01af41 100644 --- a/core.php +++ b/core.php @@ -360,48 +360,65 @@ class page_renderer

Timed at {generation-date}

Powered by Pepperminty Wiki.

"; + // An array of functions that have been registered to process the + // find / replace array before the page is rendered. Note that the function + // should take a *reference* to an array as its only argument. + protected static $part_processors = []; + + // Registers a function as a part post processor. + public static function register_part_preprocessor($function) + { + // Make sure that the function we are about to register is valid + if(!is_callable($function)) + { + http_response_code(500); + $admin_name = $settings->admindetails["name"]; + $admin_email = hide_email($settings->admindetails["email"]); + exit(page_renderer::render("$settings->sitename - Module Error", "

$settings->sitename has got a misbehaving module installed that tried to register an invalid HTML handler with the page renderer. Please contact $settings->sitename's administrator $admin_name at $admin_email.")); + } + + self::$part_processors[] = $function; + + return true; + } + public static function render($title, $content, $body_template = false) { global $settings, $start_time; if($body_template === false) - $body_template = page_renderer::$main_content_template; + $body_template = self::$main_content_template; + + $parts = [ + "{body}" => $body_template, + + "{sitename}" => $settings->sitename, + "{favicon-url}" => $settings->favicon, + "{header-html}" => self::get_css_as_html(), + + "{navigation-bar}" => self::render_navigation_bar($settings->nav_links, $settings->nav_links_extra, "top"), + "{navigation-bar-bottom}" => self::render_navigation_bar($settings->nav_links_bottom, [], "bottom"), + + "{admin-details-name}" => $settings->admindetails["name"], + "{admin-details-email}" => $settings->admindetails["email"], + + "{admins-name-list}" => implode(", ", $settings->admins), + + "{generation-date}" => date("l jS \of F Y \a\\t h:ia T"), + + "{all-pages-datalist}" => self::generate_all_pages_datalist() + ]; + + // Pass the parts through the part processors + foreach(self::$part_processors as $function) + { + $function($parts); + } $result = self::$html_template; - $result = str_replace("{body}", $body_template, $result); - $result = str_replace([ - "{sitename}", - "{favicon-url}", - "{header-html}", - - "{navigation-bar}", - "{navigation-bar-bottom}", - - "{admin-details-name}", - "{admin-details-email}", - - "{admins-name-list}", - - "{generation-date}", - - "{all-pages-datalist}" - ], [ - $settings->sitename, - $settings->favicon, - self::get_css_as_html(), - - self::render_navigation_bar($settings->nav_links, $settings->nav_links_extra, "top"), - self::render_navigation_bar($settings->nav_links_bottom, [], "bottom"), - - $settings->admindetails["name"], - $settings->admindetails["email"], - - implode(", ", $settings->admins), - - date("l jS \of F Y \a\\t h:ia T"), - - self::generate_all_pages_datalist() - ], $result); + $result = str_replace("{body}", $parts["{body}"], $result); + + $result = str_replace(array_keys($parts), array_values($parts), $result); $result = str_replace([ "{title}", diff --git a/index.php b/index.php index 3c8b37b..3171e4e 100644 --- a/index.php +++ b/index.php @@ -166,7 +166,7 @@ textarea { width: calc(100% - 2rem); min-height: 35rem; font-size: 1.25rem; } textarea ~ input[type=submit] { width: calc(100% - 0.3rem); margin: 0.5rem 0.8rem; padding: 0.5rem; font-weight: bolder; } footer { padding: 2rem; } -"; +/* #ffdb6d #36962c */"; // A url that points to the favicon you want to use for your wiki. By default // this is set to a data: url of a Peppermint. @@ -568,48 +568,65 @@ class page_renderer

Timed at {generation-date}

Powered by Pepperminty Wiki.

"; + // An array of functions that have been registered to process the + // find / replace array before the page is rendered. Note that the function + // should take a *reference* to an array as its only argument. + protected static $part_processors = []; + + // Registers a function as a part post processor. + public static function register_part_preprocessor($function) + { + // Make sure that the function we are about to register is valid + if(!is_callable($function)) + { + http_response_code(500); + $admin_name = $settings->admindetails["name"]; + $admin_email = hide_email($settings->admindetails["email"]); + exit(page_renderer::render("$settings->sitename - Module Error", "

$settings->sitename has got a misbehaving module installed that tried to register an invalid HTML handler with the page renderer. Please contact $settings->sitename's administrator $admin_name at $admin_email.")); + } + + self::$part_processors[] = $function; + + return true; + } + public static function render($title, $content, $body_template = false) { global $settings, $start_time; if($body_template === false) - $body_template = page_renderer::$main_content_template; + $body_template = self::$main_content_template; + + $parts = [ + "{body}" => $body_template, + + "{sitename}" => $settings->sitename, + "{favicon-url}" => $settings->favicon, + "{header-html}" => self::get_css_as_html(), + + "{navigation-bar}" => self::render_navigation_bar($settings->nav_links, $settings->nav_links_extra, "top"), + "{navigation-bar-bottom}" => self::render_navigation_bar($settings->nav_links_bottom, [], "bottom"), + + "{admin-details-name}" => $settings->admindetails["name"], + "{admin-details-email}" => $settings->admindetails["email"], + + "{admins-name-list}" => implode(", ", $settings->admins), + + "{generation-date}" => date("l jS \of F Y \a\\t h:ia T"), + + "{all-pages-datalist}" => self::generate_all_pages_datalist() + ]; + + // Pass the parts through the part processors + foreach(self::$part_processors as $function) + { + $function($parts); + } $result = self::$html_template; - $result = str_replace("{body}", $body_template, $result); - $result = str_replace([ - "{sitename}", - "{favicon-url}", - "{header-html}", - - "{navigation-bar}", - "{navigation-bar-bottom}", - - "{admin-details-name}", - "{admin-details-email}", - - "{admins-name-list}", - - "{generation-date}", - - "{all-pages-datalist}" - ], [ - $settings->sitename, - $settings->favicon, - self::get_css_as_html(), - - self::render_navigation_bar($settings->nav_links, $settings->nav_links_extra, "top"), - self::render_navigation_bar($settings->nav_links_bottom, [], "bottom"), - - $settings->admindetails["name"], - $settings->admindetails["email"], - - implode(", ", $settings->admins), - - date("l jS \of F Y \a\\t h:ia T"), - - self::generate_all_pages_datalist() - ], $result); + $result = str_replace("{body}", $parts["{body}"], $result); + + $result = str_replace(array_keys($parts), array_values($parts), $result); $result = str_replace([ "{title}", @@ -800,6 +817,58 @@ register_module([ +register_module([ + "name" => "Sidebar", + "version" => "0.1", + "author" => "Starbeamrainbowlabs", + "description" => "", + "id" => "extra-sidebar", + "code" => function() { + $show_sidebar = false; + + // Show the sidebar if it is enabled in the settings + if(isset($settings->sidebar_show) && $settings->sidebar_show === true) + $show_sidebar = true; + + // Also show and persist the sidebar if the special GET parameter + // sidebar is seet + if(!$show_sidebar && isset($_GET["sidebar"])) + { + $show_sidebar = true; + // Set a cookie to persist the display of the sidebar + setcookie("sidebar_show", "true", 60 * 60 * 24 * 30); + } + + // Show the sidebar if the cookie is set + if(!$show_sidebar && isset($_COOKIE["sidebar_show"])) + $show_sidebar = true; + + // Delete the cookie and hide the sidebar if the special GET paramter + // nosidebar is set + if(isset($_GET["nosidebar"])) + { + $show_sidebar = false; + unset($_COOKIE["sidebar_show"]); + setcookie("sidebar_show", null, time() - 3600); + } + + page_renderer::register_part_preprocessor(function(&$parts) use ($show_sidebar) { + global $settings; + + if($show_sidebar) + { + // Show the sidebar + $sidebar_contents = "Testing"; + $parts["{body}"] = "

+
" . $parts["{body}"] . "
"; + } + }); + } +]); + + + + register_module([ "name" => "Credits", "version" => "0.6", diff --git a/module_index.json b/module_index.json index 8bd1b64..e8771fc 100644 --- a/module_index.json +++ b/module_index.json @@ -7,6 +7,14 @@ "id": "action-hash", "lastupdate": 1432497591 }, + { + "name": "Sidebar", + "version": "0.1", + "author": "Starbeamrainbowlabs", + "description": "", + "id": "extra-sidebar", + "lastupdate": 1438079423 + }, { "name": "Credits", "version": "0.6", diff --git a/modules/extra-sidebar.php b/modules/extra-sidebar.php new file mode 100644 index 0000000..16c9ef1 --- /dev/null +++ b/modules/extra-sidebar.php @@ -0,0 +1,51 @@ + "Sidebar", + "version" => "0.1", + "author" => "Starbeamrainbowlabs", + "description" => "", + "id" => "extra-sidebar", + "code" => function() { + $show_sidebar = false; + + // Show the sidebar if it is enabled in the settings + if(isset($settings->sidebar_show) && $settings->sidebar_show === true) + $show_sidebar = true; + + // Also show and persist the sidebar if the special GET parameter + // sidebar is seet + if(!$show_sidebar && isset($_GET["sidebar"])) + { + $show_sidebar = true; + // Set a cookie to persist the display of the sidebar + setcookie("sidebar_show", "true", 60 * 60 * 24 * 30); + } + + // Show the sidebar if the cookie is set + if(!$show_sidebar && isset($_COOKIE["sidebar_show"])) + $show_sidebar = true; + + // Delete the cookie and hide the sidebar if the special GET paramter + // nosidebar is set + if(isset($_GET["nosidebar"])) + { + $show_sidebar = false; + unset($_COOKIE["sidebar_show"]); + setcookie("sidebar_show", null, time() - 3600); + } + + page_renderer::register_part_preprocessor(function(&$parts) use ($show_sidebar) { + global $settings; + + if($show_sidebar) + { + // Show the sidebar + $sidebar_contents = "Testing"; + $parts["{body}"] = " +
" . $parts["{body}"] . "
"; + } + }); + } +]); + +?> diff --git a/settings.fragment.php b/settings.fragment.php index 288066a..e468e14 100644 --- a/settings.fragment.php +++ b/settings.fragment.php @@ -163,7 +163,7 @@ textarea { width: calc(100% - 2rem); min-height: 35rem; font-size: 1.25rem; } textarea ~ input[type=submit] { width: calc(100% - 0.3rem); margin: 0.5rem 0.8rem; padding: 0.5rem; font-weight: bolder; } footer { padding: 2rem; } -"; +/* #ffdb6d #36962c */"; // A url that points to the favicon you want to use for your wiki. By default // this is set to a data: url of a Peppermint.