",
)
);
}
/**
* Adds our column to the list for users to toggle via Screen Options.
*
* @param array $columns A list of existing column names.
* @return array The revised list of column names.
*/
public function manage_images_columns( $columns ) {
if ( is_array( $columns ) ) {
$columns['ewww_image_optimizer'] = esc_html__( 'Image Optimizer', 'ewww-image-optimizer' );
}
return $columns;
}
/**
* Filter for ngg_manage_images_number_of_columns hook, changed in NGG 2.0.50ish.
*
* @param int $count The number of columns for the table display.
* @return int The new number of columns.
*/
public function ewww_manage_images_number_of_columns( $count ) {
++$count;
add_filter( "ngg_manage_images_column_{$count}_header", array( $this, 'ewww_manage_images_columns' ) );
add_filter( "ngg_manage_images_column_{$count}_content", array( $this, 'ewww_manage_image_custom_column' ), 10, 2 );
return $count;
}
/**
* Outputs column header via ngg_manage_images_column_x_header hook.
*
* @param array|null $columns List of headers for the table.
* @return array|string The new list of headers, or the single header for EWWW IO.
*/
public function ewww_manage_images_columns( $columns = null ) {
if ( is_array( $columns ) ) {
$columns['ewww_image_optimizer'] = esc_html__( 'Image Optimizer', 'ewww-image-optimizer' );
return $columns;
}
return esc_html__( 'Image Optimizer', 'ewww-image-optimizer' );
}
/**
* Outputs the image optimizer column data via ngg_manage_images_column_x_content hook.
*
* @global object $wpdb
*
* @param string $column_name The name of the current column.
* @param int $id The image id for the current row.
* @return string The column output, potentially echoed instead.
*/
public function ewww_manage_image_custom_column( $column_name, $id ) {
// Once we've found our custom column (newer versions will be blank).
if ( 'ewww_image_optimizer' === $column_name || ! $column_name ) {
ewwwio_debug_message( '' . __METHOD__ . '()' );
ob_start();
if ( is_object( $id ) ) {
$image = $id;
} else {
// Get an image object.
$image = $this->get_ngg_image( $id );
}
echo '
';
}
// Get the absolute path.
$file_path = $this->get_image_abspath( $image, 'full' );
// Get the mimetype of the image.
$type = ewww_image_optimizer_quick_mimetype( $file_path );
if ( ! ewwwio()->tools_initialized && ! ewwwio()->local->os_supported() ) {
ewwwio()->local->skip_tools();
} elseif ( ! ewwwio()->tools_initialized ) {
ewwwio()->tool_init();
}
$tools = ewwwio()->local->check_all_tools();
// Check to see if we have a tool to handle the mimetype detected.
switch ( $type ) {
case 'image/jpeg':
// If jpegtran is missing, tell the user.
if ( $tools['jpegtran']['enabled'] && ! $tools['jpegtran']['path'] ) {
/* translators: %s: name of a tool like jpegtran */
$msg = '
';
}
break;
case 'image/png':
// If the PNG tools are missing, tell the user.
if ( $tools['optipng']['enabled'] && ! $tools['optipng']['path'] ) {
/* translators: %s: name of a tool like jpegtran */
$msg = '
';
}
break;
case 'image/gif':
// If gifsicle is missing, tell the user.
if ( $tools['gifsicle']['enabled'] && ! $tools['gifsicle']['path'] ) {
/* translators: %s: name of a tool like jpegtran */
$msg = '
';
// Otherwise, give the image size, and a link to optimize right now.
} else {
// Display the optimization link with the appropriate text.
echo ' ';
$this->ewww_render_optimize_action_link( $image->pid, null, false, $backup_available );
}
echo '
';
if ( is_object( $id ) ) {
return ob_get_clean();
} else {
ob_end_flush();
}
} // End if().
}
/**
* Output the action link for the custom column.
*
* @global object $wpdb
*
* @param int|string $id The ID number of the nextgen image, or the string 'optimize'.
* @param object $image A nextgen image object.
* @param bool $optimized Optional. True if the image has already been optimized. Default false.
* @param bool $restorable Optional. True if the image can be restored via the API. Default false.
*/
public function ewww_render_optimize_action_link( $id, $image = null, $optimized = false, $restorable = false ) {
if ( ! current_user_can( apply_filters( 'ewww_image_optimizer_manual_permissions', '' ) ) ) {
return;
}
ewwwio_debug_message( '' . __METHOD__ . '()' );
if ( 'optimize' === $id && is_object( $image ) && ! empty( $image->pid ) ) {
$id = $image->pid;
global $wpdb;
$optimized_images = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->ewwwio_images WHERE attachment_id = %d AND gallery = 'nextgen' AND image_size <> 0 ORDER BY orig_size DESC", $id ), ARRAY_A );
if ( ! empty( $optimized_images ) ) {
$optimized = true;
}
}
$ewww_manual_nonce = wp_create_nonce( 'ewww-manual-' . $id );
if ( $optimized ) {
printf(
'%3$s',
(int) $id,
esc_attr( $ewww_manual_nonce ),
esc_html__( 'Re-optimize', 'ewww-image-optimizer' )
);
if ( $restorable ) {
printf(
' %3$s',
(int) $id,
esc_attr( $ewww_manual_nonce ),
esc_html__( 'Restore original', 'ewww-image-optimizer' )
);
}
} else {
printf(
'%3$s',
(int) $id,
esc_attr( $ewww_manual_nonce ),
esc_html__( 'Optimize now!', 'ewww-image-optimizer' )
);
}
}
/**
* Capture the output for the action link(s).
*
* @param int|string $id The ID number of the nextgen image, or the string 'optimize'.
* @param object $image A nextgen image object.
* @return string The output from the action_link function.
*/
public function ewww_render_optimize_action_link_capture( $id, $image = null ) {
ewwwio_debug_message( '' . __METHOD__ . '()' );
ob_start();
$this->ewww_render_optimize_action_link( $id, $image, false, false );
return ob_get_clean();
}
/**
* Append our action link to the list.
*
* @param array $actions A list of actions with to display under the image.
* @return array The updated list of actions.
*/
public function ewww_manage_images_row_actions( $actions ) {
$actions['optimize'] = array( &$this, 'ewww_render_optimize_action_link_capture' );
return $actions;
}
/**
* Output the html for the bulk optimize page.
*/
public function ewww_ngg_bulk_preview() {
if ( ! empty( $_REQUEST['doaction'] ) ) {
if (
empty( $_REQUEST['_wpnonce'] ) ||
(
! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'ngg_bulkgallery' ) &&
! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'ngg_updategallery' )
)
) {
ewwwio_debug_message( 'nonce verify failed' );
return;
}
// If there is no requested bulk action, do nothing.
if ( empty( $_REQUEST['bulkaction'] ) ) {
return;
}
// If there is no media to optimize, do nothing.
if ( ! is_array( $_REQUEST['doaction'] ) ) {
return;
}
}
list( $fullsize_count, $resize_count ) = ewww_image_optimizer_count_optimized( 'ngg' );
// Make sure there are some attachments to process.
if ( $fullsize_count < 1 ) {
echo '
' . esc_html__( 'You do not appear to have uploaded any images yet.', 'ewww-image-optimizer' ) . '
' . esc_html__( 'Bulk Optimization will alter your original images and cannot be undone. Please be sure you have a backup of your images before proceeding.', 'ewww-image-optimizer' ) . '
';
}
// Retrieve the value of the 'bulk resume' option and set the button text for the form to use.
$resume = get_option( 'ewww_image_optimizer_bulk_ngg_resume' );
if ( empty( $resume ) ) {
$button_text = __( 'Start optimizing', 'ewww-image-optimizer' );
} else {
$button_text = __( 'Resume previous optimization', 'ewww-image-optimizer' );
}
$delay = ewww_image_optimizer_get_option( 'ewww_image_optimizer_delay' ) ? ewww_image_optimizer_get_option( 'ewww_image_optimizer_delay' ) : 0;
/* translators: 1-4: number(s) of images */
$selected_images_text = sprintf( __( '%1$d images have been selected, with %2$d resized versions.', 'ewww-image-optimizer' ), $fullsize_count, $resize_count );
?>
";
}
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.
* @return string|bool The name of the current file or false.
*/
public function ewww_ngg_bulk_filename( $id ) {
// Get the filename for the image, and output our current status.
$file_path = esc_html( $this->get_image_abspath( $id, 'full' ) );
if ( ! empty( $file_path ) ) {
return $file_path;
} 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 ) ) {
$output['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 );
// Get an image object.
$image = $this->get_ngg_image( $id );
$image = $this->ewww_added_new_image( $image );
$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' );
ewwwio_ob_clean();
wp_die( wp_json_encode( $output ) );
}
// Output the results of the optimization.
$output['results'] = sprintf( '
' . esc_html__( 'Optimized image:', 'ewww-image-optimizer' ) . ' %s ', esc_html( wp_basename( $this->get_image_abspath( $image, 'full' ) ) ) );
if ( ewww_image_optimizer_iterable( $this->bulk_sizes ) ) {
foreach ( $this->bulk_sizes as $size => $results_msg ) {
if ( 'backup' === $size ) {
continue;
} elseif ( 'full' === $size ) {
/* Translators: %s: The compression results/savings */
$output['results'] .= sprintf( esc_html__( 'Full size - %s', 'ewww-image-optimizer' ) . ' ', esc_html( $results_msg ) );
} elseif ( 'thumbnail' === $size ) {
// 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( $results_msg ) );
} else {
// Output savings for any other sizes, if they ever exist...
$output['results'] .= ucfirst( $size ) . ' - ' . esc_html( $results_msg ) . ' ';
}
}
$this->bulk_sizes = array();
}
// Output how much time we spent.
$elapsed = microtime( true ) - $started;
/* Translators: %s: The localized number of seconds */
$output['results'] .= sprintf( esc_html( _n( 'Elapsed: %s second', 'Elapsed: %s seconds', $elapsed, 'ewww-image-optimizer' ) ) . '
', number_format_i18n( $elapsed, 2 ) );
$output['completed'] = 1;
// Store the list back in the db.
update_option( 'ewww_image_optimizer_bulk_ngg_attachments', $attachments, false );
if ( ! empty( $attachments ) ) {
$next_attachment = array_shift( $attachments );
$next_file = $this->ewww_ngg_bulk_filename( $next_attachment );
$loading_image = plugins_url( '/images/wpspin.gif', EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE );
if ( $next_file ) {
$output['next_file'] = '