Skip to content

Commit 8eafd63

Browse files
author
Simon Wahlstrom
committed
Rename merge into preserve. Rename include_metadata to preserve_data. Make preserve_data setting work.
1 parent 9cc86c5 commit 8eafd63

6 files changed

Lines changed: 40 additions & 36 deletions

src/class-tiny-compress-curl.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ protected function shrink($input) {
7272
return array(self::decode(substr($response, $header_size)), $headers, $status_code);
7373
}
7474

75-
protected function output_options($url, $resize_options, $merge_options) {
75+
protected function output_options($url, $resize_options, $preserve_options) {
7676
$options = array(
7777
CURLOPT_URL => $url,
7878
CURLOPT_RETURNTRANSFER => true,
@@ -85,15 +85,15 @@ protected function output_options($url, $resize_options, $merge_options) {
8585

8686
$body = array();
8787

88-
if ($merge_options) {
89-
$body['merge'] = $merge_options;
88+
if ($preserve_options) {
89+
$body['preserve'] = $preserve_options;
9090
}
9191

9292
if ($resize_options) {
9393
$body['resize'] = $resize_options;
9494
}
9595

96-
if ($resize_options || $merge_options) {
96+
if ($resize_options || $preserve_options) {
9797
$options[CURLOPT_USERPWD] = 'api:' . $this->api_key;
9898
$options[CURLOPT_HTTPHEADER] = array('Content-Type: application/json');
9999
$options[CURLOPT_POSTFIELDS] = json_encode($body);
@@ -102,9 +102,9 @@ protected function output_options($url, $resize_options, $merge_options) {
102102
return $options;
103103
}
104104

105-
protected function output($url, $resize_options, $merge_options) {
105+
protected function output($url, $resize_options, $preserve_options) {
106106
$request = curl_init();
107-
$options = $this->output_options($url, $resize_options, $merge_options);
107+
$options = $this->output_options($url, $resize_options, $preserve_options);
108108
curl_setopt_array($request, $options);
109109

110110
$response = curl_exec($request);

src/class-tiny-compress-fopen.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ protected function shrink($input) {
7070
return array(self::decode($response), $headers, $status_code);
7171
}
7272

73-
protected function output_options($resize_options, $merge_options) {
73+
protected function output_options($resize_options, $preserve_options) {
7474
$options = array(
7575
'http' => array(
7676
'method' => 'GET',
@@ -86,24 +86,24 @@ protected function output_options($resize_options, $merge_options) {
8686

8787
$body = array();
8888

89-
if ($merge_options) {
90-
$body['merge'] = $merge_options;
89+
if ($preserve_options) {
90+
$body['preserve'] = $preserve_options;
9191
}
9292

9393
if ($resize_options) {
9494
$body['resize'] = $resize_options;
9595
}
9696

97-
if ($resize_options || $merge_options) {
97+
if ($resize_options || $preserve_options) {
9898
$options['http']['header'][] = 'Authorization: Basic ' . base64_encode('api:' . $this->api_key);
9999
$options['http']['header'][] = 'Content-Type: application/json';
100100
$options['http']['content'] = json_encode($body);
101101
}
102102
return $options;
103103
}
104104

105-
protected function output($url, $resize_options, $merge_options) {
106-
$context = stream_context_create($this->output_options($resize_options, $merge_options));
105+
protected function output($url, $resize_options, $preserve_options) {
106+
$context = stream_context_create($this->output_options($resize_options, $preserve_options));
107107
$request = @fopen($url, 'rb', false, $context);
108108

109109
if ($request) {

src/class-tiny-compress.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ protected function __construct($api_key, $after_compress_callback) {
4242
}
4343

4444
abstract protected function shrink($input);
45-
abstract protected function output($url, $resize_options, $merge_options);
45+
abstract protected function output($url, $resize_options, $preserve_options);
4646

4747
public function get_status(&$details) {
4848
list($details, $headers, $status_code) = $this->shrink(null);
@@ -55,7 +55,7 @@ public function get_status(&$details) {
5555
}
5656
}
5757

58-
public function compress($input, $resize_options, $merge_options) {
58+
public function compress($input, $resize_options, $preserve_options) {
5959
list($details, $headers) = $this->shrink($input);
6060
$this->call_after_compress_callback($details, $headers);
6161
$outputUrl = isset($headers['location']) ? $headers['location'] : null;
@@ -64,7 +64,7 @@ public function compress($input, $resize_options, $merge_options) {
6464
} else if ($outputUrl === null) {
6565
throw new Tiny_Exception('Could not find output url', 'OutputNotFound');
6666
}
67-
list($output, $headers) = $this->output($outputUrl, $resize_options, $merge_options);
67+
list($output, $headers) = $this->output($outputUrl, $resize_options, $preserve_options);
6868
$this->call_after_compress_callback(null, $headers);
6969
if (strlen($output) == 0) {
7070
throw new Tiny_Exception('Could not download output', 'OutputError');
@@ -73,7 +73,7 @@ public function compress($input, $resize_options, $merge_options) {
7373
return array($output, $details);
7474
}
7575

76-
public function compress_file($file, $resize_options, $merge_options) {
76+
public function compress_file($file, $resize_options, $preserve_options) {
7777
if (!file_exists($file)) {
7878
throw new Tiny_Exception('File does not exist', 'FileError');
7979
}
@@ -82,7 +82,7 @@ public function compress_file($file, $resize_options, $merge_options) {
8282
$resize_options = false;
8383
}
8484

85-
list($output, $details) = $this->compress(file_get_contents($file), $resize_options, $merge_options);
85+
list($output, $details) = $this->compress(file_get_contents($file), $resize_options, $preserve_options);
8686
file_put_contents($file, $output);
8787

8888
if ($resize_options) {

src/class-tiny-plugin.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ 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-
$merge = $this->settings->get_metadata_enabled() ? $this->settings->get_metadata_options() : false;
123-
$response = $compressor->compress_file($tiny_metadata->get_filename($uncompressed_size), $resize, $merge);
122+
$preserve = $this->settings->get_preserve_enabled() ? $this->settings->get_preserve_options() : false;
123+
$response = $compressor->compress_file($tiny_metadata->get_filename($uncompressed_size), $resize, $preserve);
124124

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

src/class-tiny-settings.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ public function admin_init() {
7777
register_setting('media', $field);
7878
add_settings_field($field, __('Savings'), $this->get_method('render_pending_savings'), 'media', $section);
7979

80+
$field = self::get_prefixed_name('preserve_data');
81+
register_setting('media', $field);
82+
8083
add_action('wp_ajax_tiny_image_sizes_notice', $this->get_method('image_sizes_notice'));
8184
add_action('wp_ajax_tiny_compress_status', $this->get_method('connection_status'));
8285
add_action('wp_ajax_tiny_compress_savings', $this->get_method('total_savings_status'));
@@ -184,14 +187,14 @@ public function get_resize_enabled() {
184187
return isset($setting['enabled']) && $setting['enabled'] === 'on';
185188
}
186189

187-
public function get_metadata_enabled() {
188-
$setting = get_option(self::get_prefixed_name('include_metadata'));
190+
public function get_preserve_enabled() {
191+
$setting = get_option(self::get_prefixed_name('preserve_data'));
189192
return isset($setting['enabled']) && $setting['enabled'] === 'on';
190193
}
191194

192-
public function get_metadata_options() {
193-
$setting = get_option(self::get_prefixed_name('include_metadata'));
194-
if (!$this->get_metadata_enabled()) {
195+
public function get_preserve_options() {
196+
$setting = get_option(self::get_prefixed_name('preserve_data'));
197+
if (!$this->get_preserve_enabled()) {
195198
return false;
196199

197200
}
@@ -335,9 +338,9 @@ public function render_resize() {
335338
esc_html_e('Resizing takes 1 additional compression for each image that is larger.', 'tiny-compress-images');
336339
echo '</p>';
337340

338-
$id = self::get_prefixed_name("include_metadata_enabled");
339-
$name = self::get_prefixed_name("include_metadata[enabled]");
340-
$checked = ( $this->get_metadata_enabled() ? ' checked="checked"' : '' );
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"' : '' );
341344
$label = esc_html__('Preserve copyright information in the original image (JPEG only)', 'tiny-compress-images');
342345

343346
echo '<p class="tiny-resize-available">';

test/unit/TinySettingsTest.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public function testAdminInitShouldRegisterKeys() {
1616
array('media', 'tinypng_sizes'),
1717
array('media', 'tinypng_resize_original'),
1818
array('media', 'tinypng_status'),
19-
array('media', 'tinypng_savings')
19+
array('media', 'tinypng_savings'),
20+
array('media', 'tinypng_preserve_data')
2021
), $this->wp->getCalls('register_setting'));
2122
}
2223

@@ -150,23 +151,23 @@ public function testShouldNotReturnResizeOptionsWhenNotEnabled() {
150151
}
151152

152153
public function testShouldReturnIncludeMetadataEnabled() {
153-
$this->wp->addOption("tinypng_include_metadata", array('enabled' => 'on'));
154-
$this->assertEquals(true, $this->subject->get_metadata_enabled());
154+
$this->wp->addOption("tinypng_preserve_data", array('enabled' => 'on'));
155+
$this->assertEquals(true, $this->subject->get_preserve_enabled());
155156
}
156157

157158
public function testShouldReturnIncludeMetadataNotEnabledWithoutConfiguration() {
158159
$this->wp->addOption("tinypng_include_metadata", array());
159-
$this->assertEquals(false, $this->subject->get_metadata_enabled());
160+
$this->assertEquals(false, $this->subject->get_preserve_enabled());
160161
}
161162

162-
public function testShouldReturnMergeOptionsWhenEnabled() {
163-
$this->wp->addOption("tinypng_include_metadata", array('enabled' => 'on'));
164-
$this->assertEquals(array('0' => 'copyright'), $this->subject->get_metadata_options());
163+
public function testShouldReturnPreserveOptionsWhenEnabled() {
164+
$this->wp->addOption("tinypng_preserve_data", array('enabled' => 'on'));
165+
$this->assertEquals(array('0' => 'copyright'), $this->subject->get_preserve_options());
165166
}
166167

167-
public function testShouldNotReturnMergeOptionsWhenDisabled() {
168+
public function testShouldNotReturnPreserveOptionsWhenDisabled() {
168169
$this->wp->addOption("tinypng_include_metadata", array());
169-
$this->assertEquals(false, $this->subject->get_metadata_options());
170+
$this->assertEquals(false, $this->subject->get_preserve_options());
170171
}
171172

172173
}

0 commit comments

Comments
 (0)