@@ -73,6 +73,7 @@ public function set_attachment_in_queue( $post_id ) {
7373 */
7474 public function auto_optimize () {
7575 $ attach_ids = array ();
76+
7677 $ queue_args = array (
7778 'post_type ' => 'attachment ' ,
7879 'post_status ' => 'inherit ' ,
@@ -94,13 +95,25 @@ public function auto_optimize() {
9495 ),
9596 );
9697 $ set_queue = new \WP_Query ( $ queue_args );
98+
9799 while ( $ set_queue ->have_posts () ) {
98100 $ set_queue ->the_post ();
99101 $ attach_ids [] = get_the_ID ();
100102 update_post_meta ( get_the_ID (), '_just_img_opt_status ' , Media::STATUS_IN_QUEUE );
101103 }
102- require ABSPATH . 'wp-admin/includes/file.php ' ;
103- $ this ->optimize_images ( $ attach_ids );
104+ require_once ABSPATH . 'wp-admin/includes/file.php ' ;
105+ $ tries = 0 ;
106+ do {
107+ $ this ->optimize_images ( $ attach_ids );
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 ++ );
104117 }
105118
106119 /**
@@ -117,9 +130,14 @@ public function filesystem_direct() {
117130 */
118131 public function manual_optimize () {
119132 $ attach_id = (int ) $ _POST ['attach_id ' ];
133+ $ model = new Media ();
134+
135+ $ tries = 1 ;
136+ do {
137+ $ this ->optimize_images ( [ $ attach_id ] );
138+ $ optimize_status = $ model ->check_optimization_status ( $ attach_id );
139+ } while ( Media::STATUS_PROCESSED !== $ optimize_status && \JustImageOptimizer::$ settings ->tries_count > $ tries ++ );
120140
121- $ this ->optimize_images ( [ $ attach_id ] );
122- $ model = new Media ();
123141 $ attach_stats = $ model ->get_total_attachment_stats ( $ attach_id );
124142 $ data_statistics = array (
125143 'saving_percent ' => ( ! empty ( $ attach_stats [0 ]->percent ) ? $ attach_stats [0 ]->percent : 0 ),
@@ -136,21 +154,28 @@ public function manual_optimize() {
136154 * Function for optimize images
137155 *
138156 * @param array $attach_ids Attachment ids.
157+ *
158+ * @return boolean
139159 */
140160 protected function optimize_images ( array $ attach_ids ) {
141- /* @var $wp_filesystem \WP_Filesystem_Direct */
161+ /* @var \WP_Filesystem_Direct $wp_filesystem */
142162 global $ wp_filesystem ;
143- $ media = new Media ();
144- $ log = new Log ();
145- $ before_attach_found = $ attach_ids ;
146- $ attach_ids = $ media ->size_limit ( $ attach_ids );
163+ $ media = new Media ();
164+ $ log = new Log ();
165+ $ attach_ids = $ media ->size_limit ( $ attach_ids );
147166 // add filter for WP_FIlesystem permission.
148167 add_filter ( 'filesystem_method ' , array ( $ this , 'filesystem_direct ' ) );
149168 WP_Filesystem ();
150169 // set statistics and status before replace images.
151170 $ request_id = $ log ->start_request ();
152171
153- foreach ( $ attach_ids as $ 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+
154179 $ file_sizes = $ media ->get_file_sizes ( $ attach_id , 'detailed ' );
155180 $ media ->save_stats ( $ attach_id , $ file_sizes );
156181 $ log ->save_details ( $ request_id , $ attach_id , $ file_sizes );
@@ -166,13 +191,14 @@ protected function optimize_images( array $attach_ids ) {
166191 if ( ! $ status || 0 === count ( glob ( $ dir ) ) || empty ( $ image_files ) ) {
167192 foreach ( $ attach_ids as $ attach_id ) {
168193 $ log ->update_status ( $ attach_id , $ request_id , Log::STATUS_REMOVED );
169- $ media ->clean_statistics ( $ attach_id );
194+ $ optimize_status = $ media ->check_optimization_status ( $ attach_id );
195+ update_post_meta ( $ attach_id , '_just_img_opt_status ' , $ optimize_status );
170196 }
171197 $ wp_filesystem ->rmdir ( $ dir , true );
172198 return false ;
173199 }
174200
175- $ get_path = $ media ->get_uploads_path ();
201+ $ get_path = $ media ->get_uploads_path ();
176202
177203 // process image replacement.
178204 foreach ( $ image_files as $ key => $ file ) {
@@ -196,7 +222,8 @@ protected function optimize_images( array $attach_ids ) {
196222 $ file_sizes = $ media ->get_file_sizes ( $ attach_id , 'detailed ' );
197223 $ media ->update_stats ( $ attach_id , $ file_sizes );
198224 $ log ->update_details ( $ request_id , $ attach_id , $ file_sizes );
199- update_post_meta ( $ attach_id , '_just_img_opt_status ' , Media::STATUS_PROCESSED );
225+ $ optimize_status = $ media ->check_optimization_status ( $ attach_id );
226+ update_post_meta ( $ attach_id , '_just_img_opt_status ' , $ optimize_status );
200227 }
201228
202229 $ wp_filesystem ->rmdir ( $ dir , true );
0 commit comments