Version Description
- updated to use Yahoo! hosted Smush.it service
- added security checks to files passed to
wp_smushit()
Download this release
Release Info
Developer | alexdunae |
Plugin | Smush Image Compression and Optimization |
Version | 1.2.2 |
Comparing to | |
See all releases |
Code changes from version 1.2.1 to 1.2.2
- options.php +10 -0
- readme.txt +11 -7
- theme.php +161 -0
- wp-smushit.php +76 -31
options.php
CHANGED
@@ -19,5 +19,15 @@
|
|
19 |
</tr>
|
20 |
</table>
|
21 |
<p class="submit"><input type="submit" name="Submit" value="<?php _e('Save Changes') ?>" /></p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
</form>
|
23 |
</div>
|
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&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.
|
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 |
|
@@ -49,19 +49,23 @@ Plugin updates are announced on [http://www.twitter.com/TheCHANGELOG](http://www
|
|
49 |
|
50 |
== Changelog ==
|
51 |
|
|
|
|
|
|
|
|
|
52 |
= 1.2.1 =
|
53 |
* added support for PHP 4
|
54 |
-
* created
|
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
|
1 |
=== WP Smush.it ===
|
2 |
Plugin Name: WP Smush.it
|
3 |
+
Version: 1.2.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.2
|
10 |
+
Stable tag: 1.2.2
|
11 |
|
12 |
Reduce image file sizes and improve performance using the <a href="http://smush.it/">Smush.it</a> API within WordPress.
|
13 |
|
49 |
|
50 |
== Changelog ==
|
51 |
|
52 |
+
= 1.2.2 =
|
53 |
+
* updated to use Yahoo! hosted Smush.it service
|
54 |
+
* added security checks to files passed to `wp_smushit()`
|
55 |
+
|
56 |
= 1.2.1 =
|
57 |
* added support for PHP 4
|
58 |
+
* created admin action hook as workaround to WordPress 2.9's `$_registered_pages` security (see [changeset 11596](http://core.trac.wordpress.org/changeset/11596))
|
59 |
+
* add savings amount in bytes to Media Library (thx [Yoast](http://www.yoast.com/))
|
60 |
|
61 |
= 1.2 =
|
62 |
+
* added support for `WP_Http`
|
63 |
|
64 |
= 1.1.3 =
|
65 |
* fixed activation error when the PEAR JSON library is already loaded
|
66 |
|
67 |
= 1.1.2 =
|
68 |
+
* added test for `allow_url_fopen`
|
69 |
|
70 |
= 1.1.1 =
|
71 |
* added error message on PHP copy error
|
theme.php
ADDED
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?php
|
2 |
+
/**
|
3 |
+
* Admin page for smushing all the images in a given WordPress theme.
|
4 |
+
*
|
5 |
+
* Expects a `theme` in the query string.
|
6 |
+
*
|
7 |
+
* @version 1.3
|
8 |
+
* @package WP_SmushIt
|
9 |
+
*/
|
10 |
+
|
11 |
+
if ( FALSE === current_user_can('edit_themes') ) {
|
12 |
+
wp_die(__('You don\'t have permission to work with themes.', WP_SMUSHIT_DOMAIN));
|
13 |
+
}
|
14 |
+
|
15 |
+
ob_start();
|
16 |
+
wp_enqueue_script( 'common');
|
17 |
+
$theme = null;
|
18 |
+
$theme_path = null;
|
19 |
+
$theme_url = null;
|
20 |
+
|
21 |
+
if ( isset($_GET['theme']) && !empty($_GET['theme']) ) {
|
22 |
+
|
23 |
+
$theme = attribute_escape($_GET['theme']);
|
24 |
+
$theme_path = get_theme_root() . '/' . $theme;
|
25 |
+
$theme_url = get_theme_root_uri() . '/' . $theme;
|
26 |
+
}
|
27 |
+
|
28 |
+
?>
|
29 |
+
<div class="wrap">
|
30 |
+
<div id="icon-plugins" class="icon32"><br /></div><h2>WP Smush.it: Smush Theme Assets</h2>
|
31 |
+
|
32 |
+
<?php
|
33 |
+
// Smush files
|
34 |
+
if (isset($_POST['action']) && $_POST['action'] == 'smush_theme' ):
|
35 |
+
|
36 |
+
check_admin_referer('wp-smushit_smush-theme' . $theme);
|
37 |
+
|
38 |
+
|
39 |
+
?>
|
40 |
+
|
41 |
+
<p>Processing files in the <strong><?php echo $theme; ?></strong> theme.</p>
|
42 |
+
|
43 |
+
<?php
|
44 |
+
|
45 |
+
foreach($_POST['smushitlink'] as $l) {
|
46 |
+
// decode and sanitize the file path
|
47 |
+
$asset_url = base64_decode($l);
|
48 |
+
$asset_path = str_replace($theme_url, $theme_path, $asset_url);
|
49 |
+
|
50 |
+
|
51 |
+
print "<p>Smushing <span class='code'>$asset_url</span><br/>";
|
52 |
+
|
53 |
+
list($processed_path, $msg) = wp_smushit($asset_path);
|
54 |
+
|
55 |
+
echo "<em>– $msg</em>.</p>\n";
|
56 |
+
ob_flush();
|
57 |
+
}
|
58 |
+
|
59 |
+
|
60 |
+
?>
|
61 |
+
|
62 |
+
<p>Finished processing all the files in the <strong><?php echo $theme; ?></strong> theme.</p>
|
63 |
+
|
64 |
+
<p><strong>Actions:</strong> <a href="<?php echo '?page=' . basename(dirname(__FILE__)) . '/theme.php'; ?>" title="Return to Plugin Installer" target="_parent">Smush another theme’s assets</a></p>
|
65 |
+
|
66 |
+
<!-- <p><strong>Actions:</strong> <a href="plugins.php?action=activate&plugin=google-sitemap-generator%2Fsitemap.php&_wpnonce=2b4ca1722c" title="Activate this plugin" target="_parent">Activate Plugin</a> | <a href="http://localhost/wp28/wp-admin/plugin-install.php" title="Return to Plugin Installer" target="_parent">Return to Plugin Installer</a></p> -->
|
67 |
+
|
68 |
+
|
69 |
+
<?php
|
70 |
+
// Select files to smush
|
71 |
+
elseif( $theme ):
|
72 |
+
$td = get_theme_data($theme_path . '/style.css');
|
73 |
+
|
74 |
+
$handle = opendir($theme_path);
|
75 |
+
if ( FALSE === $handle ) {
|
76 |
+
wp_die('Error opening ' . $theme_path);
|
77 |
+
}
|
78 |
+
?>
|
79 |
+
|
80 |
+
<form method="post" action="">
|
81 |
+
<input type="hidden" name="action" value="smush_theme"/>
|
82 |
+
<?php
|
83 |
+
if ( function_exists('wp_nonce_field') ) wp_nonce_field('wp-smushit_smush-theme' . $theme);
|
84 |
+
?>
|
85 |
+
|
86 |
+
<table class="widefat fixed" cellspacing="0">
|
87 |
+
<thead>
|
88 |
+
<tr>
|
89 |
+
<th scope="col" id="cb" class="manage-column column-cb check-column" style=""><input type="checkbox" /></th>
|
90 |
+
<th scope="col" id="name" class="manage-column column-name" style="">File name</th>
|
91 |
+
</tr>
|
92 |
+
<tbody>
|
93 |
+
<?php
|
94 |
+
|
95 |
+
while (false !== ($file = readdir($handle))) {
|
96 |
+
|
97 |
+
if ( preg_match('/\.(jpg|jpeg|png|gif)$/i', $file) < 1 ) {
|
98 |
+
continue;
|
99 |
+
}
|
100 |
+
|
101 |
+
$file_url = $theme_url . '/' . $file;
|
102 |
+
|
103 |
+
|
104 |
+
?>
|
105 |
+
<tr id="asdasdasd" valign="middle">
|
106 |
+
<th scope="row" class="check-column"><input type="checkbox" name="smushitlink[]" value="<?php echo attribute_escape(base64_encode($file_url)); ?>" /></th>
|
107 |
+
<td class="column-name"><strong><a class='row-title' href='<?php echo $file_url; ?>'><?php echo $file_url; ?></a></strong></td>
|
108 |
+
</tr>
|
109 |
+
<?php
|
110 |
+
|
111 |
+
}
|
112 |
+
closedir($handle);
|
113 |
+
?>
|
114 |
+
</table>
|
115 |
+
|
116 |
+
<input type="submit">
|
117 |
+
</form>
|
118 |
+
|
119 |
+
|
120 |
+
|
121 |
+
|
122 |
+
<?php else: ?>
|
123 |
+
|
124 |
+
|
125 |
+
<p>Select a theme.</p>
|
126 |
+
<ul>
|
127 |
+
<?php
|
128 |
+
$themes = get_themes();
|
129 |
+
|
130 |
+
foreach($themes as $t) {
|
131 |
+
|
132 |
+
printf("\t<li><a href=\"?page=%s&theme=%s\">%s</a></li>\n",
|
133 |
+
basename(dirname(__FILE__)) . '/theme.php',
|
134 |
+
$t['Template'],
|
135 |
+
$t['Name']);
|
136 |
+
|
137 |
+
|
138 |
+
}
|
139 |
+
|
140 |
+
//var_dump($themes);
|
141 |
+
?>
|
142 |
+
</ul>
|
143 |
+
<?php endif; ?>
|
144 |
+
</div>
|
145 |
+
|
146 |
+
<?php
|
147 |
+
|
148 |
+
exit;
|
149 |
+
|
150 |
+
|
151 |
+
/*
|
152 |
+
|
153 |
+
div class="wrap"> <div id="icon-plugins" class="icon32"><br /></div>
|
154 |
+
<h2>Installing Plugin: Google XML Sitemaps 3.1.4</h2><p>Downloading install package from <span class="code">http://downloads.wordpress.org/plugin/google-sitemap-generator.3.1.4.zip</span>.</p>
|
155 |
+
<p>Unpacking the package.</p>
|
156 |
+
|
157 |
+
<p>Installing the plugin.</p>
|
158 |
+
<p>Successfully installed the plugin <strong>Google XML Sitemaps 3.1.4</strong>.</p>
|
159 |
+
<p><strong>Actions:</strong> <a href="plugins.php?action=activate&plugin=google-sitemap-generator%2Fsitemap.php&_wpnonce=2b4ca1722c" title="Activate this plugin" target="_parent">Activate Plugin</a> | <a href="http://localhost/wp28/wp-admin/plugin-install.php" title="Return to Plugin Installer" target="_parent">Return to Plugin Installer</a></p>
|
160 |
+
</div>
|
161 |
+
**/
|
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 |
|
@@ -22,13 +22,13 @@ if ( !class_exists('Services_JSON') ) {
|
|
22 |
* Constants
|
23 |
*/
|
24 |
|
25 |
-
define('SMUSHIT_REQ_URL', 'http://
|
26 |
|
27 |
-
define('SMUSHIT_BASE_URL', 'http://
|
28 |
|
29 |
define('WP_SMUSHIT_DOMAIN', 'wp_smushit');
|
30 |
|
31 |
-
define('WP_SMUSHIT_UA', 'WP Smush.it/1.
|
32 |
|
33 |
define('WP_SMUSHIT_GIF_TO_PNG', intval(get_option('wp_smushit_gif_to_png')));
|
34 |
|
@@ -53,9 +53,11 @@ 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() {
|
@@ -64,11 +66,12 @@ function wp_smushit_install() {
|
|
64 |
|
65 |
function wp_smushit_init() {
|
66 |
load_plugin_textdomain(WP_SMUSHIT_DOMAIN);
|
|
|
|
|
67 |
}
|
68 |
|
69 |
function wp_smushit_add_pages() {
|
70 |
-
|
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 |
}
|
@@ -85,6 +88,14 @@ function wp_smushit_options() {
|
|
85 |
include_once 'options.php';
|
86 |
}
|
87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
/**
|
90 |
* Manually process an image from the Media Library
|
@@ -103,7 +114,6 @@ function wp_smushit_manual() {
|
|
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();
|
@@ -117,31 +127,41 @@ function wp_smushit_manual() {
|
|
117 |
*
|
118 |
* Returns an array of the $file $results.
|
119 |
*
|
120 |
-
* @param string $file Full path to the image file
|
121 |
* @returns array
|
122 |
*/
|
123 |
function wp_smushit($file) {
|
124 |
// dont't run on localhost
|
125 |
-
if( '127.0.0.1' == $_SERVER['SERVER_ADDR'] )
|
126 |
return array($file, __('Not processed (local file)', WP_SMUSHIT_DOMAIN));
|
127 |
|
|
|
|
|
128 |
|
129 |
-
|
130 |
-
$
|
|
|
|
|
|
|
131 |
|
132 |
-
|
133 |
-
|
134 |
-
$
|
135 |
-
|
136 |
-
// WordPress >= 2.6.2: determine the absolute $file_path and $file_url
|
137 |
-
$uploads = wp_upload_dir();
|
138 |
-
$file_path = trailingslashit( $uploads['basedir'] ) . $file;
|
139 |
-
$file_url = trailingslashit( $uploads['baseurl'] ) . $file;
|
140 |
}
|
141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
142 |
$data = wp_smushit_post($file_url);
|
143 |
|
144 |
-
if (
|
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
|
@@ -158,15 +178,21 @@ function wp_smushit($file) {
|
|
158 |
$data = $json->decode($data);
|
159 |
}
|
160 |
|
161 |
-
if ( -1
|
162 |
return array($file, __('No savings', WP_SMUSHIT_DOMAIN));
|
163 |
|
164 |
if ( !$data->dest ) {
|
165 |
-
$err = ($data->error ? $data->error : '
|
166 |
return array($file, __($err, WP_SMUSHIT_DOMAIN) );
|
167 |
}
|
168 |
|
169 |
-
$processed_url =
|
|
|
|
|
|
|
|
|
|
|
|
|
170 |
|
171 |
$temp_file = wp_smushit_download($processed_url);
|
172 |
|
@@ -178,7 +204,7 @@ function wp_smushit($file) {
|
|
178 |
$file = preg_replace('/.gif$/i', '.png', $file);
|
179 |
$file_path = preg_replace('/.gif$/i', '.png', $file_path);
|
180 |
|
181 |
-
if ( has_filter('wp_update_attachment_metadata', 'wp_smushit_update_attachment')
|
182 |
add_filter('wp_update_attachment_metadata', 'wp_smushit_update_attachment', 10, 2);
|
183 |
}
|
184 |
|
@@ -188,7 +214,7 @@ function wp_smushit($file) {
|
|
188 |
$savings_str = wp_smushit_format_bytes($savings, 1);
|
189 |
$savings_str = str_replace(' ', ' ', $savings_str);
|
190 |
|
191 |
-
$results_msg = sprintf(__("Reduced by %01.1f%% (%s)", WP_SMUSHIT_DOMAIN),
|
192 |
$data->percent,
|
193 |
$savings_str);
|
194 |
|
@@ -231,17 +257,36 @@ function wp_smushit_update_attachment($data, $ID) {
|
|
231 |
* Called after `wp_generate_attachment_metadata` is completed.
|
232 |
*/
|
233 |
function wp_smushit_resize_from_meta_data($meta) {
|
234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
235 |
|
|
|
236 |
if ( !isset($meta['sizes']) )
|
237 |
return $meta;
|
238 |
|
239 |
-
|
|
|
240 |
|
241 |
foreach($meta['sizes'] as $size => $data) {
|
242 |
list($smushed_file, $results) = wp_smushit($base_dir . $data['file']);
|
243 |
-
|
244 |
-
$meta['sizes'][$size]['file'] = $smushed_file;
|
245 |
$meta['sizes'][$size]['wp_smushit'] = $results;
|
246 |
}
|
247 |
|
1 |
<?php
|
2 |
/**
|
3 |
* Integrate the Smush.it API into WordPress.
|
4 |
+
* @version 1.2.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.2
|
13 |
Author URI: http://dialect.ca/?wp_smush_it
|
14 |
*/
|
15 |
|
22 |
* Constants
|
23 |
*/
|
24 |
|
25 |
+
define('SMUSHIT_REQ_URL', 'http://smushit.com/ws.php?img=%s');
|
26 |
|
27 |
+
define('SMUSHIT_BASE_URL', 'http://smushit.com/');
|
28 |
|
29 |
define('WP_SMUSHIT_DOMAIN', 'wp_smushit');
|
30 |
|
31 |
+
define('WP_SMUSHIT_UA', 'WP Smush.it/1.3 (+http://dialect.ca/code/wp-smushit)');
|
32 |
|
33 |
define('WP_SMUSHIT_GIF_TO_PNG', intval(get_option('wp_smushit_gif_to_png')));
|
34 |
|
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 |
+
add_action('admin_action_wp_smushit_theme', 'wp_smushit_theme');
|
57 |
+
|
58 |
|
59 |
/**
|
60 |
+
* Plugin admin functions
|
61 |
*/
|
62 |
|
63 |
function wp_smushit_install() {
|
66 |
|
67 |
function wp_smushit_init() {
|
68 |
load_plugin_textdomain(WP_SMUSHIT_DOMAIN);
|
69 |
+
// print 'init';
|
70 |
+
wp_enqueue_script('common');
|
71 |
}
|
72 |
|
73 |
function wp_smushit_add_pages() {
|
74 |
+
// add_submenu_page('themes.php', 'Smush.it', 'WP Smush.it', 8, dirname(__FILE__) . '/theme.php');
|
|
|
75 |
add_options_page(__('WP Smush.it Options', WP_SMUSHIT_OPTIONS), 'WP Smush.it', 8, dirname(__FILE__) . '/options.php');
|
76 |
add_filter( 'plugin_action_links', 'wp_smushit_filter_plugin_actions', 10, 2 );
|
77 |
}
|
88 |
include_once 'options.php';
|
89 |
}
|
90 |
|
91 |
+
/**
|
92 |
+
* Process all the images from a given theme
|
93 |
+
*/
|
94 |
+
function wp_smushit_theme() {
|
95 |
+
require_once('smush_theme.php');
|
96 |
+
exit(0);
|
97 |
+
}
|
98 |
+
|
99 |
|
100 |
/**
|
101 |
* Manually process an image from the Media Library
|
114 |
$original_meta = wp_get_attachment_metadata( $attachment_ID );
|
115 |
|
116 |
$new_meta = wp_smushit_resize_from_meta_data( $original_meta );
|
|
|
117 |
wp_update_attachment_metadata( $attachment_ID, $new_meta );
|
118 |
|
119 |
$sendback = wp_get_referer();
|
127 |
*
|
128 |
* Returns an array of the $file $results.
|
129 |
*
|
130 |
+
* @param string $file Full absolute path to the image file
|
131 |
* @returns array
|
132 |
*/
|
133 |
function wp_smushit($file) {
|
134 |
// dont't run on localhost
|
135 |
+
if( '127.0.0.1' == $_SERVER['SERVER_ADDR'] && FALSE )
|
136 |
return array($file, __('Not processed (local file)', WP_SMUSHIT_DOMAIN));
|
137 |
|
138 |
+
// canonicalize path
|
139 |
+
$file_path = realpath($file);
|
140 |
|
141 |
+
// check that the file exists
|
142 |
+
if ( FALSE === file_exists($file_path) || FALSE === is_file($file_path) ) {
|
143 |
+
$msg = sprintf(__("Could not $file_path find <span class='code'>%s</span>", WP_SMUSHIT_DOMAIN), $file_path);
|
144 |
+
return array($file, $msg);
|
145 |
+
}
|
146 |
|
147 |
+
// check that the file is writable
|
148 |
+
if ( FALSE === is_writable($file_path) ) {
|
149 |
+
$msg = sprintf(__("<span class='code'>%s</span> is not writable", WP_SMUSHIT_DOMAIN), $file_path);
|
150 |
+
return array($file, $msg);
|
|
|
|
|
|
|
|
|
151 |
}
|
152 |
|
153 |
+
// check that the file is within the WP_CONTENT_DIR
|
154 |
+
if ( 0 !== stripos($file_path, WP_CONTENT_DIR) || FALSE) {
|
155 |
+
$msg = sprintf(__("<span class='code'>%s</span> must be within the content directory (<span class='code'>%s</span>)", WP_SMUSHIT_DOMAIN), $file_path, WP_CONTENT_DIR);
|
156 |
+
return array($file, $msg);
|
157 |
+
}
|
158 |
+
|
159 |
+
// determine the public URL
|
160 |
+
$file_url = str_replace( WP_CONTENT_DIR, WP_CONTENT_URL, $file );
|
161 |
+
|
162 |
$data = wp_smushit_post($file_url);
|
163 |
|
164 |
+
if ( FALSE === $data )
|
165 |
return array($file, __('Error posting to Smush.it', WP_SMUSHIT_DOMAIN));
|
166 |
|
167 |
// make sure the response looks like JSON -- added 2008-12-19 when
|
178 |
$data = $json->decode($data);
|
179 |
}
|
180 |
|
181 |
+
if ( -1 === intval($data->dest_size) )
|
182 |
return array($file, __('No savings', WP_SMUSHIT_DOMAIN));
|
183 |
|
184 |
if ( !$data->dest ) {
|
185 |
+
$err = ($data->error ? 'Smush.it error: ' . $data->error : 'unknown error');
|
186 |
return array($file, __($err, WP_SMUSHIT_DOMAIN) );
|
187 |
}
|
188 |
|
189 |
+
$processed_url = $data->dest;
|
190 |
+
|
191 |
+
// The smush.it web service does not append the domain;
|
192 |
+
// smushit.com web service does
|
193 |
+
if ( 0 !== stripos($processed_url, 'http://') ) {
|
194 |
+
$processed_url = SMUSHIT_BASE_URL . $processed_url;
|
195 |
+
}
|
196 |
|
197 |
$temp_file = wp_smushit_download($processed_url);
|
198 |
|
204 |
$file = preg_replace('/.gif$/i', '.png', $file);
|
205 |
$file_path = preg_replace('/.gif$/i', '.png', $file_path);
|
206 |
|
207 |
+
if ( FALSE === has_filter('wp_update_attachment_metadata', 'wp_smushit_update_attachment') )
|
208 |
add_filter('wp_update_attachment_metadata', 'wp_smushit_update_attachment', 10, 2);
|
209 |
}
|
210 |
|
214 |
$savings_str = wp_smushit_format_bytes($savings, 1);
|
215 |
$savings_str = str_replace(' ', ' ', $savings_str);
|
216 |
|
217 |
+
$results_msg = sprintf(__("Reduced by %01.1f%% (%s)", WP_SMUSHIT_DOMAIN),
|
218 |
$data->percent,
|
219 |
$savings_str);
|
220 |
|
257 |
* Called after `wp_generate_attachment_metadata` is completed.
|
258 |
*/
|
259 |
function wp_smushit_resize_from_meta_data($meta) {
|
260 |
+
$file_path = $meta['file'];
|
261 |
+
$store_absolute_path = true;
|
262 |
+
$upload_dir = wp_upload_dir();
|
263 |
+
$upload_path = trailingslashit( $upload_dir['basedir'] );
|
264 |
+
|
265 |
+
// WordPress >= 2.6.2: determine the absolute $file_path (http://core.trac.wordpress.org/changeset/8796)
|
266 |
+
if ( FALSE === strpos($file, WP_CONTENT_DIR) ) {
|
267 |
+
$store_absolute_path = false;
|
268 |
+
$file_path = $upload_path . $file_path;
|
269 |
+
}
|
270 |
+
print '<pre>';
|
271 |
+
print $file_path . "\n";
|
272 |
+
list($meta['file'], $meta['wp_smushit']) = wp_smushit($file_path);
|
273 |
+
|
274 |
+
// strip absolute path for Wordpress >= 2.6.2
|
275 |
+
if ( FALSE === $store_absolute_path ) {
|
276 |
+
$meta['file'] = str_replace($upload_path, '', $meta['file']);
|
277 |
+
}
|
278 |
|
279 |
+
// no resized versions, so we can exit
|
280 |
if ( !isset($meta['sizes']) )
|
281 |
return $meta;
|
282 |
|
283 |
+
// meta sizes don't contain a path, so we calculate one
|
284 |
+
$base_dir = dirname($file_path) . '/';
|
285 |
|
286 |
foreach($meta['sizes'] as $size => $data) {
|
287 |
list($smushed_file, $results) = wp_smushit($base_dir . $data['file']);
|
288 |
+
|
289 |
+
$meta['sizes'][$size]['file'] = str_replace($base_dir, '', $smushed_file);
|
290 |
$meta['sizes'][$size]['wp_smushit'] = $results;
|
291 |
}
|
292 |
|