-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecommerce_image_tool.php
More file actions
317 lines (272 loc) · 11.8 KB
/
ecommerce_image_tool.php
File metadata and controls
317 lines (272 loc) · 11.8 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
/**
* Plugin Name: eCommerce Image Tool
* Plugin URI: https://github.com/krysits/ecommerce-image-plugin
* Description: Batch resize and optimize images for eCommerce platforms
* Version: 0.1
* Author: Kristaps Ledins <a href="https://github.com/krysits">@krysits</a>
* License: GPLv3
* Text Domain: ecommerce-image-tool
* Domain Path: /languages
* Started: 2025-10-15
*/
if (!defined('ABSPATH')) {
exit;
}
define('ECIT_VERSION', '0.1');
define('ECIT_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('ECIT_PLUGIN_URL', plugin_dir_url(__FILE__));
class EcommerceImageTool {
private static $instance = null;
public static function get_instance() {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
function media_include_script() {
if ( ! did_action( 'wp_enqueue_media' ) ) {
wp_enqueue_media();
}
}
public function __construct() {
add_action('admin_menu', [$this, 'add_admin_menu']);
add_action('admin_enqueue_scripts', [$this, 'media_include_script']);
add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_assets']);
add_action('wp_ajax_ecit_process_images', [$this, 'ajax_process_images']);
add_action('wp_ajax_ecit_get_status', [$this, 'ajax_get_status']);
}
public function add_admin_menu() {
add_media_page(
__('eCommerce Image Tool', 'ecommerce-image-tool'),
__('Image Optimizer', 'ecommerce-image-tool'),
'manage_options',
'ecit-tool',
[$this, 'render_admin_page']
);
}
public function enqueue_admin_assets($hook) {
if ($hook !== 'media_page_ecit-tool') {
return;
}
wp_enqueue_style(
'ecit-admin',
ECIT_PLUGIN_URL . 'assets/admin.css',
[],
ECIT_VERSION
);
wp_enqueue_script(
'ecit-admin',
ECIT_PLUGIN_URL . 'assets/admin.js',
['jquery',],
ECIT_VERSION,
true
);
wp_localize_script('ecit-admin', 'ecitData', [
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('ecit_nonce'),
'strings' => [
'processing' => __('Processing...', 'ecommerce-image-tool'),
'complete' => __('Complete!', 'ecommerce-image-tool'),
'error' => __('Error occurred', 'ecommerce-image-tool'),
]
]);
}
public function render_admin_page() {
if (!current_user_can('manage_options')) {
wp_die(__('Unauthorized', 'ecommerce-image-tool'));
}
?>
<div class="wrap ecit-container">
<h1><?php _e('eCommerce Image Tool', 'ecommerce-image-tool'); ?></h1>
<div class="ecit-settings">
<h2><?php _e('Image Processing Settings', 'ecommerce-image-tool'); ?></h2>
<table class="form-table">
<tr>
<th scope="row">
<label for="image-format"><?php _e('Image Format', 'ecommerce-image-tool'); ?></label>
</th>
<td>
<select id="image-format" name="format">
<option value="jpg">JPG</option>
<option value="png">PNG</option>
<option value="webp">WEBP</option>
<option value="gif">GIF</option>
</select>
</td>
</tr>
<tr>
<th scope="row">
<label for="output-type"><?php _e('Output Type', 'ecommerce-image-tool'); ?></label>
</th>
<td>
<select id="output-type" name="output_type">
<option value="square"><?php _e('Square', 'ecommerce-image-tool'); ?></option>
<option value="landscape"><?php _e('Landscape', 'ecommerce-image-tool'); ?></option>
<option value="both"><?php _e('Both', 'ecommerce-image-tool'); ?></option>
</select>
</td>
</tr>
<tr>
<th scope="row">
<label for="target-height"><?php _e('Target Height (px)', 'ecommerce-image-tool'); ?></label>
</th>
<td>
<input type="number" id="target-height" name="height" value="600" min="100" max="2000">
</td>
</tr>
<tr>
<th scope="row">
<label for="quality"><?php _e('Quality (%)', 'ecommerce-image-tool'); ?></label>
</th>
<td>
<input type="number" id="quality" name="quality" value="85" min="1" max="100">
</td>
</tr>
</table>
</div>
<div class="ecit-actions">
<h2><?php _e('Process Images', 'ecommerce-image-tool'); ?></h2>
<p><?php _e('Select images from your media library to resize and optimize.', 'ecommerce-image-tool'); ?></p>
<button id="select-images-btn" class="button button-primary">
<?php _e('Select Images', 'ecommerce-image-tool'); ?>
</button>
<button id="process-images-btn" class="button button-secondary" disabled>
<?php _e('Start Processing', 'ecommerce-image-tool'); ?>
</button>
</div>
<div id="ecit-progress" class="ecit-progress" style="display:none;">
<h3><?php _e('Processing Progress', 'ecommerce-image-tool'); ?></h3>
<div class="progress-bar">
<div class="progress-fill"></div>
</div>
<p class="progress-text">0%</p>
<div id="ecit-log" class="ecit-log"></div>
</div>
<div id="ecit-results" class="ecit-results" style="display:none;">
<h3><?php _e('Processing Results', 'ecommerce-image-tool'); ?></h3>
<div id="results-content"></div>
<a href="<?php echo esc_url(admin_url('upload.php')); ?>" class="button button-primary">
<?php _e('View Media Library', 'ecommerce-image-tool'); ?>
</a>
</div>
</div>
<?php
}
public function ajax_process_images() {
check_ajax_referer('ecit_nonce');
if (!current_user_can('manage_options')) {
wp_send_json_error(__('Unauthorized', 'ecommerce-image-tool'));
}
$image_ids = isset($_POST['image_ids']) ? array_map('intval', $_POST['image_ids']) : [];
$format = isset($_POST['format']) ? sanitize_text_field($_POST['format']) : 'jpg';
$output_type = isset($_POST['output_type']) ? sanitize_text_field($_POST['output_type']) : 'square';
$height = isset($_POST['height']) ? intval($_POST['height']) : 600;
$quality = isset($_POST['quality']) ? intval($_POST['quality']) : 85;
$results = [];
$processed = 0;
$total = count($image_ids);
foreach ($image_ids as $image_id) {
$result = $this->process_single_image($image_id, $format, $output_type, $height, $quality);
$results[] = $result;
$processed++;
wp_send_json([
'success' => true,
'progress' => ($processed / $total) * 100,
'processed' => $processed,
'total' => $total,
'current_result' => $result
]);
}
wp_send_json(['success' => true, 'results' => $results]);
}
private function process_single_image($image_id, $format, $output_type, $height, $quality) {
$file = get_attached_file($image_id);
if (!$file || !file_exists($file)) {
return [
'id' => $image_id,
'status' => 'error',
'message' => __('File not found', 'ecommerce-image-tool')
];
}
try {
$upload_dir = wp_upload_dir();
$base_dir = $upload_dir['basedir'];
// Create output directories
$this->ensure_output_dirs($base_dir);
if (!extension_loaded('gd')) {
throw new Exception(__('GD extension not installed', 'ecommerce-image-tool'));
}
$image = wp_get_image_editor($file);
if (is_wp_error($image)) {
throw new Exception($image->get_error_message());
}
// Get original dimensions
$size = $image->get_size();
$aspect_ratio = $size['width'] / $size['height'];
// Process square format
if ($output_type === 'square' || $output_type === 'both') {
$square_size = $height;
$image->resize($square_size, $square_size, true);
$square_file = $this->get_output_path($file, 'square', $format, $height);
$image->save($square_file, $format);
$this->set_image_quality($square_file, $quality, $format);
}
// Process landscape format
if ($output_type === 'landscape' || $output_type === 'both') {
$image = wp_get_image_editor($file); // Reload original
$landscape_width = intval($height * $aspect_ratio);
$image->resize($landscape_width, $height);
$landscape_file = $this->get_output_path($file, 'landscape', $format, $height);
$image->save($landscape_file, $format);
$this->set_image_quality($landscape_file, $quality, $format);
}
return [
'id' => $image_id,
'status' => 'success',
'message' => sprintf(__('Image %d processed successfully', 'ecommerce-image-tool'), $image_id)
];
} catch (Exception $e) {
return [
'id' => $image_id,
'status' => 'error',
'message' => $e->getMessage()
];
}
}
private function ensure_output_dirs($base_dir) {
$dirs = [
$base_dir . '/ecit-square',
$base_dir . '/ecit-landscape'
];
foreach ($dirs as $dir) {
if (!is_dir($dir)) {
wp_mkdir_p($dir);
}
}
}
private function get_output_path($original_file, $type, $format, $height) {
$upload_dir = wp_upload_dir();
$base_dir = $upload_dir['basedir'];
$filename = pathinfo($original_file, PATHINFO_FILENAME);
$new_filename = $filename . '_' . $type . '_' . $height . '.' . $format;
return $base_dir . '/ecit-' . $type . '/' . $new_filename;
}
private function set_image_quality(&$file, $quality, $format) {
if ($format === 'jpg' && extension_loaded('imagick')) {
$image = new Imagick($file);
$image->setImageCompressionQuality($quality);
$image->writeImage();
$image->destroy();
}
}
public function ajax_get_status() {
check_ajax_referer('ecit_nonce');
if (!current_user_can('manage_options')) {
wp_send_json_error(__('Unauthorized', 'ecommerce-image-tool'));
}
wp_send_json(['success' => true]);
}
}
// Initialize plugin
EcommerceImageTool::get_instance();