Imsanity - Version 2.3.8

Version Description

  • network settings page only available when plugin is network-activated, please let me know if that sounds crazy to you: https://ewww.io/contact-us/
Download this release

Release Info

Developer nosilver4u
Plugin Icon 128x128 Imsanity
Version 2.3.8
Comparing to
See all releases

Code changes from version 2.3.7 to 2.3.8

ajax.php CHANGED
@@ -7,7 +7,6 @@
7
 
8
  add_action('wp_ajax_imsanity_get_images', 'imsanity_get_images');
9
  add_action('wp_ajax_imsanity_resize_image', 'imsanity_resize_image');
10
- add_action('admin_head', 'imsanity_admin_javascript');
11
 
12
  /**
13
  * Verifies that the current user has administrator permission and, if not,
@@ -15,11 +14,11 @@ add_action('admin_head', 'imsanity_admin_javascript');
15
  */
16
  function imsanity_verify_permission()
17
  {
18
- if (!current_user_can('administrator'))
19
- {
20
- $results = array('success'=>false,'message' => 'Administrator permission is required');
21
- echo json_encode($results);
22
- die();
23
  }
24
  }
25
 
@@ -33,35 +32,34 @@ function imsanity_get_images()
33
  imsanity_verify_permission();
34
 
35
  global $wpdb;
 
 
 
 
 
 
36
 
37
- $query = $wpdb->prepare(
 
38
  "select
39
  $wpdb->posts.ID as ID,
40
- $wpdb->posts.guid as guid,
41
  $wpdb->postmeta.meta_value as file_meta
42
  from $wpdb->posts
43
  inner join $wpdb->postmeta on $wpdb->posts.ID = $wpdb->postmeta.post_id and $wpdb->postmeta.meta_key = %s
44
  where $wpdb->posts.post_type = %s
45
  and $wpdb->posts.post_mime_type like %s
46
  and $wpdb->posts.post_mime_type != %s",
47
- array('_wp_attachment_metadata', 'attachment', 'image%','image/bmp')
48
  );
49
 
50
- $images = $wpdb->get_results($query);
51
- $results = array();
52
 
53
- if ($images)
54
- {
55
- $maxW = imsanity_get_option('imsanity_max_width',IMSANITY_DEFAULT_MAX_WIDTH);
56
- $maxH = imsanity_get_option('imsanity_max_height',IMSANITY_DEFAULT_MAX_HEIGHT);
57
- $count = 0;
58
 
59
- foreach ($images as $image)
60
- {
61
- $meta = unserialize($image->file_meta);
62
 
63
- if ($meta['width'] > $maxW || $meta['height'] > $maxH)
64
- {
65
  $count++;
66
 
67
  $results[] = array(
@@ -73,12 +71,12 @@ function imsanity_get_images()
73
  }
74
 
75
  // make sure we only return a limited number of records so we don't overload the ajax features
76
- if ($count >= IMSANITY_AJAX_MAX_RECORDS) break;
77
  }
78
- }
79
-
80
- echo json_encode($results);
81
- die(); // required by wordpress
82
  }
83
 
84
  /**
@@ -91,113 +89,95 @@ function imsanity_resize_image()
91
 
92
  global $wpdb;
93
 
94
- $id = intval( $_POST['id'] );
95
 
96
- if (!$id)
97
- {
98
- $results = array('success'=>false,'message' => __('Missing ID Parameter','imsanity'));
99
- echo json_encode($results);
100
- die();
101
  }
102
 
103
- // @TODO: probably doesn't need the join...?
104
- $query = $wpdb->prepare(
105
- "select
106
- $wpdb->posts.ID as ID,
107
- $wpdb->posts.guid as guid,
108
- $wpdb->postmeta.meta_value as file_meta
109
- from $wpdb->posts
110
- inner join $wpdb->postmeta on $wpdb->posts.ID = $wpdb->postmeta.post_id and $wpdb->postmeta.meta_key = %s
111
- where $wpdb->posts.ID = %d
112
- and $wpdb->posts.post_type = %s
113
- and $wpdb->posts.post_mime_type like %s",
114
- array('_wp_attachment_metadata', $id, 'attachment', 'image%')
115
- );
116
-
117
- $images = $wpdb->get_results($query);
118
 
119
- if ($images)
120
- {
121
- $image = $images[0];
122
- $meta = unserialize($image->file_meta);
123
  $uploads = wp_upload_dir();
 
124
  $oldPath = $uploads['basedir'] . "/" . $meta['file'];
 
 
 
 
125
 
126
- $maxW = imsanity_get_option('imsanity_max_width',IMSANITY_DEFAULT_MAX_WIDTH);
127
- $maxH = imsanity_get_option('imsanity_max_height',IMSANITY_DEFAULT_MAX_HEIGHT);
128
 
129
  // method one - slow but accurate, get file size from file itself
130
- // list($oldW, $oldH) = getimagesize( $oldPath );
131
  // method two - get file size from meta, fast but resize will fail if meta is out of sync
132
- $oldW = $meta['width'];
133
- $oldH = $meta['height'];
134
-
 
135
 
136
- if (($oldW > $maxW && $maxW > 0) || ($oldH > $maxH && $maxH > 0))
137
- {
138
- $quality = imsanity_get_option('imsanity_quality',IMSANITY_DEFAULT_QUALITY);
139
 
140
- list($newW, $newH) = wp_constrain_dimensions($oldW, $oldH, $maxW, $maxH);
 
 
 
 
 
141
 
142
- $resizeResult = imsanity_image_resize( $oldPath, $newW, $newH, false, null, null, $quality);
143
- // $resizeResult = new WP_Error('invalid_image', __('Could not read image size'), $oldPath); // uncommend to debug fail condition
144
 
145
- if (!is_wp_error($resizeResult))
146
- {
147
  $newPath = $resizeResult;
148
 
149
- if ($newPath != $oldPath)
150
- {
151
- // remove original and replace with re-sized image
152
- unlink($oldPath);
153
- rename($newPath, $oldPath);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
  }
155
 
156
- $meta['width'] = $newW;
157
- $meta['height'] = $newH;
158
-
159
- // @TODO replace custom query with update_post_meta
160
- $update_query = $wpdb->prepare(
161
- "update $wpdb->postmeta
162
- set $wpdb->postmeta.meta_value = %s
163
- where $wpdb->postmeta.post_id = %d
164
- and $wpdb->postmeta.meta_key = %s",
165
- array(maybe_serialize($meta), $image->ID, '_wp_attachment_metadata')
 
166
  );
167
-
168
- $wpdb->query($update_query);
169
-
170
- $results = array('success'=>true,'id'=> $id, 'message' => sprintf(__('OK: %s','imsanity') , $oldPath) );
171
- }
172
- else
173
- {
174
- $results = array('success'=>false,'id'=> $id, 'message' => sprintf(__('ERROR: %s (%s)','imsanity'),$oldPath,htmlentities($resizeResult->get_error_message()) ) );
175
  }
 
 
176
  }
177
- else
178
- {
179
- $results = array('success'=>true,'id'=> $id, 'message' => sprintf(__('SKIPPED: %s (Resize not required)','imsanity') , $oldPath ) );
180
- }
181
-
182
- }
183
- else
184
- {
185
- $results = array('success'=>false,'id'=> $id, 'message' => sprintf(__('ERROR: (Attachment with ID of %s not found) ','imsanity') , htmlentities($id) ) );
186
  }
187
 
188
  // if there is a quota we need to reset the directory size cache so it will re-calculate
189
- delete_transient('dirsize_cache');
190
 
191
- echo json_encode($results);
192
- die(); // required by wordpress
193
  }
194
-
195
- /**
196
- * Output the javascript needed for making ajax calls into the header
197
- */
198
- function imsanity_admin_javascript()
199
- {
200
- // javascript is queued in settings.php imsanity_settings_banner()
201
- }
202
-
203
- ?>
7
 
8
  add_action('wp_ajax_imsanity_get_images', 'imsanity_get_images');
9
  add_action('wp_ajax_imsanity_resize_image', 'imsanity_resize_image');
 
10
 
11
  /**
12
  * Verifies that the current user has administrator permission and, if not,
14
  */
15
  function imsanity_verify_permission()
16
  {
17
+ if ( ! current_user_can( 'administrator' ) ) { // this isn't a real capability, but super admins can do anything, so it works
18
+ die( json_encode( array( 'success' => false, 'message' => esc_html__( 'Administrator permission is required', 'imsanity' ) ) ) );
19
+ }
20
+ if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'imsanity-bulk' ) ) {
21
+ die( json_encode( array( 'success' => false, 'message' => esc_html__( 'Access token has expired, please reload the page.', 'imsanity' ) ) ) );
22
  }
23
  }
24
 
32
  imsanity_verify_permission();
33
 
34
  global $wpdb;
35
+ $offset = 0;
36
+ $limit = apply_filters( 'imsanity_attachment_query_limit', 3000 );
37
+ $results = array();
38
+ $maxW = imsanity_get_option( 'imsanity_max_width', IMSANITY_DEFAULT_MAX_WIDTH );
39
+ $maxH = imsanity_get_option( 'imsanity_max_height', IMSANITY_DEFAULT_MAX_HEIGHT );
40
+ $count = 0;
41
 
42
+ while( $images = $wpdb->get_results( "SELECT metas.meta_value as file_meta,metas.post_id as ID FROM $wpdb->postmeta metas INNER JOIN $wpdb->posts posts ON posts.ID = metas.post_id WHERE posts.post_type LIKE 'attachment' AND posts.post_mime_type LIKE 'image%%' AND posts.post_mime_type NOT LIKE 'image/bmp' AND metas.meta_key = '_wp_attachment_metadata' $attachment_query LIMIT $offset,$limit" ) ) {
43
+ /* $query = $wpdb->prepare(
44
  "select
45
  $wpdb->posts.ID as ID,
 
46
  $wpdb->postmeta.meta_value as file_meta
47
  from $wpdb->posts
48
  inner join $wpdb->postmeta on $wpdb->posts.ID = $wpdb->postmeta.post_id and $wpdb->postmeta.meta_key = %s
49
  where $wpdb->posts.post_type = %s
50
  and $wpdb->posts.post_mime_type like %s
51
  and $wpdb->posts.post_mime_type != %s",
52
+ array( '_wp_attachment_metadata', 'attachment', 'image%', 'image/bmp' )
53
  );
54
 
55
+ $images = $wpdb->get_results($query);*/
 
56
 
57
+ // if ( $images ) {
 
 
 
 
58
 
59
+ foreach ( $images as $image ) {
60
+ $meta = unserialize( $image->file_meta );
 
61
 
62
+ if ( $meta['width'] > $maxW || $meta['height'] > $maxH ) {
 
63
  $count++;
64
 
65
  $results[] = array(
71
  }
72
 
73
  // make sure we only return a limited number of records so we don't overload the ajax features
74
+ if ( $count >= IMSANITY_AJAX_MAX_RECORDS ) break 2;
75
  }
76
+ $offset += $limit;
77
+ // }
78
+ } // endwhile
79
+ die( json_encode( $results ) );
80
  }
81
 
