Smush Image Compression and Optimization - Version 1.3.1

Version Description

Download this release

Release Info

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

Code changes from version 1.2.10 to 1.3.1

Files changed (3) hide show
  1. options.php +0 -33
  2. readme.txt +13 -10
  3. wp-smushit.php +29 -123
options.php DELETED
@@ -1,33 +0,0 @@
1
- <?php
2
- $wp_smushit_gif_to_png = intval(get_option('wp_smushit_gif_to_png'));
3
- ?>
4
- <div class="wrap">
5
- <h2>WP Smush.it</h2>
6
- <form method="post" action="options.php">
7
- <input type="hidden" name="action" value="update" />
8
- <input type="hidden" name="page_options" value="wp_smushit_gif_to_png" />
9
- <?php wp_nonce_field('update-options'); ?>
10
- <table class="form-table">
11
- <tr valign="top">
12
- <th scope="row"><?php _e('When Smush.it suggests converting a GIF to a PNG file&hellip;', WP_SMUSHIT_DOMAIN); ?></th>
13
- <td>
14
- <select name="wp_smushit_gif_to_png" id="wp_smushit_gif_to_png">
15
- <option value="1"<?php echo $wp_smushit_gif_to_png == 1 ? ' selected="selected"' : ''; ?>><?php _e('Overwrite the GIF with a PNG', WP_SMUSHIT_DOMAIN); ?></option>
16
- <option value="0"<?php echo $wp_smushit_gif_to_png == 0 ? ' selected="selected"' : ''; ?>><?php _e('Leave the GIF alone', WP_SMUSHIT_DOMAIN); ?></option>
17
- </select>
18
- </td>
19
- </tr>
20
- </table>
21
- <p class="submit"><input type="submit" name="Submit" value="<?php _e('Save Changes') ?>" /></p>
22
-
23
- <?php
24
- //echo get_template_directory() .'/style.css';
25
- //$theme_data = get_theme_data( get_template_directory() .'/style.css');
26
- //var_dump($theme_data);
27
- ?>
28
-
29
- <!--<p><a href="admin.php?action=wp_smushit_theme&amp;theme=<?php echo urlencode(get_template_directory()); ?>">Smush current theme</a> (<code><?php echo get_current_theme(); ?></code>)</p>-->
30
-
31
-
32
- </form>
33
- </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
readme.txt CHANGED
@@ -1,13 +1,13 @@
1
  === WP Smush.it ===
2
  Plugin Name: WP Smush.it
3
- Version: 1.2.10
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.9.2
10
- Stable tag: 1.2.19
11
 
12
  Reduce image file sizes and improve performance using the <a href="http://smush.it/">Smush.it</a> API within WordPress.
13
 
@@ -25,13 +25,11 @@ Yahoo's excellent <a href="http://developer.yahoo.com/performance/">Exceptional
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
 
