# searchpro/1.9.4/api/get_media_ids.php

BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS &amp; JavaScript, version 1.9.4. 36 lines.

- Page: https://pluginprobe.com/plugins/searchpro/1.9.4/code/api/get_media_ids.php
- Raw: https://pluginprobe.com/plugins/searchpro/1.9.4/raw/api/get_media_ids.php
- Modified: 2024-08-21T14:56:30+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/searchpro/1.9.4/code/api/get_media_ids.php#L10-L20`.

```php
<?php
if (!defined('ABSPATH')) exit;

$args = array(
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'post_status' => 'inherit',
    'posts_per_page' => -1,
);

$attachments = get_posts($args);
$ids = [
    'optimized' => [],
    'unoptimized' => [],
];

foreach ($attachments as $attachment) {
    $file = get_attached_file($attachment->ID);
    $file_info = pathinfo($file);

    // Check if the uploaded image is in a supported format
    $supported_formats = array('jpg', 'jpeg', 'png');
    if (in_array(strtolower($file_info['extension']), $supported_formats)) {
        $webp_file_original = $file_info['dirname'] . '/' . $file_info['filename'] . '.webp';

        if (file_exists($webp_file_original)) {
            $ids['optimized'][] = $attachment->ID;
        } else {
            $ids['unoptimized'][] = $attachment->ID;
        }
    }
}

echo json_encode($ids);

unset($attachments, $ids);
```
