Skip to content

Commit add83c2

Browse files
author
Jacob Middag
committed
Added original as size option and allow no sizes set
1 parent 619cee9 commit add83c2

12 files changed

Lines changed: 93 additions & 26 deletions

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*.log
22
/vendor
33
.DS_Store
4-
4+
/tmp/*
5+
!/tmp/.gitkeep

bin/test-wordpress

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ version="$1"
1616
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
1717
source $DIR/docker-functions
1818

19-
WORDPRESS_VERSION=$version WORDPRESS_DATABASE=wordpress_$version \
20-
MYSQL_ROOT_PASSWORD=root \
21-
HOST_IP=$(boot2docker ip) HOST_PORT=80$version \
22-
WORDPRESS_URL=http://$(boot2docker ip):$HOST_PORT phpunit test/integration
19+
export WORDPRESS_VERSION=$version
20+
export WORDPRESS_DATABASE=wordpress_$version
21+
export MYSQL_ROOT_PASSWORD=root
22+
export HOST_IP=$(boot2docker ip)
23+
export HOST_PORT=80$version
24+
export WORDPRESS_URL=http://$(boot2docker ip):$HOST_PORT
25+
phpunit test/integration

src/class-tiny-plugin.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ public function compress_attachment($metadata, $attachment_id) {
8484
$upload_dir = wp_upload_dir();
8585
$prefix = $upload_dir['basedir'] . '/' . $path_info['dirname'] . '/';
8686

87-
if (!$tiny_metadata->is_compressed()) {
87+
$settings = $this->settings->get_sizes();
88+
89+
if ($settings[Tiny_Metadata::ORIGINAL]['tinify'] && !$tiny_metadata->is_compressed()) {
8890
try {
8991
$response = $this->compressor->compress_file("$prefix${path_info['basename']}");
9092
$tiny_metadata->add_response($response);
@@ -93,7 +95,6 @@ public function compress_attachment($metadata, $attachment_id) {
9395
}
9496
}
9597

96-
$settings = $this->settings->get_sizes();
9798
foreach ($metadata['sizes'] as $size => $info) {
9899
if (isset($settings[$size]) && $settings[$size]['tinify'] && !$tiny_metadata->is_compressed($size)) {
99100
try {
@@ -154,6 +155,7 @@ public function render_media_column($column, $id) {
154155
if ($column === self::MEDIA_COLUMN) {
155156
$wp_metadata = wp_get_attachment_metadata($id);
156157
$wp_sizes = isset($wp_metadata['sizes']) ? array_keys($wp_metadata['sizes']) : array();
158+
$wp_sizes[] = Tiny_Metadata::ORIGINAL;
157159

158160
$sizes = array_intersect($wp_sizes, $this->settings->get_tinify_sizes());
159161

src/class-tiny-settings.php

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
class Tiny_Settings extends Tiny_WP_Base {
2222
const PREFIX = 'tinypng_';
23+
const DUMMY_SIZE = '_tiny_dummy';
2324

2425
protected static function get_prefixed_name($name) {
2526
return self::PREFIX . $name;
@@ -28,14 +29,6 @@ protected static function get_prefixed_name($name) {
2829
private $sizes;
2930
private $tinify_sizes;
3031

31-
public function __construct() {
32-
parent::__construct();
33-
// if (is_multisite()) {
34-
// add_action('network_admin_menu', $this->get_method('register_multisite_settings'));
35-
// add_action('network_admin_edit_save_tinypng_multisite_settings', $this->get_method('save_multisite_settings'), 10, 0);
36-
// }
37-
}
38-
3932
public function admin_init() {
4033
$section = self::get_prefixed_name('settings');
4134
add_settings_section($section, self::translate('PNG and JPEG compression'), $this->get_method('render_section'), 'media');
@@ -100,10 +93,18 @@ public function get_sizes() {
10093
return $this->sizes;
10194
}
10295

103-
$this->sizes = array();
10496
$setting = get_option(self::get_prefixed_name('sizes'));
10597

98+
$size = Tiny_Metadata::ORIGINAL;
99+
$this->sizes = array($size => array(
100+
'width' => null, 'height' => null,
101+
'tinify' => !is_array($setting) || (isset($setting[$size]) && $setting[$size] === 'on'),
102+
));
103+
106104
foreach (get_intermediate_image_sizes() as $size) {
105+
if ($size === self::DUMMY_SIZE) {
106+
continue;
107+
}
107108
list($width, $height) = self::get_intermediate_size($size);
108109
if ($width && $height) {
109110
$this->sizes[$size] = array(
@@ -168,14 +169,24 @@ public function render_api_key() {
168169

169170
public function render_sizes() {
170171
echo '<p>' . self::translate_escape('You can choose to compress different image sizes created by WordPress here') . '.<br/>';
171-
echo self::translate_escape('Remember each additional image size will affect your TinyPNG monthly usage') . "!</p>\n";
172+
echo self::translate_escape('Remember each additional image size will affect your TinyPNG monthly usage') . "!";?>
173+
<input type="hidden" name="<?php echo self::get_prefixed_name('sizes[' . self::DUMMY_SIZE .']'); ?>" value="on"/></p>
174+
<?php
172175
foreach ($this->get_sizes() as $size => $option) {
173-
$id = self::get_prefixed_name("sizes_$size");
174-
$field = self::get_prefixed_name("sizes[$size]");
175-
?>
176+
$this->render_size_checkbox($size, $option);
177+
}
178+
}
179+
180+
private function render_size_checkbox($size, $option) {
181+
$id = self::get_prefixed_name("sizes_$size");
182+
$field = self::get_prefixed_name("sizes[$size]");
183+
if ($size === Tiny_Metadata::ORIGINAL) {
184+
$label = self::translate_escape("original");
185+
} else {
186+
$label = $size . " - ${option['width']}x${option['height']}";
187+
}?>
176188
<p><input type="checkbox" id="<?php echo $id; ?>" name="<?php echo $field ?>" value="on" <?php if ($option['tinify']) { echo ' checked="checked"'; } ?>/>
177-
<label for="<?php echo $id; ?>"><?php echo $size . " - ${option['width']}x${option['height']}"; ?></label></p>
189+
<label for="<?php echo $id; ?>"><?php echo $label; ?></label></p>
178190
<?php
179-
}
180191
}
181192
}
150 Bytes
Binary file not shown.

src/languages/tiny-compress-images-nl_NL.po

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ msgstr "Multisite API-sleutel"
1919
msgid "File compression"
2020
msgstr "Bestandscompressie"
2121

22+
msgid "original"
23+
msgstr "origineel"
24+
2225
msgid "You can choose to compress different image sizes created by WordPress here"
2326
msgstr "Je kunt hier kiezen welke afmetingen je wilt comprimeren van de afbeeldingen"
2427

test/helpers/setup.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,16 @@ function restore_wordpress() {
3636
restore_webservice_url();
3737
}
3838

39+
function mysql_dump_file() {
40+
return dirname(__FILE__) . '/../../tmp/mysqldump_' . getenv('WORDPRESS_DATABASE') . '.sql.gz';
41+
}
42+
3943
function restore_wordpress_site() {
40-
shell_exec('mysql -h ' . getenv('HOST_IP') . ' -u root -p' . getenv('MYSQL_ROOT_PASSWORD') . ' ' . getenv('WORDPRESS_DATABASE') . ' < /tmp/mysqldump_' . getenv('WORDPRESS_DATABASE') . '.sql');
44+
shell_exec('gunzip -c < ' . mysql_dump_file() . ' | mysql -h ' . getenv('HOST_IP') . ' -u root -p' . getenv('MYSQL_ROOT_PASSWORD') . ' ' . getenv('WORDPRESS_DATABASE'));
4145
}
4246

4347
function backup_wordpress_site() {
44-
shell_exec('mysqldump -h ' . getenv('HOST_IP') . ' -u root -p' . getenv('MYSQL_ROOT_PASSWORD') . ' ' . getenv('WORDPRESS_DATABASE') . ' > /tmp/mysqldump_' . getenv('WORDPRESS_DATABASE') . '.sql');
48+
shell_exec('mysqldump -h ' . getenv('HOST_IP') . ' -u root -p' . getenv('MYSQL_ROOT_PASSWORD') . ' ' . getenv('WORDPRESS_DATABASE') . ' | gzip -c > ' . mysql_dump_file());
4549
}
4650

4751
function set_test_webservice_url() {

test/integration/BulkCompressIntegrationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public function setUp() {
99
}
1010

1111
public function tearDown() {
12-
$this->enable_compression_sizes(array('thumbnail', 'medium', 'large', 'post-thumbnail'));
12+
$this->enable_compression_sizes(array('0', 'thumbnail', 'medium', 'large', 'post-thumbnail'));
1313
clear_uploads(self::$driver);
1414
}
1515

test/integration/SettingsIntegrationTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public function testDefaultSizesBeingCompressed() {
3030
$elements = self::$driver->findElements(
3131
WebDriverBy::xpath('//input[@type="checkbox" and starts-with(@name, "tinypng_sizes") and @checked="checked"]'));
3232
$size_ids = array_map('elementName', $elements);
33+
$this->assertContains('tinypng_sizes[0]', $size_ids);
3334
$this->assertContains('tinypng_sizes[thumbnail]', $size_ids);
3435
$this->assertContains('tinypng_sizes[medium]', $size_ids);
3536
$this->assertContains('tinypng_sizes[large]', $size_ids);
@@ -38,13 +39,29 @@ public function testDefaultSizesBeingCompressed() {
3839
public function testShouldPersistSizes() {
3940
$element = self::$driver->findElement(WebDriverBy::id('tinypng_sizes_medium'));
4041
$element->click();
42+
$element = self::$driver->findElement(WebDriverBy::id('tinypng_sizes_0'));
43+
$element->click();
4144
self::$driver->findElement(WebDriverBy::tagName('form'))->submit();
4245

4346
$elements = self::$driver->findElements(
4447
WebDriverBy::xpath('//input[@type="checkbox" and starts-with(@name, "tinypng_sizes") and @checked="checked"]'));
4548
$size_ids = array_map('elementName', $elements);
49+
$this->assertNotContains('tinypng_sizes[0]', $size_ids);
4650
$this->assertContains('tinypng_sizes[thumbnail]', $size_ids);
4751
$this->assertNotContains('tinypng_sizes[medium]', $size_ids);
4852
$this->assertContains('tinypng_sizes[large]', $size_ids);
4953
}
54+
55+
public function testShouldPersistNoSizes() {
56+
$elements = self::$driver->findElements(
57+
WebDriverBy::xpath('//input[@type="checkbox" and starts-with(@name, "tinypng_sizes") and @checked="checked"]'));
58+
foreach ($elements as $element) {
59+
$element->click();
60+
}
61+
self::$driver->findElement(WebDriverBy::tagName('form'))->submit();
62+
63+
$elements = self::$driver->findElements(
64+
WebDriverBy::xpath('//input[@type="checkbox" and starts-with(@name, "tinypng_sizes") and @checked="checked"]'));
65+
$this->assertEquals(0, count(array_map('elementName', $elements)));
66+
}
5067
}

test/unit/TinyPluginTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public function setUp() {
1414

1515
$this->wp->stub('wp_upload_dir', create_function('', 'return array("basedir" => "/root/wp-content/upload");'));
1616
$this->wp->addOption("tinypng_api_key", "test123");
17+
$this->wp->addOption("tinypng_sizes[0]", "on");
1718
$this->wp->addOption("tinypng_sizes[large]", "on");
1819
$this->wp->addOption("tinypng_sizes[post-thumbnail]", "on");
1920

0 commit comments

Comments
 (0)