4
4
5
5
namespace TypistTech \ImageOptimizeCommand \Operations \AttachmentImages ;
6
6
7
- use Spatie \ImageOptimizer \OptimizerChain ;
8
- use Symfony \Component \Filesystem \Exception \IOException ;
9
7
use TypistTech \ImageOptimizeCommand \LoggerInterface ;
8
+ use TypistTech \ImageOptimizeCommand \Operations \Optimize as BaseOptimize ;
10
9
use TypistTech \ImageOptimizeCommand \Repositories \AttachmentRepository ;
11
- use function WP_CLI \Utils \normalize_path ;
12
10
13
11
class Optimize
14
12
{
@@ -27,120 +25,85 @@ class Optimize
27
25
protected $ repo ;
28
26
29
27
/**
30
- * The optimizer chain .
28
+ * The optimizer operation .
31
29
*
32
- * @var OptimizerChain
30
+ * @var BaseOptimize
33
31
*/
34
- protected $ optimizerChain ;
32
+ protected $ optimize ;
35
33
36
34
/**
37
35
* Optimize constructor.
38
36
*
39
- * @param AttachmentRepository $repo The repo.
40
- * @param OptimizerChain $optimizerChain The optimizer chain .
41
- * @param LoggerInterface $logger The logger.
37
+ * @param AttachmentRepository $repo The repo.
38
+ * @param BaseOptimize $optimize The optimizer operation .
39
+ * @param LoggerInterface $logger The logger.
42
40
*/
43
- public function __construct (AttachmentRepository $ repo , OptimizerChain $ optimizerChain , LoggerInterface $ logger )
41
+ public function __construct (AttachmentRepository $ repo , BaseOptimize $ optimize , LoggerInterface $ logger )
44
42
{
45
43
$ this ->repo = $ repo ;
46
- $ this ->optimizerChain = $ optimizerChain ;
44
+ $ this ->optimize = $ optimize ;
47
45
$ this ->logger = $ logger ;
48
46
}
49
47
50
48
/**
51
49
* Optimize images of attachments.
52
50
*
53
- * @param int ...$ids The attachment IDs.
51
+ * @param int|int[] ...$ids The attachment IDs.
54
52
*
55
53
* @return void
56
54
*/
57
55
public function execute (int ...$ ids ): void
58
56
{
59
57
$ this ->logger ->section ('Optimizing images for ' . count ($ ids ) . ' attachment(s) ' );
60
58
61
- $ ids = array_filter ($ ids , function (int $ id ): bool {
59
+ $ nonOptimizedIds = array_filter ($ ids , function (int $ id ): bool {
62
60
// phpcs:ignore
63
- $ isOptimized = $ this ->repo ->isOptimized ($ id );
64
-
65
- if ($ isOptimized ) {
66
- // phpcs:ignore
67
- $ this ->logger ->warning ('Skip: Attachment already optimized - ID: ' . $ id );
68
- }
69
-
70
- return ! $ isOptimized ;
61
+ return ! $ this ->isOptimized ($ id );
71
62
});
72
- $ ids = array_filter ($ ids );
73
63
74
64
array_map (function (int $ id ): void {
75
65
// phpcs:ignore
76
66
$ this ->optimizeAttachment ($ id );
77
- }, $ ids );
67
+ }, $ nonOptimizedIds );
78
68
79
69
$ this ->logger ->info ('Finished ' );
80
70
}
81
71
82
72
/**
83
- * Optimize all images of an attachment.
73
+ * Whether the attachment is optimized.
74
+ * Log warning if already optimized.
84
75
*
85
76
* @param int $id The attachment ID.
86
77
*
87
- * @return void
78
+ * @return bool
88
79
*/
89
- protected function optimizeAttachment (int $ id ): void
80
+ protected function isOptimized (int $ id ): bool
90
81
{
91
- $ this ->logger ->debug ('Optimizing images of attachment ID: ' . $ id );
92
-
93
- $ paths = $ this ->repo ->getPaths ($ id );
94
-
95
- $ normalizedPaths = array_map (function (string $ path ): string {
96
- return normalize_path ($ path );
97
- }, $ paths );
98
-
99
- $ results = array_map (function (string $ imagePath ): bool {
82
+ if ($ this ->repo ->isOptimized ($ id )) {
100
83
// phpcs:ignore
101
- return $ this ->optimizeImage ($ imagePath );
102
- }, $ normalizedPaths );
84
+ $ this ->logger ->warning ('Skip: Attachment already optimized - ID: ' . $ id );
103
85
104
- if (in_array (true , $ results , true )) {
105
- $ this ->logger ->debug ('Marking attachment ID: ' . $ id . ' as optimized. ' );
106
- $ this ->repo ->markAsOptimized ($ id );
107
- $ this ->logger ->notice ('Optimized images of attachment ID: ' . $ id );
86
+ return true ;
108
87
}
88
+
89
+ return false ;
109
90
}
110
91
111
92
/**
112
- * Optimize an image .
93
+ * Optimize all images of an attachment .
113
94
*
114
- * @param string $path Path to the image .
95
+ * @param int $id The attachment ID .
115
96
*
116
- * @return bool
97
+ * @return void
117
98
*/
118
- protected function optimizeImage ( string $ path ): bool
99
+ protected function optimizeAttachment ( int $ id ): void
119
100
{
120
- try {
121
- $ this ->logger ->debug ('Optimizing image - ' . $ path );
122
-
123
- if (! is_readable ($ path )) {
124
- $ this ->logger ->error ('Image not readable - ' . $ path );
125
-
126
- return false ;
127
- }
128
-
129
- // phpcs:ignore WordPress.VIP.FileSystemWritesDisallow.file_ops_is_writable
130
- if (! is_writable ($ path )) {
131
- $ this ->logger ->error ('Image not writable - ' . $ path );
101
+ $ this ->logger ->section ('Optimizing images of attachment ID: ' . $ id );
132
102
133
- return false ;
134
- }
135
-
136
- $ this ->optimizerChain ->optimize ($ path );
137
-
138
- return true ;
139
- // phpcs:ignore
140
- } catch (IOException $ exception ) {
141
- $ this ->logger ->error ('Failed to optimize ' . $ path );
103
+ $ paths = $ this ->repo ->getPaths ($ id );
104
+ $ this ->optimize ->execute (...$ paths );
142
105
143
- return false ;
144
- }
106
+ $ this -> repo -> markAsOptimized ( $ id ) ;
107
+ $ this -> logger -> info ( ' Marked attachment ID: ' . $ id . ' as optimized. ' );
145
108
}
146
109
}
0 commit comments