82
  /**
89
 
90
  global $wpdb;
91
 
92
+ $id = (int) $_POST['id'];
93
 
94
+ if ( ! $id ) {
95
+ die( json_encode( array( 'success' => false, 'message' => esc_html__( 'Missing ID Parameter', 'imsanity' ) ) ) );
 
 
 
96
  }
97
 
98
+ //$images = $wpdb->get_results( $query );
99
+ $meta = wp_get_attachment_metadata( $id );
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
+ if ( $meta && is_array( $meta ) ) {
 
 
 
102
  $uploads = wp_upload_dir();
103
+ // TODO: we can do better here, sub in a version of the EWWW file finder
104
  $oldPath = $uploads['basedir'] . "/" . $meta['file'];
105
+ if ( ! is_writable( $oldPath ) ) {
106
+ $msg = sprintf( esc_html__( '%s is not writable', 'imsanity' ), $oldPath );
107
+ die( json_encode( array( 'success' => false, 'message' => $msg ) ) );
108
+ }
109
 
110
+ $maxW = imsanity_get_option( 'imsanity_max_width', IMSANITY_DEFAULT_MAX_WIDTH );
111
+ $maxH = imsanity_get_option( 'imsanity_max_height', IMSANITY_DEFAULT_MAX_HEIGHT );
112
 
113
  // method one - slow but accurate, get file size from file itself
114
+ list( $oldW, $oldH ) = getimagesize( $oldPath );
115
  // method two - get file size from meta, fast but resize will fail if meta is out of sync
116
+ if ( ! $oldW || ! $oldH ) {
117
+ $oldW = $meta['width'];
118
+ $oldH = $meta['height'];
119
+ }
120
 
121
+ if ( ( $oldW > $maxW && $maxW > 0 ) || ( $oldH > $maxH && $maxH > 0 ) ) {
122
+ $quality = imsanity_get_option( 'imsanity_quality', IMSANITY_DEFAULT_QUALITY );
 
123
 
124
+ if ( $oldW > $maxW && $maxW > 0 && $oldH > $maxH && $maxH > 0 && apply_filters( 'imsanity_crop_image', false ) ) {
125
+ $newW = $maxW;
126
+ $newH = $maxH;
127
+ } else {
128
+ list( $newW, $newH ) = wp_constrain_dimensions( $oldW, $oldH, $maxW, $maxH );
129
+ }
130
 
131
+ $resizeResult = imsanity_image_resize( $oldPath, $newW, $newH, apply_filters( 'imsanity_crop_image', false ), null, null, $quality);
132
+ // $resizeResult = new WP_Error('invalid_image', __('Could not read image size'), $oldPath); // uncomment to debug fail condition
133
 
134
+ if ( $resizeResult && ! is_wp_error( $resizeResult ) ) {
 
135
  $newPath = $resizeResult;
136
 
137
+ if ( $newPath != $oldPath && is_file( $newPath ) && filesize( $newPath ) < filesize( $oldPath ) ) {
138
+ // we saved some file space. remove original and replace with resized image
139
+ unlink( $oldPath );
140
+ rename( $newPath, $oldPath );
141
+ $meta['width'] = $newW;
142
+ $meta['height'] = $newH;
143
+
144
+ wp_update_attachment_metadata( $id, $meta );
145
+
146
+ $results = array( 'success'=>true, 'id'=> $id, 'message' => sprintf( esc_html__( 'OK: %s', 'imsanity' ) , $oldPath ) );
147
+ } elseif ( $newPath != $oldPath ) {
148
+ // theresized image is actually bigger in filesize (most likely due to jpg quality).
149
+ // keep the old one and just get rid of the resized image
150
+ if ( is_file( $newPath ) ) {
151
+ unlink( $newPath );
152
+ }
153
+ $results = array( 'success'=>false, 'id'=> $id, 'message' => sprintf( esc_html__( 'ERROR: %s (%s)', 'imsanity' ) , $oldPath, esc_html__( 'Resized image was larger than the original', 'imsanity' ) ) );
154
+ } else {
155
+ $results = array( 'success'=>false, 'id'=> $id, 'message' => sprintf( esc_html__( 'ERROR: %s (%s)', 'imsanity' ) , $oldPath, esc_html__( 'Unknown error, resizing function returned the same filename', 'imsanity' ) ) );
156
  }
157
 
158
+ } else if ( $resizeResult === false ) {
159
+ $results = array(
160
+ 'success' => false,
161
+ 'id' => $id,
162
+ 'message' => sprintf( esc_html__( 'ERROR: %s (%s)', 'imsanity' ), $oldPath, 'wp_get_image_editor missing' ),
163
+ );
164
+ } else {
165
+ $results = array(
166
+ 'success' => false,
167
+ 'id' => $id,
168
+ 'message' => sprintf( esc_html__( 'ERROR: %s (%s)', 'imsanity' ), $oldPath, htmlentities( $resizeResult->get_error_message() ) )
169
  );
 
 
 
 
 
 
 
 
170
  }
171
+ } else {
172
+ $results = array('success'=>true,'id'=> $id, 'message' => sprintf( esc_html__( 'SKIPPED: %s (Resize not required)', 'imsanity' ) , $oldPath ) );
173
  }
174
+ } else {
175
+ $results = array( 'success' => false, 'id'=> $id, 'message' => sprintf( esc_html__( 'ERROR: Attachment with ID of %s not found', 'imsanity' ) , htmlentities( $id ) ) );
 
 
 
 
 
 
 
176
  }
177
 
178
  // if there is a quota we need to reset the directory size cache so it will re-calculate
179
+ delete_transient( 'dirsize_cache' );
180
 
181
+ die( json_encode( $results ) );
 
182
  }
183
+ ?>
 
 
 
 
 
 
 
 
 
images/ajax-loader.gif CHANGED
Binary file
imsanity.php CHANGED
@@ -4,20 +4,20 @@ Plugin Name: Imsanity
4
  Plugin URI: https://wordpress.org/plugins/imsanity/
5
  Description: Imsanity stops insanely huge image uploads
6
  Author: Shane Bishop
7
- Version: 2.3.7
8
  Author URI: https://ewww.io/
9
  Text Domain: imsanity
10
  License: GPLv3
11
  */
12
 
13
- define('IMSANITY_VERSION','2.3.7');
14
- define('IMSANITY_SCHEMA_VERSION','1.1');
15
 
16
- define('IMSANITY_DEFAULT_MAX_WIDTH',2048);
17
- define('IMSANITY_DEFAULT_MAX_HEIGHT',2048);
18
- define('IMSANITY_DEFAULT_BMP_TO_JPG',1);
19
- define('IMSANITY_DEFAULT_PNG_TO_JPG',0);
20
- define('IMSANITY_DEFAULT_QUALITY',82);
21
 
22
  define( 'IMSANITY_SOURCE_POST', 1 );
23
  define( 'IMSANITY_SOURCE_LIBRARY', 2 );
@@ -27,27 +27,19 @@ if ( ! defined( 'IMSANITY_AJAX_MAX_RECORDS' ) ) {
27
  define( 'IMSANITY_AJAX_MAX_RECORDS', 250 );
28
  }
29
 
30
- /**
31
- * Load Translations
32
- */
33
- load_plugin_textdomain('imsanity', false, 'imsanity/languages/');
 
 
34
 
35
  /**
36
  * import supporting libraries
37
  */
38
- include_once(plugin_dir_path(__FILE__).'libs/utils.php');
39
- include_once(plugin_dir_path(__FILE__).'settings.php');
40
- include_once(plugin_dir_path(__FILE__).'ajax.php');
41
-
42
- /**
43
- * Fired with the WordPress upload dialog is displayed
44
- */
45
- function imsanity_upload_ui()
46
- {
47
- // TODO: output a message on the upload form showing that imanity is enabled
48
- // echo '<p class="imsanity-upload-message">' . __("Imsanity plugin is enabled. Add the text 'noresize' to the filename to bypass.") . '</p>';
49
- }
50
-
51
 
52
  /**
53
  * Inspects the request and determines where the upload came from
@@ -98,21 +90,20 @@ function imsanity_get_max_width_height( $source ) {
98
  * to see if it is too big and, if so, resize and overwrite the original
99
  * @param Array $params
100
  */
101
- function imsanity_handle_upload($params)
102
- {
103
  /* debug logging... */
104
  // file_put_contents ( "debug.txt" , print_r($params,1) . "\n" );
105
 
106
  // if "noresize" is included in the filename then we will bypass imsanity scaling
107
- if (strpos($params['file'],'noresize') !== false) return $params;
108
 
109
  // if preferences specify so then we can convert an original bmp or png file into jpg
110
- if ($params['type'] == 'image/bmp' && imsanity_get_option('imsanity_bmp_to_jpg',IMSANITY_DEFAULT_BMP_TO_JPG)) {
111
- $params = imsanity_convert_to_jpg('bmp',$params);
112
  }
113
 
114
- if ($params['type'] == 'image/png' && imsanity_get_option('imsanity_png_to_jpg',IMSANITY_DEFAULT_PNG_TO_JPG)) {
115
- $params = imsanity_convert_to_jpg('png',$params);
116
  }
117
 
118
  // make sure this is a type of image that we want to convert and that it exists
@@ -126,15 +117,14 @@ function imsanity_handle_upload($params)
126
  // $oldPath = $ud['path'] . DIRECTORY_SEPARATOR . $oldPath;
127
  // }
128
 
129
- if ( (!is_wp_error($params)) && file_exists($oldPath) && in_array($params['type'], array('image/png','image/gif','image/jpeg')))
130
- {
131
 
132
  // figure out where the upload is coming from
133
  $source = imsanity_get_source();
134
 
135
- list($maxW,$maxH) = imsanity_get_max_width_height($source);
136
 
137
- list($oldW, $oldH) = getimagesize( $oldPath );
138
 
139
  /* HACK: if getimagesize returns an incorrect value (sometimes due to bad EXIF data..?)
140
  $img = imagecreatefromjpeg ($oldPath);
@@ -150,51 +140,52 @@ function imsanity_handle_upload($params)
150
  $oldH = $header['width'];
151
  //*/
152
 
153
- if (($oldW > $maxW && $maxW > 0) || ($oldH > $maxH && $maxH > 0))
154
- {
155
- $quality = imsanity_get_option('imsanity_quality',IMSANITY_DEFAULT_QUALITY);
156
-
157
- list($newW, $newH) = wp_constrain_dimensions($oldW, $oldH, $maxW, $maxH);
 
 
 
 
158
 
159
- $resizeResult = imsanity_image_resize( $oldPath, $newW, $newH, false, null, null, $quality);
 
 
 
 
160
 
161
  /* uncomment to debug error handling code: */
162
  // $resizeResult = new WP_Error('invalid_image', __(print_r($_REQUEST)), $oldPath);
163
 
164
- if (!is_wp_error($resizeResult))
165
- {
166
  $newPath = $resizeResult;
167
-
168
- if (filesize($newPath) < filesize($oldPath)) {
169
  // we saved some file space. remove original and replace with resized image
170
- unlink($oldPath);
171
- rename($newPath, $oldPath);
172
- }
173
- else {
174
  // theresized image is actually bigger in filesize (most likely due to jpg quality).
175
  // keep the old one and just get rid of the resized image
176
- unlink($newPath);
177
  }
178
- }
179
- else
180
- {
181
  // resize didn't work, likely because the image processing libraries are missing
182
 
183
  // remove the old image so we don't leave orphan files hanging around
184
- unlink($oldPath);
185
 
186
  $params = wp_handle_upload_error( $oldPath ,
187
- sprintf( __("Oh Snap! Imsanity was unable to resize this image "
188
- . "for the following reason: '%s'
189
- . If you continue to see this error message, you may need to either install missing server"
190
- . " components or disable the Imsanity plugin."
191
- . " If you think you have discovered a bug, please report it on the Imsanity support forum.", 'imsanity' ) ,$resizeResult->get_error_message() ) );
192
 
193
  }
194
  }
195
 
196
  }
197
-
198
  return $params;
199
  }
