Smush Image Compression and Optimization - Version 1.2.1

Version Description

  • added support for PHP 4
  • created admin_action_ hook as workaround to WordPress 2.9's $_registered_pages security (see http://core.trac.wordpress.org/changeset/11596)
  • add savings amount in bytes to Media Library (thx Yoast)
Download this release

Release Info

Developer alexdunae
Plugin Icon 128x128 Smush Image Compression and Optimization
Version 1.2.1
Comparing to
See all releases

Code changes from version 1.2 to 1.2.1

Files changed (4) hide show
  1. history.txt +0 -27
  2. readme.txt +90 -56
  3. smush.php +0 -29
  4. wp-smushit.php +155 -82
history.txt DELETED
@@ -1,27 +0,0 @@
1
- === WP Smush.it Release History ===
2
-
3
- == 1.2 ==
4
- * added support for WP_Http
5
-
6
- == 1.1.3 ==
7
- * fixed activation error when the PEAR JSON library is already loaded
8
-
9
- == 1.1.2 ==
10
- * added test for allow_url_fopen
11
-
12
- == 1.1.1 ==
13
- * added error message on PHP copy error
14
-
15
- == 1.1 ==
16
- * improved handling of errors from Smush.it
17
- * added ability to manually smush images from media library
18
- * fixed inconsistent path handling from WP 2.5 -> WP 2.7
19
-
20
- == 1.0.2 ==
21
- * added 'Not processed' status message when browsing media library
22
-
23
- == 1.0.1 ==
24
- * added i10n functions
25
-
26
- == 1.0 ==
27
- * first edition
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
readme.txt CHANGED
@@ -1,56 +1,90 @@
1
- === WP Smush.it ===
2
- Plugin Name: WP Smush.it
3
- Version: 1.2
4
- Author: Dialect
5
- Author URI: http://dialect.ca/?wp_smush_it
6
- Contributors: alexdunae
7
- Tags: images, image, attachments, attachment
8
- Requires at least: 2.5
9
- Tested up to: 2.8
10
- Stable tag: 1.2
11
-
12
- Reduce image file sizes and improve performance using the <a href="http://smush.it/">Smush.it</a> API within WordPress.
13
-
14
- == Description ==
15
-
16
- Yahoo's excellent <a href="http://developer.yahoo.com/performance/">Exceptional Performance series</a> recommends <a href="http://developer.yahoo.com/performance/rules.html#opt_images">optimizing images</a> in several lossless ways:
17
-
18
- * stripping meta data from JPEGs
19
- * optimizing JPEG compression
20
- * converting certain GIFs to indexed PNGs
21
- * stripping the un-used colours from indexed images
22
-
23
- <a href="http://smush.it/">Smush.it</a> offers an API that performs these optimizations (except for stripping JPEG meta data) automatically, and this plugin seamlessly integrates Smush.it with WordPress.
24
-
25
- = How does it work? =
26
- Every image you add to a page or post will be automatically run through Smush.it behind the scenes. You don&rsquo;t have to do anything different.
27
-
28
- *N. B. In some cases GIFs should be replaced with PNG files. You can control this behaviour on the `Options` page. It is off by default.*
29
-
30
- = Existing images =
31
- You can also run your existing images through Smush.it via the WordPress `Media Library`. Click on the `Smush.it now!` link for any image you'd like to smush.
32
-
33
- = Privacy =
34
- Be sure you&rsquo;re comfortable with Smush.it&rsquo;s privacy policy (found on their <a href="http://smush.it/faq.php">FAQ</a>).
35
-
36
- = Updates, etc... =
37
-
38
- Plugin updates are announced on [http://www.twitter.com/TheCHANGELOG](http://www.twitter.com/TheCHANGELOG).
39
-
40
- == Screenshots ==
41
-
42
- 1. See the savings from Smush.it in the Media Library.
43
-
44
- == Installation ==
45
-
46
- 1. Upload the `wp-smushit` plugin to your `/wp-content/plugins/` directory.
47
- 1. Activate the plugin through the 'Plugins' menu in WordPress.
48
- 1. Done!
49
-
50
- == Contact and Credits ==
51
-
52
- Written by Alex Dunae at Dialect ([dialect.ca](http://dialect.ca/?wp_smush_it), e-mail 'alex' at 'dialect dot ca'), 2008-09.
53
-
54
- WP Smush.it includes a copy of the [PEAR JSON library](http://pear.php.net/pepr/pepr-proposal-show.php?id=198) written by Michal Migurski.
55
-
56
- Smush.it was created by [Nicole Sullivan](http://www.stubbornella.org/content/) and [Stoyan Stefanov](http://phpied.com/).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === WP Smush.it ===
2
+ Plugin Name: WP Smush.it
3
+ Version: 1.2.1
4
+ Author: Dialect
5
+ Author URI: http://dialect.ca/?wp_smush_it
6
+ Contributors: alexdunae
7
+ Tags: images, image, attachments, attachment
8
+ Requires at least: 2.5
9
+ Tested up to: 2.8.1
10
+ Stable tag: 1.2.1
11
+
12
+ Reduce image file sizes and improve performance using the <a href="http://smush.it/">Smush.it</a> API within WordPress.
13
+
14
+ == Description ==
15
+
16
+ Yahoo's excellent <a href="http://developer.yahoo.com/performance/">Exceptional Performance series</a> recommends <a href="http://developer.yahoo.com/performance/rules.html#opt_images">optimizing images</a> in several lossless ways:
17
+
18
+ * stripping meta data from JPEGs
19
+ * optimizing JPEG compression
20
+ * converting certain GIFs to indexed PNGs
21
+ * stripping the un-used colours from indexed images
22
+
23
+ <a href="http://smush.it/">Smush.it</a> offers an API that performs these optimizations (except for stripping JPEG meta data) automatically, and this plugin seamlessly integrates Smush.it with WordPress.
24
+
25
+ = How does it work? =
26
+ Every image you add to a page or post will be automatically run through Smush.it behind the scenes. You don&rsquo;t have to do anything different.
27
+
28
+ *N. B. In some cases GIFs should be replaced with PNG files. You can control this behaviour on the `Options` page. It is off by default.*
29
+
30
+ = Existing images =
31
+ You can also run your existing images through Smush.it via the WordPress `Media Library`. Click on the `Smush.it now!` link for any image you'd like to smush.
32
+
33
+ = Privacy =
34
+ Be sure you&rsquo;re comfortable with Smush.it&rsquo;s privacy policy (found on their <a href="http://smush.it/faq.php">FAQ</a>).
35
+
36
+ = Updates, etc... =
37
+
38
+ Plugin updates are announced on [http://www.twitter.com/TheCHANGELOG](http://www.twitter.com/TheCHANGELOG).
39
+
40
+ == Screenshots ==
41
+
42
+ 1. See the savings from Smush.it in the Media Library.
43
+
44
+ == Installation ==
45
+
46
+ 1. Upload the `wp-smushit` plugin to your `/wp-content/plugins/` directory.
47
+ 1. Activate the plugin through the 'Plugins' menu in WordPress.
48
+ 1. Done!
49
+
50
+ == Changelog ==
51
+
52
+ = 1.2.1 =
53
+ * added support for PHP 4
54
+ * created admin_action_ hook as workaround to WordPress 2.9's $_registered_pages security (see http://core.trac.wordpress.org/changeset/11596)
55
+ * add savings amount in bytes to Media Library (thx Yoast)
56
+
57
+ = 1.2 =
58
+ * added support for WP_Http
59
+
60
+ = 1.1.3 =
61
+ * fixed activation error when the PEAR JSON library is already loaded
62
+
63
+ = 1.1.2 =
64
+ * added test for allow_url_fopen
65
+
66
+ = 1.1.1 =
67
+ * added error message on PHP copy error
68
+
69
+ = 1.1 =
70
+ * improved handling of errors from Smush.it
71
+ * added ability to manually smush images from media library
72
+ * fixed inconsistent path handling from WP 2.5 -> WP 2.7
73
+
74
+ = 1.0.2 =
75
+ * added 'Not processed' status message when browsing media library
76
+
77
+ = 1.0.1 =
78
+ * added i10n functions
79
+
80
+ = 1.0 =
81
+ * first edition
82
+
83
+
84
+ == Contact and Credits ==
85
+
86
+ Written by Alex Dunae at Dialect ([dialect.ca](http://dialect.ca/?wp_smush_it), e-mail 'alex' at 'dialect dot ca'), 2008-09.
87
+
88
+ WP Smush.it includes a copy of the [PEAR JSON library](http://pear.php.net/pepr/pepr-proposal-show.php?id=198) written by Michal Migurski.
89
+
90
+ Smush.it was created by [Nicole Sullivan](http://www.stubbornella.org/content/) and [Stoyan Stefanov](http://phpied.com/).
smush.php DELETED
@@ -1,29 +0,0 @@
1
- <?php
2
- /**
3
- * Admin page for manually smushing an attachment.
4
- *
5
- * Expects an `attachment_ID` in the query string.
6
- *
7
- * @version 1.2
8
- * @package WP_SmushIt
9
- */
10
-
11
- if ( !isset($_GET['attachment_ID']) )
12
- wp_die('Must provide attachment ID');
13
-
14
- $attachment_ID = intval($_GET['attachment_ID']);
15
-
16
-
17
- $original_meta = wp_get_attachment_metadata( $attachment_ID );
18
-
19
-
20
- $new_meta = wp_smushit_resize_from_meta_data( $original_meta );
21
-
22
- wp_update_attachment_metadata( $attachment_ID, $new_meta );
23
-
24
- $sendback = wp_get_referer();
25
- $sendback = preg_replace('|[^a-z0-9-~+_.?#=&;,/:]|i', '', $sendback);
26
-
27
- wp_redirect($sendback);
28
-
29
- exit(0);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
wp-smushit.php CHANGED
@@ -1,7 +1,7 @@
1
  <?php
2
  /**
3
  * Integrate the Smush.it API into WordPress.
4
- * @version 1.2
5
  * @package WP_SmushIt
6
  */
7
  /*
@@ -9,7 +9,7 @@ Plugin Name: WP Smush.it
9
  Plugin URI: http://dialect.ca/code/wp-smushit/
10
  Description: Reduce image file sizes and improve performance using the <a href="http://smush.it/">Smush.it</a> API within WordPress.
11
  Author: Dialect
12
- Version: 1.2
13
  Author URI: http://dialect.ca/?wp_smush_it
14
  */
15
 
@@ -28,7 +28,7 @@ define('SMUSHIT_BASE_URL', 'http://smush.it/');
28
 
29
  define('WP_SMUSHIT_DOMAIN', 'wp_smushit');
30
 
31
- define('WP_SMUSHIT_UA', 'WP Smush.it/1.2 (+http://dialect.ca/code/wp-smushit)');
32
 
33
  define('WP_SMUSHIT_GIF_TO_PNG', intval(get_option('wp_smushit_gif_to_png')));
34
 
@@ -52,9 +52,66 @@ add_filter('manage_media_columns', 'wp_smushit_columns');
52
  add_action('manage_media_custom_column', 'wp_smushit_custom_column', 10, 2);
53
  add_action('admin_menu', 'wp_smushit_add_pages');
54
  add_action('admin_init', 'wp_smushit_init');
 
55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  /**
59
  * Process an image with Smush.it.
60
  *
@@ -87,12 +144,11 @@ function wp_smushit($file) {
87
  if ( false === $data )
88
  return array($file, __('Error posting to Smush.it', WP_SMUSHIT_DOMAIN));
89
 
90
-
91
  // make sure the response looks like JSON -- added 2008-12-19 when
92
  // Smush.it was returning PHP warnings before the JSON output
93
- if ( strpos( trim($data), '{' ) != 0 )
94
  return array($file, __('Bad response from Smush.it', WP_SMUSHIT_DOMAIN));
95
-
96
 
97
  // read the JSON response
98
  if ( function_exists('json_decode') ) {
@@ -112,7 +168,6 @@ function wp_smushit($file) {
112
 
113
  $processed_url = SMUSHIT_BASE_URL . $data->dest;
114
 
115
-
116
  $temp_file = wp_smushit_download($processed_url);
117
 
118
  if ( false === $temp_file )
@@ -129,14 +184,17 @@ function wp_smushit($file) {
129
 
130
  @rename( $temp_file, $file_path );
131
 
132
- $results_msg = sprintf(__("Reduced by %01.1f%%", WP_SMUSHIT_DOMAIN), $data->percent);
 
 
 
 
 
 
133
 
134
  return array($file, $results_msg);
135
  }
136
 
137
-
138
-
139
-
140
  /**
141
  * Update the attachment's meta data after being smushed.
142
  *
@@ -190,35 +248,6 @@ function wp_smushit_resize_from_meta_data($meta) {
190
  return $meta;
191
  }
192
 
193
-
194
- /**
195
- * Print column header for Smush.it results in the media library using
196
- * the `manage_media_columns` hook.
197
- */
198
- function wp_smushit_columns($defaults) {
199
- $defaults['smushit'] = 'Smush.it';
200
- return $defaults;
201
- }
202
-
203
- /**
204
- * Print column data for Smush.it results in the media library using
205
- * the `manage_media_custom_column` hook.
206
- */
207
- function wp_smushit_custom_column($column_name, $id) {
208
- if( $column_name == 'smushit' ) {
209
- $data = wp_get_attachment_metadata($id);
210
- if ( isset($data['wp_smushit']) && !empty($data['wp_smushit']) )
211
- print $data['wp_smushit'];
212
- else
213
- print __('Not processed', WP_SMUSHIT_DOMAIN);
214
-
215
- printf("<br><a href=\"admin.php?page=%s/smush.php&amp;attachment_ID=%d&amp;noheader\">%s</a>",
216
- WP_SMUSHIT_PLUGIN_DIR,
217
- $id,
218
- __('Smush.it now!', WP_SMUSHIT_DOMAIN));
219
- }
220
- }
221
-
222
  /**
223
  * Compare file names to see if the extension changed from `.gif` to `.png`.
224
  *
@@ -230,35 +259,6 @@ function wp_smushit_did_gif_to_png($orig, $new) {
230
 
231
  }
232
 
233
- function wp_smushit_install() {
234
- add_option('wp_smushit_gif_to_png', 0);
235
- }
236
-
237
- function wp_smushit_init() {
238
- load_plugin_textdomain(WP_SMUSHIT_DOMAIN);
239
- }
240
-
241
- function wp_smushit_add_pages() {
242
- add_options_page(__('WP Smush.it Options', WP_SMUSHIT_OPTIONS), 'WP Smush.it', 8, dirname(__FILE__) . '/options.php');
243
-
244
- add_filter( 'plugin_action_links', 'wp_smushit_filter_plugin_actions', 10, 2 );
245
- }
246
-
247
- function wp_smushit_filter_plugin_actions($links, $file) {
248
- if ( 'wp-smushit/wp-smushit.php' === $file ) {
249
- $settings_link = '<a href="options-general.php?page=wp-smushit/options.php">' . __('Settings') . '</a>';
250
- array_unshift( $links, $settings_link ); // before other links
251
- }
252
- return $links;
253
-
254
-
255
- }
256
-
257
- function wp_smushit_options() {
258
- include_once 'options.php';
259
- }
260
-
261
-
262
  /**
263
  * Download a remote file to a temp file.
264
  *
@@ -279,12 +279,27 @@ function wp_smushit_download($remote_file) {
279
 
280
  $data = wp_remote_retrieve_body($response);
281
 
282
- if ( false === file_put_contents($temp_file, $data) )
283
- return false;
 
 
 
 
 
 
 
 
 
 
 
 
 
284
 
285
  } else {
 
 
286
  // try using 'fopen' via 'copy'
287
- if( !@copy( $remote_file, $temp_file ) )
288
  return false;
289
  }
290
 
@@ -294,9 +309,6 @@ function wp_smushit_download($remote_file) {
294
  }
295
 
296
 
297
-
298
-
299
-
300
  /**
301
  * Post an image to Smush.it.
302
  *
@@ -308,7 +320,7 @@ function wp_smushit_post($file_url) {
308
 
309
  $data = false;
310
 
311
- if ( function_exists('wp_remote_get') ) {
312
  $response = wp_remote_get($req, array('user-agent' => WP_SMUSHIT_UA));
313
 
314
  if ( 200 != wp_remote_retrieve_response_code($response) )
@@ -316,20 +328,81 @@ function wp_smushit_post($file_url) {
316
 
317
  $data = wp_remote_retrieve_body($response);
318
  } else {
319
- if ( ! function_exists('fopen') || (function_exists('ini_get') && true != ini_get('allow_url_fopen')) ) {
320
- $err = __('Remote fopen is not enabled (<a href="http://dialect.ca/code/wp-smushit/#fopen_note" target="_blank">more info</a>)', WP_SMUSHIT_DOMAIN);
321
- wp_die($err);
322
- return false;
323
- }
324
 
325
  $fh = @fopen( $req, 'r' ); // post to Smush.it
326
 
327
  if ( !$fh )
328
  return false;
329
 
330
- $data = stream_get_contents( $fh );
 
 
 
 
 
 
 
331
  fclose( $fh );
332
  }
333
 
334
  return $data;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
335
  }
1
  <?php
2
  /**
3
  * Integrate the Smush.it API into WordPress.
4
+ * @version 1.2.1
5
  * @package WP_SmushIt
6
  */
7
  /*
9
  Plugin URI: http://dialect.ca/code/wp-smushit/
10
  Description: Reduce image file sizes and improve performance using the <a href="http://smush.it/">Smush.it</a> API within WordPress.
11
  Author: Dialect
12
+ Version: 1.2.1
13
  Author URI: http://dialect.ca/?wp_smush_it
14
  */
15
 
28
 
29
  define('WP_SMUSHIT_DOMAIN', 'wp_smushit');
30
 
31
+ define('WP_SMUSHIT_UA', 'WP Smush.it/1.2.1 (+http://dialect.ca/code/wp-smushit)');
32
 
33
  define('WP_SMUSHIT_GIF_TO_PNG', intval(get_option('wp_smushit_gif_to_png')));
34
 
52
  add_action('manage_media_custom_column', 'wp_smushit_custom_column', 10, 2);
53
  add_action('admin_menu', 'wp_smushit_add_pages');
54
  add_action('admin_init', 'wp_smushit_init');
55
+ add_action('admin_action_wp_smushit_manual', 'wp_smushit_manual');
56
 
57
+ /**
58
+ * Plugin admin functions
59
+ */
60
+
61
+ function wp_smushit_install() {
62
+ add_option('wp_smushit_gif_to_png', 0);
63
+ }
64
+
65
+ function wp_smushit_init() {
66
+ load_plugin_textdomain(WP_SMUSHIT_DOMAIN);
67
+ }
68
+
69
+ function wp_smushit_add_pages() {
70
+ global $_registered_pages;
71
+
72
+ add_options_page(__('WP Smush.it Options', WP_SMUSHIT_OPTIONS), 'WP Smush.it', 8, dirname(__FILE__) . '/options.php');
73
+ add_filter( 'plugin_action_links', 'wp_smushit_filter_plugin_actions', 10, 2 );
74
+ }
75
+
76
+ function wp_smushit_filter_plugin_actions($links, $file) {
77
+ if ( 'wp-smushit/wp-smushit.php' === $file ) {
78
+ $settings_link = '<a href="options-general.php?page=wp-smushit/options.php">' . __('Settings') . '</a>';
79
+ array_unshift( $links, $settings_link ); // before other links
80
+ }
81
+ return $links;
82
+ }
83
+
84
+ function wp_smushit_options() {
85
+ include_once 'options.php';
86
+ }
87
 
88
 
89
+ /**
90
+ * Manually process an image from the Media Library
91
+ */
92
+ function wp_smushit_manual() {
93
+ if ( FALSE === current_user_can('upload_files') ) {
94
+ wp_die(__('You don\'t have permission to work with uploaded files.', WP_SMUSHIT_DOMAIN));
95
+ }
96
+
97
+ if ( FALSE === isset($_GET['attachment_ID'])) {
98
+ wp_die(__('No attachment ID was provided.', WP_SMUSHIT_DOMAIN));
99
+ }
100
+
101
+ $attachment_ID = intval($_GET['attachment_ID']);
102
+
103
+ $original_meta = wp_get_attachment_metadata( $attachment_ID );
104
+
105
+ $new_meta = wp_smushit_resize_from_meta_data( $original_meta );
106
+
107
+ wp_update_attachment_metadata( $attachment_ID, $new_meta );
108
+
109
+ $sendback = wp_get_referer();
110
+ $sendback = preg_replace('|[^a-z0-9-~+_.?#=&;,/:]|i', '', $sendback);
111
+ wp_redirect($sendback);
112
+ exit(0);
113
+ }
114
+
115
  /**
116
  * Process an image with Smush.it.
117
  *
144
  if ( false === $data )
145
  return array($file, __('Error posting to Smush.it', WP_SMUSHIT_DOMAIN));
146
 
 
147
  // make sure the response looks like JSON -- added 2008-12-19 when
148
  // Smush.it was returning PHP warnings before the JSON output
149
+ if ( strpos( trim($data), '{' ) != 0 ) {
150
  return array($file, __('Bad response from Smush.it', WP_SMUSHIT_DOMAIN));
151
+ }
152
 
153
  // read the JSON response
154
  if ( function_exists('json_decode') ) {
168
 
169
  $processed_url = SMUSHIT_BASE_URL . $data->dest;
170
 
 
171
  $temp_file = wp_smushit_download($processed_url);
172
 
173
  if ( false === $temp_file )
184
 
185
  @rename( $temp_file, $file_path );
186
 
187
+ $savings = intval($data->src_size) - intval($data->dest_size);
188
+ $savings_str = wp_smushit_format_bytes($savings, 1);
189
+ $savings_str = str_replace(' ', '&nbsp;', $savings_str);
190
+
191
+ $results_msg = sprintf(__("Reduced by %01.1f%% (%s)", WP_SMUSHIT_DOMAIN),
192
+ $data->percent,
193
+ $savings_str);
194
 
195
  return array($file, $results_msg);
196
  }
197
 
 
 
 
198
  /**
199
  * Update the attachment's meta data after being smushed.
200
  *
248
  return $meta;
249
  }
250
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
251
  /**
252
  * Compare file names to see if the extension changed from `.gif` to `.png`.
253
  *
259
 
260
  }
261
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
262
  /**
263
  * Download a remote file to a temp file.
264
  *
279
 
280
  $data = wp_remote_retrieve_body($response);
281
 
282
+ if ( function_exists('file_put_contents') ) {
283
+ if ( false === file_put_contents($temp_file, $data) )
284
+ return false;
285
+
286
+ } else { // PHP < 5
287
+ $fh = @fopen($temp_file, 'w');
288
+
289
+ if ( FALSE === $fh )
290
+ return false;
291
+
292
+ if ( FALSE === fwrite($fh, $data) )
293
+ return false;
294
+
295
+ fclose($fh);
296
+ }
297
 
298
  } else {
299
+ wp_smushit_check_url_fopen();
300
+
301
  // try using 'fopen' via 'copy'
302
+ if( FALSE === @copy( $remote_file, $temp_file ) )
303
  return false;
304
  }
305
 
309
  }
310
 
311
 
 
 
 
312
  /**
313
  * Post an image to Smush.it.
314
  *
320
 
321
  $data = false;
322
 
323
+ if ( FALSE && function_exists('wp_remote_get') ) {
324
  $response = wp_remote_get($req, array('user-agent' => WP_SMUSHIT_UA));
325
 
326
  if ( 200 != wp_remote_retrieve_response_code($response) )
328
 
329
  $data = wp_remote_retrieve_body($response);
330
  } else {
331
+ wp_smushit_check_url_fopen();
 
 
 
 
332
 
333
  $fh = @fopen( $req, 'r' ); // post to Smush.it
334
 
335
  if ( !$fh )
336
  return false;
337
 
338
+ if( FALSE && function_exists('stream_get_contents') ) {
339
+ $data = stream_get_contents($fh);
340
+ } else {
341
+ while( FALSE === feof($fh) ) {
342
+ $data .= fread($fh, 8192);
343
+ }
344
+ }
345
+
346
  fclose( $fh );
347
  }
348
 
349
  return $data;
350
+ }
351
+
352
+ /**
353
+ * Check if `allow_url_fopen` is `true`.
354
+ * Calls `wp_die()` if not, otherwise returns true.
355
+ */
356
+ function wp_smushit_check_url_fopen() {
357
+ if ( FALSE === function_exists('fopen') ||
358
+ FALSE === ini_get('allow_url_fopen') ) {
359
+ $err = __('Remote fopen is not enabled (<a href="http://dialect.ca/code/wp-smushit/#fopen_note" target="_blank">more info</a>)', WP_SMUSHIT_DOMAIN);
360
+ wp_die($err);
361
+ return false;
362
+ }
363
+
364
+ return true;
365
+ }
366
+
367
+ /**
368
+ * Print column header for Smush.it results in the media library using
369
+ * the `manage_media_columns` hook.
370
+ */
371
+ function wp_smushit_columns($defaults) {
372
+ $defaults['smushit'] = 'Smush.it';
373
+ return $defaults;
374
+ }
375
+
376
+ /**
377
+ * Return the filesize in a humanly readable format.
378
+ * Taken from http://www.php.net/manual/en/function.filesize.php#91477
379
+ */
380
+ function wp_smushit_format_bytes($bytes, $precision = 2) {
381
+ $units = array('B', 'KB', 'MB', 'GB', 'TB');
382
+ $bytes = max($bytes, 0);
383
+ $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
384
+ $pow = min($pow, count($units) - 1);
385
+ $bytes /= pow(1024, $pow);
386
+ return round($bytes, $precision) . ' ' . $units[$pow];
387
+ }
388
+
389
+ /**
390
+ * Print column data for Smush.it results in the media library using
391
+ * the `manage_media_custom_column` hook.
392
+ */
393
+ function wp_smushit_custom_column($column_name, $id) {
394
+ if( $column_name == 'smushit' ) {
395
+ $data = wp_get_attachment_metadata($id);
396
+ if ( isset($data['wp_smushit']) && !empty($data['wp_smushit']) ) {
397
+ print $data['wp_smushit'];
398
+ printf("<br><a href=\"admin.php?action=wp_smushit_manual&amp;attachment_ID=%d\">%s</a>",
399
+ $id,
400
+ __('Re-smush', WP_SMUSHIT_DOMAIN));
401
+ } else {
402
+ print __('Not processed', WP_SMUSHIT_DOMAIN);
403
+ printf("<br><a href=\"admin.php?action=wp_smushit_manual&amp;attachment_ID=%d\">%s</a>",
404
+ $id,
405
+ __('Smush.it now!', WP_SMUSHIT_DOMAIN));
406
+ }
407
+ }
408
  }