Skip to content

Commit 2f1f5c3

Browse files
authored
Merge pull request #8 from justcoded/develop
Compatibility with JIO, Regenerate thumbnails
2 parents 983c138 + d798691 commit 2f1f5c3

File tree

11 files changed

+84
-43
lines changed

11 files changed

+84
-43
lines changed

components/MediaInfo.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,34 @@ public function __construct() {
2424
add_action( 'admin_print_scripts-upload.php', array( $this, 'registerAssets' ) );
2525
add_action( 'admin_print_scripts-post.php', array( $this, 'registerAssets' ) );
2626
add_action( 'add_meta_boxes_attachment', array( $this, 'add_optimize_meta_boxes' ), 99 );
27-
add_action( 'wp_ajax_regeneratethumbnail', array( $this, 'clean_stats' ), 5 );
2827

28+
// clean stats on regenerate thumbnails regen.
29+
add_filter( 'wp_generate_attachment_metadata', array( $this, 'clean_stats' ), 99, 2 );
2930
}
3031

3132
/**
3233
* Clean attachment statistics with Regenerate Thumbnails
34+
*
35+
* @param array $metadata Attachment new metadata.
36+
* @param int $attachment_id Attachment ID.
37+
*
38+
* @return array
3339
*/
34-
public function clean_stats() {
35-
if ( ! empty( $_REQUEST['id'] ) ) {
36-
$attach_id = (int) $_REQUEST['id'];
40+
public function clean_stats( $metadata, $attachment_id ) {
41+
// Regenerate thumbnails has no any hooks, we can define this only by REST_REQUEST.
42+
if ( defined( 'REST_REQUEST' )
43+
&& ! empty( $attachment_id )
44+
&& (
45+
// In different cases it send POST or GET request, so we check some keys it submit.
46+
isset( $_POST['regeneration_args'] )
47+
|| isset( $_GET['only_regenerate_missing_thumbnails'] )
48+
|| ( ! empty( $_SERVER['REQUEST_URI'] ) && false !== strpos( $_SERVER['REQUEST_URI'], 'regenerate-thumbnails' ) )
49+
)
50+
) {
3751
$media = new Media();
38-
$media->clean_statistics( $attach_id );
52+
$media->clean_statistics( $attachment_id );
3953
}
54+
return $metadata;
4055
}
4156

4257
/**

components/Optimizer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ public function manual_optimize() {
141141
$attach_stats = $model->get_total_attachment_stats( $attach_id );
142142
$data_statistics = array(
143143
'saving_percent' => ( ! empty( $attach_stats[0]->percent ) ? $attach_stats[0]->percent : 0 ),
144-
'saving_size' => ( ! empty( $attach_stats[0]->saving_size ) ? size_format( $attach_stats[0]->saving_size ) : 0 ),
145-
'total_size' => ( ! empty( $attach_stats[0]->disk_usage ) ? size_format( $attach_stats[0]->disk_usage ) : 0 ),
144+
'saving_size' => ( ! empty( $attach_stats[0]->saving_size ) ? jio_size_format( $attach_stats[0]->saving_size ) : 0 ),
145+
'total_size' => ( ! empty( $attach_stats[0]->disk_usage ) ? jio_size_format( $attach_stats[0]->disk_usage ) : 0 ),
146146
'count_images' => $model->get_count_images( $attach_id ),
147147
);
148148
header( 'Content-Type: application/json; charset=' . get_bloginfo( 'charset' ) );

functions.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,20 @@ function sanitize_float( $number ) {
2525
return (float) $number;
2626
}
2727
}
28+
29+
if ( ! function_exists( 'jio_size_format' ) ) {
30+
/**
31+
* Print file size friendly number with 1 decimal for MB/GB and 0 decimals for KB.
32+
*
33+
* @param int $bytes Bytes size.
34+
*
35+
* @return string Friendly file size.
36+
*/
37+
function jio_size_format( $bytes ) {
38+
$size = size_format( $bytes, 1 );
39+
if ( false !== strpos( $size, 'KB' ) ) {
40+
$size = size_format( $bytes, 0 );
41+
}
42+
return $size;
43+
}
44+
}

just-image-optimizer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
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.
66
Tags: image, resize, optimize, optimise, compress, performance, optimisation, optimise JPG, pictures, optimizer, Google Page Speed
7-
Version: 1.0
7+
Version: 1.1
88
Author: JustCoded
99
License: GPLv2 or later
1010
*/
@@ -78,7 +78,7 @@ protected function __construct() {
7878
$loader = new core\PluginLoader();
7979
// init plugin name and version.
8080
self::$plugin_name = __( 'Just Image Optimizer', self::TEXTDOMAIN );
81-
self::$version = '1.000';
81+
self::$version = '1.100';
8282
self::$opt_version = get_option( self::OPT_VERSION );
8383
self::$settings = new models\Settings();
8484
self::$service = services\ImageOptimizerFactory::create();

models/Media.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,18 @@ public function get_file_sizes( $id, $type = 'detailed' ) {
269269
WP_Filesystem();
270270
$total_size = 0;
271271
$sizes_array = array();
272-
$attachments = wp_get_attachment_metadata( $id );
272+
$meta = wp_get_attachment_metadata( $id );
273273
$get_path = $this->get_uploads_path();
274-
if ( ! $attachments ) {
274+
if ( ! $meta ) {
275275
return 0;
276276
}
277-
foreach ( $attachments['sizes'] as $size_key => $attachment ) {
278-
foreach ( $get_path as $path ) {
279-
if ( $wp_filesystem->exists( $path . '/' . $attachment['file'] ) ) {
280-
$sizes_array[ $size_key ] = $this->get_filesize( $path . '/' . $attachment['file'] );
277+
278+
foreach ( $meta['sizes'] as $size_key => $attachment ) {
279+
if( basename( $meta['file'] ) !== $attachment['file'] ) {
280+
foreach ( $get_path as $path ) {
281+
if ( $wp_filesystem->exists( $path . '/' . $attachment['file'] ) ) {
282+
$sizes_array[ $size_key ] = $this->get_filesize( $path . '/' . $attachment['file'] );
283+
}
281284
}
282285
}
283286
}
@@ -351,7 +354,7 @@ public static function image_dimensions() {
351354
);
352355
}
353356

354-
return $sizes;
357+
return apply_filters( 'jio_settings_image_sizes', $sizes );
355358

356359
}
357360

@@ -448,7 +451,7 @@ public function get_disk_space_size() {
448451
public function size_format_explode( $bytes ) {
449452
$size = array(
450453
'bytes' => $bytes,
451-
'unit' => size_format( $bytes ),
454+
'unit' => jio_size_format( $bytes ),
452455
);
453456

454457
return $size;

readme.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
=== Just Image Optimizer ===
22
Contributors: aprokopenko
33
Plugin Name: Just Image Optimizer
4-
Version: 1.0
4+
Version: 1.1
55
Description: Compress image files, improve performance and boost your SEO rank using Google Page Speed Insights compression and optimization.
66
Tags: image, resize, optimize, optimise, compress, performance, optimisation, optimise JPG, pictures, optimizer, Google Page Speed
77
Author: JustCoded
@@ -30,11 +30,12 @@ https://github.com/justcoded/just-image-optimizer/issues
3030

3131
= Plugin compatibility =
3232

33-
In the upcoming releases, we plan to add compatibility with such plugins and features:
33+
Plugin is compatible with such plugins:
3434

3535
* [Regenerate Thumbnails](https://wordpress.org/plugins/regenerate-thumbnails/)
3636
* [Just Responsive Images](https://wordpress.org/plugins/just-responsive-images/)
37-
* WordPress MultiSite installation
37+
38+
In the upcoming releases, we plan to add compatibility with WordPress MultiSite installation.
3839

3940
== Installation ==
4041

@@ -64,6 +65,10 @@ No special actions are required during the upgrade.
6465

6566
== Changelog ==
6667

68+
= 1.1 =
69+
* Added compatibility with Just Responsive Images plugin (v1.5+)
70+
* Added compatibility with Regenerate Thumbnails plugin (v3+)
71+
6772
= 1.0 =
6873
* Upgraded optimization logic to continuosly optimizing images with several tries, due to unstable Google Page Speed API responses.
6974
* Improved Log.

views/dashboard/index.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
</div>
3535
<div class="column middle">
3636
<h2>Disk Space Saving</h2>
37-
<p><strong><?php echo esc_html($dash_saving_percent); ?>% saved</strong> (<?php echo size_format( $dash_saving_size ); ?>),
38-
disk usage: <?php echo size_format( $model->get_images_disk_usage() ); ?></p>
37+
<p><strong><?php echo esc_html($dash_saving_percent); ?>% saved</strong> (<?php echo jio_size_format( $dash_saving_size ); ?>),
38+
disk usage: <?php echo jio_size_format( $model->get_images_disk_usage() ); ?></p>
3939
<div id="saving" style="height: 300px; width: 95%;"></div>
4040
</div>
4141

views/log/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
<b><?php echo esc_html( $optimized = $model->files_count_stat( $request_id, Log::STATUS_OPTIMIZED ) ); ?></b>
4444
/ <span class="text-danger"><?php echo esc_html( max( $total_files - $optimized, 0 ) ); ?></span>
4545
</td>
46-
<td class="num"><strong><?php echo esc_html( ! empty( $row['total_save'] ) ? size_format( $row['total_save'] ) : '0 B' ); ?></strong></td>
46+
<td class="num"><strong><?php echo esc_html( ! empty( $row['total_save'] ) ? jio_size_format( $row['total_save'] ) : '0 B' ); ?></strong></td>
4747
</tr>
4848
<?php endforeach; ?>
4949
<?php else : ?>

views/log/single-log.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
</td>
3939
<td><?php echo esc_html( $row[ Log::COL_IMAGE_SIZE ] ); ?></td>
4040
<td><?php echo esc_html( $row[ Log::COL_ATTACH_NAME ] ); ?></td>
41-
<td class="num"><?php echo size_format( $row[ Log::COL_BYTES_BEFORE ] ); ?></td>
42-
<td class="num"><?php echo size_format( $row[ Log::COL_BYTES_AFTER ] ); ?></td>
41+
<td class="num"><?php echo jio_size_format( $row[ Log::COL_BYTES_BEFORE ] ); ?></td>
42+
<td class="num"><?php echo jio_size_format( $row[ Log::COL_BYTES_AFTER ] ); ?></td>
4343
<td><?php echo esc_html( $model->get_status_message( $row[ Log::COL_STATUS ] ) ); ?></td>
4444
</tr>
4545
<?php endforeach; ?>

views/media/column.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
<?php if ( Media::STATUS_PROCESSED === $image_status || Media::STATUS_PARTIALY_PROCESSED === $image_status ) : ?>
1515
<?php if ( ! empty( $total_stats[0]->percent ) ) : ?>
1616
<p><?php echo esc_html( $total_stats[0]->percent ); ?>% saved
17-
(<?php echo esc_html( ! empty( $total_stats[0]->saving_size ) ? size_format( $total_stats[0]->saving_size ) : 0 ); ?>)
17+
(<?php echo esc_html( ! empty( $total_stats[0]->saving_size ) ? jio_size_format( $total_stats[0]->saving_size ) : 0 ); ?>)
1818
</p>
19-
<p>disk usage: <?php echo size_format( $total_stats[0]->disk_usage ); ?>
19+
<p>disk usage: <?php echo jio_size_format( $total_stats[0]->disk_usage ); ?>
2020
(<?php echo esc_html( $model->get_count_images( $id ) ); ?> images) </p>
2121
<?php if ( Media::STATUS_PARTIALY_PROCESSED === $image_status ) : ?>
2222
<em>&nbsp; * can be better,

0 commit comments

Comments
 (0)