200
 
@@ -208,31 +199,29 @@ function imsanity_handle_upload($params)
208
  * @param array $params
209
  * @return array altered params
210
  */
211
- function imsanity_convert_to_jpg($type,$params)
212
  {
213
 
214
  $img = null;
215
 
216
- if ($type == 'bmp') {
217
- include_once('libs/imagecreatefrombmp.php');
218
- $img = imagecreatefrombmp($params['file']);
219
- }
220
- elseif ($type == 'png') {
221
-
222
- if(!function_exists('imagecreatefrompng')) {
223
- return wp_handle_upload_error( $params['file'],'imsanity_convert_to_jpg requires gd library enabled');
224
  }
225
 
226
- $img = imagecreatefrompng($params['file']);
227
  // convert png transparency to white
228
- $bg = imagecreatetruecolor(imagesx($img), imagesy($img));
229
- imagefill($bg, 0, 0, imagecolorallocate($bg, 255, 255, 255));
230
- imagealphablending($bg, TRUE);
231
  imagecopy($bg, $img, 0, 0, 0, 0, imagesx($img), imagesy($img));
232
 
233
  }
234
  else {
235
- return wp_handle_upload_error( $params['file'],'Unknown image type specified in imsanity_convert_to_jpg');
236
  }
237
 
238
  // we need to change the extension from the original to .jpg so we have to ensure it will be a unique filename
@@ -241,10 +230,9 @@ function imsanity_convert_to_jpg($type,$params)
241
  $newFileName = basename(str_ireplace(".".$type, ".jpg", $oldFileName));
242
  $newFileName = wp_unique_filename( $uploads['path'], $newFileName );
243
 
244
- $quality = imsanity_get_option('imsanity_quality',IMSANITY_DEFAULT_QUALITY);
245
 
246
- if (imagejpeg($img,$uploads['path'] . '/' . $newFileName, $quality))
247
- {
248
  // conversion succeeded. remove the original bmp & remap the params
249
  unlink($params['file']);
250
 
@@ -257,25 +245,13 @@ function imsanity_convert_to_jpg($type,$params)
257
  unlink($params['file']);
258
 
259
  return wp_handle_upload_error( $oldPath,
260
- __("Oh Snap! Imsanity was Unable to process the $type file. "
261
- ."If you continue to see this error you may need to disable the $type-To-JPG "
262
- ."feature in Imsanity settings.", 'imsanity' ) );
263
  }
264
 
265
  return $params;
266
  }
267
 
268
-
269
  /* add filters to hook into uploads */
270
  add_filter( 'wp_handle_upload', 'imsanity_handle_upload' );
271
-
272
- /* add filters/actions to customize upload page */
273
- //add_action('post-upload-ui', 'imsanity_upload_ui');
274
-
275
-
276
-
277
-
278
- // TODO: if necessary to update the post data in the future...
279
- // add_filter( 'wp_update_attachment_metadata', 'imsanity_handle_update_attachment_metadata' );
280
-
281
  ?>
4
  Plugin URI: https://wordpress.org/plugins/imsanity/
5
  Description: Imsanity stops insanely huge image uploads
6
  Author: Shane Bishop
7
+ Version: 2.3.8
8
  Author URI: https://ewww.io/
9
  Text Domain: imsanity
10
  License: GPLv3
11
  */
12
 
13
+ define( 'IMSANITY_VERSION', '2.3.8' );
14
+ define( 'IMSANITY_SCHEMA_VERSION', '1.1' );
15
 
16
+ define( 'IMSANITY_DEFAULT_MAX_WIDTH', 2048 );
17
+ define( 'IMSANITY_DEFAULT_MAX_HEIGHT', 2048 );
18
+ define( 'IMSANITY_DEFAULT_BMP_TO_JPG', 1 );
19
+ define( 'IMSANITY_DEFAULT_PNG_TO_JPG', 0 );
20
+ define( 'IMSANITY_DEFAULT_QUALITY', 82 );
21
 
22
  define( 'IMSANITY_SOURCE_POST', 1 );
23
  define( 'IMSANITY_SOURCE_LIBRARY', 2 );
27
  define( 'IMSANITY_AJAX_MAX_RECORDS', 250 );
28
  }
29
 
