Skip to content

Commit b6e1556

Browse files
committed
Merge branch 'release/1.4.31' into v1
2 parents 4ac642b + 83abb8d commit b6e1556

File tree

6 files changed

+368
-106
lines changed

6 files changed

+368
-106
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# ImageOptimize Changelog
22

3+
## 1.4.31 - 2018.04.22
4+
### Added
5+
* Added CraftQL support
6+
37
## 1.4.30 - 2018.04.09
48
### Added
59
* Added additional profiling information

README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,71 @@ Re-saving many images at a time can be intensive, and on certain setups may requ
533533

534534
All you need to do is install the plugin, and any queue jobs in Craft CMS 3 will now run entirely in the background via the CLI php, which isn't subject to the same restrictions that the web php is.
535535

536+
### GraphQL via CraftQL Plugin
537+
538+
ImageOptimize has built-in support for accessing the OptimizedImages field via GraphQL using the [CraftQL plugin](https://github.com/markhuot/craftql).
539+
540+
You can access all of the primary OptimizeImages methods:
541+
542+
```
543+
{
544+
entries(section:[homepage], limit:1) {
545+
...on Homepage {
546+
title
547+
url
548+
someAsset {
549+
...on AssetsVolume {
550+
title
551+
optimizedImages {
552+
...on OptimizedImagesData {
553+
src,
554+
srcset,
555+
srcWebp,
556+
srcsetWebp,
557+
maxSrcsetWidth,
558+
placeholderImage,
559+
placeholderBox,
560+
placeholderSilhouette
561+
}
562+
}
563+
}
564+
}
565+
}
566+
}
567+
}
568+
```
569+
570+
...as well as all of the object properties:
571+
572+
```
573+
entries(section:[homepage], limit:1) {
574+
...on Homepage {
575+
title
576+
url
577+
someAsset {
578+
...on AssetsVolume {
579+
title
580+
optimizedImages {
581+
...on OptimizedImagesData {
582+
optimizedImageUrls,
583+
optimizedWebPImageUrls,
584+
variantSourceWidths,
585+
originalImageWidth,
586+
originalImageHeight
587+
placeholder,
588+
placeholderSvg,
589+
colorPalette,
590+
placeholderWidth,
591+
placeholderHeight
592+
}
593+
}
594+
}
595+
}
596+
}
597+
}
598+
}
599+
```
600+
536601
## Using Optimized Image Transforms
537602

538603
Once ImageOptimize is set up and configured, there's nothing left to do for optimizing your image transforms. It just works.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "nystudio107/craft-imageoptimize",
33
"description": "Automatically create & optimize responsive image transforms, using either native Craft transforms or a service like Imgix, with zero template changes.",
44
"type": "craft-plugin",
5-
"version": "1.4.30",
5+
"version": "1.4.31",
66
"keywords": [
77
"craft",
88
"cms",

src/ImageOptimize.php

Lines changed: 125 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
use nystudio107\imageoptimize\fields\OptimizedImages;
1414
use nystudio107\imageoptimize\imagetransforms\ImageTransformInterface;
15+
use nystudio107\imageoptimize\listeners\GetCraftQLSchema;
1516
use nystudio107\imageoptimize\models\Settings;
1617
use nystudio107\imageoptimize\services\Optimize as OptimizeService;
1718
use nystudio107\imageoptimize\services\OptimizedImages as OptimizedImagesService;
@@ -22,7 +23,6 @@
2223
use craft\base\Field;
2324
use craft\base\Plugin;
2425
use craft\base\Volume;
25-
use craft\console\Application as ConsoleApplication;
2626
use craft\elements\Asset;
2727
use craft\events\AssetTransformImageEvent;
2828
use craft\events\ElementEvent;
@@ -44,7 +44,10 @@
4444
use craft\web\twig\variables\CraftVariable;
4545
use craft\web\Controller;
4646

47+
use markhuot\CraftQL\CraftQL;
48+
4749
use yii\base\Event;
50+
use yii\base\Exception;
4851

4952
/** @noinspection MissingPropertyAnnotationsInspection */
5053

@@ -63,6 +66,12 @@
6366
*/
6467
class ImageOptimize extends Plugin
6568
{
69+
70+
// Constants
71+
// =========================================================================
72+
73+
const CRAFTQL_PLUGIN_HANDLE = 'craftql';
74+
6675
// Static Properties
6776
// =========================================================================
6877

@@ -150,15 +159,23 @@ public function settingsHtml()
150159
$settings = $this->getSettings();
151160

152161
// Render the settings template
153-
return Craft::$app->getView()->renderTemplate(
154-
'image-optimize/settings',
155-
[
156-
'settings' => $settings,
157-
'imageProcessors' => $imageProcessors,
158-
'variantCreators' => $variantCreators,
159-
'gdInstalled' => function_exists('imagecreatefromjpeg'),
160-
]
161-
);
162+
try {
163+
return Craft::$app->getView()->renderTemplate(
164+
'image-optimize/settings',
165+
[
166+
'settings' => $settings,
167+
'imageProcessors' => $imageProcessors,
168+
'variantCreators' => $variantCreators,
169+
'gdInstalled' => \function_exists('imagecreatefromjpeg'),
170+
]
171+
);
172+
} catch (\Twig_Error_Loader $e) {
173+
Craft::error($e->getMessage(), __METHOD__);
174+
} catch (Exception $e) {
175+
Craft::error($e->getMessage(), __METHOD__);
176+
}
177+
178+
return '';
162179
}
163180

164181
// Protected Methods
@@ -210,83 +227,7 @@ protected function installEventHandlers()
210227
$this->installAssetEventHandlers();
211228
$this->installElementEventHandlers();
212229
$this->installMiscEventHandlers();
213-
}
214-
215-
/**
216-
* Install our miscellaneous event handlers
217-
*/
218-
protected function installMiscEventHandlers()
219-
{
220-
// Handler: Fields::EVENT_AFTER_SAVE_FIELD
221-
Event::on(
222-
Fields::class,
223-
Fields::EVENT_AFTER_SAVE_FIELD,
224-
function (FieldEvent $event) {
225-
Craft::debug(
226-
'Fields::EVENT_AFTER_SAVE_FIELD',
227-
__METHOD__
228-
);
229-
$settings = $this->getSettings();
230-
/** @var Field $field */
231-
if (!$event->isNew && $settings->automaticallyResaveImageVariants) {
232-
$this->checkForOptimizedImagesField($event);
233-
}
234-
}
235-
);
236-
237-
// Handler: Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS
238-
Event::on(
239-
Plugins::class,
240-
Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS,
241-
function (PluginEvent $event) {
242-
if ($event->plugin === $this) {
243-
Craft::debug(
244-
'Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS',
245-
__METHOD__
246-
);
247-
$settings = $this->getSettings();
248-
if ($settings->automaticallyResaveImageVariants) {
249-
// After they have changed the settings, resave all of the assets
250-
ImageOptimize::$plugin->optimizedImages->resaveAllVolumesAssets();
251-
}
252-
}
253-
}
254-
);
255-
256-
// Handler: Volumes::EVENT_AFTER_SAVE_VOLUME
257-
Event::on(
258-
Volumes::class,
259-
Volumes::EVENT_AFTER_SAVE_VOLUME,
260-
function (VolumeEvent $event) {
261-
Craft::debug(
262-
'Volumes::EVENT_AFTER_SAVE_VOLUME',
263-
__METHOD__
264-
);
265-
$settings = $this->getSettings();
266-
// Only worry about this volume if it's not new
267-
if (!$event->isNew && $settings->automaticallyResaveImageVariants) {
268-
/** @var Volume $volume */
269-
$volume = $event->volume;
270-
if (!empty($volume)) {
271-
ImageOptimize::$plugin->optimizedImages->resaveVolumeAssets($volume);
272-
}
273-
}
274-
}
275-
);
276-
277-
// Handler: Plugins::EVENT_AFTER_INSTALL_PLUGIN
278-
Event::on(
279-
Plugins::class,
280-
Plugins::EVENT_AFTER_INSTALL_PLUGIN,
281-
function (PluginEvent $event) {
282-
if ($event->plugin === $this) {
283-
$request = Craft::$app->getRequest();
284-
if ($request->isCpRequest) {
285-
Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('image-optimize/welcome'))->send();
286-
}
287-
}
288-
}
289-
);
230+
$this->installCraftQLEventHandlers();
290231
}
291232

