function ewww_image_optimizer_import_loop()
{
    // verify that an authorized user has started the optimizer
    if (!wp_verify_nonce($_REQUEST['_wpnonce'], 'ewww-image-optimizer-bulk')) {
        wp_die(__('Cheatin’ eh?', EWWW_IMAGE_OPTIMIZER_DOMAIN));
    }
    global $wpdb;
    global $ewww_debug;
    //	echo "starting usage: " . memory_get_usage() . "<br>";
    // retrieve the time when the optimizer starts
    //	$started = microtime(true);
    $import_finished = false;
    $import_status = get_option('ewww_image_optimizer_import_status');
    $attachments = $wpdb->get_results("SELECT posts.ID,metas.meta_value FROM {$wpdb->postmeta} metas INNER JOIN {$wpdb->posts} posts ON posts.ID = metas.post_id WHERE posts.post_mime_type LIKE '%image%' AND metas.meta_key = '_wp_attachment_metadata' AND metas.meta_value LIKE '%ewww_image_optimizer%' LIMIT {$import_status['media']}, 100", ARRAY_N);
    //	echo "after loading metadata: " . memory_get_usage() . "<br>";
    if (count($attachments) === 0) {
        $import_finished = true;
    } else {
        $import_status['media'] += count($attachments);
    }
    $already_optimized = array();
    //	echo "after loading already optimized: " . memory_get_usage() . "<br>";
    $ewww_debug .= "importing " . count($attachments) . " attachments<br>";
    $insert_query = "INSERT INTO {$wpdb->ewwwio_images} (path, image_size, orig_size, results, temp) VALUES ";
    $rows = array();
    foreach ($attachments as $attachment) {
        $record = array();
        $gallery_type = 0;
        $id = $attachment[0];
        $meta = unserialize($attachment[1]);
        if (empty($attachment) || empty($attachment[1])) {
            continue;
        }
        list($attachment, $upload_path) = ewww_image_optimizer_attachment_path($meta, $id);
        if ('ims_image' == get_post_type($id)) {
            $gallery_type = 6;
        }
        // make sure the meta actually contains data for ewww_image_optimizer
        if (empty($meta['ewww_image_optimizer'])) {
            $prev_results = '';
        } else {
            $prev_results = $meta['ewww_image_optimizer'];
        }
        $record = ewww_image_optimizer_import_file($attachment, $prev_results, $already_optimized);
        if (!empty($record)) {
            $rows[] = "('{$record['0']}', '{$record['1']}', '{$record['2']}', '{$record['3']}', true)";
        }
        // resized versions, so we can continue
        if (isset($meta['sizes'])) {
            $record = array();
            $ewww_debug .= "processing resizes<br>";
            // meta sizes don't contain a path, so we calculate one
            if ($gallery_type === 6) {
                $base_dir = dirname($attachment) . '/_resized/';
            } else {
                $base_dir = dirname($attachment) . '/';
            }
            foreach ($meta['sizes'] as $size => $data) {
                $resize_path = $base_dir . $data['file'];
                $ewww_debug .= "current resize: {$resize_path}<br>";
                // make sure the meta actually contains data for ewww_image_optimizer
                if (empty($data['ewww_image_optimizer'])) {
                    $prev_results = '';
                } else {
                    $prev_results = $data['ewww_image_optimizer'];
                }
                $record = ewww_image_optimizer_import_file($resize_path, $prev_results, $already_optimized);
                if (!empty($record)) {
                    $rows[] = "('{$record['0']}', '{$record['1']}', '{$record['2']}', '{$record['3']}', true)";
                }
            }
        }
        ewww_image_optimizer_debug_log();
    }
    $import_count = $import_status['media'];
    //nextgen import
    if ($import_finished && isset($import_status['nextgen'])) {
        $import_finished = false;
        $images = $wpdb->get_results("SELECT pid,meta_data,filename,galleryid FROM {$wpdb->nggpictures} WHERE meta_data LIKE '%ewww_image_optimizer%' LIMIT {$import_status['nextgen']}, 100", ARRAY_N);
        if (count($images) === 0) {
            $import_finished = true;
        } else {
            $import_status['nextgen'] += count($images);
        }
        $galleries = $wpdb->get_results("SELECT gid,path FROM {$wpdb->nggallery}", ARRAY_N);
        // creating the 'registry' object for working with nextgen
        $registry = C_Component_Registry::get_instance();
        // creating a database storage object from the 'registry' object
        $storage = $registry->get_utility('I_Gallery_Storage');
        $sizes = $storage->get_image_sizes();
        foreach ($images as $image) {
            $record = array();
            $gallery_path = '';
            foreach ($galleries as $gallery) {
                if ($gallery[0] == $image[3]) {
                    $gallery_path = trailingslashit($gallery[1]);
                }
            }
            $meta = unserialize($image[1]);
            // get an array of sizes available for the $image
            foreach ($sizes as $size) {
                // get the absolute path
                if ($size === 'full' || $size === 'original' || $size === 'image') {
                    if (!empty($meta['ewww_image_optimizer'])) {
                        $file_path = ABSPATH . $gallery_path . $image[2];
                        $ewww_debug .= "nextgen path generated: {$file_path}<br>";
                        $record = ewww_image_optimizer_import_file($file_path, $meta['ewww_image_optimizer'], $already_optimized);
                    }
                } elseif (!empty($meta[$size]['ewww_image_optimizer'])) {
                    if (isset($meta[$size]['filename'])) {
                        $file_path = ABSPATH . $gallery_path . trailingslashit('thumbs') . $meta[$size]['filename'];
                    } else {
                        $file_path = ABSPATH . $gallery_path . trailingslashit('thumbs') . 'thumbs_' . $image[2];
                    }
                    $ewww_debug .= "nextgen path generated: {$file_path}<br>";
                    $record = ewww_image_optimizer_import_file($file_path, $meta[$size]['ewww_image_optimizer'], $already_optimized);
                } elseif (!empty($meta['ewww_image_optimizer'])) {
                    if (isset($meta[$size]['filename'])) {
                        $file_path = ABSPATH . $gallery_path . trailingslashit('thumbs') . $meta[$size]['filename'];
                    } else {
                        $file_path = ABSPATH . $gallery_path . trailingslashit('thumbs') . 'thumbs_' . $image[2];
                    }
                    $ewww_debug .= "nextgen path generated: {$file_path}<br>";
                    $meta[$size]['ewww_image_optimizer'] = __('Unknown Savings', EWWW_IMAGE_OPTIMIZER_DOMAIN);
                    nggdb::update_image_meta($image[0], $meta);
                    $record = ewww_image_optimizer_import_file($file_path, $meta[$size]['ewww_image_optimizer'], $already_optimized);
                }
                if (!empty($record)) {
                    $rows[] = "('{$record['0']}', '{$record['1']}', '{$record['2']}', '{$record['3']}', true)";
                }
            }
            ewww_image_optimizer_debug_log();
        }
        $import_count += $import_status['nextgen'];
    }
    // fla gallery import
    if ($import_finished && isset($import_status['flag'])) {
        $import_finished = false;
        $images = $wpdb->get_results("SELECT pid,meta_data,filename,galleryid FROM {$wpdb->flagpictures} WHERE meta_data LIKE '%ewww_image_optimizer%' LIMIT {$import_status['flag']}, 100", ARRAY_N);
        $galleries = $wpdb->get_results("SELECT gid,path FROM {$wpdb->flaggallery}", ARRAY_N);
        if (count($images) === 0) {
            $import_finished = true;
        } else {
            $import_status['flag'] += count($images);
        }
        // need this file to work with flag meta
        foreach ($images as $image) {
            $record = array();
            $gallery_path = '';
            foreach ($galleries as $gallery) {
                if ($gallery[0] == $image[3]) {
                    $gallery_path = trailingslashit($gallery[1]);
                }
            }
            // get the image meta for the current ID
            $meta = unserialize($image[1]);
            $file_path = ABSPATH . $gallery_path . $image[2];
            $ewww_debug .= "flagallery path generated: {$file_path}<br>";
            if (!empty($meta['ewww_image_optimizer'])) {
                $record = ewww_image_optimizer_import_file($file_path, $meta['ewww_image_optimizer']);
                if (!empty($record)) {
                    $rows[] = "('{$record['0']}', '{$record['1']}', '{$record['2']}', '{$record['3']}', true)";
                }
                $thumb_path = ABSPATH . $gallery_path . 'thumbs/thumbs_' . $image[2];
                if (empty($meta['thumbnail']['ewww_image_optimizer'])) {
                    $meta['thumbnail']['ewww_image_optimizer'] = __('Unknown Savings', EWWW_IMAGE_OPTIMIZER_DOMAIN);
                    // update the image metadata in the db
                    flagdb::update_image_meta($id, $meta);
                    $record = ewww_image_optimizer_import_file($thumb_path, __('Unknown Savings', EWWW_IMAGE_OPTIMIZER_DOMAIN));
                } else {
                    $record = ewww_image_optimizer_import_file($thumb_path, $meta['thumbnail']['ewww_image_optimizer']);
                }
                if (!empty($record)) {
                    $rows[] = "('{$record['0']}', '{$record['1']}', '{$record['2']}', '{$record['3']}', true)";
                }
            }
            ewww_image_optimizer_debug_log();
        }
        $import_count += $import_status['flag'];
    }
    if (!empty($rows)) {
        $wpdb->query($insert_query . implode(', ', $rows));
        $rows = array();
    }
    //	echo "after we are done: " . memory_get_usage() . "<br>";
    if ($import_finished) {
        update_option('ewww_image_optimizer_imported', true);
        update_option('ewww_image_optimizer_import_status', '');
        $wpdb->query("ALTER TABLE {$wpdb->ewwwio_images} DROP temp");
        echo "<b>" . __('Finished importing', EWWW_IMAGE_OPTIMIZER_DOMAIN) . "</b>";
    } else {
        update_option('ewww_image_optimizer_import_status', $import_status);
        echo $import_count;
    }
    //	$elapsed = microtime(true) - $started;
    //	echo "<br>importing images took $elapsed seconds<br>";
    die;
}
Esempio n. 2
0
/**
 * Print column data for optimizer results in the media library using
 * the `manage_media_custom_column` hook.
 */
