PluginProbe
Autoptimize / 1.2
Autoptimize v1.2
2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 All 107 releases
autoptimize / classes / autoptimizeCDN.php

autoptimizeCDN.php in Autoptimize 1.2, at classes/autoptimizeCDN.php

100 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class autoptimizeCDN extends autoptimizeBase
4 {
5 private $js = false;
6 private $jsurl = null;
7 private $css = false;
8 private $cssurl = null;
9 private $replace = array();
10
11 //Gets the tags that need replacing
12 public function read($options)
13 {
14 //Remove the HTML comments?
15 $this->js = (bool) $options['js'];
16 $this->jsurl = $options['jsurl'];
17 $this->css = (bool) $options['css'];
18 $this->cssurl = $options['cssurl'];
19 $this->img = (bool) $options['img'];
20 $this->imgurl = $options['imgurl'];
21 $siteurl = get_bloginfo('siteurl');
22
23 if($this->js)
24 {
25 if(preg_match_all('#<script[^>]*src[^>]*>.*</script>#Usmi',$this->content,$matches))
26 {
27 foreach($matches[0] as $tag)
28 {
29 if(preg_match('#src=("|\')(.*)("|\')#Usmi',$tag,$url))
30 {
31 $url = $url[2];
32 if(strpos($url,$siteurl)!==false)
33 {
34 $this->replace[$tag] = str_replace($siteurl,$this->jsurl,$tag);
35 }
36 }
37 }
38 }
39 }
40
41 if($this->css)
42 {
43 if(preg_match_all('#<link[^>]*stylesheet[^>]*>#Usmi',$this->content,$matches))
44 {
45 foreach($matches[0] as $tag)
46 {
47 if(preg_match('#href=("|\')(.*)("|\')#Usmi',$tag,$url))
48 {
49 $url = $url[2];
50 if(strpos($url,$siteurl)!==false)
51 {
52 $this->replace[$tag] = str_replace($siteurl,$this->cssurl,$tag);
53 }
54 }
55 }
56 }
57 }
58
59 if($this->img)
60 {
61 if(preg_match_all('#<img[^>]*src[^>]*>#Usmi',$this->content,$matches))
62 {
63 foreach($matches[0] as $tag)
64 {
65 if(preg_match('#src=("|\')(.*)("|\')#Usmi',$tag,$url))
66 {
67 $url = $url[2];
68 if(strpos($url,$siteurl)!==false)
69 {
70 $this->replace[$tag] = str_replace($siteurl,$this->imgurl,$tag);
71 }
72 }
73 }
74 }
75 }
76 //Do we need further processing?
77 return (count($this->replace)>0);
78 }
79
80 //Do the tag replacing
81 public function minify()
82 {
83 //Replace the tags with the CDNed ones
84 $this->content = str_replace(array_keys($this->replace),array_values($this->replace),$this->content);
85 }
86
87 //Does nothing
88 public function cache()
89 {
90 //No cache for CDN
91 return true;
92 }
93
94 //Returns the content
95 public function getcontent()
96 {
97 return $this->content;
98 }
99 }
100