292233
/**
@@ -375,7 +316,7 @@ function (ReplaceAssetEvent $event) {
375316
);
376317
/** @var Asset $element */
377318
$element = $event->asset;
378-
if (!empty($element->id)) {
319+
if ($element->id !== null) {
379320
ImageOptimize::$plugin->optimizedImages->resaveAsset($element->id);
380321
}
381322
}
@@ -434,9 +375,101 @@ function (ElementEvent $event) {
434375
);
435376
}
436377

378+
379+
/**
380+
* Install our miscellaneous event handlers
381+
*/
382+
protected function installMiscEventHandlers()
383+
{
384+
// Handler: Fields::EVENT_AFTER_SAVE_FIELD
385+
Event::on(
386+
Fields::class,
387+
Fields::EVENT_AFTER_SAVE_FIELD,
388+
function (FieldEvent $event) {
389+
Craft::debug(
390+
'Fields::EVENT_AFTER_SAVE_FIELD',
391+
__METHOD__
392+
);
393+
$settings = $this->getSettings();
394+
/** @var Field $field */
395+
if (!$event->isNew && $settings->automaticallyResaveImageVariants) {
396+
$this->checkForOptimizedImagesField($event);
397+
}
398+
}
399+
);
400+
401+
// Handler: Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS
402+
Event::on(
403+
Plugins::class,
404+
Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS,
405+
function (PluginEvent $event) {
406+
if ($event->plugin === $this) {
407+
Craft::debug(
408+
'Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS',
409+
__METHOD__
410+
);
411+
$settings = $this->getSettings();
412+
if ($settings->automaticallyResaveImageVariants) {
413+
// After they have changed the settings, resave all of the assets
414+
ImageOptimize::$plugin->optimizedImages->resaveAllVolumesAssets();
415+
}
416+
}
417+
}
418+
);
419+
420+
// Handler: Volumes::EVENT_AFTER_SAVE_VOLUME
421+
Event::on(
422+
Volumes::class,
423+
Volumes::EVENT_AFTER_SAVE_VOLUME,
424+
function (VolumeEvent $event) {
425+
Craft::debug(
426+
'Volumes::EVENT_AFTER_SAVE_VOLUME',
427+
__METHOD__
428+
);
429+
$settings = $this->getSettings();
430+
// Only worry about this volume if it's not new
431+
if (!$event->isNew && $settings->automaticallyResaveImageVariants) {
432+
/** @var Volume $volume */
433+
$volume = $event->volume;
434+
if ($volume !== null) {
435+
ImageOptimize::$plugin->optimizedImages->resaveVolumeAssets($volume);
436+
}
437+
}
438+
}
439+
);
440+
441+
// Handler: Plugins::EVENT_AFTER_INSTALL_PLUGIN
442+
Event::on(
443+
Plugins::class,
444+
Plugins::EVENT_AFTER_INSTALL_PLUGIN,
445+
function (PluginEvent $event) {
446+
if ($event->plugin === $this) {
447+
$request = Craft::$app->getRequest();
448+
if ($request->isCpRequest) {
449+
Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('image-optimize/welcome'))->send();
450+
}
451+
}
452+
}
453+
);
454+
}
455+
456+
/**
457+
* Install our CraftQL event handlers
458+
*/
459+
protected function installCraftQLEventHandlers()
460+
{
461+
if (class_exists(CraftQL::class)) {
462+
Event::on(
463+
OptimizedImages::class,
464+
GetCraftQLSchema::EVENT_GET_FIELD_SCHEMA,
465+
[new GetCraftQLSchema, 'handle']
466+
);
467+
}
468+
}
469+
437470
/**
438-
* If the Field being saved is an OptimizedImages field, re-save the responsive
439-
* image variants automatically
471+
* If the Field being saved is an OptimizedImages field, re-save the
472+
* responsive image variants automatically
440473
*
441474
* @param FieldEvent $event
442475
*
@@ -456,7 +489,8 @@ protected function checkForOptimizedImagesField(FieldEvent $event)
456489
if ($fieldLayout) {
457490
$fields = $fieldLayout->getFields();
458491
foreach ($fields as $field) {
459-
if ($thisField->handle == $field->handle) {
492+
/** @var Field $field */
493+
if ($thisField->handle === $field->handle) {
460494
$needToReSave = true;
461495
}
462496
}

0 commit comments

Comments
 (0)