Created
October 18, 2018 12:15
-
-
Save sareiodata/dc060c258d03291189c1702d79a86cd0 to your computer and use it in GitHub Desktop.
This file contains 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
function ginger_parse_dom($output) | |
{ | |
$ginger_script_tags = array( | |
'platform.twitter.com/widgets.js', | |
'apis.google.com/js/plusone.js', | |
'apis.google.com/js/platform.js', | |
'connect.facebook.net', | |
'platform.linkedin.com', | |
'assets.pinterest.com', | |
'www.youtube.com/iframe_api', | |
'www.google-analytics.com/analytics.js', | |
'google-analytics.com/ga.js', | |
'new google.maps.', | |
'_getTracker', | |
'disqus.com', | |
); | |
$ginger_script_tags = apply_filters('ginger_script_tags', $ginger_script_tags); | |
$ginger_script_async_tags = array( | |
'addthis.com', | |
'sharethis.com' | |
); | |
$ginger_script_async_tags = apply_filters('ginger_script_async_tags', $ginger_script_async_tags); | |
$ginger_iframe_tags = array( | |
'youtube.com', | |
'platform.twitter.com', | |
'www.facebook.com/plugins/like.php', | |
'www.facebook.com/plugins/likebox.php', | |
'apis.google.com', | |
'www.google.com/maps/embed/', | |
'player.vimeo.com', | |
'disqus.com' | |
); | |
$ginger_iframe_tags = apply_filters('ginger_add_iframe', $ginger_iframe_tags); | |
if (strpos($output, '<html') === false): | |
return $output; | |
elseif (strpos($output, '<html') > 200): | |
return $output; | |
endif; | |
libxml_use_internal_errors(true); | |
$doc = new DOMDocument(); | |
$doc->encoding = 'utf-8'; | |
$doc->loadHTML(mb_convert_encoding($output, 'HTML-ENTITIES', 'UTF-8')); | |
// get all the script tags | |
$script_tags = $doc->getElementsByTagName('script'); | |
$async_array = array(); | |
$domElemsToRemove = array(); | |
foreach ($script_tags as $script): | |
$src_script = $script->getAttribute('src'); | |
if ($src_script): | |
if (strpos_arr($src_script, $ginger_script_tags) !== false): | |
$script->setAttribute("class", "ginger-script"); | |
$script->setAttribute("type", "text/plain"); | |
continue; | |
endif; | |
if (strpos_arr($src_script, $ginger_script_async_tags) !== false): | |
//return print_r($script->nodeValue); | |
$async_array[] = $src_script; | |
$domElemsToRemove[] = $script; | |
continue; | |
endif; | |
endif; | |
if ($script->nodeValue): | |
$key = strpos_arr($script->nodeValue, $ginger_script_tags); | |
if ($key !== false): | |
if ($ginger_script_tags[$key] == 'www.google-analytics.com/analytics.js' || $ginger_script_tags[$key] == 'google-analytics.com/ga.js') | |
if (strpos($script->nodeValue, 'anonymizeIp') !== false): | |
continue; | |
endif; | |
$script->setAttribute("class", "ginger-script"); | |
$script->setAttribute("type", "text/plain"); | |
if ($ginger_script_tags[$key] == 'disqus.com/embed.js' || $ginger_script_tags[$key] == 'disqus.com'): | |
$script->setAttribute("class", "ginger-script"); | |
$script->setAttribute("type", "text/plain"); | |
endif; | |
endif; | |
endif; | |
endforeach; | |
foreach ($domElemsToRemove as $domElement) { | |
$domElement->parentNode->removeChild($domElement); | |
} | |
// get all the iframe tags | |
$iframe_tags = $doc->getElementsByTagName('iframe'); | |
foreach ($iframe_tags as $iframe): | |
$src_iframe = $iframe->getAttribute('src'); | |
if ($src_iframe): | |
if (strpos_arr($src_iframe, $ginger_iframe_tags) !== false): | |
$iframe->removeAttribute('src'); | |
$iframe->setAttribute("data-ce-src", $src_iframe); | |
if ($iframe->hasAttribute('class')): | |
$addclass = $iframe->getAttribute('class'); | |
else: | |
$addclass = ''; | |
endif; | |
$iframe->setAttribute("class", "ginger-iframe " . $addclass); | |
endif; | |
endif; | |
endforeach; | |
if (!empty($async_array)): | |
$text = json_encode($async_array); | |
$text = 'var async_ginger_script = ' . $text . ';'; | |
$head = $doc->getElementsByTagName('head')->item(0); | |
$element = $doc->createElement('script', $text); | |
$head->appendChild($element); | |
endif; | |
// get the HTML string back | |
$output = $doc->saveHTML(); | |
libxml_use_internal_errors(false); | |
return $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment