@@ -27,20 +27,20 @@ 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 | - add_filter( 'wp_get_attachment_url', [ $this, 'replace_image_url' ] ); |
|
33 | - add_filter( 'imgix/add-image-url', [ $this, 'replace_image_url' ] ); |
|
32 | + add_filter('wp_get_attachment_url', [$this, 'replace_image_url']); |
|
33 | + add_filter('imgix/add-image-url', [$this, 'replace_image_url']); |
|
34 | 34 | |
35 | - add_filter( 'image_downsize', [ $this, 'image_downsize' ], 10, 3 ); |
|
35 | + add_filter('image_downsize', [$this, 'image_downsize'], 10, 3); |
|
36 | 36 | |
37 | - add_filter( 'wp_calculate_image_srcset', [ $this, 'calculate_image_srcset' ], 10, 3 ); |
|
37 | + add_filter('wp_calculate_image_srcset', [$this, 'calculate_image_srcset'], 10, 3); |
|
38 | 38 | |
39 | - add_filter( 'the_content', [ $this, 'replace_images_in_content' ] ); |
|
40 | - add_action( 'wp_head', [ $this, 'prefetch_cdn' ], 1 ); |
|
39 | + add_filter('the_content', [$this, 'replace_images_in_content']); |
|
40 | + add_action('wp_head', [$this, 'prefetch_cdn'], 1); |
|
41 | 41 | |
42 | - add_action( 'after_setup_theme', [ $this, 'buffer_start_for_retina' ] ); |
|
43 | - add_action( 'shutdown', [ $this, 'buffer_end_for_retina' ] ); |
|
42 | + add_action('after_setup_theme', [$this, 'buffer_start_for_retina']); |
|
43 | + add_action('shutdown', [$this, 'buffer_end_for_retina']); |
|
44 | 44 | } |
45 | 45 | |
46 | 46 | /** |
@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | * @return Images_Via_Imgix |
50 | 50 | */ |
51 | 51 | public static function instance() { |
52 | - if ( ! isset( self::$instance ) ) { |
|
52 | + if ( ! isset(self::$instance)) { |
|
53 | 53 | self::$instance = new self; |
54 | 54 | } |
55 | 55 | |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | * |
63 | 63 | * @param array $options |
64 | 64 | */ |
65 | - public function set_options( $options ) { |
|
65 | + public function set_options($options) { |
|
66 | 66 | $this->options = $options; |
67 | 67 | } |
68 | 68 | |
@@ -75,13 +75,13 @@ discard block |
||
75 | 75 | * |
76 | 76 | * @return string Content with retina-enriched image tags. |
77 | 77 | */ |
78 | - public function add_retina( $content ) { |
|
78 | + public function add_retina($content) { |
|
79 | 79 | $pattern = '/<img((?![^>]+srcset )([^>]*)'; |
80 | 80 | $pattern .= 'src=[\'"]([^\'"]*imgix.net[^\'"]*\?[^\'"]*w=[^\'"]*)[\'"]([^>]*)*?)>/i'; |
81 | 81 | $repl = '<img$2src="$3" srcset="${3}, ${3}&dpr=2 2x, ${3}&dpr=3 3x,"$4>'; |
82 | - $content = preg_replace( $pattern, $repl, $content ); |
|
82 | + $content = preg_replace($pattern, $repl, $content); |
|
83 | 83 | |
84 | - return preg_replace( $pattern, $repl, $content ); |
|
84 | + return preg_replace($pattern, $repl, $content); |
|
85 | 85 | } |
86 | 86 | |
87 | 87 | /** |
@@ -91,22 +91,22 @@ discard block |
||
91 | 91 | * |
92 | 92 | * @return string |
93 | 93 | */ |
94 | - public function replace_image_url( $url ) { |
|
95 | - if ( ! empty ( $this->options['cdn_link'] ) ) { |
|
96 | - $parsed_url = parse_url( $url ); |
|
97 | - |
|
98 | - if ( isset( $parsed_url['host'], $parsed_url['path'] ) && $parsed_url['host'] === parse_url( home_url( '/' ), PHP_URL_HOST ) && preg_match( '/\.(jpg|jpeg|gif|png)$/', $parsed_url['path'] ) ) { |
|
99 | - $cdn = parse_url( $this->options['cdn_link'] ); |
|
100 | - foreach ( [ 'scheme', 'host', 'port' ] as $url_part ) { |
|
101 | - if ( isset( $cdn[ $url_part ] ) ) { |
|
102 | - $parsed_url[ $url_part ] = $cdn[ $url_part ]; |
|
94 | + public function replace_image_url($url) { |
|
95 | + if ( ! empty ($this->options['cdn_link'])) { |
|
96 | + $parsed_url = parse_url($url); |
|
97 | + |
|
98 | + if (isset($parsed_url['host'], $parsed_url['path']) && $parsed_url['host'] === parse_url(home_url('/'), PHP_URL_HOST) && preg_match('/\.(jpg|jpeg|gif|png)$/', $parsed_url['path'])) { |
|
99 | + $cdn = parse_url($this->options['cdn_link']); |
|
100 | + foreach (['scheme', 'host', 'port'] as $url_part) { |
|
101 | + if (isset($cdn[$url_part])) { |
|
102 | + $parsed_url[$url_part] = $cdn[$url_part]; |
|
103 | 103 | } else { |
104 | - unset( $parsed_url[ $url_part ] ); |
|
104 | + unset($parsed_url[$url_part]); |
|
105 | 105 | } |
106 | 106 | } |
107 | - $url = http_build_url( $parsed_url ); |
|
107 | + $url = http_build_url($parsed_url); |
|
108 | 108 | |
109 | - $url = add_query_arg( $this->get_global_params(), $url ); |
|
109 | + $url = add_query_arg($this->get_global_params(), $url); |
|
110 | 110 | } |
111 | 111 | } |
112 | 112 | |
@@ -122,35 +122,35 @@ discard block |
||
122 | 122 | * |
123 | 123 | * @return false|array |
124 | 124 | */ |
125 | - public function image_downsize( $return, $attachment_id, $size ) { |
|
126 | - if ( ! empty ( $this->options['cdn_link'] ) ) { |
|
127 | - $img_url = wp_get_attachment_url( $attachment_id ); |
|
125 | + public function image_downsize($return, $attachment_id, $size) { |
|
126 | + if ( ! empty ($this->options['cdn_link'])) { |
|
127 | + $img_url = wp_get_attachment_url($attachment_id); |
|
128 | 128 | |
129 | 129 | $params = []; |
130 | - if ( is_array( $size ) ) { |
|
131 | - $params['w'] = $width = isset( $size[0] ) ? $size[0] : 0; |
|
132 | - $params['h'] = $height = isset( $size[1] ) ? $size[1] : 0; |
|
130 | + if (is_array($size)) { |
|
131 | + $params['w'] = $width = isset($size[0]) ? $size[0] : 0; |
|
132 | + $params['h'] = $height = isset($size[1]) ? $size[1] : 0; |
|
133 | 133 | } else { |
134 | 134 | $available_sizes = $this->get_all_defined_sizes(); |
135 | - if ( isset( $available_sizes[ $size ] ) ) { |
|
136 | - $size = $available_sizes[ $size ]; |
|
135 | + if (isset($available_sizes[$size])) { |
|
136 | + $size = $available_sizes[$size]; |
|
137 | 137 | $params['w'] = $width = $size['width']; |
138 | 138 | $params['h'] = $height = $size['height']; |
139 | 139 | } |
140 | 140 | } |
141 | 141 | |
142 | - $params = array_filter( $params ); |
|
142 | + $params = array_filter($params); |
|
143 | 143 | |
144 | - $img_url = add_query_arg( $params, $img_url ); |
|
144 | + $img_url = add_query_arg($params, $img_url); |
|
145 | 145 | |
146 | - if ( ! isset( $width ) || ! isset( $height ) ) { |
|
146 | + if ( ! isset($width) || ! isset($height)) { |
|
147 | 147 | // any other type: use the real image |
148 | - $meta = wp_get_attachment_metadata( $attachment_id ); |
|
149 | - $width = isset( $width ) ? $width : $meta['width']; |
|
150 | - $height = isset( $height ) ? $height : $meta['height']; |
|
148 | + $meta = wp_get_attachment_metadata($attachment_id); |
|
149 | + $width = isset($width) ? $width : $meta['width']; |
|
150 | + $height = isset($height) ? $height : $meta['height']; |
|
151 | 151 | } |
152 | 152 | |
153 | - $return = [ $img_url, $width, $height, true ]; |
|
153 | + $return = [$img_url, $width, $height, true]; |
|
154 | 154 | } |
155 | 155 | |
156 | 156 | return $return; |
@@ -165,11 +165,11 @@ discard block |
||
165 | 165 | * |
166 | 166 | * @return array |
167 | 167 | */ |
168 | - public function calculate_image_srcset( $image_meta, $size_array, $image_src ) { |
|
169 | - foreach ( $image_meta as $i => $image_size ) { |
|
170 | - if ( $image_size['descriptor'] === 'w' ) { |
|
171 | - $image_src = remove_query_arg( 'h', $image_src ); |
|
172 | - $image_meta[ $i ]['url'] = add_query_arg( 'w', $image_size['value'], $image_src ); |
|
168 | + public function calculate_image_srcset($image_meta, $size_array, $image_src) { |
|
169 | + foreach ($image_meta as $i => $image_size) { |
|
170 | + if ($image_size['descriptor'] === 'w') { |
|
171 | + $image_src = remove_query_arg('h', $image_src); |
|
172 | + $image_meta[$i]['url'] = add_query_arg('w', $image_size['value'], $image_src); |
|
173 | 173 | } |
174 | 174 | } |
175 | 175 | |
@@ -183,28 +183,28 @@ discard block |
||
183 | 183 | * |
184 | 184 | * @return string |
185 | 185 | */ |
186 | - public function replace_images_in_content( $content ) { |
|
187 | - if ( ! empty ( $this->options['cdn_link'] ) ) { |
|
188 | - if ( preg_match_all( '/<img\s[^>]*src=([\"\']??)([^\" >]*?)\1[^>]*>/iU', $content, $matches ) ) { |
|
189 | - foreach ( $matches[2] as $image_src ) { |
|
190 | - $content = str_replace( $image_src, apply_filters( 'wp_get_attachment_url', $image_src ), $content ); |
|
186 | + public function replace_images_in_content($content) { |
|
187 | + if ( ! empty ($this->options['cdn_link'])) { |
|
188 | + if (preg_match_all('/<img\s[^>]*src=([\"\']??)([^\" >]*?)\1[^>]*>/iU', $content, $matches)) { |
|
189 | + foreach ($matches[2] as $image_src) { |
|
190 | + $content = str_replace($image_src, apply_filters('wp_get_attachment_url', $image_src), $content); |
|
191 | 191 | } |
192 | 192 | } |
193 | 193 | |
194 | - if ( preg_match_all( '/<img\s[^>]*srcset=([\"\']??)([^\">]*?)\1[^>]*\/?>/iU', $content, $matches ) ) { |
|
194 | + if (preg_match_all('/<img\s[^>]*srcset=([\"\']??)([^\">]*?)\1[^>]*\/?>/iU', $content, $matches)) { |
|
195 | 195 | |
196 | - foreach ( $matches[2] as $image_srcset ) { |
|
197 | - $new_image_srcset = preg_replace_callback( '/(\S+)(\s\d+\w)/', function ( $srcset_matches ) { |
|
198 | - return apply_filters( 'wp_get_attachment_url', $srcset_matches[1] ) . $srcset_matches[2]; |
|
199 | - }, $image_srcset ); |
|
196 | + foreach ($matches[2] as $image_srcset) { |
|
197 | + $new_image_srcset = preg_replace_callback('/(\S+)(\s\d+\w)/', function($srcset_matches) { |
|
198 | + return apply_filters('wp_get_attachment_url', $srcset_matches[1]) . $srcset_matches[2]; |
|
199 | + }, $image_srcset); |
|
200 | 200 | |
201 | - $content = str_replace( $image_srcset, $new_image_srcset, $content ); |
|
201 | + $content = str_replace($image_srcset, $new_image_srcset, $content); |
|
202 | 202 | } |
203 | 203 | } |
204 | 204 | |
205 | - if ( preg_match_all( '/<a\s[^>]*href=([\"\']??)([^\" >]*?)\1[^>]*>(.*)<\/a>/iU', $content, $matches ) ) { |
|
206 | - foreach ( $matches[0] as $link ) { |
|
207 | - $content = str_replace( $link[2], apply_filters( 'wp_get_attachment_url', $link[2] ), $content ); |
|
205 | + if (preg_match_all('/<a\s[^>]*href=([\"\']??)([^\" >]*?)\1[^>]*>(.*)<\/a>/iU', $content, $matches)) { |
|
206 | + foreach ($matches[0] as $link) { |
|
207 | + $content = str_replace($link[2], apply_filters('wp_get_attachment_url', $link[2]), $content); |
|
208 | 208 | } |
209 | 209 | } |
210 | 210 | } |
@@ -216,12 +216,12 @@ discard block |
||
216 | 216 | * Add tag to dns prefetch cdn host |
217 | 217 | */ |
218 | 218 | public function prefetch_cdn() { |
219 | - if ( ! empty ( $this->options['cdn_link'] ) ) { |
|
220 | - $host = parse_url( $this->options['cdn_link'], PHP_URL_HOST ); |
|
219 | + if ( ! empty ($this->options['cdn_link'])) { |
|
220 | + $host = parse_url($this->options['cdn_link'], PHP_URL_HOST); |
|
221 | 221 | |
222 | 222 | printf( |
223 | 223 | '<link rel="dns-prefetch" href="%s"/>', |
224 | - esc_attr( '//' . $host ) |
|
224 | + esc_attr('//' . $host) |
|
225 | 225 | ); |
226 | 226 | } |
227 | 227 | } |
@@ -230,9 +230,9 @@ discard block |
||
230 | 230 | * Start output buffer if auto retina is enabled |
231 | 231 | */ |
232 | 232 | public function buffer_start_for_retina() { |
233 | - if ( ! empty ( $this->options['add_dpi2_srcset'] ) ) { |
|
233 | + if ( ! empty ($this->options['add_dpi2_srcset'])) { |
|
234 | 234 | $this->buffer_started = true; |
235 | - ob_start( [ $this, 'add_retina' ] ); |
|
235 | + ob_start([$this, 'add_retina']); |
|
236 | 236 | } |
237 | 237 | } |
238 | 238 | |
@@ -240,7 +240,7 @@ discard block |
||
240 | 240 | * Stop output buffer if it was enabled by the plugin |
241 | 241 | */ |
242 | 242 | public function buffer_end_for_retina() { |
243 | - if ( $this->buffer_started === true ) { |
|
243 | + if ($this->buffer_started === true) { |
|
244 | 244 | ob_end_flush(); |
245 | 245 | } |
246 | 246 | } |
@@ -256,20 +256,20 @@ discard block |
||
256 | 256 | |
257 | 257 | // For now, only "auto" is supported. |
258 | 258 | $auto = []; |
259 | - if ( ! empty ( $this->options['auto_format'] ) ) { |
|
260 | - array_push( $auto, 'format' ); |
|
259 | + if ( ! empty ($this->options['auto_format'])) { |
|
260 | + array_push($auto, 'format'); |
|
261 | 261 | } |
262 | 262 | |
263 | - if ( ! empty ( $this->options['auto_enhance'] ) ) { |
|
264 | - array_push( $auto, 'enhance' ); |
|
263 | + if ( ! empty ($this->options['auto_enhance'])) { |
|
264 | + array_push($auto, 'enhance'); |
|
265 | 265 | } |
266 | 266 | |
267 | - if ( ! empty ( $this->options['auto_compress'] ) ) { |
|
268 | - array_push( $auto, 'compress' ); |
|
267 | + if ( ! empty ($this->options['auto_compress'])) { |
|
268 | + array_push($auto, 'compress'); |
|
269 | 269 | } |
270 | 270 | |
271 | - if ( ! empty( $auto ) ) { |
|
272 | - $params['auto'] = implode( '%2C', $auto ); |
|
271 | + if ( ! empty($auto)) { |
|
272 | + $params['auto'] = implode('%2C', $auto); |
|
273 | 273 | } |
274 | 274 | |
275 | 275 | return $params; |
@@ -285,18 +285,18 @@ discard block |
||
285 | 285 | $theme_image_sizes = wp_get_additional_image_sizes(); |
286 | 286 | |
287 | 287 | $sizes = []; |
288 | - foreach ( get_intermediate_image_sizes() as $s ) { |
|
289 | - $sizes[ $s ] = [ 'width' => '', 'height' => '', 'crop' => false ]; |
|
290 | - if ( isset( $theme_image_sizes[ $s ] ) ) { |
|
288 | + foreach (get_intermediate_image_sizes() as $s) { |
|
289 | + $sizes[$s] = ['width' => '', 'height' => '', 'crop' => false]; |
|
290 | + if (isset($theme_image_sizes[$s])) { |
|
291 | 291 | // For theme-added sizes |
292 | - $sizes[ $s ]['width'] = intval( $theme_image_sizes[ $s ]['width'] ); |
|
293 | - $sizes[ $s ]['height'] = intval( $theme_image_sizes[ $s ]['height'] ); |
|
294 | - $sizes[ $s ]['crop'] = $theme_image_sizes[ $s ]['crop']; |
|
292 | + $sizes[$s]['width'] = intval($theme_image_sizes[$s]['width']); |
|
293 | + $sizes[$s]['height'] = intval($theme_image_sizes[$s]['height']); |
|
294 | + $sizes[$s]['crop'] = $theme_image_sizes[$s]['crop']; |
|
295 | 295 | } else { |
296 | 296 | // For default sizes set in options |
297 | - $sizes[ $s ]['width'] = get_option( "{$s}_size_w" ); |
|
298 | - $sizes[ $s ]['height'] = get_option( "{$s}_size_h" ); |
|
299 | - $sizes[ $s ]['crop'] = get_option( "{$s}_crop" ); |
|
297 | + $sizes[$s]['width'] = get_option("{$s}_size_w"); |
|
298 | + $sizes[$s]['height'] = get_option("{$s}_size_h"); |
|
299 | + $sizes[$s]['crop'] = get_option("{$s}_crop"); |
|
300 | 300 | } |
301 | 301 | } |
302 | 302 |