@@ -27,21 +27,21 @@ discard block |
||
27 | 27 | * ImagesViaImgix constructor. |
28 | 28 | */ |
29 | 29 | public function __construct() { |
30 | - $this->options = get_option( 'imgix_settings', [] ); |
|
30 | + $this->options = get_option('imgix_settings', []); |
|
31 | 31 | |
32 | 32 | // Change filter load order to ensure it loads after other CDN url transformations i.e. Amazon S3 which loads at position 99. |
33 | - add_filter( 'wp_get_attachment_url', [ $this, 'replace_image_url' ], 100 ); |
|
34 | - add_filter( 'imgix/add-image-url', [ $this, 'replace_image_url' ] ); |
|
33 | + add_filter('wp_get_attachment_url', [$this, 'replace_image_url'], 100); |
|
34 | + add_filter('imgix/add-image-url', [$this, 'replace_image_url']); |
|
35 | 35 | |
36 | - add_filter( 'image_downsize', [ $this, 'image_downsize' ], 10, 3 ); |
|
36 | + add_filter('image_downsize', [$this, 'image_downsize'], 10, 3); |
|
37 | 37 | |
38 | - add_filter( 'wp_calculate_image_srcset', [ $this, 'calculate_image_srcset' ], 10, 5 ); |
|
38 | + add_filter('wp_calculate_image_srcset', [$this, 'calculate_image_srcset'], 10, 5); |
|
39 | 39 | |
40 | - add_filter( 'the_content', [ $this, 'replace_images_in_content' ] ); |
|
41 | - add_action( 'wp_head', [ $this, 'prefetch_cdn' ], 1 ); |
|
40 | + add_filter('the_content', [$this, 'replace_images_in_content']); |
|
41 | + add_action('wp_head', [$this, 'prefetch_cdn'], 1); |
|
42 | 42 | |
43 | - add_action( 'after_setup_theme', [ $this, 'buffer_start_for_retina' ] ); |
|
44 | - add_action( 'shutdown', [ $this, 'buffer_end_for_retina' ] ); |
|
43 | + add_action('after_setup_theme', [$this, 'buffer_start_for_retina']); |
|
44 | + add_action('shutdown', [$this, 'buffer_end_for_retina']); |
|
45 | 45 | } |
46 | 46 | |
47 | 47 | /** |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | * @return Images_Via_Imgix |
51 | 51 | */ |
52 | 52 | public static function instance() { |
53 | - if ( ! isset( self::$instance ) ) { |
|
53 | + if ( ! isset(self::$instance)) { |
|
54 | 54 | self::$instance = new self; |
55 | 55 | } |
56 | 56 | |
@@ -63,7 +63,7 @@ discard block |
||
63 | 63 | * |
64 | 64 | * @param array $options |
65 | 65 | */ |
66 | - public function set_options( $options ) { |
|
66 | + public function set_options($options) { |
|
67 | 67 | $this->options = $options; |
68 | 68 | } |
69 | 69 | |
@@ -76,13 +76,13 @@ discard block |
||
76 | 76 | * |
77 | 77 | * @return string Content with retina-enriched image tags. |
78 | 78 | */ |
79 | - public function add_retina( $content ) { |
|
79 | + public function add_retina($content) { |
|
80 | 80 | $pattern = '/<img((?![^>]+srcset )([^>]*)'; |
81 | 81 | $pattern .= 'src=[\'"]([^\'"]*imgix.net[^\'"]*\?[^\'"]*w=[^\'"]*)[\'"]([^>]*)*?)>/i'; |
82 | 82 | $repl = '<img$2src="$3" srcset="${3}, ${3}&dpr=2 2x, ${3}&dpr=3 3x,"$4>'; |
83 | - $content = preg_replace( $pattern, $repl, $content ); |
|
83 | + $content = preg_replace($pattern, $repl, $content); |
|
84 | 84 | |
85 | - return preg_replace( $pattern, $repl, $content ); |
|
85 | + return preg_replace($pattern, $repl, $content); |
|
86 | 86 | } |
87 | 87 | |
88 | 88 | /** |
@@ -92,33 +92,33 @@ discard block |
||
92 | 92 | * |
93 | 93 | * @return string |
94 | 94 | */ |
95 | - public function replace_image_url( $url ) { |
|
96 | - if ( ! empty ( $this->options['cdn_link'] ) ) { |
|
97 | - $parsed_url = parse_url( $url ); |
|
95 | + public function replace_image_url($url) { |
|
96 | + if ( ! empty ($this->options['cdn_link'])) { |
|
97 | + $parsed_url = parse_url($url); |
|
98 | 98 | |
99 | 99 | //Check if image is hosted on current site url -OR- the CDN url specified. Using strpos because we're comparing the host to a full CDN url. |
100 | 100 | if ( |
101 | - isset( $parsed_url['host'], $parsed_url['path'] ) |
|
102 | - && ($parsed_url['host'] === parse_url( home_url( '/' ), PHP_URL_HOST ) || ( isset($this->options['external_cdn_link']) && ! empty($this->options['external_cdn_link']) && strpos( $this->options['external_cdn_link'], $parsed_url['host']) !== false ) ) |
|
103 | - && preg_match( '/\.(jpg|jpeg|gif|png)$/i', $parsed_url['path'] ) |
|
101 | + isset($parsed_url['host'], $parsed_url['path']) |
|
102 | + && ($parsed_url['host'] === parse_url(home_url('/'), PHP_URL_HOST) || (isset($this->options['external_cdn_link']) && ! empty($this->options['external_cdn_link']) && strpos($this->options['external_cdn_link'], $parsed_url['host']) !== false)) |
|
103 | + && preg_match('/\.(jpg|jpeg|gif|png)$/i', $parsed_url['path']) |
|
104 | 104 | ) { |
105 | - $cdn = parse_url( $this->options['cdn_link'] ); |
|
106 | - foreach ( [ 'scheme', 'host', 'port' ] as $url_part ) { |
|
107 | - if ( isset( $cdn[ $url_part ] ) ) { |
|
108 | - $parsed_url[ $url_part ] = $cdn[ $url_part ]; |
|
105 | + $cdn = parse_url($this->options['cdn_link']); |
|
106 | + foreach (['scheme', 'host', 'port'] as $url_part) { |
|
107 | + if (isset($cdn[$url_part])) { |
|
108 | + $parsed_url[$url_part] = $cdn[$url_part]; |
|
109 | 109 | } else { |
110 | - unset( $parsed_url[ $url_part ] ); |
|
110 | + unset($parsed_url[$url_part]); |
|
111 | 111 | } |
112 | 112 | } |
113 | - if ( ! empty( $this->options['external_cdn_link'] ) ) { |
|
113 | + if ( ! empty($this->options['external_cdn_link'])) { |
|
114 | 114 | //Modify the CDN URL, we won't need any parts after the host. |
115 | - $parsed_cdn_url = parse_url( $this->options['external_cdn_link'] ); |
|
116 | - $parsed_url['path'] = str_replace( $parsed_cdn_url['path'], "", $parsed_url['path'] ); |
|
115 | + $parsed_cdn_url = parse_url($this->options['external_cdn_link']); |
|
116 | + $parsed_url['path'] = str_replace($parsed_cdn_url['path'], "", $parsed_url['path']); |
|
117 | 117 | } |
118 | 118 | |
119 | - $url = http_build_url( $parsed_url ); |
|
119 | + $url = http_build_url($parsed_url); |
|
120 | 120 | |
121 | - $url = add_query_arg( $this->get_global_params(), $url ); |
|
121 | + $url = add_query_arg($this->get_global_params(), $url); |
|
122 | 122 | } |
123 | 123 | } |
124 | 124 | |
@@ -134,40 +134,40 @@ discard block |
||
134 | 134 | * |
135 | 135 | * @return false|array |
136 | 136 | */ |
137 | - public function image_downsize( $return, $attachment_id, $size ) { |
|
138 | - if ( ! empty ( $this->options['cdn_link'] ) ) { |
|
139 | - $img_url = wp_get_attachment_url( $attachment_id ); |
|
137 | + public function image_downsize($return, $attachment_id, $size) { |
|
138 | + if ( ! empty ($this->options['cdn_link'])) { |
|
139 | + $img_url = wp_get_attachment_url($attachment_id); |
|
140 | 140 | |
141 | 141 | $params = []; |
142 | - if ( is_array( $size ) ) { |
|
143 | - $params['w'] = $width = isset( $size[0] ) ? $size[0] : 0; |
|
144 | - $params['h'] = $height = isset( $size[1] ) ? $size[1] : 0; |
|
142 | + if (is_array($size)) { |
|
143 | + $params['w'] = $width = isset($size[0]) ? $size[0] : 0; |
|
144 | + $params['h'] = $height = isset($size[1]) ? $size[1] : 0; |
|
145 | 145 | } else { |
146 | 146 | $available_sizes = $this->get_all_defined_sizes(); |
147 | - if ( isset( $available_sizes[ $size ] ) ) { |
|
148 | - $size = $available_sizes[ $size ]; |
|
147 | + if (isset($available_sizes[$size])) { |
|
148 | + $size = $available_sizes[$size]; |
|
149 | 149 | $params['w'] = $width = $size['width']; |
150 | 150 | $params['h'] = $height = $size['height']; |
151 | 151 | } |
152 | 152 | } |
153 | 153 | |
154 | - $params = array_filter( $params ); |
|
154 | + $params = array_filter($params); |
|
155 | 155 | |
156 | - $img_url = add_query_arg( $params, $img_url ); |
|
156 | + $img_url = add_query_arg($params, $img_url); |
|
157 | 157 | |
158 | - if ( ! isset( $width ) || ! isset( $height ) ) { |
|
158 | + if ( ! isset($width) || ! isset($height)) { |
|
159 | 159 | // any other type: use the real image |
160 | - $meta = wp_get_attachment_metadata( $attachment_id ); |
|
160 | + $meta = wp_get_attachment_metadata($attachment_id); |
|
161 | 161 | |
162 | 162 | // Image sizes is missing for pdf thumbnails |
163 | - $meta['width'] = isset( $meta['width'] ) ? $meta['width'] : 0; |
|
164 | - $meta['height'] = isset( $meta['height'] ) ? $meta['height'] : 0; |
|
163 | + $meta['width'] = isset($meta['width']) ? $meta['width'] : 0; |
|
164 | + $meta['height'] = isset($meta['height']) ? $meta['height'] : 0; |
|
165 | 165 | |
166 | - $width = isset( $width ) ? $width : $meta['width']; |
|
167 | - $height = isset( $height ) ? $height : $meta['height']; |
|
166 | + $width = isset($width) ? $width : $meta['width']; |
|
167 | + $height = isset($height) ? $height : $meta['height']; |
|
168 | 168 | } |
169 | 169 | |
170 | - $return = [ $img_url, $width, $height, true ]; |
|
170 | + $return = [$img_url, $width, $height, true]; |
|
171 | 171 | } |
172 | 172 | |
173 | 173 | return $return; |
@@ -184,16 +184,16 @@ discard block |
||
184 | 184 | * |
185 | 185 | * @return array |
186 | 186 | */ |
187 | - public function calculate_image_srcset( $sources, $size_array, $image_src, $image_meta, $attachment_id ) { |
|
188 | - if ( ! empty ( $this->options['cdn_link'] ) ) { |
|
189 | - foreach ( $sources as $i => $image_size ) { |
|
190 | - if ( $image_size['descriptor'] === 'w' ) { |
|
191 | - if ( $attachment_id ) { |
|
192 | - $image_src = wp_get_attachment_url( $attachment_id ); |
|
187 | + public function calculate_image_srcset($sources, $size_array, $image_src, $image_meta, $attachment_id) { |
|
188 | + if ( ! empty ($this->options['cdn_link'])) { |
|
189 | + foreach ($sources as $i => $image_size) { |
|
190 | + if ($image_size['descriptor'] === 'w') { |
|
191 | + if ($attachment_id) { |
|
192 | + $image_src = wp_get_attachment_url($attachment_id); |
|
193 | 193 | } |
194 | 194 | |
195 | - $image_src = remove_query_arg( 'h', $image_src ); |
|
196 | - $sources[ $i ]['url'] = add_query_arg( 'w', $image_size['value'], $image_src ); |
|
195 | + $image_src = remove_query_arg('h', $image_src); |
|
196 | + $sources[$i]['url'] = add_query_arg('w', $image_size['value'], $image_src); |
|
197 | 197 | } |
198 | 198 | } |
199 | 199 | } |
@@ -208,29 +208,29 @@ discard block |
||
208 | 208 | * |
209 | 209 | * @return string |
210 | 210 | */ |
211 | - public function replace_images_in_content( $content ) { |
|
211 | + public function replace_images_in_content($content) { |
|
212 | 212 | // Added null to apply filters wp_get_attachment_url to improve compatibility with https://en-gb.wordpress.org/plugins/amazon-s3-and-cloudfront/ - does not break wordpress if the plugin isn't present. |
213 | - if ( ! empty ( $this->options['cdn_link'] ) ) { |
|
214 | - if ( preg_match_all( '/<img\s[^>]*src=([\"\']??)([^\" >]*?)\1[^>]*>/iU', $content, $matches ) ) { |
|
215 | - foreach ( $matches[2] as $image_src ) { |
|
216 | - $content = str_replace( $image_src, apply_filters( 'wp_get_attachment_url', $image_src, null ), $content ); |
|
213 | + if ( ! empty ($this->options['cdn_link'])) { |
|
214 | + if (preg_match_all('/<img\s[^>]*src=([\"\']??)([^\" >]*?)\1[^>]*>/iU', $content, $matches)) { |
|
215 | + foreach ($matches[2] as $image_src) { |
|
216 | + $content = str_replace($image_src, apply_filters('wp_get_attachment_url', $image_src, null), $content); |
|
217 | 217 | } |
218 | 218 | } |
219 | 219 | |
220 | - if ( preg_match_all( '/<img\s[^>]*srcset=([\"\']??)([^\">]*?)\1[^>]*\/?>/iU', $content, $matches ) ) { |
|
220 | + if (preg_match_all('/<img\s[^>]*srcset=([\"\']??)([^\">]*?)\1[^>]*\/?>/iU', $content, $matches)) { |
|
221 | 221 | |
222 | - foreach ( $matches[2] as $image_srcset ) { |
|
223 | - $new_image_srcset = preg_replace_callback( '/(\S+)(\s\d+\w)/', function ( $srcset_matches ) { |
|
224 | - return apply_filters( 'wp_get_attachment_url', $srcset_matches[1], null ) . $srcset_matches[2]; |
|
225 | - }, $image_srcset ); |
|
222 | + foreach ($matches[2] as $image_srcset) { |
|
223 | + $new_image_srcset = preg_replace_callback('/(\S+)(\s\d+\w)/', function($srcset_matches) { |
|
224 | + return apply_filters('wp_get_attachment_url', $srcset_matches[1], null) . $srcset_matches[2]; |
|
225 | + }, $image_srcset); |
|
226 | 226 | |
227 | - $content = str_replace( $image_srcset, $new_image_srcset, $content ); |
|
227 | + $content = str_replace($image_srcset, $new_image_srcset, $content); |
|
228 | 228 | } |
229 | 229 | } |
230 | 230 | |
231 | - if ( preg_match_all( '/<a\s[^>]*href=([\"\']??)([^\" >]*?)\1[^>]*>(.*)<\/a>/iU', $content, $matches ) ) { |
|
232 | - foreach ( $matches[0] as $link ) { |
|
233 | - $content = str_replace( $link[2], apply_filters( 'wp_get_attachment_url', $link[2], null ), $content ); |
|
231 | + if (preg_match_all('/<a\s[^>]*href=([\"\']??)([^\" >]*?)\1[^>]*>(.*)<\/a>/iU', $content, $matches)) { |
|
232 | + foreach ($matches[0] as $link) { |
|
233 | + $content = str_replace($link[2], apply_filters('wp_get_attachment_url', $link[2], null), $content); |
|
234 | 234 | } |
235 | 235 | } |
236 | 236 | } |
@@ -241,12 +241,12 @@ discard block |
||
241 | 241 | * Add tag to dns prefetch cdn host |
242 | 242 | */ |
243 | 243 | public function prefetch_cdn() { |
244 | - if ( ! empty ( $this->options['cdn_link'] ) ) { |
|
245 | - $host = parse_url( $this->options['cdn_link'], PHP_URL_HOST ); |
|
244 | + if ( ! empty ($this->options['cdn_link'])) { |
|
245 | + $host = parse_url($this->options['cdn_link'], PHP_URL_HOST); |
|
246 | 246 | |
247 | 247 | printf( |
248 | 248 | '<link rel="dns-prefetch" href="%s"/>', |
249 | - esc_attr( '//' . $host ) |
|
249 | + esc_attr('//' . $host) |
|
250 | 250 | ); |
251 | 251 | } |
252 | 252 | } |
@@ -255,9 +255,9 @@ discard block |
||
255 | 255 | * Start output buffer if auto retina is enabled |
256 | 256 | */ |
257 | 257 | public function buffer_start_for_retina() { |
258 | - if ( ! empty ( $this->options['add_dpi2_srcset'] ) ) { |
|
258 | + if ( ! empty ($this->options['add_dpi2_srcset'])) { |
|
259 | 259 | $this->buffer_started = true; |
260 | - ob_start( [ $this, 'add_retina' ] ); |
|
260 | + ob_start([$this, 'add_retina']); |
|
261 | 261 | } |
262 | 262 | } |
263 | 263 | |
@@ -265,7 +265,7 @@ discard block |
||
265 | 265 | * Stop output buffer if it was enabled by the plugin |
266 | 266 | */ |
267 | 267 | public function buffer_end_for_retina() { |
268 | - if ( $this->buffer_started === true ) { |
|
268 | + if ($this->buffer_started === true) { |
|
269 | 269 | ob_end_flush(); |
270 | 270 | } |
271 | 271 | } |
@@ -281,20 +281,20 @@ discard block |
||
281 | 281 | |
282 | 282 | // For now, only "auto" is supported. |
283 | 283 | $auto = []; |
284 | - if ( ! empty ( $this->options['auto_format'] ) ) { |
|
285 | - array_push( $auto, 'format' ); |
|
284 | + if ( ! empty ($this->options['auto_format'])) { |
|
285 | + array_push($auto, 'format'); |
|
286 | 286 | } |
287 | 287 | |
288 | - if ( ! empty ( $this->options['auto_enhance'] ) ) { |
|
289 | - array_push( $auto, 'enhance' ); |
|
288 | + if ( ! empty ($this->options['auto_enhance'])) { |
|
289 | + array_push($auto, 'enhance'); |
|
290 | 290 | } |
291 | 291 | |
292 | - if ( ! empty ( $this->options['auto_compress'] ) ) { |
|
293 | - array_push( $auto, 'compress' ); |
|
292 | + if ( ! empty ($this->options['auto_compress'])) { |
|
293 | + array_push($auto, 'compress'); |
|
294 | 294 | } |
295 | 295 | |
296 | - if ( ! empty( $auto ) ) { |
|
297 | - $params['auto'] = implode( '%2C', $auto ); |
|
296 | + if ( ! empty($auto)) { |
|
297 | + $params['auto'] = implode('%2C', $auto); |
|
298 | 298 | } |
299 | 299 | |
300 | 300 | return $params; |
@@ -310,18 +310,18 @@ discard block |
||
310 | 310 | $theme_image_sizes = wp_get_additional_image_sizes(); |
311 | 311 | |
312 | 312 | $sizes = []; |
313 | - foreach ( get_intermediate_image_sizes() as $s ) { |
|
314 | - $sizes[ $s ] = [ 'width' => '', 'height' => '', 'crop' => false ]; |
|
315 | - if ( isset( $theme_image_sizes[ $s ] ) ) { |
|
313 | + foreach (get_intermediate_image_sizes() as $s) { |
|
314 | + $sizes[$s] = ['width' => '', 'height' => '', 'crop' => false]; |
|
315 | + if (isset($theme_image_sizes[$s])) { |
|
316 | 316 | // For theme-added sizes |
317 | - $sizes[ $s ]['width'] = intval( $theme_image_sizes[ $s ]['width'] ); |
|
318 | - $sizes[ $s ]['height'] = intval( $theme_image_sizes[ $s ]['height'] ); |
|
319 | - $sizes[ $s ]['crop'] = $theme_image_sizes[ $s ]['crop']; |
|
317 | + $sizes[$s]['width'] = intval($theme_image_sizes[$s]['width']); |
|
318 | + $sizes[$s]['height'] = intval($theme_image_sizes[$s]['height']); |
|
319 | + $sizes[$s]['crop'] = $theme_image_sizes[$s]['crop']; |
|
320 | 320 | } else { |
321 | 321 | // For default sizes set in options |
322 | - $sizes[ $s ]['width'] = get_option( "{$s}_size_w" ); |
|
323 | - $sizes[ $s ]['height'] = get_option( "{$s}_size_h" ); |
|
324 | - $sizes[ $s ]['crop'] = get_option( "{$s}_crop" ); |
|
322 | + $sizes[$s]['width'] = get_option("{$s}_size_w"); |
|
323 | + $sizes[$s]['height'] = get_option("{$s}_size_h"); |
|
324 | + $sizes[$s]['crop'] = get_option("{$s}_crop"); |
|
325 | 325 | } |
326 | 326 | } |
327 | 327 |