Smush Image Compression and Optimization - Version 1.5.0

Version Description

  • added basic integration for the NextGEN gallery plugin
  • add support for media bulk action dropdown
  • compatibility with WordPress earlier than 3.1
  • added a donate link
Download this release

Release Info

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

Code changes from version 1.4.3 to 1.5.0

Files changed (3) hide show
  1. bulk.php +7 -8
  2. readme.txt +16 -2
  3. wp-smushit.php +76 -21
bulk.php CHANGED
@@ -6,7 +6,7 @@
6
  if ( sizeof($attachments) < 1 ):
7
  echo '<p>You don’t appear to have uploaded any images yet.</p>';
8
  else:
9
- if ( empty($_POST) ): // instructions page
10
  ?>
11
  <p>This tool will run all of the images in your media library through the WP Smush.it web service. It won't re-smush images that were successfully smushed before. It will retry images that were not successfully smushed.</p>
12
 
@@ -19,20 +19,20 @@
19
  <p><strong>This is an experimental feature.</strong> Please post any feedback to the <a href="http://wordpress.org/tags/wp-smushit">WordPress WP Smush.it forums</a>.</p>
20
 
21
  <form method="post" action="">
22
- <?php wp_nonce_field( 'wp-smushit-bulk', '_wp-smushit-bulk-nonce'); ?>
23
  <button type="submit" class="button-secondary action">Run all my images through WP Smush.it right now</button>
24
  </form>
25
 
26
  <?php
27
  else: // run the script
28
 
29
- if (!wp_verify_nonce( $_POST['_wp-smushit-bulk-nonce'], 'wp-smushit-bulk' ) || !current_user_can( 'edit_others_posts' ) ) {
30
  wp_die( __( 'Cheatin&#8217; uh?' ) );
31
  }
32
 
 
33
  ob_implicit_flush(true);
34
  ob_end_flush();
