From 2ae6556b3293f20647edeea36b793fda8f65691f Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Thu, 20 Sep 2018 21:30:51 -0400 Subject: [PATCH] Add new 'head' hook - Add new App->registerStylesheet method - Reworked App->init_pagehead into App->initHead --- doc/Addons.md | 8 +++++++- index.php | 2 +- src/App.php | 26 +++++++++++++++++--------- view/templates/head.tpl | 4 ++++ view/theme/frio/templates/head.tpl | 4 ++++ 5 files changed, 33 insertions(+), 11 deletions(-) diff --git a/doc/Addons.md b/doc/Addons.md index b126fedc4..b8364d935 100644 --- a/doc/Addons.md +++ b/doc/Addons.md @@ -252,6 +252,11 @@ Called after conversion of bbcode to HTML. Called after tag conversion of HTML to bbcode (e.g. remote message posting) `$b` is a string converted text +### head +Called when building the `` sections. +Stylesheets should be registered using this hook. +`$b` is an HTML string of the `` tag. + ### page_header Called after building the page navigation section. `$b` is a string HTML of nav region. @@ -288,8 +293,8 @@ Called after HTML content functions have completed. ### footer Called after HTML content functions have completed. +Deferred Javascript files should be registered using this hook. `$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: @@ -568,6 +573,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('head'); Addon::callHooks('footer'); ### src/Model/Item.php diff --git a/index.php b/index.php index 1adfe4ad1..359be7dcb 100644 --- a/index.php +++ b/index.php @@ -395,7 +395,7 @@ if ($a->module_loaded) { * theme choices made by the modules can take effect. */ -$a->init_pagehead(); +$a->initHead(); /* * Build the page ending -- this is stuff that goes right before diff --git a/src/App.php b/src/App.php index 26bc7362f..479813703 100644 --- a/src/App.php +++ b/src/App.php @@ -96,13 +96,21 @@ class App public $force_max_items = 0; public $theme_events_in_profile = true; + public $stylesheets = []; public $footerScripts = []; + public function registerStylesheet($path) + { + $url = str_replace($this->get_basepath() . DIRECTORY_SEPARATOR, '', $path); + + $this->stylesheets[] = trim($url, '/'); + } + public function registerFooterScript($path) { $url = str_replace($this->get_basepath() . DIRECTORY_SEPARATOR, '', $path); - $this->footerScripts[] = $this->get_baseurl() . '/' . trim($url, '/'); + $this->footerScripts[] = trim($url, '/'); } /** @@ -741,7 +749,7 @@ class App $this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage']; } - public function init_pagehead() + public function initHead() { $interval = ((local_user()) ? PConfig::get(local_user(), 'system', 'update_interval') : 40000); @@ -766,9 +774,6 @@ class App * since the code added by the modules frequently depends on it * being first */ - if (!isset($this->page['htmlhead'])) { - $this->page['htmlhead'] = ''; - } // If we're using Smarty, then doing replace_macros() will replace // any unrecognized variables with a blank string. Since we delay @@ -791,7 +796,9 @@ class App } // get data wich is needed for infinite scroll on the network page - $invinite_scroll = infinite_scroll_data($this->module); + $infinite_scroll = infinite_scroll_data($this->module); + + Core\Addon::callHooks('head', $this->page['htmlhead']); $tpl = get_markup_template('head.tpl'); $this->page['htmlhead'] = replace_macros($tpl, [ @@ -805,8 +812,9 @@ class App '$shortcut_icon' => $shortcut_icon, '$touch_icon' => $touch_icon, '$stylesheet' => $stylesheet, - '$infinite_scroll' => $invinite_scroll, + '$infinite_scroll' => $infinite_scroll, '$block_public' => intval(Config::get('system', 'block_public')), + '$stylesheets' => $this->stylesheets, ]) . $this->page['htmlhead']; } @@ -845,10 +853,10 @@ class App Core\Addon::callHooks('footer', $this->page['footer']); $tpl = get_markup_template('footer.tpl'); - $this->page['footer'] .= replace_macros($tpl, [ + $this->page['footer'] = replace_macros($tpl, [ '$baseurl' => $this->get_baseurl(), '$footerScripts' => $this->footerScripts, - ]); + ]) . $this->page['footer']; } public function set_curl_code($code) diff --git a/view/templates/head.tpl b/view/templates/head.tpl index eef985bc6..46b7283ac 100644 --- a/view/templates/head.tpl +++ b/view/templates/head.tpl @@ -11,6 +11,10 @@ +{{foreach $stylesheets as $stylesheetUrl}} + +{{/foreach}} +