-
Notifications
You must be signed in to change notification settings - Fork 4
/
titlechange.php
87 lines (79 loc) · 2.01 KB
/
titlechange.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
/**
* System Plugin for Joomla! - Title change
*
* @author Viviana Menzel <[email protected]>
* @copyright Copyright 2015 Viviana Menzel
* @license GNU Public License version 3 or later
* @link http://www.dr-menzel-it.de/
*/
defined('_JEXEC') or die;
use Joomla\CMS\Plugin\CMSPlugin;
/**
* Class PlgSystemTitlechange
*
* @since 1.0.0
*/
class PlgSystemTitlechange extends JPlugin
{
/**
* var JApplicationCms
*/
protected $app;
/**
* Event method onAfterRender
*
* @return void
*
* @since 1.0.0
*/
public function onAfterRender()
{
// Don't run this plugin in the Admin
if ($this->app->isClient('administrator'))
{
return;
}
$body = $this->app->getBody();
if (strpos($body, '{titlechange') !== false)
{
$body = $this->replaceTags($body);
$this->app->setBody($body);
}
}
/**
* Method to replace tags in a text
*
* @param string $text Text to replace tags in
*
* @return string
*
* @since 1.0.0
*/
public function replaceTags($text)
{
// This regEx search for {titlechange:myClass my text} parts inside a <title></title> tag, should only appear once...
while (preg_match_all('/<title>([^\{\}]*){titlechange:([^\s}]+)\ ([^\}]+)\}(.*?)<\/title>/', $text, $matches))
{
foreach ($matches[2] as $matchIndex => $match)
{
$tag = $matches[0][$matchIndex];
$group1 = $matches[1][$matchIndex];
$group3 = $matches[3][$matchIndex];
$group4 = $matches[4][$matchIndex];
$text = str_replace($tag, "<title>" . $group1 . $group3 . $group4 . "</title>", $text);
}
}
// This regEx search for {titlechange:myClass [([^\s}]+)\] my text [([^\}]+)\]} parts
if (preg_match_all('/{titlechange:([^\s}]+)\ ([^\}]+)\}/', $text, $matches))
{
foreach ($matches[2] as $matchIndex => $match)
{
$tag = $matches[0][$matchIndex];
$classname = $matches[1][$matchIndex];
$text = str_replace($tag, "<span class=" . $classname . ">" . $match . "</span>", $text);
}
}
return $text;
}
}