PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.4.2
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.4.2
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / vendor / mrclay / minify / lib / Minify / Source.php

Source.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.4.2, at vendor/mrclay/minify/lib/Minify/Source.php

220 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class Minify_Source
4 * @package Minify
5 */
6
7 /**
8 * A content source to be minified by Minify.
9 *
10 * This allows per-source minification options and the mixing of files with
11 * content from other sources.
12 *
13 * @package Minify
14 * @author Stephen Clay <[email protected]>
15 */
16 class Minify_Source implements Minify_SourceInterface
17 {
18
19 /**
20 * @var int time of last modification
21 */
22 protected $lastModified;
23
24 /**
25 * @var callback minifier function specifically for this source.
26 */
27 protected $minifier;
28
29 /**
30 * @var array minification options specific to this source.
31 */
32 protected $minifyOptions = array();
33
34 /**
35 * @var string full path of file
36 */
37 protected $filepath;
38
39 /**
40 * @var string HTTP Content Type (Minify requires one of the constants Minify::TYPE_*)
41 */
42 protected $contentType;
43
44 /**
45 * @var string
46 */
47 protected $content;
48
49 /**
50 * @var callable
51 */
52 protected $getContentFunc;
53
54 /**
55 * @var string
56 */
57 protected $id;
58
59 /**
60 * Create a Minify_Source
61 *
62 * In the $spec array(), you can either provide a 'filepath' to an existing
63 * file (existence will not be checked!) or give 'id' (unique string for
64 * the content), 'content' (the string content) and 'lastModified'
65 * (unixtime of last update).
66 *
67 * @param array $spec options
68 */
69 public function __construct($spec)
70 {
71 if (isset($spec['filepath'])) {
72 $ext = pathinfo($spec['filepath'], PATHINFO_EXTENSION);
73 switch ($ext) {
74 case 'js': $this->contentType = Minify::TYPE_JS;
75 break;
76 case 'less': // fallthrough
77 case 'scss': // fallthrough
78 case 'css': $this->contentType = Minify::TYPE_CSS;
79 break;
80 case 'htm': // fallthrough
81 case 'html': $this->contentType = Minify::TYPE_HTML;
82 break;
83 }
84 $this->filepath = $spec['filepath'];
85 $this->id = $spec['filepath'];
86
87 // TODO ideally not touch disk in constructor
88 $this->lastModified = filemtime($spec['filepath']);
89
90 if (!empty($spec['uploaderHoursBehind'])) {
91 // offset for Windows uploaders with out of sync clocks
92 $this->lastModified += round($spec['uploaderHoursBehind'] * 3600);
93 }
94 } elseif (isset($spec['id'])) {
95 $this->id = 'id::' . $spec['id'];
96 if (isset($spec['content'])) {
97 $this->content = $spec['content'];
98 } else {
99 $this->getContentFunc = $spec['getContentFunc'];
100 }
101 $this->lastModified = isset($spec['lastModified']) ? $spec['lastModified'] : time();
102 }
103 if (isset($spec['contentType'])) {
104 $this->contentType = $spec['contentType'];
105 }
106 if (isset($spec['minifier'])) {
107 $this->setMinifier($spec['minifier']);
108 }
109 if (isset($spec['minifyOptions'])) {
110 $this->minifyOptions = $spec['minifyOptions'];
111 }
112 }
113
114 /**
115 * {@inheritdoc}
116 */
117 public function getLastModified()
118 {
119 return $this->lastModified;
120 }
121
122 /**
123 * {@inheritdoc}
124 */
125 public function getMinifier()
126 {
127 return $this->minifier;
128 }
129
130 /**
131 * {@inheritdoc}
132 */
133 public function setMinifier($minifier = null)
134 {
135 if ($minifier === '') {
136 error_log(__METHOD__ . " cannot accept empty string. Use 'Minify::nullMinifier' or 'trim'.");
137 $minifier = 'Minify::nullMinifier';
138 }
139 if ($minifier !== null && !is_callable($minifier, true)) {
140 throw new InvalidArgumentException('minifier must be null or a valid callable');
141 }
142 $this->minifier = $minifier;
143 }
144
145 /**
146 * {@inheritdoc}
147 */
148 public function getMinifierOptions()
149 {
150 return $this->minifyOptions;
151 }
152
153 /**
154 * {@inheritdoc}
155 */
156 public function setMinifierOptions(array $options)
157 {
158 $this->minifyOptions = $options;
159 }
160
161 /**
162 * {@inheritdoc}
163 */
164 public function getContentType()
165 {
166 return $this->contentType;
167 }
168
169 /**
170 * {@inheritdoc}
171 */
172 public function getContent()
173 {
174 if (null === $this->filepath) {
175 if (null === $this->content) {
176 $content = call_user_func($this->getContentFunc, $this->id);
177 } else {
178 $content = $this->content;
179 }
180 } else {
181 $content = file_get_contents($this->filepath);
182 }
183
184 // remove UTF-8 BOM if present
185 if (strpos($content, "\xEF\xBB\xBF") === 0) {
186 return substr($content, 3);
187 }
188
189 return $content;
190 }
191
192 /**
193 * {@inheritdoc}
194 */
195 public function getId()
196 {
197 return $this->id;
198 }
199
200 /**
201 * {@inheritdoc}
202 */
203 public function getFilePath()
204 {
205 return $this->filepath;
206 }
207
208 /**
209 * {@inheritdoc}
210 */
211 public function setupUriRewrites()
212 {
213 if ($this->filepath
214 && !isset($this->minifyOptions['currentDir'])
215 && !isset($this->minifyOptions['prependRelativePath'])) {
216 $this->minifyOptions['currentDir'] = dirname($this->filepath);
217 }
218 }
219 }
220