コード例 #1
0
 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 = size_format(ewww_image_optimizer_filesize($file_path), 2);
         $file_size = str_replace('B ', 'B', $file_size);
         $valid = true;
         // check to see if we have a tool to handle the mimetype detected
         //			$skip = ewww_image_optimizer_skip_tools();
         if (!defined('EWWW_IMAGE_OPTIMIZER_JPEGTRAN')) {
             ewww_image_optimizer_tool_init();
             ewww_image_optimizer_notice_utils('quiet');
         }
         switch ($type) {
             case 'image/jpeg':
                 // if jpegtran is missing, tell the user
                 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()));
                 }
                 break;
             case 'image/png':
                 // if the PNG tools 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()));
                 }
                 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()));
                 }
                 break;
             default:
                 $valid = false;
         }
         // file isn't in a format we can work with, we don't work with strangers
         if ($valid == false) {
             echo esc_html__('Unsupported file type', EWWW_IMAGE_OPTIMIZER_DOMAIN) . $msg;
             return;
         }
         $ewww_manual_nonce = wp_create_nonce("ewww-manual-" . $id);
         // if we have a valid status, display it, the image size, and give a re-optimize link
         if (!empty($status)) {
             echo esc_html($status);
             echo "<br>" . sprintf(esc_html__('Image Size: %s', EWWW_IMAGE_OPTIMIZER_DOMAIN), $file_size);
             if (current_user_can(apply_filters('ewww_image_optimizer_manual_permissions', ''))) {
                 printf("<br><a href=\"admin.php?action=ewww_ngg_manual&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-ngg-' . $id)) {
             esc_html_e('In Progress', EWWW_IMAGE_OPTIMIZER_DOMAIN);
             // otherwise, give the image size, and a link to optimize right now
         } else {
             esc_html_e('Not processed', EWWW_IMAGE_OPTIMIZER_DOMAIN);
             echo "<br>" . sprintf(esc_html__('Image Size: %s', EWWW_IMAGE_OPTIMIZER_DOMAIN), $file_size);
             if (current_user_can(apply_filters('ewww_image_optimizer_manual_permissions', ''))) {
                 printf("<br><a href=\"admin.php?action=ewww_ngg_manual&amp;ewww_manual_nonce={$ewww_manual_nonce}&amp;ewww_attachment_ID=%d\">%s</a>", $id, esc_html__('Optimize now!', EWWW_IMAGE_OPTIMIZER_DOMAIN));
             }
         }
     }
 }
コード例 #2
0
 function ewww_manage_image_custom_column($column_name, $id)
 {
     // once we've found our custom column
     if ($column_name == 'ewww_image_optimizer' || $column_name == '') {
         $output = '';
         // 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');
         if (is_object($id)) {
             $image = $id;
         } else {
             // get an image object
             $image = $storage->object->_image_mapper->find($id);
         }
         if (ewww_image_optimizer_get_option('ewww_image_optimizer_debug')) {
             $print_meta = print_r($image->meta_data, TRUE);
             $print_meta = preg_replace(array('/ /', '/\\n+/'), array('&nbsp;', '<br />'), esc_html($print_meta));
             $output .= '<div style="background-color:#ffff99;font-size: 10px;padding: 10px;margin:-10px -10px 10px;line-height: 1.1em">' . $print_meta . '</div>';
         }
         $msg = '';
         // get the absolute path
         $file_path = $storage->get_image_abspath($image, 'full');
         // get the mimetype of the image
         $type = ewww_image_optimizer_mimetype($file_path, 'i');
         // retrieve the human-readable filesize of the image
         $file_size = size_format(ewww_image_optimizer_filesize($file_path), 2);
         $file_size = str_replace('B ', 'B', $file_size);
         $valid = true;
         // check to see if we have a tool to handle the mimetype detected
         if (!defined('EWWW_IMAGE_OPTIMIZER_JPEGTRAN')) {
             ewww_image_optimizer_tool_init();
             ewww_image_optimizer_notice_utils('quiet');
         }
         //			$skip = ewww_image_optimizer_skip_tools();
         switch ($type) {
             case 'image/jpeg':
                 // if jpegtran is missing, tell the user
                 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()));
                 }
                 break;
             case 'image/png':
                 // if the PNG tools 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()));
                 }
                 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()));
                 }
                 break;
             default:
                 $valid = false;
         }
         // file isn't in a format we can work with, we don't work with strangers
         if ($valid == false) {
             echo esc_html__('Unsupported file type', EWWW_IMAGE_OPTIMIZER_DOMAIN) . $msg;
             return;
         }
         // if we have a valid status, display it
         if (!empty($image->meta_data['ewww_image_optimizer'])) {
             $output .= esc_html($image->meta_data['ewww_image_optimizer']);
         } elseif (get_transient('ewwwio-background-in-progress-ngg-' . $image->pid)) {
             $output .= esc_html('In Progress', EWWW_IMAGE_OPTIMIZER_DOMAIN);
             // otherwise, give the image size, and a link to optimize right now
         } else {
             $output .= esc_html__('Not processed', EWWW_IMAGE_OPTIMIZER_DOMAIN);
         }
         // display the image size
         $output .= "<br>" . sprintf(esc_html__('Image Size: %s', EWWW_IMAGE_OPTIMIZER_DOMAIN), $file_size) . "<br>";
         // display the optimization link with the appropriate text
         $output .= $this->ewww_render_optimize_action_link($id, $image);
         if (is_object($id)) {
             return $output;
         } else {
             echo $output;
         }
     }
 }
