Total Complexity | 43 |
Total Lines | 355 |
Duplicated Lines | 0 % |
Changes | 8 | ||
Bugs | 1 | Features | 2 |
Complex classes like TimberDynamicResize often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TimberDynamicResize, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class TimberDynamicResize |
||
10 | { |
||
11 | const DB_VERSION = '2.0'; |
||
12 | const TABLE_NAME = 'resized_images'; |
||
13 | const IMAGE_QUERY_VAR = 'resized-images'; |
||
14 | const IMAGE_PATH_SEPARATOR = 'resized'; |
||
15 | |||
16 | public $flyntResizedImages = []; |
||
17 | |||
18 | protected $enabled = false; |
||
19 | protected $webpEnabled = false; |
||
20 | |||
21 | public function __construct() |
||
22 | { |
||
23 | $this->enabled = get_field('field_global_TimberDynamicResize_dynamicImageGeneration', 'option'); |
||
|
|||
24 | $this->webpEnabled = get_field('field_global_TimberDynamicResize_webpSupport', 'option'); |
||
25 | if ($this->enabled) { |
||
26 | $this->createTable(); |
||
27 | $this->addDynamicHooks(); |
||
28 | } |
||
29 | $this->addHooks(); |
||
30 | } |
||
31 | |||
32 | protected function createTable() |
||
58 | } |
||
59 | } |
||
60 | |||
61 | protected function addDynamicHooks() |
||
62 | { |
||
63 | add_filter('init', [$this, 'addRewriteTag']); |
||
64 | add_action('generate_rewrite_rules', [$this, 'registerRewriteRule']); |
||
65 | add_action('parse_request', [$this, 'parseRequest']); |
||
66 | } |
||
67 | |||
68 | public function parseRequest($wp) |
||
69 | { |
||
70 | if (isset($wp->query_vars[static::IMAGE_QUERY_VAR])) { |
||
71 | $this->checkAndGenerateImage($wp->query_vars[static::IMAGE_QUERY_VAR]); |
||
72 | } |
||
73 | } |
||
74 | |||
75 | protected function addHooks() |
||
76 | { |
||
77 | add_action('timber/twig/filters', function ($twig) { |
||
78 | $twig->addFilter( |
||
79 | new TwigFilter('resizeDynamic', [$this, 'resizeDynamic']) |
||
80 | ); |
||
81 | return $twig; |
||
82 | }); |
||
83 | if ($this->webpEnabled) { |
||
84 | add_filter('mod_rewrite_rules', [$this, 'addWebpRewriteRule']); |
||
85 | } |
||
86 | if ($this->enabled || $this->webpEnabled) { |
||
87 | add_action('after_switch_theme', function () { |
||
88 | add_action('shutdown', 'flush_rewrite_rules'); |
||
89 | }); |
||
90 | add_action('switch_theme', function () { |
||
91 | flush_rewrite_rules(true); |
||
92 | }); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | public function getTableName() |
||
97 | { |
||
98 | global $wpdb; |
||
99 | return $wpdb->prefix . static::TABLE_NAME; |
||
100 | } |
||
101 | |||
102 | public static function getDefaultRelativeUploadDir() |
||
103 | { |
||
104 | require_once(ABSPATH . 'wp-admin/includes/file.php'); |
||
105 | $uploadDir = wp_upload_dir(); |
||
106 | $homePath = get_home_path(); |
||
107 | if (!empty($homePath) && $homePath !== '/') { |
||
108 | $relativeUploadDir = str_replace($homePath, '', $uploadDir['basedir']); |
||
109 | } else { |
||
110 | $relativeUploadDir = $uploadDir['relative']; |
||
111 | } |
||
112 | return $relativeUploadDir; |
||
113 | } |
||
114 | |||
115 | public function getRelativeUploadDir() |
||
122 | } |
||
123 | } |
||
124 | |||
125 | public function getUploadsBaseurl() |
||
126 | { |
||
127 | $uploadDir = wp_upload_dir(); |
||
128 | return $uploadDir['baseurl']; |
||
129 | } |
||
130 | |||
131 | public function getUploadsBasedir() |
||
132 | { |
||
133 | $uploadDir = wp_upload_dir(); |
||
134 | return $uploadDir['basedir']; |
||
135 | } |
||
136 | |||
137 | public function resizeDynamic( |
||
160 | } |
||
161 | } |
||
162 | |||
163 | public function registerRewriteRule($wpRewrite) |
||
164 | { |
||
165 | $routeName = static::IMAGE_QUERY_VAR; |
||
166 | $relativeUploadDir = $this->getRelativeUploadDir(); |
||
167 | $relativeUploadDir = trailingslashit($relativeUploadDir) . static::IMAGE_PATH_SEPARATOR; |
||
168 | $wpRewrite->rules = array_merge( |
||
169 | ["^{$relativeUploadDir}/?(.*?)/?$" => "index.php?{$routeName}=\$matches[1]"], |
||
170 | $wpRewrite->rules |
||
171 | ); |
||
172 | } |
||
173 | |||
174 | public function addRewriteTag() |
||
175 | { |
||
176 | $routeName = static::IMAGE_QUERY_VAR; |
||
177 | add_rewrite_tag("%{$routeName}%", "([^&]+)"); |
||
178 | } |
||
179 | |||
180 | public function removeRewriteTag() |
||
181 | { |
||
182 | $routeName = static::IMAGE_QUERY_VAR; |
||
183 | remove_rewrite_tag("%{$routeName}%"); |
||
184 | } |
||
185 | |||
186 | public function checkAndGenerateImage($relativePath) |
||
224 | } |
||
225 | } |
||
226 | |||
227 | protected function generateImage($url, $w, $h, $crop, $force = false) |
||
228 | { |
||
229 | add_filter('timber/image/new_url', [$this, 'addImageSeparatorToUploadUrl']); |
||
230 | add_filter('timber/image/new_path', [$this, 'addImageSeparatorToUploadPath']); |
||
231 | |||
232 | $resizedUrl = ImageHelper::resize( |
||
233 | $url, |
||
234 | $w, |
||
235 | $h, |
||
236 | $crop, |
||
237 | $force |
||
238 | ); |
||
239 | |||
240 | remove_filter('timber/image/new_url', [$this, 'addImageSeparatorToUploadUrl']); |
||
241 | remove_filter('timber/image/new_path', [$this, 'addImageSeparatorToUploadPath']); |
||
242 | |||
243 | if ($this->webpEnabled) { |
||
244 | $fileinfo = pathinfo($resizedUrl); |
||
245 | if ( |
||
246 | in_array($fileinfo['extension'], [ |
||
247 | 'jpeg', |
||
248 | 'jpg', |
||
249 | 'png', |
||
250 | ]) |
||
251 | ) { |
||
252 | ImageHelper::img_to_webp($resizedUrl); |
||
253 | } |
||
254 | } |
||
255 | |||
256 | return $resizedUrl; |
||
257 | } |
||
258 | |||
259 | public function addImageSeparatorToUploadUrl($url) |
||
260 | { |
||
261 | $baseurl = $this->getUploadsBaseurl(); |
||
262 | return str_replace( |
||
263 | $baseurl, |
||
264 | trailingslashit($baseurl) . static::IMAGE_PATH_SEPARATOR, |
||
265 | $url |
||
266 | ); |
||
267 | } |
||
268 | |||
269 | public function addImageSeparatorToUploadPath($path = '') |
||
270 | { |
||
271 | $basepath = $this->getUploadsBasedir(); |
||
272 | return str_replace( |
||
273 | $basepath, |
||
274 | trailingslashit($basepath) . static::IMAGE_PATH_SEPARATOR, |
||
275 | empty($path) ? $basepath : $path |
||
276 | ); |
||
277 | } |
||
278 | |||
279 | public function addWebpRewriteRule($rules) |
||
280 | { |
||
281 | $dynamicImageRule = <<<EOD |
||
282 | \n# BEGIN Flynt dynamic images |
||
283 | <IfModule mod_setenvif.c> |
||
284 | # Vary: Accept for all the requests to jpeg and png |
||
285 | SetEnvIf Request_URI "\.(jpe?g|png)$" REQUEST_image |
||
286 | </IfModule> |
||
287 | |||
288 | <IfModule mod_rewrite.c> |
||
289 | RewriteEngine On |
||
290 | |||
291 | # Check if browser supports WebP images |
||
292 | RewriteCond %{HTTP_ACCEPT} image/webp |
||
293 | |||
294 | # Check if WebP replacement image exists |
||
295 | RewriteCond %{DOCUMENT_ROOT}/$1.webp -f |
||
296 | |||
297 | # Serve WebP image instead |
||
298 | RewriteRule (.+)\.(jpe?g|png)$ $1.webp [T=image/webp] |
||
299 | </IfModule> |
||
300 | |||
301 | <IfModule mod_headers.c> |
||
302 | Header merge Vary Accept env=REQUEST_image |
||
303 | </IfModule> |
||
304 | |||
305 | <IfModule mod_mime.c> |
||
306 | AddType image/webp .webp |
||
307 | </IfModule>\n |
||
308 | # END Flynt dynamic images\n\n |
||
309 | EOD; |
||
310 | return $dynamicImageRule . $rules; |
||
311 | } |
||
312 | |||
313 | public function storeResizedUrls() |
||
324 | ) |
||
325 | ); |
||
326 | } |
||
327 | |||
328 | public function toggleDynamic($enable) |
||
329 | { |
||
330 | if ($enable) { |
||
331 | $this->addRewriteTag(); |
||
332 | add_action('generate_rewrite_rules', [$this, 'registerRewriteRule']); |
||
341 | }); |
||
342 | } |
||
343 | |||
344 | public function toggleWebp($enable) |
||
345 | { |
||
346 | if ($enable) { |
||
347 | add_filter('mod_rewrite_rules', [$this, 'addWebpRewriteRule']); |
||
348 | } else { |
||
349 | remove_filter('mod_rewrite_rules', [$this, 'addWebpRewriteRule']); |
||
350 | } |
||
351 | add_action('shutdown', function () { |
||
352 | require_once(ABSPATH . 'wp-admin/includes/file.php'); |
||
353 | WP_Filesystem(); |
||
354 | global $wp_filesystem; |
||
355 | flush_rewrite_rules(true); |
||
356 | @$wp_filesystem->rmdir($this->addImageSeparatorToUploadPath(), true); |
||
357 | }); |
||
358 | } |
||
359 | |||
360 | public function changeRelativeUploadPath($relativeUploadPath) |
||
364 | }); |
||
365 | } |
||
366 | } |
||
367 |