function ewww_image_optimizer_custom_column($column_name, $id)
{
    global $ewww_debug;
    $ewww_debug .= "<b>ewww_image_optimizer_custom_column()</b><br>";
    // once we get to the EWWW IO custom column
    if ($column_name == 'ewww-image-optimizer') {
        // retrieve the metadata
        $meta = wp_get_attachment_metadata($id);
        if (ewww_image_optimizer_get_option('ewww_image_optimizer_debug')) {
            $print_meta = print_r($meta, TRUE);
            $print_meta = preg_replace(array('/ /', '/\\n+/'), array('&nbsp;', '<br />'), $print_meta);
            echo '<div style="background-color:#ffff99;font-size: 10px;padding: 10px;margin:-10px -10px 10px;line-height: 1.1em">' . $print_meta . '</div>';
        }
        if (!empty($meta['cloudinary'])) {
            _e('Cloudinary image', EWWW_IMAGE_OPTIMIZER_DOMAIN);
            return;
        }
        // if the filepath isn't set in the metadata
        if (empty($meta['file'])) {
            if (isset($meta['file'])) {
                unset($meta['file']);
                if (strpos($meta['ewww_image_optimizer'], 'Could not find') === 0) {
                    unset($meta['ewww_image_optimizer']);
                }
                wp_update_attachment_metadata($id, $meta);
            }
        }
        list($file_path, $upload_path) = ewww_image_optimizer_attachment_path($meta, $id);
        // if the file does not exist
        if (empty($file_path)) {
            _e('Could not retrieve file path.', EWWW_IMAGE_OPTIMIZER_DOMAIN);
            return;
        }
        $msg = '';
        $convert_desc = '';
        $convert_link = '';
        // retrieve the mimetype of the attachment
        $type = ewww_image_optimizer_mimetype($file_path, 'i');
        // get a human readable filesize
        $file_size = size_format(filesize($file_path), 2);
        $file_size = preg_replace('/\\.00 B /', ' B', $file_size);
        // run the appropriate code based on the mimetype
        switch ($type) {
            case 'image/jpeg':
                // if jpegtran is missing, tell them that
                if (!EWWW_IMAGE_OPTIMIZER_JPEGTRAN && !ewww_image_optimizer_get_option('ewww_image_optimizer_cloud_jpg')) {
                    $valid = false;
                    $msg = '<br>' . sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>jpegtran</em>');
                } else {
                    $convert_link = __('JPG to PNG', EWWW_IMAGE_OPTIMIZER_DOMAIN);
                    $class_type = 'jpg';
                    $convert_desc = __('WARNING: Removes metadata. Requires GD or ImageMagick. PNG is generally much better than JPG for logos and other images with a limited range of colors.', EWWW_IMAGE_OPTIMIZER_DOMAIN);
                }
                break;
            case 'image/png':
                // if pngout and optipng are missing, tell the user
                if (!EWWW_IMAGE_OPTIMIZER_PNGOUT && !EWWW_IMAGE_OPTIMIZER_OPTIPNG && !ewww_image_optimizer_get_option('ewww_image_optimizer_cloud_png')) {
                    $valid = false;
                    $msg = '<br>' . sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>optipng/pngout</em>');
                } else {
                    $convert_link = __('PNG to JPG', EWWW_IMAGE_OPTIMIZER_DOMAIN);
                    $class_type = 'png';
                    $convert_desc = __('WARNING: This is not a lossless conversion and requires GD or ImageMagick. JPG is much better than PNG for photographic use because it compresses the image and discards data. Transparent images will only be converted if a background color has been set.', EWWW_IMAGE_OPTIMIZER_DOMAIN);
                }
                break;
            case 'image/gif':
                // if gifsicle is missing, tell the user
                if (!EWWW_IMAGE_OPTIMIZER_GIFSICLE && !ewww_image_optimizer_get_option('ewww_image_optimizer_cloud_gif')) {
                    $valid = false;
                    $msg = '<br>' . sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>gifsicle</em>');
                } else {
                    $convert_link = __('GIF to PNG', EWWW_IMAGE_OPTIMIZER_DOMAIN);
                    $class_type = 'gif';
                    $convert_desc = __('PNG is generally better than GIF, but does not support animation. Animated images will not be converted.', EWWW_IMAGE_OPTIMIZER_DOMAIN);
                }
                break;
            default:
                // not a supported mimetype
                _e('Unsupported file type', EWWW_IMAGE_OPTIMIZER_DOMAIN);
                return;
        }
        // if the optimizer metadata exists
        if (!empty($meta['ewww_image_optimizer'])) {
            // output the optimizer results
            echo $meta['ewww_image_optimizer'];
            // output the filesize
            echo "<br>" . sprintf(__('Image Size: %s', EWWW_IMAGE_OPTIMIZER_DOMAIN), $file_size);
            if (empty($msg) && current_user_can(apply_filters('ewww_image_optimizer_manual_permissions', ''))) {
                // output a link to re-optimize manually
                printf("<br><a href=\"admin.php?action=ewww_image_optimizer_manual_optimize&amp;ewww_force=1&amp;ewww_attachment_ID=%d\">%s</a>", $id, __('Re-optimize', EWWW_IMAGE_OPTIMIZER_DOMAIN));
                if (!ewww_image_optimizer_get_option('ewww_image_optimizer_disable_convert_links') && 'ims_image' != get_post_type($id)) {
                    echo " | <a class='ewww-convert' title='{$convert_desc}' href='admin.php?action=ewww_image_optimizer_manual_optimize&amp;ewww_attachment_ID={$id}&amp;ewww_convert=1&amp;ewww_force=1'>{$convert_link}</a>";
                }
            } else {
                echo $msg;
            }
            $restorable = false;
            if (!empty($meta['converted'])) {
                if (!empty($meta['orig_file']) && file_exists($meta['orig_file'])) {
                    $restorable = true;
                }
            }
            if (isset($meta['sizes'])) {
                // meta sizes don't contain a path, so we calculate one
                $base_dir = dirname($file_path) . '/';
                foreach ($meta['sizes'] as $size => $data) {
                    if (!empty($data['converted'])) {
                        if (!empty($data['orig_file']) && file_exists($base_dir . $data['orig_file'])) {
                            $restorable = true;
                        }
                    }
                }
            }
            if ($restorable && current_user_can(apply_filters('ewww_image_optimizer_manual_permissions', ''))) {
                printf("<br><a href=\"admin.php?action=ewww_image_optimizer_manual_restore&amp;ewww_attachment_ID=%d\">%s</a>", $id, __('Restore original', EWWW_IMAGE_OPTIMIZER_DOMAIN));
            }
            // link to webp upgrade script
            $oldwebpfile = preg_replace('/\\.\\w+$/', '.webp', $file_path);
            if (file_exists($oldwebpfile) && current_user_can(apply_filters('ewww_image_optimizer_admin_permissions', ''))) {
                echo "<br><a href='options.php?page=ewww-image-optimizer-webp-migrate'>Run WebP upgrade</a>";
            }
            // determine filepath for webp
            $webpfile = $file_path . '.webp';
            if (file_exists($webpfile)) {
                $webpurl = wp_get_attachment_url($id) . '.webp';
                // get a human readable filesize
                $webp_size = size_format(filesize($webpfile), 2);
                $webp_size = preg_replace('/\\.00 B /', ' B', $webp_size);
                echo "<br>WebP: <a href='{$webpurl}'>{$webp_size}</a>";
            }
        } else {
            // otherwise, this must be an image we haven't processed
            _e('Not processed', EWWW_IMAGE_OPTIMIZER_DOMAIN);
            // tell them the filesize
            echo "<br>" . sprintf(__('Image Size: %s', EWWW_IMAGE_OPTIMIZER_DOMAIN), $file_size);
            if (empty($msg) && current_user_can(apply_filters('ewww_image_optimizer_manual_permissions', ''))) {
                // and give the user the option to optimize the image right now
                printf("<br><a href=\"admin.php?action=ewww_image_optimizer_manual_optimize&amp;ewww_attachment_ID=%d\">%s</a>", $id, __('Optimize now!', EWWW_IMAGE_OPTIMIZER_DOMAIN));
                if (!ewww_image_optimizer_get_option('ewww_image_optimizer_disable_convert_links') && 'ims_image' != get_post_type($id)) {
                    echo " | <a class='ewww-convert' title='{$convert_desc}' href='admin.php?action=ewww_image_optimizer_manual_optimize&amp;ewww_attachment_ID={$id}&amp;ewww_convert=1&amp;ewww_force=1'>{$convert_link}</a>";
                }
            } else {
                echo $msg;
            }
        }
    }
    ewwwio_memory(__FUNCTION__);
}
Esempio n. 3
0
/**
 * Print column data for optimizer results in the media library using
 * the `manage_media_custom_column` hook.
 */
