Guest User

Untitled

a guest
Oct 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. function ginger_parse_dom($output)
  2. {
  3.  
  4. $ginger_script_tags = array(
  5. 'platform.twitter.com/widgets.js',
  6. 'apis.google.com/js/plusone.js',
  7. 'apis.google.com/js/platform.js',
  8. 'connect.facebook.net',
  9. 'platform.linkedin.com',
  10. 'assets.pinterest.com',
  11. 'www.youtube.com/iframe_api',
  12. 'www.google-analytics.com/analytics.js',
  13. 'google-analytics.com/ga.js',
  14. 'new google.maps.',
  15. '_getTracker',
  16. 'disqus.com',
  17. );
  18. $ginger_script_tags = apply_filters('ginger_script_tags', $ginger_script_tags);
  19. $ginger_script_async_tags = array(
  20. 'addthis.com',
  21. 'sharethis.com'
  22. );
  23. $ginger_script_async_tags = apply_filters('ginger_script_async_tags', $ginger_script_async_tags);
  24. $ginger_iframe_tags = array(
  25. 'youtube.com',
  26. 'platform.twitter.com',
  27. 'www.facebook.com/plugins/like.php',
  28. 'www.facebook.com/plugins/likebox.php',
  29. 'apis.google.com',
  30. 'www.google.com/maps/embed/',
  31. 'player.vimeo.com',
  32. 'disqus.com'
  33. );
  34. $ginger_iframe_tags = apply_filters('ginger_add_iframe', $ginger_iframe_tags);
  35.  
  36.  
  37. if (strpos($output, '<html') === false):
  38. return $output;
  39. elseif (strpos($output, '<html') > 200):
  40. return $output;
  41. endif;
  42. libxml_use_internal_errors(true);
  43. $doc = new DOMDocument();
  44. $doc->encoding = 'utf-8';
  45. $doc->loadHTML(mb_convert_encoding($output, 'HTML-ENTITIES', 'UTF-8'));
  46. // get all the script tags
  47. $script_tags = $doc->getElementsByTagName('script');
  48. $async_array = array();
  49. $domElemsToRemove = array();
  50. foreach ($script_tags as $script):
  51. $src_script = $script->getAttribute('src');
  52. if ($src_script):
  53. if (strpos_arr($src_script, $ginger_script_tags) !== false):
  54. $script->setAttribute("class", "ginger-script");
  55. $script->setAttribute("type", "text/plain");
  56. continue;
  57. endif;
  58. if (strpos_arr($src_script, $ginger_script_async_tags) !== false):
  59. //return print_r($script->nodeValue);
  60. $async_array[] = $src_script;
  61. $domElemsToRemove[] = $script;
  62. continue;
  63. endif;
  64. endif;
  65. if ($script->nodeValue):
  66. $key = strpos_arr($script->nodeValue, $ginger_script_tags);
  67. if ($key !== false):
  68. if ($ginger_script_tags[$key] == 'www.google-analytics.com/analytics.js' || $ginger_script_tags[$key] == 'google-analytics.com/ga.js')
  69. if (strpos($script->nodeValue, 'anonymizeIp') !== false):
  70. continue;
  71. endif;
  72. $script->setAttribute("class", "ginger-script");
  73. $script->setAttribute("type", "text/plain");
  74. if ($ginger_script_tags[$key] == 'disqus.com/embed.js' || $ginger_script_tags[$key] == 'disqus.com'):
  75. $script->setAttribute("class", "ginger-script");
  76. $script->setAttribute("type", "text/plain");
  77. endif;
  78. endif;
  79. endif;
  80. endforeach;
  81. foreach ($domElemsToRemove as $domElement) {
  82. $domElement->parentNode->removeChild($domElement);
  83. }
  84. // get all the iframe tags
  85. $iframe_tags = $doc->getElementsByTagName('iframe');
  86. foreach ($iframe_tags as $iframe):
  87. $src_iframe = $iframe->getAttribute('src');
  88. if ($src_iframe):
  89. if (strpos_arr($src_iframe, $ginger_iframe_tags) !== false):
  90. $iframe->removeAttribute('src');
  91. $iframe->setAttribute("data-ce-src", $src_iframe);
  92. if ($iframe->hasAttribute('class')):
  93. $addclass = $iframe->getAttribute('class');
  94. else:
  95. $addclass = '';
  96. endif;
  97. $iframe->setAttribute("class", "ginger-iframe " . $addclass);
  98. endif;
  99. endif;
  100. endforeach;
  101. if (!empty($async_array)):
  102. $text = json_encode($async_array);
  103. $text = 'var async_ginger_script = ' . $text . ';';
  104. $head = $doc->getElementsByTagName('head')->item(0);
  105. $element = $doc->createElement('script', $text);
  106. $head->appendChild($element);
  107. endif;
  108.  
  109. // get the HTML string back
  110. $output = $doc->saveHTML();
  111. libxml_use_internal_errors(false);
  112. return $output;
  113. }
Add Comment
Please, Sign In to add comment