Passed
Push — develop-v4 ( 41334a...545c86 )
by Andrew
19:30 queued 14:59
created

ImageOptimize::setImageTransformComponent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 15
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
/**
3
 * ImageOptimize plugin for Craft CMS 3.x
4
 *
5
 * Automatically optimize images after they've been transformed
6
 *
7
 * @link      https://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2017 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\imageoptimize;
12
13
use Craft;
14
use craft\base\Field;
15
use craft\base\Model;
16
use craft\base\Plugin;
17
use craft\elements\Asset;
0 ignored issues
show
Bug introduced by
The type craft\elements\Asset was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use craft\events\DefineAssetThumbUrlEvent;
19
use craft\events\DefineAssetUrlEvent;
20
use craft\events\ElementEvent;
21
use craft\events\FieldEvent;
22
use craft\events\ImageTransformerOperationEvent;
23
use craft\events\PluginEvent;
24
use craft\events\RegisterComponentTypesEvent;
25
use craft\events\RegisterTemplateRootsEvent;
26
use craft\events\RegisterUrlRulesEvent;
27
use craft\events\ReplaceAssetEvent;
28
use craft\events\VolumeEvent;
29
use craft\helpers\ArrayHelper;
30
use craft\helpers\UrlHelper;
31
use craft\imagetransforms\ImageTransformer;
32
use craft\models\FieldLayout;
33
use craft\services\Assets;
34
use craft\services\Elements;
35
use craft\services\Fields;
36
use craft\services\Plugins;
37
use craft\services\Utilities;
38
use craft\services\Volumes;
39
use craft\web\Controller;
40
use craft\web\TemplateResponseBehavior;
41
use craft\web\twig\variables\CraftVariable;
42
use craft\web\UrlManager;
43
use craft\web\View;
44
use nystudio107\imageoptimize\fields\OptimizedImages;
45
use nystudio107\imageoptimize\imagetransforms\CraftImageTransform;
46
use nystudio107\imageoptimize\imagetransforms\ImageTransformInterface;
47
use nystudio107\imageoptimize\models\Settings;
48
use nystudio107\imageoptimize\services\ServicesTrait;
49
use nystudio107\imageoptimize\utilities\ImageOptimizeUtility;
50
use nystudio107\imageoptimize\variables\ImageOptimizeVariable;
51
use yii\base\Event;
52
use yii\base\Exception;
53
use yii\base\InvalidConfigException;
54
use yii\web\Response;
55
use function function_exists;
56
57
/** @noinspection MissingPropertyAnnotationsInspection */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
58
59
/**
60
 * Class ImageOptimize
61
 *
62
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
63
 * @package   ImageOptimize
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
64
 * @since     1.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
65
 *
66
 * @property ImageTransformInterface $transformMethod
67
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
68
class ImageOptimize extends Plugin
69
{
70
    // Traits
71
    // =========================================================================
72
73
    use ServicesTrait;
74
75
    // Static Properties
76
    // =========================================================================
77
78
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
79
     * @var ?ImageOptimize
80
     */
81
    public static ?ImageOptimize $plugin = null;
82
83
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
84
     * @var bool
85
     */
86
    public static bool $generatePlaceholders = true;
87
88
    // Public Properties
89
    // =========================================================================
90
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
91
     * @var string
92
     */
93
    public string $schemaVersion = '1.0.0';
94
95
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
96
     * @var bool
97
     */
98
    public bool $hasCpSection = false;
99
100
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
101
     * @var bool
102
     */
103
    public bool $hasCpSettings = true;
104
105
    // Public Methods
106
    // =========================================================================
107
108
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
109
     * @inheritdoc
110
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
111
    public function init(): void
112
    {
113
        parent::init();
114
        self::$plugin = $this;
115
        // Handle any console commands
116
        $request = Craft::$app->getRequest();
117
        if ($request->getIsConsoleRequest()) {
118
            $this->controllerNamespace = 'nystudio107\imageoptimize\console\controllers';
119
        }
120
        // Set the image transform component
121
        $this->setImageTransformComponent();
122
        // Add in our Craft components
123
        $this->addComponents();
124
        // Install our global event handlers
125
        $this->installEventHandlers();
126
        // Log that the plugin has loaded
127
        Craft::info(
128
            Craft::t(
129
                'image-optimize',
130
                '{name} plugin loaded',
131
                ['name' => $this->name]
132
            ),
133
            __METHOD__
134
        );
135
    }
