From 55f1d7b90e180b4609a4473399ea1b23912dfcfa Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Thu, 20 Sep 2018 21:01:05 -0400 Subject: [PATCH] Add new footer hook - Add new App->footerScripts array - Add footer.tpl template - Add documentation - Rework App->init_page_end to App->initFooter --- doc/Addons.md | 6 +++++ index.php | 38 +--------------------------- src/App.php | 53 +++++++++++++++++++++++++++++++++------ view/templates/footer.tpl | 3 +++ 4 files changed, 56 insertions(+), 44 deletions(-) create mode 100644 view/templates/footer.tpl diff --git a/doc/Addons.md b/doc/Addons.md index ec413c6ac8..b126fedc4d 100644 --- a/doc/Addons.md +++ b/doc/Addons.md @@ -286,6 +286,11 @@ No hook data. Called after HTML content functions have completed. `$b` is (string) HTML of content div. +### footer +Called after HTML content functions have completed. +`$b` is (string) HTML of footer div/element. +Used to load deferred Javascript files. + ### avatar_lookup Called when looking up the avatar. `$b` is an array: @@ -563,6 +568,7 @@ Here is a complete list of all hook callbacks with file locations (as of 01-Apr- ### src/App.php Addon::callHooks('load_config'); + Addon::callHooks('footer'); ### src/Model/Item.php diff --git a/index.php b/index.php index 8b0bd47251..aa7704930e 100644 --- a/index.php +++ b/index.php @@ -153,10 +153,6 @@ if (! x($_SESSION, 'authenticated')) { header('X-Account-Management-Status: none'); } -/* set up page['htmlhead'] and page['end'] for the modules to use */ -$a->page['htmlhead'] = ''; -$a->page['end'] = ''; - $_SESSION['sysmsg'] = defaults($_SESSION, 'sysmsg' , []); $_SESSION['sysmsg_info'] = defaults($_SESSION, 'sysmsg_info' , []); $_SESSION['last_updated'] = defaults($_SESSION, 'last_updated', []); @@ -326,10 +322,6 @@ if (file_exists($theme_info_file)) { /* initialise content region */ -if (! x($a->page, 'content')) { - $a->page['content'] = ''; -} - if ($a->mode == App::MODE_NORMAL) { Addon::callHooks('page_content_top', $a->page['content']); } @@ -411,18 +403,7 @@ $a->init_pagehead(); * Build the page ending -- this is stuff that goes right before * the closing tag */ -$a->init_page_end(); - -// If you're just visiting, let javascript take you home -if (x($_SESSION, 'visitor_home')) { - $homebase = $_SESSION['visitor_home']; -} elseif (local_user()) { - $homebase = 'profile/' . $a->user['nickname']; -} - -if (isset($homebase)) { - $a->page['content'] .= ''; -} +$a->initFooter(); /* * now that we've been through the module content, see if the page reported @@ -444,23 +425,6 @@ if ($a->module != 'install' && $a->module != 'maintenance') { Nav::build($a); } -/* - * Add a "toggle mobile" link if we're using a mobile device - */ -if ($a->is_mobile || $a->is_tablet) { - if (isset($_SESSION['show-mobile']) && !$_SESSION['show-mobile']) { - $link = 'toggle_mobile?address=' . curPageURL(); - } else { - $link = 'toggle_mobile?off=1&address=' . curPageURL(); - } - $a->page['footer'] = replace_macros( - get_markup_template("toggle_mobile_footer.tpl"), - [ - '$toggle_link' => $link, - '$toggle_text' => L10n::t('toggle mobile')] - ); -} - /** * Build the page - now that we have all the components */ diff --git a/src/App.php b/src/App.php index 2a5fba8541..1a26dd6507 100644 --- a/src/App.php +++ b/src/App.php @@ -96,6 +96,15 @@ class App public $force_max_items = 0; public $theme_events_in_profile = true; + public $footerScripts = []; + + public function registerFooterScript($path) + { + $url = str_replace($this->get_basepath() . DIRECTORY_SEPARATOR, '', $path); + + $this->footerScripts[] = $this->get_baseurl() . '/' . trim($url, '/'); + } + /** * @brief An array for all theme-controllable parameters * @@ -802,15 +811,45 @@ class App ]) . $this->page['htmlhead']; } - public function init_page_end() + public function initFooter() { - if (!isset($this->page['end'])) { - $this->page['end'] = ''; + if (!isset($this->page['footer'])) { + $this->page['footer'] = ''; + } + + // If you're just visiting, let javascript take you home + if (!empty($_SESSION['visitor_home'])) { + $homebase = $_SESSION['visitor_home']; + } elseif (local_user()) { + $homebase = 'profile/' . $a->user['nickname']; + } + + if (isset($homebase)) { + $this->page['footer'] .= '' . "\n"; } - $tpl = get_markup_template('end.tpl'); - $this->page['end'] = replace_macros($tpl, [ - '$baseurl' => $this->get_baseurl() - ]) . $this->page['end']; + + /* + * Add a "toggle mobile" link if we're using a mobile device + */ + if ($this->is_mobile || $this->is_tablet) { + if (isset($_SESSION['show-mobile']) && !$_SESSION['show-mobile']) { + $link = 'toggle_mobile?address=' . curPageURL(); + } else { + $link = 'toggle_mobile?off=1&address=' . curPageURL(); + } + $this->page['footer'] .= replace_macros(get_markup_template("toggle_mobile_footer.tpl"), [ + '$toggle_link' => $link, + '$toggle_text' => Core\L10n::t('toggle mobile') + ]); + } + + Core\Addon::callHooks('footer', $this->page['footer']); + + $tpl = get_markup_template('footer.tpl'); + $this->page['footer'] .= replace_macros($tpl, [ + '$baseurl' => $this->get_baseurl(), + '$footerScripts' => $this->footerScripts, + ]); } public function set_curl_code($code) diff --git a/view/templates/footer.tpl b/view/templates/footer.tpl new file mode 100644 index 0000000000..1b9d700020 --- /dev/null +++ b/view/templates/footer.tpl @@ -0,0 +1,3 @@ +{{foreach $footerScripts as $scriptUrl}} + +{{/foreach}} -- 2.39.5