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 |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\imageoptimize\jobs; |
12
|
|
|
|
13
|
|
|
use Craft; |
14
|
|
|
use craft\base\ElementInterface; |
|
|
|
|
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
|
|
|
/** |
|
|
|
|
27
|
|
|
* @author nystudio107 |
|
|
|
|
28
|
|
|
* @package ImageOptimize |
|
|
|
|
29
|
|
|
* @since 1.4.8 |
|
|
|
|
30
|
|
|
*/ |
|
|
|
|
31
|
|
|
class ResaveOptimizedImages extends BaseJob |
32
|
|
|
{ |
33
|
|
|
// Constants |
34
|
|
|
// ========================================================================= |
35
|
|
|
|
36
|
|
|
/** |
|
|
|
|
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
|
|
|
/** |
|
|
|
|
45
|
|
|
* @var ?array The element criteria that determines which elements should be resaved |
46
|
|
|
*/ |
47
|
|
|
public ?array $criteria = null; |
48
|
|
|
|
49
|
|
|
/** |
|
|
|
|
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
|
|
|
/** |
|
|
|
|
55
|
|
|
* @var bool Whether image variants should be forced to recreated, even if they already exist on disk |
|
|
|
|
56
|
|
|
* @since 1.6.18 |
57
|
|
|
*/ |
58
|
|
|
public bool $force = false; |
59
|
|
|
|
60
|
|
|
// Public Methods |
61
|
|
|
// ========================================================================= |
62
|
|
|
|
63
|
|
|
/** |
|
|
|
|
64
|
|
|
* @inheritdoc |
65
|
|
|
*/ |
|
|
|
|
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 */ |
|
|
|
|
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, [ |
|
|
|
|
84
|
|
|
'pageSize' => self::ASSET_QUERY_PAGE_SIZE, |
85
|
|
|
]); |
|
|
|
|
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 */ |
|
|
|
|
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 */ |
|
|
|
|
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
|
|
|
/** |
|
|
|
|
139
|
|
|
* @inheritdoc |
140
|
|
|
*/ |
|
|
|
|
141
|
|
|
protected function defaultDescription(): ?string |
142
|
|
|
{ |
143
|
|
|
return Craft::t('app', 'Resaving {class} elements', [ |
|
|
|
|
144
|
|
|
'class' => App::humanizeClass(Asset::class), |
145
|
|
|
]); |
|
|
|
|
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|