@@ -5,7 +5,7 @@ discard block |
||
| 5 | 5 | * @package imgix |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | -if ( ! function_exists( 'wp_parse_url' ) ) { |
|
| 8 | +if ( ! function_exists('wp_parse_url')) { |
|
| 9 | 9 | /** |
| 10 | 10 | * A wrapper for PHP's parse_url() function that handles edgecases in < PHP 5.4.7 |
| 11 | 11 | * |
@@ -23,30 +23,30 @@ discard block |
||
| 23 | 23 | * @return bool|array False on failure; Array of URL components on success; |
| 24 | 24 | * See parse_url()'s return values. |
| 25 | 25 | */ |
| 26 | - function wp_parse_url( $url ) { |
|
| 27 | - $parts = @parse_url( $url ); |
|
| 28 | - if ( ! $parts ) { |
|
| 26 | + function wp_parse_url($url) { |
|
| 27 | + $parts = @parse_url($url); |
|
| 28 | + if ( ! $parts) { |
|
| 29 | 29 | // < PHP 5.4.7 compat, trouble with relative paths including a scheme break in the path |
| 30 | - if ( '/' == $url[0] && false !== strpos( $url, '://' ) ) { |
|
| 30 | + if ('/' == $url[0] && false !== strpos($url, '://')) { |
|
| 31 | 31 | // Since we know it's a relative path, prefix with a scheme/host placeholder and try again |
| 32 | - if ( ! $parts = @parse_url( 'placeholder://placeholder' . $url ) ) { |
|
| 32 | + if ( ! $parts = @parse_url('placeholder://placeholder' . $url)) { |
|
| 33 | 33 | return $parts; |
| 34 | 34 | } |
| 35 | 35 | // Remove the placeholder values |
| 36 | - unset( $parts['scheme'], $parts['host'] ); |
|
| 36 | + unset($parts['scheme'], $parts['host']); |
|
| 37 | 37 | } else { |
| 38 | 38 | return $parts; |
| 39 | 39 | } |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | // < PHP 5.4.7 compat, doesn't detect schemeless URL's host field |
| 43 | - if ( '//' == substr( $url, 0, 2 ) && ! isset( $parts['host'] ) ) { |
|
| 44 | - $path_parts = explode( '/', substr( $parts['path'], 2 ), 2 ); |
|
| 43 | + if ('//' == substr($url, 0, 2) && ! isset($parts['host'])) { |
|
| 44 | + $path_parts = explode('/', substr($parts['path'], 2), 2); |
|
| 45 | 45 | $parts['host'] = $path_parts[0]; |
| 46 | - if ( isset( $path_parts[1] ) ) { |
|
| 46 | + if (isset($path_parts[1])) { |
|
| 47 | 47 | $parts['path'] = '/' . $path_parts[1]; |
| 48 | 48 | } else { |
| 49 | - unset( $parts['path'] ); |
|
| 49 | + unset($parts['path']); |
|
| 50 | 50 | } |
| 51 | 51 | } |
| 52 | 52 | |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | } |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | -if ( ! function_exists( 'http_build_url' ) ) { |
|
| 57 | +if ( ! function_exists('http_build_url')) { |
|
| 58 | 58 | /** |
| 59 | 59 | * This is a simplified version of http_build_url if pecl_http is missing |
| 60 | 60 | * |
@@ -62,16 +62,16 @@ discard block |
||
| 62 | 62 | * |
| 63 | 63 | * @return string |
| 64 | 64 | */ |
| 65 | - function http_build_url( $parsed_url ) { |
|
| 66 | - $scheme = isset( $parsed_url['scheme'] ) ? $parsed_url['scheme'] . '://' : ''; |
|
| 67 | - $host = isset( $parsed_url['host'] ) ? $parsed_url['host'] : ''; |
|
| 68 | - $port = isset( $parsed_url['port'] ) ? ':' . $parsed_url['port'] : ''; |
|
| 69 | - $user = isset( $parsed_url['user'] ) ? $parsed_url['user'] : ''; |
|
| 70 | - $pass = isset( $parsed_url['pass'] ) ? ':' . $parsed_url['pass'] : ''; |
|
| 71 | - $pass = ( $user || $pass ) ? "$pass@" : ''; |
|
| 72 | - $path = isset( $parsed_url['path'] ) ? $parsed_url['path'] : ''; |
|
| 73 | - $query = isset( $parsed_url['query'] ) ? '?' . $parsed_url['query'] : ''; |
|
| 74 | - $fragment = isset( $parsed_url['fragment'] ) ? '#' . $parsed_url['fragment'] : ''; |
|
| 65 | + function http_build_url($parsed_url) { |
|
| 66 | + $scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : ''; |
|
| 67 | + $host = isset($parsed_url['host']) ? $parsed_url['host'] : ''; |
|
| 68 | + $port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : ''; |
|
| 69 | + $user = isset($parsed_url['user']) ? $parsed_url['user'] : ''; |
|
| 70 | + $pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : ''; |
|
| 71 | + $pass = ($user || $pass) ? "$pass@" : ''; |
|
| 72 | + $path = isset($parsed_url['path']) ? $parsed_url['path'] : ''; |
|
| 73 | + $query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : ''; |
|
| 74 | + $fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : ''; |
|
| 75 | 75 | |
| 76 | 76 | return "$scheme$user$pass$host$port$path$query$fragment"; |
| 77 | 77 | } |
@@ -15,26 +15,26 @@ |
||
| 15 | 15 | * Author URI: http://github.com/wladston |
| 16 | 16 | */ |
| 17 | 17 | |
| 18 | -define('IMGIX_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); |
|
| 18 | +define('IMGIX_PLUGIN_URL', plugin_dir_url(__FILE__)); |
|
| 19 | 19 | |
| 20 | -$imgix_options = get_option( 'imgix_settings' ); |
|
| 20 | +$imgix_options = get_option('imgix_settings'); |
|
| 21 | 21 | |
| 22 | -include( 'includes/compability.php' ); |
|
| 23 | -include( 'includes/do-functions.php' ); |
|
| 24 | -include( 'includes/options-page.php' ); |
|
| 22 | +include('includes/compability.php'); |
|
| 23 | +include('includes/do-functions.php'); |
|
| 24 | +include('includes/options-page.php'); |
|
| 25 | 25 | |
| 26 | -function imgix_plugin_admin_action_links( $links, $file ) { |
|
| 26 | +function imgix_plugin_admin_action_links($links, $file) { |
|
| 27 | 27 | static $my_plugin; |
| 28 | 28 | |
| 29 | - if ( ! $my_plugin ) { |
|
| 30 | - $my_plugin = plugin_basename( __FILE__ ); |
|
| 29 | + if ( ! $my_plugin) { |
|
| 30 | + $my_plugin = plugin_basename(__FILE__); |
|
| 31 | 31 | } |
| 32 | 32 | |
| 33 | - if ( $file === $my_plugin ) { |
|
| 33 | + if ($file === $my_plugin) { |
|
| 34 | 34 | $settings_link = '<a href="options-general.php?page=imgix-options">Settings</a>'; |
| 35 | - array_unshift( $links, $settings_link ); |
|
| 35 | + array_unshift($links, $settings_link); |
|
| 36 | 36 | } |
| 37 | 37 | return $links; |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | -add_filter( 'plugin_action_links', 'imgix_plugin_admin_action_links', 10, 2 ); |
|
| 40 | +add_filter('plugin_action_links', 'imgix_plugin_admin_action_links', 10, 2); |
|
@@ -13,13 +13,13 @@ discard block |
||
| 13 | 13 | * |
| 14 | 14 | * @return string Content with retina-enriched image tags. |
| 15 | 15 | */ |
| 16 | -function imgix_add_retina( $content ) { |
|
| 16 | +function imgix_add_retina($content) { |
|
| 17 | 17 | $pattern = '/<img((?![^>]+srcset )([^>]*)'; |
| 18 | 18 | $pattern .= 'src=[\'"]([^\'"]*imgix.net[^\'"]*\?[^\'"]*w=[^\'"]*)[\'"]([^>]*)*?)>/i'; |
| 19 | 19 | $repl = '<img$2src="$3" srcset="${3}, ${3}&dpr=2 2x, ${3}&dpr=3 3x,"$4>'; |
| 20 | - $content = preg_replace( $pattern, $repl, $content ); |
|
| 20 | + $content = preg_replace($pattern, $repl, $content); |
|
| 21 | 21 | |
| 22 | - return preg_replace( $pattern, $repl, $content ); |
|
| 22 | + return preg_replace($pattern, $repl, $content); |
|
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | /** |
@@ -35,16 +35,16 @@ discard block |
||
| 35 | 35 | // For now, only "auto" is supported. |
| 36 | 36 | |
| 37 | 37 | $auto = []; |
| 38 | - if ( ! empty( $imgix_options['auto_format'] ) ) { |
|
| 39 | - array_push( $auto, 'format' ); |
|
| 38 | + if ( ! empty($imgix_options['auto_format'])) { |
|
| 39 | + array_push($auto, 'format'); |
|
| 40 | 40 | } |
| 41 | 41 | |
| 42 | - if ( ! empty( $imgix_options['auto_enhance'] ) ) { |
|
| 43 | - array_push( $auto, 'enhance' ); |
|
| 42 | + if ( ! empty($imgix_options['auto_enhance'])) { |
|
| 43 | + array_push($auto, 'enhance'); |
|
| 44 | 44 | } |
| 45 | 45 | |
| 46 | - if ( ! empty( $auto ) ) { |
|
| 47 | - $params['auto'] = implode( ',', $auto ); |
|
| 46 | + if ( ! empty($auto)) { |
|
| 47 | + $params['auto'] = implode(',', $auto); |
|
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | return $params; |
@@ -58,19 +58,19 @@ discard block |
||
| 58 | 58 | * |
| 59 | 59 | * @return array with filename and size arguments. |
| 60 | 60 | */ |
| 61 | -function convert_filename_to_size_args( $filename ) { |
|
| 61 | +function convert_filename_to_size_args($filename) { |
|
| 62 | 62 | $arguments = []; |
| 63 | 63 | |
| 64 | - $filename = preg_replace_callback( '/-(?<width>\d+)x(?<height>\d+)(?<extension>\.\w{3,4}$)/', function ( $match ) use ( &$arguments ) { |
|
| 64 | + $filename = preg_replace_callback('/-(?<width>\d+)x(?<height>\d+)(?<extension>\.\w{3,4}$)/', function($match) use (&$arguments) { |
|
| 65 | 65 | $arguments = [ |
| 66 | 66 | 'w' => $match['width'], |
| 67 | 67 | 'h' => $match['height'] |
| 68 | 68 | ]; |
| 69 | 69 | |
| 70 | 70 | return $match['extension']; |
| 71 | - }, $filename ); |
|
| 71 | + }, $filename); |
|
| 72 | 72 | |
| 73 | - return [ $filename, $arguments ]; |
|
| 73 | + return [$filename, $arguments]; |
|
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | /** |
@@ -80,25 +80,25 @@ discard block |
||
| 80 | 80 | * |
| 81 | 81 | * @return string |
| 82 | 82 | */ |
| 83 | -function imgix_file_url( $url ) { |
|
| 83 | +function imgix_file_url($url) { |
|
| 84 | 84 | |
| 85 | 85 | global $imgix_options; |
| 86 | 86 | |
| 87 | - if ( empty ( $imgix_options['cdn_link'] ) ) { |
|
| 87 | + if (empty ($imgix_options['cdn_link'])) { |
|
| 88 | 88 | return $url; |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | - $file = pathinfo( $url ); |
|
| 91 | + $file = pathinfo($url); |
|
| 92 | 92 | |
| 93 | - if ( in_array( $file['extension'], [ 'jpg', 'gif', 'png', 'jpeg' ] ) ) { |
|
| 94 | - return str_replace( trailingslashit( home_url( '/' ) ), trailingslashit( $imgix_options['cdn_link'] ), $url ); |
|
| 93 | + if (in_array($file['extension'], ['jpg', 'gif', 'png', 'jpeg'])) { |
|
| 94 | + return str_replace(trailingslashit(home_url('/')), trailingslashit($imgix_options['cdn_link']), $url); |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | return $url; |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | -add_filter( 'wp_get_attachment_url', 'imgix_file_url' ); |
|
| 101 | -add_filter( 'imgix/add-image-url', 'imgix_file_url' ); |
|
| 100 | +add_filter('wp_get_attachment_url', 'imgix_file_url'); |
|
| 101 | +add_filter('imgix/add-image-url', 'imgix_file_url'); |
|
| 102 | 102 | |
| 103 | 103 | /** |
| 104 | 104 | * Modify image urls in srcset to use imgix host. |
@@ -107,15 +107,15 @@ discard block |
||
| 107 | 107 | * |
| 108 | 108 | * @return array $sources |
| 109 | 109 | */ |
| 110 | -function imgix_cdn_srcset( $sources ) { |
|
| 111 | - foreach ( $sources as $source ) { |
|
| 112 | - $sources[ $source['value'] ]['url'] = apply_filters( 'imgix/add-image-url', $sources[ $source['value'] ]['url'] ); |
|
| 110 | +function imgix_cdn_srcset($sources) { |
|
| 111 | + foreach ($sources as $source) { |
|
| 112 | + $sources[$source['value']]['url'] = apply_filters('imgix/add-image-url', $sources[$source['value']]['url']); |
|
| 113 | 113 | } |
| 114 | 114 | |
| 115 | 115 | return $sources; |
| 116 | 116 | } |
| 117 | 117 | |
| 118 | -add_filter( 'wp_calculate_image_srcset', 'imgix_cdn_srcset', 10 ); |
|
| 118 | +add_filter('wp_calculate_image_srcset', 'imgix_cdn_srcset', 10); |
|
| 119 | 119 | |
| 120 | 120 | /** |
| 121 | 121 | * Modify image urls in content to use imgix host. |
@@ -124,75 +124,75 @@ discard block |
||
| 124 | 124 | * |
| 125 | 125 | * @return string |
| 126 | 126 | */ |
| 127 | -function imgix_replace_non_wp_images( $content ) { |
|
| 127 | +function imgix_replace_non_wp_images($content) { |
|
| 128 | 128 | global $imgix_options; |
| 129 | 129 | |
| 130 | - if ( ! empty( $imgix_options['cdn_link'] ) ) { |
|
| 130 | + if ( ! empty($imgix_options['cdn_link'])) { |
|
| 131 | 131 | |
| 132 | - $content = preg_replace_callback( '/(?<=\shref="|\ssrc="|\shref=\'|\ssrc=\').*(?=\'|")/', function ( $match ) use ( $imgix_options ) { |
|
| 132 | + $content = preg_replace_callback('/(?<=\shref="|\ssrc="|\shref=\'|\ssrc=\').*(?=\'|")/', function($match) use ($imgix_options) { |
|
| 133 | 133 | $url = $match[0]; |
| 134 | 134 | |
| 135 | - $pathinfo = pathinfo( $url ); |
|
| 135 | + $pathinfo = pathinfo($url); |
|
| 136 | 136 | |
| 137 | - if ( in_array( $pathinfo['extension'], [ 'jpg', 'gif', 'png', 'jpeg' ], true ) ) { |
|
| 138 | - $file_url = parse_url( $url ); |
|
| 139 | - if ( $file_url['host'] === parse_url( home_url( '/' ), PHP_URL_HOST ) ) { |
|
| 140 | - $cdn = parse_url( $imgix_options['cdn_link'] ); |
|
| 141 | - foreach ( [ 'scheme', 'host', 'port' ] as $url_part ) { |
|
| 142 | - if ( isset( $cdn[ $url_part ] ) ) { |
|
| 143 | - $file_url[ $url_part ] = $cdn[ $url_part ]; |
|
| 137 | + if (in_array($pathinfo['extension'], ['jpg', 'gif', 'png', 'jpeg'], true)) { |
|
| 138 | + $file_url = parse_url($url); |
|
| 139 | + if ($file_url['host'] === parse_url(home_url('/'), PHP_URL_HOST)) { |
|
| 140 | + $cdn = parse_url($imgix_options['cdn_link']); |
|
| 141 | + foreach (['scheme', 'host', 'port'] as $url_part) { |
|
| 142 | + if (isset($cdn[$url_part])) { |
|
| 143 | + $file_url[$url_part] = $cdn[$url_part]; |
|
| 144 | 144 | } else { |
| 145 | - unset( $file_url[ $url_part ] ); |
|
| 145 | + unset($file_url[$url_part]); |
|
| 146 | 146 | } |
| 147 | 147 | } |
| 148 | 148 | |
| 149 | - list( $filename, $arguments ) = convert_filename_to_size_args( $pathinfo['basename'] ); |
|
| 149 | + list($filename, $arguments) = convert_filename_to_size_args($pathinfo['basename']); |
|
| 150 | 150 | |
| 151 | - $arguments = array_merge( $arguments, imgix_get_global_params() ); |
|
| 151 | + $arguments = array_merge($arguments, imgix_get_global_params()); |
|
| 152 | 152 | |
| 153 | - $file_url['path'] = trailingslashit( dirname( $file_url['path'] ) ) . $filename; |
|
| 153 | + $file_url['path'] = trailingslashit(dirname($file_url['path'])) . $filename; |
|
| 154 | 154 | |
| 155 | - if ( ! empty( $arguments ) ) { |
|
| 156 | - $file_url['query'] = empty( $file_url['query'] ) ? build_query( $arguments ) : $file_url['query'] . '&' . build_query( $arguments ); |
|
| 155 | + if ( ! empty($arguments)) { |
|
| 156 | + $file_url['query'] = empty($file_url['query']) ? build_query($arguments) : $file_url['query'] . '&' . build_query($arguments); |
|
| 157 | 157 | } |
| 158 | 158 | |
| 159 | - $url = http_build_url( $file_url ); |
|
| 159 | + $url = http_build_url($file_url); |
|
| 160 | 160 | } |
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | |
| 164 | - return esc_url_raw( $url ); |
|
| 165 | - }, $content ); |
|
| 164 | + return esc_url_raw($url); |
|
| 165 | + }, $content); |
|
| 166 | 166 | |
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | return $content; |
| 170 | 170 | } |
| 171 | 171 | |
| 172 | -add_filter( 'the_content', 'imgix_replace_non_wp_images' ); |
|
| 172 | +add_filter('the_content', 'imgix_replace_non_wp_images'); |
|
| 173 | 173 | |
| 174 | 174 | function imgix_wp_head() { |
| 175 | 175 | global $imgix_options; |
| 176 | 176 | |
| 177 | - if ( isset( $imgix_options['cdn_link'] ) && $imgix_options['cdn_link'] ) { |
|
| 178 | - printf( "<link rel='dns-prefetch' href='%s'/>", |
|
| 179 | - esc_url( preg_replace( '/^https?:/', '', untrailingslashit( $imgix_options['cdn_link'] ) ) ) |
|
| 177 | + if (isset($imgix_options['cdn_link']) && $imgix_options['cdn_link']) { |
|
| 178 | + printf("<link rel='dns-prefetch' href='%s'/>", |
|
| 179 | + esc_url(preg_replace('/^https?:/', '', untrailingslashit($imgix_options['cdn_link']))) |
|
| 180 | 180 | ); |
| 181 | 181 | } |
| 182 | 182 | } |
| 183 | 183 | |
| 184 | -add_action( 'wp_head', 'imgix_wp_head', 1 ); |
|
| 184 | +add_action('wp_head', 'imgix_wp_head', 1); |
|
| 185 | 185 | |
| 186 | -if ( isset( $imgix_options['add_dpi2_srcset'] ) && $imgix_options['add_dpi2_srcset'] ) { |
|
| 186 | +if (isset($imgix_options['add_dpi2_srcset']) && $imgix_options['add_dpi2_srcset']) { |
|
| 187 | 187 | function imgix_buffer_start() { |
| 188 | - ob_start( 'imgix_add_retina' ); |
|
| 188 | + ob_start('imgix_add_retina'); |
|
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | function imgix_buffer_end() { |
| 192 | 192 | ob_end_flush(); |
| 193 | 193 | } |
| 194 | 194 | |
| 195 | - add_action( 'after_setup_theme', 'imgix_buffer_start' ); |
|
| 196 | - add_action( 'shutdown', 'imgix_buffer_end' ); |
|
| 197 | - add_filter( 'the_content', 'imgix_add_retina' ); |
|
| 195 | + add_action('after_setup_theme', 'imgix_buffer_start'); |
|
| 196 | + add_action('shutdown', 'imgix_buffer_end'); |
|
| 197 | + add_filter('the_content', 'imgix_add_retina'); |
|
| 198 | 198 | } |