-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjscdn.php
67 lines (56 loc) · 1.59 KB
/
jscdn.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
<?php
/**
* @package oeJ! Javascript from CDN Plugin.
*
* @copyright Copyright (C) 2014 Ove Eriksson.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
/**
* oeJ! Javascript from CDN Plugin.
*
* @subpackage System.Jscdn
* @since 3.4
*/
class PlgSystemJscdn extends JPlugin
{
/**
* Convert the site preloaded javascripts to be loaded from other urls like public CDNs.
*
* @return void
*/
public function onBeforeRender()
{
$app = JFactory::getApplication();
$document = JFactory::getDocument();
if (( !$app->isSite() && ( !(int)$this->params->get('backend',0))) || $document->getType() !== 'html' )
{
return;
}
//convert the json array from the repeatable field
$registry = new JRegistry;
$registry->loadString( $this->params->get('list_replacements','') );
$list_replacements = $registry->toArray();
$replacement = array();
foreach ($list_replacements['prefix'] as $i => $val )
{
$replacement[] = array('prefix' => $val, 'old' => $list_replacements['old'][$i],
'new' => $list_replacements['new'][$i] );
}
//prefix Joomla standard
$prefix = JURI::root(true);
// Replace the js links in the document.
foreach ($replacement as $i => $vals )
{
$keys = array_keys($document->_scripts);
$prefixold = $vals['prefix']? $vals['prefix'] : $prefix;
$index = array_search($prefixold . $vals['old'], $keys);
if ($index !== false)
{
$keys[$index] = $vals['new'];
$document->_scripts = array_combine($keys, $document->_scripts);
}
}
return true;
}
}