コード例 #3
0
function ewww_image_optimizer_tool_init($hook = false, $admin = true)
{
    global $ewww_debug;
    //	global $ewww_admin;
    $ewww_debug .= "<b>ewww_image_optimizer_tool_init()</b><br>";
    if ($admin) {
        $ewww_debug .= 'we are in the admin, feel free to shout<br>';
    } else {
        $ewww_debug .= 'no admin, be quiet<br>';
    }
    // make sure the bundled tools are installed
    if (!ewww_image_optimizer_get_option('ewww_image_optimizer_skip_bundle')) {
        ewww_image_optimizer_install_tools();
    }
    if ($admin) {
        //then we run the function to check for optimization utilities
        add_action('network_admin_notices', 'ewww_image_optimizer_notice_utils');
        add_action('admin_notices', 'ewww_image_optimizer_notice_utils');
    } else {
        if (EWWW_IMAGE_OPTIMIZER_CLOUD) {
            $ewww_debug .= 'cloud options enabled, shutting off binaries<br>';
            ewww_image_optimizer_disable_tools();
        }
        ewww_image_optimizer_notice_utils();
    }
}
コード例 #4
0
function ewww_image_optimizer_tool_init($admin = true)
{
    // make sure the bundled tools are installed
    if (!ewww_image_optimizer_get_option('ewww_image_optimizer_skip_bundle')) {
        ewww_image_optimizer_install_tools();
    }
    if ($admin) {
        //then we run the function to check for optimization utilities
        add_action('network_admin_notices', 'ewww_image_optimizer_notice_utils');
        add_action('admin_notices', 'ewww_image_optimizer_notice_utils');
    } else {
        ewww_image_optimizer_notice_utils();
    }
}
コード例 #5
0
ファイル: common.php プロジェクト: aaronfrey/PepperLillie-CVM
/**
 * 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, $meta = null, $return_output = false)
{
    ewwwio_debug_message('<b>' . __FUNCTION__ . '()</b>');
    // once we get to the EWWW IO custom column
    if ($column_name == 'ewww-image-optimizer') {
        $output = '';
        if ($meta == null) {
            // retrieve the metadata
            $meta = wp_get_attachment_metadata($id);
        }
        if (ewww_image_optimizer_get_option('ewww_image_optimizer_debug') && !$return_output) {
            $print_meta = print_r($meta, TRUE);
            $print_meta = preg_replace(array('/ /', '/\\n+/'), array('&nbsp;', '<br />'), $print_meta);
            $output .= '<div style="background-color:#ffff99;font-size: 10px;padding: 10px;margin:-10px -10px 10px;line-height: 1.1em">' . $print_meta . '</div>';
        }
        $ewww_cdn = false;
        if (is_array($meta) && !empty($meta['cloudinary'])) {
            $output .= esc_html__('Cloudinary image', EWWW_IMAGE_OPTIMIZER_DOMAIN);
            if ($return_output) {
                return $output;
            }
            echo $output;
            return;
        }
        if (is_array($meta) & class_exists('WindowsAzureStorageUtil') && !empty($meta['url'])) {
            $output .= esc_html__('Azure Storage image', EWWW_IMAGE_OPTIMIZER_DOMAIN);
            $ewww_cdn = true;
        }
        if (is_array($meta) && class_exists('Amazon_S3_And_CloudFront') && preg_match('/^(http|s3)\\w*:/', get_attached_file($id))) {
            $output .= esc_html__('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) {
            $output .= esc_html__('Could not retrieve file path.', EWWW_IMAGE_OPTIMIZER_DOMAIN);
            ewww_image_optimizer_debug_log();
            if ($return_output) {
                return $output;
            }
            echo $output;
            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);
        }
        if (!defined('EWWW_IMAGE_OPTIMIZER_JPEGTRAN')) {
            ewww_image_optimizer_tool_init();
            ewww_image_optimizer_notice_utils('quiet');
        }
        $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
                $output .= esc_html__('Unsupported file type', EWWW_IMAGE_OPTIMIZER_DOMAIN);
                ewww_image_optimizer_debug_log();
                if ($return_output) {
                    return $output;
                }
                echo $output;
                return;
        }
        $ewww_manual_nonce = wp_create_nonce("ewww-manual");
        $output .= "<div id='ewww-media-status-{$id}'>";
        if ($ewww_cdn) {
            // if the optimizer metadata exists
            if (is_array($meta) && !empty($meta['ewww_image_optimizer'])) {
                // output the optimizer results
                $output .= "<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
                    $output .= sprintf("<br><a class='ewww-manual-optimize' data-id='{$id}' data-nonce='{$ewww_manual_nonce}' 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)) {
                $output .= esc_html__('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
                $output .= sprintf("<br><a class='ewww-manual-optimize' data-id='{$id}' data-nonce='{$ewww_manual_nonce}' ref=\"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));
            }
            $output .= '</div>';
            if ($return_output) {
                return $output;
            }
            echo $output;
            return;
        }
        // if the optimizer metadata exists
        if (is_array($meta) && !empty($meta['ewww_image_optimizer'])) {
            // output the optimizer results
            $output .= esc_html($meta['ewww_image_optimizer']);
            // output the filesize
            $output .= "<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
                $output .= sprintf("<br><a class='ewww-manual-optimize' data-id='{$id}' data-nonce='{$ewww_manual_nonce}' 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)) {
                    $output .= " | <a class='ewww-manual-convert' data-id='{$id}' data-nonce='{$ewww_manual_nonce}' 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 {
                $output .= $msg;
            }
            $restorable = false;
            if (!empty($meta['converted'])) {
                if (!empty($meta['orig_file']) && file_exists($meta['orig_file'])) {
                    $restorable = true;
                }
            }
            if (isset($meta['sizes']) && ewww_image_optimizer_iterable($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', ''))) {
                $output .= sprintf("<br><a class='ewww-manual-restore' data-id='{$id}' data-nonce='{$ewww_manual_nonce}' 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', ''))) {
                $output .= "<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);
                $output .= "<br>WebP: <a href='{$webpurl}'>{$webp_size}</a>";
            }
        } elseif (get_transient('ewwwio-background-in-progress-' . $id)) {
            $output .= esc_html__('In Progress', EWWW_IMAGE_OPTIMIZER_DOMAIN);
        } else {
            // otherwise, this must be an image we haven't processed
            $output .= esc_html__('Not processed', EWWW_IMAGE_OPTIMIZER_DOMAIN);
            // tell them the filesize
            $output .= "<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
                $output .= sprintf("<br><a class='ewww-manual-optimize' data-id='{$id}' data-nonce='{$ewww_manual_nonce}' 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)) {
                    $output .= " | <a class='ewww-manual-convert' data-id='{$id}' data-nonce='{$ewww_manual_nonce}' 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 {
                $output .= $msg;
            }
        }
        $output .= '</div>';
        if ($return_output) {
            return $output;
        }
        echo $output;
    }
    ewwwio_memory(__FUNCTION__);
}