@@ -47,9 +45,14 @@ Plugin updates are announced on [http://www.twitter.com/TheCHANGELOG](http://www
47
  1. Activate the plugin through the 'Plugins' menu in WordPress.
48
  1. Done!
49
 
50
- == Changelog ==
51
- = 1.2.10 =
52
- * removed testing link
 
 
 
 
 
53
 
54
  = 1.2.9 =
55
  * updated Smush.it endpoint URL
1
  === WP Smush.it ===
2
  Plugin Name: WP Smush.it
3
+ Version: 1.3.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.8
9
+ Tested up to: 3.0.3
10
+ Stable tag: 1.3.1
11
 
12
  Reduce image file sizes and improve performance using the <a href="http://smush.it/">Smush.it</a> API within WordPress.
13
 
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
  = Existing images =
29
  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.
30
 
31
  = Privacy =
32
+ Be sure you&rsquo;re comfortable with Smush.it&rsquo;s privacy policy (found on their <a href="http://info.yahoo.com/legal/us/yahoo/smush_it/smush_it-4378.html">FAQ</a>).
33
 
34
  = Updates, etc... =
35
 
45
  1. Activate the plugin through the 'Plugins' menu in WordPress.
46
  1. Done!
47
 
48
+ == Changelog ==
49
+
50
+ = 1.3.1
51
+ * handle images stored on other domains -- props to [ka-ri-ne](http://wordpress.org/support/profile/ka-ri-ne) for the fix
52
+ * avoid time-out errors when working with larger files -- props to [Milan Dinić](http://wordpress.org/support/profile/dimadin) for the fix
53
+
54
+ = 1.2.10 =
55
+ * removed testing link
56
 
57
  = 1.2.9 =
58
  * updated Smush.it endpoint URL
wp-smushit.php CHANGED
@@ -1,7 +1,7 @@
1
  <?php
2
  /**
3
  * Integrate the Smush.it API into WordPress.
4
- * @version 1.2.10
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.10
13
  Author URI: http://dialect.ca/?wp_smush_it
14
  */
15
 
@@ -17,44 +17,31 @@ if ( !function_exists('json_encode') ) {
17
  require_once('JSON/JSON.php');
18
  }
19
 
 
 
 
20
 
21
  /**
22
  * Constants
23
  */
24
-
25
- //define('SMUSHIT_REQ_URL', 'http://smushit.com/ws.php?img=%s');
26
- //define('SMUSHIT_REQ_URL', 'http://ws1.adq.ac4.yahoo.com/ysmush.it/ws.php?img=%s');
27
  define('SMUSHIT_REQ_URL', 'http://www.smushit.com/ysmush.it/ws.php?img=%s');
28
-
29
  define('SMUSHIT_BASE_URL', 'http://www.smushit.com/');
30
 
31
-
32
  define('WP_SMUSHIT_DOMAIN', 'wp_smushit');
33
-
34
- define('WP_SMUSHIT_UA', 'WP Smush.it/1.2.9 (+http://dialect.ca/code/wp-smushit)');
35
-
36
  define('WP_SMUSHIT_GIF_TO_PNG', intval(get_option('wp_smushit_gif_to_png')));
37
-
38
  define('WP_SMUSHIT_PLUGIN_DIR', dirname(plugin_basename(__FILE__)));
39
 
40
- if ( !defined('WP_CONTENT_URL') )
41
- define('WP_CONTENT_URL', get_option('siteurl') . '/wp-content');
42
- //PATH_SEPARATOR
43
- if ( !defined('WP_CONTENT_DIR') )
44
- define('WP_CONTENT_DIR', ABSPATH . 'wp-content' );
45
 
46
  /**
47
  * Hooks
48
  */
49
-
50
  register_activation_hook(__FILE__,'wp_smushit_install');
51
 
52
  add_filter('wp_generate_attachment_metadata', 'wp_smushit_resize_from_meta_data');
53
-
54
  add_filter('manage_media_columns', 'wp_smushit_columns');
55
  add_action('manage_media_custom_column', 'wp_smushit_custom_column', 10, 2);
56
- add_action('admin_menu', 'wp_smushit_add_pages');
57
- add_action('admin_init', 'wp_smushit_init');
58
  add_action('admin_action_wp_smushit_manual', 'wp_smushit_manual');
59
  //add_action('admin_action_wp_smushit_theme', 'wp_smushit_theme');
60
 
@@ -62,22 +49,16 @@ add_action('admin_action_wp_smushit_manual', 'wp_smushit_manual');
62
  /**
63
  * Plugin admin functions
64
  */
65
-
66
  function wp_smushit_install() {
67
- add_option('wp_smushit_gif_to_png', 0);
 
68
  }
69
 
70
- function wp_smushit_init() {
71
  load_plugin_textdomain(WP_SMUSHIT_DOMAIN);
72
  wp_enqueue_script('common');
73
  }
74
 
75
- function wp_smushit_add_pages() {
76
- //add_submenu_page('themes.php', 'Smush.it', 'WP Smush.it', 8, dirname(__FILE__) . '/theme.php');
77
- add_options_page(__('WP Smush.it Options', WP_SMUSHIT_DOMAIN), 'WP Smush.it', 8, dirname(__FILE__) . '/options.php');
78
- add_filter( 'plugin_action_links', 'wp_smushit_filter_plugin_actions', 10, 2 );
79
- }
80
-
81
  function wp_smushit_filter_plugin_actions($links, $file) {
82
  if ( 'wp-smushit/wp-smushit.php' === $file ) {
83
  $settings_link = '<a href="options-general.php?page=wp-smushit/options.php">' . __('Settings') . '</a>';
@@ -86,10 +67,6 @@ function wp_smushit_filter_plugin_actions($links, $file) {
86
  return $links;
87
  }
88
 
89
- function wp_smushit_options() {
90
- include_once 'options.php';
91
- }
92
-
93
  /**
94
  * Process all the images from a given theme
95
  */
@@ -152,14 +129,17 @@ function wp_smushit($file) {
152
  }
153
 
154
  // check that the file is within the WP_CONTENT_DIR
155
- if ( 0 !== stripos($file_path, realpath(WP_CONTENT_DIR)) ) {
156
- $msg = sprintf(__("<span class='code'>%s</span> must be within the content directory (<span class='code'>%s</span>)", WP_SMUSHIT_DOMAIN), htmlentities($file_path), WP_CONTENT_DIR);
 
 
 
157
 
158
  return array($file, $msg);
159
  }
160
 
161
  // determine the public URL
162
- $file_url = str_replace( WP_CONTENT_DIR, WP_CONTENT_URL, $file );
163
 
164
  $data = wp_smushit_post($file_url);
165
 
@@ -205,17 +185,6 @@ function wp_smushit($file) {
205
  return array($file, $results_msg );
206
  }
207
 
208
-
209
-
210
- // check if Smush.it converted a GIF to a PNG
211
- if( 1 == WP_SMUSHIT_GIF_TO_PNG && wp_smushit_did_gif_to_png($file, $data->dest) ) {
212
- $file = preg_replace('/.gif$/i', '.png', $file);
213
- $file_path = preg_replace('/.gif$/i', '.png', $file_path);
214
-
215
- if ( FALSE === has_filter('wp_update_attachment_metadata', 'wp_smushit_update_attachment') )
216
- add_filter('wp_update_attachment_metadata', 'wp_smushit_update_attachment', 10, 2);
217
- }
218
-
219
  @rename( $temp_file, $file_path );
220
 
221
  $savings = intval($data->src_size) - intval($data->dest_size);
@@ -229,32 +198,6 @@ function wp_smushit($file) {
229
  return array($file, $results_msg);
230
  }
231
 
232
- /**
233
- * Update the attachment's meta data after being smushed.
234
- *
235
- * This is only needed when GIFs become PNGs so we add the filter near
236
- * the end of `wp_smushit()`. It's used by the `wp_update_attachment_metadata`
237
- * hook, which is called after the `wp_generate_attachment_metadata` on upload.
238
- */
239
- function wp_smushit_update_attachment($data, $ID) {
240
- $orig_file = get_attached_file( $ID );
241
-
242
- if( wp_smushit_did_gif_to_png($orig_file, $data['file']) ) {
243
- update_attached_file( $ID, $data['file'] );
244
-
245
- // get_media_item() uses the GUID for a display title so we should
246
- // update the GUID here
247
- $post = get_post( $ID );
248
- $guid = preg_replace('/.gif$/i', '.png', $post->guid);
249
-
250
- wp_update_post( array('ID' => $ID,
251
- 'post_mime_type' => 'image/png',
252
- 'guid' => $guid) );
253
- }
254
-
255
- return $data;
256
- }
257
-
258
 
259
  /**
260
  * Read the image paths from an attachment's meta data and process each image
@@ -306,16 +249,6 @@ function wp_smushit_resize_from_meta_data($meta) {
306
  return $meta;
307
  }
308
 
309
- /**
310
- * Compare file names to see if the extension changed from `.gif` to `.png`.
311
- *
312
- * @returns bool
313
- */
314
- function wp_smushit_did_gif_to_png($orig, $new) {
315
- return (0 === stripos(strrev($new), 'gnp.') &&
316
- 0 === stripos(strrev($orig), 'fig.') );
317
-
318
- }
319
 
320
  /**
321
  * Post an image to Smush.it.
@@ -327,57 +260,24 @@ function wp_smushit_post($file_url) {
327
  $req = sprintf( SMUSHIT_REQ_URL, urlencode( $file_url ) );
328
 
329
  $data = false;
330
-
331
  if ( function_exists('wp_remote_get') ) {
 
332
  $response = wp_remote_get($req, array('user-agent' => WP_SMUSHIT_UA));
333
 
334
- if ( 200 != wp_remote_retrieve_response_code($response) ) {
335
- return false;
336
  }
337
 
338
  $data = wp_remote_retrieve_body($response);
 
339
  } else {
340
- wp_smushit_check_url_fopen();
341
-
342
- $fh = @fopen( $req, 'r' ); // post to Smush.it
343
-
344
- if ( !$fh )
345
- return false;
346
-
347
- if( FALSE && function_exists('stream_get_contents') ) {
348
- $data = stream_get_contents($fh);
349
- } else {
350
- while( FALSE === feof($fh) ) {
351
- $data .= fread($fh, 8192);
352
- }
353
- }
354
-
355
- fclose( $fh );
356
  }
357
-
358
  return $data;
359
  }
360
 
361
- /**
362
- * Check if `allow_url_fopen` is `true`.
363
- * Calls `wp_die()` if not, otherwise returns true.
364
- */
365
- function wp_smushit_check_url_fopen() {
366
- $use_fopen = false;
367
-
368
- if ( class_exists('WP_Http_Fopen') ) {
369
- $use_fopen = WP_Http_Fopen::test();
370
- } else {
371
- $use_fopen = ( function_exists('fopen') && ini_get('allow_url_fopen') == true );
372
- }
373
-
374
- if ( !$use_fopen ) {
375
- $err = __('Remote fopen is not enabled (<a href="http://dialect.ca/code/wp-smushit/#fopen_note" target="_blank">more info</a>)', WP_SMUSHIT_DOMAIN);
376
- wp_die($err);
377
- }
378
-
379
- return $use_fopen;
380
- }
381
 
382
  /**
383
  * Print column header for Smush.it results in the media library using
@@ -420,4 +320,10 @@ function wp_smushit_custom_column($column_name, $id) {
420
  __('Smush.it now!', WP_SMUSHIT_DOMAIN));
421
  }
422
  }
 
 
 
 
 
 
423
  }
1
  <?php
2
  /**
3
  * Integrate the Smush.it API into WordPress.
4
+ * @version 1.3.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.3.1
13
  Author URI: http://dialect.ca/?wp_smush_it
14
  */
15
 
17
  require_once('JSON/JSON.php');
18
  }
19
 
20
+ if ( !function_exists('download_url') ) {
21
+ require_once(ABSPATH . 'wp-admin/includes/file.php');
22
+ }
23
 
24
  /**
25
  * Constants
26
  */
 
 
 
27
  define('SMUSHIT_REQ_URL', 'http://www.smushit.com/ysmush.it/ws.php?img=%s');
 
28
  define('SMUSHIT_BASE_URL', 'http://www.smushit.com/');
29
 
 
30
  define('WP_SMUSHIT_DOMAIN', 'wp_smushit');
31
+ define('WP_SMUSHIT_UA', 'WP Smush.it/1.3.0 (+http://dialect.ca/code/wp-smushit)');
 
 
32
  define('WP_SMUSHIT_GIF_TO_PNG', intval(get_option('wp_smushit_gif_to_png')));
 
33
  define('WP_SMUSHIT_PLUGIN_DIR', dirname(plugin_basename(__FILE__)));
34
 
 
 
 
 
 
35
 
36
  /**
37
  * Hooks
38
  */
 
39
  register_activation_hook(__FILE__,'wp_smushit_install');
40
 
41
  add_filter('wp_generate_attachment_metadata', 'wp_smushit_resize_from_meta_data');
 
42
  add_filter('manage_media_columns', 'wp_smushit_columns');
43
  add_action('manage_media_custom_column', 'wp_smushit_custom_column', 10, 2);
44
+ add_action('admin_init', 'wp_smushit_admin_init');
 
45
  add_action('admin_action_wp_smushit_manual', 'wp_smushit_manual');
46
  //add_action('admin_action_wp_smushit_theme', 'wp_smushit_theme');
47
 
49
  /**
50
  * Plugin admin functions
51
  */
 
52
  function wp_smushit_install() {
53
+ // clean up from previous versions
54
+ delete_option('wp_smushit_gif_to_png');
55
  }
56
 
57
+ function wp_smushit_admin_init() {
58
  load_plugin_textdomain(WP_SMUSHIT_DOMAIN);
59
  wp_enqueue_script('common');
60
  }
61
 
 
 
 
 
 
 
62
  function wp_smushit_filter_plugin_actions($links, $file) {
63
  if ( 'wp-smushit/wp-smushit.php' === $file ) {
64
  $settings_link = '<a href="options-general.php?page=wp-smushit/options.php">' . __('Settings') . '</a>';
67
  return $links;
68
  }
69
 
 
 
 
 
70
  /**
71
  * Process all the images from a given theme
72
  */
129
  }
130
 
131
  // check that the file is within the WP_CONTENT_DIR
132
+ $upload_dir = wp_upload_dir();
133
+ $wp_upload_dir = $upload_dir['basedir'];
134
+ $wp_upload_url = $upload_dir['baseurl'];
135
+ if ( 0 !== stripos(realpath($file_path), realpath($wp_upload_dir)) ) {
136
+ $msg = sprintf(__("<span class='code'>%s</span> must be within the content directory (<span class='code'>%s</span>)", WP_SMUSHIT_DOMAIN), htmlentities($file_path), $wp_upload_dir);
137
 
138
  return array($file, $msg);
139
  }
140
 
141
  // determine the public URL
142
+ $file_url = str_replace( $wp_upload_dir, $wp_upload_url, $file );
143
 
144
  $data = wp_smushit_post($file_url);
145
 
185
  return array($file, $results_msg );
186
  }
187
 
 
 
 
 
 
 
 
 
 
 
 
188
  @rename( $temp_file, $file_path );
189
 
190
  $savings = intval($data->src_size) - intval($data->dest_size);
198
  return array($file, $results_msg);
199
  }
200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
 
202
  /**
203
  * Read the image paths from an attachment's meta data and process each image
249
  return $meta;
250
  }
251
 
 
 
 
 
 
 
 
 
 
 
252
 
253
  /**
254
  * Post an image to Smush.it.
260
  $req = sprintf( SMUSHIT_REQ_URL, urlencode( $file_url ) );
261
 
262
  $data = false;
263
+
264
  if ( function_exists('wp_remote_get') ) {
265
+ add_filter('http_request_timeout', 'wp_smushit_http_request_timeout');
266
  $response = wp_remote_get($req, array('user-agent' => WP_SMUSHIT_UA));
267
 
268
+ if( is_wp_error( $response ) ) {
269
+ wp_die( $response );
270
  }
271
 
272
  $data = wp_remote_retrieve_body($response);
273
+ remove_filter('http_request_timeout', 'wp_smushit_http_request_timeout');
274
  } else {
275
+ wp_die( __('WP Smush.it requires WordPress 2.8 or greater', WP_SMUSHIT_DOMAIN) );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
276
  }
277
+
278
  return $data;
279
  }
280
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
281
 
282
  /**
283
  * Print column header for Smush.it results in the media library using
320
  __('Smush.it now!', WP_SMUSHIT_DOMAIN));
321
  }
322
  }
323
+ }
324
+ /**
325
+ * http_request_timeout filter -- bumpbed up to 25 seconds for larger images
326
+ */
327
+ function wp_smushit_http_request_timeout($time) {
328
+ return 25;
329
  }