136
137
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
138
     * @inheritdoc
139
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
140
    public function getSettingsResponse(): TemplateResponseBehavior|Response
141
    {
142
        $view = Craft::$app->getView();
143
        $namespace = $view->getNamespace();
144
        $view->setNamespace('settings');
145
        $settingsHtml = $this->settingsHtml();
146
        $view->setNamespace($namespace);
147
        /** @var Controller $controller */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
148
        $controller = Craft::$app->controller;
0 ignored issues
show
Documentation Bug introduced by
It seems like Craft::app->controller can also be of type yii\web\Controller. However, the property $controller is declared as type yii\console\Controller. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
149
150
        return $controller->renderTemplate('image-optimize/settings/index.twig', [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
151
            'plugin' => $this,
152
            'settingsHtml' => $settingsHtml,
153
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
154
    }
155
156
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
157
     * @inheritdoc
158
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
159
    public function settingsHtml(): ?string
160
    {
161
        // Get only the user-editable settings
162
        $settings = $this->getSettings();
163
164
        // Get the image transform types
165
        $allImageTransformTypes = self::$plugin->optimize->getAllImageTransformTypes();
166
        $imageTransformTypeOptions = [];
167
        /** @var ImageTransformInterface $class */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
168
        foreach ($allImageTransformTypes as $class) {
169
            if ($class::isSelectable()) {
170
                $imageTransformTypeOptions[] = [
171
                    'value' => $class,
172
                    'label' => $class::displayName(),
173
                ];
174
            }
175
        }
176
        // Sort them by name
177
        ArrayHelper::multisort($imageTransformTypeOptions, 'label');
178
179
        // Render the settings template
180
        try {
181
            return Craft::$app->getView()->renderTemplate(
182
                'image-optimize/settings/_settings.twig',
183
                [
184
                    'settings' => $settings,
185
                    'gdInstalled' => function_exists('imagecreatefromjpeg'),
186
                    'imageTransformTypeOptions' => $imageTransformTypeOptions,
187
                    'allImageTransformTypes' => $allImageTransformTypes,
188
                    'imageTransform' => self::$plugin->transformMethod,
189
                ]
190
            );
191
        } catch (Exception $e) {
192
            Craft::error($e->getMessage(), __METHOD__);
193
        }
194
195
        return '';
196
    }
197
198
    // Protected Methods
199
    // =========================================================================
200
201
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
202
     * @inheritdoc
203
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
204
    protected function createSettingsModel(): ?Model
205
    {
206
        return new Settings();
207
    }
208
209
    /**
210
     * Set the transformMethod component
211
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
212
    protected function setImageTransformComponent(): void
213
    {
214
        /* @var Settings $settings */
215
        $settings = $this->getSettings();
216
        if ($settings === null) {
217
            return;
218
        }
219
        $definition = array_merge(
220
            $settings->imageTransformTypeSettings[$settings->transformClass] ?? [],
221
            ['class' => $settings->transformClass]
222
        );
223
        try {
224
            $this->set('transformMethod', $definition);
225
        } catch (InvalidConfigException $e) {
226
            Craft::error($e->getMessage(), __METHOD__);
227
        }
228
    }
229
230
    /**
231
     * Add in our Craft components
232
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
233
    protected function addComponents(): void
234
    {
235
        // Register our variables
236
        Event::on(
237
            CraftVariable::class,
238
            CraftVariable::EVENT_INIT,
239
            function (Event $event) {
240
                /** @var CraftVariable $variable */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
241
                $variable = $event->sender;
242
                $variable->set('imageOptimize', [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
243
                    'class' => ImageOptimizeVariable::class,
244
                    'viteService' => $this->vite,
245
                ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
246
            }
247
        );
248
249
        // Register our Field
250
        Event::on(
251
            Fields::class,
252
            Fields::EVENT_REGISTER_FIELD_TYPES,
253
            static function (RegisterComponentTypesEvent $event) {
254
                Craft::debug(
255
                    'Fields::EVENT_REGISTER_FIELD_TYPES',
256
                    __METHOD__
257
                );
258
                $event->types[] = OptimizedImages::class;
259
            }
260
        );
261
262
        // Register our Utility only if they are using the CraftImageTransform method
263
        if (self::$plugin->transformMethod instanceof CraftImageTransform) {
264
            Event::on(
265
                Utilities::class,
266
                Utilities::EVENT_REGISTER_UTILITY_TYPES,
267
                static function (RegisterComponentTypesEvent $event) {
268
                    $event->types[] = ImageOptimizeUtility::class;
269
                }
270
            );
271
        }
272
    }
