@@ -27,17 +27,17 @@ 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( 'wp_calculate_image_srcset', [ $this, 'replace_host_in_srcset' ], 10 ); |
|
| 36 | - add_filter( 'the_content', [ $this, 'replace_images_in_content' ] ); |
|
| 37 | - add_action( 'wp_head', [ $this, 'prefetch_cdn' ], 1 ); |
|
| 35 | + add_filter('wp_calculate_image_srcset', [$this, 'replace_host_in_srcset'], 10); |
|
| 36 | + add_filter('the_content', [$this, 'replace_images_in_content']); |
|
| 37 | + add_action('wp_head', [$this, 'prefetch_cdn'], 1); |
|
| 38 | 38 | |
| 39 | - add_action( 'after_setup_theme', [ $this, 'buffer_start_for_retina' ] ); |
|
| 40 | - add_action( 'shutdown', [ $this, 'buffer_end_for_retina' ] ); |
|
| 39 | + add_action('after_setup_theme', [$this, 'buffer_start_for_retina']); |
|
| 40 | + add_action('shutdown', [$this, 'buffer_end_for_retina']); |
|
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | /** |
@@ -46,7 +46,7 @@ discard block |
||
| 46 | 46 | * @return Images_Via_Imgix |
| 47 | 47 | */ |
| 48 | 48 | public static function instance() { |
| 49 | - if ( ! isset( self::$instance ) ) { |
|
| 49 | + if ( ! isset(self::$instance)) { |
|
| 50 | 50 | self::$instance = new self; |
| 51 | 51 | } |
| 52 | 52 | |
@@ -59,7 +59,7 @@ discard block |
||
| 59 | 59 | * |
| 60 | 60 | * @param array $options |
| 61 | 61 | */ |
| 62 | - public function set_options( $options ) { |
|
| 62 | + public function set_options($options) { |
|
| 63 | 63 | $this->options = $options; |
| 64 | 64 | } |
| 65 | 65 | |
@@ -72,13 +72,13 @@ discard block |
||
| 72 | 72 | * |
| 73 | 73 | * @return string Content with retina-enriched image tags. |
| 74 | 74 | */ |
| 75 | - public function add_retina( $content ) { |
|
| 75 | + public function add_retina($content) { |
|
| 76 | 76 | $pattern = '/<img((?![^>]+srcset )([^>]*)'; |
| 77 | 77 | $pattern .= 'src=[\'"]([^\'"]*imgix.net[^\'"]*\?[^\'"]*w=[^\'"]*)[\'"]([^>]*)*?)>/i'; |
| 78 | 78 | $repl = '<img$2src="$3" srcset="${3}, ${3}&dpr=2 2x, ${3}&dpr=3 3x,"$4>'; |
| 79 | - $content = preg_replace( $pattern, $repl, $content ); |
|
| 79 | + $content = preg_replace($pattern, $repl, $content); |
|
| 80 | 80 | |
| 81 | - return preg_replace( $pattern, $repl, $content ); |
|
| 81 | + return preg_replace($pattern, $repl, $content); |
|
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | /** |
@@ -88,39 +88,39 @@ discard block |
||
| 88 | 88 | * |
| 89 | 89 | * @return string |
| 90 | 90 | */ |
| 91 | - public function replace_image_url( $url ) { |
|
| 92 | - if ( ! empty ( $this->options['cdn_link'] ) ) { |
|
| 93 | - $pathinfo = pathinfo( $url ); |
|
| 91 | + public function replace_image_url($url) { |
|
| 92 | + if ( ! empty ($this->options['cdn_link'])) { |
|
| 93 | + $pathinfo = pathinfo($url); |
|
| 94 | 94 | |
| 95 | - if ( isset( $pathinfo['extension'] ) && in_array( $pathinfo['extension'], [ |
|
| 95 | + if (isset($pathinfo['extension']) && in_array($pathinfo['extension'], [ |
|
| 96 | 96 | 'jpg', |
| 97 | 97 | 'gif', |
| 98 | 98 | 'png', |
| 99 | 99 | 'jpeg' |
| 100 | - ] ) |
|
| 100 | + ]) |
|
| 101 | 101 | ) { |
| 102 | - $parsed_url = parse_url( $url ); |
|
| 103 | - if ( isset( $parsed_url['host'] ) && $parsed_url['host'] === parse_url( home_url( '/' ), PHP_URL_HOST ) ) { |
|
| 104 | - $cdn = parse_url( $this->options['cdn_link'] ); |
|
| 105 | - foreach ( [ 'scheme', 'host', 'port' ] as $url_part ) { |
|
| 106 | - if ( isset( $cdn[ $url_part ] ) ) { |
|
| 107 | - $parsed_url[ $url_part ] = $cdn[ $url_part ]; |
|
| 102 | + $parsed_url = parse_url($url); |
|
| 103 | + if (isset($parsed_url['host']) && $parsed_url['host'] === parse_url(home_url('/'), PHP_URL_HOST)) { |
|
| 104 | + $cdn = parse_url($this->options['cdn_link']); |
|
| 105 | + foreach (['scheme', 'host', 'port'] as $url_part) { |
|
| 106 | + if (isset($cdn[$url_part])) { |
|
| 107 | + $parsed_url[$url_part] = $cdn[$url_part]; |
|
| 108 | 108 | } else { |
| 109 | - unset( $parsed_url[ $url_part ] ); |
|
| 109 | + unset($parsed_url[$url_part]); |
|
| 110 | 110 | } |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | - list( $filename, $arguments ) = $this->convert_filename_to_size_args( $pathinfo['basename'] ); |
|
| 113 | + list($filename, $arguments) = $this->convert_filename_to_size_args($pathinfo['basename']); |
|
| 114 | 114 | |
| 115 | - $arguments = array_merge( $arguments, $this->get_global_params() ); |
|
| 115 | + $arguments = array_merge($arguments, $this->get_global_params()); |
|
| 116 | 116 | |
| 117 | - $parsed_url['path'] = trailingslashit( dirname( $parsed_url['path'] ) ) . $filename; |
|
| 117 | + $parsed_url['path'] = trailingslashit(dirname($parsed_url['path'])) . $filename; |
|
| 118 | 118 | |
| 119 | - if ( ! empty( $arguments ) ) { |
|
| 120 | - $parsed_url['query'] = empty( $parsed_url['query'] ) ? build_query( $arguments ) : $parsed_url['query'] . '&' . build_query( $arguments ); |
|
| 119 | + if ( ! empty($arguments)) { |
|
| 120 | + $parsed_url['query'] = empty($parsed_url['query']) ? build_query($arguments) : $parsed_url['query'] . '&' . build_query($arguments); |
|
| 121 | 121 | } |
| 122 | 122 | |
| 123 | - $url = http_build_url( $parsed_url ); |
|
| 123 | + $url = http_build_url($parsed_url); |
|
| 124 | 124 | } |
| 125 | 125 | } |
| 126 | 126 | } |
@@ -135,9 +135,9 @@ discard block |
||
| 135 | 135 | * |
| 136 | 136 | * @return array $sources |
| 137 | 137 | */ |
| 138 | - public function replace_host_in_srcset( $sources ) { |
|
| 139 | - foreach ( $sources as $source ) { |
|
| 140 | - $sources[ $source['value'] ]['url'] = apply_filters( 'imgix/add-image-url', $sources[ $source['value'] ]['url'] ); |
|
| 138 | + public function replace_host_in_srcset($sources) { |
|
| 139 | + foreach ($sources as $source) { |
|
| 140 | + $sources[$source['value']]['url'] = apply_filters('imgix/add-image-url', $sources[$source['value']]['url']); |
|
| 141 | 141 | } |
| 142 | 142 | |
| 143 | 143 | return $sources; |
@@ -150,11 +150,11 @@ discard block |
||
| 150 | 150 | * |
| 151 | 151 | * @return string |
| 152 | 152 | */ |
| 153 | - public function replace_images_in_content( $content ) { |
|
| 154 | - if ( ! empty ( $this->options['cdn_link'] ) ) { |
|
| 155 | - $content = preg_replace_callback( '/(?<=\shref="|\ssrc="|\shref=\'|\ssrc=\').*(?=\'|")/', function ( $match ) { |
|
| 156 | - return esc_url( apply_filters( 'imgix/add-image-url', $match[0] ) ); |
|
| 157 | - }, $content ); |
|
| 153 | + public function replace_images_in_content($content) { |
|
| 154 | + if ( ! empty ($this->options['cdn_link'])) { |
|
| 155 | + $content = preg_replace_callback('/(?<=\shref="|\ssrc="|\shref=\'|\ssrc=\').*(?=\'|")/', function($match) { |
|
| 156 | + return esc_url(apply_filters('imgix/add-image-url', $match[0])); |
|
| 157 | + }, $content); |
|
| 158 | 158 | |
| 159 | 159 | } |
| 160 | 160 | |
@@ -165,12 +165,12 @@ discard block |
||
| 165 | 165 | * Add tag to dns prefetch cdn host |
| 166 | 166 | */ |
| 167 | 167 | public function prefetch_cdn() { |
| 168 | - if ( ! empty ( $this->options['cdn_link'] ) ) { |
|
| 169 | - $host = parse_url( $this->options['cdn_link'], PHP_URL_HOST ); |
|
| 168 | + if ( ! empty ($this->options['cdn_link'])) { |
|
| 169 | + $host = parse_url($this->options['cdn_link'], PHP_URL_HOST); |
|
| 170 | 170 | |
| 171 | 171 | printf( |
| 172 | 172 | '<link rel="dns-prefetch" href="%s"/>', |
| 173 | - esc_attr( '//' . $host ) |
|
| 173 | + esc_attr('//' . $host) |
|
| 174 | 174 | ); |
| 175 | 175 | } |
| 176 | 176 | } |
@@ -179,9 +179,9 @@ discard block |
||
| 179 | 179 | * Start output buffer if auto retina is enabled |
| 180 | 180 | */ |
| 181 | 181 | public function buffer_start_for_retina() { |
| 182 | - if ( ! empty ( $this->options['add_dpi2_srcset'] ) ) { |
|
| 182 | + if ( ! empty ($this->options['add_dpi2_srcset'])) { |
|
| 183 | 183 | $this->buffer_started = true; |
| 184 | - ob_start( [ $this, 'add_retina' ] ); |
|
| 184 | + ob_start([$this, 'add_retina']); |
|
| 185 | 185 | } |
| 186 | 186 | } |
| 187 | 187 | |
@@ -189,7 +189,7 @@ discard block |
||
| 189 | 189 | * Stop output buffer if it was enabled by the plugin |
| 190 | 190 | */ |
| 191 | 191 | public function buffer_end_for_retina() { |
| 192 | - if ( $this->buffer_started === true ) { |
|
| 192 | + if ($this->buffer_started === true) { |
|
| 193 | 193 | ob_end_flush(); |
| 194 | 194 | } |
| 195 | 195 | } |
@@ -205,16 +205,16 @@ discard block |
||
| 205 | 205 | |
| 206 | 206 | // For now, only "auto" is supported. |
| 207 | 207 | $auto = []; |
| 208 | - if ( ! empty ( $this->options['auto_format'] ) ) { |
|
| 209 | - array_push( $auto, 'format' ); |
|
| 208 | + if ( ! empty ($this->options['auto_format'])) { |
|
| 209 | + array_push($auto, 'format'); |
|
| 210 | 210 | } |
| 211 | 211 | |
| 212 | - if ( ! empty ( $this->options['auto_enhance'] ) ) { |
|
| 213 | - array_push( $auto, 'enhance' ); |
|
| 212 | + if ( ! empty ($this->options['auto_enhance'])) { |
|
| 213 | + array_push($auto, 'enhance'); |
|
| 214 | 214 | } |
| 215 | 215 | |
| 216 | - if ( ! empty( $auto ) ) { |
|
| 217 | - $params['auto'] = implode( ',', $auto ); |
|
| 216 | + if ( ! empty($auto)) { |
|
| 217 | + $params['auto'] = implode(',', $auto); |
|
| 218 | 218 | } |
| 219 | 219 | |
| 220 | 220 | return $params; |
@@ -228,19 +228,19 @@ discard block |
||
| 228 | 228 | * |
| 229 | 229 | * @return array with filename and size arguments. |
| 230 | 230 | */ |
| 231 | - protected function convert_filename_to_size_args( $filename ) { |
|
| 231 | + protected function convert_filename_to_size_args($filename) { |
|
| 232 | 232 | $arguments = []; |
| 233 | 233 | |
| 234 | - $filename = preg_replace_callback( '/-(?<width>\d+)x(?<height>\d+)(?<extension>\.\w{3,4}$)/', function ( $match ) use ( &$arguments ) { |
|
| 234 | + $filename = preg_replace_callback('/-(?<width>\d+)x(?<height>\d+)(?<extension>\.\w{3,4}$)/', function($match) use (&$arguments) { |
|
| 235 | 235 | $arguments = [ |
| 236 | 236 | 'w' => $match['width'], |
| 237 | 237 | 'h' => $match['height'] |
| 238 | 238 | ]; |
| 239 | 239 | |
| 240 | 240 | return $match['extension']; |
| 241 | - }, $filename ); |
|
| 241 | + }, $filename); |
|
| 242 | 242 | |
| 243 | - return [ $filename, $arguments ]; |
|
| 243 | + return [$filename, $arguments]; |
|
| 244 | 244 | } |
| 245 | 245 | } |
| 246 | 246 | |