Skip to content

Commit ac98b98

Browse files
Do not attempt to process non-JPG/PNG images, and don't show any compression info for file types that cannot be compressed.
1 parent 9933b58 commit ac98b98

10 files changed

Lines changed: 107 additions & 80 deletions

src/class-tiny-metadata.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ public function get_name() {
116116
return $this->name;
117117
}
118118

119+
public function can_be_compressed() {
120+
return in_array($this->get_mime_type(), array("image/jpeg", "image/png"));
121+
}
122+
123+
public function get_mime_type() {
124+
return get_post_mime_type($this->id);
125+
}
126+
119127
public function get_filename($size=self::ORIGINAL) {
120128
return isset($this->filenames[$size]) ? $this->filenames[$size] : null;
121129
}

src/class-tiny-plugin.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private function compress($metadata, $attachment_id) {
102102
$mime_type = get_post_mime_type($attachment_id);
103103
$tiny_metadata = new Tiny_Metadata($attachment_id, $metadata);
104104

105-
if ($this->settings->get_compressor() === null || strpos($mime_type, 'image/') !== 0) {
105+
if ($this->settings->get_compressor() === null || !$tiny_metadata->can_be_compressed()) {
106106
return array($tiny_metadata, null);
107107
}
108108

@@ -230,7 +230,7 @@ public function bulk_compress_page() {
230230
echo '<div class="wrap" id="tiny-bulk-compress">';
231231
echo '<h2>' . __('Compress JPEG & PNG Images', 'tiny-compress-images') . '</h2>';
232232
if (empty($_POST['tiny-bulk-compress']) && empty($_REQUEST['ids'])) {
233-
$result = $wpdb->get_results("SELECT COUNT(*) AS `count` FROM $wpdb->posts WHERE post_type = 'attachment' AND post_mime_type LIKE 'image/%' ORDER BY ID DESC", ARRAY_A);
233+
$result = $wpdb->get_results("SELECT COUNT(*) AS `count` FROM $wpdb->posts WHERE post_type = 'attachment' AND (post_mime_type = 'image/jpeg' OR post_mime_type = 'image/png') ORDER BY ID DESC", ARRAY_A);
234234
$image_count = $result[0]['count'];
235235
$sizes_count = count($this->settings->get_active_tinify_sizes());
236236

@@ -266,7 +266,7 @@ public function bulk_compress_page() {
266266
}
267267

268268
// Get all ids and names of the images and not the whole objects which will only fill memory
269-
$items = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'attachment' AND post_mime_type LIKE 'image/%' $cond ORDER BY ID DESC", ARRAY_A);
269+
$items = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'attachment' AND (post_mime_type = 'image/jpeg' OR post_mime_type = 'image/png') $cond ORDER BY ID DESC", ARRAY_A);
270270

271271
echo '<p>';
272272
esc_html_e('Please be patient while the images are being optimized, it can take a while if you have many images. Do not navigate away from this page because it will stop the process.', 'tiny-compress-images');

src/class-tiny-settings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ public function render_total_savings() {
298298
global $wpdb;
299299

300300
$total_savings = 0;
301-
$result = $wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND post_mime_type LIKE 'image/%' ORDER BY ID DESC", ARRAY_A);
301+
$result = $wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND (post_mime_type = 'image/jpeg' OR post_mime_type = 'image/png') ORDER BY ID DESC", ARRAY_A);
302302
for ($i = 0; $i < sizeof($result); $i++) {
303303
$tiny_metadata = new Tiny_Metadata($result[$i]["ID"]);
304304
$savings = $tiny_metadata->get_savings();

src/views/compress-details.php

Lines changed: 54 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,69 @@
11
<div class="details-container">
22
<div class="details" id="tinify-compress-details">
3-
<?php if ($error) { ?>
4-
<span class="icon dashicons dashicons-warning error"></span>
5-
<?php } else if ($missing > 0 || $modified > 0) { ?>
6-
<span class="icon dashicons dashicons-yes alert"></span>
7-
<?php } else if ($tiny_metadata->get_success_count() > 0 && count($uncompressed) > 0) { ?>
8-
<span class="icon dashicons dashicons-yes alert"></span>
9-
<?php } else if ($tiny_metadata->get_success_count() > 0) { ?>
10-
<span class="icon dashicons dashicons-yes success"></span>
11-
<?php } ?>
12-
<span class="icon spinner hidden"></span>
3+
<?php if ($tiny_metadata->can_be_compressed()) { ?>
4+
<?php if ($error) { ?>
5+
<span class="icon dashicons dashicons-warning error"></span>
6+
<?php } else if ($missing > 0 || $modified > 0) { ?>
7+
<span class="icon dashicons dashicons-yes alert"></span>
8+
<?php } else if ($tiny_metadata->get_success_count() > 0 && count($uncompressed) > 0) { ?>
9+
<span class="icon dashicons dashicons-yes alert"></span>
10+
<?php } else if ($tiny_metadata->get_success_count() > 0) { ?>
11+
<span class="icon dashicons dashicons-yes success"></span>
12+
<?php } ?>
13+
<span class="icon spinner hidden"></span>
1314

14-
<?php if ($tiny_metadata->get_compressed_count() > 0 || ($tiny_metadata->get_compressed_count() == 0 && count($uncompressed) == 0)) { ?>
15-
<span class="message">
16-
<strong><?php echo $tiny_metadata->get_compressed_count() ?></strong>
17-
<span>
18-
<?= htmlspecialchars(_n('size compressed', 'sizes compressed', $tiny_metadata->get_compressed_count(), 'tiny-compress-images')) ?>
15+
<?php if ($tiny_metadata->get_compressed_count() > 0 || ($tiny_metadata->get_compressed_count() == 0 && count($uncompressed) == 0)) { ?>
16+
<span class="message">
17+
<strong><?php echo $tiny_metadata->get_compressed_count() ?></strong>
18+
<span>
19+
<?= htmlspecialchars(_n('size compressed', 'sizes compressed', $tiny_metadata->get_compressed_count(), 'tiny-compress-images')) ?>
20+
</span>
1921
</span>
20-
</span>
21-
<br/>
22-
<?php } ?>
22+
<br/>
23+
<?php } ?>
2324

24-
<?php if ($not_compressed_active > 0) { ?>
25-
<span class="message">
26-
<?= htmlspecialchars(sprintf(_n('%d size not compressed', '%d sizes not compressed', $not_compressed_active), $not_compressed_active)) ?>
27-
</span>
28-
<br />
29-
<?php } ?>
25+
<?php if ($not_compressed_active > 0) { ?>
26+
<span class="message">
27+
<?= htmlspecialchars(sprintf(_n('%d size not compressed', '%d sizes not compressed', $not_compressed_active), $not_compressed_active)) ?>
28+
</span>
29+
<br />
30+
<?php } ?>
3031

31-
<?php if ($missing > 0) { ?>
32-
<span class="message">
33-
<?= htmlspecialchars(sprintf(_n('%d file removed', '%d files removed', $missing), $missing)) ?>
34-
</span>
35-
<br />
36-
<?php } ?>
32+
<?php if ($missing > 0) { ?>
33+
<span class="message">
34+
<?= htmlspecialchars(sprintf(_n('%d file removed', '%d files removed', $missing), $missing)) ?>
35+
</span>
36+
<br />
37+
<?php } ?>
3738

38-
<?php if ($modified > 0) { ?>
39-
<span class="message">
40-
<?= htmlspecialchars(sprintf(_n('%d file modified after compression', '%d files modified after compression', $modified), $modified)) ?>
41-
</span>
42-
<br />
43-
<?php } ?>
39+
<?php if ($modified > 0) { ?>
40+
<span class="message">
41+
<?= htmlspecialchars(sprintf(_n('%d file modified after compression', '%d files modified after compression', $modified), $modified)) ?>
42+
</span>
43+
<br />
44+
<?php } ?>
4445

45-
<?php if ($savings["input"] - $savings["output"]) { ?>
46-
<span class="message">
47-
<?php printf(esc_html__('Total savings %s', 'tiny-compress-images'), str_replace(" ", "&nbsp;", size_format($savings["input"] - $savings["output"], 1))) ?>
48-
</span>
49-
<br />
50-
<?php } ?>
46+
<?php if ($savings["input"] - $savings["output"]) { ?>
47+
<span class="message">
48+
<?php printf(esc_html__('Total savings %s', 'tiny-compress-images'), str_replace(" ", "&nbsp;", size_format($savings["input"] - $savings["output"], 1))) ?>
49+
</span>
50+
<br />
51+
<?php } ?>
5152

52-
<?php if ($error) { ?>
53-
<span class="message error_message">
54-
<?= esc_html__('Latest error', 'tiny-compress-images') . ': '. esc_html__($error, 'tiny-compress-images') ?>
55-
</span>
56-
<br/>
57-
<?php } ?>
53+
<?php if ($error) { ?>
54+
<span class="message error_message">
55+
<?= esc_html__('Latest error', 'tiny-compress-images') . ': '. esc_html__($error, 'tiny-compress-images') ?>
56+
</span>
57+
<br/>
58+
<?php } ?>
5859

59-
<?php if ($tiny_metadata->get_compressed_count() > 0) { ?>
60-
<a class="thickbox message" href="#TB_inline?width=700&amp;height=500&amp;inlineId=modal_<?php echo $tiny_metadata->get_id() ?>">Details</a>
60+
<?php if ($tiny_metadata->get_compressed_count() > 0) { ?>
61+
<a class="thickbox message" href="#TB_inline?width=700&amp;height=500&amp;inlineId=modal_<?php echo $tiny_metadata->get_id() ?>">Details</a>
62+
<?php } ?>
6163
<?php } ?>
6264
</div>
6365

64-
<?php if (count($uncompressed) > 0) { ?>
66+
<?php if ($tiny_metadata->can_be_compressed() && count($uncompressed) > 0) { ?>
6567
<button type="button" class="tiny-compress button button-small button-primary" data-id="<?php echo $tiny_metadata->get_id() ?>">
6668
<?= esc_html__('Compress', 'tiny-compress-images') ?>
6769
</button>
@@ -120,4 +122,4 @@
120122
<p><strong><?php printf(esc_html__('Total savings %s', 'tiny-compress-images'), size_format($savings["input"] - $savings["output"], 1)) ?></strong></p>
121123
</div>
122124
</div>
123-
<?php } ?>
125+
<?php } ?>

test/fixtures/input-example.gif

27.9 KB
Loading

test/fixtures/input-example.pdf

6.57 KB
Binary file not shown.

test/integration/BulkCompressIntegrationTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function tearDown() {
1818
}
1919

2020
public function testBulkCompressActionShouldBePresentInMedia() {
21-
$this->upload_image(dirname(__FILE__) . '/../fixtures/input-example.png');
21+
$this->upload_media(dirname(__FILE__) . '/../fixtures/input-example.png');
2222
$this->assertEquals('Optimize Images', self::$driver->findElement(
2323
WebDriverBy::cssSelector('select[name="action"] option[value="tiny_bulk_compress"]')
2424
)->getText());
@@ -29,12 +29,15 @@ private function prepare($normal=1, $large=0) {
2929
$this->enable_compression_sizes(array());
3030

3131
for ($i = 0; $i < $normal; $i++) {
32-
$this->upload_image(dirname(__FILE__) . '/../fixtures/input-example.jpg');
32+
$this->upload_media(dirname(__FILE__) . '/../fixtures/input-example.jpg');
3333
}
3434
for ($i = 0; $i < $large; $i++) {
35-
$this->upload_image(dirname(__FILE__) . '/../fixtures/input-example.png');
35+
$this->upload_media(dirname(__FILE__) . '/../fixtures/input-example.png');
3636
}
3737

38+
$this->upload_media(dirname(__FILE__) . '/../fixtures/input-example.gif');
39+
$this->upload_media(dirname(__FILE__) . '/../fixtures/input-example.pdf');
40+
3841
$this->enable_compression_sizes(array('thumbnail', 'medium', 'large'));
3942
}
4043

0 commit comments

Comments
 (0)