background_ngg->push_to_queue( array( 'id' => $id ) ); ewwwio_debug_message( "optimization (nextcellent) queued for $id" ); } ewwwio()->background_ngg->dispatch(); } /** * Optimizes a new image from the queue. * * @global object $ewww_image Contains more information about the image currently being processed. * * @param int $id The ID number of the image. * @param array $meta The image metadata. */ public function ewww_added_new_image( $id, $meta ) { global $ewww_image; // Retrieve the image path. $file_path = $meta->image->imagePath; $ewww_image = new EWWW_Image( $id, 'nextcell', $file_path ); $ewww_image->resize = 'full'; // Run the optimizer on the current image. $fres = ewww_image_optimizer( $file_path, 2, false, false, true ); } /** * Optimizes a new image in foreground mode. * * @global bool $ewww_defer Set to false to avoid deferring image optimization. * @global object $wpdb * @global object $ewww_image Contains more information about the image currently being processed. * * @param array $image The new image and all the related data. */ public function ewww_added_new_image_slow( $image ) { // Query the filesystem path of the gallery from the database. global $ewww_defer; global $wpdb; global $ewww_image; $gallery_path = $wpdb->get_var( $wpdb->prepare( "SELECT path FROM {$wpdb->prefix}ngg_gallery WHERE gid = %d LIMIT 1", $image['galleryID'] ) ); // If we have a path to work with. if ( $gallery_path ) { // Construct the absolute path of the current image. $file_path = trailingslashit( $gallery_path ) . $image['filename']; $ewww_image = new EWWW_Image( $image['id'], 'nextcell', $file_path ); $ewww_image->resize = 'full'; // Run the optimizer on the current image. $res = ewww_image_optimizer( ABSPATH . $file_path, 2, false, false, true ); } } /** * Optimizes the thumbnail generated for a new upload. * * @global bool $ewww_defer Set to false to avoid deferring image optimization. * @global object $ewww_image Contains more information about the image currently being processed. * * @param string $filename The name of the file generated. */ public function ewww_ngg_image_save( $filename ) { ewwwio_debug_message( '' . __METHOD__ . '()' ); global $ewww_defer; global $ewww_image; ewwwio_debug_message( 'nextcellent new image thumb' ); if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'ngg-ajax' ) ) { ewwwio_debug_message( 'failed verification' ); return; } if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_debug' ) && ewww_image_optimizer_function_exists( 'print_r' ) ) { ewwwio_debug_message( print_r( $_REQUEST, true ) ); } if ( file_exists( $filename ) ) { if ( ! empty( $_POST['id'] ) ) { $id = (int) $_POST['id']; } elseif ( ! empty( $_POST['image'] ) ) { $id = sanitize_key( $_POST['image'] ); } $ewww_image = new EWWW_Image( $id, 'nextcell', $filename ); $ewww_image->resize = 'thumbnail'; ewww_image_optimizer( $filename ); } ewwwio_memory( __METHOD__ ); } /** * Manually process an image from the NextGEN Gallery. */ public function ewww_ngg_manual() { // Check permission of current user. $permissions = apply_filters( 'ewww_image_optimizer_manual_permissions', '' ); if ( false === current_user_can( $permissions ) ) { if ( ! wp_doing_ajax() ) { wp_die( esc_html__( 'You do not have permission to optimize images.', 'ewww-image-optimizer' ) ); } ewwwio_ob_clean(); wp_die( wp_json_encode( array( 'error' => esc_html__( 'You do not have permission to optimize images.', 'ewww-image-optimizer' ) ) ) ); } // Make sure function wasn't called without an attachment to work with. if ( empty( $_REQUEST['ewww_attachment_ID'] ) ) { if ( ! wp_doing_ajax() ) { wp_die( esc_html__( 'No attachment ID was provided.', 'ewww-image-optimizer' ) ); } ewwwio_ob_clean(); wp_die( wp_json_encode( array( 'error' => esc_html__( 'No attachment ID was provided.', 'ewww-image-optimizer' ) ) ) ); } // Store the attachment $id. $id = intval( $_REQUEST['ewww_attachment_ID'] ); if ( empty( $_REQUEST['ewww_manual_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_REQUEST['ewww_manual_nonce'] ), "ewww-manual-$id" ) ) { if ( ! wp_doing_ajax() ) { wp_die( esc_html__( 'Access denied.', 'ewww-image-optimizer' ) ); } ewwwio_ob_clean(); wp_die( wp_json_encode( array( 'error' => esc_html__( 'Access denied.', 'ewww-image-optimizer' ) ) ) ); } global $ewww_force; $ewww_force = ! empty( $_REQUEST['ewww_force'] ) ? true : false; $this->ewww_ngg_optimize( $id ); $success = $this->ewww_manage_image_custom_column( 'ewww_image_optimizer', $id, true ); if ( ! wp_doing_ajax() ) { // Get the referring page, and send the user back there. wp_safe_redirect( wp_get_referer() ); die; } ewwwio_ob_clean(); wp_die( wp_json_encode( array( 'success' => $success ) ) ); } /** * Restore an image from the NextGEN Gallery. */ public function ewww_ngg_cloud_restore() { // Check permission of current user. $permissions = apply_filters( 'ewww_image_optimizer_manual_permissions', '' ); if ( false === current_user_can( $permissions ) ) { if ( ! wp_doing_ajax() ) { wp_die( esc_html__( 'You do not have permission to optimize images.', 'ewww-image-optimizer' ) ); } ewwwio_ob_clean(); wp_die( wp_json_encode( array( 'error' => esc_html__( 'You do not have permission to optimize images.', 'ewww-image-optimizer' ) ) ) ); } // Make sure function wasn't called without an attachment to work with. if ( ! isset( $_REQUEST['ewww_attachment_ID'] ) ) { if ( ! wp_doing_ajax() ) { wp_die( esc_html__( 'No attachment ID was provided.', 'ewww-image-optimizer' ) ); } ewwwio_ob_clean(); wp_die( wp_json_encode( array( 'error' => esc_html__( 'No attachment ID was provided.', 'ewww-image-optimizer' ) ) ) ); } // Sanitize the attachment $id. $id = (int) $_REQUEST['ewww_attachment_ID']; if ( empty( $_REQUEST['ewww_manual_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_REQUEST['ewww_manual_nonce'] ), "ewww-manual-$id" ) ) { if ( ! wp_doing_ajax() ) { wp_die( esc_html__( 'Access denied.', 'ewww-image-optimizer' ) ); } ewwwio_ob_clean(); wp_die( wp_json_encode( array( 'error' => esc_html__( 'Access denied.', 'ewww-image-optimizer' ) ) ) ); } ewww_image_optimizer_cloud_restore_from_meta_data( $id, 'nextcell' ); $success = $this->ewww_manage_image_custom_column( 'ewww_image_optimizer', $id, true ); ewwwio_ob_clean(); wp_die( wp_json_encode( array( 'success' => $success ) ) ); } /** * Optimize a nextcellent image by ID. * * @global object $ewww_image Contains more information about the image currently being processed. * @global object nggdb * * @param int $id The ID number of the image. * @return array { * The optimization results for the image. * * @type array $fres The optimization results for the full-size image. * @type array $tres The optimization results for the thumbnail. * } */ public function ewww_ngg_optimize( $id ) { global $ewww_image; // Need this file to work with metadata. require_once WP_CONTENT_DIR . '/plugins/nextcellent-gallery-nextgen-legacy/lib/meta.php'; // Retrieve the metadata for the image. $meta = new nggMeta( $id ); // Retrieve the image path. $file_path = $meta->image->imagePath; $ewww_image = new EWWW_Image( $id, 'nextcell', $file_path ); $ewww_image->resize = 'full'; // Run the optimizer on the current image. $fres = ewww_image_optimizer( $file_path, 2, false, false, true ); // Get the filepath of the thumbnail image. $thumb_path = $meta->image->thumbPath; $ewww_image = new EWWW_Image( $id, 'nextcell', $thumb_path ); $ewww_image->resize = 'thumbnail'; // Run the optimization on the thumbnail. $tres = ewww_image_optimizer( $thumb_path, 2, false, true ); return array( $fres, $tres ); } /** * Adds the Image Optimizer column via the ngg_manage_images_columns hook. * * @param array $columns A list of columns to display in the images table. * @return array The updated list of columns. */ public function ewww_manage_images_columns( $columns ) { $columns['ewww_image_optimizer'] = esc_html__( 'Image Optimizer', 'ewww-image-optimizer' ); return $columns; } /** * Displays the Image Optimizer data via the ngg_manage_image_custom_column hook. * * @param string $column_name The name of the current column. * @param int $id The ID number of the current image. * @param bool $return_output Return the output instead of sending it straight to the screen. * @return string The output when $return is true. */ public function ewww_manage_image_custom_column( $column_name, $id, $return_output = false ) { // Once we've found our custom column. if ( 'ewww_image_optimizer' === $column_name ) { if ( $return_output ) { ob_start(); } // Need this file to work with metadata. require_once WP_CONTENT_DIR . '/plugins/nextcellent-gallery-nextgen-legacy/lib/meta.php'; // Get the metadata for the image. $meta = new nggMeta( $id ); echo '
' . esc_html__( 'You do not appear to have uploaded any images yet.', 'ewww-image-optimizer' ) . '
'; return; } ?>
' . esc_html__( 'Optimizing', 'ewww-image-optimizer' ) . "
"; } else { $output['results'] = '' . esc_html__( 'Optimizing', 'ewww-image-optimizer' ) . ' ' . $file_name . "
"; } ewwwio_ob_clean(); wp_die( wp_json_encode( $output ) ); } /** * Retrieve the filename of the image being optimized. * * @param int $id The ID number of the image. */ public function ewww_ngg_bulk_filename( $id ) { // Need this file to work with metadata. require_once WP_CONTENT_DIR . '/plugins/nextcellent-gallery-nextgen-legacy/lib/meta.php'; // Get the meta for the image. $meta = new nggMeta( $id ); // Get the filename for the image, and output our current status. $file_name = esc_html( $meta->image->filename ); if ( $file_name ) { return $file_name; } else { return false; } } /** * Process each image in the bulk queue. * * @global bool $ewww_defer Set to false to avoid deferring image optimization. */ public function ewww_ngg_bulk_loop() { global $ewww_defer; $ewww_defer = false; $output = array(); $permissions = apply_filters( 'ewww_image_optimizer_bulk_permissions', '' ); if ( empty( $_REQUEST['ewww_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_REQUEST['ewww_wpnonce'] ), 'ewww-image-optimizer-bulk' ) || ! current_user_can( $permissions ) ) { $outupt['error'] = esc_html__( 'Access token has expired, please reload the page.', 'ewww-image-optimizer' ); ewwwio_ob_clean(); wp_die( wp_json_encode( $output ) ); } session_write_close(); // Find out if our nonce is on it's last leg/tick. $tick = wp_verify_nonce( sanitize_key( $_REQUEST['ewww_wpnonce'] ), 'ewww-image-optimizer-bulk' ); if ( 2 === $tick ) { $output['new_nonce'] = wp_create_nonce( 'ewww-image-optimizer-bulk' ); } else { $output['new_nonce'] = ''; } // Find out what time we started, in microseconds. $started = microtime( true ); // Get the list of attachments remaining from the db. $attachments = get_option( 'ewww_image_optimizer_bulk_ngg_attachments' ); $id = array_shift( $attachments ); list( $fres, $tres ) = $this->ewww_ngg_optimize( $id ); if ( 'exceeded' === get_transient( 'ewww_image_optimizer_cloud_status' ) ) { $output['error'] = '' . esc_html__( 'License Exceeded', 'ewww-image-optimizer' ) . ''; ewwwio_ob_clean(); wp_die( wp_json_encode( $output ) ); } if ( 'exceeded quota' === get_transient( 'ewww_image_optimizer_cloud_status' ) ) { $output['error'] = '' . esc_html__( 'Soft quota reached, contact us for more', 'ewww-image-optimizer' ) . ''; ewwwio_ob_clean(); wp_die( wp_json_encode( $output ) ); } // Output the results of the optimization. if ( $fres[0] ) { $output['results'] = sprintf( '' . esc_html__( 'Optimized image:', 'ewww-image-optimizer' ) . ' %s
', esc_html( $fres[0] ) );
}
/* Translators: %s: The compression results/savings */
$output['results'] .= sprintf( esc_html__( 'Full size - %s', 'ewww-image-optimizer' ) . '
', esc_html( $fres[1] ) );
// Output the results of the thumb optimization.
/* Translators: %s: The compression results/savings */
$output['results'] .= sprintf( esc_html__( 'Thumbnail - %s', 'ewww-image-optimizer' ) . '
', esc_html( $tres[1] ) );
// Output how much time we spent.
$elapsed = microtime( true ) - $started;
/* Translators: %s: localized number of seconds */
$output['results'] .= sprintf( esc_html( _n( 'Elapsed: %s second', 'Elapsed: %s seconds', $elapsed, 'ewww-image-optimizer' ) ) . '
' . esc_html__( 'Optimizing', 'ewww-image-optimizer' ) . " $next_file
"; } else { $output['next_file'] = '' . esc_html__( 'Optimizing', 'ewww-image-optimizer' ) . "
"; } } else { $output['done'] = 1; } ewwwio_ob_clean(); wp_die( wp_json_encode( $output ) ); } /** * Finish the bulk operation. */ public function ewww_ngg_bulk_cleanup() { $permissions = apply_filters( 'ewww_image_optimizer_bulk_permissions', '' ); if ( empty( $_REQUEST['ewww_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_REQUEST['ewww_wpnonce'] ), 'ewww-image-optimizer-bulk' ) || ! current_user_can( $permissions ) ) { ewwwio_ob_clean(); wp_die( esc_html__( 'Access token has expired, please reload the page.', 'ewww-image-optimizer' ) ); } // Reset all the bulk options in the db... update_option( 'ewww_image_optimizer_bulk_ngg_resume', '' ); update_option( 'ewww_image_optimizer_bulk_ngg_attachments', '', false ); // and let the user know we are done. ewwwio_ob_clean(); wp_die( '' . esc_html__( 'Finished Optimization!', 'ewww-image-optimizer' ) . '
' ); } /** * Prepare javascript for one-click actions on manage gallery page. * * @param string $hook The hook value for the current page. */ public function ewww_ngg_manual_actions_script( $hook ) { if ( 'galleries_page_nggallery-manage' !== $hook ) { return; } if ( ! current_user_can( apply_filters( 'ewww_image_optimizer_manual_permissions', '' ) ) ) { return; } add_thickbox(); wp_enqueue_script( 'ewwwnextcellentscript', plugins_url( '/includes/nextcellent.js', EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE ), array( 'jquery' ), EWWW_IMAGE_OPTIMIZER_VERSION ); wp_enqueue_style( 'jquery-ui-tooltip-custom', plugins_url( '/includes/jquery-ui-1.10.1.custom.css', EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE ), array(), EWWW_IMAGE_OPTIMIZER_VERSION ); // Submit a couple variables needed for javascript functions. $loading_image = plugins_url( '/images/spinner.gif', EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE ); wp_localize_script( 'ewwwnextcellentscript', 'ewww_vars', array( 'optimizing' => '' . esc_html__( 'Optimizing', 'ewww-image-optimizer' ) . "
", 'restoring' => '' . esc_html__( 'Restoring', 'ewww-image-optimizer' ) . "
", ) ); } } global $ewwwngg; $ewwwngg = new EWWW_Nextcellent(); } // End if().