35
-
36
  foreach( $attachments as $attachment ) {
37
  printf( "<p>Processing <strong>%s</strong>&hellip;<br>", esc_html($attachment->post_name) );
38
  $original_meta = wp_get_attachment_metadata( $attachment->ID, true );
@@ -41,8 +41,8 @@
41
 
42
  printf( "– %dx%d: ", intval($meta['width']), intval($meta['height']) );
43
 
44
- if ( $original_meta['wp_smushit'] == $meta['wp_smushit'] ) {
45
- echo 'already smushed';
46
  } else {
47
  echo $meta['wp_smushit'];
48
  }
@@ -53,8 +53,7 @@
53
  if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) {
54
  foreach( $meta['sizes'] as $size_name => $size ) {
55
  printf( "– %dx%d: ", intval($size['width']), intval($size['height']) );
56
-
57
- if ( $original_meta['sizes'][$size_name]['wp_smushit'] == $size['wp_smushit'] ) {
58
  echo 'already smushed';
59
  } else {
60
  echo $size['wp_smushit'];
6
  if ( sizeof($attachments) < 1 ):
7
  echo '<p>You don’t appear to have uploaded any images yet.</p>';
8
  else:
9
+ if ( empty($_POST) && !$auto_start ): // instructions page
10
  ?>
11
  <p>This tool will run all of the images in your media library through the WP Smush.it web service. It won't re-smush images that were successfully smushed before. It will retry images that were not successfully smushed.</p>
12
 
19
  <p><strong>This is an experimental feature.</strong> Please post any feedback to the <a href="http://wordpress.org/tags/wp-smushit">WordPress WP Smush.it forums</a>.</p>
20
 
21
  <form method="post" action="">
22
+ <?php wp_nonce_field( 'wp-smushit-bulk', '_wpnonce'); ?>
23
  <button type="submit" class="button-secondary action">Run all my images through WP Smush.it right now</button>
24
  </form>
25
 
26
  <?php
27
  else: // run the script
28
 
29
+ if (!wp_verify_nonce( $_REQUEST['_wpnonce'], 'wp-smushit-bulk' ) || !current_user_can( 'edit_others_posts' ) ) {
30
  wp_die( __( 'Cheatin&#8217; uh?' ) );
31
  }
32
 
33
+
34
  ob_implicit_flush(true);
35
  ob_end_flush();
 
36
  foreach( $attachments as $attachment ) {
37
  printf( "<p>Processing <strong>%s</strong>&hellip;<br>", esc_html($attachment->post_name) );
38
  $original_meta = wp_get_attachment_metadata( $attachment->ID, true );
41
 
42
  printf( "– %dx%d: ", intval($meta['width']), intval($meta['height']) );
43
 
44
+ if ( $original_meta['wp_smushit'] == $meta['wp_smushit'] && stripos( $meta['wp_smushit'], 'Smush.it error' ) === false ) {
45
+ echo 'already smushed' . $meta['wp_smushit'];
46
  } else {
47
  echo $meta['wp_smushit'];
48
  }
53
  if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) {
54
  foreach( $meta['sizes'] as $size_name => $size ) {
55
  printf( "– %dx%d: ", intval($size['width']), intval($size['height']) );
56
+ if ( $original_meta['sizes'][$size_name]['wp_smushit'] == $size['wp_smushit'] && stripos( $meta['sizes'][$size_name]['wp_smushit'], 'Smush.it error' ) === false ) {
 
57
  echo 'already smushed';
58
  } else {
59
  echo $size['wp_smushit'];
readme.txt CHANGED
@@ -1,13 +1,14 @@
1
  === WP Smush.it ===
2
  Plugin Name: WP Smush.it
3
- Version: 1.4.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.9
9
  Tested up to: 3.2.1
10
- Stable tag: 1.4.3
 
11
 
12
  Reduce image file sizes and improve performance using the <a href="http://smush.it/">Smush.it</a> API within WordPress.
13
 
@@ -30,9 +31,16 @@ You can also run your existing images through Smush.it via the WordPress `Media
30
 
31
  As of version 1.4.0 there is a new, experimental `Bulk Smush.it` feature. You can find the link under the `Media Library` tab.
32
 
 
 
 
33
  = Privacy =
34
  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>).
35
 
 
 
 
 
36
  = Updates, etc... =
37
 
38
  Plugin updates are announced on [http://www.twitter.com/TheCHANGELOG](http://www.twitter.com/TheCHANGELOG).
@@ -49,6 +57,12 @@ Plugin updates are announced on [http://www.twitter.com/TheCHANGELOG](http://www
49
 
50
  == Changelog ==
51
 
 
 
 
 
 
 
52
  = 1.4.3 =
53
  * cleaner handling of file paths
54
 
1
  === WP Smush.it ===
2
  Plugin Name: WP Smush.it
3
+ Version: 1.5.0
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.9
9
  Tested up to: 3.2.1
10
+ Stable tag: 1.5.0
11
+ Donate link: http://dunae.ca/donate.html
12
 
13
  Reduce image file sizes and improve performance using the <a href="http://smush.it/">Smush.it</a> API within WordPress.
14
 
31
 
32
  As of version 1.4.0 there is a new, experimental `Bulk Smush.it` feature. You can find the link under the `Media Library` tab.
33
 
34
+ = NextGEN Gallery =
35
+ NextGEN user? Also download the <a href="http://wordpress.org/extend/plugins/wp-smushit-nextgen-gallery-integration/">WP Smush.it NextGEN Integration</a> plugin.
36
+
37
  = Privacy =
38
  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>).
39
 
40
+ = Donate? =
41
+
42
+ If you're so inclined, I've setup <a href="http://dunae.ca/donate.html">a donation page</a>.
43
+
44
  = Updates, etc... =
45
 
46
  Plugin updates are announced on [http://www.twitter.com/TheCHANGELOG](http://www.twitter.com/TheCHANGELOG).
57
 
58
  == Changelog ==
59
 
60
+ = 1.5.0 =
61
+ * added basic integration for the <a href="http://wordpress.org/extend/plugins/wp-smushit-nextgen-gallery-integration/">NextGEN gallery plugin</a>
62
+ * add support for media bulk action dropdown
63
+ * compatibility with WordPress earlier than 3.1
64
+ * added a <a href="http://dunae.ca/donate.html">donate link</a>
65
+
66
  = 1.4.3 =
67
  * cleaner handling of file paths
68
 
wp-smushit.php CHANGED
@@ -1,15 +1,10 @@
1
  <?php
2
- /**
3
- * Integrate the Smush.it API into WordPress.
4
- * @version 1.4.3
5
- * @package WP_SmushIt
6
- */
7
  /*
8
  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.4.3
13
  Author URI: http://dialect.ca/
14
  */
15
 
@@ -28,7 +23,7 @@ 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.4.3 (+http://dialect.ca/code/wp-smushit)');
32
  define('WP_SMUSHIT_PLUGIN_DIR', dirname(plugin_basename(__FILE__)));
33
 
34
 
@@ -47,7 +42,6 @@ add_action('admin_action_wp_smushit_manual', 'wp_smushit_manual');
47
  function wp_smushit_admin_init() {
48
  load_plugin_textdomain(WP_SMUSHIT_DOMAIN);
49
  wp_enqueue_script('common');
50
-
51
  }
52
 
53
  function wp_smushit_admin_menu() {
@@ -62,11 +56,27 @@ function wp_smushit_bulk_preview() {
62
  @ini_set('output_buffering','on');
63
  @ini_set('zlib.output_compression', 0);
64
  @ini_set('implicit_flush', 1);
65
- $attachments = get_posts( array(
66
- 'numberposts' => -1,
67
- 'post_type' => 'attachment',
68
- 'post_mime_type' => 'image'
69
- ));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  require( dirname(__FILE__) . '/bulk.php' );
71
  }
72
 
@@ -101,9 +111,10 @@ function wp_smushit_manual() {
101
  * Returns an array of the $file $results.
102
  *
103
  * @param string $file Full absolute path to the image file
 
104
  * @returns array
105
  */
106
- function wp_smushit($file) {
107
  // don't run on localhost, IPv4 and IPv6 checks
108
  // if( in_array($_SERVER['SERVER_ADDR'], array('127.0.0.1', '::1')) )
109
  // return array($file, __('Not processed (local file)', WP_SMUSHIT_DOMAIN));
@@ -114,7 +125,6 @@ function wp_smushit($file) {
114
  // $file_path = realpath($file);
115
 
116
  $file_path = $file;
117
-
118
  // check that the file exists
119
  if ( FALSE === file_exists($file_path) || FALSE === is_file($file_path) ) {
120
  $msg = sprintf(__("Could not find <span class='code'>%s</span>", WP_SMUSHIT_DOMAIN), $file_path);
@@ -131,14 +141,16 @@ function wp_smushit($file) {
131
  $upload_dir = wp_upload_dir();
132
  $wp_upload_dir = $upload_dir['basedir'];
133
  $wp_upload_url = $upload_dir['baseurl'];
134
- if ( 0 !== stripos(realpath($file_path), realpath($wp_upload_dir)) ) {
135
  $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);
136
 
137
  return array($file, $msg);
138
  }
139
 
140
- // determine the public URL
141
- $file_url = str_replace( $wp_upload_dir, $wp_upload_url, $file );
 
 
142
 
143
  $data = wp_smushit_post($file_url);
144
 
@@ -222,22 +234,24 @@ function wp_smushit_should_resmush($previous_status) {
222
  */
223
  function wp_smushit_resize_from_meta_data($meta, $ID = null, $force_resmush = true) {
224
  $file_path = $meta['file'];
225
-
226
  $store_absolute_path = true;
227
  $upload_dir = wp_upload_dir();
228
  $upload_path = trailingslashit( $upload_dir['basedir'] );
229
 
230
  // WordPress >= 2.6.2: determine the absolute $file_path (http://core.trac.wordpress.org/changeset/8796)
231
- if ( FALSE === strpos($file, WP_CONTENT_DIR) ) {
232
  $store_absolute_path = false;
233
  $file_path = $upload_path . $file_path;
234
  }
 
 
 
235
 
236
  if ( $force_resmush || wp_smushit_should_resmush( @$meta['wp_smushit'] ) ) {
237
  list($file, $msg) = wp_smushit($file_path);
238
  $meta['wp_smushit'] = $msg;
239
  }
240
-
241
  // strip absolute path for Wordpress >= 2.6.2
242
  if ( FALSE === $store_absolute_path ) {
243
  $meta['file'] = str_replace($upload_path, '', $meta['file']);
@@ -333,9 +347,50 @@ function wp_smushit_custom_column($column_name, $id) {
333
  }
334
  }
335
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
336
  /**
337
  * http_request_timeout filter -- bumped up to 25 seconds for larger images
338
  */
339
  function wp_smushit_http_request_timeout($time) {
340
  return 25;
 
 
 
 
 
 
 
 
 
 
341
  }
1
  <?php
 
 
 
 
 
2
  /*
3
  Plugin Name: WP Smush.it
4
  Plugin URI: http://dialect.ca/code/wp-smushit/
5
  Description: Reduce image file sizes and improve performance using the <a href="http://smush.it/">Smush.it</a> API within WordPress.
6
  Author: Dialect
7
+ Version: 1.5.0
8
  Author URI: http://dialect.ca/
9
  */
10
 
23
  define('SMUSHIT_BASE_URL', 'http://www.smushit.com/');
24
 
25
  define('WP_SMUSHIT_DOMAIN', 'wp_smushit');
26
+ define('WP_SMUSHIT_UA', 'WP Smush.it/1.5.0 (+http://dialect.ca/code/wp-smushit)');
27
  define('WP_SMUSHIT_PLUGIN_DIR', dirname(plugin_basename(__FILE__)));
28
 
29
 
42
  function wp_smushit_admin_init() {
43
  load_plugin_textdomain(WP_SMUSHIT_DOMAIN);
44
  wp_enqueue_script('common');
 
45
  }
46
 
47
  function wp_smushit_admin_menu() {
56
  @ini_set('output_buffering','on');
57
  @ini_set('zlib.output_compression', 0);
58
  @ini_set('implicit_flush', 1);
59
+
60
+ $attachments = null;
61
+ $auto_start = false;
62
+
63
+ if ( isset($_REQUEST['ids'])) {
64
+ $attachments = get_posts( array(
65
+ 'numberposts' => -1,
66
+ 'include' => explode(',', $_REQUEST['ids']),
67
+ 'post_type' => 'attachment',
68
+ 'post_mime_type' => 'image'
69
+ ));
70
+ $auto_start = true;
71
+ } else {
72
+ $attachments = get_posts( array(
73
+ 'numberposts' => -1,
74
+ 'post_type' => 'attachment',
75
+ 'post_mime_type' => 'image'
76
+ ));
77
+ }
78
+
79
+
80
  require( dirname(__FILE__) . '/bulk.php' );
81
  }
82
 
111
  * Returns an array of the $file $results.
112
  *
113
  * @param string $file Full absolute path to the image file
114
+ * @param string $file_url Optional full URL to the image file
115
  * @returns array
116
  */
117
+ function wp_smushit($file, $file_url = null) {
118
  // don't run on localhost, IPv4 and IPv6 checks
119
  // if( in_array($_SERVER['SERVER_ADDR'], array('127.0.0.1', '::1')) )
120
  // return array($file, __('Not processed (local file)', WP_SMUSHIT_DOMAIN));
125
  // $file_path = realpath($file);
126
 
127
  $file_path = $file;
 
128
  // check that the file exists
129
  if ( FALSE === file_exists($file_path) || FALSE === is_file($file_path) ) {
130
  $msg = sprintf(__("Could not find <span class='code'>%s</span>", WP_SMUSHIT_DOMAIN), $file_path);
141
  $upload_dir = wp_upload_dir();
142
  $wp_upload_dir = $upload_dir['basedir'];
143
  $wp_upload_url = $upload_dir['baseurl'];
144
+ if ( 0 !== stripos(realpath($file_path), realpath(ABSPATH)) ) {
145
  $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);
146
 
147
  return array($file, $msg);
148
  }
149
 
150
+ if ( !$file_url ) {
151
+ // determine the public URL
152
+ $file_url = str_replace( $wp_upload_dir, $wp_upload_url, $file );
153
+ }
154
 
155
  $data = wp_smushit_post($file_url);
156
 
234
  */
235
  function wp_smushit_resize_from_meta_data($meta, $ID = null, $force_resmush = true) {
236
  $file_path = $meta['file'];
 
237
  $store_absolute_path = true;
238
  $upload_dir = wp_upload_dir();
239
  $upload_path = trailingslashit( $upload_dir['basedir'] );
240
 
241
  // WordPress >= 2.6.2: determine the absolute $file_path (http://core.trac.wordpress.org/changeset/8796)
242
+ if ( FALSE === strpos($file, $upload_path) ) {
243
  $store_absolute_path = false;
244
  $file_path = $upload_path . $file_path;
245
  }
246
+
247
+
248
+
249
 
250
  if ( $force_resmush || wp_smushit_should_resmush( @$meta['wp_smushit'] ) ) {
251
  list($file, $msg) = wp_smushit($file_path);
252
  $meta['wp_smushit'] = $msg;
253
  }
254
+
255
  // strip absolute path for Wordpress >= 2.6.2
256
  if ( FALSE === $store_absolute_path ) {
257
  $meta['file'] = str_replace($upload_path, '', $meta['file']);
347
  }
348
  }
349
  }
350
+
351
+ add_action( 'admin_head-upload.php', 'wp_smushit_add_bulk_actions_via_javascript' );
352
+
353
+ // Borrowed from http://www.viper007bond.com/wordpress-plugins/regenerate-thumbnails/
354
+ function wp_smushit_add_bulk_actions_via_javascript() { ?>
355
+ <script type="text/javascript">
356
+ jQuery(document).ready(function($){
357
+ $('select[name^="action"] option:last-child').before('<option value="bulk_smushit">Bulk Smush.it</option>');
358
+ });
359
+ </script>
360
+ <?php }
361
+
362
+
363
+ add_action( 'admin_action_bulk_smushit', 'wp_smushit_bulk_action_handler' );
364
+
365
+ // Handles the bulk actions POST
366
+ // Borrowed from http://www.viper007bond.com/wordpress-plugins/regenerate-thumbnails/
367
+ function wp_smushit_bulk_action_handler() {
368
+ check_admin_referer( 'bulk-media' );
369
+
370
+ if ( empty( $_REQUEST['media'] ) || ! is_array( $_REQUEST['media'] ) )
371
+ return;
372
+
373
+ $ids = implode( ',', array_map( 'intval', $_REQUEST['media'] ) );
374
+
375
+
376
+ // Can't use wp_nonce_url() as it escapes HTML entities
377
+ wp_redirect( add_query_arg( '_wpnonce', wp_create_nonce( 'wp-smushit-bulk' ), admin_url( 'upload.php?page=wp-smushit-bulk&goback=1&ids=' . $ids ) ) );
378
+ exit();
379
+ }
380
+
381
  /**
382
  * http_request_timeout filter -- bumped up to 25 seconds for larger images
383
  */
384
  function wp_smushit_http_request_timeout($time) {
385
  return 25;
386
+ }
387
+
388
+
389
+ if ( function_exists( 'wp_basename' ) === false ) {
390
+ /**
391
+ * Introduced in WP 3.1... this is copied verbatim from wp-includes/formatting.php.
392
+ */
393
+ function wp_basename( $path, $suffix = '' ) {
394
+ return urldecode( basename( str_replace( '%2F', '/', urlencode( $path ) ), $suffix ) );
395
+ }
396
  }