Skip to content

Commit

Permalink
Changed not to use "defer" property to load Cookie Consent js file
Browse files Browse the repository at this point in the history
  • Loading branch information
mystralkk committed Aug 18, 2019
1 parent cc7e861 commit 67c7bdd
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
3 changes: 2 additions & 1 deletion public_html/lib-common.php
Expand Up @@ -1301,7 +1301,8 @@ function COM_createHTMLDocument(&$content = '', $information = array())
);
$_SCRIPTS->setJavaScriptFile(
'cookie_consent', 'https://cdn.jsdelivr.net/npm/cookieconsent@3/build/cookieconsent.min.js',
true, 100
false, 100, false,
array('data-cfasync' => 'false')
);

// To customize appearance and behavior, edit the following file
Expand Down
60 changes: 50 additions & 10 deletions system/classes/Resource.php
Expand Up @@ -41,7 +41,7 @@ class Resource
const DEFAULT_CACHE_LIFESPAN = 604800; // 1 week

const JS_TAG_TEMPLATE = '<script type="text/javascript" src="%s"></script>';
const EXTERNAL_JS_TAG_TEMPLATE = '<script type="text/javascript" src="%s" async defer></script>';
const EXTERNAL_JS_TAG_TEMPLATE = '<script type="text/javascript" src="%s"%s%s></script>';

// Default theme
const DEFAULT_THEME = 'denim';
Expand Down Expand Up @@ -501,13 +501,15 @@ public function setJavaScript($code, $wrap = false, $isFooter = true)
/**
* Set JavaScript file to load
*
* @param string $name (not used)
* @param string $file relative to public_html (must start with '/')
* @param string $name (not used)
* @param string $file relative to public_html (must start with '/')
* @param bool $isFooter
* @param int $priority
* @param bool $isDefer whether to set "defer" property for external files
* @param array $attributes additional attributes for script tag
* @return bool
*/
public function setJavaScriptFile($name, $file, $isFooter = true, $priority = 100)
public function setJavaScriptFile($name, $file, $isFooter = true, $priority = 100, $isDefer = true, array $attributes = array())
{
if ($this->isHeaderSet && !$isFooter) {
return false;
Expand All @@ -517,17 +519,20 @@ public function setJavaScriptFile($name, $file, $isFooter = true, $priority = 10

if ($this->isExternal($file) && array_search($file, array_column($this->externalJsFiles[$position], 'file')) == 0) {
$this->externalJsFiles[$position][] = array(
'file' => $file,
'priority' => $priority,
'file' => $file,
'priority' => (int) $priority,
'isDefer' => (bool) $isDefer,
'attributes' => $attributes,
);

return true;
} else {
// See if file exists and has not already been added (could happen on multiple calls of the same function by different plugins)
if ($this->exists($this->config['path_html'] . $file) && array_search($file, array_column($this->localJsFiles[$position], 'file')) == 0) {
$this->localJsFiles[$position][] = array(
'file' => $file,
'priority' => $priority,
'file' => $file,
'priority' => (int) $priority,
'attributes' => $attributes,
);

return true;
Expand Down Expand Up @@ -1055,6 +1060,29 @@ private function makeTagsForSystemLibraries($isFooter = true)
return $retval;
}

/**
* Format attributes for script tag
*
* @param array $attributes
* @return string
*/
private function formatAttributes(array $attributes = array())
{
$retval = '';

if (count($attributes) > 0) {
foreach ($attributes as $key => $value) {
$retval .= sprintf(
' %s="%s"',
htmlspecialchars($key, ENT_QUOTES, 'utf-8'),
htmlspecialchars($value, ENT_QUOTES, 'utf-8')
);
}
}

return $retval;
}

/**
* Returns header code (JavaScript and CSS) to include in the Head of the web page
*
Expand Down Expand Up @@ -1138,7 +1166,13 @@ public function getHeader()
usort($this->externalJsFiles['header'], array('\\Geeklog\\Resource', 'comparePriority'));

foreach ($this->externalJsFiles['header'] as $jsFile) {
$retval .= sprintf(self::EXTERNAL_JS_TAG_TEMPLATE, $jsFile['file']) . PHP_EOL;
$defer = isset($jsFile['isDefer']) && $jsFile['isDefer'] ? ' defer' : '';
$attributes = '';
if (isset($jsFile['attributes'])) {
$attributes = $this->formatAttributes($jsFile['attributes']);
}

$retval .= sprintf(self::EXTERNAL_JS_TAG_TEMPLATE, $jsFile['file'], $defer, $attributes) . PHP_EOL;
}
}

Expand Down Expand Up @@ -1213,7 +1247,13 @@ public function getFooter()
usort($this->externalJsFiles['footer'], array('\\Geeklog\\Resource', 'comparePriority'));

foreach ($this->externalJsFiles['footer'] as $jsFile) {
$retval .= sprintf(self::EXTERNAL_JS_TAG_TEMPLATE, $jsFile['file']) . PHP_EOL;
$defer = isset($jsFile['isDefer']) && $jsFile['isDefer'] ? ' defer' : '';
$attributes = '';
if (isset($jsFile['attributes'])) {
$attributes = $this->formatAttributes($jsFile['attributes']);
}

$retval .= sprintf(self::EXTERNAL_JS_TAG_TEMPLATE, $jsFile['file'], $defer, $attributes) . PHP_EOL;
}
}

Expand Down

0 comments on commit 67c7bdd

Please sign in to comment.