30
+ function imsanity_init() {
31
+ /**
32
+ * Load Translations
33
+ */
34
+ load_plugin_textdomain( 'imsanity', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
35
+ }
36
 
37
  /**
38
  * import supporting libraries
39
  */
40
+ include_once( plugin_dir_path(__FILE__) . 'libs/utils.php' );
41
+ include_once( plugin_dir_path(__FILE__) . 'settings.php' );
42
+ include_once( plugin_dir_path(__FILE__) . 'ajax.php' );
 
 
 
 
 
 
 
 
 
 
43
 
44
  /**
45
  * Inspects the request and determines where the upload came from
90
  * to see if it is too big and, if so, resize and overwrite the original
91
  * @param Array $params
92
  */
93
+ function imsanity_handle_upload( $params ) {
 
94
  /* debug logging... */
95
  // file_put_contents ( "debug.txt" , print_r($params,1) . "\n" );
96
 
97
  // if "noresize" is included in the filename then we will bypass imsanity scaling
98
+ if ( strpos( $params['file'], 'noresize' ) !== false ) return $params;
99
 
100
  // if preferences specify so then we can convert an original bmp or png file into jpg
101
+ if ( $params['type'] == 'image/bmp' && imsanity_get_option( 'imsanity_bmp_to_jpg', IMSANITY_DEFAULT_BMP_TO_JPG ) ) {
102
+ $params = imsanity_convert_to_jpg( 'bmp', $params );
103
  }
104
 
105
+ if ( $params['type'] == 'image/png' && imsanity_get_option( 'imsanity_png_to_jpg', IMSANITY_DEFAULT_PNG_TO_JPG ) ) {
106
+ $params = imsanity_convert_to_jpg( 'png', $params );
107
  }
108
 
109
  // make sure this is a type of image that we want to convert and that it exists
117
  // $oldPath = $ud['path'] . DIRECTORY_SEPARATOR . $oldPath;
118
  // }
119
 
120
+ if ( ( ! is_wp_error( $params ) ) && is_writable( $oldPath ) && in_array( $params['type'], array( 'image/png', 'image/gif', 'image/jpeg' ) ) ) {
 
121
 
122
  // figure out where the upload is coming from
123
  $source = imsanity_get_source();
124
 
125
+ list( $maxW,$maxH ) = imsanity_get_max_width_height( $source );
126
 
127
+ list( $oldW, $oldH ) = getimagesize( $oldPath );
128
 
129
  /* HACK: if getimagesize returns an incorrect value (sometimes due to bad EXIF data..?)
130
  $img = imagecreatefromjpeg ($oldPath);
140
  $oldH = $header['width'];
141
  //*/
142
 
143
+ if ( ( $oldW > $maxW && $maxW > 0 ) || ( $oldH > $maxH && $maxH > 0 ) ) {
144
+ $quality = imsanity_get_option( 'imsanity_quality', IMSANITY_DEFAULT_QUALITY );
145
+
146
+ if ( $oldW > $maxW && $maxW > 0 && $oldH > $maxH && $maxH > 0 && apply_filters( 'imsanity_crop_image', false ) ) {
147
+ $newW = $maxW;
148
+ $newH = $maxH;
149
+ } else {
150
+ list( $newW, $newH ) = wp_constrain_dimensions( $oldW, $oldH, $maxW, $maxH );
151
+ }
152
 
153
+ remove_filter( 'wp_image_editors', 'ewww_image_optimizer_load_editor', 60 );
154
+ $resizeResult = imsanity_image_resize( $oldPath, $newW, $newH, apply_filters( 'imsanity_crop_image', false ), null, null, $quality );
155
+ if ( function_exists( 'ewww_image_optimizer_load_editor' ) ) {
156
+ add_filter( 'wp_image_editors', 'ewww_image_optimizer_load_editor', 60 );
157
+ }
158
 
159
  /* uncomment to debug error handling code: */
160
  // $resizeResult = new WP_Error('invalid_image', __(print_r($_REQUEST)), $oldPath);
161
 
162
+ if ( $resizeResult && ! is_wp_error( $resizeResult ) ) {
 
163
  $newPath = $resizeResult;
164
+
165
+ if ( is_file( $newPath ) && filesize( $newPath ) < filesize( $oldPath ) ) {
166
  // we saved some file space. remove original and replace with resized image
167
+ unlink( $oldPath );
168
+ rename( $newPath, $oldPath );
169
+ } elseif ( is_file( $newPath ) ) {
 
170
  // theresized image is actually bigger in filesize (most likely due to jpg quality).
171
  // keep the old one and just get rid of the resized image
172
+ unlink( $newPath );
173
  }
174
+ } else if ( $resizeResult === false ) {
175
+ return $params;
176
+ } else {
177
  // resize didn't work, likely because the image processing libraries are missing
178
 
179
  // remove the old image so we don't leave orphan files hanging around
180
+ unlink( $oldPath );
181
 
182
  $params = wp_handle_upload_error( $oldPath ,
183
+ sprintf( esc_html__( "Imsanity was unable to resize this image for the following reason: %s. If you continue to see this error message, you may need to install missing server components. If you think you have discovered a bug, please report it on the Imsanity support forum: %s", 'imsanity' ), $resizeResult->get_error_message(), 'https://wordpress.org/support/plugin/imsanity' ) );
 
 
 
 
184
 
185
  }
186
  }
187
 
188
  }
 
189
  return $params;
190
  }
191
 
199
  * @param array $params
200
  * @return array altered params
201
  */
202
+ function imsanity_convert_to_jpg( $type, $params )
203
  {
204
 
205
  $img = null;
206
 
207
+ if ( $type == 'bmp' ) {
208
+ include_once( 'libs/imagecreatefrombmp.php' );
209
+ $img = imagecreatefrombmp( $params['file'] );
210
+ } elseif ( $type == 'png' ) {
211
+ if( ! function_exists( 'imagecreatefrompng' ) ) {
212
+ return wp_handle_upload_error( $params['file'], esc_html__( 'Imsanity requires the GD library to convert PNG images to JPG', 'imsanity' ) );
 
 
213
  }
214
 
215
+ $img = imagecreatefrompng( $params['file'] );
216
  // convert png transparency to white
217
+ $bg = imagecreatetruecolor( imagesx( $img ), imagesy( $img ) );
218
+ imagefill( $bg, 0, 0, imagecolorallocate( $bg, 255, 255, 255 ) );
219
+ imagealphablending( $bg, TRUE );
220
  imagecopy($bg, $img, 0, 0, 0, 0, imagesx($img), imagesy($img));
221
 
222
  }
223
  else {
224
+ return wp_handle_upload_error( $params['file'], esc_html__( 'Unknown image type specified in imsanity_convert_to_jpg', 'imsanity' ) );
225
  }
226
 
227
  // we need to change the extension from the original to .jpg so we have to ensure it will be a unique filename
230
  $newFileName = basename(str_ireplace(".".$type, ".jpg", $oldFileName));
231
  $newFileName = wp_unique_filename( $uploads['path'], $newFileName );
232
 
233
+ $quality = imsanity_get_option('imsanity_quality', IMSANITY_DEFAULT_QUALITY );
234
 
235
+ if ( imagejpeg( $img, $uploads['path'] . '/' . $newFileName, $quality ) ) {
 
236
  // conversion succeeded. remove the original bmp & remap the params
237
  unlink($params['file']);
238
 
245
  unlink($params['file']);
246
 
247
  return wp_handle_upload_error( $oldPath,
248
+ sprintf( esc_html__( "Imsanity was unable to process the %s file. If you continue to see this error you may need to disable the conversion option in the Imsanity settings.", 'imsanity' ), $type ) );
 
 
249
  }
250
 
251
  return $params;
252
  }
253
 
 
254
  /* add filters to hook into uploads */
255
  add_filter( 'wp_handle_upload', 'imsanity_handle_upload' );
256
+ add_action( 'plugins_loaded', 'imsanity_init' );
 
 
 
 
 
 
 
 
 
257
  ?>
languages/readme.txt CHANGED
@@ -2,44 +2,10 @@ LANGUAGE TRANSLATION FILES FOR IMSANITY
2
  ---------------------------------------
3
 
4
  If you are interested in creating a language translation for Imsanity then
5
- you've found the right place! Your help is greatly appreciated and allows
6
- Imsanity to be more easily used by people all over the world.
7
- Imsanity uses the language that is configured for WordPress in wp-config.php
8
- and so it can support any language for which a translation has been provided.
9
-
10
- The language files used by WordPress and Imsanity are extremely simple
11
- to create and edit, however a plugin or software application is required to work
12
- with the file formats. The only difficult part is actually translating the
13
- text into the new language!
14
-
15
- A ".po" file is a plain text file that you edit using a plugin or application.
16
- You simply enter a tranlations for each phrase. Once all of the phrases have
17
- been translated, you use the software to generate an ".mo" file. Save both
18
- of these files in the imsanity/languages folder and it will be used on
19
- all imsanity configuration pages.
20
-
21
- HOW TO CREATE OR UPDATE TRANSLATION FILES:
22
-
23
- The easiest way to create WordPress translations is with the Codestyling Localization
24
- plugin at http://wordpress.org/plugins/codestyling-localization/ . After
25
- installing the plugin, simply login as admin and go to Tools -> Localization.
26
- Refer to the plugin documentation for instructions.
27
 
28
- If you prefer desktop software, POEdit is a free application to view and
29
- edit translation files: http://sourceforge.net/projects/poedit/ You can copy
30
- and use one of the existing .po files as a starting point.
31
-
32
- There's plenty of video tutorials that you can find by searching YouTube
33
- for "po files"
34
-
35
- WHAT DO I DO ONCE I'VE CREATED A TRANSLATION FILE FOR IMSANITY?
36
-
37
- Once you have created the .po and .mo files, zip them up and upload them
38
- to either a file sharing service, your own blog, or any public server
39
- where I can download them.
40
-
41
- Once the files are available, post a message on the Imsanity Support Forum at
42
- http://wordpress.org/support/plugin/imsanity and let me know where to get
43
- them.
44
 
45
  Thank you for supporting Imsanity and all free software!
2
  ---------------------------------------
3
 
4
  If you are interested in creating a language translation for Imsanity then
5
+ you're in the right place! We would love your help, and you
6
+ can get started translating Imsanity at https://translate.wordpress.org/projects/wp-plugins/imsanity
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
+ Anything you can do to help is greatly appreciated and allows
9
+ Imsanity to be more easily used by people all over the world.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  Thank you for supporting Imsanity and all free software!
libs/imagecreatefrombmp.php CHANGED
@@ -11,14 +11,14 @@ if (!function_exists('imagecreatefrombmp')) {
11
  function imagecreatefrombmp($filename) {
12
  // version 1.00
13
  if (!($fh = fopen($filename, 'rb'))) {
14
- trigger_error(sprintf(__('imagecreatefrombmp: Can not open %s!','imsanity'),$filename), E_USER_WARNING);
15
  return false;
16
  }
17
  // read file header
18
- $meta = unpack('vtype/Vfilesize/Vreserved/Voffset', fread($fh, 14));
19
  // check for bitmap
20
  if ($meta['type'] != 19778) {
21
- trigger_error(sprintf(__('imagecreatefrombmp: %s is not a bitmap!','imsanity'), $filename), E_USER_WARNING);
22
  return false;
23
  }
24
  // read image header
@@ -40,7 +40,7 @@ if (!function_exists('imagecreatefrombmp')) {
40
  if ($meta['imagesize'] < 1) {
41
  $meta['imagesize'] = @filesize($filename) - $meta['offset'];
42
  if ($meta['imagesize'] < 1) {
43
- trigger_error(sprintf(__('imagecreatefrombmp: Can not obtain filesize of %s !','imsanity'),$filename ), E_USER_WARNING);
44
  return false;
45
  }
46
  }
@@ -126,7 +126,7 @@ if (!function_exists('imagecreatefrombmp')) {
126
  $color[1] = $palette[ $color[1] + 1 ];
127
  break;
128
  default:
129
- trigger_error( sprintf(__('imagecreatefrombmp: %s has %d bits and this is not supported!','imsanity'),$filename,$meta['bits']), E_USER_WARNING);
130
  return false;
131
  }
132
  imagesetpixel($im, $x, $y, $color[1]);
@@ -141,4 +141,4 @@ if (!function_exists('imagecreatefrombmp')) {
141
  }
142
  }
143
 
144
- ?>
11
  function imagecreatefrombmp($filename) {
12
  // version 1.00
13
  if (!($fh = fopen($filename, 'rb'))) {
14
+ trigger_error( sprintf( __( 'imagecreatefrombmp: Can not open %s!','imsanity') , $filename ), E_USER_WARNING );
15
  return false;
16
  }
17
  // read file header
18
+ $meta = unpack('vtype/Vfilesize/Vreserved/Voffset', fread( $fh, 14 ) );
19
  // check for bitmap
20
  if ($meta['type'] != 19778) {
21
+ trigger_error( sprintf( __( 'imagecreatefrombmp: %s is not a bitmap!', 'imsanity' ), $filename ), E_USER_WARNING );
22
  return false;
23
  }
24
  // read image header
40
  if ($meta['imagesize'] < 1) {
41
  $meta['imagesize'] = @filesize($filename) - $meta['offset'];
42
  if ($meta['imagesize'] < 1) {
43
+ trigger_error( sprintf( __( 'imagecreatefrombmp: Cannot obtain filesize of %s !', 'imsanity' ), $filename ), E_USER_WARNING );
44
  return false;
45
  }
46
  }
126
  $color[1] = $palette[ $color[1] + 1 ];
127
  break;
128
  default:
129
+ trigger_error( sprintf( __( 'imagecreatefrombmp: %s has %d bits and this is not supported!', 'imsanity' ), $filename, $meta['bits'] ), E_USER_WARNING );
130
  return false;
131
  }
132
  imagesetpixel($im, $x, $y, $color[1]);
141
  }
142
  }
143
 
144
+ ?>
libs/utils.php CHANGED
@@ -43,9 +43,7 @@ function imsanity_fatal( $message, $title = "", $die = false ) {
43
  * @return mixed WP_Error on failure. String with new destination path.
44
  */
45
  function imsanity_image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 82 ) {
46
-
47
  if ( function_exists( 'wp_get_image_editor' ) ) {
48
- // TODO: check to make sure file is writable
49
  // WP 3.5 and up use the image editor
50
 
51
  $editor = wp_get_image_editor( $file );
@@ -92,12 +90,7 @@ function imsanity_image_resize( $file, $max_w, $max_h, $crop = false, $suffix =
92
 
93
  return $dest_file;
94
  }
95
- else
96
- {
97
- // TODO: remove this, but handle failure properly upstream
98
- // wordpress prior to 3.5 uses the old image_resize function
99
- return image_resize( $file, $max_w, $max_h, $crop, $suffix, $dest_path, $jpeg_quality);
100
- }
101
  }
102
 
103
  ?>
43
  * @return mixed WP_Error on failure. String with new destination path.
44
  */
45
  function imsanity_image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 82 ) {
 
46
  if ( function_exists( 'wp_get_image_editor' ) ) {
 
47
  // WP 3.5 and up use the image editor
48
 
49
  $editor = wp_get_image_editor( $file );
90
 
91
  return $dest_file;
92
  }
93
+ return false;
 
 
 
 
 
94
  }
95
 
96
  ?>
readme.txt CHANGED
@@ -2,9 +2,9 @@
2
  Contributors: nosilver4u,verysimple
3
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=MKMQKCBFFG3WW
4
  Tags: image, scale, resize, space saver, quality
5
- Requires at least: 3.9
6
  Tested up to: 4.7
7
- Stable tag: 2.3.7
8
 
9
  Imsanity automatically resizes huge image uploads. Are contributors uploading huge photos? Tired of manually scaling? Imsanity to the rescue!
10
 
@@ -133,6 +133,9 @@ Questions may be posted on the support forum at https://wordpress.org/support/pl
133
 
134
  == Upgrade Notice ==
135
 
 
 
 
136
  = 2.3.6 =
137
  * tested up to WP 4.4
138
  * if resized image is not smaller than original, then keep original
@@ -143,6 +146,19 @@ Questions may be posted on the support forum at https://wordpress.org/support/pl
143
 
144
  As some already know, Jason Hinkle passed away earlier this year. He will be sorely missed, but I hope to continue his legacy, to improve and support Imsanity. Who am I? https://ewww.io
145
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  = 2.3.7 =
147
  * fixed: uploads to Media Library not detected properly
148
  * changed: default JPG quality is now 82, to match the WordPress default
2
  Contributors: nosilver4u,verysimple
3
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=MKMQKCBFFG3WW
4
  Tags: image, scale, resize, space saver, quality
5
+ Requires at least: 4.0
6
  Tested up to: 4.7
7
+ Stable tag: 2.3.8
8
 
9
  Imsanity automatically resizes huge image uploads. Are contributors uploading huge photos? Tired of manually scaling? Imsanity to the rescue!
10
 
133
 
134
  == Upgrade Notice ==
135
 
136
+ = 2.3.8 =
137
+ * network settings page only available when plugin is network-activated, please let me know if that sounds crazy to you: https://ewww.io/contact-us/
138
+
139
  = 2.3.6 =
140
  * tested up to WP 4.4
141
  * if resized image is not smaller than original, then keep original
146
 
147
  As some already know, Jason Hinkle passed away earlier this year. He will be sorely missed, but I hope to continue his legacy, to improve and support Imsanity. Who am I? https://ewww.io
148
 
149
+ = 2.3.8 =
150
+ * added: 'imsanity_crop_image' filter to crop images during resizing
151
+ * added: increased security of network settings and AJAX requests
152
+ * changed: metadata fetch and update use correct functions instead of direct database queries
153
+ * changed: bulk resize search is kinder to your database
154
+ * fixed: bulk resize could produce a larger image
155
+ * fixed: image file permissions not checked prior to resizing
156
+ * fixed: EWWW Image Optimizer optimizes image during resizing instead of waiting for metadata generation
157
+ * fixed: JPG quality not displaying correctly on network/multisite settings page
158
+ * fixed: some strings were not translatable
159
+ * fixed: bulk resize results container was not scrollable
160
+ * removed: legacy resize function for WP lower than 3.5
161
+
162
  = 2.3.7 =
163
  * fixed: uploads to Media Library not detected properly
164
  * changed: default JPG quality is now 82, to match the WordPress default
scripts/imsanity.js CHANGED
@@ -2,10 +2,6 @@
2
  * imsanity admin javascript functions
3
  */
4
 
5
- // this must run inline so that the script is detected correctly
6
- var imsanity_scripts = document.getElementsByTagName("script");
7
- var imsanity_script_url = imsanity_scripts[imsanity_scripts.length-1].src;
8
-
9
  /**
10
  * Begin the process of re-sizing all of the checked images
11
  */
@@ -18,23 +14,12 @@ function imsanity_resize_images()
18
 
19
  var target = jQuery('#resize_results');
20
  target.html('');
21
- target.show();
22
- jQuery(document).scrollTop(target.offset().top);
23
 
24
  // start the recursion
25
  imsanity_resize_next(images,0);
26
  }
