@@ -102,10 +102,18 @@ public function auto_optimize() {
102102 update_post_meta ( get_the_ID (), '_just_img_opt_status ' , Media::STATUS_IN_QUEUE );
103103 }
104104 require_once ABSPATH . 'wp-admin/includes/file.php ' ;
105- $ tries = 1 ;
105+ $ tries = 0 ;
106106 do {
107107 $ this ->optimize_images ( $ attach_ids );
108- } while ( \JustImageOptimizer::$ settings ->tries_count > $ tries ++ );
108+
109+ // remove processed attachments from list.
110+ foreach ( $ attach_ids as $ key => $ attach_id ) {
111+ $ status = (int ) get_post_meta ( $ attach_id , '_just_img_opt_status ' );
112+ if ( Media::STATUS_PROCESSED === $ status ) {
113+ unset( $ attach_ids [ $ key ] );
114+ }
115+ }
116+ } while ( ! empty ( $ attach_ids ) && \JustImageOptimizer::$ settings ->tries_count > $ tries ++ );
109117 }
110118
111119 /**
@@ -150,25 +158,27 @@ public function manual_optimize() {
150158 * @return boolean
151159 */
152160 protected function optimize_images ( array $ attach_ids ) {
153- /* @var $wp_filesystem \WP_Filesystem_Direct */
161+ /* @var \WP_Filesystem_Direct $wp_filesystem */
154162 global $ wp_filesystem ;
155- $ media = new Media ();
156- $ log = new Log ();
157- $ before_attach_found = $ attach_ids ;
158- $ attach_ids = $ media ->size_limit ( $ attach_ids );
163+ $ media = new Media ();
164+ $ log = new Log ();
165+ $ attach_ids = $ media ->size_limit ( $ attach_ids );
159166 // add filter for WP_FIlesystem permission.
160167 add_filter ( 'filesystem_method ' , array ( $ this , 'filesystem_direct ' ) );
161168 WP_Filesystem ();
162169 // set statistics and status before replace images.
163170 $ request_id = $ log ->start_request ();
164171
165- foreach ( $ attach_ids as $ attach_id ) {
166- $ optimize_status = $ media ->check_optimization_status ( $ attach_id );
172+ foreach ( $ attach_ids as $ key => $ attach_id ) {
173+ $ optimize_status = (int ) get_post_meta ( $ attach_id , '_just_img_opt_status ' );
174+ if ( Media::STATUS_PROCESSED === $ optimize_status ) {
175+ unset( $ attach_ids [ $ key ] );
176+ continue ;
177+ }
178+
167179 $ file_sizes = $ media ->get_file_sizes ( $ attach_id , 'detailed ' );
168180 $ media ->save_stats ( $ attach_id , $ file_sizes );
169- if ( Media::STATUS_PROCESSED !== $ optimize_status ) {
170- $ log ->save_details ( $ request_id , $ attach_id , $ file_sizes );
171- }
181+ $ log ->save_details ( $ request_id , $ attach_id , $ file_sizes );
172182 update_post_meta ( $ attach_id , '_just_img_opt_status ' , Media::STATUS_IN_PROCESS );
173183 }
174184 // upload images from service.
@@ -188,7 +198,7 @@ protected function optimize_images( array $attach_ids ) {
188198 return false ;
189199 }
190200
191- $ get_path = $ media ->get_uploads_path ();
201+ $ get_path = $ media ->get_uploads_path ();
192202
193203 // process image replacement.
194204 foreach ( $ image_files as $ key => $ file ) {
0 commit comments