| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')) exit; |
| 3 |
|
| 4 |
$args = array( |
| 5 |
'post_type' => 'attachment', |
| 6 |
'post_mime_type' => 'image', |
| 7 |
'post_status' => 'inherit', |
| 8 |
'posts_per_page' => -1, |
| 9 |
); |
| 10 |
|
| 11 |
$attachments = get_posts($args); |
| 12 |
$ids = [ |
| 13 |
'optimized' => [], |
| 14 |
'unoptimized' => [], |
| 15 |
]; |
| 16 |
|
| 17 |
foreach ($attachments as $attachment) { |
| 18 |
$file = get_attached_file($attachment->ID); |
| 19 |
$file_info = pathinfo($file); |
| 20 |
|
| 21 |
// Check if the uploaded image is in a supported format |
| 22 |
$supported_formats = array('jpg', 'jpeg', 'png'); |
| 23 |
if (in_array(strtolower($file_info['extension']), $supported_formats)) { |
| 24 |
$webp_file_original = $file_info['dirname'] . '/' . $file_info['filename'] . '.webp'; |
| 25 |
|
| 26 |
if (file_exists($webp_file_original)) { |
| 27 |
$ids['optimized'][] = $attachment->ID; |
| 28 |
} else { |
| 29 |
$ids['unoptimized'][] = $attachment->ID; |
| 30 |
} |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
echo json_encode($ids); |
| 35 |
|
| 36 |
unset($attachments, $ids); |