27
 
28
- /**
29
- * Detect the base url for the imsanity plugin folder
30
- * @returns
31
- */
32
- function imsanity_get_base_url()
33
- {
34
- return imsanity_script_url.substring(0,imsanity_script_url.indexOf('scripts/imsanity.js'));
35
- }
36
-
37
-
38
  /**
39
  * recursive function for resizing images
40
  */
@@ -44,22 +29,26 @@ function imsanity_resize_next(images,next_index)
44
 
45
  jQuery.post(
46
  ajaxurl, // (defined by wordpress - points to admin-ajax.php)
47
- {action: 'imsanity_resize_image', id: images[next_index]},
48
  function(response)
49
  {
50
  var result;
51
  var target = jQuery('#resize_results');
 
52
 
53
  try {
54
  result = JSON.parse(response);
55
- target.append('<div>' + (next_index+1) + ' of ' + images.length + ' &gt;&gt; ' + result['message'] +'</div>');
56
  }
57
  catch(e) {
58
- target.append('<div>Error parsing server response for POST ' + images[next_index] + ': '+ e.message +'. Check the console for details.</div>');
59
- if (console) console.warn('Invalid JSON Response: ' + response);
 
 
 
60
  }
61
 
62
- target.animate({scrollTop: target.height()}, 500);
63
 
64
  // recurse
65
  imsanity_resize_next(images,next_index+1);
@@ -73,8 +62,8 @@ function imsanity_resize_next(images,next_index)
73
  function imsanity_resize_complete()
74
  {
75
  var target = jQuery('#resize_results');
76
- target.append('<div>RESIZE COMPLETE</div>');
77
- target.animate({scrollTop: target.height()}, 500);
78
  }
79
 
80
  /**
@@ -84,11 +73,11 @@ function imsanity_resize_complete()
84
  function imsanity_load_images(container_id)
85
  {
86
  var container = jQuery('#'+container_id);
87
- container.html('<div id="imsanity_target" style="border: solid 2px #666666; padding: 10px; height: 0px; overflow: auto;" />');
88
 
89
  var target = jQuery('#imsanity_target');
90
-
91
- target.html('<div><image src="'+ imsanity_get_base_url() +'images/ajax-loader.gif" style="margin-bottom: .25em; vertical-align:middle;" /> Examining existing attachments. This may take a few moments...</div>');
 
92
 
93
  target.animate({height: [250,'swing']},500, function()
94
  {
@@ -96,29 +85,30 @@ function imsanity_load_images(container_id)
96
 
97
  jQuery.post(
98
  ajaxurl, // (global defined by wordpress - points to admin-ajax.php)
99
- {action: 'imsanity_get_images'},
100
  function(response)
101
  {
102
  var images = JSON.parse(response);
103
 
 
104
  if (images.length > 0)
105
  {
106
- target.html('<div><input id="imsanity_check_all" type="checkbox" checked="checked" onclick="jQuery(\'.imsanity_image_cb\').attr(\'checked\', this.checked);" /> Select All</div>');
107
-
108
  for (var i = 0; i < images.length; i++)
109
  {
110
- target.append('<div><input class="imsanity_image_cb" name="imsanity_images" value="' + images[i].id + '" type="checkbox" checked="checked" /> POST ' + images[i].id + ': ' + images[i].file +' ('+images[i].width+' x '+images[i].height+')</div>');
 
 
 
 
111
  }
112
-
113
- container.append('<p class="submit"><button class="button-primary" onclick="imsanity_resize_images();">Resize Checked Images...</button></p>');
114
- container.append('<div id="resize_results" style="display: none; border: solid 2px #666666; padding: 10px; height: 250px; overflow: auto;" />');
115
  }
116
  else
117
  {
118
- target.html('<div>There are no existing attachments that require resizing. Blam!</div>');
119
 
120
  }
121
  }
122
  );
123
  });
124
- }
2
  * imsanity admin javascript functions
3
  */
4
 
 
 
 
 
5
  /**
6
  * Begin the process of re-sizing all of the checked images
7
  */
14
 
15
  var target = jQuery('#resize_results');
16
  target.html('');
17
+ //jQuery(document).scrollTop(target.offset().top);
 
18
 
19
  // start the recursion
20
  imsanity_resize_next(images,0);
21
  }
22
 
 
 
 
 
 
 
 
 
 
 
23
  /**
24
  * recursive function for resizing images
25
  */
29
 
30
  jQuery.post(
31
  ajaxurl, // (defined by wordpress - points to admin-ajax.php)
32
+ {_wpnonce: imsanity_vars._wpnonce, action: 'imsanity_resize_image', id: images[next_index]},
33
  function(response)
34
  {
35
  var result;
36
  var target = jQuery('#resize_results');
37
+ target.show();
38
 
39
  try {
40
  result = JSON.parse(response);
41
+ target.append('<div>' + (next_index+1) + '/' + images.length + ' &gt;&gt; ' + result['message'] +'</div>');
42
  }
43
  catch(e) {
44
+ target.append('<div>' + imsanity_vars.invalid_response + '</div>');
45
+ if (console) {
46
+ console.warn(images[next_index] + ': '+ e.message);
47
+ console.warn('Invalid JSON Response: ' + response);
48
+ }
49
  }
50
 
51
+ target.animate({scrollTop: target.height()});
52
 
53
  // recurse
54
  imsanity_resize_next(images,next_index+1);
62
  function imsanity_resize_complete()
63
  {
64
  var target = jQuery('#resize_results');
65
+ target.append('<div><strong>' + imsanity_vars.resizing_complete + '</strong></div>');
66
+ target.animate({scrollTop: target.height()});
67
  }
68
 
69
  /**
73
  function imsanity_load_images(container_id)
74
  {
75
  var container = jQuery('#'+container_id);
 
76
 
77
  var target = jQuery('#imsanity_target');
78
+ target.show();
79
+ jQuery('.imsanity-selection').remove();
80
+ jQuery('#imsanity_loading').show();
81
 
82
  target.animate({height: [250,'swing']},500, function()
83
  {
85
 
86
  jQuery.post(
87
  ajaxurl, // (global defined by wordpress - points to admin-ajax.php)
88
+ {_wpnonce: imsanity_vars._wpnonce, action: 'imsanity_get_images'},
89
  function(response)
90
  {
91
  var images = JSON.parse(response);
92
 
93
+ jQuery('#imsanity_loading').hide();
94
  if (images.length > 0)
95
  {
96
+ target.append('<div class="imsanity-selection"><input id="imsanity_check_all" type="checkbox" checked="checked" onclick="jQuery(\'.imsanity_image_cb\').attr(\'checked\', this.checked);" /> Select All</div>');
 
97
  for (var i = 0; i < images.length; i++)
98
  {
99
+ target.append('<div class="imsanity-selection"><input class="imsanity_image_cb" name="imsanity_images" value="' + images[i].id + '" type="checkbox" checked="checked" />' + imsanity_vars.image + ' ' + images[i].id + ': ' + images[i].file +' ('+images[i].width+' x '+images[i].height+')</div>');
100
+ }
101
+ if ( ! jQuery( '#resize-submit' ).length ) {
102
+ container.append('<p id="resize-submit" class="submit"><button class="button-primary" onclick="imsanity_resize_images();">' + imsanity_vars.resize_selected + '</button></p>');
103
+ container.append('<div id="resize_results" style="display: none; border: solid 2px #666666; padding: 10px; height: 250px; overflow: auto;" />');
104
  }
 
 
 
105
  }
106
  else
107
  {
108
+ target.html('<div>' + imsanity_vars.none_found + '</div>');
109
 
110
  }
111
  }
112
  );
113
  });
114
+ }
settings.php CHANGED
@@ -6,9 +6,11 @@
6
  */
7
 
8
  // register the plugin settings menu
9
- add_action('admin_menu', 'imsanity_create_menu');
10
  add_action( 'network_admin_menu', 'imsanity_register_network' );
11
- add_filter("plugin_action_links_imsanity/imsanity.php", 'imsanity_settings_link' );
 
 
12
 
13
  // activation hooks
14
  // TODO: custom table is not removed because de-activating one site shouldn't affect the entire server
@@ -20,27 +22,55 @@ register_activation_hook('imsanity/imsanity.php', 'imsanity_maybe_created_custom
20
  // settings cache
21
  $_imsanity_multisite_settings = null;
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  /**
24
  * Settings link that appears on the plugins overview page
25
  * @param array $links
26
  * @return array
27
  */
28
  function imsanity_settings_link( $links ) {
29
- $links[] = '<a href="'. get_admin_url(null, 'options-general.php?page='.__FILE__) .'">Settings</a>';
30
  return $links;
31
  }
32
 
33
- /**
34
- * Create the settings menu item in the WordPress admin navigation and
35
- * link it to the plugin settings page
36
- */
37
- function imsanity_create_menu()
38
- {
39
- // create new menu for site configuration
40
- add_options_page(__('Imsanity Plugin Settings','imsanity'), 'Imsanity', 'administrator', __FILE__, 'imsanity_settings_page');
41
-
42
- // call register settings function
43
- add_action( 'admin_init', 'imsanity_register_settings' );
 
 
 
 
 
44
  }
45
 
46
  // TODO: legacy code to support previous MU version... ???
@@ -89,7 +119,7 @@ function imsanity_multisite_table_schema_version()
89
  if (!imsanity_multisite_table_exists()) return '0';
90
 
91
  global $wpdb;
92
- $version = $wpdb->get_var('select data from ' . imsanity_get_custom_table_name() . " where setting = 'schema'");
93
 
94
  if (!$version) $version = '1.0'; // this is a legacy version 1.0 installation
95
 
@@ -180,14 +210,6 @@ function imsanity_maybe_created_custom_table()
180
 
181
  }
182
 
183
- /**
184
- * Register the network settings page
185
- */
186
- function imsanity_register_network()
187
- {
188
- add_submenu_page('settings.php', __('Imsanity Network Settings','imsanity'), 'Imsanity', 'manage_options', 'imsanity_network', 'imsanity_network_settings');
189
- }
190
-
191
  /**
192
  * display the form for the multi-site settings page
193
  */
@@ -197,81 +219,80 @@ function imsanity_network_settings()
197
 
198
  echo '
199
  <div class="wrap">
200
- <div id="icon-options-general" class="icon32"><br></div>
201
- <h2>'.__('Imsanity Network Settings','imsanity').'</h2>
202
  ';
203
 
204
  // we only want to update if the form has been submitted
205
- if (isset($_POST['update_settings']))
206
- {
207
- imsanity_network_settings_update();
208
- echo "<div class='updated settings-error'><p><strong>".__("Imsanity network settings saved.",'imsanity')."</strong></p></div>";
209
- }
210
 
211
- imsanity_settings_banner();
212
 
213
  $settings = imsanity_get_multisite_settings();
214
-
215
  ?>
216
 
217
  <form method="post" action="settings.php?page=imsanity_network">
218
- <input type="hidden" name="update_settings" value="1" />
219
-
220
  <table class="form-table">
221
  <tr valign="top">
222
- <th scope="row"><?php _e("Global Settings Override",'imsanity'); ?></th>
223
  <td>
224
  <select name="imsanity_override_site">
225
- <option value="0" <?php if ($settings->imsanity_override_site == '0') echo "selected='selected'" ?> ><?php _e("Allow each site to configure Imsanity settings",'imsanity'); ?></option>
226
- <option value="1" <?php if ($settings->imsanity_override_site == '1') echo "selected='selected'" ?> ><?php _e("Use global Imsanity settings (below) for all sites",'imsanity'); ?></option>
227
  </select>
228
  </td>
229
  </tr>
230
 
231
  <tr valign="top">
232
- <th scope="row"><?php _e("Images uploaded within a Page/Post",'imsanity');?></th>
233
  <td>
234
- Fit within <input name="imsanity_max_width" value="<?php echo $settings->imsanity_max_width ?>" style="width: 50px;" />
235
- x <input name="imsanity_max_height" value="<?php echo $settings->imsanity_max_height ?>" style="width: 50px;" /> pixels width/height <?php _e(" (or enter 0 to disable)",'imsanity'); ?>
236
  </td>
237
  </tr>
238
 
239
  <tr valign="top">
240
- <th scope="row"><?php _e("Images uploaded directly to the Media Library",'imsanity'); ?></th>
241
  <td>
242
- Fit within <input name="imsanity_max_width_library" value="<?php echo $settings->imsanity_max_width_library ?>" style="width: 50px;" />
243
- x <input name="imsanity_max_height_library" value="<?php echo $settings->imsanity_max_height_library ?>" style="width: 50px;" /> pixels width/height <?php _e(" (or enter 0 to disable)",'imsanity'); ?>
244
  </td>
245
  </tr>
246
 
247
  <tr valign="top">
248
- <th scope="row"><?php _e("Images uploaded elsewhere (Theme headers, backgrounds, logos, etc)",'imsanity'); ?></th>
249
  <td>
250
- Fit within <input name="imsanity_max_width_other" value="<?php echo $settings->imsanity_max_width_other ?>" style="width: 50px;" />
251
- x <input name="imsanity_max_height_other" value="<?php echo $settings->imsanity_max_height_other ?>" style="width: 50px;" /> pixels width/height <?php _e(" (or enter 0 to disable)",'imsanity'); ?>
252
  </td>
253
  </tr>
254
 
255
  <tr valign="top">
256
- <th scope="row"><?php _e("Convert BMP to JPG",'imsanity'); ?></th>
257
  <td><select name="imsanity_bmp_to_jpg">
258
- <option value="1" <?php if ($settings->imsanity_bmp_to_jpg == '1') echo "selected='selected'" ?> ><?php _e("Yes",'imsanity'); ?></option>
259
- <option value="0" <?php if ($settings->imsanity_bmp_to_jpg == '0') echo "selected='selected'" ?> ><?php _e("No",'imsanity'); ?></option>
260
  </select></td>
261
  </tr>
262
 
263
  <tr valign="top">
264
- <th scope="row"><?php _e("Convert PNG to JPG",'imsanity'); ?></th>
265
  <td><select name="imsanity_png_to_jpg">
266
- <option value="1" <?php if ($settings->imsanity_png_to_jpg == '1') echo "selected='selected'" ?> ><?php _e("Yes",'imsanity'); ?></option>
267
- <option value="0" <?php if ($settings->imsanity_png_to_jpg == '0') echo "selected='selected'" ?> ><?php _e("No",'imsanity'); ?></option>
268
  </select></td>
269
  </tr>
270
 
271
  <tr>
272
  <th><label for='imsanity_quality' ><?php esc_html_e( "JPG image quality", 'imsanity' ); ?></th>
273
- <td><input type='text' id='imsanity_quality' name='imsanity_quality' class='small-text' value='<?php echo imsanity_jpg_quality(); ?>' /> <?php esc_html_e('Valid values are 1-100.', 'imsanity' ); ?>
274
- <p class='description'><?php esc_html_e( 'WordPress default is 82','imsanity' ); ?></p></td>
275
  </tr>
276
 
277
  </table>
@@ -288,8 +309,10 @@ function imsanity_network_settings()
288
  * Process the form, update the network settings
289
  * and clear the cached settings
290
  */
291
- function imsanity_network_settings_update()
292
- {
 
 
293
  global $wpdb;
294
  global $_imsanity_multisite_settings;
295
 
@@ -311,7 +334,6 @@ function imsanity_network_settings_update()
311
  $data->imsanity_bmp_to_jpg = $_POST['imsanity_bmp_to_jpg'] == 1;
312
  $data->imsanity_png_to_jpg = $_POST['imsanity_png_to_jpg'] == 1;
313
  $data->imsanity_quality = imsanity_jpg_quality($_POST['imsanity_quality']);
314
-
315
  $wpdb->update(
316
  $table_name,
317
  array('data' => maybe_serialize($data)),
@@ -320,6 +342,12 @@ function imsanity_network_settings_update()
320
 
321
  // clear the cache
322
  $_imsanity_multisite_settings = null;
 
 
 
 
 
 
323
  }
324
 
325
  /**
@@ -391,8 +419,14 @@ function imsanity_get_option($key,$ifnull)
391
  /**
392
  * Register the configuration settings that the plugin will use
393
  */
394
- function imsanity_register_settings()
395
- {
 
 
 
 
 
 
396
  //register our settings
397
  register_setting( 'imsanity-settings-group', 'imsanity_max_height' );
398
  register_setting( 'imsanity-settings-group', 'imsanity_max_width' );
@@ -427,30 +461,15 @@ function imsanity_settings_css()
427
  {
428
  echo "
429
  <style>
430
- #imsanity_header
431
- {
432
  border: solid 1px #c6c6c6;
433
- margin: 12px 2px 8px 2px;
434
- padding: 20px;
435
  background-color: #e1e1e1;
436
  }
437
- #imsanity_header h4
438
- {
439
- margin: 0px 0px 0px 0px;
440
- }
441
- #imsanity_header tr
442
- {
443
- vertical-align: top;
444
- }
445
-
446
- .imsanity_section_header
447
- {
448
- border: solid 1px #c6c6c6;
449
- margin: 12px 2px 8px 2px;
450
- padding: 20px;
451
- background-color: #e1e1e1;
452
- }
453
-
454
  </style>";
455
  }
456
 
@@ -460,10 +479,6 @@ function imsanity_settings_css()
460
  */
461
  function imsanity_settings_banner()
462
  {
463
- // TODO: put this where it belongs, and merge the calls
464
- // register the scripts that are used by the bulk resizer
465
- wp_register_script( 'my_plugin_script', plugins_url('/imsanity/scripts/imsanity.js?v='.IMSANITY_VERSION), array('jquery'));
466
- wp_enqueue_script( 'my_plugin_script' );
467
 
468
  // echo '
469
  // <div id="imsanity_header" style="float: left;">';
@@ -505,11 +520,10 @@ function imsanity_settings_page()
505
 
506
  ?>
507
  <div class="wrap">
508
- <div id="icon-options-general" class="icon32"><br></div>
509
- <h2><?php _e("Imsanity Settings",'imsanity'); ?></h2>
510
  <?php
511
 
512
- imsanity_settings_banner();
513
 
514
  $settings = imsanity_get_multisite_settings();
515
 
@@ -527,26 +541,27 @@ function imsanity_settings_page()
527
  <h2 style="margin-top: 0px;"><?php _e("Bulk Resize Images",'imsanity'); ?></h2>
528
 
529
  <div id="imsanity_header">
530
- <?php _e('<p>If you have existing images that were uploaded prior to installing Imsanity, you may resize them
531
- all in bulk to recover disk space. To begin, click the "Search Images" button to search all existing
532
- attachments for images that are larger than the configured limit.</p>
533
- <p>Limitations: For performance reasons a maximum of ' . IMSANITY_AJAX_MAX_RECORDS . ' images will be returned at one time. Bitmap
534
- image types are not supported and will not appear in the search results.</p>','imsanity'); ?>
535
  </div>
536
 
537
- <div style="border: solid 1px #ff6666; background-color: #ffbbbb; padding: 8px;">
538
- <h4><?php _e('WARNING: BULK RESIZE WILL ALTER YOUR ORIGINAL IMAGES AND CANNOT BE UNDONE!','imsanity'); ?></h4>
539
 
540
- <p><?php _e('It is <strong>HIGHLY</strong> recommended that you backup
541
- your wp-content/uploads folder before proceeding. You will have a chance to preview and select the images to convert.
542
- It is also recommended that you initially select only 1 or 2 images and verify that everything is ok before
543
- processing your entire library. You have been warned!','imsanity'); ?></p>
544
  </div>
545
 
546
  <p class="submit" id="imsanity-examine-button">
547
- <button class="button-primary" onclick="imsanity_load_images('imsanity_image_list');"><?php _e('Search Images...','imsanity'); ?></button>
548
  </p>
549
- <div id='imsanity_image_list'></div>
 
 
 
 
 
 
550
 
551
  <?php
552
 
@@ -561,7 +576,7 @@ function imsanity_settings_page_notice()
561
  {
562
  ?>
563
  <div class="updated settings-error">
564
- <p><strong><?php _e("Imsanity settings have been configured by the server administrator. There are no site-specific settings available.",'imsanity'); ?></strong></p>
565
  </div>
566
 
567
  <?php
@@ -575,28 +590,30 @@ function imsanity_settings_page_form()
575
  {
576
  ?>
577
  <form method="post" action="options.php">
578
-
579
  <?php settings_fields( 'imsanity-settings-group' ); ?>
580
  <table class="form-table">
581
 
582
  <tr valign="middle">
583
  <th scope="row"><?php _e("Images uploaded within a Page/Post",'imsanity'); ?></th>
584
- <td>Fit within <input type="text" style="width: 50px;" name="imsanity_max_width" value="<?php echo get_option('imsanity_max_width',IMSANITY_DEFAULT_MAX_WIDTH); ?>" />
585
- x <input type="text" style="width: 50px;" name="imsanity_max_height" value="<?php echo get_option('imsanity_max_height',IMSANITY_DEFAULT_MAX_HEIGHT); ?>" /> pixels width/height <?php _e(" (or enter 0 to disable)",'imsanity'); ?>
 
586
  </td>
587
  </tr>
588
 
589
  <tr valign="middle">
590
  <th scope="row"><?php _e("Images uploaded directly to the Media Library",'imsanity'); ?></th>
591
- <td>Fit within <input type="text" style="width: 50px;" name="imsanity_max_width_library" value="<?php echo get_option('imsanity_max_width_library',IMSANITY_DEFAULT_MAX_WIDTH); ?>" />
592
- x <input type="text" style="width: 50px;" name="imsanity_max_height_library" value="<?php echo get_option('imsanity_max_height_library',IMSANITY_DEFAULT_MAX_HEIGHT); ?>" /> pixels width/height <?php _e(" (or enter 0 to disable)",'imsanity'); ?>
 
593
  </td>
594
  </tr>
595
 
596
  <tr valign="middle">
597
  <th scope="row"><?php _e("Images uploaded elsewhere (Theme headers, backgrounds, logos, etc)",'imsanity'); ?></th>
598
- <td>Fit within <input type="text" style="width: 50px;" name="imsanity_max_width_other" value="<?php echo get_option('imsanity_max_width_other',IMSANITY_DEFAULT_MAX_WIDTH); ?>" />
599
- x <input type="text" style="width: 50px;" name="imsanity_max_height_other" value="<?php echo get_option('imsanity_max_height_other',IMSANITY_DEFAULT_MAX_HEIGHT); ?>" /> pixels width/height <?php _e(" (or enter 0 to disable)",'imsanity'); ?>
 
600
  </td>
601
  </tr>
602
 
@@ -608,24 +625,24 @@ function imsanity_settings_page_form()
608
  </tr>
609
 
610
  <tr valign="middle">
611
- <th scope="row"><?php _e("Convert BMP To JPG",'imsanity'); ?></th>
612
  <td><select name="imsanity_bmp_to_jpg">
613
- <option <?php if (get_option('imsanity_bmp_to_jpg',IMSANITY_DEFAULT_BMP_TO_JPG) == "1") {echo "selected='selected'";} ?> value="1"><?php _e("Yes",'imsanity'); ?></option>
614
- <option <?php if (get_option('imsanity_bmp_to_jpg',IMSANITY_DEFAULT_BMP_TO_JPG) == "0") {echo "selected='selected'";} ?> value="0"><?php _e("No",'imsanity'); ?></option>
615
  </select></td>
616
  </tr>
617
 
618
  <tr valign="middle">
619
- <th scope="row"><?php _e("Convert PNG To JPG",'imsanity'); ?></th>
620
  <td><select name="imsanity_png_to_jpg">
621
- <option <?php if (get_option('imsanity_png_to_jpg',IMSANITY_DEFAULT_PNG_TO_JPG) == "1") {echo "selected='selected'";} ?> value="1"><?php _e("Yes",'imsanity'); ?></option>
622
- <option <?php if (get_option('imsanity_png_to_jpg',IMSANITY_DEFAULT_PNG_TO_JPG) == "0") {echo "selected='selected'";} ?> value="0"><?php _e("No",'imsanity'); ?></option>
623
  </select></td>
624
  </tr>
625
 
626
  </table>
627
 
628
- <p class="submit"><input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" /></p>
629
 
630
  </form>
631
  <?php
6
  */
7
 
8
  // register the plugin settings menu
9
+ add_action( 'admin_menu', 'imsanity_create_menu' );
10
  add_action( 'network_admin_menu', 'imsanity_register_network' );
11
+ add_filter( 'plugin_action_links_imsanity/imsanity.php', 'imsanity_settings_link' );
12
+ add_action( 'admin_enqueue_scripts', 'imsanity_queue_script' );
13
+ add_action( 'admin_init', 'imsanity_register_settings' );
14
 
15
  // activation hooks
16
  // TODO: custom table is not removed because de-activating one site shouldn't affect the entire server
22
  // settings cache
23
  $_imsanity_multisite_settings = null;
24
 
25
+ /**
26
+ * Create the settings menu item in the WordPress admin navigation and
27
+ * link it to the plugin settings page
28
+ */
29
+ function imsanity_create_menu()
30
+ {
31
+ // create new menu for site configuration
32
+ add_options_page( esc_html__( 'Imsanity Plugin Settings', 'imsanity' ), 'Imsanity', 'administrator', __FILE__, 'imsanity_settings_page' );
33
+ }
34
+
35
+ /**
36
+ * Register the network settings page
37
+ */
38
+ function imsanity_register_network() {
39
+ if ( ! function_exists( 'is_plugin_active_for_network' ) && is_multisite() ) {
40
+ // need to include the plugin library for the is_plugin_active function
41
+ require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
42
+ }
43
+ if ( is_multisite() && is_plugin_active_for_network( 'imsanity/imsanity.php' ) ) {
44
+ add_submenu_page( 'settings.php', esc_html__( 'Imsanity Network Settings', 'imsanity' ), 'Imsanity', 'manage_options', 'imsanity_network', 'imsanity_network_settings' );
45
+ }
46
+ }
47
+
48
  /**
49
  * Settings link that appears on the plugins overview page
50
  * @param array $links
51
  * @return array
52
  */
53
  function imsanity_settings_link( $links ) {
54
+ $links[] = '<a href="'. get_admin_url( null, 'options-general.php?page=' . __FILE__ ) . '">' . esc_html__( 'Settings', 'imsanity' ) . '</a>';
55
  return $links;
56
  }
57
 
58
+ function imsanity_queue_script( $hook ) {
59
+ // make sure we are being called from the settings page
60
+ if ( strpos( $hook, 'settings_page_imsanity' ) !== 0 ) {
61
+ return;
62
+ }
63
+ // register the scripts that are used by the bulk resizer
64
+ wp_enqueue_script( 'imsanity_script', plugins_url( '/imsanity/scripts/imsanity.js' ), array( 'jquery' ), IMSANITY_VERSION );
65
+ wp_localize_script( 'imsanity_script', 'imsanity_vars', array(
66
+ '_wpnonce' => wp_create_nonce( 'imsanity-bulk' ),
67
+ 'resizing_complete' => esc_html__( 'Resizing Complete', 'imsanity' ),
68
+ 'resize_selected' => esc_html__( 'Resize Selected Images', 'imsanity' ),
69
+ 'image' => esc_html__( 'Image', 'imsanity' ),
70
+ 'invalid_response' => esc_html__( 'Received an invalid response, please check for errors in the Developer Tools console of your browser.', 'imsanity' ),
71
+ 'none_found' => esc_html__( 'There are no images that need to be resized.', 'imsanity' ),
72
+ )
73
+ );
74
  }
75
 
76
  // TODO: legacy code to support previous MU version... ???
119
  if (!imsanity_multisite_table_exists()) return '0';
120
 
121
  global $wpdb;
122
+ $version = $wpdb->get_var('SELECT data FROM ' . imsanity_get_custom_table_name() . " WHERE setting = 'schema'");
123
 
124
  if (!$version) $version = '1.0'; // this is a legacy version 1.0 installation
125
 
210
 
211
  }
212
 
 
 
 
 
 
 
 
 
213
  /**
214
  * display the form for the multi-site settings page
215
  */
219
 
220
  echo '
221
  <div class="wrap">
222
+ <h1>' . esc_html__( 'Imsanity Network Settings' , 'imsanity' ) . '</h1>
 
223
  ';
224
 
225
  // we only want to update if the form has been submitted
226
+ // if (isset($_POST['update_settings']))
227
+ // {
228
+ // imsanity_network_settings_update();
229
+ // echo "<div id='imsanity-network-settings-saved' class='updated fade'><p><strong>". esc_html__( "Imsanity network settings saved.", 'imsanity' ) . "</strong></p></div>";
230
+ // }
231
 
232
+ // imsanity_settings_banner();
233
 
234
  $settings = imsanity_get_multisite_settings();
235
+ // TODO: insert labels for all settings
236
  ?>
237
 
238
  <form method="post" action="settings.php?page=imsanity_network">
239
+ <input type="hidden" name="update_imsanity_settings" value="1" />
240
+ <?php wp_nonce_field( "imsanity_network_options" ); ?>
241
  <table class="form-table">
242
  <tr valign="top">
243
+ <th scope="row"><?php esc_html_e( 'Global Settings Override', 'imsanity' ); ?></th>
244
  <td>
245
  <select name="imsanity_override_site">
246
+ <option value="0" <?php if ($settings->imsanity_override_site == '0') echo "selected='selected'" ?> ><?php esc_html_e("Allow each site to configure Imsanity settings",'imsanity'); ?></option>
247
+ <option value="1" <?php if ($settings->imsanity_override_site == '1') echo "selected='selected'" ?> ><?php esc_html_e("Use global Imsanity settings (below) for all sites",'imsanity'); ?></option>
248
  </select>
249
  </td>
250
  </tr>
251
 
252
  <tr valign="top">
253
+ <th><?php esc_html_e("Images uploaded within a Page/Post",'imsanity');?></th>
254
  <td>
255
+ <?php esc_html_e( 'Max Width', 'imsanity' ); ?> <input type="number" step="1" min="0" class='small-text' name="imsanity_max_width" value="<?php echo $settings->imsanity_max_width ?>" />
256
+ <?php esc_html_e( 'Max Height', 'imsanity' ); ?> <input type="number" step="1" min="0" class="small-text" name="imsanity_max_height" value="<?php echo $settings->imsanity_max_height ?>" /> <?php esc_html_e( 'in pixels, enter 0 to disable','imsanity'); ?>
257
  </td>
258
  </tr>
259
 
260
  <tr valign="top">
261
+ <th><?php esc_html_e("Images uploaded directly to the Media Library",'imsanity'); ?></th>
262
  <td>
263
+ <?php esc_html_e( 'Max Width', 'imsanity' ); ?> <input type="number" step="1" min="0" class='small-text' name="imsanity_max_width_library" value="<?php echo $settings->imsanity_max_width_library ?>" />
264
+ <?php esc_html_e( 'Max Height', 'imsanity' ); ?> <input type="number" step="1" min="0" class="small-text" name="imsanity_max_height_library" value="<?php echo $settings->imsanity_max_height_library ?>" /> <?php esc_html_e( 'in pixels, enter 0 to disable','imsanity'); ?>
265
  </td>
266
  </tr>
267
 
268
  <tr valign="top">
269
+ <th scope="row"><?php esc_html_e("Images uploaded elsewhere (Theme headers, backgrounds, logos, etc)",'imsanity'); ?></th>
270
  <td>
271
+ <?php esc_html_e( 'Max Width', 'imsanity' ); ?> <input type="number" step="1" min="0" class='small-text' name="imsanity_max_width_other" value="<?php echo $settings->imsanity_max_width_other ?>" />
272
+ <?php esc_html_e( 'Max Height', 'imsanity' ); ?> <input type="number" step="1" min="0" class="small-text" name="imsanity_max_height_other" value="<?php echo $settings->imsanity_max_height_other ?>" /> <?php esc_html_e( 'in pixels, enter 0 to disable','imsanity'); ?>
273
  </td>
274
  </tr>
275
 
276
  <tr valign="top">
277
+ <th scope="row"><?php esc_html_e("Convert BMP to JPG",'imsanity'); ?></th>
278
  <td><select name="imsanity_bmp_to_jpg">
279
+ <option value="1" <?php if ($settings->imsanity_bmp_to_jpg == '1') echo "selected='selected'" ?> ><?php esc_html_e("Yes",'imsanity'); ?></option>
280
+ <option value="0" <?php if ($settings->imsanity_bmp_to_jpg == '0') echo "selected='selected'" ?> ><?php esc_html_e("No",'imsanity'); ?></option>
281
  </select></td>
282
  </tr>
283
 
284
  <tr valign="top">
285
+ <th scope="row"><?php esc_html_e("Convert PNG to JPG",'imsanity'); ?></th>
286
  <td><select name="imsanity_png_to_jpg">
287
+ <option value="1" <?php if ($settings->imsanity_png_to_jpg == '1') echo "selected='selected'" ?> ><?php esc_html_e("Yes",'imsanity'); ?></option>
288
+ <option value="0" <?php if ($settings->imsanity_png_to_jpg == '0') echo "selected='selected'" ?> ><?php esc_html_e("No",'imsanity'); ?></option>
289
  </select></td>
290
  </tr>
291
 
292
  <tr>
293
  <th><label for='imsanity_quality' ><?php esc_html_e( "JPG image quality", 'imsanity' ); ?></th>
294
+ <td><input type='text' id='imsanity_quality' name='imsanity_quality' class='small-text' value='<?php echo (int) $settings->imsanity_quality; ?>' /> <?php esc_html_e( 'Valid values are 1-100.', 'imsanity' ); ?>
295
+ <p class='description'><?php esc_html_e( 'WordPress default is 82', 'imsanity' ); ?></p></td>
296
  </tr>
297
 
298
  </table>
309
  * Process the form, update the network settings
310
  * and clear the cached settings
311
  */
312
+ function imsanity_network_settings_update() {
313
+ if ( ! current_user_can( 'manage_options' ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'imsanity_network_options' ) ) {
314
+ return;
315
+ }
316
  global $wpdb;
317
  global $_imsanity_multisite_settings;
318
 
334
  $data->imsanity_bmp_to_jpg = $_POST['imsanity_bmp_to_jpg'] == 1;
335
  $data->imsanity_png_to_jpg = $_POST['imsanity_png_to_jpg'] == 1;
336
  $data->imsanity_quality = imsanity_jpg_quality($_POST['imsanity_quality']);
 
337
  $wpdb->update(
338
  $table_name,
339
  array('data' => maybe_serialize($data)),
342
 
343
  // clear the cache
344
  $_imsanity_multisite_settings = null;
345
+ add_action( 'network_admin_notices', 'imsanity_network_settings_saved' );
346
+ }
347
+
348
+ function imsanity_network_settings_saved() {
349
+ // TODO: figure out why this won't fade
350
+ echo "<div id='imsanity-network-settings-saved' class='updated fade'><p><strong>" . esc_html__( "Imsanity network settings saved.", 'imsanity' ) . "</strong></p></div>";
351
  }
352
 
353
  /**
419
  /**
420
  * Register the configuration settings that the plugin will use
421
  */
422
+ function imsanity_register_settings() {
423
+ if ( ! function_exists( 'is_plugin_active_for_network' ) && is_multisite() ) {
424
+ require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
425
+ }
426
+ // we only want to update if the form has been submitted
427
+ if ( isset( $_POST['update_imsanity_settings'] ) && is_multisite() && is_plugin_active_for_network( 'imsanity/imsanity.php' ) ) {
428
+ imsanity_network_settings_update();
429
+ }
430
  //register our settings
431
  register_setting( 'imsanity-settings-group', 'imsanity_max_height' );
432
  register_setting( 'imsanity-settings-group', 'imsanity_max_width' );
461
  {
462
  echo "
463
  <style>
464
+ #imsanity_header {
 
465
  border: solid 1px #c6c6c6;
466
+ margin: 10px 0px;
467
+ padding: 0px 10px;
468
  background-color: #e1e1e1;
469
  }
470
+ #imsanity_header p {
471
+ margin: .5em 0;
472
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
473
  </style>";
474
  }
475
 
479
  */
480
  function imsanity_settings_banner()
481
  {
 
 
 
 
482
 
483
  // echo '
484
  // <div id="imsanity_header" style="float: left;">';
520
 
521
  ?>
522
  <div class="wrap">
523
+ <h1><?php esc_html_e( 'Imsanity Settings', 'imsanity' ); ?></h1>
 
524
  <?php
525
 
526
+ // imsanity_settings_banner();
527
 
528
  $settings = imsanity_get_multisite_settings();
529
 
541
  <h2 style="margin-top: 0px;"><?php _e("Bulk Resize Images",'imsanity'); ?></h2>
542
 
543
  <div id="imsanity_header">
544
+ <p><?php esc_html_e( 'If you have existing images that were uploaded prior to installing Imsanity, you may resize them all in bulk to recover disk space. To begin, click the "Search Images" button to search all existing attachments for images that are larger than the configured limit.', 'imsanity' ); ?></p>
545
+ <p><?php printf( esc_html__( 'NOTE: To give you greater control over the resizing process, a maximum of %d images will be returned at one time. Bitmap images cannot be bulk resized and will not appear in the search results.', 'imsanity' ), IMSANITY_AJAX_MAX_RECORDS ); ?></p>
 
 
 
546
  </div>
547
 
548
+ <div style="border: solid 1px #ff6666; background-color: #ffbbbb; padding: 0 10px;">
549
+ <h4><?php esc_html_e( 'WARNING: Bulk Resize will alter your original images and cannot be undone!', 'imsanity' ); ?></h4>
550
 
551
+ <p><?php esc_html_e( 'It is HIGHLY recommended that you backup your wp-content/uploads folder before proceeding. You will have a chance to preview and select the images to convert.', 'imsanity' ); ?><br>
552
+ <?php esc_html_e( 'It is also recommended that you initially select only 1 or 2 images and verify that everything is working properly before processing your entire library.', 'imsanity' ); ?></p>
 
 
553
  </div>
554
 
555
  <p class="submit" id="imsanity-examine-button">
556
+ <button class="button-primary" onclick="imsanity_load_images('imsanity_image_list');"><?php esc_html_e( 'Search Images...', 'imsanity' ); ?></button>
557
  </p>
558
+ <div id='imsanity_image_list'>
559
+ <div id="imsanity_target" style="display: none; border: solid 2px #666666; padding: 10px; height: 0px; overflow: auto;">
560
+ <div id="imsanity_loading" style="display: none;"><img src="<?php echo plugins_url( 'images/ajax-loader.gif', __FILE__ ); ?>" style="margin-bottom: .25em; vertical-align:middle;" />
561
+ <?php esc_html_e( 'Scanning existing images. This may take a moment.', 'imsanity' ); ?>
562
+ </div>
563
+ </div>
564
+ </div>
565
 
566
  <?php
567
 
576
  {
577
  ?>
578
  <div class="updated settings-error">
579
+ <p><strong><?php esc_html_e("Imsanity settings have been configured by the server administrator. There are no site-specific settings available.",'imsanity'); ?></strong></p>
580
  </div>
581
 
582
  <?php
590
  {
591
  ?>
592
  <form method="post" action="options.php">
 
593
  <?php settings_fields( 'imsanity-settings-group' ); ?>
594
  <table class="form-table">
595
 
596
  <tr valign="middle">
597
  <th scope="row"><?php _e("Images uploaded within a Page/Post",'imsanity'); ?></th>
598
+ <td>
599
+ <?php esc_html_e( 'Max Width', 'imsanity' ); ?> <input type="number" step="1" min="0" class="small-text" name="imsanity_max_width" value="<?php echo get_option('imsanity_max_width',IMSANITY_DEFAULT_MAX_WIDTH); ?>" />
600
+ <?php esc_html_e( 'Max Height', 'imsanity' ); ?> <input type="number" step="1" min="0" class="small-text" name="imsanity_max_height" value="<?php echo get_option('imsanity_max_height',IMSANITY_DEFAULT_MAX_HEIGHT); ?>" /> <?php esc_html_e( 'in pixels, enter 0 to disable','imsanity'); ?>
601
  </td>
602
  </tr>
603
 
604
  <tr valign="middle">
605
  <th scope="row"><?php _e("Images uploaded directly to the Media Library",'imsanity'); ?></th>
606
+ <td>
607
+ <?php esc_html_e( 'Max Width', 'imsanity' ); ?> <input type="number" step="1" min="0" class="small-text" name="imsanity_max_width_library" value="<?php echo get_option('imsanity_max_width_library',IMSANITY_DEFAULT_MAX_WIDTH); ?>" />
608
+ <?php esc_html_e( 'Max Height', 'imsanity' ); ?> <input type="number" step="1" min="0" class="small-text" name="imsanity_max_height_library" value="<?php echo get_option('imsanity_max_height_library',IMSANITY_DEFAULT_MAX_HEIGHT); ?>" /> <?php esc_html_e( 'in pixels, enter 0 to disable','imsanity'); ?>
609
  </td>
610
  </tr>
611
 
612
  <tr valign="middle">
613
  <th scope="row"><?php _e("Images uploaded elsewhere (Theme headers, backgrounds, logos, etc)",'imsanity'); ?></th>
614
+ <td>
615
+ <?php esc_html_e( 'Max Width', 'imsanity' ); ?> <input type="number" step="1" min="0" class="small-text" name="imsanity_max_width_other" value="<?php echo get_option('imsanity_max_width_other',IMSANITY_DEFAULT_MAX_WIDTH); ?>" />
616
+ <?php esc_html_e( 'Max Height', 'imsanity' ); ?> <input type="number" step="1" min="0" class="small-text" name="imsanity_max_height_other" value="<?php echo get_option('imsanity_max_height_other',IMSANITY_DEFAULT_MAX_HEIGHT); ?>" /> <?php esc_html_e( 'in pixels, enter 0 to disable','imsanity'); ?>
617
  </td>
618
  </tr>
619
 
625
  </tr>
626
 
627
  <tr valign="middle">
628
+ <th scope="row"><?php esc_html_e("Convert BMP To JPG",'imsanity'); ?></th>
629
  <td><select name="imsanity_bmp_to_jpg">
630
+ <option <?php if ( get_option( 'imsanity_bmp_to_jpg', IMSANITY_DEFAULT_BMP_TO_JPG) == "1" ) { echo "selected='selected'"; } ?> value="1"><?php esc_html_e("Yes",'imsanity'); ?></option>
631
+ <option <?php if ( get_option( 'imsanity_bmp_to_jpg', IMSANITY_DEFAULT_BMP_TO_JPG) == "0" ) { echo "selected='selected'"; } ?> value="0"><?php esc_html_e("No",'imsanity'); ?></option>
632
  </select></td>
633
  </tr>
634
 
635
  <tr valign="middle">
636
+ <th scope="row"><?php esc_html_e("Convert PNG To JPG",'imsanity'); ?></th>
637
  <td><select name="imsanity_png_to_jpg">
638
+ <option <?php if (get_option('imsanity_png_to_jpg',IMSANITY_DEFAULT_PNG_TO_JPG) == "1") {echo "selected='selected'";} ?> value="1"><?php esc_html_e("Yes",'imsanity'); ?></option>
639
+ <option <?php if (get_option('imsanity_png_to_jpg',IMSANITY_DEFAULT_PNG_TO_JPG) == "0") {echo "selected='selected'";} ?> value="0"><?php esc_html_e("No",'imsanity'); ?></option>
640
  </select></td>
641
  </tr>
642
 
643
  </table>
644
 
645
+ <p class="submit"><input type="submit" class="button-primary" value="<?php esc_attr_e('Save Changes') ?>" /></p>
646
 
647
  </form>
648
  <?php