273
274
    /**
275
     * Install our event handlers
276
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
277
    protected function installEventHandlers(): void
278
    {
279
        $this->installAssetEventHandlers();
280
        $this->installElementEventHandlers();
281
        $this->installMiscEventHandlers();
282
        $request = Craft::$app->getRequest();
283
        // Install only for non-console site requests
284
        if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) {
285
            $this->installSiteEventListeners();
286
        }
287
        // Install only for non-console cp requests
288
        if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) {
289
            $this->installCpEventListeners();
290
        }
291
    }
292
293
    /**
294
     * Install our Asset event handlers
295
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
296
    protected function installAssetEventHandlers(): void
297
    {
298
        // Handler: Assets::EVENT_GET_ASSET_URL
299
        Event::on(
300
            Asset::class,
301
            Asset::EVENT_DEFINE_URL,
302
            static function (DefineAssetUrlEvent $event): void {
303
                Craft::debug(
304
                    'Asset::EVENT_GET_ASSET_URL',
305
                    __METHOD__
306
                );
307
                // Return the URL to the asset URL or null to let Craft handle it
308
                $event->url = ImageOptimize::$plugin->optimize->handleGetAssetUrlEvent(
309
                    $event
310
                );
311
            }
312
        );
313
314
        // Handler: Assets::EVENT_GET_ASSET_THUMB_URL
315
        Event::on(
316
            Assets::class,
317
            Assets::EVENT_DEFINE_THUMB_URL,
318
            static function (DefineAssetThumbUrlEvent $event): void {
319
                Craft::debug(
320
                    'Assets::EVENT_DEFINE_THUMB_URL',
321
                    __METHOD__
322
                );
323
                // Return the URL to the asset URL or null to let Craft handle it
324
                $event->url = ImageOptimize::$plugin->optimize->handleGetAssetThumbUrlEvent(
325
                    $event
326
                );
327
            }
328
        );
329
330
        // Handler: ImageTransformer::EVENT_TRANSFORM_IMAGE
331
        Event::on(
332
            ImageTransformer::class,
333
            ImageTransformer::EVENT_TRANSFORM_IMAGE,
334
            static function (ImageTransformerOperationEvent $event): void {
335
                Craft::debug(
336
                    'ImageTransformer::EVENT_TRANSFORM_IMAGE',
337
                    __METHOD__
338
                );
339
                // Return the path to the optimized image to _createTransformForAsset()
340
                $event->path = ImageOptimize::$plugin->optimize->handleGenerateTransformEvent(
341
                    $event
342
                );
343
            }
344
        );
345
346
        // Handler: ImageTransformer::EVENT_DELETE_TRANSFORMED_IMAGE
347
        Event::on(
348
            ImageTransformer::class,
349
            ImageTransformer::EVENT_DELETE_TRANSFORMED_IMAGE,
350
            static function (ImageTransformerOperationEvent $event): void {
351
                Craft::debug(
352
                    'ImageTransformer::EVENT_DELETE_TRANSFORMED_IMAGE',
353
                    __METHOD__
354
                );
355
                // Clean up any stray variant files
356
                ImageOptimize::$plugin->optimize->handleAfterDeleteTransformsEvent(
357
                    $event
358
                );
359
            }
360
        );
361
362
        // Handler: Assets::EVENT_BEFORE_REPLACE_ASSET
363
        Event::on(
364
            Assets::class,
365
            Assets::EVENT_BEFORE_REPLACE_ASSET,
366
            static function (ReplaceAssetEvent $event) {
367
                Craft::debug(
368
                    'Assets::EVENT_BEFORE_REPLACE_ASSET',
369
                    __METHOD__
370
                );
371
                $element = $event->asset;
372
                // Purge the URL
373
                $purgeUrl = ImageOptimize::$plugin->transformMethod->getPurgeUrl($element);
374
                if ($purgeUrl) {
375
                    ImageOptimize::$plugin->transformMethod->purgeUrl($purgeUrl);
376
                }
377
            }
378
        );
379
380
        // Handler: Assets::EVENT_AFTER_REPLACE_ASSET
381
        Event::on(
382
            Assets::class,
383
            Assets::EVENT_AFTER_REPLACE_ASSET,
384
            static function (ReplaceAssetEvent $event) {
385
                Craft::debug(
386
                    'Assets::EVENT_AFTER_REPLACE_ASSET',
387
                    __METHOD__
388
                );
389
                $element = $event->asset;
390
                if ($element->id !== null) {
391
                    ImageOptimize::$plugin->optimizedImages->resaveAsset($element->id, true);
392
                }
393
            }
394
        );
395
    }
396
397
    /**
398
     * Install our Element event handlers
399
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
400
    protected function installElementEventHandlers(): void
401
    {
402
        // Handler: Elements::EVENT_BEFORE_SAVE_ELEMENT
403
        Event::on(
404
            Assets::class,
405
            Elements::EVENT_BEFORE_SAVE_ELEMENT,
406
            static function (ElementEvent $event) {
407
                Craft::debug(
408
                    'Elements::EVENT_BEFORE_SAVE_ELEMENT',
409
                    __METHOD__
410
                );
411
                /** @var Asset $asset */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
