protected function task($item) { session_write_close(); global $ewww_defer; $ewww_defer = false; $max_attempts = 15; $id = $item['id']; if (empty($item['attempts'])) { $item['attempts'] = 0; } ewwwio_debug_message("background processing {$id}, type: " . $item['type']); $type = $item['type']; $image_types = array('image/jpeg', 'image/png', 'image/gif'); $meta = wp_get_attachment_metadata($id, true); if (in_array($type, $image_types) && empty($meta) && $item['attempts'] < $max_attempts) { $item['attempts']++; sleep(4); ewwwio_debug_message("metadata is missing, requeueing {$item['attempts']}"); ewww_image_optimizer_debug_log(); return $item; } elseif (in_array($type, $image_types) && empty($meta)) { ewwwio_debug_message("metadata is missing for image, out of attempts"); ewww_image_optimizer_debug_log(); delete_transient('ewwwio-background-in-progress-' . $id); return false; } $meta = ewww_image_optimizer_resize_from_meta_data($meta, $id, true, $item['new']); if (!empty($meta['processing'])) { $item['attempts']++; ewwwio_debug_message("image not finished, try again"); ewww_image_optimizer_debug_log(); return $item; } wp_update_attachment_metadata($id, $meta); ewww_image_optimizer_debug_log(); delete_transient('ewwwio-background-in-progress-' . $id); return false; }
function ewww_image_optimizer_bulk_media($delay = 0) { $attachments = null; // check if there is a previous bulk operation to resume if (get_option('ewww_image_optimizer_bulk_resume')) { // retrieve the attachment IDs that have not been finished from the 'bulk attachments' option $attachments = get_option('ewww_image_optimizer_bulk_attachments'); // since we aren't resuming, and weren't given a list of IDs, we will optimize everything } else { global $wpdb; // load up all the image attachments we can find $attachments = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE (post_type = 'attachment' OR post_type = 'ims_image') AND (post_mime_type LIKE '%%image%%' OR post_mime_type LIKE '%%pdf%%') ORDER BY ID DESC"); // store the attachment IDs we retrieved in the 'bulk_attachments' option so we can keep track of our progress in the database update_option('ewww_image_optimizer_bulk_attachments', $attachments, false); // update the 'bulk resume' option to show that an operation is in progress update_option('ewww_image_optimizer_bulk_resume', 'true'); } if (ewww_image_optimizer_iterable($attachments)) { foreach ($attachments as $attachment_ID) { // get the 'bulk attachments' with a list of IDs remaining $attachments_left = get_option('ewww_image_optimizer_bulk_attachments'); $meta = wp_get_attachment_metadata($attachment_ID); if (!empty($meta['file'])) { // let the user know the file we are currently optimizing WP_CLI::line(__('Optimizing', EWWW_IMAGE_OPTIMIZER_DOMAIN) . " {$meta['file']}:"); } sleep($delay); // retrieve the time when the optimizer starts $started = microtime(true); // do the optimization for the current attachment (including resizes) $meta = ewww_image_optimizer_resize_from_meta_data($meta, $attachment_ID, false); if (empty($meta['file'])) { WP_CLI::warning(__('Skipped image, ID:', EWWW_IMAGE_OPTIMIZER_DOMAIN) . " {$attachment}"); } if (!empty($meta['ewww_image_optimizer'])) { // tell the user what the results were for the original image WP_CLI::line(sprintf(__('Full size – %s', EWWW_IMAGE_OPTIMIZER_DOMAIN), html_entity_decode($meta['ewww_image_optimizer']))); } // check to see if there are resized version of the image if (isset($meta['sizes']) && is_array($meta['sizes'])) { // cycle through each resize foreach ($meta['sizes'] as $size) { if (!empty($size['ewww_image_optimizer'])) { // output the results for the current resized version WP_CLI::line("{$size['file']} – " . html_entity_decode($size['ewww_image_optimizer'])); } } } // calculate how much time has elapsed since we started $elapsed = microtime(true) - $started; // output how much time has elapsed since we started WP_CLI::line(sprintf(__('Elapsed: %.3f seconds', EWWW_IMAGE_OPTIMIZER_DOMAIN), $elapsed)); // update the metadata for the current attachment wp_update_attachment_metadata($attachment_ID, $meta); // remove the first element from the $attachments array if (!empty($attachments_left)) { array_shift($attachments_left); } // store the updated list of attachment IDs back in the 'bulk_attachments' option update_option('ewww_image_optimizer_bulk_attachments', $attachments_left, false); } } // all done, so we can update the bulk options with empty values update_option('ewww_image_optimizer_bulk_resume', ''); update_option('ewww_image_optimizer_bulk_attachments', '', false); // and let the user know we are done WP_CLI::success(__('Finished Optimization!', EWWW_IMAGE_OPTIMIZER_DOMAIN)); }
/** * Manually process an image from the Media Library */ function ewww_image_optimizer_manual() { global $ewww_debug; $ewww_debug .= "<b>ewww_image_optimizer_manual()</b><br>"; // check permissions of current user $permissions = apply_filters('ewww_image_optimizer_manual_permissions', ''); if (FALSE === current_user_can($permissions)) { // display error message if insufficient permissions wp_die(__('You don\'t have permission to optimize images.', EWWW_IMAGE_OPTIMIZER_DOMAIN)); } // make sure we didn't accidentally get to this page without an attachment to work on if (FALSE === isset($_GET['ewww_attachment_ID'])) { // display an error message since we don't have anything to work on wp_die(__('No attachment ID was provided.', EWWW_IMAGE_OPTIMIZER_DOMAIN)); } // store the attachment ID value $attachment_ID = intval($_GET['ewww_attachment_ID']); // retrieve the existing attachment metadata $original_meta = wp_get_attachment_metadata($attachment_ID); // if the call was to optimize... if ($_REQUEST['action'] === 'ewww_image_optimizer_manual_optimize') { // call the optimize from metadata function and store the resulting new metadata $new_meta = ewww_image_optimizer_resize_from_meta_data($original_meta, $attachment_ID); } elseif ($_REQUEST['action'] === 'ewww_image_optimizer_manual_restore') { $new_meta = ewww_image_optimizer_restore_from_meta_data($original_meta, $attachment_ID); } // update the attachment metadata in the database wp_update_attachment_metadata($attachment_ID, $new_meta); // store the referring webpage location $sendback = wp_get_referer(); // sanitize the referring webpage location $sendback = preg_replace('|[^a-z0-9-~+_.?#=&;,/:]|i', '', $sendback); // send the user back where they came from wp_redirect($sendback); // we are done, nothing to see here ewwwio_memory(__FUNCTION__); exit(0); }
function ewww_image_optimizer_bulk_loop() { global $ewww_debug; global $ewww_exceed; // verify that an authorized user has started the optimizer $permissions = apply_filters('ewww_image_optimizer_bulk_permissions', ''); if (!wp_verify_nonce($_REQUEST['ewww_wpnonce'], 'ewww-image-optimizer-bulk') || !current_user_can($permissions)) { wp_die(__('Cheatin’ eh?', EWWW_IMAGE_OPTIMIZER_DOMAIN)); } if (!empty($_REQUEST['ewww_sleep'])) { sleep($_REQUEST['ewww_sleep']); } // retrieve the time when the optimizer starts $started = microtime(true); // get the attachment ID of the current attachment $attachment = $_POST['ewww_attachment']; // get the 'bulk attachments' with a list of IDs remaining $attachments = get_option('ewww_image_optimizer_bulk_attachments'); $meta = wp_get_attachment_metadata($attachment, true); // do the optimization for the current attachment (including resizes) $meta = ewww_image_optimizer_resize_from_meta_data($meta, $attachment, false); if (!empty($ewww_exceed)) { echo '-9exceeded'; die; } if (!empty($meta['file'])) { // output the filename (and path relative to 'uploads' folder) printf("<p>" . __('Optimized image:', EWWW_IMAGE_OPTIMIZER_DOMAIN) . " <strong>%s</strong><br>", esc_html($meta['file'])); } else { printf("<p>" . __('Skipped image, ID:', EWWW_IMAGE_OPTIMIZER_DOMAIN) . " <strong>%s</strong><br>", $attachment); } if (!empty($meta['ewww_image_optimizer'])) { // tell the user what the results were for the original image printf(__('Full size – %s', EWWW_IMAGE_OPTIMIZER_DOMAIN) . "<br>", $meta['ewww_image_optimizer']); } // check to see if there are resized version of the image if (isset($meta['sizes']) && is_array($meta['sizes'])) { // cycle through each resize foreach ($meta['sizes'] as $size) { if (!empty($size['ewww_image_optimizer'])) { // output the results for the current resized version printf("%s – %s<br>", $size['file'], $size['ewww_image_optimizer']); } } } // calculate how much time has elapsed since we started $elapsed = microtime(true) - $started; // output how much time has elapsed since we started printf(__('Elapsed: %.3f seconds', EWWW_IMAGE_OPTIMIZER_DOMAIN) . "</p>", $elapsed); global $ewww_attachment; $ewww_attachment['id'] = $attachment; $ewww_attachment['meta'] = $meta; add_filter('w3tc_cdn_update_attachment_metadata', 'ewww_image_optimizer_w3tc_update_files'); // update the metadata for the current attachment wp_update_attachment_metadata($attachment, $meta); // remove the first element from the $attachments array if (!empty($attachments)) { array_shift($attachments); } // store the updated list of attachment IDs back in the 'bulk_attachments' option update_option('ewww_image_optimizer_bulk_attachments', $attachments); if (ewww_image_optimizer_get_option('ewww_image_optimizer_debug')) { echo '<div style="background-color:#ffff99;">' . $ewww_debug . '</div>'; } ewww_image_optimizer_debug_log(); ewwwio_memory(__FUNCTION__); die; }
function ewww_image_optimizer_bulk_loop() { ewwwio_debug_message('<b>' . __FUNCTION__ . '()</b>'); global $ewww_defer; $ewww_defer = false; $output = array(); // verify that an authorized user has started the optimizer $permissions = apply_filters('ewww_image_optimizer_bulk_permissions', ''); if (!wp_verify_nonce($_REQUEST['ewww_wpnonce'], 'ewww-image-optimizer-bulk') || !current_user_can($permissions)) { $output['error'] = esc_html__('Access token has expired, please reload the page.', EWWW_IMAGE_OPTIMIZER_DOMAIN); echo json_encode($output); die; } session_write_close(); // retrieve the time when the optimizer starts $started = microtime(true); if (ewww_image_optimizer_stl_check() && ini_get('max_execution_time')) { set_time_limit(0); } // find out if our nonce is on it's last leg/tick $tick = wp_verify_nonce($_REQUEST['ewww_wpnonce'], 'ewww-image-optimizer-bulk'); if ($tick === 2) { $output['new_nonce'] = wp_create_nonce('ewww-image-optimizer-bulk'); } else { $output['new_nonce'] = ''; } // get the 'bulk attachments' with a list of IDs remaining $attachments = get_option('ewww_image_optimizer_bulk_attachments'); $attachment = array_shift($attachments); $meta = wp_get_attachment_metadata($attachment, true); // do the optimization for the current attachment (including resizes) $meta = ewww_image_optimizer_resize_from_meta_data($meta, $attachment, false); $ewww_status = get_transient('ewww_image_optimizer_cloud_status'); if (!empty($ewww_status) && preg_match('/exceeded/', $ewww_status)) { $output['error'] = esc_html__('License Exceeded', EWWW_IMAGE_OPTIMIZER_DOMAIN); echo json_encode($output); die; } if (!empty($meta['file'])) { // output the filename (and path relative to 'uploads' folder) $output['results'] = sprintf("<p>" . esc_html__('Optimized', EWWW_IMAGE_OPTIMIZER_DOMAIN) . " <strong>%s</strong><br>", esc_html($meta['file'])); } else { $output['results'] = sprintf("<p>" . esc_html__('Skipped image, ID:', EWWW_IMAGE_OPTIMIZER_DOMAIN) . " <strong>%s</strong><br>", esc_html($attachment)); } if (!empty($meta['ewww_image_optimizer'])) { // tell the user what the results were for the original image $output['results'] .= sprintf(esc_html__('Full size – %s', EWWW_IMAGE_OPTIMIZER_DOMAIN) . "<br>", esc_html($meta['ewww_image_optimizer'])); } // check to see if there are resized version of the image if (isset($meta['sizes']) && is_array($meta['sizes'])) { // cycle through each resize foreach ($meta['sizes'] as $size) { if (!empty($size['ewww_image_optimizer'])) { // output the results for the current resized version $output['results'] .= sprintf("%s – %s<br>", esc_html($size['file']), esc_html($size['ewww_image_optimizer'])); } } } // calculate how much time has elapsed since we started $elapsed = microtime(true) - $started; // output how much time has elapsed since we started $output['results'] .= sprintf(esc_html__('Elapsed: %.3f seconds', EWWW_IMAGE_OPTIMIZER_DOMAIN) . "</p>", $elapsed); global $ewww_attachment; $ewww_attachment['id'] = $attachment; $ewww_attachment['meta'] = $meta; add_filter('w3tc_cdn_update_attachment_metadata', 'ewww_image_optimizer_w3tc_update_files'); // update the metadata for the current attachment wp_update_attachment_metadata($attachment, $meta); // store the updated list of attachment IDs back in the 'bulk_attachments' option update_option('ewww_image_optimizer_bulk_attachments', $attachments); if (ewww_image_optimizer_get_option('ewww_image_optimizer_debug')) { global $ewww_debug; $output['results'] .= '<div style="background-color:#ffff99;">' . $ewww_debug . '</div>'; } if (!empty($attachments)) { $next_attachment = array_shift($attachments); $next_file = ewww_image_optimizer_bulk_filename($next_attachment); // generate the WP spinner image for display $loading_image = plugins_url('/images/wpspin.gif', __FILE__); if ($next_file) { $output['next_file'] = "<p>" . esc_html__('Optimizing', EWWW_IMAGE_OPTIMIZER_DOMAIN) . " <b>{$next_file}</b> <img src='{$loading_image}' /></p>"; } else { $output['next_file'] = "<p>" . esc_html__('Optimizing', EWWW_IMAGE_OPTIMIZER_DOMAIN) . " <img src='{$loading_image}' /></p>"; } } echo json_encode($output); ewww_image_optimizer_debug_log(); ewwwio_memory(__FUNCTION__); die; }
/** * Manually process an image from the Media Library */ function ewww_image_optimizer_manual() { ewwwio_debug_message('<b>' . __FUNCTION__ . '()</b>'); global $ewww_defer; $ewww_defer = false; // check permissions of current user $permissions = apply_filters('ewww_image_optimizer_manual_permissions', ''); if (FALSE === current_user_can($permissions)) { // display error message if insufficient permissions wp_die(esc_html__('You do not have permission to optimize images.', EWWW_IMAGE_OPTIMIZER_DOMAIN)); } // make sure we didn't accidentally get to this page without an attachment to work on if (FALSE === isset($_GET['ewww_attachment_ID'])) { // display an error message since we don't have anything to work on wp_die(esc_html__('No attachment ID was provided.', EWWW_IMAGE_OPTIMIZER_DOMAIN)); } session_write_close(); // store the attachment ID value $attachment_ID = intval($_GET['ewww_attachment_ID']); if (empty($_REQUEST['ewww_manual_nonce']) || !wp_verify_nonce($_REQUEST['ewww_manual_nonce'], "ewww-manual-{$attachment_ID}")) { wp_die(esc_html__('Access denied.', EWWW_IMAGE_OPTIMIZER_DOMAIN)); } // retrieve the existing attachment metadata $original_meta = wp_get_attachment_metadata($attachment_ID); // if the call was to optimize... if ($_REQUEST['action'] === 'ewww_image_optimizer_manual_optimize') { // call the optimize from metadata function and store the resulting new metadata $new_meta = ewww_image_optimizer_resize_from_meta_data($original_meta, $attachment_ID); } elseif ($_REQUEST['action'] === 'ewww_image_optimizer_manual_restore') { $new_meta = ewww_image_optimizer_restore_from_meta_data($original_meta, $attachment_ID); } global $ewww_attachment; $ewww_attachment['id'] = $attachment_ID; $ewww_attachment['meta'] = $new_meta; add_filter('w3tc_cdn_update_attachment_metadata', 'ewww_image_optimizer_w3tc_update_files'); // update the attachment metadata in the database wp_update_attachment_metadata($attachment_ID, $new_meta); ewww_image_optimizer_debug_log(); // store the referring webpage location $sendback = wp_get_referer(); // sanitize the referring webpage location $sendback = preg_replace('|[^a-z0-9-~+_.?#=&;,/:]|i', '', $sendback); // send the user back where they came from wp_redirect($sendback); // we are done, nothing to see here ewwwio_memory(__FUNCTION__); exit(0); }
function ewww_image_optimizer_update_saved_file($meta, $ID) { global $ewww_debug; $ewww_debug = "{$ewww_debug} <b>ewww_image_optimizer_update_saved_file()</b><br>"; $meta = ewww_image_optimizer_resize_from_meta_data($meta, $ID); return $meta; }
function ewww_image_optimizer_bulk_loop() { global $ewww_debug; // verify that an authorized user has started the optimizer if (!wp_verify_nonce($_REQUEST['_wpnonce'], 'ewww-image-optimizer-bulk') || !current_user_can('edit_others_posts')) { wp_die(__('Cheatin’ eh?')); } // retrieve the time when the optimizer starts $started = microtime(true); // allow 50 seconds for each image (this doesn't include any exec calls, only php processing time) set_time_limit(50); // get the attachment ID of the current attachment $attachment = $_POST['attachment']; // get the 'bulk attachments' with a list of IDs remaining $attachments = get_option('ewww_image_optimizer_bulk_attachments'); // do the optimization for the current attachment (including resizes) $meta = ewww_image_optimizer_resize_from_meta_data(wp_get_attachment_metadata($attachment, true), $attachment); // output the filename (and path relative to 'uploads' folder printf("<p>Optimized image: <strong>%s</strong><br>", esc_html($meta['file'])); // tell the user what the results were for the original image printf("Full size – %s<br>", $meta['ewww_image_optimizer']); // check to see if there are resized version of the image if (isset($meta['sizes']) && is_array($meta['sizes'])) { // cycle through each resize foreach ($meta['sizes'] as $size) { // output the results for the current resized version printf("%s – %s<br>", $size['file'], $size['ewww_image_optimizer']); } } // calculate how much time has elapsed since we started $elapsed = microtime(true) - $started; // output how much time has elapsed since we started echo "Elapsed: " . round($elapsed, 3) . " seconds</p>"; // update the metadata for the current attachment wp_update_attachment_metadata($attachment, $meta); // remove the first element fromt the $attachments array array_shift($attachments); // store the updated list of attachment IDs back in the 'bulk_attachments' option update_option('ewww_image_optimizer_bulk_attachments', $attachments); if (get_site_option('ewww_image_optimizer_debug')) { echo '<div style="background-color:#ffff99;">' . $ewww_debug . '</div>'; } die; }