¡Hi!
I'm new in the forum, but a 3 year user of osTicket, ¡great software!
This year, I started making plugins, now I have new requiremente (already solved) and I want to know if is a better alternartive for what I just do.
I need a plugin to add javascript to all pages in public area (client side). Specifically, this plugin was for adding a chat to the support page. But the same idea apply for new plugins that need the same, example: for adding a Facebook Pixel or a Google Analytics code to the support public page.
I just want that, a plugin for adding script in head tag. I don't want to modify the layout or anything that is replaceble with an update, that is why I want a Plugin.
I was unable to find the way to add just code to the head from a plugin.
First aproach: in include/class.osticket.php exist a method called getExtraHeaders but doesn't work inside Plugin::bootstrap(), because $ost global variable is not yet available.
Second aproach: taking the idea of getExtraHeaders I made two modifications in osTicket core:
- In include/class.plugin.php I added this to PluginManager:
function getExtraHeaders() {
$headers = [];
foreach ($this->allActive() as $p) {
if (method_exists($p, 'getExtraHeaders')) {
$headers = array_merge($headers, $p->getExtraHeaders());
}
}
return $headers;
}
- In include/client/header.inc.php I added this inside the head tag:
if ($ost && ($headers=$ost->plugins->getExtraHeaders())) {
echo "\n\t".implode("\n\t", array_map(function($h) {return str_replace("\n", "\n\t", $h);}, $headers))."\n";
}
Then, in my Plugin I just create the method getExtraHeaders and it's works. I have de script inside the header tag and the chat shows up.
Now my question: Is any another way of doing this? Maybe I miss something. Another class or feature that can be used?
If is no other way of doing this, is this change suitable for a pull request? I don't want to do it before asking for a third aproeach here 🙂
For now, is working what I want, but I prefeer use a osTicket core functionalitty without making any changes to the core files.
Thanks!