@@ -27,18 +27,18 @@ 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( 'the_content', [ $this, 'replace_images_in_content' ] ); |
|
| 38 | - add_action( 'wp_head', [ $this, 'prefetch_cdn' ], 1 ); |
|
| 37 | + add_filter('the_content', [$this, 'replace_images_in_content']); |
|
| 38 | + add_action('wp_head', [$this, 'prefetch_cdn'], 1); |
|
| 39 | 39 | |
| 40 | - add_action( 'after_setup_theme', [ $this, 'buffer_start_for_retina' ] ); |
|
| 41 | - add_action( 'shutdown', [ $this, 'buffer_end_for_retina' ] ); |
|
| 40 | + add_action('after_setup_theme', [$this, 'buffer_start_for_retina']); |
|
| 41 | + add_action('shutdown', [$this, 'buffer_end_for_retina']); |
|
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | /** |
@@ -47,7 +47,7 @@ discard block |
||
| 47 | 47 | * @return Images_Via_Imgix |
| 48 | 48 | */ |
| 49 | 49 | public static function instance() { |
| 50 | - if ( ! isset( self::$instance ) ) { |
|
| 50 | + if ( ! isset(self::$instance)) { |
|
| 51 | 51 | self::$instance = new self; |
| 52 | 52 | } |
| 53 | 53 | |
@@ -60,7 +60,7 @@ discard block |
||
| 60 | 60 | * |
| 61 | 61 | * @param array $options |
| 62 | 62 | */ |
| 63 | - public function set_options( $options ) { |
|
| 63 | + public function set_options($options) { |
|
| 64 | 64 | $this->options = $options; |
| 65 | 65 | } |
| 66 | 66 | |
@@ -73,13 +73,13 @@ discard block |
||
| 73 | 73 | * |
| 74 | 74 | * @return string Content with retina-enriched image tags. |
| 75 | 75 | */ |
| 76 | - public function add_retina( $content ) { |
|
| 76 | + public function add_retina($content) { |
|
| 77 | 77 | $pattern = '/<img((?![^>]+srcset )([^>]*)'; |
| 78 | 78 | $pattern .= 'src=[\'"]([^\'"]*imgix.net[^\'"]*\?[^\'"]*w=[^\'"]*)[\'"]([^>]*)*?)>/i'; |
| 79 | 79 | $repl = '<img$2src="$3" srcset="${3}, ${3}&dpr=2 2x, ${3}&dpr=3 3x,"$4>'; |
| 80 | - $content = preg_replace( $pattern, $repl, $content ); |
|
| 80 | + $content = preg_replace($pattern, $repl, $content); |
|
| 81 | 81 | |
| 82 | - return preg_replace( $pattern, $repl, $content ); |
|
| 82 | + return preg_replace($pattern, $repl, $content); |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | /** |
@@ -89,39 +89,39 @@ discard block |
||
| 89 | 89 | * |
| 90 | 90 | * @return string |
| 91 | 91 | */ |
| 92 | - public function replace_image_url( $url ) { |
|
| 93 | - if ( ! empty ( $this->options['cdn_link'] ) ) { |
|
| 94 | - $pathinfo = pathinfo( $url ); |
|
| 92 | + public function replace_image_url($url) { |
|
| 93 | + if ( ! empty ($this->options['cdn_link'])) { |
|
| 94 | + $pathinfo = pathinfo($url); |
|
| 95 | 95 | |
| 96 | - if ( isset( $pathinfo['extension'] ) && in_array( $pathinfo['extension'], [ |
|
| 96 | + if (isset($pathinfo['extension']) && in_array($pathinfo['extension'], [ |
|
| 97 | 97 | 'jpg', |
| 98 | 98 | 'gif', |
| 99 | 99 | 'png', |
| 100 | 100 | 'jpeg' |
| 101 | - ] ) |
|
| 101 | + ]) |
|
| 102 | 102 | ) { |
| 103 | - $parsed_url = parse_url( $url ); |
|
| 104 | - if ( isset( $parsed_url['host'] ) && $parsed_url['host'] === parse_url( home_url( '/' ), PHP_URL_HOST ) ) { |
|
| 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 ]; |
|
| 103 | + $parsed_url = parse_url($url); |
|
| 104 | + if (isset($parsed_url['host']) && $parsed_url['host'] === parse_url(home_url('/'), PHP_URL_HOST)) { |
|
| 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 | 113 | |
| 114 | 114 | $arguments = []; |
| 115 | - if ( ! empty( $parsed_url['query'] ) ) { |
|
| 116 | - parse_str( $parsed_url['query'], $arguments ); |
|
| 115 | + if ( ! empty($parsed_url['query'])) { |
|
| 116 | + parse_str($parsed_url['query'], $arguments); |
|
| 117 | 117 | } |
| 118 | - $arguments = array_merge( $arguments, $this->get_global_params() ); |
|
| 118 | + $arguments = array_merge($arguments, $this->get_global_params()); |
|
| 119 | 119 | |
| 120 | - if ( ! empty( $arguments ) ) { |
|
| 121 | - $parsed_url['query'] = build_query( urlencode_deep( $arguments ) ); |
|
| 120 | + if ( ! empty($arguments)) { |
|
| 121 | + $parsed_url['query'] = build_query(urlencode_deep($arguments)); |
|
| 122 | 122 | } |
| 123 | 123 | |
| 124 | - $url = http_build_url( $parsed_url ); |
|
| 124 | + $url = http_build_url($parsed_url); |
|
| 125 | 125 | } |
| 126 | 126 | } |
| 127 | 127 | } |
@@ -138,45 +138,45 @@ discard block |
||
| 138 | 138 | * |
| 139 | 139 | * @return false|array |
| 140 | 140 | */ |
| 141 | - public function image_downsize( $return, $attachment_id, $size ) { |
|
| 142 | - if ( ! empty ( $this->options['cdn_link'] ) ) { |
|
| 143 | - $img_url = wp_get_attachment_url( $attachment_id ); |
|
| 144 | - $parsed_url = parse_url( $img_url ); |
|
| 141 | + public function image_downsize($return, $attachment_id, $size) { |
|
| 142 | + if ( ! empty ($this->options['cdn_link'])) { |
|
| 143 | + $img_url = wp_get_attachment_url($attachment_id); |
|
| 144 | + $parsed_url = parse_url($img_url); |
|
| 145 | 145 | |
| 146 | - if ( is_array( $parsed_url ) ) { |
|
| 146 | + if (is_array($parsed_url)) { |
|
| 147 | 147 | $params = []; |
| 148 | - if ( ! empty( $parsed_url['query'] ) ) { |
|
| 149 | - parse_str( $parsed_url['query'], $params ); |
|
| 148 | + if ( ! empty($parsed_url['query'])) { |
|
| 149 | + parse_str($parsed_url['query'], $params); |
|
| 150 | 150 | } |
| 151 | 151 | |
| 152 | - if ( is_array( $size ) ) { |
|
| 153 | - $params['w'] = $width = isset( $size[0] ) ? $size[0] : 0; |
|
| 154 | - $params['h'] = $height = isset( $size[1] ) ? $size[1] : 0; |
|
| 152 | + if (is_array($size)) { |
|
| 153 | + $params['w'] = $width = isset($size[0]) ? $size[0] : 0; |
|
| 154 | + $params['h'] = $height = isset($size[1]) ? $size[1] : 0; |
|
| 155 | 155 | } else { |
| 156 | 156 | $available_sizes = $this->get_all_defined_sizes(); |
| 157 | - if ( isset( $available_sizes[ $size ] ) ) { |
|
| 158 | - $size = $available_sizes[ $size ]; |
|
| 157 | + if (isset($available_sizes[$size])) { |
|
| 158 | + $size = $available_sizes[$size]; |
|
| 159 | 159 | $params['w'] = $width = $size['width']; |
| 160 | 160 | $params['h'] = $height = $size['height']; |
| 161 | 161 | } |
| 162 | 162 | } |
| 163 | 163 | |
| 164 | - $params = array_filter( $params ); |
|
| 164 | + $params = array_filter($params); |
|
| 165 | 165 | |
| 166 | - if ( ! empty( $params ) ) { |
|
| 167 | - $parsed_url['query'] = build_query( urlencode_deep( $params ) ); |
|
| 166 | + if ( ! empty($params)) { |
|
| 167 | + $parsed_url['query'] = build_query(urlencode_deep($params)); |
|
| 168 | 168 | } |
| 169 | 169 | |
| 170 | - $img_url = http_build_url( $parsed_url ); |
|
| 170 | + $img_url = http_build_url($parsed_url); |
|
| 171 | 171 | |
| 172 | - if ( ! isset( $width ) || ! isset( $height ) ) { |
|
| 172 | + if ( ! isset($width) || ! isset($height)) { |
|
| 173 | 173 | // any other type: use the real image |
| 174 | - $meta = wp_get_attachment_metadata( $attachment_id ); |
|
| 175 | - $width = isset( $width ) ? $width : $meta['width']; |
|
| 176 | - $height = isset( $height ) ? $height : $meta['height']; |
|
| 174 | + $meta = wp_get_attachment_metadata($attachment_id); |
|
| 175 | + $width = isset($width) ? $width : $meta['width']; |
|
| 176 | + $height = isset($height) ? $height : $meta['height']; |
|
| 177 | 177 | } |
| 178 | 178 | |
| 179 | - $return = [ $img_url, $width, $height, true ]; |
|
| 179 | + $return = [$img_url, $width, $height, true]; |
|
| 180 | 180 | } |
| 181 | 181 | } |
| 182 | 182 | |
@@ -190,11 +190,11 @@ discard block |
||
| 190 | 190 | * |
| 191 | 191 | * @return string |
| 192 | 192 | */ |
| 193 | - public function replace_images_in_content( $content ) { |
|
| 194 | - if ( ! empty ( $this->options['cdn_link'] ) ) { |
|
| 195 | - $content = preg_replace_callback( '/(?<=\shref="|\ssrc="|\shref=\'|\ssrc=\').*(?=\'|")/', function ( $match ) { |
|
| 196 | - return esc_url( apply_filters( 'imgix/add-image-url', $match[0] ) ); |
|
| 197 | - }, $content ); |
|
| 193 | + public function replace_images_in_content($content) { |
|
| 194 | + if ( ! empty ($this->options['cdn_link'])) { |
|
| 195 | + $content = preg_replace_callback('/(?<=\shref="|\ssrc="|\shref=\'|\ssrc=\').*(?=\'|")/', function($match) { |
|
| 196 | + return esc_url(apply_filters('imgix/add-image-url', $match[0])); |
|
| 197 | + }, $content); |
|
| 198 | 198 | |
| 199 | 199 | } |
| 200 | 200 | |
@@ -205,12 +205,12 @@ discard block |
||
| 205 | 205 | * Add tag to dns prefetch cdn host |
| 206 | 206 | */ |
| 207 | 207 | public function prefetch_cdn() { |
| 208 | - if ( ! empty ( $this->options['cdn_link'] ) ) { |
|
| 209 | - $host = parse_url( $this->options['cdn_link'], PHP_URL_HOST ); |
|
| 208 | + if ( ! empty ($this->options['cdn_link'])) { |
|
| 209 | + $host = parse_url($this->options['cdn_link'], PHP_URL_HOST); |
|
| 210 | 210 | |
| 211 | 211 | printf( |
| 212 | 212 | '<link rel="dns-prefetch" href="%s"/>', |
| 213 | - esc_attr( '//' . $host ) |
|
| 213 | + esc_attr('//' . $host) |
|
| 214 | 214 | ); |
| 215 | 215 | } |
| 216 | 216 | } |
@@ -219,9 +219,9 @@ discard block |
||
| 219 | 219 | * Start output buffer if auto retina is enabled |
| 220 | 220 | */ |
| 221 | 221 | public function buffer_start_for_retina() { |
| 222 | - if ( ! empty ( $this->options['add_dpi2_srcset'] ) ) { |
|
| 222 | + if ( ! empty ($this->options['add_dpi2_srcset'])) { |
|
| 223 | 223 | $this->buffer_started = true; |
| 224 | - ob_start( [ $this, 'add_retina' ] ); |
|
| 224 | + ob_start([$this, 'add_retina']); |
|
| 225 | 225 | } |
| 226 | 226 | } |
| 227 | 227 | |
@@ -229,7 +229,7 @@ discard block |
||
| 229 | 229 | * Stop output buffer if it was enabled by the plugin |
| 230 | 230 | */ |
| 231 | 231 | public function buffer_end_for_retina() { |
| 232 | - if ( $this->buffer_started === true ) { |
|
| 232 | + if ($this->buffer_started === true) { |
|
| 233 | 233 | ob_end_flush(); |
| 234 | 234 | } |
| 235 | 235 | } |
@@ -245,16 +245,16 @@ discard block |
||
| 245 | 245 | |
| 246 | 246 | // For now, only "auto" is supported. |
| 247 | 247 | $auto = []; |
| 248 | - if ( ! empty ( $this->options['auto_format'] ) ) { |
|
| 249 | - array_push( $auto, 'format' ); |
|
| 248 | + if ( ! empty ($this->options['auto_format'])) { |
|
| 249 | + array_push($auto, 'format'); |
|
| 250 | 250 | } |
| 251 | 251 | |
| 252 | - if ( ! empty ( $this->options['auto_enhance'] ) ) { |
|
| 253 | - array_push( $auto, 'enhance' ); |
|
| 252 | + if ( ! empty ($this->options['auto_enhance'])) { |
|
| 253 | + array_push($auto, 'enhance'); |
|
| 254 | 254 | } |
| 255 | 255 | |
| 256 | - if ( ! empty( $auto ) ) { |
|
| 257 | - $params['auto'] = implode( '%2C', $auto ); |
|
| 256 | + if ( ! empty($auto)) { |
|
| 257 | + $params['auto'] = implode('%2C', $auto); |
|
| 258 | 258 | } |
| 259 | 259 | |
| 260 | 260 | return $params; |
@@ -270,18 +270,18 @@ discard block |
||
| 270 | 270 | $theme_image_sizes = wp_get_additional_image_sizes(); |
| 271 | 271 | |
| 272 | 272 | $sizes = []; |
| 273 | - foreach ( get_intermediate_image_sizes() as $s ) { |
|
| 274 | - $sizes[ $s ] = [ 'width' => '', 'height' => '', 'crop' => false ]; |
|
| 275 | - if ( isset( $theme_image_sizes[ $s ] ) ) { |
|
| 273 | + foreach (get_intermediate_image_sizes() as $s) { |
|
| 274 | + $sizes[$s] = ['width' => '', 'height' => '', 'crop' => false]; |
|
| 275 | + if (isset($theme_image_sizes[$s])) { |
|
| 276 | 276 | // For theme-added sizes |
| 277 | - $sizes[ $s ]['width'] = intval( $theme_image_sizes[ $s ]['width'] ); |
|
| 278 | - $sizes[ $s ]['height'] = intval( $theme_image_sizes[ $s ]['height'] ); |
|
| 279 | - $sizes[ $s ]['crop'] = $theme_image_sizes[ $s ]['crop']; |
|
| 277 | + $sizes[$s]['width'] = intval($theme_image_sizes[$s]['width']); |
|
| 278 | + $sizes[$s]['height'] = intval($theme_image_sizes[$s]['height']); |
|
| 279 | + $sizes[$s]['crop'] = $theme_image_sizes[$s]['crop']; |
|
| 280 | 280 | } else { |
| 281 | 281 | // For default sizes set in options |
| 282 | - $sizes[ $s ]['width'] = get_option( "{$s}_size_w" ); |
|
| 283 | - $sizes[ $s ]['height'] = get_option( "{$s}_size_h" ); |
|
| 284 | - $sizes[ $s ]['crop'] = get_option( "{$s}_crop" ); |
|
| 282 | + $sizes[$s]['width'] = get_option("{$s}_size_w"); |
|
| 283 | + $sizes[$s]['height'] = get_option("{$s}_size_h"); |
|
| 284 | + $sizes[$s]['crop'] = get_option("{$s}_crop"); |
|
| 285 | 285 | } |
| 286 | 286 | } |
| 287 | 287 | |
@@ -5,7 +5,7 @@ discard block |
||
| 5 | 5 | * @package imgix |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | -if ( ! function_exists( 'http_build_url' ) ) { |
|
| 8 | +if ( ! function_exists('http_build_url')) { |
|
| 9 | 9 | /** |
| 10 | 10 | * This is a simplified version of http_build_url if pecl_http is missing |
| 11 | 11 | * |
@@ -13,22 +13,22 @@ discard block |
||
| 13 | 13 | * |
| 14 | 14 | * @return string |
| 15 | 15 | */ |
| 16 | - function http_build_url( $parsed_url ) { |
|
| 17 | - $scheme = isset( $parsed_url['scheme'] ) ? $parsed_url['scheme'] . '://' : ''; |
|
| 18 | - $host = isset( $parsed_url['host'] ) ? $parsed_url['host'] : ''; |
|
| 19 | - $port = isset( $parsed_url['port'] ) ? ':' . $parsed_url['port'] : ''; |
|
| 20 | - $user = isset( $parsed_url['user'] ) ? $parsed_url['user'] : ''; |
|
| 21 | - $pass = isset( $parsed_url['pass'] ) ? ':' . $parsed_url['pass'] : ''; |
|
| 22 | - $pass = ( $user || $pass ) ? "$pass@" : ''; |
|
| 23 | - $path = isset( $parsed_url['path'] ) ? $parsed_url['path'] : ''; |
|
| 24 | - $query = isset( $parsed_url['query'] ) ? '?' . $parsed_url['query'] : ''; |
|
| 25 | - $fragment = isset( $parsed_url['fragment'] ) ? '#' . $parsed_url['fragment'] : ''; |
|
| 16 | + function http_build_url($parsed_url) { |
|
| 17 | + $scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : ''; |
|
| 18 | + $host = isset($parsed_url['host']) ? $parsed_url['host'] : ''; |
|
| 19 | + $port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : ''; |
|
| 20 | + $user = isset($parsed_url['user']) ? $parsed_url['user'] : ''; |
|
| 21 | + $pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : ''; |
|
| 22 | + $pass = ($user || $pass) ? "$pass@" : ''; |
|
| 23 | + $path = isset($parsed_url['path']) ? $parsed_url['path'] : ''; |
|
| 24 | + $query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : ''; |
|
| 25 | + $fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : ''; |
|
| 26 | 26 | |
| 27 | 27 | return "$scheme$user$pass$host$port$path$query$fragment"; |
| 28 | 28 | } |
| 29 | 29 | } |
| 30 | 30 | |
| 31 | -if ( ! function_exists( 'wp_get_additional_image_sizes' ) ) { |
|
| 31 | +if ( ! function_exists('wp_get_additional_image_sizes')) { |
|
| 32 | 32 | /** |
| 33 | 33 | * Retrieve additional image sizes. |
| 34 | 34 | * |
@@ -40,7 +40,7 @@ discard block |
||
| 40 | 40 | */ |
| 41 | 41 | function wp_get_additional_image_sizes() { |
| 42 | 42 | global $_wp_additional_image_sizes; |
| 43 | - if ( ! $_wp_additional_image_sizes ) { |
|
| 43 | + if ( ! $_wp_additional_image_sizes) { |
|
| 44 | 44 | $_wp_additional_image_sizes = []; |
| 45 | 45 | } |
| 46 | 46 | |