Skip to content

Commit 983c138

Browse files
authored
Merge pull request #7 from justcoded/develop
v1.0 Added tries in optimization process, optimize only not optimize images
2 parents 62f82c7 + 9edf993 commit 983c138

File tree

14 files changed

+264
-67
lines changed

14 files changed

+264
-67
lines changed

components/Optimizer.php

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

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
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.900';
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
*

migrations/m1x000.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace JustCoded\WP\ImageOptimizer\migrations;
4+
5+
use JustCoded\WP\ImageOptimizer\models;
6+
7+
/**
8+
* Class m1x000
9+
*/
10+
class m1x000 extends \JustCoded\WP\ImageOptimizer\core\Migration {
11+
/**
12+
* There are no changes in components structure
13+
*
14+
* @return bool
15+
*/
16+
public function test() {
17+
// no compatibility issues.
18+
return false;
19+
}
20+
21+
/**
22+
* Update DB table
23+
* update bytes count columns from varchar to bigint.
24+
*
25+
* @return boolean
26+
*/
27+
public function update() {
28+
global $wpdb;
29+
$table_stats = $wpdb->prefix . models\Media::TABLE_IMAGE_STATS;
30+
$table_log_details = $wpdb->prefix . models\Log::TABLE_IMAGE_LOG_DETAILS;
31+
$table_log = $wpdb->prefix . models\Log::TABLE_IMAGE_LOG;
32+
33+
$wpdb->query( "ALTER TABLE `{$table_stats}` CHANGE `bytes_before` `bytes_before` BIGINT(20) NOT NULL;" );
34+
$wpdb->query( "ALTER TABLE `{$table_stats}` CHANGE `bytes_after` `bytes_after` BIGINT(20) NOT NULL;" );
35+
36+
$wpdb->query( "ALTER TABLE `{$table_log_details}` CHANGE `bytes_before` `bytes_before` BIGINT(20) NOT NULL;" );
37+
$wpdb->query( "ALTER TABLE `{$table_log_details}` CHANGE `bytes_after` `bytes_after` BIGINT(20) NOT NULL;" );
38+
39+
return true;
40+
}
41+
}
42+

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: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ class Media extends core\Model {
1313

1414
const TABLE_IMAGE_STATS = 'image_optimize';
1515

16-
const COL_ATTACH_ID = 'attach_id';
17-
const COL_ATTACH_NAME = 'attach_name';
18-
const COL_IMAGE_SIZE = 'image_size';
16+
const COL_ATTACH_ID = 'attach_id';
17+
const COL_ATTACH_NAME = 'attach_name';
18+
const COL_IMAGE_SIZE = 'image_size';
1919
const COL_BYTES_BEFORE = 'bytes_before';
20-
const COL_BYTES_AFTER = 'bytes_after';
20+
const COL_BYTES_AFTER = 'bytes_after';
2121

22-
const STATUS_IN_QUEUE = 1;
23-
const STATUS_IN_PROCESS = 2;
24-
const STATUS_PROCESSED = 3;
22+
const STATUS_IN_QUEUE = 1;
23+
const STATUS_IN_PROCESS = 2;
24+
const STATUS_PROCESSED = 3;
25+
const STATUS_PARTIALY_PROCESSED = 4;
2526

2627
/**
2728
* Arguments query array to use.
@@ -40,7 +41,7 @@ class Media extends core\Model {
4041
/**
4142
* Save attachment stats before optimize
4243
*
43-
* @param int $attach_id Attach ID.
44+
* @param int $attach_id Attach ID.
4445
* @param array $stats Array with stats attachments.
4546
*/
4647
public function save_stats( $attach_id, $stats ) {
@@ -480,4 +481,67 @@ public function size_limit( array $attach_ids ) {
480481
return $attach_ids;
481482
}
482483

484+
/**
485+
* Check optimization table and define what is the real optimization status.
486+
*
487+
* @param int $attach_id Attachment ID to check.
488+
*
489+
* @return int real image optimization status
490+
*/
491+
public static function check_optimization_status( int $attach_id ) {
492+
global $wpdb;
493+
$table_name = $wpdb->prefix . self::TABLE_IMAGE_STATS;
494+
495+
$tried_images = $wpdb->get_var( $wpdb->prepare(
496+
"
497+
SELECT COUNT(`id`)
498+
FROM $table_name
499+
WHERE `attach_id` = %d
500+
",
501+
$attach_id
502+
) );
503+
504+
$failed_images = $wpdb->get_var( $wpdb->prepare(
505+
"
506+
SELECT COUNT(`id`)
507+
FROM $table_name
508+
WHERE `attach_id` = %d
509+
AND `bytes_before` <= `bytes_after`
510+
",
511+
$attach_id
512+
) );
513+
514+
$status = static::STATUS_IN_QUEUE;
515+
if ( $tried_images ) {
516+
$status = static::STATUS_PROCESSED;
517+
if ( $failed_images ) {
518+
$status = static::STATUS_PARTIALY_PROCESSED;
519+
}
520+
}
521+
522+
return $status;
523+
}
524+
525+
/**
526+
* Get queued image sizes, which are not optimized yet.
527+
*
528+
* @param int $attach_id Attachment ID to get image sizes in queue.
529+
*
530+
* @return string[] image sizes names
531+
*/
532+
public static function get_queued_image_sizes( int $attach_id ) {
533+
global $wpdb;
534+
$table_name = $wpdb->prefix . self::TABLE_IMAGE_STATS;
535+
$queued_images = $wpdb->get_col( $wpdb->prepare(
536+
"
537+
SELECT image_size
538+
FROM $table_name
539+
WHERE `attach_id` = %d
540+
AND `bytes_before` <= `bytes_after`
541+
",
542+
$attach_id
543+
) );
544+
return $queued_images;
545+
}
546+
483547
}

models/Settings.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Settings extends core\Model {
1818
const DB_OPT_BEFORE_REGEN = 'joi_before_regen';
1919
const DB_OPT_SIZE_CHECKED = 'joi_image_sizes_all';
2020
const DB_OPT_KEEP_ORIGIN = 'joi_keep_origin';
21+
const DB_OPT_TRIES_COUNT = 'joi_tries_count';
2122

2223
/**
2324
* Optimize all image sizes or not.
@@ -47,6 +48,12 @@ class Settings extends core\Model {
4748
*/
4849
public $image_limit;
4950

51+
/**
52+
* Tries count for optimization request
53+
*
54+
* @var int
55+
*/
56+
public $tries_count;
5057
/**
5158
* Files size limit per one optimization request
5259
*
@@ -80,6 +87,7 @@ class Settings extends core\Model {
8087
'auto_optimize' => 'int',
8188
'image_limit' => 'int',
8289
'size_limit' => 'int',
90+
'tries_count' => 'int',
8391
'before_regen' => 'int',
8492
'keep_origin' => 'int',
8593
);
@@ -99,6 +107,7 @@ public function reset() {
99107
$this->auto_optimize = get_option( self::DB_OPT_AUTO_OPTIMIZE, '1' );
100108
$this->image_limit = get_option( self::DB_OPT_IMAGE_LIMIT, 5 );
101109
$this->size_limit = get_option( self::DB_OPT_SIZE_LIMIT, 10 );
110+
$this->tries_count = get_option( self::DB_OPT_TRIES_COUNT, 5 );
102111
$this->before_regen = get_option( self::DB_OPT_BEFORE_REGEN );
103112
$this->image_sizes_all = get_option( self::DB_OPT_SIZE_CHECKED, '1' );
104113
$this->keep_origin = get_option( self::DB_OPT_KEEP_ORIGIN );
@@ -127,6 +136,7 @@ public function save() {
127136
update_option( self::DB_OPT_AUTO_OPTIMIZE, $this->auto_optimize );
128137
update_option( self::DB_OPT_SIZE_CHECKED, $this->image_sizes_all );
129138
update_option( self::DB_OPT_KEEP_ORIGIN, $this->keep_origin );
139+
update_option( self::DB_OPT_TRIES_COUNT, $this->tries_count );
130140
$this->reset();
131141

132142
flush_rewrite_rules();

0 commit comments

Comments
 (0)