nystudio107 /
craft-imageoptimize
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * ImageOptimize plugin for Craft CMS |
||
| 4 | * |
||
| 5 | * Automatically optimize images after they've been transformed |
||
| 6 | * |
||
| 7 | * @link https://nystudio107.com |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 8 | * @copyright Copyright (c) 2017 nystudio107 |
||
|
0 ignored issues
–
show
|
|||
| 9 | */ |
||
|
0 ignored issues
–
show
|
|||
| 10 | |||
| 11 | namespace nystudio107\imageoptimize\jobs; |
||
| 12 | |||
| 13 | use Craft; |
||
| 14 | use craft\base\ElementInterface; |
||
|
0 ignored issues
–
show
The type
craft\base\ElementInterface 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. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 15 | use craft\base\Field; |
||
| 16 | use craft\console\Application as ConsoleApplication; |
||
| 17 | use craft\db\Paginator; |
||
| 18 | use craft\elements\Asset; |
||
| 19 | use craft\elements\db\ElementQuery; |
||
| 20 | use craft\helpers\App; |
||
| 21 | use craft\queue\BaseJob; |
||
| 22 | use nystudio107\imageoptimize\fields\OptimizedImages as OptimizedImagesField; |
||
| 23 | use nystudio107\imageoptimize\ImageOptimize; |
||
| 24 | use yii\base\Exception; |
||
| 25 | |||
| 26 | /** |
||
|
0 ignored issues
–
show
|
|||
| 27 | * @author nystudio107 |
||
|
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||
| 28 | * @package ImageOptimize |
||
|
0 ignored issues
–
show
|
|||
| 29 | * @since 1.4.8 |
||
|
0 ignored issues
–
show
|
|||
| 30 | */ |
||
|
0 ignored issues
–
show
|
|||
| 31 | class ResaveOptimizedImages extends BaseJob |
||
| 32 | { |
||
| 33 | // Constants |
||
| 34 | // ========================================================================= |
||
| 35 | |||
| 36 | /** |
||
|
0 ignored issues
–
show
|
|||
| 37 | * @const The number of assets to return in a single paginated query |
||
| 38 | */ |
||
| 39 | protected const ASSET_QUERY_PAGE_SIZE = 100; |
||
| 40 | |||
| 41 | // Properties |
||
| 42 | // ========================================================================= |
||
| 43 | |||
| 44 | /** |
||
|
0 ignored issues
–
show
|
|||
| 45 | * @var ?array The element criteria that determines which elements should be resaved |
||
| 46 | */ |
||
| 47 | public ?array $criteria = null; |
||
| 48 | |||
| 49 | /** |
||
|
0 ignored issues
–
show
|
|||
| 50 | * @var int|null The id of the field to resave images for, or null for all images |
||
| 51 | */ |
||
| 52 | public ?int $fieldId = null; |
||
| 53 | |||
| 54 | /** |
||
|
0 ignored issues
–
show
|
|||
| 55 | * @var bool Whether image variants should be forced to recreated, even if they already exist on disk |
||
|
0 ignored issues
–
show
|
|||
| 56 | * @since 1.6.18 |
||
| 57 | */ |
||
| 58 | public bool $force = false; |
||
| 59 | |||
| 60 | // Public Methods |
||
| 61 | // ========================================================================= |
||
| 62 | |||
| 63 | /** |
||
|
0 ignored issues
–
show
|
|||
| 64 | * @inheritdoc |
||
| 65 | */ |
||
|
0 ignored issues
–
show
|
|||
| 66 | public function execute($queue): void |
||
| 67 | { |
||
| 68 | // Let's save ourselves some trouble and just clear all the caches for this element class |
||
| 69 | Craft::$app->getElements()->invalidateCachesForElementType(Asset::class); |
||
| 70 | |||
| 71 | // Now find the affected element IDs |
||
| 72 | /** @var ElementQuery $query */ |
||
|
0 ignored issues
–
show
|
|||
| 73 | $query = Asset::find(); |
||
| 74 | if (!empty($this->criteria)) { |
||
| 75 | Craft::configure($query, $this->criteria); |
||
| 76 | } |
||
| 77 | if (Craft::$app instanceof ConsoleApplication) { |
||
| 78 | echo $this->description . PHP_EOL; |
||
| 79 | } |
||
| 80 | // Use craft\db\Paginator to paginate the results so we don't exceed any memory limits |
||
| 81 | // See batch() and each() discussion here: https://github.com/yiisoft/yii2/issues/8420 |
||
| 82 | // and here: https://github.com/craftcms/cms/issues/7338 |
||
| 83 | $paginator = new Paginator($query, [ |
||
|
0 ignored issues
–
show
|
|||
| 84 | 'pageSize' => self::ASSET_QUERY_PAGE_SIZE, |
||
| 85 | ]); |
||
|
0 ignored issues
–
show
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...
|
|||
| 86 | $currentElement = 0; |
||
| 87 | $totalElements = $paginator->getTotalResults(); |
||
| 88 | // Iterate through the paginated results |
||
| 89 | while ($currentElement < $totalElements) { |
||
| 90 | $elements = $paginator->getPageResults(); |
||
| 91 | if (Craft::$app instanceof ConsoleApplication) { |
||
| 92 | echo 'Query ' . $paginator->getCurrentPage() . '/' . $paginator->getTotalPages() |
||
| 93 | . ' - assets: ' . $paginator->getTotalResults() |
||
| 94 | . PHP_EOL; |
||
| 95 | } |
||
| 96 | /** @var ElementInterface $element */ |
||
|
0 ignored issues
–
show
|
|||
| 97 | foreach ($elements as $element) { |
||
| 98 | $currentElement++; |
||
| 99 | // Find each OptimizedImages field and process it |
||
| 100 | $layout = $element->getFieldLayout(); |
||
| 101 | if ($layout !== null) { |
||
| 102 | $fields = $layout->getCustomFields(); |
||
| 103 | /** @var Field $field */ |
||
|
0 ignored issues
–
show
|
|||
| 104 | foreach ($fields as $field) { |
||
| 105 | if ($field instanceof OptimizedImagesField && $element instanceof Asset) { |
||
| 106 | if ($this->fieldId === null || (int)$field->id === (int)$this->fieldId) { |
||
| 107 | if (Craft::$app instanceof ConsoleApplication) { |
||
| 108 | echo $currentElement . '/' . $totalElements |
||
| 109 | . ' - processing asset: ' . $element->title |
||
| 110 | . ' from field: ' . $field->name . PHP_EOL; |
||
| 111 | } |
||
| 112 | try { |
||
| 113 | ImageOptimize::$plugin->optimizedImages->updateOptimizedImageFieldData($field, $element, $this->force); |
||
| 114 | } catch (Exception $e) { |
||
| 115 | Craft::error($e->getMessage(), __METHOD__); |
||
| 116 | if (Craft::$app instanceof ConsoleApplication) { |
||
| 117 | echo '[error]: ' |
||
| 118 | . $e->getMessage() |
||
| 119 | . ' while processing ' |
||
| 120 | . $currentElement . '/' . $totalElements |
||
| 121 | . ' - processing asset: ' . $element->title |
||
| 122 | . ' from field: ' . $field->name . PHP_EOL; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | } |
||
| 126 | } |
||
| 127 | } |
||
| 128 | } |
||
| 129 | $this->setProgress($queue, $currentElement / $totalElements); |
||
| 130 | } |
||
| 131 | $paginator->currentPage++; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | // Protected Methods |
||
| 136 | // ========================================================================= |
||
| 137 | |||
| 138 | /** |
||
|
0 ignored issues
–
show
|
|||
| 139 | * @inheritdoc |
||
| 140 | */ |
||
|
0 ignored issues
–
show
|
|||
| 141 | protected function defaultDescription(): ?string |
||
| 142 | { |
||
| 143 | return Craft::t('app', 'Resaving {class} elements', [ |
||
|
0 ignored issues
–
show
|
|||
| 144 | 'class' => App::humanizeClass(Asset::class), |
||
| 145 | ]); |
||
|
0 ignored issues
–
show
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...
|
|||
| 146 | } |
||
| 147 | } |
||
| 148 |