Example #1
0
/**
 * Read the image paths from an attachment's meta data and process each image
 * with wp_smushit().
 *
 * This method also adds a `wp_smushit` meta key for use in the media library.
 *
 * Called after `wp_generate_attachment_metadata` is completed.
 */
function wp_smushit_resize_from_meta_data($meta, $attachment_ID)
{
    if (empty($meta['file'])) {
        $url = wp_get_attachment_url($attachment_ID);
        $path = preg_replace('/.*\\/wp-content\\/uploads\\//', '', $url);
        $meta['file'] = $path;
    }
    $file_path = $meta['file'];
    $store_absolute_path = true;
    $upload_dir = wp_upload_dir();
    $upload_path = trailingslashit($upload_dir['basedir']);
    // WordPress >= 2.6.2: determine the absolute $file_path (http://core.trac.wordpress.org/changeset/8796)
    if (FALSE === strpos($file, WP_CONTENT_DIR)) {
        $store_absolute_path = false;
        $file_path = $upload_path . $file_path;
    }
    list($file, $msg) = wp_smushit($file_path);
    $meta['file'] = $file;
    $meta['wp_smushit'] = $msg;
    // strip absolute path for Wordpress >= 2.6.2
    if (FALSE === $store_absolute_path) {
        $meta['file'] = str_replace($upload_path, '', $meta['file']);
    }
    // no resized versions, so we can exit
    if (!isset($meta['sizes'])) {
        return $meta;
    }
    // meta sizes don't contain a path, so we calculate one
    $base_dir = dirname($file_path) . '/';
    foreach ($meta['sizes'] as $size => $data) {
        list($smushed_file, $results) = wp_smushit($base_dir . $data['file']);
        $meta['sizes'][$size]['file'] = str_replace($base_dir, '', $smushed_file);
        $meta['sizes'][$size]['wp_smushit'] = $results;
    }
    return $meta;
}
Example #2
0
        check_admin_referer('wp-smushit_smush-theme' . $theme);
    }
    ?>

<p>Processing files in the <strong><?php 
    echo $theme;
    ?>
</strong> theme.</p>

<?php 
    foreach ($_POST['smushitlink'] as $l) {
        // decode and sanitize the file path
        $asset_url = base64_decode($l);
        $asset_path = str_replace($theme_url, $theme_path, $asset_url);
        print "<p>Smushing <span class='code'>{$asset_url}</span><br/>";
        list($processed_path, $msg) = wp_smushit($asset_path);
        echo "<em>&#x2013; {$msg}</em>.</p>\n";
        ob_flush();
    }
    ?>

<p>Finished processing all the files in the <strong><?php 
    echo $theme;
    ?>
</strong> theme.</p>

<p><strong>Actions:</strong> 
	<a href="themes.php?page=<?php 
    echo basename(dirname(__FILE__)) . '/theme.php&amp;theme=' . $theme;
    ?>
" title="<?php 
Example #3
0
/**
 * Read the image paths from an attachment's meta data and process each image
 * with wp_smushit().
 *
 * This method also adds a `wp_smushit` meta key for use in the media library.
 *
 * Called after `wp_generate_attachment_metadata` is completed.
 */
function wp_smushit_resize_from_meta_data($meta, $ID = null, $force_resmush = true)
{
    $file_path = $meta['file'];
    $store_absolute_path = true;
    $upload_dir = wp_upload_dir();
    $upload_path = trailingslashit($upload_dir['basedir']);
    // WordPress >= 2.6.2: determine the absolute $file_path (http://core.trac.wordpress.org/changeset/8796)
    if (FALSE === strpos($file, $upload_path)) {
        $store_absolute_path = false;
        $file_path = $upload_path . $file_path;
    }
    if ($force_resmush || wp_smushit_should_resmush(@$meta['wp_smushit'])) {
        list($file, $msg) = wp_smushit($file_path);
        $meta['wp_smushit'] = $msg;
    }
    // strip absolute path for Wordpress >= 2.6.2
    if (FALSE === $store_absolute_path) {
        $meta['file'] = str_replace($upload_path, '', $meta['file']);
    }
    // no resized versions, so we can exit
    if (!isset($meta['sizes'])) {
        return $meta;
    }
    // meta sizes don't contain a path, so we calculate one
    $base_dir = trailingslashit(dirname($file_path));
    foreach ($meta['sizes'] as $size => $data) {
        if (!$force_resmush && wp_smushit_should_resmush(@$meta['sizes'][$size]['wp_smushit']) === FALSE) {
            continue;
        }
        list($smushed_file, $results) = wp_smushit($base_dir . wp_basename($data['file']));
        $meta['sizes'][$size]['wp_smushit'] = $results;
    }
    return $meta;
}