Skip to content

Commit

Permalink
Merge branch 'master' into bmp-to-jpg-or-png
Browse files Browse the repository at this point in the history
  • Loading branch information
nosilver4u committed Aug 29, 2024
2 parents 0bb6104 + c26b9ef commit 03a8bc9
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 50 deletions.
46 changes: 46 additions & 0 deletions bulk.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,8 @@ function ewww_image_optimizer_bulk_preview() {
$fullsize_count = ewww_image_optimizer_aux_images_table_count_pending();
$button_text = esc_attr__( 'Resume previous optimization', 'ewww-image-optimizer' );
}
// Check that quota is reset after purchasing more credits.
ewww_image_optimizer_cloud_verify( ewww_image_optimizer_get_option( 'ewww_image_optimizer_cloud_key' ), false );
// Create the html for the bulk optimize form and status divs.
ewww_image_optimizer_bulk_head_output();
echo '<div id="ewww-bulk-forms">';
Expand Down Expand Up @@ -987,6 +989,25 @@ function ewww_image_optimizer_bulk_async_init() {
session_write_close();
$output = array();

if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_cloud_key' ) ) {
$verify_cloud = ewww_image_optimizer_cloud_verify( ewww_image_optimizer_get_option( 'ewww_image_optimizer_cloud_key' ), false );
if ( 'exceeded' === $verify_cloud ) {
$output['error'] = ewww_image_optimizer_credits_exceeded();
ewwwio_ob_clean();
die( wp_json_encode( $output ) );
}
if ( 'exceeded quota' === $verify_cloud ) {
$output['error'] = ewww_image_optimizer_soft_quota_exceeded();
ewwwio_ob_clean();
die( wp_json_encode( $output ) );
}
if ( 'exceeded subkey' === $verify_cloud ) {
$output['error'] = esc_html__( 'Out of credits', 'ewww-image=optimizer' );
ewwwio_ob_clean();
die( wp_json_encode( $output ) );
}
}

// Update the 'bulk resume' option to show that an operation is in progress.
update_option( 'ewww_image_optimizer_bulk_resume', 'scanning' );
delete_option( 'ewwwio_stop_scheduled_scan' );
Expand Down Expand Up @@ -1031,6 +1052,24 @@ function ewww_image_optimizer_bulk_async_get_status() {
session_write_close();
$output = array();

if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_cloud_key' ) ) {
if ( 'exceeded' === get_transient( 'ewww_image_optimizer_cloud_status' ) ) {
$output['complete'] = ewww_image_optimizer_credits_exceeded();
ewwwio_ob_clean();
die( wp_json_encode( $output ) );
}
if ( 'exceeded quota' === get_transient( 'ewww_image_optimizer_cloud_status' ) ) {
$output['complete'] = ewww_image_optimizer_soft_quota_exceeded();
ewwwio_ob_clean();
die( wp_json_encode( $output ) );
}
if ( 'exceeded subkey' === get_transient( 'ewww_image_optimizer_cloud_status' ) ) {
$output['complete'] = esc_html__( 'Out of credits', 'ewww-image=optimizer' );
ewwwio_ob_clean();
die( wp_json_encode( $output ) );
}
}

$media_queue_running = false;
if ( ewwwio()->background_media->is_process_running() ) {
$media_queue_running = true;
Expand Down Expand Up @@ -2265,6 +2304,13 @@ function ewww_image_optimizer_bulk_loop( $hook = '', $delay = 0 ) {
ewwwio_ob_clean();
die( wp_json_encode( $output ) );
}
if ( 'exceeded subkey' === get_transient( 'ewww_image_optimizer_cloud_status' ) ) {
$output['error'] = esc_html__( 'Out of credits', 'ewww-image-optimizer' );
delete_transient( 'ewww_image_optimizer_bulk_counter_measures' );
delete_transient( 'ewww_image_optimizer_bulk_current_image' );
ewwwio_ob_clean();
die( wp_json_encode( $output ) );
}
}

