@@ -27,19 +27,19 @@ 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, 'replace_host_in_srcset' ], 10 ); |
|
| 38 | - add_filter( 'the_content', [ $this, 'replace_images_in_content' ] ); |
|
| 39 | - add_action( 'wp_head', [ $this, 'prefetch_cdn' ], 1 ); |
|
| 37 | + add_filter('wp_calculate_image_srcset', [$this, 'replace_host_in_srcset'], 10); |
|
| 38 | + add_filter('the_content', [$this, 'replace_images_in_content']); |
|
| 39 | + add_action('wp_head', [$this, 'prefetch_cdn'], 1); |
|
| 40 | 40 | |
| 41 | - add_action( 'after_setup_theme', [ $this, 'buffer_start_for_retina' ] ); |
|
| 42 | - add_action( 'shutdown', [ $this, 'buffer_end_for_retina' ] ); |
|
| 41 | + add_action('after_setup_theme', [$this, 'buffer_start_for_retina']); |
|
| 42 | + add_action('shutdown', [$this, 'buffer_end_for_retina']); |
|
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | /** |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | * @return Images_Via_Imgix |
| 49 | 49 | */ |
| 50 | 50 | public static function instance() { |
| 51 | - if ( ! isset( self::$instance ) ) { |
|
| 51 | + if ( ! isset(self::$instance)) { |
|
| 52 | 52 | self::$instance = new self; |
| 53 | 53 | } |
| 54 | 54 | |
@@ -61,7 +61,7 @@ discard block |
||
| 61 | 61 | * |
| 62 | 62 | * @param array $options |
| 63 | 63 | */ |
| 64 | - public function set_options( $options ) { |
|
| 64 | + public function set_options($options) { |
|
| 65 | 65 | $this->options = $options; |
| 66 | 66 | } |
| 67 | 67 | |
@@ -74,13 +74,13 @@ discard block |
||
| 74 | 74 | * |
| 75 | 75 | * @return string Content with retina-enriched image tags. |
| 76 | 76 | */ |
| 77 | - public function add_retina( $content ) { |
|
| 77 | + public function add_retina($content) { |
|
| 78 | 78 | $pattern = '/<img((?![^>]+srcset )([^>]*)'; |
| 79 | 79 | $pattern .= 'src=[\'"]([^\'"]*imgix.net[^\'"]*\?[^\'"]*w=[^\'"]*)[\'"]([^>]*)*?)>/i'; |
| 80 | 80 | $repl = '<img$2src="$3" srcset="${3}, ${3}&dpr=2 2x, ${3}&dpr=3 3x,"$4>'; |
| 81 | - $content = preg_replace( $pattern, $repl, $content ); |
|
| 81 | + $content = preg_replace($pattern, $repl, $content); |
|
| 82 | 82 | |
| 83 | - return preg_replace( $pattern, $repl, $content ); |
|
| 83 | + return preg_replace($pattern, $repl, $content); |
|
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | /** |
@@ -90,33 +90,33 @@ discard block |
||
| 90 | 90 | * |
| 91 | 91 | * @return string |
| 92 | 92 | */ |
| 93 | - public function replace_image_url( $url ) { |
|
| 94 | - if ( ! empty ( $this->options['cdn_link'] ) ) { |
|
| 95 | - $pathinfo = pathinfo( $url ); |
|
| 96 | - |
|
| 97 | - if ( in_array( $pathinfo['extension'], [ 'jpg', 'gif', 'png', 'jpeg' ] ) ) { |
|
| 98 | - $parsed_url = parse_url( $url ); |
|
| 99 | - if ( isset( $parsed_url['host'] ) && $parsed_url['host'] === parse_url( home_url( '/' ), PHP_URL_HOST ) ) { |
|
| 100 | - $cdn = parse_url( $this->options['cdn_link'] ); |
|
| 101 | - foreach ( [ 'scheme', 'host', 'port' ] as $url_part ) { |
|
| 102 | - if ( isset( $cdn[ $url_part ] ) ) { |
|
| 103 | - $parsed_url[ $url_part ] = $cdn[ $url_part ]; |
|
| 93 | + public function replace_image_url($url) { |
|
| 94 | + if ( ! empty ($this->options['cdn_link'])) { |
|
| 95 | + $pathinfo = pathinfo($url); |
|
| 96 | + |
|
| 97 | + if (in_array($pathinfo['extension'], ['jpg', 'gif', 'png', 'jpeg'])) { |
|
| 98 | + $parsed_url = parse_url($url); |
|
| 99 | + if (isset($parsed_url['host']) && $parsed_url['host'] === parse_url(home_url('/'), PHP_URL_HOST)) { |
|
| 100 | + $cdn = parse_url($this->options['cdn_link']); |
|
| 101 | + foreach (['scheme', 'host', 'port'] as $url_part) { |
|
| 102 | + if (isset($cdn[$url_part])) { |
|
| 103 | + $parsed_url[$url_part] = $cdn[$url_part]; |
|
| 104 | 104 | } else { |
| 105 | - unset( $parsed_url[ $url_part ] ); |
|
| 105 | + unset($parsed_url[$url_part]); |
|
| 106 | 106 | } |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | - list( $filename, $arguments ) = $this->convert_filename_to_size_args( $pathinfo['basename'] ); |
|
| 109 | + list($filename, $arguments) = $this->convert_filename_to_size_args($pathinfo['basename']); |
|
| 110 | 110 | |
| 111 | - $arguments = array_merge( $arguments, $this->get_global_params() ); |
|
| 111 | + $arguments = array_merge($arguments, $this->get_global_params()); |
|
| 112 | 112 | |
| 113 | - $parsed_url['path'] = trailingslashit( dirname( $parsed_url['path'] ) ) . $filename; |
|
| 113 | + $parsed_url['path'] = trailingslashit(dirname($parsed_url['path'])) . $filename; |
|
| 114 | 114 | |
| 115 | - if ( ! empty( $arguments ) ) { |
|
| 116 | - $parsed_url['query'] = empty( $parsed_url['query'] ) ? build_query( $arguments ) : $parsed_url['query'] . '&' . build_query( $arguments ); |
|
| 115 | + if ( ! empty($arguments)) { |
|
| 116 | + $parsed_url['query'] = empty($parsed_url['query']) ? build_query($arguments) : $parsed_url['query'] . '&' . build_query($arguments); |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | - $url = http_build_url( $parsed_url ); |
|
| 119 | + $url = http_build_url($parsed_url); |
|
| 120 | 120 | } |
| 121 | 121 | } |
| 122 | 122 | } |
@@ -133,40 +133,40 @@ discard block |
||
| 133 | 133 | * |
| 134 | 134 | * @return false|array |
| 135 | 135 | */ |
| 136 | - public function image_downsize( $return, $attachment_id, $size ) { |
|
| 137 | - if ( ! empty ( $this->options['cdn_link'] ) ) { |
|
| 138 | - $img_url = wp_get_attachment_url( $attachment_id ); |
|
| 139 | - $parsed_url = parse_url( $img_url ); |
|
| 136 | + public function image_downsize($return, $attachment_id, $size) { |
|
| 137 | + if ( ! empty ($this->options['cdn_link'])) { |
|
| 138 | + $img_url = wp_get_attachment_url($attachment_id); |
|
| 139 | + $parsed_url = parse_url($img_url); |
|
| 140 | 140 | |
| 141 | - if ( is_array( $parsed_url ) ) { |
|
| 142 | - parse_str( $parsed_url['query'], $params ); |
|
| 141 | + if (is_array($parsed_url)) { |
|
| 142 | + parse_str($parsed_url['query'], $params); |
|
| 143 | 143 | |
| 144 | - if ( is_array( $size ) ) { |
|
| 145 | - $params['w'] = $width = isset( $size[0] ) ? $size[0] : 0; |
|
| 146 | - $params['h'] = $height = isset( $size[1] ) ? $size[1] : 0; |
|
| 144 | + if (is_array($size)) { |
|
| 145 | + $params['w'] = $width = isset($size[0]) ? $size[0] : 0; |
|
| 146 | + $params['h'] = $height = isset($size[1]) ? $size[1] : 0; |
|
| 147 | 147 | } else { |
| 148 | 148 | $available_sizes = $this->get_all_defined_sizes(); |
| 149 | - if ( isset( $available_sizes[ $size ] ) ) { |
|
| 150 | - $size = $available_sizes[ $size ]; |
|
| 149 | + if (isset($available_sizes[$size])) { |
|
| 150 | + $size = $available_sizes[$size]; |
|
| 151 | 151 | $params['w'] = $width = $size['width']; |
| 152 | 152 | $params['h'] = $height = $size['height']; |
| 153 | 153 | } |
| 154 | 154 | } |
| 155 | 155 | |
| 156 | - $params = array_filter( $params ); |
|
| 156 | + $params = array_filter($params); |
|
| 157 | 157 | |
| 158 | - $parsed_url['query'] = build_query( $params ); |
|
| 158 | + $parsed_url['query'] = build_query($params); |
|
| 159 | 159 | |
| 160 | - $img_url = http_build_url( $parsed_url ); |
|
| 160 | + $img_url = http_build_url($parsed_url); |
|
| 161 | 161 | |
| 162 | - if ( ! isset( $width ) || ! isset( $height ) ) { |
|
| 162 | + if ( ! isset($width) || ! isset($height)) { |
|
| 163 | 163 | // any other type: use the real image |
| 164 | - $meta = wp_get_attachment_metadata( $attachment_id ); |
|
| 165 | - $width = isset( $width ) ? $width : $meta['width']; |
|
| 166 | - $height = isset( $height ) ? $height : $meta['height']; |
|
| 164 | + $meta = wp_get_attachment_metadata($attachment_id); |
|
| 165 | + $width = isset($width) ? $width : $meta['width']; |
|
| 166 | + $height = isset($height) ? $height : $meta['height']; |
|
| 167 | 167 | } |
| 168 | 168 | |
| 169 | - $return = [ $img_url, $width, $height, true ]; |
|
| 169 | + $return = [$img_url, $width, $height, true]; |
|
| 170 | 170 | } |
| 171 | 171 | } |
| 172 | 172 | |
@@ -180,9 +180,9 @@ discard block |
||
| 180 | 180 | * |
| 181 | 181 | * @return array $sources |
| 182 | 182 | */ |
| 183 | - public function replace_host_in_srcset( $sources ) { |
|
| 184 | - foreach ( $sources as $source ) { |
|
| 185 | - $sources[ $source['value'] ]['url'] = apply_filters( 'imgix/add-image-url', $sources[ $source['value'] ]['url'] ); |
|
| 183 | + public function replace_host_in_srcset($sources) { |
|
| 184 | + foreach ($sources as $source) { |
|
| 185 | + $sources[$source['value']]['url'] = apply_filters('imgix/add-image-url', $sources[$source['value']]['url']); |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | return $sources; |
@@ -195,11 +195,11 @@ discard block |
||
| 195 | 195 | * |
| 196 | 196 | * @return string |
| 197 | 197 | */ |
| 198 | - public function replace_images_in_content( $content ) { |
|
| 199 | - if ( ! empty ( $this->options['cdn_link'] ) ) { |
|
| 200 | - $content = preg_replace_callback( '/(?<=\shref="|\ssrc="|\shref=\'|\ssrc=\').*(?=\'|")/', function ( $match ) { |
|
| 201 | - return esc_url( apply_filters( 'imgix/add-image-url', $match[0] ) ); |
|
| 202 | - }, $content ); |
|
| 198 | + public function replace_images_in_content($content) { |
|
| 199 | + if ( ! empty ($this->options['cdn_link'])) { |
|
| 200 | + $content = preg_replace_callback('/(?<=\shref="|\ssrc="|\shref=\'|\ssrc=\').*(?=\'|")/', function($match) { |
|
| 201 | + return esc_url(apply_filters('imgix/add-image-url', $match[0])); |
|
| 202 | + }, $content); |
|
| 203 | 203 | |
| 204 | 204 | } |
| 205 | 205 | |
@@ -210,12 +210,12 @@ discard block |
||
| 210 | 210 | * Add tag to dns prefetch cdn host |
| 211 | 211 | */ |
| 212 | 212 | public function prefetch_cdn() { |
| 213 | - if ( ! empty ( $this->options['cdn_link'] ) ) { |
|
| 214 | - $host = parse_url( $this->options['cdn_link'], PHP_URL_HOST ); |
|
| 213 | + if ( ! empty ($this->options['cdn_link'])) { |
|
| 214 | + $host = parse_url($this->options['cdn_link'], PHP_URL_HOST); |
|
| 215 | 215 | |
| 216 | 216 | printf( |
| 217 | 217 | '<link rel="dns-prefetch" href="%s"/>', |
| 218 | - esc_attr( '//' . $host ) |
|
| 218 | + esc_attr('//' . $host) |
|
| 219 | 219 | ); |
| 220 | 220 | } |
| 221 | 221 | } |
@@ -224,9 +224,9 @@ discard block |
||
| 224 | 224 | * Start output buffer if auto retina is enabled |
| 225 | 225 | */ |
| 226 | 226 | public function buffer_start_for_retina() { |
| 227 | - if ( ! empty ( $this->options['add_dpi2_srcset'] ) ) { |
|
| 227 | + if ( ! empty ($this->options['add_dpi2_srcset'])) { |
|
| 228 | 228 | $this->buffer_started = true; |
| 229 | - ob_start( [ $this, 'add_retina' ] ); |
|
| 229 | + ob_start([$this, 'add_retina']); |
|
| 230 | 230 | } |
| 231 | 231 | } |
| 232 | 232 | |
@@ -234,7 +234,7 @@ discard block |
||
| 234 | 234 | * Stop output buffer if it was enabled by the plugin |
| 235 | 235 | */ |
| 236 | 236 | public function buffer_end_for_retina() { |
| 237 | - if ( $this->buffer_started === true ) { |
|
| 237 | + if ($this->buffer_started === true) { |
|
| 238 | 238 | ob_end_flush(); |
| 239 | 239 | } |
| 240 | 240 | } |
@@ -250,16 +250,16 @@ discard block |
||
| 250 | 250 | |
| 251 | 251 | // For now, only "auto" is supported. |
| 252 | 252 | $auto = []; |
| 253 | - if ( ! empty ( $this->options['auto_format'] ) ) { |
|
| 254 | - array_push( $auto, 'format' ); |
|
| 253 | + if ( ! empty ($this->options['auto_format'])) { |
|
| 254 | + array_push($auto, 'format'); |
|
| 255 | 255 | } |
| 256 | 256 | |
| 257 | - if ( ! empty ( $this->options['auto_enhance'] ) ) { |
|
| 258 | - array_push( $auto, 'enhance' ); |
|
| 257 | + if ( ! empty ($this->options['auto_enhance'])) { |
|
| 258 | + array_push($auto, 'enhance'); |
|
| 259 | 259 | } |
| 260 | 260 | |
| 261 | - if ( ! empty( $auto ) ) { |
|
| 262 | - $params['auto'] = implode( ',', $auto ); |
|
| 261 | + if ( ! empty($auto)) { |
|
| 262 | + $params['auto'] = implode(',', $auto); |
|
| 263 | 263 | } |
| 264 | 264 | |
| 265 | 265 | return $params; |
@@ -273,19 +273,19 @@ discard block |
||
| 273 | 273 | * |
| 274 | 274 | * @return array with filename and size arguments. |
| 275 | 275 | */ |
| 276 | - protected function convert_filename_to_size_args( $filename ) { |
|
| 276 | + protected function convert_filename_to_size_args($filename) { |
|
| 277 | 277 | $arguments = []; |
| 278 | 278 | |
| 279 | - $filename = preg_replace_callback( '/-(?<width>\d+)x(?<height>\d+)(?<extension>\.\w{3,4}$)/', function ( $match ) use ( &$arguments ) { |
|
| 279 | + $filename = preg_replace_callback('/-(?<width>\d+)x(?<height>\d+)(?<extension>\.\w{3,4}$)/', function($match) use (&$arguments) { |
|
| 280 | 280 | $arguments = [ |
| 281 | 281 | 'w' => $match['width'], |
| 282 | 282 | 'h' => $match['height'] |
| 283 | 283 | ]; |
| 284 | 284 | |
| 285 | 285 | return $match['extension']; |
| 286 | - }, $filename ); |
|
| 286 | + }, $filename); |
|
| 287 | 287 | |
| 288 | - return [ $filename, $arguments ]; |
|
| 288 | + return [$filename, $arguments]; |
|
| 289 | 289 | } |
| 290 | 290 | |
| 291 | 291 | /** |
@@ -298,30 +298,30 @@ discard block |
||
| 298 | 298 | $theme_image_sizes = wp_get_additional_image_sizes(); |
| 299 | 299 | |
| 300 | 300 | $sizes = []; |
| 301 | - foreach ( get_intermediate_image_sizes() as $s ) { |
|
| 302 | - $sizes[ $s ] = [ 'width' => '', 'height' => '', 'crop' => false ]; |
|
| 303 | - if ( isset( $theme_image_sizes[ $s ]['width'] ) ) { |
|
| 301 | + foreach (get_intermediate_image_sizes() as $s) { |
|
| 302 | + $sizes[$s] = ['width' => '', 'height' => '', 'crop' => false]; |
|
| 303 | + if (isset($theme_image_sizes[$s]['width'])) { |
|
| 304 | 304 | // For theme-added sizes |
| 305 | - $sizes[ $s ]['width'] = intval( $theme_image_sizes[ $s ]['width'] ); |
|
| 305 | + $sizes[$s]['width'] = intval($theme_image_sizes[$s]['width']); |
|
| 306 | 306 | } else { |
| 307 | 307 | // For default sizes set in options |
| 308 | - $sizes[ $s ]['width'] = get_option( "{$s}_size_w" ); |
|
| 308 | + $sizes[$s]['width'] = get_option("{$s}_size_w"); |
|
| 309 | 309 | } |
| 310 | 310 | |
| 311 | - if ( isset( $theme_image_sizes[ $s ]['height'] ) ) { |
|
| 311 | + if (isset($theme_image_sizes[$s]['height'])) { |
|
| 312 | 312 | // For theme-added sizes |
| 313 | - $sizes[ $s ]['height'] = intval( $theme_image_sizes[ $s ]['height'] ); |
|
| 313 | + $sizes[$s]['height'] = intval($theme_image_sizes[$s]['height']); |
|
| 314 | 314 | } else { |
| 315 | 315 | // For default sizes set in options |
| 316 | - $sizes[ $s ]['height'] = get_option( "{$s}_size_h" ); |
|
| 316 | + $sizes[$s]['height'] = get_option("{$s}_size_h"); |
|
| 317 | 317 | } |
| 318 | 318 | |
| 319 | - if ( isset( $theme_image_sizes[ $s ]['crop'] ) ) { |
|
| 319 | + if (isset($theme_image_sizes[$s]['crop'])) { |
|
| 320 | 320 | // For theme-added sizes |
| 321 | - $sizes[ $s ]['crop'] = $theme_image_sizes[ $s ]['crop']; |
|
| 321 | + $sizes[$s]['crop'] = $theme_image_sizes[$s]['crop']; |
|
| 322 | 322 | } else { |
| 323 | 323 | // For default sizes set in options |
| 324 | - $sizes[ $s ]['crop'] = get_option( "{$s}_crop" ); |
|
| 324 | + $sizes[$s]['crop'] = get_option("{$s}_crop"); |
|
| 325 | 325 | } |
| 326 | 326 | } |
| 327 | 327 | |