PageRenderTime 56ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/public_html/wp-content/plugins/autoptimize/classes/autoptimizeBase.php

https://gitlab.com/hop23typhu/list-theme
PHP | 317 lines | 254 code | 34 blank | 29 comment | 50 complexity | ef7ca813d2d22a9f0df14d533418d2fc MD5 | raw file
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  3. abstract class autoptimizeBase {
  4. protected $content = '';
  5. protected $tagWarning = false;
  6. public function __construct($content) {
  7. $this->content = $content;
  8. }
  9. //Reads the page and collects tags
  10. abstract public function read($justhead);
  11. //Joins and optimizes collected things
  12. abstract public function minify();
  13. //Caches the things
  14. abstract public function cache();
  15. //Returns the content
  16. abstract public function getcontent();
  17. //Converts an URL to a full path
  18. protected function getpath($url) {
  19. $url=apply_filters( 'autoptimize_filter_cssjs_alter_url', $url);
  20. if (strpos($url,'%')!==false) {
  21. $url=urldecode($url);
  22. }
  23. // normalize
  24. if (strpos($url,'//')===0) {
  25. if (is_ssl()) {
  26. $url = "https:".$url;
  27. } else {
  28. $url = "http:".$url;
  29. }
  30. } else if ((strpos($url,'//')===false) && (strpos($url,parse_url(AUTOPTIMIZE_WP_SITE_URL,PHP_URL_HOST))===false)) {
  31. $url = AUTOPTIMIZE_WP_SITE_URL.$url;
  32. }
  33. // first check; hostname wp site should be hostname of url
  34. $thisHost=@parse_url($url,PHP_URL_HOST);
  35. if ($thisHost!==parse_url(AUTOPTIMIZE_WP_SITE_URL,PHP_URL_HOST)) {
  36. /*
  37. * first try to get all domains from WPML (if available)
  38. * then explicitely declare $this->cdn_url as OK as well
  39. * then apply own filter autoptimize_filter_cssjs_multidomain takes an array of hostnames
  40. * each item in that array will be considered part of the same WP multisite installation
  41. */
  42. $multidomains = array();
  43. $multidomainsWPML = apply_filters('wpml_setting', array(), 'language_domains');
  44. if (!empty($multidomainsWPML)) {
  45. $multidomains = array_map(array($this,"ao_getDomain"),$multidomainsWPML);
  46. }
  47. if (!empty($this->cdn_url)) {
  48. $multidomains[]=parse_url($this->cdn_url,PHP_URL_HOST);
  49. }
  50. $multidomains = apply_filters('autoptimize_filter_cssjs_multidomain', $multidomains);
  51. if (!empty($multidomains)) {
  52. if (in_array($thisHost,$multidomains)) {
  53. $url=str_replace($thisHost, parse_url(AUTOPTIMIZE_WP_SITE_URL,PHP_URL_HOST), $url);
  54. } else {
  55. return false;
  56. }
  57. } else {
  58. return false;
  59. }
  60. }
  61. // try to remove "wp root url" from url while not minding http<>https
  62. $tmp_ao_root = preg_replace('/https?/','',AUTOPTIMIZE_WP_ROOT_URL);
  63. $tmp_url = preg_replace('/https?/','',$url);
  64. $path = str_replace($tmp_ao_root,'',$tmp_url);
  65. // final check; if path starts with :// or //, this is not a URL in the WP context and we have to assume we can't aggregate
  66. if (preg_match('#^:?//#',$path)) {
  67. /** External script/css (adsense, etc) */
  68. return false;
  69. }
  70. $path = str_replace('//','/',WP_ROOT_DIR.$path);
  71. return $path;
  72. }
  73. // needed for WPML-filter
  74. protected function ao_getDomain($in) {
  75. // make sure the url starts with something vaguely resembling a protocol
  76. if ((strpos($in,"http")!==0) && (strpos($in,"//")!==0)) {
  77. $in="http://".$in;
  78. }
  79. // do the actual parse_url
  80. $out = parse_url($in,PHP_URL_HOST);
  81. // fallback if parse_url does not understand the url is in fact a url
  82. if (empty($out)) $out=$in;
  83. return $out;
  84. }
  85. // logger
  86. protected function ao_logger($logmsg,$appendHTML=true) {
  87. if ($appendHTML) {
  88. $logmsg="<!--noptimize--><!-- ".$logmsg." --><!--/noptimize-->";
  89. $this->content.=$logmsg;
  90. } else {
  91. error_log("Autoptimize: ".$logmsg);
  92. }
  93. }
  94. // hide everything between noptimize-comment tags
  95. protected function hide_noptimize($noptimize_in) {
  96. if ( preg_match( '/<!--\s?noptimize\s?-->/', $noptimize_in ) ) {
  97. $noptimize_out = preg_replace_callback(
  98. '#<!--\s?noptimize\s?-->.*?<!--\s?/\s?noptimize\s?-->#is',
  99. create_function(
  100. '$matches',
  101. 'return "%%NOPTIMIZE%%".base64_encode($matches[0])."%%NOPTIMIZE%%";'
  102. ),
  103. $noptimize_in
  104. );
  105. } else {
  106. $noptimize_out = $noptimize_in;
  107. }
  108. return $noptimize_out;
  109. }
  110. // unhide noptimize-tags
  111. protected function restore_noptimize($noptimize_in) {
  112. if ( strpos( $noptimize_in, '%%NOPTIMIZE%%' ) !== false ) {
  113. $noptimize_out = preg_replace_callback(
  114. '#%%NOPTIMIZE%%(.*?)%%NOPTIMIZE%%#is',
  115. create_function(
  116. '$matches',
  117. 'return base64_decode($matches[1]);'
  118. ),
  119. $noptimize_in
  120. );
  121. } else {
  122. $noptimize_out = $noptimize_in;
  123. }
  124. return $noptimize_out;
  125. }
  126. protected function hide_iehacks($iehacks_in) {
  127. if ( strpos( $iehacks_in, '<!--[if' ) !== false ) {
  128. $iehacks_out = preg_replace_callback(
  129. '#<!--\[if.*?\[endif\]-->#is',
  130. create_function(
  131. '$matches',
  132. 'return "%%IEHACK%%".base64_encode($matches[0])."%%IEHACK%%";'
  133. ),
  134. $iehacks_in
  135. );
  136. } else {
  137. $iehacks_out = $iehacks_in;
  138. }
  139. return $iehacks_out;
  140. }
  141. protected function restore_iehacks($iehacks_in) {
  142. if ( strpos( $iehacks_in, '%%IEHACK%%' ) !== false ) {
  143. $iehacks_out = preg_replace_callback(
  144. '#%%IEHACK%%(.*?)%%IEHACK%%#is',
  145. create_function(
  146. '$matches',
  147. 'return base64_decode($matches[1]);'
  148. ),
  149. $iehacks_in
  150. );
  151. } else {
  152. $iehacks_out=$iehacks_in;
  153. }
  154. return $iehacks_out;
  155. }
  156. protected function hide_comments($comments_in) {
  157. if ( strpos( $comments_in, '<!--' ) !== false ) {
  158. $comments_out = preg_replace_callback(
  159. '#<!--.*?-->#is',
  160. create_function(
  161. '$matches',
  162. 'return "%%COMMENTS%%".base64_encode($matches[0])."%%COMMENTS%%";'
  163. ),
  164. $comments_in
  165. );
  166. } else {
  167. $comments_out = $comments_in;
  168. }
  169. return $comments_out;
  170. }
  171. protected function restore_comments($comments_in) {
  172. if ( strpos( $comments_in, '%%COMMENTS%%' ) !== false ) {
  173. $comments_out = preg_replace_callback(
  174. '#%%COMMENTS%%(.*?)%%COMMENTS%%#is',
  175. create_function(
  176. '$matches',
  177. 'return base64_decode($matches[1]);'
  178. ),
  179. $comments_in
  180. );
  181. } else {
  182. $comments_out=$comments_in;
  183. }
  184. return $comments_out;
  185. }
  186. protected function url_replace_cdn( $url ) {
  187. $cdn_url = apply_filters( 'autoptimize_filter_base_cdnurl', $this->cdn_url );
  188. if ( !empty($cdn_url) ) {
  189. // secondly prepend domain-less absolute URL's
  190. if ( ( substr( $url, 0, 1 ) === '/' ) && ( substr( $url, 1, 1 ) !== '/' ) ) {
  191. $url = rtrim( $cdn_url, '/' ) . $url;
  192. } else {
  193. // get wordpress base URL
  194. $WPSiteBreakdown = parse_url( AUTOPTIMIZE_WP_SITE_URL );
  195. $WPBaseUrl = $WPSiteBreakdown['scheme'] . '://' . $WPSiteBreakdown['host'];
  196. if ( ! empty( $WPSiteBreakdown['port'] ) ) {
  197. $WPBaseUrl .= ":" . $WPSiteBreakdown['port'];
  198. }
  199. // three: replace full url's with scheme
  200. $tmp_url = str_replace( $WPBaseUrl, rtrim( $cdn_url, '/' ), $url );
  201. if ( $tmp_url === $url ) {
  202. // last attempt; replace scheme-less URL's
  203. $url = str_replace( preg_replace( '/https?:/', '', $WPBaseUrl ), rtrim( $cdn_url, '/' ), $url );
  204. } else {
  205. $url = $tmp_url;
  206. }
  207. }
  208. }
  209. // allow API filter to take alter after CDN replacement
  210. $url = apply_filters( 'autoptimize_filter_base_replace_cdn', $url );
  211. return $url;
  212. }
  213. protected function inject_in_html($payload,$replaceTag) {
  214. if (strpos($this->content,$replaceTag[0])!== false) {
  215. if ($replaceTag[1]==="after") {
  216. $replaceBlock=$replaceTag[0].$payload;
  217. } else if ($replaceTag[1]==="replace"){
  218. $replaceBlock=$payload;
  219. } else {
  220. $replaceBlock=$payload.$replaceTag[0];
  221. }
  222. $this->content = substr_replace($this->content,$replaceBlock,strpos($this->content,$replaceTag[0]),strlen($replaceTag[0]));
  223. } else {
  224. $this->content .= $payload;
  225. if (!$this->tagWarning) {
  226. $this->content .= "<!--noptimize--><!-- Autoptimize found a problem with the HTML in your Theme, tag ".$replaceTag[0]." missing --><!--/noptimize-->";
  227. $this->tagWarning=true;
  228. }
  229. }
  230. }
  231. protected function isremovable($tag, $removables) {
  232. foreach ($removables as $match) {
  233. if (strpos($tag,$match)!==false) {
  234. return true;
  235. }
  236. }
  237. return false;
  238. }
  239. // inject already minified code in optimized JS/CSS
  240. protected function inject_minified($in) {
  241. if ( strpos( $in, '%%INJECTLATER%%' ) !== false ) {
  242. $out = preg_replace_callback(
  243. '#%%INJECTLATER%%(.*?)%%INJECTLATER%%#is',
  244. create_function(
  245. '$matches',
  246. '$filepath=base64_decode(strtok($matches[1],"|"));
  247. $filecontent=file_get_contents($filepath);
  248. // remove BOM
  249. $filecontent = preg_replace("#\x{EF}\x{BB}\x{BF}#","",$filecontent);
  250. // remove comments and blank lines
  251. if (substr($filepath,-3,3)===".js") {
  252. $filecontent=preg_replace("#^\s*\/\/.*$#Um","",$filecontent);
  253. }
  254. $filecontent=preg_replace("#^\s*\/\*[^!].*\*\/\s?#Us","",$filecontent);
  255. $filecontent=preg_replace("#(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+#", "\n", $filecontent);
  256. // specific stuff for JS-files
  257. if (substr($filepath,-3,3)===".js") {
  258. if ((substr($filecontent,-1,1)!==";")&&(substr($filecontent,-1,1)!=="}")) {
  259. $filecontent.=";";
  260. }
  261. if (get_option("autoptimize_js_trycatch")==="on") {
  262. $filecontent="try{".$filecontent."}catch(e){}";
  263. }
  264. } else if ((substr($filepath,-4,4)===".css")) {
  265. $filecontent=autoptimizeStyles::fixurls($filepath,$filecontent);
  266. }
  267. // return
  268. return "\n".$filecontent;'
  269. ),
  270. $in
  271. );
  272. } else {
  273. $out = $in;
  274. }
  275. return $out;
  276. }
  277. }