Skip to content

Commit 3907778

Browse files
author
Edwin Westerhoud
committed
Support for WP Retina 2x plugin.
1 parent d34f04a commit 3907778

7 files changed

Lines changed: 150 additions & 9 deletions

File tree

src/class-tiny-image.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,14 @@ private function parse_tiny_metadata( $tiny_metadata ) {
114114
if ( $tiny_metadata ) {
115115
foreach ( $tiny_metadata as $size => $meta ) {
116116
if ( ! isset( $this->sizes[ $size ] ) ) {
117-
$this->sizes[ $size ] = new Tiny_Image_Size();
117+
if ( self::is_retina( $size ) && Tiny_Settings::wr2x_active() ) {
118+
$retina_path = wr2x_get_retina(
119+
$this->sizes[ rtrim( $size, '_wr2x' ) ]->filename
120+
);
121+
$this->sizes[ $size ] = new Tiny_Image_Size( $retina_path );
122+
} else {
123+
$this->sizes[ $size ] = new Tiny_Image_Size();
124+
}
118125
}
119126
$this->sizes[ $size ]->meta = $meta;
120127
}
@@ -174,6 +181,43 @@ public function compress() {
174181
return array( 'success' => $success, 'failed' => $failed );
175182
}
176183

184+
public function compress_retina( $size_name, $path ) {
185+
if ( $this->settings->get_compressor() === null || ! $this->file_type_allowed() ) {
186+
return;
187+
}
188+
189+
if ( ! isset( $this->sizes[ $size_name ] ) ) {
190+
$this->sizes[ $size_name ] = new Tiny_Image_Size( $path );
191+
}
192+
$size = $this->sizes[ $size_name ];
193+
194+
if ( ! $size->has_been_compressed() ) {
195+
$size->add_tiny_meta_start();
196+
$this->update_tiny_post_meta();
197+
$compressor = $this->settings->get_compressor();
198+
$preserve = $this->settings->get_preserve_options( $size_name );
199+
200+
try {
201+
$response = $compressor->compress_file( $path, false, $preserve );
202+
$size->add_tiny_meta( $response );
203+
} catch (Tiny_Exception $e) {
204+
$size->add_tiny_meta_error( $e );
205+
}
206+
$this->update_tiny_post_meta();
207+
}
208+
}
209+
210+
public function remove_retina_metadata() {
211+
// Remove metadata from all sizes, as this callback only fires when all
212+
// retina sizes are deleted.
213+
foreach ( $this->sizes as $size_name => $size ) {
214+
if ( self::is_retina( $size_name ) ) {
215+
unset( $this->sizes[ $size_name ] );
216+
}
217+
}
218+
$this->update_tiny_post_meta();
219+
}
220+
177221
public function add_wp_metadata( $size_name, $size ) {
178222
if ( self::is_original( $size_name ) ) {
179223
if ( isset( $size->meta['output'] ) ) {
@@ -414,4 +458,8 @@ public static function get_optimization_statistics( $settings, $result = null )
414458
public static function is_original( $size ) {
415459
return self::ORIGINAL === $size;
416460
}
461+
462+
public static function is_retina( $size ) {
463+
return strrpos( $size, 'wr2x' ) === strlen( $size ) - strlen( 'wr2x' );
464+
}
417465
}

src/class-tiny-plugin.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ public function admin_init() {
113113
$this->get_method( 'add_plugin_links' )
114114
);
115115

116+
add_action( 'wr2x_retina_file_added',
117+
$this->get_method( 'compress_retina_image' ),
118+
10, 3
119+
);
120+
121+
add_action( 'wr2x_retina_file_removed',
122+
$this->get_method( 'remove_retina_image' ),
123+
10, 2
124+
);
125+
116126
add_thickbox();
117127
}
118128

@@ -141,6 +151,18 @@ public function add_plugin_links( $current_links ) {
141151
return array_merge( $additional, $current_links );
142152
}
143153

154+
public function compress_retina_image( $attachment_id, $path, $size_name ) {
155+
if ( $this->settings->compress_wr2x_images() ) {
156+
$tiny_image = new Tiny_Image( $this->settings, $attachment_id );
157+
$tiny_image->compress_retina( $size_name . '_wr2x', $path );
158+
}
159+
}
160+
161+
public function remove_retina_image( $attachment_id, $path ) {
162+
$tiny_image = new Tiny_Image( $this->settings, $attachment_id );
163+
$tiny_image->remove_retina_metadata();
164+
}
165+
144166
public function enqueue_scripts( $hook ) {
145167
wp_enqueue_style( self::NAME .'_admin',
146168
plugins_url( '/css/admin.css', __FILE__ ),

src/class-tiny-settings.php

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ public function admin_init() {
166166
public function image_sizes_notice() {
167167
$this->render_image_sizes_notice(
168168
$_GET['image_sizes_selected'],
169-
isset( $_GET['resize_original'] )
169+
isset( $_GET['resize_original'] ),
170+
isset( $_GET['compress_wr2x'] )
170171
);
171-
172172
exit();
173173
}
174174

@@ -356,12 +356,16 @@ public function render_sizes() {
356356
foreach ( $this->get_sizes() as $size => $option ) {
357357
$this->render_size_checkbox( $size, $option );
358358
}
359+
if ( self::wr2x_active() ) {
360+
$this->render_size_checkbox( 'wr2x', $this->get_wr2x_option() );
361+
}
359362
echo '<br>';
360363
echo '<div id="tiny-image-sizes-notice">';
361364

362365
$this->render_image_sizes_notice(
363366
count( self::get_active_tinify_sizes() ),
364-
self::get_resize_enabled()
367+
self::get_resize_enabled(),
368+
self::compress_wr2x_images()
365369
);
366370

367371
echo '</div>';
@@ -374,6 +378,8 @@ private function render_size_checkbox( $size, $option ) {
374378
if ( Tiny_Image::is_original( $size ) ) {
375379
$label = esc_html__( 'Original image', 'tiny-compress-images' ) . ' (' .
376380
esc_html__( 'overwritten by compressed image', 'tiny-compress-images' ) . ')';
381+
} else if ( Tiny_Image::is_retina( $size ) ) {
382+
$label = esc_html__( 'WP Retina 2x sizes', 'tiny-compress-images' );
377383
} else {
378384
$label = esc_html__( ucfirst( $size ) )
379385
. ' - ' . $option['width'] . 'x' . $option['height'];
@@ -385,11 +391,15 @@ private function render_size_checkbox( $size, $option ) {
385391
echo '</p>';
386392
}
387393

388-
public function render_image_sizes_notice( $active_sizes_count, $resize_original_enabled ) {
394+
public function render_image_sizes_notice(
395+
$active_sizes_count, $resize_original_enabled, $compress_wr2x ) {
389396
echo '<p>';
390397
if ( $resize_original_enabled ) {
391398
$active_sizes_count++;
392399
}
400+
if ( $compress_wr2x ) {
401+
$active_sizes_count *= 2;
402+
}
393403

394404
if ( $active_sizes_count < 1 ) {
395405
esc_html_e(
@@ -405,6 +415,20 @@ public function render_image_sizes_notice( $active_sizes_count, $resize_original
405415
'<strong> at least %s images </strong> for free each month.',
406416
'tiny-compress-images'
407417
), array( 'strong' => array() ) ), $free_images_per_month );
418+
419+
if ( self::wr2x_active() ) {
420+
echo '</p>';
421+
echo '<p>';
422+
esc_html_e(
423+
'If selected, retina sizes will be compressed when generated by WP Retina 2x',
424+
'tiny-compress-images'
425+
);
426+
echo '<br>';
427+
esc_html_e(
428+
'Each retina size will count as an additional compression.',
429+
'tiny-compress-images'
430+
);
431+
}
408432
}
409433
echo '</p>';
410434
}
@@ -659,4 +683,22 @@ public function update_api_key() {
659683
echo json_encode( $status );
660684
exit();
661685
}
686+
687+
public static function wr2x_active() {
688+
return is_plugin_active( 'wp-retina-2x/wp-retina-2x.php' );
689+
}
690+
691+
public function get_wr2x_option() {
692+
$setting = get_option( self::get_prefixed_name( 'sizes' ) );
693+
return array(
694+
'width' => null,
695+
'height' => null,
696+
'tinify' => ( isset( $setting['wr2x'] ) && 'on' === $setting['wr2x'] ),
697+
);
698+
}
699+
700+
public function compress_wr2x_images() {
701+
$option = $this->get_wr2x_option();
702+
return self::wr2x_active() && $option['tinify'];
703+
}
662704
}

src/js/admin.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,18 @@
208208
/* Unfortunately, we need some additional information to display
209209
the correct notice. */
210210
totalSelectedSizes = jQuery('input[name*=tinypng_sizes]:checked').length
211+
compressWr2x = propOf('#tinypng_sizes_wr2x', 'checked')
212+
if (compressWr2x) {
213+
totalSelectedSizes--;
214+
}
215+
211216
var image_count_url = ajaxurl + '?action=tiny_image_sizes_notice&image_sizes_selected=' + totalSelectedSizes
212217
if (propOf('#tinypng_resize_original_enabled', 'checked') && propOf('#tinypng_sizes_0', 'checked')) {
213218
image_count_url += '&resize_original=true'
214219
}
220+
if (compressWr2x) {
221+
image_count_url += '&compress_wr2x=true'
222+
}
215223
jQuery('#tiny-image-sizes-notice').load(image_count_url)
216224
})
217225

src/views/bulk-optimization.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@
6767
}
6868
?>
6969
</p>
70+
<p>
71+
<?php
72+
if ( Tiny_Settings::wr2x_active() ) {
73+
esc_html_e( 'Notice that the WP Retina 2x sizes will not be compressed using this page. You will need to bulk generate the retina sizes separately from the WP Retina 2x page.', 'tiny-compress-images' );
74+
}
75+
?>
76+
</p>
7077
<table class="totals">
7178
<tr>
7279
<td class="item">

src/views/compress-details.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,16 @@
8787
<tr class="<?php echo ( 0 == $i % 2 ) ? 'even' : 'odd' ?>">
8888
<?php
8989
echo '<td>';
90-
echo ( Tiny_Image::is_original( $size_name ) ? esc_html__( 'Original', 'tiny-compress-images' ) : esc_html__( ucfirst( $size_name ) ) );
90+
echo ( Tiny_Image::is_original( $size_name ) ? esc_html__( 'Original', 'tiny-compress-images' ) : esc_html__( ucfirst( rtrim( $size_name, '_wr2x' ) ) ) );
9191
echo ' ';
92-
if ( ! array_key_exists( $size_name, $active_sizes ) ) {
92+
if ( ! array_key_exists( $size_name, $active_sizes ) && ! Tiny_Image::is_retina( $size_name ) ) {
9393
echo '<em>' . esc_html__( '(not in use)', 'tiny-compress-images' ) . '</em>';
94-
} else if ( $size->missing() ) {
94+
} else if ( $size->missing() && ( Tiny_Settings::wr2x_active() || ! Tiny_Image::is_retina( $size_name ) ) ) {
9595
echo '<em>' . esc_html__( '(file removed)', 'tiny-compress-images' ) . '</em>';
9696
} else if ( $size->modified() ) {
9797
echo '<em>' . esc_html__( '(modified after compression)', 'tiny-compress-images' ) . '</em>';
98+
} else if ( Tiny_Image::is_retina( $size_name ) ) {
99+
echo '<em>' . esc_html__( '(WP Retina 2x)', 'tiny-compress-images' ) . '</em>';
98100
} else if ( $size->resized() ) {
99101
printf( '<em>' . esc_html__( '(resized to %dx%d)', 'tiny-compress-images' ) . '</em>', $size->meta['output']['width'], $size->meta['output']['height'] );
100102
}
@@ -110,7 +112,7 @@
110112
} else if ( ! $size->exists() ) {
111113
echo '<td>-</td>';
112114
echo '<td colspan=2><em>' . esc_html__( 'Not present', 'tiny-compress-images' ) . '</em></td>';
113-
} else if ( isset( $size_active[ $size_name ] ) ) {
115+
} else if ( isset( $size_active[ $size_name ] ) || Tiny_Image::is_retina( $size_name ) ) {
114116
echo '<td>' . size_format( $size->filesize(), 1 ) . '</td>';
115117
echo '<td colspan=2><em>' . esc_html__( 'Not compressed', 'tiny-compress-images' ) . '</em></td>';
116118
} else if ( isset( $size_exists[ $size_name ] ) ) {

test/unit/TinyImageTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,16 @@ public function test_get_optimization_statistics() {
121121
Tiny_Image::get_optimization_statistics( new Tiny_Settings(), $wpdb_results )
122122
);
123123
}
124+
125+
public function test_is_retina_for_retina_size() {
126+
$this->assertEquals( true, Tiny_Image::is_retina( 'small_wr2x' ) );
127+
}
128+
129+
public function test_is_retina_for_non_retina_size() {
130+
$this->assertEquals( false, Tiny_Image::is_retina( 'small' ) );
131+
}
132+
133+
public function test_is_retina_for_non_retina_size_with_short_name() {
134+
$this->assertEquals( false, Tiny_Image::is_retina( 'file' ) );
135+
}
124136
}

0 commit comments

Comments
 (0)