Skip to content

Commit 7d0061c

Browse files
author
Edwin Westerhoud
committed
Simplify resize settings and update free images estimation according to resize setting.
1 parent 45f797d commit 7d0061c

6 files changed

Lines changed: 58 additions & 25 deletions

File tree

src/class-tiny-settings.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function admin_init() {
6868
}
6969

7070
public function image_sizes_notice() {
71-
$this->render_image_sizes_notice($_GET["image_sizes_selected"]);
71+
$this->render_image_sizes_notice($_GET["image_sizes_selected"], $_GET["resize_original"]);
7272
exit();
7373
}
7474

@@ -217,7 +217,7 @@ public function render_sizes() {
217217
}
218218

219219
echo '<div id="tiny-image-sizes-notice">';
220-
$this->render_image_sizes_notice(count(self::get_active_tinify_sizes()));
220+
$this->render_image_sizes_notice(count(self::get_active_tinify_sizes()), self::get_resize_enabled());
221221
echo '</div>';
222222
}
223223

@@ -234,8 +234,11 @@ private function render_size_checkbox($size, $option) {
234234
<?php
235235
}
236236

237-
public function render_image_sizes_notice($active_image_sizes_count) {
238-
echo '<br/>';
237+
public function render_image_sizes_notice($active_image_sizes_count, $resize_original_enabled) {
238+
echo '<br>';
239+
if ($resize_original_enabled) {
240+
$active_image_sizes_count++;
241+
}
239242
if ($active_image_sizes_count < 1) {
240243
echo '<p>' . self::translate_escape('With these settings no images will be compressed') . '.</p>';
241244
}
@@ -245,27 +248,34 @@ public function render_image_sizes_notice($active_image_sizes_count) {
245248
echo '<p>';
246249
echo self::translate_escape('With these settings you can compress');
247250
echo ' <strong>';
248-
printf(self::translate_escape('%s images'), $free_images_per_month);
251+
printf(self::translate_escape('at least %s images'), $free_images_per_month);
249252
echo '</strong> ';
250253
echo self::translate_escape('for free each month') . '.';
251254
echo '</p>';
252255
}
253256
}
254257

255258
public function render_resize() {
256-
echo '<p>' . self::translate_escape("Automatically resize the orginal image to a lower resolution. Enabling this option will resize the original image when it exceeds the specified width or height, using the TinyPNG API") . '.</p>';
257259
echo '<p class="tiny-resize-unavailable" style="display: none">' . self::translate_escape("Enable the compression of the original image size to configure resizing") . '.</p>';
258260

259261
$id = self::get_prefixed_name("resize_original_enabled");
260262
$field = self::get_prefixed_name("resize_original[enabled]");
261-
$label = self::translate_escape('fit original image within');
262-
$class = "tiny-resize-available";
263+
$label = self::translate_escape('Resize orginal images larger than');
264+
265+
echo '<p class="tiny-resize-available">';
263266
?>
264-
<p><input class="<?php echo $class ?>" type="checkbox" id="<?php echo $id ?>" name="<?php echo $field ?>" value="on" <?php if ($this->get_resize_enabled()) { echo ' checked="checked"'; } ?>/>
265-
<label class="<?php echo $class ?>" for="<?php echo $id; ?>"><?php echo $label; ?>
266-
<?php $this->render_resize_input('width') ?> x <?php $this->render_resize_input('height') ?> <?php printf("%s (%s x %s)", self::translate_escape('pixels'), self::translate_escape('width'), self::translate_escape('height')) ?></label></p>
267+
<input type="checkbox" id="<?php echo $id ?>" name="<?php echo $field ?>" value="on" <?php if ($this->get_resize_enabled()) { echo ' checked="checked"'; } ?>/>
268+
<label for="<?php echo $id; ?>"><?php echo $label; ?>:</label><br>
267269
<?php
268-
echo '<p class="tiny-resize-available">' . sprintf(self::translate_escape("Resizing takes %s per image larger than the specified resolution"), '<strong>' . self::translate_escape('1 additional compression') . '</strong>') . '.</p>';
270+
271+
echo '</p>';
272+
echo '<p class="tiny-resize-available tiny-resize-resolution">';
273+
274+
printf("%s: ", self::translate_escape('Max Width'));
275+
$this->render_resize_input('width');
276+
printf("%s: ", self::translate_escape('Max Height'));
277+
$this->render_resize_input('height');
278+
echo '</p>';
269279
}
270280

271281
public function render_resize_input($name) {

src/scripts/admin.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,14 @@
156156
if (adminpage === "options-media-php") {
157157
jQuery('#tiny-compress-status').load(ajaxurl + '?action=tiny_compress_status')
158158

159-
jQuery('input[name*="tinypng_sizes"]').on("click", function() {
159+
jQuery('input[name*="tinypng_sizes"], input#tinypng_resize_original_enabled').on("click", function() {
160160
// Unfortunately, we need some additional information to display the correct notice.
161161
totalSelectedSizes = jQuery('input[name*="tinypng_sizes"]:checked').length
162-
jQuery('#tiny-image-sizes-notice').load(ajaxurl + '?action=tiny_image_sizes_notice&image_sizes_selected=' + totalSelectedSizes)
162+
var image_count_url = ajaxurl + '?action=tiny_image_sizes_notice&image_sizes_selected=' + totalSelectedSizes
163+
if (jQuery('input#tinypng_resize_original_enabled').prop('checked') && jQuery('input#tinypng_sizes_0').prop('checked')) {
164+
image_count_url += '&resize_original=true'
165+
}
166+
jQuery('#tiny-image-sizes-notice').load(image_count_url)
163167
})
164168

165169
function update_resize_settings() {

src/styles/admin.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ p.tiny-resize-unavailable {
5656
font-style: italic;
5757
}
5858

59+
p.tiny-resize-resolution {
60+
margin-left: 24px;
61+
}
62+
63+
p.tiny-resize-resolution input {
64+
margin-right: 6px;
65+
}
66+
5967
input[type=number][name*="tinypng_resize_original"] {
6068
width: 65px;
6169
}

test/integration/CompressIntegrationTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function testResizeFit() {
8787
$this->upload_image(dirname(__FILE__) . '/../fixtures/input-example.png');
8888
$this->assertContains('Resized original to 300x200',
8989
self::$driver->findElement(WebDriverBy::cssSelector('td.tiny-compress-images'))->getText());
90-
self::$driver->findElement(WebDriverBy::xpath('//a[contains(text(),"input-example")]'))->click();
90+
$this->view_edit_image();
9191
$this->assertContains('Dimensions: 300 × 200',
9292
self::$driver->findElement(WebDriverBy::cssSelector('div.misc-pub-dimensions'))->getText());
9393
}
@@ -98,7 +98,7 @@ public function testResizeScale() {
9898
$this->upload_image(dirname(__FILE__) . '/../fixtures/input-example.png');
9999
$this->assertContains('Resized original to 300x200',
100100
self::$driver->findElement(WebDriverBy::cssSelector('td.tiny-compress-images'))->getText());
101-
self::$driver->findElement(WebDriverBy::xpath('//a[contains(text(),"input-example")]'))->click();
101+
$this->view_edit_image();
102102
$this->assertContains('Dimensions: 300 × 200',
103103
self::$driver->findElement(WebDriverBy::cssSelector('div.misc-pub-dimensions'))->getText());
104104
}
@@ -110,7 +110,7 @@ public function testResizeNotNeeded()
110110
$this->upload_image(dirname(__FILE__) . '/../fixtures/input-example.png');
111111
$this->assertNotContains('Resized original',
112112
self::$driver->findElement(WebDriverBy::cssSelector('td.tiny-compress-images'))->getText());
113-
self::$driver->findElement(WebDriverBy::xpath('//a[contains(text(),"input-example")]'))->click();
113+
$this->view_edit_image();
114114
$this->assertContains('Dimensions: 1080 × 720',
115115
self::$driver->findElement(WebDriverBy::cssSelector('div.misc-pub-dimensions'))->getText());
116116
}
@@ -123,7 +123,7 @@ public function testResizeDisabled()
123123
$this->upload_image(dirname(__FILE__) . '/../fixtures/input-example.png');
124124
$this->assertNotContains('Resized original',
125125
self::$driver->findElement(WebDriverBy::cssSelector('td.tiny-compress-images'))->getText());
126-
self::$driver->findElement(WebDriverBy::xpath('//a[contains(text(),"input-example")]'))->click();
126+
$this->view_edit_image();
127127
$this->assertContains('Dimensions: 1080 × 720',
128128
self::$driver->findElement(WebDriverBy::cssSelector('div.misc-pub-dimensions'))->getText());
129129
}

test/integration/IntegrationTestCase.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,17 @@ protected function disable_resize() {
8888
}
8989
self::$driver->findElement(WebDriverBy::tagName('form'))->submit();
9090
}
91+
92+
protected function view_edit_image($image_title = 'input-example') {
93+
$url = wordpress('/wp-admin/upload.php');
94+
if (self::$driver->getCurrentUrl() != $url) {
95+
self::$driver->get($url);
96+
}
97+
if (wordpress_version() >= 43) {
98+
$selector = "//span[text()='" . $image_title . "']";
99+
} else {
100+
$selector = "//a[contains(text(),'" . $image_title . "')]";
101+
}
102+
self::$driver->findElement(WebDriverBy::xpath($selector))->click();
103+
}
91104
}

test/integration/SettingsIntegrationTest.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,19 @@ public function testShouldPersistNoSizes() {
8989
public function testShouldShowTotalImagesInfo() {
9090
$elements = self::$driver->findElement(WebDriverBy::id('tiny-image-sizes-notice'))->findElements(WebDriverBy::tagName('p'));
9191
$statuses = array_map('innerText', $elements);
92-
$this->assertContains('With these settings you can compress 100 images for free each month.', $statuses);
92+
$this->assertContains('With these settings you can compress at least 100 images for free each month.', $statuses);
9393
}
9494

9595
public function testShouldUpdateTotalImagesInfo() {
9696
$element = self::$driver->findElement(
9797
WebDriverBy::xpath('//input[@type="checkbox" and @name="tinypng_sizes[0]" and @checked="checked"]'));
9898
$element->click();
9999
self::$driver->wait(2)->until(WebDriverExpectedCondition::textToBePresentInElement(
100-
WebDriverBy::cssSelector('#tiny-image-sizes-notice'), 'With these settings you can compress 125 images for free each month.'));
100+
WebDriverBy::cssSelector('#tiny-image-sizes-notice'), 'With these settings you can compress at least 125 images for free each month.'));
101101
// Not really necessary anymore to assert this.
102102
$elements = self::$driver->findElement(WebDriverBy::id('tiny-image-sizes-notice'))->findElements(WebDriverBy::tagName('p'));
103103
$statuses = array_map('innerText', $elements);
104-
$this->assertContains('With these settings you can compress 125 images for free each month.', $statuses);
104+
$this->assertContains('With these settings you can compress at least 125 images for free each month.', $statuses);
105105
}
106106

107107
public function testShouldShowCorrectNoImageSizesInfo() {
@@ -125,10 +125,9 @@ public function testShouldShowResizingWhenOriginalEnabled() {
125125
}
126126
$labels = self::$driver->findElements(WebDriverBy::tagName('label'));
127127
$texts = array_map('innerText', $labels);
128-
$this->assertContains('fit original image within x pixels (width x height)', $texts);
128+
$this->assertContains('Resize orginal images larger than:', $texts);
129129
$paragraphs = self::$driver->findElements(WebDriverBy::tagName('p'));
130130
$texts = array_map('innerText', $paragraphs);
131-
$this->assertContains('Resizing takes 1 additional compression per image larger than the specified resolution.', $texts);
132131
$this->assertNotContains('Enable the compression of the original image size to configure resizing.', $texts);
133132
}
134133

@@ -139,10 +138,9 @@ public function testShouldNotShowResizingWhenOriginalDisabled() {
139138
}
140139
$labels = self::$driver->findElements(WebDriverBy::tagName('label'));
141140
$texts = array_map('innerText', $labels);
142-
$this->assertNotContains('fit original image within x pixels (width x height)', $texts);
141+
$this->assertNotContains('Resize orginal images larger than:', $texts);
143142
$paragraphs = self::$driver->findElements(WebDriverBy::tagName('p'));
144143
$texts = array_map('innerText', $paragraphs);
145-
$this->assertNotContains('Resizing takes 1 additional compression per image larger than the specified resolution.', $texts);
146144
$this->assertContains('Enable the compression of the original image size to configure resizing.', $texts);
147145
}
148146

0 commit comments

Comments
 (0)