/**
 * 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 = "{$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 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);
            }
            echo 'Metadata is missing file path.';
            //print __('Unsupported file type', EWWW_IMAGE_OPTIMIZER_DOMAIN) . $msg;
            return;
        }
        // retrieve the filepath from the metadata
        $file_path = $meta['file'];
        // retrieve the wordpress upload folder
        $upload_dir = wp_upload_dir();
        // retrieve the wordpress upload folder path
        $upload_path = trailingslashit($upload_dir['basedir']);
        // if the path given is not the absolute path
        if (FALSE === file_exists($file_path)) {
            // find the absolute path
            $file_path = $upload_path . $file_path;
        }
        $msg = '';
        $type = ewww_image_optimizer_mimetype($file_path, 'i');
        // get a human readable filesize
        $file_size = ewww_image_optimizer_format_bytes(filesize($file_path));
        // initialize $valid
        $valid = true;
        // 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 == false) {
                    $valid = false;
                    $msg = '<br>' . __('<em>jpegtran</em> is missing');
                } else {
                    $convert_link = 'JPG to PNG';
                    $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.";
                }
                break;
            case 'image/png':
                // if pngout and optipng are missing, tell the user
                if (EWWW_IMAGE_OPTIMIZER_PNGOUT == false && EWWW_IMAGE_OPTIMIZER_OPTIPNG == false) {
                    $valid = false;
                    $msg = '<br>' . __('<em>optipng/pngout</em> is missing');
                } else {
                    $convert_link = 'PNG to JPG';
                    $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.";
                }
                break;
            case 'image/gif':
                // if gifsicle is missing, tell the user
                if (EWWW_IMAGE_OPTIMIZER_GIFSICLE == false) {
                    $valid = false;
                    $msg = '<br>' . __('<em>gifsicle</em> is missing');
                } else {
                    $convert_link = 'GIF to PNG';
                    $class_type = 'gif';
                    $convert_desc = "PNG is generally better than GIF, but does not support animation. Animated images will not be converted.";
                }
                break;
            default:
                // not a supported mimetype
                $valid = false;
        }
        // if this is an unsupported filetype, tell the user we don't work with strangers
        if ($valid == false) {
            print __('Unsupported file type', EWWW_IMAGE_OPTIMIZER_DOMAIN) . $msg;
            return;
        }
        // if the optimizer metadata exists
        if (isset($meta['ewww_image_optimizer']) && !empty($meta['ewww_image_optimizer'])) {
            // output the optimizer results
            print $meta['ewww_image_optimizer'];
            // output the filesize
            print "<br>Image Size: {$file_size}";
            // output a link to re-optimize manually
            printf("<br><a href=\"admin.php?action=ewww_image_optimizer_manual&amp;attachment_ID=%d\">%s</a>", $id, __('Re-optimize', EWWW_IMAGE_OPTIMIZER_DOMAIN));
            if (!get_site_option('ewww_image_optimizer_disable_convert_links')) {
                echo " | <a class='ewww-convert' title='{$convert_desc}' href='admin.php?action=ewww_image_optimizer_manual&amp;attachment_ID={$id}&amp;convert=1'>{$convert_link}</a>";
            }
            $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) {
                printf("<br><a href=\"admin.php?action=ewww_image_optimizer_restore&amp;attachment_ID=%d\">%s</a>", $id, __('Restore original', EWWW_IMAGE_OPTIMIZER_DOMAIN));
            }
        } else {
            // otherwise, this must be an image we haven't processed
            print __('Not processed', EWWW_IMAGE_OPTIMIZER_DOMAIN);
            // tell them the filesize
            print "<br>Image Size: {$file_size}";
            // and give the user the option to optimize the image right now
            printf("<br><a href=\"admin.php?action=ewww_image_optimizer_manual&amp;attachment_ID=%d\">%s</a>", $id, __('Optimize now!', EWWW_IMAGE_OPTIMIZER_DOMAIN));
            if (!get_site_option('ewww_image_optimizer_disable_convert_links')) {
                echo " | <a class='ewww-convert' title='{$convert_desc}' href='admin.php?action=ewww_image_optimizer_manual&amp;attachment_ID={$id}&amp;convert=1'>{$convert_link}</a>";
            }
        }
    }
}
 function ewww_manage_image_custom_column($column_name, $id)
 {
     // once we've found our custom column
     if ($column_name == 'ewww_image_optimizer') {
         // get the metadata for the image
         $meta = new nggMeta($id);
         // get the optimization status for the image
         $status = $meta->get_META('ewww_image_optimizer');
         $msg = '';
         // get the file path of the image
         $file_path = $meta->image->imagePath;
         // get the mimetype of the image
         $type = ewww_image_optimizer_mimetype($file_path, 'i');
         // retrieve the human-readable filesize of the image
         $file_size = ewww_image_optimizer_format_bytes(filesize($file_path));
         $valid = true;
         // check to see if we have a tool to handle the mimetype detected
         switch ($type) {
             case 'image/jpeg':
                 // if jpegtran is missing, tell the user
                 if (EWWW_IMAGE_OPTIMIZER_JPEGTRAN == false) {
                     $valid = false;
                     $msg = '<br>' . __('<em>jpegtran</em> is missing');
                 }
                 break;
             case 'image/png':
                 // if the PNG tools are missing, tell the user
                 if (EWWW_IMAGE_OPTIMIZER_PNGOUT == false && EWWW_IMAGE_OPTIMIZER_OPTIPNG == false) {
                     $valid = false;
                     $msg = '<br>' . __('<em>optipng/pngout</em> is missing');
                 }
                 break;
             case 'image/gif':
                 // if gifsicle is missing, tell the user
                 if (EWWW_IMAGE_OPTIMIZER_GIFSICLE == false) {
                     $valid = false;
                     $msg = '<br>' . __('<em>gifsicle</em> is missing');
                 }
                 break;
             default:
                 $valid = false;
         }
         // file isn't in a format we can work with, we don't work with strangers
         if ($valid == false) {
             print __('Unsupported file type', EWWW_IMAGE_OPTIMIZER_DOMAIN) . $msg;
             return;
         }
         // if we have a valid status, display it, the image size, and give a re-optimize link
         if ($status && !empty($status)) {
             echo $status;
             print "<br>Image Size: {$file_size}";
             printf("<br><a href=\"admin.php?action=ewww_ngg_manual&amp;attachment_ID=%d\">%s</a>", $id, __('Re-optimize', EWWW_IMAGE_OPTIMIZER_DOMAIN));
             // otherwise, give the image size, and a link to optimize right now
         } else {
             print __('Not processed', EWWW_IMAGE_OPTIMIZER_DOMAIN);
             print "<br>Image Size: {$file_size}";
             printf("<br><a href=\"admin.php?action=ewww_ngg_manual&amp;attachment_ID=%d\">%s</a>", $id, __('Optimize now!', EWWW_IMAGE_OPTIMIZER_DOMAIN));
         }
     }
 }
 function ewww_manage_image_custom_column($column_name, $id)
 {
     // check to make sure we're outputing our custom column
     if ($column_name == 'ewww_image_optimizer') {
         // get the metadata
         $meta = new flagMeta($id);
         // grab the image status from the meta
         $status = $meta->get_META('ewww_image_optimizer');
         $msg = '';
         // get the image path from the meta
         $file_path = $meta->image->imagePath;
         // get the mimetype
         $type = ewww_image_optimizer_mimetype($file_path, 'i');
         // get the file size
         $file_size = ewww_image_optimizer_format_bytes(filesize($file_path));
         $valid = true;
         // if we don't have a valid tool for the image type, output the appropriate message
         switch ($type) {
             case 'image/jpeg':
                 if (EWWW_IMAGE_OPTIMIZER_JPEGTRAN == false) {
                     $valid = false;
                     $msg = '<br>' . __('<em>jpegtran</em> is missing');
                 }
                 break;
             case 'image/png':
                 if (EWWW_IMAGE_OPTIMIZER_PNGOUT == false && EWWW_IMAGE_OPTIMIZER_OPTIPNG == false) {
                     $valid = false;
                     $msg = '<br>' . __('<em>optipng/pngout</em> is missing');
                 }
                 break;
             case 'image/gif':
                 if (EWWW_IMAGE_OPTIMIZER_GIFSICLE == false) {
                     $valid = false;
                     $msg = '<br>' . __('<em>gifsicle</em> is missing');
                 }
                 break;
             default:
                 $valid = false;
         }
         // let user know if the file type is unsupported
         if ($valid == false) {
             print __('Unsupported file type', EWWW_IMAGE_OPTIMIZER_DOMAIN) . $msg;
             return;
         }
         // output the image status if we know it
         if (!empty($status)) {
             echo $status;
             print "<br>Image Size: {$file_size}";
             printf("<br><a href=\"admin.php?action=ewww_flag_manual&amp;attachment_ID=%d\">%s</a>", $id, __('Re-optimize', EWWW_IMAGE_OPTIMIZER_DOMAIN));
             // otherwise, tell the user that they can optimize the image now
         } else {
             print __('Not processed', EWWW_IMAGE_OPTIMIZER_DOMAIN);
             print "<br>Image Size: {$file_size}";
             printf("<br><a href=\"admin.php?action=ewww_flag_manual&amp;attachment_ID=%d\">%s</a>", $id, __('Optimize now!', EWWW_IMAGE_OPTIMIZER_DOMAIN));
         }
     }
 }