-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathService.php
207 lines (186 loc) · 4.21 KB
/
Service.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
* Service
*
* Service os responsible for processing file attachments
* and executing commands.
*
* @package Squidge
* @version 0.1.4
* @author Ainsley Clark
* @category Class
* @repo https://github.com/ainsleyclark/squidge
*
*/
namespace Squidge\Package;
use Exception;
use Squidge\Log\Logger;
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
class Service
{
/**
* META_KEY is the meta key for lookup for the
* service.
*/
const META_KEY = "_squidge_compressed";
/**
* Processes the file attachment.
*
* @param $attachment
* @param $args
* @return void
* @throws Exception
* @since 0.1.0
* @date 24/11/2021
*/
public static function process($attachment, $args)
{
// If the attachment is an ID, obtain the metadata.
if (is_int($attachment)) {
$attachment = wp_get_attachment_metadata($attachment);
}
// Return if the library is not installed.
if (!self::installed()) {
return;
}
// Check if the file key exists.
if (!isset($attachment['file'])) {
throw new Exception("File attachment is not set.");
}
// Obtain the file and check if it exists.
$mainFile = self::get_file_path($attachment['file']);
if (!$mainFile) {
return;
}
// Check if the attachment has already been compressed.
$id = attachment_url_to_postid($attachment['file']);
if (self::has_compressed($id)) {
return;
}
// Convert main image.
static::convert($mainFile, self::get_mime_type($mainFile), $args);
// Loop over the sizes and convert them.
foreach ($attachment['sizes'] as $size) {
if (!isset($size['file'])) {
continue;
}
$path = self::get_file_path($size['file']);
if (!$path) {
continue;
}
static::convert($path, self::get_mime_type($path), $args);
}
// Update post meta for attachment.
self::update_meta($id);
}
/**
* Deletes all images with file extensions associated
* with the image (if the file exists).
*
* @param $id
* @return void
* @since 0.1.0
* @date 24/11/2021
*/
public static function delete($id)
{
// Delete the original file.
$original = wp_get_original_image_path($id) . static::extension();
if (file_exists($original)) {
unlink($original);
}
// Delete the image sizes.
$sizes = get_intermediate_image_sizes();
foreach ($sizes as $size) {
$src = wp_get_attachment_image_src($id, $size);
$path = self::get_file_path($src[0] . static::extension());
if (!$path) {
continue;
}
unlink($path);
}
}
/**
* Checks if a command exists.
*
* shell_exec is required to check to see if the library
* is installed on the client's operating system.
* Checks to see if the command name is in the allowed
* array before continuing.
*
* @return bool
* @since 0.1.2
* @date 05/12/2021
*/
public static function installed()
{
$allowed = [
'jpegoptim',
'optipng',
'cwebp',
'avifenc',
];
if (!in_array(static::cmd_name(), $allowed)) {
return false;
}
$return = shell_exec(sprintf("which %s", escapeshellarg(static::cmd_name())));
return !empty($return);
}
/**
* Obtains the absolute filepath including the
* upload directory.
* If the file does not exist on the file system, the
* function will return false.
*
* @param $path
* @return string
* @since 0.1.0
* @date 24/11/2021
*/
private static function get_file_path($path)
{
$file = wp_get_upload_dir()['path'] . DIRECTORY_SEPARATOR . basename($path);
if (file_exists($file)) {
return $file;
}
return false;
}
/**
* Returns the mimetype of a file passed.
*
* @param $file
* @return false|string
* @since 0.1.0
* @date 24/11/2021
*/
private static function get_mime_type($file)
{
return mime_content_type($file);
}
/**
* Determines if the attachment has been compressed.
*
* @param $id
* @return bool
* @since 0.1.4
* @date 21/12/2021
*/
public static function has_compressed($id)
{
$meta = get_post_meta($id, self::META_KEY . '_' . static::cmd_name());
return !empty($meta);
}
/**
* Updates post meta for squidge.
*
* @param $id
* @since 0.1.4
* @date 21/12/2021
*/
public static function update_meta($id)
{
update_post_meta($id, self::META_KEY . '_' . static::cmd_name(), true);
}
}