Skip to content

Commit 9edf993

Browse files
committed
Tested 1.0 with new upgraded image optimization flow
1 parent 315183c commit 9edf993

File tree

10 files changed

+68
-37
lines changed

10 files changed

+68
-37
lines changed

components/Optimizer.php

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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 ) {

just-image-optimizer.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<?php
22

33
/*
4-
Plugin Name: Just Image Optimizer BETA
4+
Plugin Name: Just Image Optimizer
55
Description: Compress image files, improve performance and boost your SEO rank using Google Page Speed Insights compression and optimization.
6-
Version: 0.9.2
6+
Tags: image, resize, optimize, optimise, compress, performance, optimisation, optimise JPG, pictures, optimizer, Google Page Speed
7+
Version: 1.0
78
Author: JustCoded
89
License: GPLv2 or later
910
*/
@@ -77,7 +78,7 @@ protected function __construct() {
7778
$loader = new core\PluginLoader();
7879
// init plugin name and version.
7980
self::$plugin_name = __( 'Just Image Optimizer', self::TEXTDOMAIN );
80-
self::$version = '0.910';
81+
self::$version = '1.000';
8182
self::$opt_version = get_option( self::OPT_VERSION );
8283
self::$settings = new models\Settings();
8384
self::$service = services\ImageOptimizerFactory::create();
@@ -89,7 +90,7 @@ protected function __construct() {
8990
new components\Optimizer();
9091

9192
// admin panel option pages.
92-
// we use wp_doing_ajax to prevent version check under ajax
93+
// we use wp_doing_ajax to prevent version check under ajax.
9394
if ( ! wp_doing_ajax() && $loader->check_migrations_available() ) {
9495
new controllers\MigrateController();
9596
} else {

migrations/m0x110.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/**
88
* Class m0x110
99
*/
10-
class m0X110 extends \JustCoded\WP\ImageOptimizer\core\Migration {
10+
class m0x110 extends \JustCoded\WP\ImageOptimizer\core\Migration {
1111
/**
1212
* There are no changes in components structure
1313
*
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
use JustCoded\WP\ImageOptimizer\models;
66

77
/**
8-
* Class m0x910
8+
* Class m1x000
99
*/
10-
class m0X910 extends \JustCoded\WP\ImageOptimizer\core\Migration {
10+
class m1x000 extends \JustCoded\WP\ImageOptimizer\core\Migration {
1111
/**
1212
* There are no changes in components structure
1313
*

models/Log.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,17 @@ public function save_details( $request_id, $attach_id, $stats ) {
140140
global $wpdb;
141141
$table_name = $wpdb->prefix . self::TABLE_IMAGE_LOG_DETAILS;
142142

143+
$settings = \JustImageOptimizer::$settings;
144+
$not_optimized = Media::get_queued_image_sizes( $attach_id );
145+
143146
foreach ( $stats as $size => $file_size ) {
147+
// skip image sizes which we do not optimize by settings or they are optimized already.
148+
if ( ! in_array( $size, $not_optimized, true )
149+
|| ( ! $settings->image_sizes_all && ! in_array( $size, $settings->image_sizes, true ) )
150+
) {
151+
continue;
152+
}
153+
144154
$image_data = image_get_intermediate_size( $attach_id, $size );
145155

146156
$wpdb->insert(

models/Media.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ public function size_limit( array $attach_ids ) {
488488
*
489489
* @return int real image optimization status
490490
*/
491-
public function check_optimization_status( int $attach_id ) {
491+
public static function check_optimization_status( int $attach_id ) {
492492
global $wpdb;
493493
$table_name = $wpdb->prefix . self::TABLE_IMAGE_STATS;
494494

@@ -529,7 +529,7 @@ public function check_optimization_status( int $attach_id ) {
529529
*
530530
* @return string[] image sizes names
531531
*/
532-
public function get_queued_image_sizes( int $attach_id ) {
532+
public static function get_queued_image_sizes( int $attach_id ) {
533533
global $wpdb;
534534
$table_name = $wpdb->prefix . self::TABLE_IMAGE_STATS;
535535
$queued_images = $wpdb->get_col( $wpdb->prepare(

models/Settings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function reset() {
107107
$this->auto_optimize = get_option( self::DB_OPT_AUTO_OPTIMIZE, '1' );
108108
$this->image_limit = get_option( self::DB_OPT_IMAGE_LIMIT, 5 );
109109
$this->size_limit = get_option( self::DB_OPT_SIZE_LIMIT, 10 );
110-
$this->tries_count = get_option( self::DB_OPT_TRIES_COUNT, 3 );
110+
$this->tries_count = get_option( self::DB_OPT_TRIES_COUNT, 5 );
111111
$this->before_regen = get_option( self::DB_OPT_BEFORE_REGEN );
112112
$this->image_sizes_all = get_option( self::DB_OPT_SIZE_CHECKED, '1' );
113113
$this->keep_origin = get_option( self::DB_OPT_KEEP_ORIGIN );

readme.txt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
=== Just Image Optimizer ===
22
Contributors: aprokopenko
3-
Plugin Name: Just Image Optimizer BETA
4-
Version: 0.9
3+
Plugin Name: Just Image Optimizer
4+
Version: 1.0
55
Description: Compress image files, improve performance and boost your SEO rank using Google Page Speed Insights compression and optimization.
6+
Tags: image, resize, optimize, optimise, compress, performance, optimisation, optimise JPG, pictures, optimizer, Google Page Speed
67
Author: JustCoded
78
Author URI: https://justcoded.com
89
Requires at least: 4.4
@@ -29,10 +30,11 @@ https://github.com/justcoded/just-image-optimizer/issues
2930

3031
= Plugin compatibility =
3132

32-
In the upcoming releases, we plan to add compatibility with such plugins :
33+
In the upcoming releases, we plan to add compatibility with such plugins and features:
3334

3435
* [Regenerate Thumbnails](https://wordpress.org/plugins/regenerate-thumbnails/)
3536
* [Just Responsive Images](https://wordpress.org/plugins/just-responsive-images/)
37+
* WordPress MultiSite installation
3638

3739
== Installation ==
3840

@@ -62,5 +64,9 @@ No special actions are required during the upgrade.
6264

6365
== Changelog ==
6466

67+
= 1.0 =
68+
* Upgraded optimization logic to continuosly optimizing images with several tries, due to unstable Google Page Speed API responses.
69+
* Improved Log.
70+
6571
= 0.9 =
6672
* First beta version of the plugin. We still work on compatibility with 3rd party plugins and WordPress MultiSite installation.

views/log/index.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
<th>Service</th>
1818
<th class="num">Image/Size Limits</th>
1919
<th class="num">Attachments #</th>
20-
<th class="num">Files #</th>
21-
<th class="num">Files optim.</th>
22-
<th class="num">Files failed</th>
20+
<th class="num">Img Sizes #</th>
21+
<th class="num">Optimized / Failed</th>
2322
<th class="num">Saved Size</th>
2423
</tr>
2524
</thead>
@@ -40,8 +39,10 @@
4039
<td class="num"><?php echo esc_html( $row[ Log::COL_IMAGE_LIMIT ] ); ?> attachm. / <?php echo esc_html( $row[ Log::COL_SIZE_LIMIT ] ); ?>MB</td>
4140
<td class="num"><?php echo esc_html( $model->attach_count( $request_id ) ); ?></td>
4241
<td class="num"><?php echo esc_html( $total_files = ! empty( $row['total_count'] ) ? $row['total_count'] : '0' ); ?></td>
43-
<td class="num"><?php echo esc_html( $optimized = $model->files_count_stat( $request_id, Log::STATUS_OPTIMIZED ) ); ?></td>
44-
<td class="num"><?php echo max($total_files - $optimized, 0); ?></td>
42+
<td class="num">
43+
<b><?php echo esc_html( $optimized = $model->files_count_stat( $request_id, Log::STATUS_OPTIMIZED ) ); ?></b>
44+
/ <span class="text-danger"><?php echo esc_html( max( $total_files - $optimized, 0 ) ); ?></span>
45+
</td>
4546
<td class="num"><strong><?php echo esc_html( ! empty( $row['total_save'] ) ? size_format( $row['total_save'] ) : '0 B' ); ?></strong></td>
4647
</tr>
4748
<?php endforeach; ?>
@@ -58,9 +59,8 @@
5859
<th>Service</th>
5960
<th class="num">Image/Size Limits</th>
6061
<th class="num">Attachments #</th>
61-
<th class="num">Files #</th>
62-
<th class="num">Files optim.</th>
63-
<th class="num">Files failed</th>
62+
<th class="num">Img Sizes #</th>
63+
<th class="num">Optimized / Failed</th>
6464
<th class="num">Saved Size</th>
6565
</tr>
6666
</tfoot>
@@ -70,4 +70,8 @@
7070
<?php echo paginate_links( $log_store_data['pagination'] ); ?>
7171
</div>
7272
</div>
73-
</div>
73+
</div>
74+
75+
<style type="text/css">
76+
.text-danger { color:#a00; }
77+
</style>

views/optimize/google-page-speed.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
* @var $service \JustCoded\WP\ImageOptimizer\services\GooglePagespeed
66
* @var $media \JustCoded\WP\ImageOptimizer\models\Media
7-
* @var $settings Settings
7+
* @var $settings \JustCoded\WP\ImageOptimizer\models\Settings
88
* @var $attach_ids array
99
*/
1010

@@ -23,7 +23,7 @@
2323
foreach ( $attach_ids as $attach_id ) {
2424
if ( $image_sizes = $media->get_queued_image_sizes( $attach_id ) ) {
2525
foreach ( $image_sizes as $image_size ) {
26-
if( in_array( $image_size, $settings->image_sizes ) ) {
26+
if ( $settings->image_sizes_all || in_array( $image_size, $settings->image_sizes, true ) ) {
2727
if ( $img_src = $service->get_image_proxy_url( $attach_id, $image_size ) ) {
2828
echo '<img src="' . esc_url( $img_src ) . '" />';
2929
}

0 commit comments

Comments
 (0)