412
                $asset = $event->element;
413
                if (!$event->isNew) {
414
                    // Purge the URL
415
                    $purgeUrl = ImageOptimize::$plugin->transformMethod->getPurgeUrl($asset);
416
                    if ($purgeUrl) {
417
                        ImageOptimize::$plugin->transformMethod->purgeUrl($purgeUrl);
418
                    }
419
                }
420
            }
421
        );
422
423
        // Handler: Elements::EVENT_BEFORE_DELETE_ELEMENT
424
        Event::on(
425
            Asset::class,
426
            Elements::EVENT_BEFORE_DELETE_ELEMENT,
427
            static function (ElementEvent $event) {
428
                Craft::debug(
429
                    'Elements::EVENT_BEFORE_DELETE_ELEMENT',
430
                    __METHOD__
431
                );
432
                /** @var Asset $asset */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
433
                $asset = $event->element;
434
                // Purge the URL
435
                $purgeUrl = ImageOptimize::$plugin->transformMethod->getPurgeUrl($asset);
436
                if ($purgeUrl) {
437
                    ImageOptimize::$plugin->transformMethod->purgeUrl($purgeUrl);
438
                }
439
            }
440
        );
441
    }
442
443
    /**
444
     * Install our miscellaneous event handlers
445
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
446
    protected function installMiscEventHandlers(): void
447
    {
448
        // Handler: Fields::EVENT_AFTER_SAVE_FIELD
449
        Event::on(
450
            Fields::class,
451
            Fields::EVENT_AFTER_SAVE_FIELD,
452
            function (FieldEvent $event) {
453
                Craft::debug(
454
                    'Fields::EVENT_AFTER_SAVE_FIELD',
455
                    __METHOD__
456
                );
457
                /* @var Settings $settings */
458
                $settings = $this->getSettings();
459
                /** @var Field $field */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
460
                if (($settings !== null) && !$event->isNew && $settings->automaticallyResaveImageVariants) {
461
                    $this->checkForOptimizedImagesField($event);
462
                }
463
            }
464
        );
465
466
        // Handler: Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS
467
        Event::on(
468
            Plugins::class,
469
            Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS,
470
            function (PluginEvent $event) {
471
                if ($event->plugin === $this) {
0 ignored issues
show
introduced by
The condition $event->plugin === $this is always false.
Loading history...
472
                    Craft::debug(
473
                        'Plugins::EVENT_AFTER_SAVE_PLUGIN_SETTINGS',
474
                        __METHOD__
475
                    );
476
                    /* @var Settings $settings */
477
                    $settings = $this->getSettings();
478
                    if (($settings !== null) && $settings->automaticallyResaveImageVariants) {
479
                        // After they have changed the settings, resave all the assets
480
                        ImageOptimize::$plugin->optimizedImages->resaveAllVolumesAssets();
481
                    }
482
                }
483
            }
484
        );
485
486
        // Handler: Volumes::EVENT_AFTER_SAVE_VOLUME
487
        Event::on(
488
            Volumes::class,
489
            Volumes::EVENT_AFTER_SAVE_VOLUME,
490
            function (VolumeEvent $event) {
491
                Craft::debug(
492
                    'Volumes::EVENT_AFTER_SAVE_VOLUME',
493
                    __METHOD__
494
                );
495
                /* @var Settings $settings */
496
                $settings = $this->getSettings();
497
                // Only worry about this volume if it's not new
498
                if (($settings !== null) && !$event->isNew && $settings->automaticallyResaveImageVariants) {
499
                    $volume = $event->volume;
500
                    ImageOptimize::$plugin->optimizedImages->resaveVolumeAssets($volume);
501
                }
502
            }
503
        );
