Exemple #1
0
        wp_nonce_field('cw-image-optimizer-bulk', '_cw-image-optimizer-bulk-nonce');
        ?>
    <button type="submit" class="button-secondary action">Run all my images through image optimizers right now</button>
  </form>
  
<?php 
    } else {
        // run the script
        if (!wp_verify_nonce($_POST['_cw-image-optimizer-bulk-nonce'], 'cw-image-optimizer-bulk') || !current_user_can('edit_others_posts')) {
            wp_die(__('Cheatin&#8217; uh?'));
        }
        ob_implicit_flush(true);
        ob_end_flush();
        foreach ($attachments as $attachment) {
            printf("<p>Processing <strong>%s</strong>&hellip;<br>", esc_html($attachment->post_name));
            $meta = cw_image_optimizer_resize_from_meta_data(wp_get_attachment_metadata($attachment->ID, true), $attachment->ID);
            if (isset($meta['sizes']) && is_array($meta['sizes'])) {
                foreach ($meta['sizes'] as $size) {
                    printf("– %s<br>", $size['cw_image_optimizer']);
                }
            }
            echo "</p>";
            wp_update_attachment_metadata($attachment->ID, $meta);
            @ob_flush();
            flush();
        }
    }
}
?>
</div>
/**
 * Manually process an image from the Media Library
 */
function cw_image_optimizer_manual()
{
    if (FALSE === current_user_can('upload_files')) {
        wp_die(__('You don\'t have permission to work with uploaded files.', CW_IMAGE_OPTIMIZER_DOMAIN));
    }
    if (FALSE === isset($_GET['attachment_ID'])) {
        wp_die(__('No attachment ID was provided.', CW_IMAGE_OPTIMIZER_DOMAIN));
    }
    $attachment_ID = intval($_GET['attachment_ID']);
    $original_meta = wp_get_attachment_metadata($attachment_ID);
    $new_meta = cw_image_optimizer_resize_from_meta_data($original_meta, $attachment_ID);
    wp_update_attachment_metadata($attachment_ID, $new_meta);
    $sendback = wp_get_referer();
    $sendback = preg_replace('|[^a-z0-9-~+_.?#=&;,/:]|i', '', $sendback);
    wp_redirect($sendback);
    exit(0);
}