Last active
December 11, 2019 08:50
-
-
Save hereswhatidid/8c8edef106ee95138b03 to your computer and use it in GitHub Desktop.
PrestaShop Media class override to allow for forcing some inline JavaScripts to remain inline.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p>Some HTML goes here</p> | |
<script type="text/javascript" data-keepinline="true"> | |
// this script will remain here when rendered | |
alert( "hello!" ); | |
</script> | |
<script type="text/javascript"> | |
// this script will be forced to the bottom of the page | |
alert( "hello again!" ); | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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]); | |
/* 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 ''; | |
/* 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 (isset($results[1]) && (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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I can not seem to find
override/classes/Media.php
in my wordpress project.Is this directory specific to the prestashop theme? As I'm having this issue with the Scalia theme, and can not find where to place this code.