Smush Image Compression and Optimization - Version 1.2

Version Description

Download this release

Release Info

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

Code changes from version 1.1.3 to 1.2

Files changed (4) hide show
  1. history.txt +3 -0
  2. readme.txt +6 -6
  3. smush.php +1 -1
  4. wp-smushit.php +105 -22
history.txt CHANGED
@@ -1,5 +1,8 @@
1
  === WP Smush.it Release History ===
2
 
 
 
 
3
  == 1.1.3 ==
4
  * fixed activation error when the PEAR JSON library is already loaded
5
 
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
 
readme.txt CHANGED
@@ -1,13 +1,13 @@
1
  === WP Smush.it ===
2
  Plugin Name: WP Smush.it
3
- Version: 1.1.3
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.7
10
- Stable tag: 1.1.3
11
 
12
  Reduce image file sizes and improve performance using the <a href="http://smush.it/">Smush.it</a> API within WordPress.
13
 
@@ -33,9 +33,9 @@ You can also run your existing images through Smush.it via the WordPress `Media
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
- = Did you find this plugin useful? =
37
 
38
- You can write a "recommendation" for me on my [LinkedIn profile](http://www.linkedin.com/in/alexdunae).
39
 
40
  == Screenshots ==
41
 
@@ -49,7 +49,7 @@ You can write a "recommendation" for me on my [LinkedIn profile](http://www.link
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.
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
 
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
 
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
 
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
 
smush.php CHANGED
@@ -4,7 +4,7 @@
4
  *
5
  * Expects an `attachment_ID` in the query string.
6
  *
7
- * @version 1.1.3
8
  * @package WP_SmushIt
9
  */
10
 
4
  *
5
  * Expects an `attachment_ID` in the query string.
6
  *
7
+ * @version 1.2
8
  * @package WP_SmushIt
9
  */
10
 
wp-smushit.php CHANGED
@@ -1,7 +1,7 @@
1
  <?php
2
  /**
3
  * Integrate the Smush.it API into WordPress.
4
- * @version 1.1.3
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.1.3
13
  Author URI: http://dialect.ca/?wp_smush_it
14
  */
15
 
@@ -28,6 +28,8 @@ define('SMUSHIT_BASE_URL', 'http://smush.it/');
28
 
29
  define('WP_SMUSHIT_DOMAIN', 'wp_smushit');
30
 
 
 
31
  define('WP_SMUSHIT_GIF_TO_PNG', intval(get_option('wp_smushit_gif_to_png')));
32
 
33
  define('WP_SMUSHIT_PLUGIN_DIR', dirname(plugin_basename(__FILE__)));
@@ -51,6 +53,8 @@ add_action('manage_media_custom_column', 'wp_smushit_custom_column', 10, 2);
51
  add_action('admin_menu', 'wp_smushit_add_pages');
52
  add_action('admin_init', 'wp_smushit_init');
53
 
 
 
54
  /**
55
  * Process an image with Smush.it.
56
  *
@@ -61,8 +65,8 @@ add_action('admin_init', 'wp_smushit_init');
61
  */
62
  function wp_smushit($file) {
63
  // dont't run on localhost
64
- // if( '127.0.0.1' == $_SERVER['SERVER_ADDR'] )
65
- // return array($file, __('Not processed (local file)', WP_SMUSHIT_DOMAIN));
66
 
67
 
68
  $file_path = $file;
@@ -78,27 +82,18 @@ function wp_smushit($file) {
78
  $file_url = trailingslashit( $uploads['baseurl'] ) . $file;
79
  }
80
 
81
- $req = sprintf( SMUSHIT_REQ_URL, urlencode( $file_url ) );
82
-
83
- // test for allow_url_fopen -- this is temporary and will be replaced with the new
84
- // WP_Http class in the near future
85
- // borrowed from wp-includes/http.php, line 696
86
- if ( ! function_exists('fopen') || (function_exists('ini_get') && true != ini_get('allow_url_fopen')) )
87
- return array($file, __('Remote fopen is not enabled (<a href="http://dialect.ca/code/wp-smushit/#fopen_note" target="_blank">more info</a>)', WP_SMUSHIT_DOMAIN));
88
-
89
- $fh = @fopen( $req, 'r' ); // post to Smush.it
90
 
91
- if ( !$fh )
92
  return array($file, __('Error posting to Smush.it', WP_SMUSHIT_DOMAIN));
93
 
94
- $data = stream_get_contents( $fh );
95
- fclose( $fh );
96
 
97
  // make sure the response looks like JSON -- added 2008-12-19 when
98
- // Smush.it was returning PHP warnings before the JSON
99
  if ( strpos( trim($data), '{' ) != 0 )
100
  return array($file, __('Bad response from Smush.it', WP_SMUSHIT_DOMAIN));
101
 
 
102
  // read the JSON response
103
  if ( function_exists('json_decode') ) {
104
  $data = json_decode( $data );
@@ -115,15 +110,13 @@ function wp_smushit($file) {
115
  return array($file, __($err, WP_SMUSHIT_DOMAIN) );
116
  }
117
 
118
- // download the processed image to a temp file
119
  $processed_url = SMUSHIT_BASE_URL . $data->dest;
120
 
121
- $temp_file = tempnam( WP_CONTENT_DIR, '___' );
122
 
123
- if( !@copy( $processed_url, $temp_file ) )
124
- return array($file, __('Error updating file', WP_SMUSHIT_DOMAIN) );
125
 
126
- chmod( $temp_file, 0644 );
 
127
 
128
  // check if Smush.it converted a GIF to a PNG
129
  if( 1 == WP_SMUSHIT_GIF_TO_PNG && wp_smushit_did_gif_to_png($file, $data->dest) ) {
@@ -142,6 +135,8 @@ function wp_smushit($file) {
142
  }
143
 
144
 
 
 
145
  /**
146
  * Update the attachment's meta data after being smushed.
147
  *
@@ -245,8 +240,96 @@ function wp_smushit_init() {
245
 
246
  function wp_smushit_add_pages() {
247
  add_options_page(__('WP Smush.it Options', WP_SMUSHIT_OPTIONS), 'WP Smush.it', 8, dirname(__FILE__) . '/options.php');
 
 
 
 
 
 
 
 
 
 
 
 
248
  }
249
 
250
  function wp_smushit_options() {
251
  include_once 'options.php';
252
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  <?php
2
  /**
3
  * Integrate the Smush.it API into WordPress.
4
+ * @version 1.2
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
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 (+http://dialect.ca/code/wp-smushit)');
32
+
33
  define('WP_SMUSHIT_GIF_TO_PNG', intval(get_option('wp_smushit_gif_to_png')));
34
 
35
  define('WP_SMUSHIT_PLUGIN_DIR', dirname(plugin_basename(__FILE__)));
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
  *
65
  */
66
  function wp_smushit($file) {
67
  // dont't run on localhost
68
+ if( '127.0.0.1' == $_SERVER['SERVER_ADDR'] )
69
+ return array($file, __('Not processed (local file)', WP_SMUSHIT_DOMAIN));
70
 
71
 
72
  $file_path = $file;
82
  $file_url = trailingslashit( $uploads['baseurl'] ) . $file;
83
  }
84
 
85
+ $data = wp_smushit_post($file_url);
 
 
 
 
 
 
 
 
86
 
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') ) {
99
  $data = json_decode( $data );
110
  return array($file, __($err, WP_SMUSHIT_DOMAIN) );
111
  }
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 )
119
+ return array($file, __('Error updating file', WP_SMUSHIT_DOMAIN) );
120
 
121
  // check if Smush.it converted a GIF to a PNG
122
  if( 1 == WP_SMUSHIT_GIF_TO_PNG && wp_smushit_did_gif_to_png($file, $data->dest) ) {
135
  }
136
 
137
 
138
+
139
+
140
  /**
141
  * Update the attachment's meta data after being smushed.
142
  *
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
+ *
265
+ * Used to download a processed image from Smush.it.
266
+ *
267
+ * @param string $remote_file URL of the file to download
268
+ * @return string|boolean Returns the temp file path on success or else FALSE
269
+ */
270
+ function wp_smushit_download($remote_file) {
271
+ $temp_file = tempnam( WP_CONTENT_DIR, '___' );
272
+
273
+ if ( function_exists('wp_remote_get') ) {
274
+ // try using WordPress's built-in HTTP class
275
+ $response = wp_remote_get($remote_file, array('user-agent' => WP_SMUSHIT_UA));
276
+
277
+ if ( 200 != wp_remote_retrieve_response_code($response) )
278
+ return false;
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
+
291
+ chmod( $temp_file, 0644 );
292
+
293
+ return $temp_file;
294
+ }
295
+
296
+
297
+
298
+
299
+
300
+ /**
301
+ * Post an image to Smush.it.
302
+ *
303
+ * @param string $file_url URL of the file to send to Smush.it
304
+ * @return string|boolean Returns the JSON response on success or else FALSE
305
+ */
306
+ function wp_smushit_post($file_url) {
307
+ $req = sprintf( SMUSHIT_REQ_URL, urlencode( $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) )
315
+ return false;
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
+ }