function ewww_image_optimizer_bulk_script($hook) { ewwwio_debug_message('<b>' . __FUNCTION__ . '()</b>'); // make sure we are being called from the bulk optimization page if ('media_page_ewww-image-optimizer-bulk' != $hook) { return; } // initialize the $attachments variable $attachments = array(); // check to see if we are supposed to reset the bulk operation and verify we are authorized to do so if (!empty($_REQUEST['ewww_reset']) && wp_verify_nonce($_REQUEST['ewww_wpnonce'], 'ewww-image-optimizer-bulk-reset')) { // set the 'bulk resume' option to an empty string to reset the bulk operation update_option('ewww_image_optimizer_bulk_resume', ''); } // check to see if we are supposed to reset the bulk operation and verify we are authorized to do so if (!empty($_REQUEST['ewww_reset_aux']) && wp_verify_nonce($_REQUEST['ewww_wpnonce'], 'ewww-image-optimizer-aux-images-reset')) { // set the 'bulk resume' option to an empty string to reset the bulk operation update_option('ewww_image_optimizer_aux_resume', ''); } // check to see if we are supposed to convert the auxiliary images table and verify we are authorized to do so if (!empty($_REQUEST['ewww_convert']) && wp_verify_nonce($_REQUEST['ewww_wpnonce'], 'ewww-image-optimizer-aux-images-convert')) { ewww_image_optimizer_aux_images_convert(); } global $wpdb; // check the 'bulk resume' option $resume = get_option('ewww_image_optimizer_bulk_resume'); // see if we were given attachment IDs to work with via GET/POST if (!empty($_REQUEST['ids']) && preg_match('/^[\\d,]+$/', $_REQUEST['ids'], $request_ids)) { $ids = explode(',', $request_ids[0]); $sample_post_type = get_post_type($ids[0]); //ewwwio_debug_message( "ids: " . $request_ids[0] ); ewwwio_debug_message("post type (checking for ims_gallery): {$sample_post_type}"); if ('ims_gallery' == $sample_post_type) { $attachments = array(); foreach ($ids as $gid) { ewwwio_debug_message("gallery id: {$gid}"); $ims_images = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE post_type = 'ims_image' AND post_mime_type LIKE '%%image%%' AND post_parent = {$gid} ORDER BY ID DESC"); $attachments = array_merge($attachments, $ims_images); } } else { // retrieve post IDs correlating to the IDs submitted to make sure they are all valid $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%%') AND ID IN ({$request_ids[0]}) ORDER BY ID DESC"); } // unset the 'bulk resume' option since we were given specific IDs to optimize update_option('ewww_image_optimizer_bulk_resume', ''); // check if there is a previous bulk operation to resume } elseif (!empty($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 { // 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); wp_enqueue_script('ewwwbulkscript', plugins_url('/includes/eio.js', __FILE__), array('jquery', 'jquery-ui-slider', 'jquery-ui-progressbar', 'postbox', 'dashboard')); // number of images in the ewwwio_table (previously optimized images) $image_count = ewww_image_optimizer_aux_images_table_count(); // number of image attachments to be optimized $attachment_count = count($attachments); // submit a couple variables to the javascript to work with wp_localize_script('ewwwbulkscript', 'ewww_vars', array('_wpnonce' => wp_create_nonce('ewww-image-optimizer-bulk'), 'attachments' => $attachment_count, 'image_count' => $image_count, 'count_string' => sprintf(esc_html__('%d images', EWWW_IMAGE_OPTIMIZER_DOMAIN), $image_count), 'scan_fail' => esc_html__('Operation timed out, you may need to increase the max_execution_time for PHP', EWWW_IMAGE_OPTIMIZER_DOMAIN), 'operation_stopped' => esc_html__('Optimization stopped, reload page to resume.', EWWW_IMAGE_OPTIMIZER_DOMAIN), 'operation_interrupted' => esc_html__('Operation Interrupted', EWWW_IMAGE_OPTIMIZER_DOMAIN), 'temporary_failure' => esc_html__('Temporary failure, seconds left to retry:', EWWW_IMAGE_OPTIMIZER_DOMAIN), 'remove_failed' => esc_html__('Could not remove image from table.', EWWW_IMAGE_OPTIMIZER_DOMAIN), 'optimized' => esc_html__('Optimized', EWWW_IMAGE_OPTIMIZER_DOMAIN))); // load the stylesheet for the jquery progressbar wp_enqueue_style('jquery-ui-progressbar', plugins_url('/includes/jquery-ui-1.10.1.custom.css', __FILE__)); ewwwio_memory(__FUNCTION__); }
function ewww_image_optimizer_bulk_script($hook) { global $ewww_debug; // make sure we are being called from the bulk optimization page if ('media_page_ewww-image-optimizer-bulk' != $hook) { return; } // initialize the $attachments variable $attachments = null; // check to see if we are supposed to reset the bulk operation and verify we are authorized to do so if (!empty($_REQUEST['ewww_reset']) && wp_verify_nonce($_REQUEST['ewww_wpnonce'], 'ewww-image-optimizer-bulk')) { // set the 'bulk resume' option to an empty string to reset the bulk operation update_option('ewww_image_optimizer_bulk_resume', ''); } // check to see if we are supposed to reset the bulk operation and verify we are authorized to do so if (!empty($_REQUEST['ewww_reset_aux']) && wp_verify_nonce($_REQUEST['ewww_wpnonce'], 'ewww-image-optimizer-aux-images')) { // set the 'bulk resume' option to an empty string to reset the bulk operation update_option('ewww_image_optimizer_aux_resume', ''); } // check to see if we are supposed to convert the auxiliary images table and verify we are authorized to do so if (!empty($_REQUEST['ewww_convert']) && wp_verify_nonce($_REQUEST['ewww_wpnonce'], 'ewww-image-optimizer-aux-images')) { ewww_image_optimizer_aux_images_convert(); } // check the 'bulk resume' option $resume = get_option('ewww_image_optimizer_bulk_resume'); // see if we were given attachment IDs to work with via GET/POST if (!empty($_REQUEST['ids'])) { $ids = explode(',', $_REQUEST['ids']); $ewww_debug .= "gallery ids: " . print_r($ids, true) . "<br>"; $ewww_debug .= "post_type: " . get_post_type($ids[0]) . "<br>"; if ('ims_gallery' == get_post_type($ids[0])) { $attachments = array(); foreach ($ids as $gid) { $ewww_debug .= "gallery id: {$gid}<br>"; $ims_images = get_posts(array('numberposts' => -1, 'post_type' => 'ims_image', 'post_status' => 'any', 'post_mime_type' => 'image', 'post_parent' => $gid, 'fields' => 'ids')); $attachments = array_merge($attachments, $ims_images); $ewww_debug .= "attachment ids: " . print_r($attachments, true) . "<br>"; } } else { // retrieve post IDs correlating to the IDs submitted to make sure they are all valid $attachments = get_posts(array('numberposts' => -1, 'include' => $ids, 'post_type' => array('attachment', 'ims_image'), 'post_status' => 'any', 'post_mime_type' => 'image', 'fields' => 'ids')); } // unset the 'bulk resume' option since we were given specific IDs to optimize update_option('ewww_image_optimizer_bulk_resume', ''); // check if there is a previous bulk operation to resume } else { if (!empty($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 { // load up all the image attachments we can find $attachments = get_posts(array('numberposts' => -1, 'post_type' => array('attachment', 'ims_image'), 'post_status' => 'any', 'post_mime_type' => 'image', 'fields' => 'ids')); } } // 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); wp_enqueue_script('ewwwbulkscript', plugins_url('/eio.js', __FILE__), array('jquery', 'jquery-ui-slider', 'jquery-ui-progressbar')); $image_count = ewww_image_optimizer_aux_images_table_count(); // submit a couple variables to the javascript to work with $attachments = json_encode($attachments); wp_localize_script('ewwwbulkscript', 'ewww_vars', array('_wpnonce' => wp_create_nonce('ewww-image-optimizer-bulk'), 'attachments' => $attachments, 'image_count' => $image_count, 'count_string' => sprintf(__('%d images', EWWW_IMAGE_OPTIMIZER_DOMAIN), $image_count), 'scan_fail' => __('Operation timed out, you may need to increase the max_execution_time for PHP', EWWW_IMAGE_OPTIMIZER_DOMAIN), 'license_exceeded' => __('License Exceeded', EWWW_IMAGE_OPTIMIZER_DOMAIN), 'operation_stopped' => __('Optimization stopped, reload page to resume.', EWWW_IMAGE_OPTIMIZER_DOMAIN), 'operation_interrupted' => __('Operation Interrupted', EWWW_IMAGE_OPTIMIZER_DOMAIN), 'temporary_failure' => __('Temporary failure, seconds left to retry:', EWWW_IMAGE_OPTIMIZER_DOMAIN), 'remove_failed' => __('Could not remove image from table.', EWWW_IMAGE_OPTIMIZER_DOMAIN))); // load the stylesheet for the jquery progressbar wp_enqueue_style('jquery-ui-progressbar', plugins_url('jquery-ui-1.10.1.custom.css', __FILE__)); ewwwio_memory(__FUNCTION__); }
function ewww_image_optimizer_bulk_script($hook) { global $ewww_debug; global $wpdb; // make sure we are being called from the bulk optimization page if ('media_page_ewww-image-optimizer-bulk' != $hook) { return; } // $ewww_debug .= "starting memory usage: " . memory_get_usage(true) . "<br>"; // initialize the $attachments variable $attachments = null; // check to see if we are supposed to reset the bulk operation and verify we are authorized to do so if (!empty($_REQUEST['reset']) && wp_verify_nonce($_REQUEST['_wpnonce'], 'ewww-image-optimizer-bulk')) { // set the 'bulk resume' option to an empty string to reset the bulk operation update_option('ewww_image_optimizer_bulk_resume', ''); } // check to see if we are supposed to reset the bulk operation and verify we are authorized to do so if (!empty($_REQUEST['reset-aux']) && wp_verify_nonce($_REQUEST['_wpnonce'], 'ewww-image-optimizer-aux-images')) { // set the 'bulk resume' option to an empty string to reset the bulk operation update_option('ewww_image_optimizer_aux_resume', ''); } // check to see if we are supposed to empty the auxiliary images table and verify we are authorized to do so if (!empty($_REQUEST['empty']) && wp_verify_nonce($_REQUEST['_wpnonce'], 'ewww-image-optimizer-aux-images')) { // empty the ewwwio_images table to allow re-optimization $wpdb->query("TRUNCATE {$wpdb->ewwwio_images}"); update_option('ewww_image_optimizer_aux_last', ''); update_option('ewww_image_optimizer_imported', ''); } // check to see if we are supposed to convert the auxiliary images table and verify we are authorized to do so if (!empty($_REQUEST['convert']) && wp_verify_nonce($_REQUEST['_wpnonce'], 'ewww-image-optimizer-aux-images')) { ewww_image_optimizer_aux_images_convert(); } // check the 'bulk resume' option $resume = get_option('ewww_image_optimizer_bulk_resume'); // see if we were given attachment IDs to work with via GET/POST if (!empty($_REQUEST['ids'])) { $ids = explode(',', $_REQUEST['ids']); $ewww_debug .= "gallery ids: " . print_r($ids, true) . "<br>"; $ewww_debug .= "post_type: " . get_post_type($ids[0]) . "<br>"; if ('ims_gallery' == get_post_type($ids[0])) { $attachments = array(); foreach ($ids as $gid) { $ewww_debug .= "gallery id: {$gid}<br>"; $ims_images = get_posts(array('numberposts' => -1, 'post_type' => 'ims_image', 'post_status' => 'any', 'post_mime_type' => 'image', 'post_parent' => $gid, 'fields' => 'ids')); $attachments = array_merge($attachments, $ims_images); $ewww_debug .= "attachment ids: " . print_r($attachments, true) . "<br>"; } } else { // retrieve post IDs correlating to the IDs submitted to make sure they are all valid $attachments = get_posts(array('numberposts' => -1, 'include' => $ids, 'post_type' => array('attachment', 'ims_image'), 'post_status' => 'any', 'post_mime_type' => 'image', 'fields' => 'ids')); } // unset the 'bulk resume' option since we were given specific IDs to optimize update_option('ewww_image_optimizer_bulk_resume', ''); // check if there is a previous bulk operation to resume } else { if (!empty($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 { // load up all the image attachments we can find $attachments = get_posts(array('numberposts' => -1, 'post_type' => array('attachment', 'ims_image'), 'post_status' => 'any', 'post_mime_type' => 'image', 'fields' => 'ids')); } } // 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); wp_enqueue_script('ewwwbulkscript', plugins_url('/eio.js', __FILE__), array('jquery', 'jquery-ui-slider', 'jquery-ui-progressbar')); $image_count = ewww_image_optimizer_aux_images_table_count(); // submit a couple variables to the javascript to work with $attachments = json_encode($attachments); wp_localize_script('ewwwbulkscript', 'ewww_vars', array('_wpnonce' => wp_create_nonce('ewww-image-optimizer-bulk'), 'attachments' => $attachments, 'image_count' => $image_count)); // load the stylesheet for the jquery progressbar wp_enqueue_style('jquery-ui-progressbar', plugins_url('jquery-ui-1.10.1.custom.css', __FILE__)); }