function ewww_image_optimizer_custom_column($column_name, $id)
{
    ewwwio_debug_message('<b>' . __FUNCTION__ . '()</b>');
    // once we get to the EWWW IO custom column
    if ($column_name == 'ewww-image-optimizer') {
        // retrieve the metadata
        $meta = wp_get_attachment_metadata($id);
        if (ewww_image_optimizer_get_option('ewww_image_optimizer_debug')) {
            $print_meta = print_r($meta, TRUE);
            $print_meta = preg_replace(array('/ /', '/\\n+/'), array('&nbsp;', '<br />'), $print_meta);
            echo '<div style="background-color:#ffff99;font-size: 10px;padding: 10px;margin:-10px -10px 10px;line-height: 1.1em">' . $print_meta . '</div>';
        }
        // TODO: make sure meta is an array
        $ewww_cdn = false;
        if (!empty($meta['cloudinary'])) {
            esc_html_e('Cloudinary image', EWWW_IMAGE_OPTIMIZER_DOMAIN);
            return;
        }
        if (class_exists('WindowsAzureStorageUtil') && !empty($meta['url'])) {
            esc_html_e('Azure Storage image', EWWW_IMAGE_OPTIMIZER_DOMAIN);
            $ewww_cdn = true;
        }
        if (class_exists('Amazon_S3_And_CloudFront') && preg_match('/^(http|s3)\\w*:/', get_attached_file($id))) {
            esc_html_e('Amazon S3 image', EWWW_IMAGE_OPTIMIZER_DOMAIN);
            $ewww_cdn = true;
        }
        list($file_path, $upload_path) = ewww_image_optimizer_attachment_path($meta, $id);
        // if the file does not exist
        if (empty($file_path) && !$ewww_cdn) {
            esc_html_e('Could not retrieve file path.', EWWW_IMAGE_OPTIMIZER_DOMAIN);
            ewww_image_optimizer_debug_log();
            return;
        }
        $msg = '';
        $convert_desc = '';
        $convert_link = '';
        if ($ewww_cdn) {
            $type = get_post_mime_type($id);
        } else {
            // retrieve the mimetype of the attachment
            $type = ewww_image_optimizer_mimetype($file_path, 'i');
            // get a human readable filesize
            $file_size = size_format(filesize($file_path), 2);
            $file_size = preg_replace('/\\.00 B /', ' B', $file_size);
        }
        $skip = ewww_image_optimizer_skip_tools();
        // run the appropriate code based on the mimetype
        switch ($type) {
            case 'image/jpeg':
                // if jpegtran is missing, tell them that
                if (!EWWW_IMAGE_OPTIMIZER_JPEGTRAN && !$skip['jpegtran']) {
                    $valid = false;
                    $msg = '<br>' . wp_kses(sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>jpegtran</em>'), array('em' => array()));
                } else {
                    $convert_link = esc_html__('JPG to PNG', EWWW_IMAGE_OPTIMIZER_DOMAIN);
                    $convert_desc = esc_attr__('WARNING: Removes metadata. Requires GD or ImageMagick. PNG is generally much better than JPG for logos and other images with a limited range of colors.', EWWW_IMAGE_OPTIMIZER_DOMAIN);
                }
                break;
            case 'image/png':
                // if pngout and optipng are missing, tell the user
                if (!EWWW_IMAGE_OPTIMIZER_PNGOUT && !EWWW_IMAGE_OPTIMIZER_OPTIPNG && !$skip['optipng'] && !$skip['pngout']) {
                    $valid = false;
                    $msg = '<br>' . wp_kses(sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>optipng/pngout</em>'), array('em' => array()));
                } else {
                    $convert_link = esc_html__('PNG to JPG', EWWW_IMAGE_OPTIMIZER_DOMAIN);
                    $convert_desc = esc_attr__('WARNING: This is not a lossless conversion and requires GD or ImageMagick. JPG is much better than PNG for photographic use because it compresses the image and discards data. Transparent images will only be converted if a background color has been set.', EWWW_IMAGE_OPTIMIZER_DOMAIN);
                }
                break;
            case 'image/gif':
                // if gifsicle is missing, tell the user
                if (!EWWW_IMAGE_OPTIMIZER_GIFSICLE && !$skip['gifsicle']) {
                    $valid = false;
                    $msg = '<br>' . wp_kses(sprintf(__('%s is missing', EWWW_IMAGE_OPTIMIZER_DOMAIN), '<em>gifsicle</em>'), array('em' => array()));
                } else {
                    $convert_link = esc_html__('GIF to PNG', EWWW_IMAGE_OPTIMIZER_DOMAIN);
                    $convert_desc = esc_attr__('PNG is generally better than GIF, but does not support animation. Animated images will not be converted.', EWWW_IMAGE_OPTIMIZER_DOMAIN);
                }
                break;
            case 'application/pdf':
                $convert_desc = '';
                break;
            default:
                // not a supported mimetype
                esc_html_e('Unsupported file type', EWWW_IMAGE_OPTIMIZER_DOMAIN);
                ewww_image_optimizer_debug_log();
                return;
        }
        $ewww_manual_nonce = wp_create_nonce("ewww-manual-{$id}");
        if ($ewww_cdn) {
            // if the optimizer metadata exists
            if (!empty($meta['ewww_image_optimizer'])) {
                // output the optimizer results
                echo "<br>" . esc_html($meta['ewww_image_optimizer']);
                if (current_user_can(apply_filters('ewww_image_optimizer_manual_permissions', ''))) {
                    // output a link to re-optimize manually
                    printf("<br><a href=\"admin.php?action=ewww_image_optimizer_manual_optimize&amp;ewww_manual_nonce={$ewww_manual_nonce}&amp;ewww_force=1&amp;ewww_attachment_ID=%d\">%s</a>", $id, esc_html__('Re-optimize', EWWW_IMAGE_OPTIMIZER_DOMAIN));
                }
            } elseif (get_transient('ewwwio-background-in-progress-' . $id)) {
                esc_html_e('In Progress', EWWW_IMAGE_OPTIMIZER_DOMAIN);
            } elseif (current_user_can(apply_filters('ewww_image_optimizer_manual_permissions', ''))) {
                // and give the user the option to optimize the image right now
                printf("<br><a href=\"admin.php?action=ewww_image_optimizer_manual_optimize&amp;ewww_manual_nonce={$ewww_manual_nonce}&amp;ewww_attachment_ID=%d\">%s</a>", $id, esc_html__('Optimize now!', EWWW_IMAGE_OPTIMIZER_DOMAIN));
            }
            return;
        }
        // if the optimizer metadata exists
        if (!empty($meta['ewww_image_optimizer'])) {
            // output the optimizer results
            echo esc_html($meta['ewww_image_optimizer']);
            // output the filesize
            echo "<br>" . sprintf(esc_html__('Image Size: %s', EWWW_IMAGE_OPTIMIZER_DOMAIN), $file_size);
            if (empty($msg) && current_user_can(apply_filters('ewww_image_optimizer_manual_permissions', ''))) {
                // output a link to re-optimize manually
                printf("<br><a href=\"admin.php?action=ewww_image_optimizer_manual_optimize&amp;ewww_manual_nonce={$ewww_manual_nonce}&amp;ewww_force=1&amp;ewww_attachment_ID=%d\">%s</a>", $id, esc_html__('Re-optimize', EWWW_IMAGE_OPTIMIZER_DOMAIN));
                if (!ewww_image_optimizer_get_option('ewww_image_optimizer_disable_convert_links') && 'ims_image' != get_post_type($id) && !empty($convert_desc)) {
                    echo " | <a class='ewww-convert' title='{$convert_desc}' href='admin.php?action=ewww_image_optimizer_manual_optimize&amp;ewww_manual_nonce={$ewww_manual_nonce}&amp;ewww_attachment_ID={$id}&amp;ewww_convert=1&amp;ewww_force=1'>{$convert_link}</a>";
                }
            } else {
                echo $msg;
            }
            $restorable = false;
            if (!empty($meta['converted'])) {
                if (!empty($meta['orig_file']) && file_exists($meta['orig_file'])) {
                    $restorable = true;
                }
            }
            if (isset($meta['sizes'])) {
                // meta sizes don't contain a path, so we calculate one
                $base_dir = trailingslashit(dirname($file_path));
                foreach ($meta['sizes'] as $size => $data) {
                    if (!empty($data['converted'])) {
                        if (!empty($data['orig_file']) && file_exists($base_dir . $data['orig_file'])) {
                            $restorable = true;
                        }
                    }
                }
            }
            if ($restorable && current_user_can(apply_filters('ewww_image_optimizer_manual_permissions', ''))) {
                printf("<br><a href=\"admin.php?action=ewww_image_optimizer_manual_restore&amp;ewww_manual_nonce={$ewww_manual_nonce}&amp;ewww_attachment_ID=%d\">%s</a>", $id, esc_html__('Restore original', EWWW_IMAGE_OPTIMIZER_DOMAIN));
            }
            // link to webp upgrade script
            $oldwebpfile = preg_replace('/\\.\\w+$/', '.webp', $file_path);
            if (file_exists($oldwebpfile) && current_user_can(apply_filters('ewww_image_optimizer_admin_permissions', ''))) {
                echo "<br><a href='options.php?page=ewww-image-optimizer-webp-migrate'>" . esc_html__('Run WebP upgrade', EWWW_IMAGE_OPTIMIZER_DOMAIN) . "</a>";
            }
            // determine filepath for webp
            $webpfile = $file_path . '.webp';
            $webp_size = ewww_image_optimizer_filesize($webpfile);
            if ($webp_size) {
                $webp_size = size_format($webp_size, 2);
                $webpurl = esc_url(wp_get_attachment_url($id) . '.webp');
                // get a human readable filesize
                $webp_size = preg_replace('/\\.00 B /', ' B', $webp_size);
                echo "<br>WebP: <a href='{$webpurl}'>{$webp_size}</a>";
            }
        } elseif (get_transient('ewwwio-background-in-progress-' . $id)) {
            esc_html_e('In Progress', EWWW_IMAGE_OPTIMIZER_DOMAIN);
        } else {
            // otherwise, this must be an image we haven't processed
            esc_html_e('Not processed', EWWW_IMAGE_OPTIMIZER_DOMAIN);
            // tell them the filesize
            echo "<br>" . sprintf(esc_html__('Image Size: %s', EWWW_IMAGE_OPTIMIZER_DOMAIN), $file_size);
            if (empty($msg) && current_user_can(apply_filters('ewww_image_optimizer_manual_permissions', ''))) {
                // and give the user the option to optimize the image right now
                printf("<br><a href=\"admin.php?action=ewww_image_optimizer_manual_optimize&amp;ewww_manual_nonce={$ewww_manual_nonce}&amp;ewww_attachment_ID=%d\">%s</a>", $id, esc_html__('Optimize now!', EWWW_IMAGE_OPTIMIZER_DOMAIN));
                if (!ewww_image_optimizer_get_option('ewww_image_optimizer_disable_convert_links') && 'ims_image' != get_post_type($id) && !empty($convert_desc)) {
                    echo " | <a class='ewww-convert' title='{$convert_desc}' href='admin.php?action=ewww_image_optimizer_manual_optimize&amp;ewww_manual_nonce={$ewww_manual_nonce}&amp;ewww_attachment_ID={$id}&amp;ewww_convert=1&amp;ewww_force=1'>{$convert_link}</a>";
                }
            } else {
                echo $msg;
            }
        }
    }
    ewwwio_memory(__FUNCTION__);
}