Skip to content

Commit 4e730d5

Browse files
author
Jippe Holwerda
committed
Store copyright preservation setting under specific copyright key.
1 parent e1a922a commit 4e730d5

3 files changed

Lines changed: 26 additions & 19 deletions

File tree

src/class-tiny-plugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private function compress($metadata, $attachment_id) {
119119
$tiny_metadata->update();
120120

121121
$resize = $tiny_metadata->is_resizable($uncompressed_size) ? $this->settings->get_resize_options() : false;
122-
$preserve = $this->settings->get_preserve_enabled() ? $this->settings->get_preserve_options() : false;
122+
$preserve = count($this->settings->get_preserve_options()) > 0 ? $this->settings->get_preserve_options() : false;
123123
$response = $compressor->compress_file($tiny_metadata->get_filename($uncompressed_size), $resize, $preserve);
124124

125125
$tiny_metadata->add_response($response, $uncompressed_size);

src/class-tiny-settings.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -187,18 +187,21 @@ public function get_resize_enabled() {
187187
return isset($setting['enabled']) && $setting['enabled'] === 'on';
188188
}
189189

190-
public function get_preserve_enabled() {
190+
public function get_preserve_enabled($name) {
191191
$setting = get_option(self::get_prefixed_name('preserve_data'));
192-
return isset($setting['enabled']) && $setting['enabled'] === 'on';
192+
return isset($setting[$name]) && $setting[$name] === 'on';
193193
}
194194

195195
public function get_preserve_options() {
196-
$setting = get_option(self::get_prefixed_name('preserve_data'));
197-
if (!$this->get_preserve_enabled()) {
198-
return false;
199-
196+
$settings = get_option(self::get_prefixed_name('preserve_data'));
197+
$options = array();
198+
if ($settings) {
199+
foreach (array_keys($settings) as &$setting) {
200+
if ($settings[$setting] === "on") {
201+
array_push($options, $setting);
202+
}
203+
}
200204
}
201-
$options = array('copyright');
202205
return $options;
203206
}
204207

@@ -338,13 +341,17 @@ public function render_resize() {
338341
esc_html_e('Resizing takes 1 additional compression for each image that is larger.', 'tiny-compress-images');
339342
echo '</p>';
340343

341-
$id = self::get_prefixed_name("preserve_data_enabled");
342-
$name = self::get_prefixed_name("preserve_data[enabled]");
343-
$checked = ( $this->get_preserve_enabled() ? ' checked="checked"' : '' );
344-
$label = esc_html__('Preserve copyright information in the original image (JPEG only)', 'tiny-compress-images');
344+
echo '<br>';
345+
$this->render_preserve_input("copyright", 'Preserve copyright information in the original image (JPEG only)');
346+
}
345347

346-
echo '<p class="tiny-resize-available">';
347-
echo '<input type="checkbox" id="' . $id . '" name="' . $name . '" value="on" '. $checked . '/>';
348+
public function render_preserve_input($name, $description) {
349+
echo '<p>';
350+
$id = sprintf(self::get_prefixed_name('preserve_data_%s'), $name);
351+
$field = sprintf(self::get_prefixed_name('preserve_data[%s]'), $name);
352+
$checked = ( $this->get_preserve_enabled($name) ? ' checked="checked"' : '' );
353+
$label = esc_html__($description, 'tiny-compress-images');
354+
echo '<input type="checkbox" id="' . $id . '" name="' . $field . '" value="on" ' . $checked . '/>';
348355
echo '<label for="' . $id . '">' . $label . '</label>';
349356
echo '<br>';
350357
echo '</p>';

test/unit/TinySettingsTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,22 +151,22 @@ public function testShouldNotReturnResizeOptionsWhenNotEnabled() {
151151
}
152152

153153
public function testShouldReturnIncludeMetadataEnabled() {
154-
$this->wp->addOption("tinypng_preserve_data", array('enabled' => 'on'));
155-
$this->assertEquals(true, $this->subject->get_preserve_enabled());
154+
$this->wp->addOption("tinypng_preserve_data", array('copyright' => 'on'));
155+
$this->assertEquals(true, $this->subject->get_preserve_enabled("copyright"));
156156
}
157157

158158
public function testShouldReturnIncludeMetadataNotEnabledWithoutConfiguration() {
159159
$this->wp->addOption("tinypng_include_metadata", array());
160-
$this->assertEquals(false, $this->subject->get_preserve_enabled());
160+
$this->assertEquals(false, $this->subject->get_preserve_enabled("copyright"));
161161
}
162162

163163
public function testShouldReturnPreserveOptionsWhenEnabled() {
164-
$this->wp->addOption("tinypng_preserve_data", array('enabled' => 'on'));
164+
$this->wp->addOption("tinypng_preserve_data", array('copyright' => 'on'));
165165
$this->assertEquals(array('0' => 'copyright'), $this->subject->get_preserve_options());
166166
}
167167

168168
public function testShouldNotReturnPreserveOptionsWhenDisabled() {
169169
$this->wp->addOption("tinypng_include_metadata", array());
170-
$this->assertEquals(false, $this->subject->get_preserve_options());
170+
$this->assertEquals(array(), $this->subject->get_preserve_options());
171171
}
172172
}

0 commit comments

Comments
 (0)