Skip to content

Instantly share code, notes, and snippets.

@prestarocket
Created January 26, 2015 21:08
Show Gist options
  • Save prestarocket/1ca849e576fb439183e3 to your computer and use it in GitHub Desktop.
Save prestarocket/1ca849e576fb439183e3 to your computer and use it in GitHub Desktop.
Override Media.php to keep some files in header
<?php
Class Media extends MediaCore
{
public static function deferScript($matches)
{
if (!is_array($matches))
return false;
$inline = '';
if (isset($matches[0]))
$original = trim($matches[0]);
if (isset($matches[1]))
$inline = trim($matches[1]);
// prestarocket
/* This is an inline script, add its content to inline scripts stack then remove it from content */
if (!empty($inline) && preg_match('/<\s*script(?!.*data-keepinline)[^>]*>/ims', $original) !== 0 && Media::$inline_script[] = $inline)
return '';
//end prestarocket
/* This is an external script, if it already belongs to js_files then remove it from content */
preg_match('/src\s*=\s*["\']?([^"\']*)[^>]/ims', $original, $results);
if (array_key_exists(1, $results))
{
if (substr($results[1], 0, 2) == '//')
{
$protocol_link = Tools::getCurrentUrlProtocolPrefix();
$results[1] = $protocol_link.ltrim($results[1], '/');
}
if (in_array($results[1], Context::getContext()->controller->js_files) || in_array($results[1], Media::$inline_script_src))
return '';
}
/* return original string because no match was found */
return $original;
}
}
@michaelhjulskov
Copy link

Thank You :)

@desire2create
Copy link

Actually this script didn't work for me. I found this one to work.
https://gist.github.com/hereswhatidid/8c8edef106ee95138b03

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment