diff --git a/CHANGELOG.md b/CHANGELOG.md index 3bee1a4e..9d0c01e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,16 @@ # ImageOptimize Changelog +## 1.4.10 - 2018.01.26 +### Added +* Shows an error if an OptimizedImages field is added to anything other than an Asset's layout +* Added a warning if a config setting is being overridden by the `config.php` +* Added a number of config settings to the AdminCP GUI + +### Changed +* `UrlHelper::urlWithProtocol` -> `UrlHelper::urlWithScheme` for Craft CMS 3 RC7 compatibility +* Fixed an issue where you could delete the last Image Variant in the field settings, even though the option was disabled +* Handle the display of Optimized Image Variant names better when coming from Imgix + ## 1.4.9 - 2018.01.20 ### Added * Added an *Edit Image* button to the Optimized Images field view diff --git a/composer.json b/composer.json index f5a525d0..2cf8d632 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "nystudio107/craft3-imageoptimize", "description": "Automatically create & optimize responsive image transforms, using either native Craft transforms or a service like Imgix, with zero template changes.", "type": "craft-plugin", - "version": "1.4.9", + "version": "1.4.10", "keywords": [ "craft", "cms", @@ -28,7 +28,7 @@ "minimum-stability": "dev", "prefer-stable": true, "require": { - "craftcms/cms": "^3.0.0-RC3", + "craftcms/cms": "^3.0.0-RC7", "cloudinary/cloudinary_php": "^1.8.0", "imageoptim/imageoptim": "^1.0.0", "imgix/imgix-php": "^1.1.0", diff --git a/src/assetbundles/optimizedimagesfield/dist/js/OptimizedImages.js b/src/assetbundles/optimizedimagesfield/dist/js/OptimizedImages.js index 688ad6d2..4136c142 100644 --- a/src/assetbundles/optimizedimagesfield/dist/js/OptimizedImages.js +++ b/src/assetbundles/optimizedimagesfield/dist/js/OptimizedImages.js @@ -128,7 +128,9 @@ Craft.OptimizedImagesInput = Garnish.Base.extend( break; } case 'delete': { - this.deleteVariantBlock(container); + if (!$option.hasClass('disabled')) { + this.deleteVariantBlock(container); + } break; } } @@ -184,9 +186,9 @@ Craft.OptimizedImagesInput = Garnish.Base.extend( menuBtn = $value.data('menubtn'); $menuItem = $(menuBtn.menu.$menuList[1]); if (disabledDeleteItem) { - $menuItem.addClass('disabled'); + $menuItem.find("> li > a").addClass('disabled').disable(); } else { - $menuItem.removeClass('disabled'); + $menuItem.find("> li > a").removeClass('disabled').enable(); } } }); diff --git a/src/config.php b/src/config.php index ff965abd..073e55d5 100644 --- a/src/config.php +++ b/src/config.php @@ -49,7 +49,7 @@ // as per https://www.netvlies.nl/tips-updates/design-interactie/design-interactie/retina-revolution/ 'lowerQualityRetinaImageVariants' => true, - // Controls whether Optimized Image Variants be created that would be up-scaled + // Controls whether Optimized Image Variants are created that would be up-scaled // to be larger than the original source image 'allowUpScaledImageVariants' => false, diff --git a/src/fields/OptimizedImages.php b/src/fields/OptimizedImages.php index d0fe9e81..8778df73 100644 --- a/src/fields/OptimizedImages.php +++ b/src/fields/OptimizedImages.php @@ -225,39 +225,50 @@ public function getSettingsHtml() */ public function getInputHtml($value, ElementInterface $element = null): string { - // Register our asset bundle - Craft::$app->getView()->registerAssetBundle(OptimizedImagesFieldAsset::class); - - // Get our id and namespace - $id = Craft::$app->getView()->formatInputId($this->handle); - $nameSpaceId = Craft::$app->getView()->namespaceInputId($id); - - // Variables to pass down to our field JavaScript to let it namespace properly - $jsonVars = [ - 'id' => $id, - 'name' => $this->handle, - 'namespace' => $nameSpaceId, - 'prefix' => Craft::$app->getView()->namespaceInputId(''), - ]; - $jsonVars = Json::encode($jsonVars); - $view = Craft::$app->getView(); - $view->registerJs("$('#{$nameSpaceId}-field').ImageOptimizeOptimizedImages(".$jsonVars.");"); - - $settings = ImageOptimize::$plugin->getSettings(); - - // Render the input template - return Craft::$app->getView()->renderTemplate( - 'image-optimize/_components/fields/OptimizedImages_input', - [ - 'name' => $this->handle, - 'value' => $value, - 'variants' => $this->variants, - 'field' => $this, - 'settings' => $settings, - 'elementId' => $element->id, - 'id' => $id, - 'nameSpaceId' => $nameSpaceId, - ] - ); + if (!empty($element) && $element instanceof Asset) { + /** @var Asset $element */ + // Register our asset bundle + Craft::$app->getView()->registerAssetBundle(OptimizedImagesFieldAsset::class); + + // Get our id and namespace + $id = Craft::$app->getView()->formatInputId($this->handle); + $nameSpaceId = Craft::$app->getView()->namespaceInputId($id); + + // Variables to pass down to our field JavaScript to let it namespace properly + $jsonVars = [ + 'id' => $id, + 'name' => $this->handle, + 'namespace' => $nameSpaceId, + 'prefix' => Craft::$app->getView()->namespaceInputId(''), + ]; + $jsonVars = Json::encode($jsonVars); + $view = Craft::$app->getView(); + $view->registerJs("$('#{$nameSpaceId}-field').ImageOptimizeOptimizedImages(".$jsonVars.");"); + + $settings = ImageOptimize::$plugin->getSettings(); + + // Render the input template + return Craft::$app->getView()->renderTemplate( + 'image-optimize/_components/fields/OptimizedImages_input', + [ + 'name' => $this->handle, + 'value' => $value, + 'variants' => $this->variants, + 'field' => $this, + 'settings' => $settings, + 'elementId' => $element->id, + 'format' => $element->getExtension(), + 'id' => $id, + 'nameSpaceId' => $nameSpaceId, + ] + ); + } else { + // Render an error template, since the field only works when attached to an Asset + return Craft::$app->getView()->renderTemplate( + 'image-optimize/_components/fields/OptimizedImages_error', + [ + ] + ); + } } } diff --git a/src/imagetransforms/ImageTransform.php b/src/imagetransforms/ImageTransform.php index 05bd674e..4a09000e 100644 --- a/src/imagetransforms/ImageTransform.php +++ b/src/imagetransforms/ImageTransform.php @@ -15,6 +15,8 @@ use craft\helpers\UrlHelper; use craft\models\AssetTransform; +use yii\base\Exception; + /** * @author nystudio107 * @package ImageOptimize @@ -116,9 +118,12 @@ public static function prefetchRemoteFile($url) } else { $protocol = "http"; } - $url = UrlHelper::urlWithProtocol($url, $protocol); + $url = UrlHelper::urlWithScheme($url, $protocol); } else { - $url = UrlHelper::siteUrl($url); + try { + $url = UrlHelper::siteUrl($url); + } catch (Exception $e) { + } } } diff --git a/src/models/OptimizedImage.php b/src/models/OptimizedImage.php index 9b3f6ee0..38cce367 100644 --- a/src/models/OptimizedImage.php +++ b/src/models/OptimizedImage.php @@ -16,6 +16,7 @@ use craft\helpers\UrlHelper; use craft\base\Model; use craft\validators\ArrayValidator; + use yii\base\Exception; /** @@ -340,18 +341,18 @@ public function getRemoteFileSize($url, $formatSize = true, $useHead = true) { // Make this a full URL / aaw -- 2017.09.08 if (!UrlHelper::isAbsoluteUrl($url)) { - if (isset($_SERVER['HTTPS']) && (strcasecmp($_SERVER['HTTPS'], 'on') === 0 || $_SERVER['HTTPS'] == 1) - || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strcasecmp($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') === 0 - ) { - $protocol = "https"; - } else { - $protocol = "http"; - } if (UrlHelper::isProtocolRelativeUrl($url)) { - $url = UrlHelper::urlWithProtocol($url, $protocol); + if (isset($_SERVER['HTTPS']) && (strcasecmp($_SERVER['HTTPS'], 'on') === 0 || $_SERVER['HTTPS'] == 1) + || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strcasecmp($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') === 0 + ) { + $protocol = "https"; + } else { + $protocol = "http"; + } + $url = UrlHelper::urlWithScheme($url, $protocol); } else { try { - $url = UrlHelper::siteUrl($url, null, $protocol); + $url = UrlHelper::siteUrl($url); } catch (Exception $e) { } } diff --git a/src/models/Settings.php b/src/models/Settings.php index d52426d8..f3c711a1 100644 --- a/src/models/Settings.php +++ b/src/models/Settings.php @@ -80,7 +80,7 @@ class Settings extends Model public $lowerQualityRetinaImageVariants = true; /** - * Controls whether Optimized Image Variants be created that would be up-scaled + * Controls whether Optimized Image Variants are created that would be up-scaled * to be larger than the original source image * * @var bool diff --git a/src/templates/_components/fields/OptimizedImages_error.twig b/src/templates/_components/fields/OptimizedImages_error.twig new file mode 100644 index 00000000..aa5514b0 --- /dev/null +++ b/src/templates/_components/fields/OptimizedImages_error.twig @@ -0,0 +1,21 @@ +{# +/** + * Image Optimize plugin for Craft CMS 3.x + * + * OptimizedImages Field Input + * + * @author nystudio107 + * @copyright Copyright (c) 2017 nystudio107 + * @link https://nystudio107.com + * @package ImageOptimize + * @since 1.4.10 + */ +#} + +{% import "_includes/forms" as forms %} + +