This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/custom_html_headers/main.php

73 lines
2 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
namespace Shimmie2;
use function MicroHTML\rawHTML;
2019-09-29 16:37:03 +00:00
class CustomHtmlHeaders extends Extension
{
2015-09-12 10:43:28 +00:00
# Adds setup block for custom <head> content
public function onSetupBuilding(SetupBuildingEvent $event): void
{
$sb = $event->panel->create_new_block("Custom HTML Headers");
// custom headers
$sb->add_longtext_option(
"custom_html_headers",
"HTML Code to place within &lt;head&gt;&lt;/head&gt; on all pages<br>"
);
// 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");
}
public function onInitExt(InitExtEvent $event): void
{
global $config;
2020-01-26 13:19:35 +00:00
$config->set_default_string("sitename_in_title", "none");
}
# Load Analytics tracking code on page request
public function onPageRequest(PageRequestEvent $event): void
{
$this->handle_custom_html_headers();
$this->handle_modified_page_title();
}
private function handle_custom_html_headers(): void
{
global $config, $page;
$header = $config->get_string('custom_html_headers', '');
2023-11-11 21:49:12 +00:00
if ($header != '') {
$page->add_html_header(rawHTML($header));
}
}
private function handle_modified_page_title(): void
{
global $config, $page;
// 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");
2020-01-26 13:19:35 +00:00
// sitename is already in title (can occur on index & other pages)
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";
}
}
}