Skip to content

Commit 566685c

Browse files
Show indication of how many images can be compressed for free per month and show warning for when the original image size has been selected.
1 parent 3ee39f9 commit 566685c

4 files changed

Lines changed: 94 additions & 1 deletion

File tree

src/class-tiny-settings.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

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

2425
private $sizes;
2526
private $tinify_sizes;
@@ -58,9 +59,15 @@ public function admin_init() {
5859
register_setting('media', $field);
5960
add_settings_field($field, self::translate('Connection status'), $this->get_method('render_pending_status'), 'media', $section);
6061

62+
add_action('wp_ajax_tiny_image_sizes_notices', $this->get_method('image_sizes_notices'));
6163
add_action('wp_ajax_tiny_compress_status', $this->get_method('connection_status'));
6264
}
6365

66+
public function image_sizes_notices() {
67+
$this->render_image_sizes_notices($_GET["image_sizes_selected"], $_GET["original_selected"]);
68+
exit();
69+
}
70+
6471
public function connection_status() {
6572
$this->render_status();
6673
exit();
@@ -179,6 +186,10 @@ public function render_sizes() {
179186
foreach ($this->get_sizes() as $size => $option) {
180187
$this->render_size_checkbox($size, $option);
181188
}
189+
190+
echo '<div id="tiny-image-sizes-notices">';
191+
$this->render_image_sizes_notices(count(self::get_active_tinify_sizes()), $this->get_sizes()[Tiny_Metadata::ORIGINAL]['tinify']);
192+
echo '</div>';
182193
}
183194

184195
private function render_size_checkbox($size, $option) {
@@ -194,6 +205,23 @@ private function render_size_checkbox($size, $option) {
194205
<?php
195206
}
196207

208+
public function render_image_sizes_notices($active_image_sizes_count, $original_selected) {
209+
echo '<br/>';
210+
if ($active_image_sizes_count < 1) {
211+
echo '<p>' . self::translate_escape('With these settings no images will be compressed') . '.</p>';
212+
}
213+
else {
214+
$free_images_per_month = floor(self::MONTHLY_FREE_COMPRESSIONS / $active_image_sizes_count);
215+
216+
echo '<p>';
217+
printf(self::translate_escape('With these settings you can compress %s images for free each month') . '.', $free_images_per_month);
218+
echo '</p>';
219+
if ($original_selected) {
220+
echo '<p>' . self::translate_escape('Note: your original images will be overwritten with the compressed versions') . '!</p>';
221+
}
222+
}
223+
}
224+
197225
public function get_compression_count() {
198226
$field = self::get_prefixed_name('status');
199227
return get_option($field);
@@ -241,7 +269,7 @@ public function render_status() {
241269
$compressions = self::get_compression_count();
242270
echo '<p>';
243271
// We currently have no way to check if a user is free or flexible.
244-
if ($compressions == 500) {
272+
if ($compressions == self::MONTHLY_FREE_COMPRESSIONS) {
245273
$link = '<a href="https://tinypng.com/developers" target="_blank">' . self::translate_escape('TinyPNG API account') . '</a>';
246274
printf(self::translate_escape('You have reached your limit of %s compressions this month') . '.', $compressions);
247275
echo '<br>';

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,15 @@ msgstr "Als je meer afbeeldingen wilt comprimeren kun je je %s aanpassen"
133133
msgid "Upgrade your %s if you like to compress more images"
134134
msgstr "Upgrade je %s als je meer afbeeldingen wilt comprimeren"
135135

136+
msgid "With these settings you can compress %s images for free each month"
137+
msgstr "Met deze instellingen kun je elke maand %s afbeeldingen gratis comprimeren"
138+
139+
msgid "With these settings no images will be compressed"
140+
msgstr "Met deze instellingen worden geen afbeeldingen gecomprimeerd"
141+
142+
msgid "Note: your original images will be overwritten with the compressed versions"
143+
msgstr "Belangrijk: je originele afbeeldingen worden overschreven met de gecomprimeerde afbeeldingen"
144+
136145
msgid "Please fill in an API key to start compressing images"
137146
msgstr "Vul een API-sleutel in om te starten met comprimeren"
138147

src/scripts/admin.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@
155155

156156
if (adminpage === "options-media-php") {
157157
jQuery('#tiny-compress-status').load(ajaxurl + '?action=tiny_compress_status')
158+
159+
jQuery('input[name*="tinypng_sizes"]').on("click", function() {
160+
// Unfortunately, we need some additional information to display the correct notices.
161+
originalSelected = jQuery('input[name="tinypng_sizes[0]"]:checked').length
162+
totalSelectedSizes = jQuery('input[name*="tinypng_sizes"]:checked').length
163+
jQuery('#tiny-image-sizes-notices').load(ajaxurl + '?action=tiny_image_sizes_notices&image_sizes_selected=' + totalSelectedSizes + '&original_selected=' + originalSelected)
164+
})
158165
}
159166

160167
jQuery('.tiny-notice a.tiny-dismiss').click(dismiss_notice)

test/integration/SettingsIntegrationTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,55 @@ public function testShouldPersistNoSizes() {
8686
$this->assertEquals(0, count(array_map('elementName', $elements)));
8787
}
8888

89+
public function testShouldShowOriginalsMessageWhenChecked() {
90+
$elements = self::$driver->findElement(WebDriverBy::id('tiny-image-sizes-notices'))->findElements(WebDriverBy::tagName('p'));
91+
$statuses = array_map('innerText', $elements);
92+
$this->assertContains('Note: your original images will be overwritten with the compressed versions!', $statuses);
93+
}
94+
95+
public function testShouldHideOriginalsMessageWhenUnchecked() {
96+
$element = self::$driver->findElement(
97+
WebDriverBy::xpath('//input[@type="checkbox" and @name="tinypng_sizes[0]" and @checked="checked"]'));
98+
$element->click();
99+
self::$driver->findElement(WebDriverBy::tagName('form'))->submit();
100+
101+
$elements = self::$driver->findElement(WebDriverBy::id('tiny-image-sizes-notices'))->findElements(WebDriverBy::tagName('p'));
102+
$statuses = array_map('innerText', $elements);
103+
$this->assertNotContains('Note: your original images will be overwritten with the compressed versions!', $statuses);
104+
}
105+
106+
public function testShouldShowTotalImagesInfo() {
107+
$elements = self::$driver->findElement(WebDriverBy::id('tiny-image-sizes-notices'))->findElements(WebDriverBy::tagName('p'));
108+
$statuses = array_map('innerText', $elements);
109+
$this->assertContains('With these settings you can compress 100 images for free each month.', $statuses);
110+
}
111+
112+
public function testShouldUpdateTotalImagesInfo() {
113+
$element = self::$driver->findElement(
114+
WebDriverBy::xpath('//input[@type="checkbox" and @name="tinypng_sizes[0]" and @checked="checked"]'));
115+
$element->click();
116+
self::$driver->wait(2)->until(WebDriverExpectedCondition::textToBePresentInElement(
117+
WebDriverBy::cssSelector('#tiny-image-sizes-notices'), 'With these settings you can compress 125 images for free each month.'));
118+
// Not really necessary anymore to assert this.
119+
$elements = self::$driver->findElement(WebDriverBy::id('tiny-image-sizes-notices'))->findElements(WebDriverBy::tagName('p'));
120+
$statuses = array_map('innerText', $elements);
121+
$this->assertContains('With these settings you can compress 125 images for free each month.', $statuses);
122+
}
123+
124+
public function testShouldShowCorrectNoImageSizesInfo() {
125+
$elements = self::$driver->findElements(
126+
WebDriverBy::xpath('//input[@type="checkbox" and starts-with(@name, "tinypng_sizes") and @checked="checked"]'));
127+
foreach ($elements as $element) {
128+
$element->click();
129+
}
130+
self::$driver->wait(2)->until(WebDriverExpectedCondition::textToBePresentInElement(
131+
WebDriverBy::cssSelector('#tiny-image-sizes-notices'), 'With these settings no images will be compressed.'));
132+
// Not really necessary anymore to assert this.
133+
$elements = self::$driver->findElement(WebDriverBy::id('tiny-image-sizes-notices'))->findElements(WebDriverBy::tagName('p'));
134+
$statuses = array_map('innerText', $elements);
135+
$this->assertContains('With these settings no images will be compressed.', $statuses);
136+
}
137+
89138
public function testStatusPresenceOK() {
90139
reset_webservice();
91140
$this->set_api_key('PNG123');

0 commit comments

Comments
 (0)