// Delete a pending record if the optimization failed for whatever reason.
Expand Down
7 changes: 3 additions & 4 deletions classes/class-background-process-image.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ class Background_Process_Image extends Background_Process {
*/
protected function handle() {
if ( \get_option( 'ewww_image_optimizer_pause_queues' ) ) {
\ewwwio_debug_message( 'all queues paused' );
return;
}
if ( \get_option( 'ewww_image_optimizer_pause_image_queue' ) ) {
\ewwwio_debug_message( 'this queue paused' );
return;
}
parent::handle();
Expand Down Expand Up @@ -262,10 +264,7 @@ protected function process_image( $image, $attempts = 0 ) {

// Gotta make sure we don't delete a pending record if the license is exceeded, so the license check goes first.
if ( \ewww_image_optimizer_get_option( 'ewww_image_optimizer_cloud_key' ) ) {
if (
'exceeded' === \get_transient( 'ewww_image_optimizer_cloud_status' ) ||
'exceeded quota' === \get_transient( 'ewww_image_optimizer_cloud_status' )
) {
if ( false !== \strpos( \get_transient( 'ewww_image_optimizer_cloud_status' ), 'exceeded' ) ) {
\delete_transient( 'ewww_image_optimizer_bulk_counter_measures' );
\delete_transient( 'ewww_image_optimizer_bulk_current_image' );
\update_option( 'ewww_image_optimizer_pause_image_queue', true, false );
Expand Down
1 change: 1 addition & 0 deletions classes/class-background-process-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class Background_Process_Media extends Background_Process {
*/
protected function handle() {
if ( \get_option( 'ewww_image_optimizer_pause_queues' ) ) {
\ewwwio_debug_message( 'all queues paused' );
return;
}

Expand Down
20 changes: 16 additions & 4 deletions classes/class-background-process.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,22 +224,31 @@ public function delete( $key ) {
public function maybe_handle() {
session_write_close();

\ewwwio_debug_message( '<b>' . __METHOD__ . '()</b>' );
\ewwwio_debug_message( "$this->identifier checking for valid nonce" );
\check_ajax_referer( $this->identifier, 'nonce' );

if ( ! empty( $_REQUEST['lock_key'] ) ) {
$this->lock_key = \sanitize_text_field( \wp_unslash( $_REQUEST['lock_key'] ) );
}
\ewwwio_debug_message( "nonce was valid, lock key is $this->lock_key" );

if ( $this->is_process_running() && ! $this->is_key_valid() ) {
// Background process already running.
\ewwwio_debug_message( 'background process already running and the submitted lock key is not the active/valid key' );
die;
}

\ewwwio_debug_message( 'not already running, checking queue' );

if ( $this->is_queue_empty() ) {
// No data to process.
\ewwwio_debug_message( 'nothing in the queue, bye!' );
die;
}

\ewwwio_debug_message( 'queue has items, lets handle them...' );

$this->handle();

die;
Expand Down Expand Up @@ -273,6 +282,7 @@ protected function is_queue_empty() {
* @return bool
*/
public function is_process_running() {
\ewwwio_debug_message( '<b>' . __METHOD__ . '()</b>' );
if ( $this->get_process_lock() ) {
// Process already running.
return true;
Expand Down Expand Up @@ -300,12 +310,12 @@ protected function get_lock_dir() {
/**
* Is disk-based lock valid?
*
* @param string $lock_file Location of the process lock file.
* @return bool True if it is valid, false if it is expired.
*/
protected function is_disk_lock_valid() {
protected function is_disk_lock_valid( $lock_file ) {
\ewwwio_debug_message( '<b>' . __METHOD__ . '()</b>' );
$lock_duration = \apply_filters( $this->identifier . '_queue_lock_time', $this->queue_lock_time );
$lock_file = $this->process_lock_file();
\clearstatcache();
if ( \ewwwio_is_file( $lock_file ) && \time() - \filemtime( $lock_file ) < $lock_duration ) {
\ewwwio_debug_message( 'process lock file in place' );
Expand All @@ -324,8 +334,9 @@ protected function get_process_lock() {
\ewwwio_debug_message( '<b>' . __METHOD__ . '()</b>' );
$db_key = false;
if ( $this->lock_dir ) {
\ewwwio_debug_message( "$this->lock_dir is valid" );
\ewwwio_debug_message( "lock dir is $this->lock_dir" );
$lock_file = $this->process_lock_file();
\ewwwio_debug_message( "checking $lock_file" );
if ( $this->is_disk_lock_valid( $lock_file ) ) {
$db_key = \trim( \file_get_contents( $lock_file ) );
\ewwwio_debug_message( "retrieved lock key: $db_key" );
Expand Down Expand Up @@ -439,7 +450,8 @@ protected function get_batch() {
* within server memory and time limit constraints.
*/
protected function handle() {
$this->start_time = time(); // Set start time of current process.
\ewwwio_debug_message( '<b>' . __METHOD__ . '()</b>' );
$this->start_time = \time(); // Set start time of current process.

do {
$batch = $this->get_batch();
Expand Down
7 changes: 6 additions & 1 deletion classes/class-ewww-flag.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function ewww_flag_bulk() {
<div class="wrap"><h1>GRAND FlAGallery <?php esc_html_e( 'Bulk Optimize', 'ewww-image-optimizer' ); ?></h1>
<?php
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_cloud_key' ) ) {
ewww_image_optimizer_cloud_verify( ewww_image_optimizer_get_option( 'ewww_image_optimizer_cloud_key' ) );
ewww_image_optimizer_cloud_verify( ewww_image_optimizer_get_option( 'ewww_image_optimizer_cloud_key' ), false );
echo '<a id="ewww-bulk-credits-available" target="_blank" class="page-title-action" style="float:right;" href="https://ewww.io/my-account/">' . esc_html__( 'Image credits available:', 'ewww-image-optimizer' ) . ' ' . esc_html( ewww_image_optimizer_cloud_quota() ) . '</a>';
}
if ( ! ewww_image_optimizer_get_option( 'ewww_image_optimizer_backup_files' ) ) {
Expand Down Expand Up @@ -613,6 +613,11 @@ public function ewww_flag_bulk_loop() {
ewwwio_ob_clean();
wp_die( wp_json_encode( $output ) );
}
if ( 'exceeded subkey' === get_transient( 'ewww_image_optimizer_cloud_status' ) ) {
$output['error'] = esc_html__( 'Out of credits', 'ewww-image-optimizer' );
ewwwio_ob_clean();
wp_die( wp_json_encode( $output ) );
}
// Let the user know what happened.
$output['results'] = sprintf( '<p>' . esc_html__( 'Optimized image:', 'ewww-image-optimizer' ) . ' <strong>%s</strong><br>', esc_html( $meta->image->filename ) );
/* Translators: %s: The compression results/savings */
Expand Down
7 changes: 6 additions & 1 deletion classes/class-ewww-nextcellent.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ public function ewww_ngg_bulk_preview() {
<h1><?php esc_html_e( 'Bulk Optimize', 'ewww-image-optimizer' ); ?></h1>
<?php
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_cloud_key' ) ) {
ewww_image_optimizer_cloud_verify( ewww_image_optimizer_get_option( 'ewww_image_optimizer_cloud_key' ) );
ewww_image_optimizer_cloud_verify( ewww_image_optimizer_get_option( 'ewww_image_optimizer_cloud_key' ), false );
echo '<a id="ewww-bulk-credits-available" target="_blank" class="page-title-action" style="float:right;" href="https://ewww.io/my-account/">' . esc_html__( 'Image credits available:', 'ewww-image-optimizer' ) . ' ' . esc_html( ewww_image_optimizer_cloud_quota() ) . '</a>';
}
// Retrieve the value of the 'bulk resume' option and set the button text for the form to use.
Expand Down Expand Up @@ -609,6 +609,11 @@ public function ewww_ngg_bulk_loop() {
ewwwio_ob_clean();
wp_die( wp_json_encode( $output ) );
}
if ( 'exceeded subkey' === get_transient( 'ewww_image_optimizer_cloud_status' ) ) {
$output['error'] = esc_html__( 'Out of credits', 'ewww-image-optimizer' );
ewwwio_ob_clean();
wp_die( wp_json_encode( $output ) );
}
// Output the results of the optimization.
if ( $fres[0] ) {
$output['results'] = sprintf( '<p>' . esc_html__( 'Optimized image:', 'ewww-image-optimizer' ) . ' <strong>%s</strong><br>', esc_html( $fres[0] ) );
Expand Down
12 changes: 11 additions & 1 deletion classes/class-ewww-nextgen.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,16 @@ public function ewww_ngg_manual() {
)
);
}
if ( 'exceeded subkey' === get_transient( 'ewww_image_optimizer_cloud_status' ) ) {
ewwwio_ob_clean();
wp_die(
wp_json_encode(
array(
'error' => esc_html__( 'Out of credits', 'ewww-image-optimizer' ),
)
)
);
}
if ( ! wp_doing_ajax() ) {
// Get the referring page, and send the user back there.
wp_safe_redirect( wp_get_referer() );
Expand Down Expand Up @@ -704,7 +714,7 @@ public function ewww_ngg_bulk_preview() {
<h1 class="wp-heading-inline"><?php esc_html_e( 'Bulk Optimize', 'ewww-image-optimizer' ); ?></h1>
<?php
if ( ewww_image_optimizer_get_option( 'ewww_image_optimizer_cloud_key' ) ) {
ewww_image_optimizer_cloud_verify( ewww_image_optimizer_get_option( 'ewww_image_optimizer_cloud_key' ) );
ewww_image_optimizer_cloud_verify( ewww_image_optimizer_get_option( 'ewww_image_optimizer_cloud_key' ), false );
echo '<span id="ewww-bulk-credits-available">' . esc_html__( 'Image credits available:', 'ewww-image-optimizer' ) . ' ' . wp_kses_post( ewww_image_optimizer_cloud_quota() ) . '</span>';
}
if ( ! ewww_image_optimizer_get_option( 'ewww_image_optimizer_backup_files' ) ) {
Expand Down
Loading

0 comments on commit 03a8bc9

Please sign in to comment.