Skip to content

Commit

Permalink
add imgix default query option
Browse files Browse the repository at this point in the history
  • Loading branch information
kevindees committed Dec 16, 2021
1 parent 978d271 commit cea283b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
31 changes: 21 additions & 10 deletions src/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,36 @@ class Core
protected static array $options = [
'admin' => false,
'imgix_host' => null,
'imgix_query' => 'auto=format',
'webp' => true,
'ext_replace' => true,
];

/**
* @param array $options
*
* - admin (bool) - Default is `false`, set to `true` to allow in admin
* admin (bool) - Default is `false`, set to `true` to allow in admin
*
* - imgix_host (null|string) - Default is `null`, this is to be set to your imgix
* source URL
* imgix_host (null|string) - Default is `null`, this is to be set to your imgix
* source URL
*
* - webp (bool) - Default is `true`, this will rewrite image URLs as
* .webp even when imgix is not used
* imgix_query (string) - Default is `auto=format`, adds query to all imgix
* image URLs.
*
* - ext_replace (bool) - Default is `true`, this will replace image extensions
* with .webp but when `false` will append .webp
* webp (bool) - Default is `true`, this will rewrite image URLs as
* .webp even when imgix is not used
*
* ext_replace (bool) - Default is `true`, this will replace image extensions
* with .webp but when `false` will append .webp
*
* @return void
*/
public static function init(array $options = [])
public static function init(array $options = []) : void
{
static::$options = array_merge(static::$options, $options, array_filter([
'admin' => defined('IMGIX_WP_ADMIN') ? constant('IMGIX_WP_ADMIN') : null,
'imgix_host' => defined('IMGIX_WP_IMGIX_HOST') ? constant('IMGIX_WP_IMGIX_HOST') : null,
'imgix_query' => defined('IMGIX_WP_IMGIX_QUERY') ? constant('IMGIX_WP_IMGIX_QUERY') : null,
'webp' => defined('IMGIX_WP_WEBP') ? constant('IMGIX_WP_WEBP') : null,
'ext_replace' => defined('IMGIX_WP_EXT_REPLACE') ? constant('IMGIX_WP_EXT_REPLACE') : null,
]));
Expand All @@ -44,9 +50,14 @@ public static function init(array $options = [])
add_filter( 'wp_calculate_image_srcset', static::class . '::filter_wp_calculate_image_srcset');
}

public static function imgixHost() : string
public static function imgixHost() : ?string
{
return static::$options['imgix_host'];
}

public static function defaultImgixQuery() : string
{
return trim((string) static::$options['imgix_host'], "/\ \t\n\r\0\x0B");
return trim(static::$options['imgix_query']);
}

public static function maybeReplaceExtensionWithWebp() : bool
Expand Down
4 changes: 2 additions & 2 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public static function transformWordPressUploadsUrlToImgixHost($url, string $fol
{
$host = Core::imgixHost();
$url = str_replace(WP_CONTENT_URL . '/' . $folder, 'https://' . $host, $url);

return $url . '?auto=format';
$query = Core::defaultImgixQuery();
return $url . ($query ? "?{$query}" : '');
}

/**
Expand Down

0 comments on commit cea283b

Please sign in to comment.