2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2023-01-10 22:44:09 +00:00
|
|
|
namespace Shimmie2;
|
|
|
|
|
2024-06-21 17:29:26 +00:00
|
|
|
use function MicroHTML\rawHTML;
|
|
|
|
|
2019-09-29 16:37:03 +00:00
|
|
|
class CustomHtmlHeaders extends Extension
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2015-09-12 10:43:28 +00:00
|
|
|
# Adds setup block for custom <head> content
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onSetupBuilding(SetupBuildingEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-10-26 15:13:28 +00:00
|
|
|
$sb = $event->panel->create_new_block("Custom HTML Headers");
|
2015-08-03 14:40:20 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// custom headers
|
|
|
|
$sb->add_longtext_option(
|
|
|
|
"custom_html_headers",
|
|
|
|
"HTML Code to place within <head></head> on all pages<br>"
|
|
|
|
);
|
2015-08-03 14:40:20 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// modified title
|
|
|
|
$sb->add_choice_option("sitename_in_title", [
|
2020-01-26 13:19:35 +00:00
|
|
|
"none" => "none",
|
|
|
|
"as prefix" => "prefix",
|
|
|
|
"as suffix" => "suffix"
|
|
|
|
], "<br>Add website name in title");
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onInitExt(InitExtEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $config;
|
2020-01-26 13:19:35 +00:00
|
|
|
$config->set_default_string("sitename_in_title", "none");
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
# Load Analytics tracking code on page request
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$this->handle_custom_html_headers();
|
|
|
|
$this->handle_modified_page_title();
|
|
|
|
}
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2024-01-20 14:10:59 +00:00
|
|
|
private function handle_custom_html_headers(): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $config, $page;
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$header = $config->get_string('custom_html_headers', '');
|
2023-11-11 21:49:12 +00:00
|
|
|
if ($header != '') {
|
2024-06-21 17:29:26 +00:00
|
|
|
$page->add_html_header(rawHTML($header));
|
2013-03-10 07:24:15 +00:00
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2024-01-20 14:10:59 +00:00
|
|
|
private function handle_modified_page_title(): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $config, $page;
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// get config values
|
2019-08-02 19:40:03 +00:00
|
|
|
$site_title = $config->get_string(SetupConfig::TITLE);
|
2020-01-26 13:19:35 +00:00
|
|
|
$sitename_in_title = $config->get_string("sitename_in_title");
|
2019-08-07 19:53:59 +00:00
|
|
|
|
2020-01-26 13:19:35 +00:00
|
|
|
// sitename is already in title (can occur on index & other pages)
|
2020-10-25 19:31:58 +00:00
|
|
|
if (str_contains($page->title, $site_title)) {
|
2020-01-26 16:38:26 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-01-26 13:19:35 +00:00
|
|
|
|
|
|
|
if ($sitename_in_title == "prefix") {
|
|
|
|
$page->title = "$site_title - $page->title";
|
2020-01-26 16:38:26 +00:00
|
|
|
} elseif ($sitename_in_title == "suffix") {
|
2020-01-26 13:19:35 +00:00
|
|
|
$page->title = "$page->title - $site_title";
|
2013-03-10 07:24:15 +00:00
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2013-03-10 07:24:15 +00:00
|
|
|
}
|