Plugin Directory

Changeset 2539116


Ignore:
Timestamp:
05/28/2021 03:10:30 PM (5 years ago)
Author:
megaoptim
Message:

Version 1.4.12

Location:
megaoptim-image-optimizer/trunk
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • megaoptim-image-optimizer/trunk/assets/js/megaoptim-processor.js

    r2260564 r2539116  
    134134                $spin.show();
    135135                self.run(index, attachments.length, attachments);
    136             }, 1000);
     136            }, 500);
    137137
    138138        };
     
    192192                                    self.update_couters(response.data);
    193193                                    if (parseInt(response.data['tokens']) === 0) {
     194                                        $(window).off('beforeunload');
    194195                                        window.location.href = window.location.href;
    195196                                    }
     
    197198                                    self.update_row_error(data[index]['ID'], response.data.error);
    198199                                    self.log(response.data, 'log');
    199                                     if (!(response.data['can_continue'] === 1 || response.data['can_continue'] === '1')) {
     200                                    if ( response.data.hasOwnProperty('can_continue') && !(response.data['can_continue'] === 1 || response.data['can_continue'] === '1')) {
    200201                                        self.stop_optimizer();
    201202                                    }
     
    216217                            self.unlock_optimizer();
    217218                            self.run(index, len, data);
    218                         }, 500);
     219                        }, 120);
    219220                    }
    220221                });
  • megaoptim-image-optimizer/trunk/includes/classes/Adapters/MGO_FileLibrary.php

    r2432752 r2539116  
    5353        $result = new MGO_ResultBag();
    5454
    55         //Dont go further if not connected
    56         $profile = MGO_Profile::_is_connected();
    57         if ( ! $profile OR is_null( $this->optimizer ) ) {
    58             throw new MGO_Exception( 'Please make sure you have set up MegaOptim.com API key' );
    59         }
    6055        //Check if attachment is optimized
    6156        $attachment_object = new MGO_FileAttachment( $attachment->path );
     
    6459        if ( $attachment_object->is_processed() ) {
    6560            throw new MGO_Attachment_Already_Optimized_Exception( 'The attachment is already fully optimized.' );
    66         }
    67 
    68         // Bail if no tokens left.
    69         $tokens = $profile->get_tokens_count();
    70         if ( $tokens != - 1 && $tokens <= 0 ) {
    71             throw new MGO_Exception( 'No tokens left. Please top up your account at https://megaoptim.com/dashboard in order to continue.' );
    7261        }
    7362
     
    7665        if ( ! empty( $params ) ) {
    7766            $request_params = array_merge( $request_params, $params );
     67        }
     68
     69        // Check if image exist
     70        if ( ! file_exists( $attachment->path ) ) {
     71            throw new MGO_Exception( __( 'Original image version does not exist on the server.', 'megaoptim-image-optimizer' ) );
    7872        }
    7973
     
    8983        do_action( 'megaoptim_before_optimization', $attachment_object, $request_params );
    9084
    91         //Create Backup If needed
    92         if ( $this->should_backup() ) {
    93             $backup_path = $attachment_object->backup();
    94             $attachment_object->set_backup_path( $backup_path );
    95         }
    96 
    97         // Check if image exist
    98         if ( ! file_exists( $attachment->path ) ) {
    99             throw new MGO_Exception( __( 'Original image version does not exist on the server.', 'megaoptim-image-optimizer' ) );
    100         }
    101 
    10285        try {
    10386            // Grab the resource
     
    10689            $response = $this->optimizer->run( $resource, $request_params );
    10790            $result->add( 'full', $response );
     91
    10892            if ( $response->isError() ) {
    10993                megaoptim_log( $response->getErrors() );
    11094            } else {
    111                 megaoptim_log($response);
     95
     96                megaoptim_log( $response );
     97
     98                //Create Backup If needed
     99                if ( $this->should_backup() ) {
     100                    $backup_path = $attachment_object->backup();
     101                    $attachment_object->set_backup_path( $backup_path );
     102                }
     103
     104                // Save optimized files
    112105                foreach ( $response->getOptimizedFiles() as $file ) {
    113106                    if ( $file->getSavedBytes() > 0 && $file->isSuccessfullyOptimized() ) {
     
    116109                    $result->total_full_size ++;
    117110                    $result->total_saved_bytes += $file->getSavedBytes();
    118                     $webp = $file->getWebP();
    119                     if ( ! is_null( $webp ) ) {
    120                         if ( $webp->getSavedBytes() > 0 ) {
    121                             $webp->saveAsFile( $attachment->path . '.webp' );
    122                         }
    123                     }
     111                    $webp                      = $file->getWebP();
     112                    if ( ! is_null( $webp ) ) {
     113                        if ( $webp->getSavedBytes() > 0 ) {
     114                            $webp->saveAsFile( $attachment->path . '.webp' );
     115                        }
     116                    }
    124117                }
    125118                $attachment_object->set_data( $response, $request_params );
    126119                $attachment_object->set( 'directory', $attachment->directory );
    127120                $attachment_object->save();
     121
    128122                // No need to backup attachments that are already optimized!
    129123                if ( $attachment_object->is_already_optimized() ) {
     
    146140            }
    147141
     142            if ( $result->is_erroneous() ) {
     143                $result->throw_last_error();
     144            }
     145
    148146            $result->set_attachment( $attachment_object );
    149147
     
    270268            $directories = array();
    271269            $excludes    = megaoptim_get_excluded_custom_dir_paths();
    272             $files       = megaoptim_find_images( $directory, true,  $excludes);
     270            $files       = megaoptim_find_images( $directory, true, $excludes );
    273271            foreach ( $files as $file ) {
    274272                array_push( $directories, dirname( $file ) . DIRECTORY_SEPARATOR );
  • megaoptim-image-optimizer/trunk/includes/classes/Adapters/MGO_MediaLibrary.php

    r2432752 r2539116  
    6666    public function optimize( $attachment, $params = array() ) {
    6767
    68         @set_time_limit(0);
     68        @set_time_limit( 0 );
    6969
    7070        $result = new MGO_ResultBag();
    71 
    72         //Don't go further if not connected
    73         $profile = MGO_Profile::_is_connected();
    74         if ( ! $profile OR is_null( $this->optimizer ) ) {
    75             throw new MGO_Exception( 'Please make sure you have set up MegaOptim.com API key' );
    76         }
    7771
    7872        //Check if Attachment is image
     
    9892        if ( $attachment_object->is_processed() ) {
    9993            throw new MGO_Attachment_Already_Optimized_Exception( 'The attachment is already fully optimized.' );
    100         }
    101 
    102         // Bail if no tokens left.
    103         $tokens = $profile->get_tokens_count();
    104         if ( $tokens != - 1 && $tokens <= 0 ) {
    105             throw new MGO_Exception( 'No tokens left. Please top up your account at https://megaoptim.com/dashboard in order to continue.' );
    10694        }
    10795
     
    124112
    125113        //Get the file names
    126         //$original_resource = $this->get_attachment( $attachment, 'full', false );
    127114        $original_path = $this->get_attachment_path( $attachment, 'full', false );
    128115        if ( ! file_exists( $original_path ) ) {
     
    130117        }
    131118
    132         //Create Backup If Enabled
    133         if ( $this->should_backup() ) {
    134             $backup_path = $attachment_object->backup();
    135             $attachment_object->set_backup_path( $backup_path );
    136         }
    137 
    138119        // Optimize the original and the thumbnails
    139120        try {
     121
    140122            megaoptim_log( 'Optimizing MediaLibrary attachment with id ' . $attachment_object->get_id() );
    141123
     124            //Create Backup If Enabled
     125            if ( $this->should_backup() ) {
     126                $backup_path = $attachment_object->backup();
     127                $attachment_object->set_backup_path( $backup_path );
     128            }
     129            // Create resources
    142130            $resources   = array();
    143131            $attachments = array();
    144 
    145132            $attachment_object->maybe_set_metadata();
    146133            $remaining_thumbnails = $attachment_object->get_remaining_thumbnails();
     
    189176                        megaoptim_log( '--- Response: ' . $response->getRawResponse() );
    190177                        foreach ( $attachments as $att ) {
    191                             $file = $response->getResultByFileName( basename( $att['save_path'] ) );
     178                            $filename = basename( $att['save_path'] );
     179                            $file     = $response->getResultByFileName( $filename );
    192180                            if ( ! is_null( $file ) ) {
    193181                                // Save data
     
    224212                                 */
    225213                                do_action( 'megaoptim_size_optimized', $attachment_object, $att['save_path'], $response, $request_params, $size );
     214                            } else {
     215                                megaoptim_log( '--- Saving Response: Response by filename not found. File name: ' . $filename );
    226216                            }
    227217                        }
     
    232222            $attachment_object->save();
    233223            $attachment_object->refresh();
     224
     225            if ( $result->is_erroneous() ) {
     226                $result->throw_last_error();
     227            }
     228
    234229            $result->set_attachment( $attachment_object );
    235 
    236230            do_action( 'megaoptim_attachment_optimized', $attachment_object, $request_params, $result );
    237231
     
    239233        } catch ( Exception $e ) {
    240234            $attachment_object->unlock();
    241             megaoptim_log( '--- Optimizer Exception: ' . $e->getMessage() );
    242             throw new MGO_Exception( $e->getMessage() . ' in ' . $e->getFile() );
     235            megaoptim_log( '--- Optimizer Exception: ' . sprintf( '%s in %s', $e->getMessage(), $e->getFile() ) );
     236            throw new MGO_Exception( $e->getMessage() );
    243237        }
    244238    }
     
    261255
    262256            return;
    263         }
    264 
    265         //Don't go further if not connected
    266         $profile = MGO_Profile::_is_connected();
    267         if ( ! $profile OR is_null( $this->optimizer ) ) {
    268             throw new MGO_Exception( 'Please make sure you have set up MegaOptim.com API key' );
    269257        }
    270258
     
    286274        if ( $attachment_object->is_locked() ) {
    287275            throw new MGO_Attachment_Locked_Exception( 'The attachment is currently being optimized. No need to re-run the optimization.' );
    288         }
    289 
    290         // Bail if no tokens left.
    291         $tokens = $profile->get_tokens_count();
    292         if ( $tokens != - 1 && $tokens <= 0 ) {
    293             throw new MGO_Exception( 'No tokens left. Please top up your account at https://megaoptim.com/dashboard in order to continue.' );
    294276        }
    295277
     
    373355    public function convert_webp( $args ) {
    374356
    375     }
     357    }
    376358
    377359    /**
     
    383365     */
    384366    public function get_images( $args = array() ) {
    385 
    386367
    387368        global $wpdb;
     
    425406        $query       = str_replace( $tag, $tag_count, $query_str );
    426407        $total_items = (int) $wpdb->get_var( $query );
    427         $total_pages = ( $total_items > 0 && $items_per_page > 0 ) ?  ceil(( $total_items / $items_per_page )) : 1;
     408        $total_pages = ( $total_items > 0 && $items_per_page > 0 ) ? ceil( ( $total_items / $items_per_page ) ) : 1;
    428409
    429410        // Setup page offset
     
    795776        $largest_thumbnail_dimensions = $attachment->get_largest_thumbnail_dimensions();
    796777
    797         $max_width  = 0;
    798         $max_height = 0;
    799 
    800         if (isset($params['max_width'])) {
    801             $max_width = intval($params['max_width']);
    802         }
    803         if (isset($params['max_height'])) {
    804             $max_height = intval($params['max_height']);
    805         }
    806 
    807         if ($max_width > 0) {
    808             if ($max_width > $largest_thumbnail_dimensions['width']) {
    809                 $max_width = 0;
    810             }
    811         }
    812 
    813         if ($max_height > 0) {
    814             if ($max_height > $largest_thumbnail_dimensions['height']) {
    815                 $max_height = 0;
    816             }
    817         }
    818 
    819         $params['max_width']  = $max_width;
    820         $params['max_height'] = $max_height;
     778        $max_width  = 0;
     779        $max_height = 0;
     780
     781        if ( isset( $params['max_width'] ) ) {
     782            $max_width = intval( $params['max_width'] );
     783        }
     784        if ( isset( $params['max_height'] ) ) {
     785            $max_height = intval( $params['max_height'] );
     786        }
     787
     788        if ( $max_width > 0 ) {
     789            if ( $max_width > $largest_thumbnail_dimensions['width'] ) {
     790                $max_width = 0;
     791            }
     792        }
     793
     794        if ( $max_height > 0 ) {
     795            if ( $max_height > $largest_thumbnail_dimensions['height'] ) {
     796                $max_height = 0;
     797            }
     798        }
     799
     800        $params['max_width']  = $max_width;
     801        $params['max_height'] = $max_height;
    821802
    822803        return $params;
  • megaoptim-image-optimizer/trunk/includes/classes/Exceptions/MGO_Exception.php

    r1952089 r2539116  
    2323}
    2424
     25/**
     26 * Class MGO_Exception
     27 */
    2528class MGO_Exception extends Exception {
    2629
    27     private $errors = array();
     30    /**
     31     * The errors
     32     * @var array|string[]
     33     */
     34    private $errors;
    2835
    29     public function __construct( $message = "", $code = 0, Exception $previous = null ) {
     36    /**
     37     * MGO_Exception constructor.
     38     *
     39     * @param array $message
     40     * @param int $code
     41     * @param null $previous
     42     */
     43    public function __construct( $message = "", $code = 0, $previous = null ) {
     44
     45        $this->errors = array();
    3046        if ( is_array( $message ) ) {
    3147            $primary_error_message = $message[0];
     
    3854    }
    3955
     56    /**
     57     * Return the errors
     58     * @return array|string[]
     59     */
    4060    public function get_errors() {
    4161        return $this->errors;
  • megaoptim-image-optimizer/trunk/includes/classes/Jobs/MGO_MediaLibrary_Process.php

    r2202391 r2539116  
    7878                    $attachment->save();
    7979                    $attachment->unlock();
     80
    8081                    return false;
    8182                }
     
    9394                    $is_retina  = $_itm['type'] === 'retina';
    9495
    95                     $file = $response->getResultByFileName( basename( $resource ) );
     96                    $filename = basename( $resource );
     97                    $file     = $response->getResultByFileName( $filename );
    9698
    9799                    if ( ! is_null( $file ) ) {
     
    112114                                $webp->saveAsFile( $local_path . '.webp' );
    113115                            }
    114 
    115116                        }
    116117
     
    137138                         */
    138139                        do_action( 'megaoptim_size_optimized', $attachment, $local_path, $response, $request_params, $size );
     140                    } else {
     141                        megaoptim_log( '--- Saving Response: Response by filename not found. File name: '.$filename );
    139142                    }
    140143                }
  • megaoptim-image-optimizer/trunk/includes/classes/MGO_Ajax.php

    r2260564 r2539116  
    112112                if ( isset( $_REQUEST['api_key'] ) && ! empty( $_REQUEST['api_key'] ) ) {
    113113                    try {
    114                         $profile = new MGO_Profile( $_REQUEST['api_key'] );
     114                        $profile = new MGO_Profile( $_REQUEST['api_key'], true );
    115115                        if ( $profile->is_valid_apikey() ) {
    116116                            MGO_Settings::instance()->update( array(
     
    140140
    141141        if ( ! megaoptim_check_referer( MGO_Ajax::NONCE_OPTIMIZER, 'nonce' ) ) {
    142             wp_send_json_error( array( 'error' => __( 'Access denied.', 'megaoptim-image-optimizer' ) ) );
     142            wp_send_json_error( array(
     143                'error'        => __( 'Access denied.', 'megaoptim-image-optimizer' ),
     144                'can_continue' => 0,
     145            ) );
    143146        }
    144147
    145148        if ( ! is_user_logged_in() || ! current_user_can( 'upload_files' ) ) {
    146             wp_send_json_error( array( 'error' => __( 'Access denied.', 'megaoptim-image-optimizer' ) ) );
     149            wp_send_json_error( array(
     150                'error'        => __( 'Access denied.', 'megaoptim-image-optimizer' ),
     151                'can_continue' => 0,
     152            ) );
    147153        }
    148154
    149155        if ( ! isset( $_REQUEST['attachment'] ) ) {
    150             wp_send_json_error( array( 'error' => __( 'No attachment provided.', 'megaoptim-image-optimizer' ) ) );
     156            wp_send_json_error( array(
     157                'error'        => __( 'No attachment provided.', 'megaoptim-image-optimizer' ),
     158                'can_continue' => 1,
     159            ) );
    151160        }
    152161        $attachment_id = $_REQUEST['attachment']['ID'];
     
    303312        if ( isset( $_REQUEST[ MGO_Settings::API_KEY ] ) && ! empty( $_REQUEST[ MGO_Settings::API_KEY ] ) ) {
    304313            try {
    305                 $response = MGO_Profile::get_user_by_api_key( $_REQUEST['megaoptimpt_api_key'] );
    306                 if ( $response === false ) {
    307                     array_push( $errors, __( 'Could not verify your API key. The API can not be reached. Please contact support.', 'megaoptim-image-optimizer' ) );
    308                 } else if ( ! isset( $response['status'] ) ) {
    309                     array_push( $errors, __( 'We received invalid response trying to authenticate your api key. Please contact support.', 'megaoptim-image-optimizer' ) );
    310                 } else if ( $response['status'] != 'ok' ) {
    311                     array_push( $errors, __( 'Your API key is invalid. Please make sure you use correct API issued by MegaOptim.com', 'megaoptim-image-optimizer' ) );
     314                $apikey  = sanitize_text_field( $_REQUEST['megaoptimpt_api_key'] );
     315                $profile = new MGO_Profile( $apikey, true );
     316                if ( ! $profile->is_connected() ) {
     317                    array_push( $errors, __( 'Could not connect to the MegaOptim API. The provided API key is empty or invalid.', 'megaoptim-image-optimizer' ) );
    312318                }
    313319            } catch ( MGO_Exception $e ) {
     
    397403
    398404        // Image sizes
    399         if ( ! isset( $_REQUEST[ MGO_Settings::IMAGE_SIZES ] ) OR ! is_array( $_REQUEST[ MGO_Settings::IMAGE_SIZES ] )
    400              OR count( $_REQUEST[ MGO_Settings::IMAGE_SIZES ] ) === 0
     405        if ( ! isset( $_REQUEST[ MGO_Settings::IMAGE_SIZES ] ) or ! is_array( $_REQUEST[ MGO_Settings::IMAGE_SIZES ] )
     406             or count( $_REQUEST[ MGO_Settings::IMAGE_SIZES ] ) === 0
    401407        ) {
    402408            array_push( $errors, __( 'No image sizes selected. Please select some!', 'megaoptim-image-optimizer' ) );
     
    511517            switch ( $context ) {
    512518                case MEGAOPTIM_TYPE_MEDIA_ATTACHMENT:
    513                     $filters = array();
    514                     foreach (array('date_from', 'date_to') as $key) {
    515                         if (isset($_REQUEST[$key]) && ! empty($_REQUEST[$key])) {
    516                             $filters[$key] = $_REQUEST[$key];
    517                         }
    518                     }
    519                     $filters['page'] = isset($_REQUEST['page']) ? $_REQUEST['page'] : null;
    520                     $filters['per_page'] = isset($_REQUEST['per_page']) ? $_REQUEST['per_page'] : 5000;
    521                     $stats = MGO_MediaLibrary::instance()->get_stats(true, $filters);
    522                     break;
     519                    $filters = array();
     520                    foreach ( array( 'date_from', 'date_to' ) as $key ) {
     521                        if ( isset( $_REQUEST[ $key ] ) && ! empty( $_REQUEST[ $key ] ) ) {
     522                            $filters[ $key ] = $_REQUEST[ $key ];
     523                        }
     524                    }
     525                    $filters['page']     = isset( $_REQUEST['page'] ) ? $_REQUEST['page'] : null;
     526                    $filters['per_page'] = isset( $_REQUEST['per_page'] ) ? $_REQUEST['per_page'] : 5000;
     527                    $stats               = MGO_MediaLibrary::instance()->get_stats( true, $filters );
     528                    break;
    523529                default:
    524530                    $stats = apply_filters( 'megaoptim_library_data', null, $context );
  • megaoptim-image-optimizer/trunk/includes/classes/MGO_CLI.php

    r2260564 r2539116  
    252252                    }
    253253                    \WP_CLI::success( $message );
    254                 } catch ( MGO_Attachment_Already_Optimized_Exception $e ) {
    255                     $message = sprintf( __( 'Attachment already %s optimized. No further optimization needed.', 'megaoptim-image-optimizer' ), $image['ID'] );
    256                     \WP_CLI::success( $message );
    257                 } catch ( MGO_Exception $e ) {
    258                     $message = sprintf( __( 'Attachment %s not optimized. Reason: %s', 'megaoptim-image-optimizer' ), $image['ID'], $e->getMessage() );
    259                     \WP_CLI::warning( $message );
     254                } catch ( \MGO_Exception $e ) {
     255                    if ( $e instanceof MGO_Attachment_Already_Optimized_Exception ) {
     256                        \WP_CLI::success( sprintf( __( 'Attachment %s already optimized. No further optimization needed.', 'megaoptim-image-optimizer' ), $image['ID'] ) );
     257                    } else if ( $e instanceof MGO_Attachment_Locked_Exception ) {
     258                        \WP_CLI::warning( sprintf( __( 'Attachment %s not optimized. Reason: %s', 'megaoptim-image-optimizer' ), $image['ID'], $e->getMessage() ) );
     259                    } else {
     260                        \WP_CLI::warning( sprintf( __( 'Attachment %s not optimized. Reason: %s', 'megaoptim-image-optimizer' ), $image['ID'], $e->getMessage() ) );
     261                        break;
     262                    }
    260263                }
    261264            }
    262265            $time_elapsed_secs = microtime( true ) - $time_start;
    263             WP_CLI::success( sprintf( __( 'Process finished in %s seconds. Total optimized %s, Totaal saved %s', 'megaoptim-image-optimizer' ), megaoptim_round($time_elapsed_secs, 5), $total_optimized, megaoptim_human_file_size($total_saved) ) );
     266            WP_CLI::success( sprintf( __( 'Process finished in %s seconds. Total optimized %s, Totaal saved %s', 'megaoptim-image-optimizer' ), megaoptim_round( $time_elapsed_secs, 5 ), $total_optimized, megaoptim_human_file_size( $total_saved ) ) );
    264267        }
    265268    }
     
    353356                        megaoptim_regenerate_thumbnails( $attachment_ID, $attachment_path );
    354357                        delete_post_meta( $attachment_ID, '_megaoptim_data' );
    355                         do_action( 'megaoptim_after_restore_attachment', new MGO_MediaAttachment($attachment_ID) );
     358                        do_action( 'megaoptim_after_restore_attachment', new MGO_MediaAttachment( $attachment_ID ) );
    356359                        WP_CLI::success( sprintf( __( 'Attachment %s successfully restored.', 'megaoptim-image-optimizer' ), $attachment_ID ) );
    357360                        $total_restored ++;
  • megaoptim-image-optimizer/trunk/includes/classes/MGO_Profile.php

    r2138495 r2539116  
    9595        $response = wp_remote_post( WP_MEGAOPTIM_API_PROFILE, array(
    9696                'method'      => 'POST',
    97                 'timeout'     => 45,
     97                'timeout'     => 10,
    9898                'redirection' => 5,
    9999                'httpversion' => '1.0',
     
    150150    /**
    151151     * Returns the Tokens count left.
     152     *
     153     * @param $refresh - Refresh if the data is not fresh.
     154     *
    152155     * @return mixed|string
    153156     */
    154     public function get_tokens_count() {
    155         if ( ! $this->fresh ) {
     157    public function get_tokens_count( $refresh = true ) {
     158        if ( ! $this->fresh && $refresh ) {
    156159            $this->refresh();
    157160        }
  • megaoptim-image-optimizer/trunk/includes/classes/MGO_ResultBag.php

    r2138495 r2539116  
    2020
    2121class MGO_ResultBag {
    22 
    2322
    2423    public $total_full_size = 0;
     
    108107        );
    109108    }
     109
     110    /**
     111     * Is errorneous?
     112     */
     113    public function is_erroneous() {
     114        $totalE = 0;
     115        $totalR = 0;
     116        foreach ( $this->responses as $response ) {
     117            if ( $response->isError() ) {
     118                $totalE ++;
     119            }
     120            $totalR ++;
     121        }
     122        return $totalR > 0 && $totalR === $totalE;
     123    }
     124
     125    /**
     126     * Throw the last error.
     127     * @throws MGO_Exception
     128     */
     129    public function throw_last_error() {
     130        $error = null;
     131        foreach ( $this->responses as $response ) {
     132            if ( method_exists( $response, 'isError' ) && $response->isError() ) {
     133                $error = $response;
     134            }
     135        }
     136        if ( ! empty( $error ) ) {
     137            $messages = $error->getErrors();
     138            if ( count( $messages ) > 0 ) {
     139                throw new MGO_Exception( $messages[0], $error->getErrorCode() );
     140            }
     141        }
     142    }
    110143}
  • megaoptim-image-optimizer/trunk/includes/compat/nextgen-gallery/classes/MGO_NGGLibrary.php

    r2202391 r2539116  
    7878        }
    7979
    80         //Dont go further if not connected
    81         $profile = MGO_Profile::_is_connected();
    82         if ( ! $profile OR is_null( $this->optimizer ) ) {
    83             throw new MGO_Exception( 'Please make sure you have set up MegaOptim.com API key' );
    84         }
    8580        //Check if attachment is optimized
    8681        $attachment_object = new MGO_NGGAttachment( $attachment->ID );
     
    9489        if ( $attachment_object->is_processed() ) {
    9590            throw new MGO_Attachment_Already_Optimized_Exception( 'The attachment is already fully optimized.' );
    96         }
    97 
    98         // Bail if no tokens left.
    99         $tokens = $profile->get_tokens_count();
    100         if ( $tokens != -1 && $tokens <= 0 ) {
    101             throw new MGO_Exception( 'No tokens left. Please top up your account at https://megaoptim.com/dashboard in order to continue.' );
    10291        }
    10392
     
    116105         */
    117106        do_action( 'megaoptim_before_optimization', $attachment_object, $request_params );
    118 
    119         //Create Backup If needed
    120         if ( $this->should_backup() ) {
    121             $backup_path = $attachment_object->backup();
    122             $attachment_object->set_backup_path( $backup_path );
    123         }
    124107
    125108        // Check if image exist
     
    140123                megaoptim_log('--- API Errors: ' . $response->getErrors() );
    141124            } else {
     125
     126                //Create Backup If needed
     127                if ( $this->should_backup() ) {
     128                    $backup_path = $attachment_object->backup();
     129                    $attachment_object->set_backup_path( $backup_path );
     130                }
     131
    142132                megaoptim_log( '--- Response: ' . $response->getRawResponse() );
    143133                foreach ( $response->getOptimizedFiles() as $file ) {
     
    169159            }
    170160
     161            if ( $result->is_erroneous() ) {
     162                $result->throw_last_error();
     163            }
     164
    171165            $attachment_object->unlock();
    172166
     
    204198        }
    205199
    206         //Dont go further if not connected
    207         $profile = MGO_Profile::_is_connected();
    208         if ( ! $profile OR is_null( $this->optimizer ) ) {
    209             throw new MGO_Exception( 'Please make sure you have set up MegaOptim.com API key' );
    210         }
    211200        //Check if attachment is optimized
    212201        $attachment_object = new MGO_NGGAttachment( $attachment->ID );
     
    220209        if ( $attachment_object->is_processed() ) {
    221210            throw new MGO_Attachment_Already_Optimized_Exception( 'The attachment is already fully optimized.' );
    222         }
    223 
    224         // Bail if no tokens left.
    225         $tokens = $profile->get_tokens_count();
    226         if ( $tokens != -1 && $tokens <= 0 ) {
    227             throw new MGO_Exception( 'No tokens left. Please top up your account at https://megaoptim.com/dashboard in order to continue.' );
    228211        }
    229212
  • megaoptim-image-optimizer/trunk/includes/hooks/notices.php

    r2260564 r2539116  
    55 */
    66function _megaoptim_notice_user_balance() {
    7     if ( ! is_admin() ) {
     7    if ( ! is_admin() || wp_doing_ajax() ) {
    88        return;
    99    }
    1010    try {
    11         $profile      = new MGO_Profile();
    12         $api_key      = MGO_Settings::instance()->get( MGO_Settings::API_KEY );
    13         $tokens_count = $profile->get_tokens_count();
    14         if ( !empty($api_key) && intval( $tokens_count ) === 0 ) {
    15             $message = sprintf(
    16                 '%s %s %s %s.',
    17                 __( 'Your MegaOptim account is out of optimization tokens. To continue using', 'megaoptim-image-optimizer' ),
    18                 '<strong>' . __( 'MegaOptim Image Optimizer', 'megaoptim-image-optimizer' ) . '</strong>',
    19                 __( 'please top up your account' ),
    20                 '<a target="_blank" href="' . WP_MEGAOPTIM_DASHBOARD_URL . '">' . __( 'here', 'megaoptim-image-optimizer' ) . '</a>'
    21             );
    22             MGO_Admin_Notices::instance()->warning( 'insufficient_balance', $message, 1 );
     11        $api_key = MGO_Settings::instance()->get( MGO_Settings::API_KEY );
     12        if ( ! empty( $api_key ) ) {
     13            $profile      = new MGO_Profile( $api_key );
     14            $tokens_count = $profile->get_tokens_count( false ); // use cached results.
     15            if ( intval( $tokens_count ) === 0 ) {
     16                $message = sprintf(
     17                    '%s %s %s %s.',
     18                    __( 'Your MegaOptim account is out of optimization tokens. To continue using', 'megaoptim-image-optimizer' ),
     19                    '<strong>' . __( 'MegaOptim Image Optimizer', 'megaoptim-image-optimizer' ) . '</strong>',
     20                    __( 'please top up your account' ),
     21                    '<a target="_blank" href="' . WP_MEGAOPTIM_DASHBOARD_URL . '">' . __( 'here', 'megaoptim-image-optimizer' ) . '</a>'
     22                );
     23                MGO_Admin_Notices::instance()->warning( 'insufficient_balance', $message, 1 );
     24            }
    2325        }
    2426    } catch ( \Exception $exception ) {
  • megaoptim-image-optimizer/trunk/megaoptim.php

    r2432757 r2539116  
    66Author: MegaOptim
    77Author URI: https://megaoptim.com
    8 Version: 1.4.11
     8Version: 1.4.12
    99Text Domain: megaoptim-image-optimizer
    1010Domain Path: /languages
     
    1515}
    1616
    17 define( 'WP_MEGAOPTIM_VER', '1.4.11' );
     17define( 'WP_MEGAOPTIM_VER', '1.4.12' );
    1818define( 'WP_MEGAOPTIM_PATH', plugin_dir_path( __FILE__ ) );
    1919define( 'WP_MEGAOPTIM_URL', plugin_dir_url( __FILE__ ) );
     
    2121define( 'WP_MEGAOPTIM_PLUGIN_FILE_PATH', __FILE__ );
    2222
    23 require_once( WP_MEGAOPTIM_PATH . 'includes' . DIRECTORY_SEPARATOR  . 'loader.php' );
     23require_once( WP_MEGAOPTIM_PATH . 'includes' . DIRECTORY_SEPARATOR . 'loader.php' );
  • megaoptim-image-optimizer/trunk/readme.txt

    r2432757 r2539116  
    33Tags: convert webp, webp, optimize images, optimize, images, compress
    44Requires at least: 3.6
    5 Tested up to: 5.6
     5Tested up to: 5.7
    66Requires PHP: 5.3
    7 Stable tag: 1.4.11
     7Stable tag: 1.4.12
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    4343- Compatible with WP Engine, SiteGround and other providers
    4444- Compatbile with localhost and password protected sites
    45 - No credits charged if 5% or less is saved per image
     45- No credits charged if %5 or less is saved per image
    4646- Multisite Support
    4747
     
    138138== Changelog ==
    139139
     140= 1.4.12 =
     141- Performance optimizations in /wp-admin
     142- Performance optimizations in the Bulk optimization process through the admin UI
     143- Imrpoved WP CLI Bulk optimizer. If there is an error like "No tokens left", terminate the process completely
     144- Removed confirm dialog when refreshing optimizer page for insufficient tokens
     145
    140146= 1.4.11 =
    141 - Fix PHP error
     147- Fix error triggered when activating the plugin
    142148
    143149= 1.4.10 =
Note: See TracChangeset for help on using the changeset viewer.