Skip to content

Commit 00c65a7

Browse files
Merge branch 'master' of github.com:wcreateweb/wordpress-plugin into tiny-cli
2 parents 622895c + 1cc10a2 commit 00c65a7

47 files changed

Lines changed: 2222 additions & 352 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ node_modules/
1313
/playwright/.cache/
1414
test/integration/.auth/user.json
1515
artifacts/
16+
.phpunit.result.cache

.vscode/launch.json

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
2-
// Use IntelliSense to learn about possible attributes.
3-
// Hover to view descriptions of existing attributes.
4-
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5-
"version": "0.2.0",
6-
"configurations": [
7-
{
8-
"name": "Listen for XDebug",
9-
"type": "php",
10-
"request": "launch",
11-
"port": 9003
12-
}
13-
]
14-
}
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "XDebug PHPUnit",
9+
"type": "php",
10+
"request": "launch",
11+
"port": 9003
12+
}
13+
]
14+
}

readme.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Donate link: https://tinypng.com/
44
Tags: compress images, compression, image size, page speed, performance
55
Requires at least: 4.0
66
Tested up to: 6.8
7-
Stable tag: 3.5.2
7+
Stable tag: 3.6.0
88
License: GPLv2 or later
99
License URI: http://www.gnu.org/licenses/gpl-2.0.html
1010