504
505
        // Handler: Plugins::EVENT_AFTER_INSTALL_PLUGIN
506
        Event::on(
507
            Plugins::class,
508
            Plugins::EVENT_AFTER_INSTALL_PLUGIN,
509
            function (PluginEvent $event) {
510
                if ($event->plugin === $this) {
0 ignored issues
show
introduced by
The condition $event->plugin === $this is always false.
Loading history...
511
                    $request = Craft::$app->getRequest();
512
                    if ($request->isCpRequest) {
513
                        Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('image-optimize/welcome'))->send();
514
                    }
515
                }
516
            }
517
        );
518
    }
519
520
    /**
521
     * Install site event listeners for site requests only
522
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
523
    protected function installSiteEventListeners(): void
524
    {
525
        // Handler: UrlManager::EVENT_REGISTER_SITE_URL_RULES
526
        Event::on(
527
            UrlManager::class,
528
            UrlManager::EVENT_REGISTER_SITE_URL_RULES,
529
            function (RegisterUrlRulesEvent $event) {
530
                Craft::debug(
531
                    'UrlManager::EVENT_REGISTER_SITE_URL_RULES',
532
                    __METHOD__
533
                );
534
                // Register our Control Panel routes
535
                $event->rules = array_merge(
536
                    $event->rules,
537
                    $this->customFrontendRoutes()
538
                );
539
            }
540
        );
541
    }
542
543
    /**
544
     * Install site event listeners for cp requests only
545
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
546
    protected function installCpEventListeners(): void
547
    {
548
        // Handler: Plugins::EVENT_AFTER_LOAD_PLUGINS
549
        Event::on(
550
            Plugins::class,
551
            Plugins::EVENT_AFTER_LOAD_PLUGINS,
552
            static function () {
553
                // Install these only after all other plugins have loaded
554
                Event::on(
555
                    View::class,
556
                    View::EVENT_REGISTER_CP_TEMPLATE_ROOTS,
557
                    static function (RegisterTemplateRootsEvent $e) {
558
                        // Register the root directodies
559
                        $allImageTransformTypes = ImageOptimize::$plugin->optimize->getAllImageTransformTypes();
560
                        /** @var ImageTransformInterface $imageTransformType */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
561
                        foreach ($allImageTransformTypes as $imageTransformType) {
562
                            [$id, $baseDir] = $imageTransformType::getTemplatesRoot();
563
                            if (is_dir($baseDir)) {
564
                                $e->roots[$id] = $baseDir;
565
                            }
566
                        }
567
                    }
568
                );
569
            }
570
        );
571
    }
572
573
    /**
574
     * Return the custom frontend routes
575
     *
576
     * @return array
577
     */
578
    protected function customFrontendRoutes(): array
579
    {
580
        return [
581
        ];
582
    }
583
584
    /**
585
     * If the Field being saved is an OptimizedImages field, re-save the
586
     * responsive image variants automatically
587
     *
588
     * @param FieldEvent $event
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
589
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
590
    protected function checkForOptimizedImagesField(FieldEvent $event): void
591
    {
592
        $thisField = $event->field;
593
        if ($thisField instanceof OptimizedImages) {
594
            $volumes = Craft::$app->getVolumes()->getAllVolumes();
595
            foreach ($volumes as $volume) {
596
                $needToReSave = false;
597
                /** @var FieldLayout $fieldLayout */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
598
                $fieldLayout = $volume->getFieldLayout();
599
                // Loop through the fields in the layout to see if it contains our field
600
                if ($fieldLayout) {
601
                    $fields = $fieldLayout->getCustomFields();
602
                    foreach ($fields as $field) {
603
                        /** @var Field $field */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
604
                        if ($thisField->handle === $field->handle) {
605
                            $needToReSave = true;
606
                        }
607
                    }
608
                    if ($needToReSave) {
609
                        self::$plugin->optimizedImages->resaveVolumeAssets($volume, $thisField->id);
0 ignored issues
show
Bug introduced by
It seems like $thisField->id can also be of type string; however, parameter $fieldId of nystudio107\imageoptimiz...s::resaveVolumeAssets() does only seem to accept integer|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

609
                        self::$plugin->optimizedImages->resaveVolumeAssets($volume, /** @scrutinizer ignore-type */ $thisField->id);
Loading history...
610
                    }
611
                }
612
            }
613
        }
614
    }
615
}
616