@@ -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' ], 0 ); |
|
| 43 | + add_action('after_setup_theme', [$this, 'buffer_start_for_retina']); |
|
| 44 | + add_action('shutdown', [$this, 'buffer_end_for_retina'], 0); |
|
| 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,8 +63,8 @@ discard block |
||
| 63 | 63 | * @param string $key |
| 64 | 64 | * @param mixed $value |
| 65 | 65 | */ |
| 66 | - public function set_option( $key, $value ) { |
|
| 67 | - $this->options[ $key ] = $value; |
|
| 66 | + public function set_option($key, $value) { |
|
| 67 | + $this->options[$key] = $value; |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | /** |
@@ -74,8 +74,8 @@ discard block |
||
| 74 | 74 | * @param mixed $default |
| 75 | 75 | * @return mixed |
| 76 | 76 | */ |
| 77 | - public function get_option( $key, $default = '' ) { |
|
| 78 | - return array_key_exists( $key, $this->options ) ? $this->options[ $key ] : $default; |
|
| 77 | + public function get_option($key, $default = '') { |
|
| 78 | + return array_key_exists($key, $this->options) ? $this->options[$key] : $default; |
|
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | /** |
@@ -84,7 +84,7 @@ discard block |
||
| 84 | 84 | * |
| 85 | 85 | * @param array $options |
| 86 | 86 | */ |
| 87 | - public function set_options( $options ) { |
|
| 87 | + public function set_options($options) { |
|
| 88 | 88 | $this->options = $options; |
| 89 | 89 | } |
| 90 | 90 | |
@@ -97,13 +97,13 @@ discard block |
||
| 97 | 97 | * |
| 98 | 98 | * @return string Content with retina-enriched image tags. |
| 99 | 99 | */ |
| 100 | - public function add_retina( $content ) { |
|
| 100 | + public function add_retina($content) { |
|
| 101 | 101 | $pattern = '/<img((?![^>]+srcset )([^>]*)'; |
| 102 | 102 | $pattern .= 'src=[\'"]([^\'"]*imgix.net[^\'"]*\?[^\'"]*w=[^\'"]*)[\'"]([^>]*)*?)>/i'; |
| 103 | 103 | $repl = '<img$2src="$3" srcset="${3}, ${3}&dpr=2 2x, ${3}&dpr=3 3x,"$4>'; |
| 104 | - $content = preg_replace( $pattern, $repl, $content ); |
|
| 104 | + $content = preg_replace($pattern, $repl, $content); |
|
| 105 | 105 | |
| 106 | - return preg_replace( $pattern, $repl, $content ); |
|
| 106 | + return preg_replace($pattern, $repl, $content); |
|
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | /** |
@@ -113,37 +113,37 @@ discard block |
||
| 113 | 113 | * |
| 114 | 114 | * @return string |
| 115 | 115 | */ |
| 116 | - public function replace_image_url( $url ) { |
|
| 117 | - if ( ! empty ( $this->options['cdn_link'] ) ) { |
|
| 118 | - $parsed_url = parse_url( $url ); |
|
| 116 | + public function replace_image_url($url) { |
|
| 117 | + if ( ! empty ($this->options['cdn_link'])) { |
|
| 118 | + $parsed_url = parse_url($url); |
|
| 119 | 119 | |
| 120 | 120 | //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. |
| 121 | 121 | if ( |
| 122 | - isset( $parsed_url['host'], $parsed_url['path'] ) |
|
| 123 | - && ($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 ) ) |
|
| 124 | - && preg_match( '/\.(jpg|jpeg|gif|png)$/i', $parsed_url['path'] ) |
|
| 122 | + isset($parsed_url['host'], $parsed_url['path']) |
|
| 123 | + && ($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)) |
|
| 124 | + && preg_match('/\.(jpg|jpeg|gif|png)$/i', $parsed_url['path']) |
|
| 125 | 125 | ) { |
| 126 | - $cdn = parse_url( $this->options['cdn_link'] ); |
|
| 126 | + $cdn = parse_url($this->options['cdn_link']); |
|
| 127 | 127 | |
| 128 | - foreach ( [ 'scheme', 'host', 'port' ] as $url_part ) { |
|
| 129 | - if ( isset( $cdn[ $url_part ] ) ) { |
|
| 130 | - $parsed_url[ $url_part ] = $cdn[ $url_part ]; |
|
| 128 | + foreach (['scheme', 'host', 'port'] as $url_part) { |
|
| 129 | + if (isset($cdn[$url_part])) { |
|
| 130 | + $parsed_url[$url_part] = $cdn[$url_part]; |
|
| 131 | 131 | } else { |
| 132 | - unset( $parsed_url[ $url_part ] ); |
|
| 132 | + unset($parsed_url[$url_part]); |
|
| 133 | 133 | } |
| 134 | 134 | } |
| 135 | 135 | |
| 136 | - if ( ! empty( $this->options['external_cdn_link'] ) ) { |
|
| 137 | - $cdn_path = parse_url( $this->options['external_cdn_link'], PHP_URL_PATH ); |
|
| 136 | + if ( ! empty($this->options['external_cdn_link'])) { |
|
| 137 | + $cdn_path = parse_url($this->options['external_cdn_link'], PHP_URL_PATH); |
|
| 138 | 138 | |
| 139 | - if ( isset( $cdn_path, $parsed_url['path'] ) && $cdn_path !== '/' && ! empty( $parsed_url['path'] ) ) { |
|
| 140 | - $parsed_url['path'] = str_replace( $cdn_path, '', $parsed_url['path'] ); |
|
| 139 | + if (isset($cdn_path, $parsed_url['path']) && $cdn_path !== '/' && ! empty($parsed_url['path'])) { |
|
| 140 | + $parsed_url['path'] = str_replace($cdn_path, '', $parsed_url['path']); |
|
| 141 | 141 | } |
| 142 | 142 | } |
| 143 | 143 | |
| 144 | - $url = http_build_url( $parsed_url ); |
|
| 144 | + $url = http_build_url($parsed_url); |
|
| 145 | 145 | |
| 146 | - $url = add_query_arg( $this->get_global_params(), $url ); |
|
| 146 | + $url = add_query_arg($this->get_global_params(), $url); |
|
| 147 | 147 | } |
| 148 | 148 | } |
| 149 | 149 | |
@@ -159,40 +159,40 @@ discard block |
||
| 159 | 159 | * |
| 160 | 160 | * @return false|array |
| 161 | 161 | */ |
| 162 | - public function image_downsize( $return, $attachment_id, $size ) { |
|
| 163 | - if ( ! empty ( $this->options['cdn_link'] ) ) { |
|
| 164 | - $img_url = wp_get_attachment_url( $attachment_id ); |
|
| 162 | + public function image_downsize($return, $attachment_id, $size) { |
|
| 163 | + if ( ! empty ($this->options['cdn_link'])) { |
|
| 164 | + $img_url = wp_get_attachment_url($attachment_id); |
|
| 165 | 165 | |
| 166 | 166 | $params = []; |
| 167 | - if ( is_array( $size ) ) { |
|
| 168 | - $params['w'] = $width = isset( $size[0] ) ? $size[0] : 0; |
|
| 169 | - $params['h'] = $height = isset( $size[1] ) ? $size[1] : 0; |
|
| 167 | + if (is_array($size)) { |
|
| 168 | + $params['w'] = $width = isset($size[0]) ? $size[0] : 0; |
|
| 169 | + $params['h'] = $height = isset($size[1]) ? $size[1] : 0; |
|
| 170 | 170 | } else { |
| 171 | 171 | $available_sizes = $this->get_all_defined_sizes(); |
| 172 | - if ( isset( $available_sizes[ $size ] ) ) { |
|
| 173 | - $size = $available_sizes[ $size ]; |
|
| 172 | + if (isset($available_sizes[$size])) { |
|
| 173 | + $size = $available_sizes[$size]; |
|
| 174 | 174 | $params['w'] = $width = $size['width']; |
| 175 | 175 | $params['h'] = $height = $size['height']; |
| 176 | 176 | } |
| 177 | 177 | } |
| 178 | 178 | |
| 179 | - $params = array_filter( $params ); |
|
| 179 | + $params = array_filter($params); |
|
| 180 | 180 | |
| 181 | - $img_url = add_query_arg( $params, $img_url ); |
|
| 181 | + $img_url = add_query_arg($params, $img_url); |
|
| 182 | 182 | |
| 183 | - if ( ! isset( $width ) || ! isset( $height ) ) { |
|
| 183 | + if ( ! isset($width) || ! isset($height)) { |
|
| 184 | 184 | // any other type: use the real image |
| 185 | - $meta = wp_get_attachment_metadata( $attachment_id ); |
|
| 185 | + $meta = wp_get_attachment_metadata($attachment_id); |
|
| 186 | 186 | |
| 187 | 187 | // Image sizes is missing for pdf thumbnails |
| 188 | - $meta['width'] = isset( $meta['width'] ) ? $meta['width'] : 0; |
|
| 189 | - $meta['height'] = isset( $meta['height'] ) ? $meta['height'] : 0; |
|
| 188 | + $meta['width'] = isset($meta['width']) ? $meta['width'] : 0; |
|
| 189 | + $meta['height'] = isset($meta['height']) ? $meta['height'] : 0; |
|
| 190 | 190 | |
| 191 | - $width = isset( $width ) ? $width : $meta['width']; |
|
| 192 | - $height = isset( $height ) ? $height : $meta['height']; |
|
| 191 | + $width = isset($width) ? $width : $meta['width']; |
|
| 192 | + $height = isset($height) ? $height : $meta['height']; |
|
| 193 | 193 | } |
| 194 | 194 | |
| 195 | - $return = [ $img_url, $width, $height, true ]; |
|
| 195 | + $return = [$img_url, $width, $height, true]; |
|
| 196 | 196 | } |
| 197 | 197 | |
| 198 | 198 | return $return; |
@@ -209,16 +209,16 @@ discard block |
||
| 209 | 209 | * |
| 210 | 210 | * @return array |
| 211 | 211 | */ |
| 212 | - public function calculate_image_srcset( $sources, $size_array, $image_src, $image_meta, $attachment_id ) { |
|
| 213 | - if ( ! empty ( $this->options['cdn_link'] ) ) { |
|
| 214 | - foreach ( $sources as $i => $image_size ) { |
|
| 215 | - if ( $image_size['descriptor'] === 'w' ) { |
|
| 216 | - if ( $attachment_id ) { |
|
| 217 | - $image_src = wp_get_attachment_url( $attachment_id ); |
|
| 212 | + public function calculate_image_srcset($sources, $size_array, $image_src, $image_meta, $attachment_id) { |
|
| 213 | + if ( ! empty ($this->options['cdn_link'])) { |
|
| 214 | + foreach ($sources as $i => $image_size) { |
|
| 215 | + if ($image_size['descriptor'] === 'w') { |
|
| 216 | + if ($attachment_id) { |
|
| 217 | + $image_src = wp_get_attachment_url($attachment_id); |
|
| 218 | 218 | } |
| 219 | 219 | |
| 220 | - $image_src = remove_query_arg( 'h', $image_src ); |
|
| 221 | - $sources[ $i ]['url'] = add_query_arg( 'w', $image_size['value'], $image_src ); |
|
| 220 | + $image_src = remove_query_arg('h', $image_src); |
|
| 221 | + $sources[$i]['url'] = add_query_arg('w', $image_size['value'], $image_src); |
|
| 222 | 222 | } |
| 223 | 223 | } |
| 224 | 224 | } |
@@ -233,29 +233,29 @@ discard block |
||
| 233 | 233 | * |
| 234 | 234 | * @return string |
| 235 | 235 | */ |
| 236 | - public function replace_images_in_content( $content ) { |
|
| 236 | + public function replace_images_in_content($content) { |
|
| 237 | 237 | // 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. |
| 238 | - if ( ! empty ( $this->options['cdn_link'] ) ) { |
|
| 239 | - if ( preg_match_all( '/<img\s[^>]*src=([\"\']??)([^\" >]*?)\1[^>]*>/iU', $content, $matches ) ) { |
|
| 240 | - foreach ( $matches[2] as $image_src ) { |
|
| 241 | - $content = str_replace( $image_src, apply_filters( 'wp_get_attachment_url', $image_src, null ), $content ); |
|
| 238 | + if ( ! empty ($this->options['cdn_link'])) { |
|
| 239 | + if (preg_match_all('/<img\s[^>]*src=([\"\']??)([^\" >]*?)\1[^>]*>/iU', $content, $matches)) { |
|
| 240 | + foreach ($matches[2] as $image_src) { |
|
| 241 | + $content = str_replace($image_src, apply_filters('wp_get_attachment_url', $image_src, null), $content); |
|
| 242 | 242 | } |
| 243 | 243 | } |
| 244 | 244 | |
| 245 | - if ( preg_match_all( '/<img\s[^>]*srcset=([\"\']??)([^\">]*?)\1[^>]*\/?>/iU', $content, $matches ) ) { |
|
| 245 | + if (preg_match_all('/<img\s[^>]*srcset=([\"\']??)([^\">]*?)\1[^>]*\/?>/iU', $content, $matches)) { |
|
| 246 | 246 | |
| 247 | - foreach ( $matches[2] as $image_srcset ) { |
|
| 248 | - $new_image_srcset = preg_replace_callback( '/(\S+)(\s\d+\w)/', function ( $srcset_matches ) { |
|
| 249 | - return apply_filters( 'wp_get_attachment_url', $srcset_matches[1], null ) . $srcset_matches[2]; |
|
| 250 | - }, $image_srcset ); |
|
| 247 | + foreach ($matches[2] as $image_srcset) { |
|
| 248 | + $new_image_srcset = preg_replace_callback('/(\S+)(\s\d+\w)/', function($srcset_matches) { |
|
| 249 | + return apply_filters('wp_get_attachment_url', $srcset_matches[1], null) . $srcset_matches[2]; |
|
| 250 | + }, $image_srcset); |
|
| 251 | 251 | |
| 252 | - $content = str_replace( $image_srcset, $new_image_srcset, $content ); |
|
| 252 | + $content = str_replace($image_srcset, $new_image_srcset, $content); |
|
| 253 | 253 | } |
| 254 | 254 | } |
| 255 | 255 | |
| 256 | - if ( preg_match_all( '/<a\s[^>]*href=([\"\']??)([^\" >]*?)\1[^>]*>(.*)<\/a>/iU', $content, $matches ) ) { |
|
| 257 | - foreach ( $matches[0] as $link ) { |
|
| 258 | - $content = str_replace( $link[2], apply_filters( 'wp_get_attachment_url', $link[2], null ), $content ); |
|
| 256 | + if (preg_match_all('/<a\s[^>]*href=([\"\']??)([^\" >]*?)\1[^>]*>(.*)<\/a>/iU', $content, $matches)) { |
|
| 257 | + foreach ($matches[0] as $link) { |
|
| 258 | + $content = str_replace($link[2], apply_filters('wp_get_attachment_url', $link[2], null), $content); |
|
| 259 | 259 | } |
| 260 | 260 | } |
| 261 | 261 | } |
@@ -266,12 +266,12 @@ discard block |
||
| 266 | 266 | * Add tag to dns prefetch cdn host |
| 267 | 267 | */ |
| 268 | 268 | public function prefetch_cdn() { |
| 269 | - if ( ! empty ( $this->options['cdn_link'] ) ) { |
|
| 270 | - $host = parse_url( $this->options['cdn_link'], PHP_URL_HOST ); |
|
| 269 | + if ( ! empty ($this->options['cdn_link'])) { |
|
| 270 | + $host = parse_url($this->options['cdn_link'], PHP_URL_HOST); |
|
| 271 | 271 | |
| 272 | 272 | printf( |
| 273 | 273 | '<link rel="dns-prefetch" href="%s"/>', |
| 274 | - esc_attr( '//' . $host ) |
|
| 274 | + esc_attr('//' . $host) |
|
| 275 | 275 | ); |
| 276 | 276 | } |
| 277 | 277 | } |
@@ -280,8 +280,8 @@ discard block |
||
| 280 | 280 | * Start output buffer if auto retina is enabled |
| 281 | 281 | */ |
| 282 | 282 | public function buffer_start_for_retina() { |
| 283 | - if ( ! empty ( $this->options['add_dpi2_srcset'] ) ) { |
|
| 284 | - $this->buffer_started = ob_start( [ $this, 'add_retina' ] ); |
|
| 283 | + if ( ! empty ($this->options['add_dpi2_srcset'])) { |
|
| 284 | + $this->buffer_started = ob_start([$this, 'add_retina']); |
|
| 285 | 285 | } |
| 286 | 286 | } |
| 287 | 287 | |
@@ -289,7 +289,7 @@ discard block |
||
| 289 | 289 | * Stop output buffer if it was enabled by the plugin |
| 290 | 290 | */ |
| 291 | 291 | public function buffer_end_for_retina() { |
| 292 | - if ( $this->buffer_started && ob_get_level() ) { |
|
| 292 | + if ($this->buffer_started && ob_get_level()) { |
|
| 293 | 293 | ob_end_flush(); |
| 294 | 294 | } |
| 295 | 295 | } |
@@ -305,20 +305,20 @@ discard block |
||
| 305 | 305 | |
| 306 | 306 | // For now, only "auto" is supported. |
| 307 | 307 | $auto = []; |
| 308 | - if ( ! empty ( $this->options['auto_format'] ) ) { |
|
| 309 | - array_push( $auto, 'format' ); |
|
| 308 | + if ( ! empty ($this->options['auto_format'])) { |
|
| 309 | + array_push($auto, 'format'); |
|
| 310 | 310 | } |
| 311 | 311 | |
| 312 | - if ( ! empty ( $this->options['auto_enhance'] ) ) { |
|
| 313 | - array_push( $auto, 'enhance' ); |
|
| 312 | + if ( ! empty ($this->options['auto_enhance'])) { |
|
| 313 | + array_push($auto, 'enhance'); |
|
| 314 | 314 | } |
| 315 | 315 | |
| 316 | - if ( ! empty ( $this->options['auto_compress'] ) ) { |
|
| 317 | - array_push( $auto, 'compress' ); |
|
| 316 | + if ( ! empty ($this->options['auto_compress'])) { |
|
| 317 | + array_push($auto, 'compress'); |
|
| 318 | 318 | } |
| 319 | 319 | |
| 320 | - if ( ! empty( $auto ) ) { |
|
| 321 | - $params['auto'] = implode( '%2C', $auto ); |
|
| 320 | + if ( ! empty($auto)) { |
|
| 321 | + $params['auto'] = implode('%2C', $auto); |
|
| 322 | 322 | } |
| 323 | 323 | |
| 324 | 324 | return $params; |
@@ -334,18 +334,18 @@ discard block |
||
| 334 | 334 | $theme_image_sizes = wp_get_additional_image_sizes(); |
| 335 | 335 | |
| 336 | 336 | $sizes = []; |
| 337 | - foreach ( get_intermediate_image_sizes() as $s ) { |
|
| 338 | - $sizes[ $s ] = [ 'width' => '', 'height' => '', 'crop' => false ]; |
|
| 339 | - if ( isset( $theme_image_sizes[ $s ] ) ) { |
|
| 337 | + foreach (get_intermediate_image_sizes() as $s) { |
|
| 338 | + $sizes[$s] = ['width' => '', 'height' => '', 'crop' => false]; |
|
| 339 | + if (isset($theme_image_sizes[$s])) { |
|
| 340 | 340 | // For theme-added sizes |
| 341 | - $sizes[ $s ]['width'] = intval( $theme_image_sizes[ $s ]['width'] ); |
|
| 342 | - $sizes[ $s ]['height'] = intval( $theme_image_sizes[ $s ]['height'] ); |
|
| 343 | - $sizes[ $s ]['crop'] = $theme_image_sizes[ $s ]['crop']; |
|
| 341 | + $sizes[$s]['width'] = intval($theme_image_sizes[$s]['width']); |
|
| 342 | + $sizes[$s]['height'] = intval($theme_image_sizes[$s]['height']); |
|
| 343 | + $sizes[$s]['crop'] = $theme_image_sizes[$s]['crop']; |
|
| 344 | 344 | } else { |
| 345 | 345 | // For default sizes set in options |
| 346 | - $sizes[ $s ]['width'] = get_option( "{$s}_size_w" ); |
|
| 347 | - $sizes[ $s ]['height'] = get_option( "{$s}_size_h" ); |
|
| 348 | - $sizes[ $s ]['crop'] = get_option( "{$s}_crop" ); |
|
| 346 | + $sizes[$s]['width'] = get_option("{$s}_size_w"); |
|
| 347 | + $sizes[$s]['height'] = get_option("{$s}_size_h"); |
|
| 348 | + $sizes[$s]['crop'] = get_option("{$s}_crop"); |
|
| 349 | 349 | } |
| 350 | 350 | } |
| 351 | 351 | |