@@ -127,6 +127,14 @@ A: Yes! After installing the plugin, go to *Media > Bulk TinyPNG*, and click on
127127
A: You can upgrade to a paid account by adding your *Payment details* on your [account dashboard](https://tinypng.com/dashboard/api). Additional compressions above 500 will then be charged at the end of each month as a one-time fee.
128128

129129
== Changelog ==
130+
= 3.6.0 =
131+
* Added a link to leave a review in the bulk and settings page
132+
* Renamed menu title for bulk to "Bulk TinyPNG"
133+
* Renamed menu title for settings to "TinyPNG"
134+
* Resolved warning when library was empty
135+
* Resolved warning when resize options was empty
136+
* New feature to enable converting images to WebP or AVIF
137+
130138
= 3.5.2 =
131139
* Removed devdependencies and test files from plug-in
132140
* Fixed a warning when library contained no images

src/class-tiny-bulk-optimization.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ public static function get_optimization_statistics( $settings, $result = null )
2727
$stats = array();
2828
$stats['uploaded-images'] = 0;
2929
$stats['optimized-image-sizes'] = 0;
30-
$stats['available-unoptimised-sizes'] = 0;
30+
$stats['available-unoptimized-sizes'] = 0;
3131
$stats['optimized-library-size'] = 0;
3232
$stats['unoptimized-library-size'] = 0;
33+
$stats['estimated_credit_use'] = 0;
3334
$stats['available-for-optimization'] = array();
3435

3536
if ( is_null( $result ) ) {
@@ -99,9 +100,13 @@ private static function wpdb_retrieve_images_and_metadata( $start_id ) {
99100
private static function populate_optimization_statistics( $settings, $result, $stats ) {
100101
$active_sizes = $settings->get_sizes();
101102
$active_tinify_sizes = $settings->get_active_tinify_sizes();
103+
$conversion_enabled = $settings->get_conversion_enabled();
104+
102105
for ( $i = 0; $i < sizeof( $result ); $i++ ) {
103106
$wp_metadata = unserialize( (string) $result[ $i ]['meta_value'] );
104-
$tiny_metadata = unserialize( (string) $result[ $i ]['tiny_meta_value'] );
107+
$tiny_metadata = isset( $result[ $i ]['tiny_meta_value'] ) ?
108+
unserialize( (string) $result[ $i ]['tiny_meta_value'] ) :
109+
array();
105110
if ( ! is_array( $tiny_metadata ) ) {
106111
$tiny_metadata = array();
107112
}
@@ -114,18 +119,35 @@ private static function populate_optimization_statistics( $settings, $result, $s
114119
$active_tinify_sizes
115120
);
116121
$image_stats = $tiny_image->get_statistics( $active_sizes, $active_tinify_sizes );
122+
117123
$stats['uploaded-images']++;
118-
$stats['available-unoptimised-sizes'] += $image_stats['available_unoptimized_sizes'];
119-
$stats['optimized-image-sizes'] += $image_stats['image_sizes_optimized'];
120-
$stats['optimized-library-size'] += $image_stats['optimized_total_size'];
124+
$stats['estimated_credit_use'] += $image_stats['available_uncompressed_sizes'];
125+
if ( $conversion_enabled ) {
126+
$stats['available-unoptimized-sizes'] +=
127+
$image_stats['available_unconverted_sizes'];
128+
$stats['optimized-image-sizes'] +=
129+
$image_stats['image_sizes_converted'];
130+
$stats['estimated_credit_use'] += $image_stats['available_unconverted_sizes'];
131+
} else {
132+
$stats['available-unoptimized-sizes'] +=
133+
$image_stats['available_uncompressed_sizes'];
134+
$stats['optimized-image-sizes'] +=
135+
$image_stats['image_sizes_compressed'];
136+
}
137+
$stats['optimized-library-size'] += $image_stats['compressed_total_size'];
121138
$stats['unoptimized-library-size'] += $image_stats['initial_total_size'];
122-
if ( $image_stats['available_unoptimized_sizes'] > 0 ) {
139+
140+
$has_conversions = $image_stats['available_unconverted_sizes'] > 0;
141+
$has_compressions = $image_stats['available_uncompressed_sizes'] > 0;
142+
$has_optimizations = $has_compressions || ($conversion_enabled && $has_conversions);
143+
if ( $has_optimizations ) {
123144
$stats['available-for-optimization'][] = array(
124145
'ID' => $result[ $i ]['ID'],
125146
'post_title' => $result[ $i ]['post_title'],
126147
);
127148
}
128-
}
149+
}// End for().
150+
129151
return $stats;
130152
}
131153
}

src/class-tiny-compress-client.php

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
}
3030

3131
class Tiny_Compress_Client extends Tiny_Compress {
32+
3233
private $last_error_code = 0;
3334
private $last_message = '';
3435
private $proxy;
@@ -72,7 +73,6 @@ protected function validate() {
7273
$this->set_request_options( \Tinify\Tinify::getClient( \Tinify\Tinify::ANONYMOUS ) );
7374
\Tinify\Tinify::getClient()->request( 'get', '/keys/' . $this->get_key() );
7475
return true;
75-
7676
} catch ( \Tinify\Exception $err ) {
7777
$this->last_error_code = $err->status;
7878

@@ -88,7 +88,7 @@ protected function validate() {
8888
}
8989
}
9090

91-
protected function compress( $input, $resize_opts, $preserve_opts ) {
91+
protected function compress( $input, $resize_opts, $preserve_opts, $convert_opts ) {
9292
try {
9393
$this->last_error_code = 0;
9494
$this->set_request_options( \Tinify\Tinify::getClient() );
@@ -103,25 +103,39 @@ protected function compress( $input, $resize_opts, $preserve_opts ) {
103103
$source = $source->preserve( $preserve_opts );
104104
}
105105

106-
$result = $source->result();
107-
106+
$compress_result = $source->result();
108107
$meta = array(
109108
'input' => array(
110109
'size' => strlen( $input ),
111-
'type' => $result->mediaType(),
110+
'type' => Tiny_Helpers::get_mimetype( $input ),
112111
),
113112
'output' => array(
114-
'size' => $result->size(),
115-
'type' => $result->mediaType(),
116-
'width' => $result->width(),
117-
'height' => $result->height(),
118-
'ratio' => round( $result->size() / strlen( $input ), 4 ),
113+
'size' => $compress_result->size(),
114+
'type' => $compress_result->mediaType(),
115+
'width' => $compress_result->width(),
116+
'height' => $compress_result->height(),
117+
'ratio' => round( $compress_result->size() / strlen( $input ), 4 ),
119118
),
120119
);
121120

122-
$buffer = $result->toBuffer();
123-
return array( $buffer, $meta );
121+
$buffer = $compress_result->toBuffer();
122+
$result = array( $buffer, $meta, null );
123+
124+
if ( isset( $convert_opts['convert'] ) && true == $convert_opts['convert'] ) {
125+
$convert_to = $convert_opts['convert_to'];
126+
$convert_source = $source->convert( array(
127+
'type' => $convert_to,
128+
) );
129+
$convert_result = $convert_source->result();
130+
$meta['convert'] = array(
131+
'type' => $convert_result->mediaType(),
132+
'size' => $convert_result->size(),
133+
);
134+
$convert_buffer = $convert_result->toBuffer();
135+
$result = array( $buffer, $meta, $convert_buffer );
136+
}
124137

138+
return $result;
125139
} catch ( \Tinify\Exception $err ) {
126140
$this->last_error_code = $err->status;
127141

@@ -130,7 +144,7 @@ protected function compress( $input, $resize_opts, $preserve_opts ) {
130144
get_class( $err ),
131145
$err->status
132146
);
133-
}// End try().
147+
} // End try().
134148
}
135149

136150
public function create_key( $email, $options ) {

src/class-tiny-compress-fopen.php

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ protected function validate() {
8282
}
8383
}
8484

85-
protected function compress( $input, $resize_opts, $preserve_opts ) {
85+
protected function compress( $input, $resize_opts, $preserve_opts, $convert_opts ) {
8686
$params = $this->request_options( 'POST', $input );
8787
list($details, $headers, $status_code) = $this->request( $params );
8888

@@ -133,7 +133,7 @@ protected function compress( $input, $resize_opts, $preserve_opts ) {
133133
$meta = array(
134134
'input' => array(
135135
'size' => strlen( $input ),
136-
'type' => $headers['content-type'],
136+
'type' => Tiny_Helpers::get_mimetype( $input ),
137137
),
138138
'output' => array(
139139
'size' => strlen( $output ),
@@ -144,9 +144,36 @@ protected function compress( $input, $resize_opts, $preserve_opts ) {
144144
),
145145
);
146146

147-
return array( $output, $meta );
148-
}
147+
$convert = null;
148+
149+
if ( isset( $convert_opts['convert'] ) && true === $convert_opts['convert'] ) {
150+
$convert_to = $convert_opts['convert_to'];
151+
$convert_params = $this->request_options(
152+
'POST',
153+
array(
154+
'convert' => array(
155+
'type' => $convert_to,
156+
),
157+
),
158+
array( 'Content-Type: application/json' )
159+
);
160+
161+
list($convert_output, $convert_headers) = $this->request(
162+
$convert_params,
163+
$output_url
164+
);
165+
$meta['convert'] = array(
166+
'type' => $convert_headers['content-type'],
167+
'size' => strlen( $convert_output ),
168+
);
169+
$convert = $convert_output;
149170

171+
}
172+
173+
$result = array( $output, $meta, $convert );
174+
175+
return $result;
176+
}
150177
private function request( $params, $url = Tiny_Config::SHRINK_URL ) {
151178
$context = stream_context_create( $params );
152179
$request = fopen( $url, 'rb', false, $context );
@@ -188,8 +215,10 @@ private function request( $params, $url = Tiny_Config::SHRINK_URL ) {
188215
$response = stream_get_contents( $request );
189216
fclose( $request );
190217

191-
if ( isset( $headers['content-type'] ) &&
192-
substr( 'application/json' == $headers['content-type'], 0, 16 ) ) {
218+
if (
219+
isset( $headers['content-type'] ) &&
220+
substr( 'application/json' == $headers['content-type'], 0, 16 )
221+
) {
193222
$response = $this->decode( $response );
194223
}
195224

@@ -221,11 +250,11 @@ private function request_options( $method, $body = null, $headers = array() ) {
221250
return array(
222251
'http' => array(
223252
'method' => $method,
224-
'header' => array_merge( $headers, array(
253+
'header' => array_merge($headers, array(
225254
'Authorization: Basic ' . base64_encode( 'api:' . $this->api_key ),
226255
'User-Agent: ' . self::identifier(),
227256
'Content-Type: multipart/form-data',
228-
) ),
257+
)),
229258
'content' => $body,
230259
'follow_location' => 0,
231260
'max_redirects' => 1, // Necessary for PHP 5.2

0 commit comments

Comments
 (0)