Imsanity - Version 2.5.0

Version Description

  • added: imsanity_allowed_mimes filter to override the default list of image formats allowed
  • added: imsanity_orientation filter to modify auto-rotation behavior, return 1 to bypass
  • added: imsanity_get_max_width_height filter to customize max width/height
  • added: define network settings as defaults for new sites in multi-site mode
  • fixed: WP threshold of 2560 overrides Imsanity when using larger dimensions
  • fixed: settings link on plugins page broken in some cases
  • fixed: crop filter not applied if max width or height is equal to existing dimension
  • fixed: invalid capabilities used for settings page - props @cfoellmann
Download this release

Release Info

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

Code changes from version 2.4.3 to 2.5.0

Files changed (6) hide show
  1. ajax.php +14 -3
  2. changelog.txt +10 -0
  3. imsanity.php +53 -14
  4. libs/utils.php +6 -7
  5. readme.txt +18 -3
  6. settings.php +171 -21
ajax.php CHANGED
@@ -13,7 +13,7 @@ add_action( 'wp_ajax_imsanity_resize_image', 'imsanity_resize_image' );
13
  * renders a json warning and dies
14
  */
15
  function imsanity_verify_permission() {
16
- if ( ! current_user_can( 'activate_plugins' ) ) { // this isn't a real capability, but super admins can do anything, so it works.
17
  die(
18
  json_encode(
19
  array(
@@ -52,7 +52,6 @@ function imsanity_get_images() {
52
  $count = 0;
53
 
54
  $images = $wpdb->get_results( $wpdb->prepare( "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 = 'attachment' AND posts.post_mime_type LIKE %s AND posts.post_mime_type != 'image/bmp' AND metas.meta_key = '_wp_attachment_metadata' ORDER BY ID DESC LIMIT %d,%d", '%image%', $offset, $limit ) );
55
- /* $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' LIMIT $offset,$limit" ); */
56
  while ( $images ) {
57
 
58
  foreach ( $images as $image ) {
@@ -66,6 +65,18 @@ function imsanity_get_images() {
66
  continue;
67
  }
68
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  if ( imsanity_get_option( 'imsanity_deep_scan', false ) ) {
70
  $file_path = imsanity_attachment_path( $meta, $image->ID, '', false );
71
  if ( $file_path ) {
@@ -153,7 +164,7 @@ function imsanity_resize_image() {
153
  if ( ( $oldw > $maxw && $maxw > 0 ) || ( $oldh > $maxh && $maxh > 0 ) ) {
154
  $quality = imsanity_get_option( 'imsanity_quality', IMSANITY_DEFAULT_QUALITY );
155
 
156
- if ( $oldw > $maxw && $maxw > 0 && $oldh > $maxh && $maxh > 0 && apply_filters( 'imsanity_crop_image', false ) ) {
157
  $neww = $maxw;
158
  $newh = $maxh;
159
  } else {
13
  * renders a json warning and dies
14
  */
15
  function imsanity_verify_permission() {
16
+ if ( ! current_user_can( 'activate_plugins' ) ) {
17
  die(
18
  json_encode(
19
  array(
52
  $count = 0;
53
 
54
  $images = $wpdb->get_results( $wpdb->prepare( "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 = 'attachment' AND posts.post_mime_type LIKE %s AND posts.post_mime_type != 'image/bmp' AND metas.meta_key = '_wp_attachment_metadata' ORDER BY ID DESC LIMIT %d,%d", '%image%', $offset, $limit ) );
 
55
  while ( $images ) {
56
 
57
  foreach ( $images as $image ) {
65
  continue;
66
  }
67
 
68
+ // Let folks filter the allowed mime-types for resizing.
69
+ $allowed_types = apply_filters( 'imsanity_allowed_mimes', array( 'image/png', 'image/gif', 'image/jpeg' ), $meta['file'] );
70
+ if ( is_string( $allowed_types ) ) {
71
+ $allowed_types = array( $allowed_types );
72
+ } elseif ( ! is_array( $allowed_types ) ) {
73
+ $allowed_types = array();
74
+ }
75
+ $ftype = imsanity_quick_mimetype( $meta['file'] );
76
+ if ( ! in_array( $ftype, $allowed_types, true ) ) {
77
+ continue;
78
+ }
79
+
80
  if ( imsanity_get_option( 'imsanity_deep_scan', false ) ) {
81
  $file_path = imsanity_attachment_path( $meta, $image->ID, '', false );
82
  if ( $file_path ) {
164
  if ( ( $oldw > $maxw && $maxw > 0 ) || ( $oldh > $maxh && $maxh > 0 ) ) {
165
  $quality = imsanity_get_option( 'imsanity_quality', IMSANITY_DEFAULT_QUALITY );
166
 
167
+ if ( $maxw > 0 && $maxh > 0 && $oldw >= $maxw && $oldh >= $maxh && ( $oldh > $maxh || $oldw > $maxw ) && apply_filters( 'imsanity_crop_image', false ) ) {
168
  $neww = $maxw;
169
  $newh = $maxh;
170
  } else {
changelog.txt CHANGED
@@ -1,3 +1,13 @@
 
 
 
 
 
 
 
 
 
 
1
  = 2.4.3 =
2
  * changed: default size from 2048 to 1920
3
  * fixed: WP Import plugin breaks during Media imports
1
+ = 2.5.0 =
2
+ * added: imsanity_allowed_mimes filter to override the default list of image formats allowed
3
+ * added: imsanity_orientation filter to modify auto-rotation behavior, return 1 to bypass
4
+ * added: imsanity_get_max_width_height filter to customize max width/height
5
+ * added: define network settings as defaults for new sites in multi-site mode
6
+ * fixed: WP threshold of 2560 overrides Imsanity when using larger dimensions
7
+ * fixed: settings link on plugins page broken in some cases
8
+ * fixed: crop filter not applied if max width or height is equal to existing dimension
9
+ * fixed: invalid capabilities used for settings page - props @cfoellmann
10
+
11
  = 2.4.3 =
12
  * changed: default size from 2048 to 1920
13
  * fixed: WP Import plugin breaks during Media imports
imsanity.php CHANGED
@@ -14,7 +14,8 @@ Plugin URI: https://wordpress.org/plugins/imsanity/
14
  Description: Imsanity stops insanely huge image uploads
15
  Author: Exactly WWW
16
  Text Domain: imsanity
17
- Version: 2.4.3
 
18
  Author URI: https://ewww.io/
19
  License: GPLv3
20
  */
@@ -23,7 +24,7 @@ if ( ! defined( 'ABSPATH' ) ) {
23
  exit;
24
  }
25
 
26
- define( 'IMSANITY_VERSION', '2.4.3' );
27
  define( 'IMSANITY_SCHEMA_VERSION', '1.1' );
28
 
29
  define( 'IMSANITY_DEFAULT_MAX_WIDTH', 1920 );
@@ -40,6 +41,19 @@ if ( ! defined( 'IMSANITY_AJAX_MAX_RECORDS' ) ) {
40
  define( 'IMSANITY_AJAX_MAX_RECORDS', 250 );
41
  }
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  /**
44
  * Load translations for Imsanity.
45
  */
@@ -50,9 +64,9 @@ function imsanity_init() {
50
  /**
51
  * Import supporting libraries.
52
  */
53
- include_once( plugin_dir_path( __FILE__ ) . 'libs/utils.php' );
54
- include_once( plugin_dir_path( __FILE__ ) . 'settings.php' );
55
- include_once( plugin_dir_path( __FILE__ ) . 'ajax.php' );
56
 
57
  /**
58
  * Inspects the request and determines where the upload came from.
@@ -105,7 +119,8 @@ function imsanity_get_max_width_height( $source ) {
105
  break;
106
  }
107
 
108
- return array( $w, $h );
 
109
  }
110
 
111
  /**
@@ -133,12 +148,32 @@ function imsanity_handle_upload( $params ) {
133
  // Make sure this is a type of image that we want to convert and that it exists.
134
  $oldpath = $params['file'];
135
 
136
- if ( ( ! is_wp_error( $params ) ) && is_file( $oldpath ) && is_readable( $oldpath ) && is_writable( $oldpath ) && filesize( $oldpath ) > 0 && in_array( $params['type'], array( 'image/png', 'image/gif', 'image/jpeg' ), true ) ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
 
138
  // figure out where the upload is coming from.
139
  $source = imsanity_get_source();
140
 
141
- list( $maxw,$maxh ) = imsanity_get_max_width_height( $source );
 
 
 
 
 
142
 
143
  list( $oldw, $oldh ) = getimagesize( $oldpath );
144
 
@@ -154,18 +189,21 @@ function imsanity_handle_upload( $params ) {
154
  $oldh = $old_oldw;
155
  }
156
 
157
- if ( $oldw > $maxw && $maxw > 0 && $oldh > $maxh && $maxh > 0 && apply_filters( 'imsanity_crop_image', false ) ) {
158
  $neww = $maxw;
159
  $newh = $maxh;
160
  } else {
161
  list( $neww, $newh ) = wp_constrain_dimensions( $oldw, $oldh, $maxw, $maxh );
162
  }
163
 
164
- remove_filter( 'wp_image_editors', 'ewww_image_optimizer_load_editor', 60 );
165
- $resizeresult = imsanity_image_resize( $oldpath, $neww, $newh, apply_filters( 'imsanity_crop_image', false ), null, null, $quality );
166
- if ( function_exists( 'ewww_image_optimizer_load_editor' ) ) {
167
- add_filter( 'wp_image_editors', 'ewww_image_optimizer_load_editor', 60 );
168
  }
 
 
 
 
169
 
170
  if ( $resizeresult && ! is_wp_error( $resizeresult ) ) {
171
  $newpath = $resizeresult;
@@ -264,6 +302,7 @@ function imsanity_convert_to_jpg( $type, $params ) {
264
  return $params;
265
  }
266
 
267
- /* add filters to hook into uploads */
268
  add_filter( 'wp_handle_upload', 'imsanity_handle_upload' );
 
269
  add_action( 'plugins_loaded', 'imsanity_init' );
14
  Description: Imsanity stops insanely huge image uploads
15
  Author: Exactly WWW
16
  Text Domain: imsanity
17
+ Domain Path: /languages
18
+ Version: 2.5.0
19
  Author URI: https://ewww.io/
20
  License: GPLv3
21
  */
24
  exit;
25
  }
26
 
27
+ define( 'IMSANITY_VERSION', '2.5.0' );
28
  define( 'IMSANITY_SCHEMA_VERSION', '1.1' );
29
 
30
  define( 'IMSANITY_DEFAULT_MAX_WIDTH', 1920 );
41
  define( 'IMSANITY_AJAX_MAX_RECORDS', 250 );
42
  }
43
 
44
+ /**
45
+ * The full path of the main plugin file.
46
+ *
47
+ * @var string IMSANITY_PLUGIN_FILE
48
+ */
49
+ define( 'IMSANITY_PLUGIN_FILE', __FILE__ );
50
+ /**
51
+ * The path of the main plugin file, relative to the plugins/ folder.
52
+ *
53
+ * @var string IMSANITY_PLUGIN_FILE_REL
54
+ */
55
+ define( 'IMSANITY_PLUGIN_FILE_REL', plugin_basename( __FILE__ ) );
56
+
57
  /**
58
  * Load translations for Imsanity.
59
  */
64
  /**
65
  * Import supporting libraries.
66
  */
67
+ require_once( plugin_dir_path( __FILE__ ) . 'libs/utils.php' );
68
+ require_once( plugin_dir_path( __FILE__ ) . 'settings.php' );
69
+ require_once( plugin_dir_path( __FILE__ ) . 'ajax.php' );
70
 
71
  /**
72
  * Inspects the request and determines where the upload came from.
119
  break;
120
  }
121
 
122
+ // NOTE: filters MUST return an array of 2 items, or the defaults will be used.
123
+ return apply_filters( 'imsanity_get_max_width_height', array( $w, $h ), $source );
124
  }
125
 
126
  /**
148
  // Make sure this is a type of image that we want to convert and that it exists.
149
  $oldpath = $params['file'];
150
 
151
+ // Let folks filter the allowed mime-types for resizing.
152
+ $allowed_types = apply_filters( 'imsanity_allowed_mimes', array( 'image/png', 'image/gif', 'image/jpeg' ), $oldpath );
153
+ if ( is_string( $allowed_types ) ) {
154
+ $allowed_types = array( $allowed_types );
155
+ } elseif ( ! is_array( $allowed_types ) ) {
156
+ $allowed_types = array();
157
+ }
158
+
159
+ if (
160
+ ( ! is_wp_error( $params ) ) &&
161
+ is_file( $oldpath ) &&
162
+ is_readable( $oldpath ) &&
163
+ is_writable( $oldpath ) &&
164
+ filesize( $oldpath ) > 0 &&
165
+ in_array( $params['type'], $allowed_types, true )
166
+ ) {
167
 
168
  // figure out where the upload is coming from.
169
  $source = imsanity_get_source();
170
 
171
+ $maxw = IMSANITY_DEFAULT_MAX_WIDTH;
172
+ $maxh = IMSANITY_DEFAULT_MAX_HEIGHT;
173
+ $max_width_height = imsanity_get_max_width_height( $source );
174
+ if ( is_array( $max_width_height ) && 2 === count( $max_width_height ) ) {
175
+ list( $maxw, $maxh ) = $max_width_height;
176
+ }
177
 
178
  list( $oldw, $oldh ) = getimagesize( $oldpath );
179
 
189
  $oldh = $old_oldw;
190
  }
191
 
192
+ if ( $maxw > 0 && $maxh > 0 && $oldw >= $maxw && $oldh >= $maxh && ( $oldh > $maxh || $oldw > $maxw ) && apply_filters( 'imsanity_crop_image', false ) ) {
193
  $neww = $maxw;
194
  $newh = $maxh;
195
  } else {
196
  list( $neww, $newh ) = wp_constrain_dimensions( $oldw, $oldh, $maxw, $maxh );
197
  }
198
 
199
+ global $ewww_preempt_editor;
200
+ if ( ! isset( $ewww_preempt_editor ) ) {
201
+ $ewww_preempt_editor = false;
 
202
  }
203
+ $original_preempt = $ewww_preempt_editor;
204
+ $ewww_preempt_editor = true;
205
+ $resizeresult = imsanity_image_resize( $oldpath, $neww, $newh, apply_filters( 'imsanity_crop_image', false ), null, null, $quality );
206
+ $ewww_preempt_editor = $original_preempt;
207
 
208
  if ( $resizeresult && ! is_wp_error( $resizeresult ) ) {
209
  $newpath = $resizeresult;
302
  return $params;
303
  }
304
 
305
+ // Add filter to hook into uploads.
306
  add_filter( 'wp_handle_upload', 'imsanity_handle_upload' );
307
+ // Run necessary actions on init (loading translations mostly).
308
  add_action( 'plugins_loaded', 'imsanity_init' );
libs/utils.php CHANGED
@@ -128,7 +128,7 @@ function imsanity_fatal( $message, $title = '', $die = false ) {
128
  * @param bool $crop Optional. Whether to crop image or resize.
129
  * @param string $suffix Optional. File suffix.
130
  * @param string $dest_path Optional. New image file path.
131
- * @param int $jpeg_quality Optional, default is 90. Image quality percentage.
132
  * @return mixed WP_Error on failure. String with new destination path.
133
  */
134
  function imsanity_image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 82 ) {
@@ -137,11 +137,12 @@ function imsanity_image_resize( $file, $max_w, $max_h, $crop = false, $suffix =
137
  if ( is_wp_error( $editor ) ) {
138
  return $editor;
139
  }
140
- $editor->set_quality( $jpeg_quality );
141
 
142
  $ftype = imsanity_quick_mimetype( $file );
143
 
144
- $orientation = imsanity_get_orientation( $file, $ftype );
 
145
  // Try to correct for auto-rotation if the info is available.
146
  switch ( $orientation ) {
147
  case 3:
@@ -162,10 +163,8 @@ function imsanity_image_resize( $file, $max_w, $max_h, $crop = false, $suffix =
162
 
163
  $dest_file = $editor->generate_filename( $suffix, $dest_path );
164
 
165
- // FIX: make sure that the destination file does not exist. this fixes
166
- // an issue during bulk resize where one of the optimized media filenames may get
167
- // used as the temporary file, which causes it to be deleted.
168
- while ( file_exists( $dest_file ) ) {
169
  $dest_file = $editor->generate_filename( 'TMP', $dest_path );
170
  }
171
 
128
  * @param bool $crop Optional. Whether to crop image or resize.
129
  * @param string $suffix Optional. File suffix.
130
  * @param string $dest_path Optional. New image file path.
131
+ * @param int $jpeg_quality Optional, default is 82. Image quality level (1-100).
132
  * @return mixed WP_Error on failure. String with new destination path.
133
  */
134
  function imsanity_image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 82 ) {
137
  if ( is_wp_error( $editor ) ) {
138
  return $editor;
139
  }
140
+ $editor->set_quality( min( 92, $jpeg_quality ) );
141
 
142
  $ftype = imsanity_quick_mimetype( $file );
143
 
144
+ // Return 1 to override auto-rotate.
145
+ $orientation = (int) apply_filters( 'imsanity_orientation', imsanity_get_orientation( $file, $ftype ) );
146
  // Try to correct for auto-rotation if the info is available.
147
  switch ( $orientation ) {
148
  case 3:
163
 
164
  $dest_file = $editor->generate_filename( $suffix, $dest_path );
165
 
166
+ // Make sure that the destination file does not exist.
167
+ if ( file_exists( $dest_file ) ) {
 
 
168
  $dest_file = $editor->generate_filename( 'TMP', $dest_path );
169
  }
170
 
readme.txt CHANGED
@@ -2,10 +2,10 @@
2
  Contributors: nosilver4u
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.9
6
- Tested up to: 5.2
7
  Requires PHP: 5.6
8
- Stable tag: 2.4.3
9
  License: GPLv3
10
 
11
  Imsanity automatically resizes huge image uploads. Are contributors uploading huge photos? Tired of manually scaling? Imsanity to the rescue!
@@ -31,6 +31,7 @@ before uploading.
31
  * Bulk-resize feature to selectively resize existing images
32
  * Allows configuration of max width/height and jpg quality
33
  * Optionally converts BMP files to JPG so image can be scaled
 
34
  * Once enabled, Imsanity requires no actions on the part of the user
35
  * Uses WordPress built-in image scaling functions
36
 
@@ -134,6 +135,16 @@ Questions may be posted on the support forum at https://wordpress.org/support/pl
134
 
135
  == Changelog ==
136
 
 
 
 
 
 
 
 
 
 
 
137
  = 2.4.3 =
138
  * changed: default size from 2048 to 1920
139
  * fixed: WP Import plugin breaks during Media imports
@@ -158,3 +169,7 @@ Questions may be posted on the support forum at https://wordpress.org/support/pl
158
 
159
  = Earlier versions =
160
  Please refer to the separate changelog.txt file.
 
 
 
 
2
  Contributors: nosilver4u
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: 5.0
6
+ Tested up to: 5.3
7
  Requires PHP: 5.6
8
+ Stable tag: 2.5.0
9
  License: GPLv3
10
 
11
  Imsanity automatically resizes huge image uploads. Are contributors uploading huge photos? Tired of manually scaling? Imsanity to the rescue!
31
  * Bulk-resize feature to selectively resize existing images
32
  * Allows configuration of max width/height and jpg quality
33
  * Optionally converts BMP files to JPG so image can be scaled
34
+ * Option to convert PNG files to JPG format for higher compression
35
  * Once enabled, Imsanity requires no actions on the part of the user
36
  * Uses WordPress built-in image scaling functions
37
 
135
 
136
  == Changelog ==
137
 
138
+ = 2.5.0 =
139
+ * added: imsanity_allowed_mimes filter to override the default list of image formats allowed
140
+ * added: imsanity_orientation filter to modify auto-rotation behavior, return 1 to bypass
141
+ * added: imsanity_get_max_width_height filter to customize max width/height
142
+ * added: define network settings as defaults for new sites in multi-site mode
143
+ * fixed: WP threshold of 2560 overrides Imsanity when using larger dimensions
144
+ * fixed: settings link on plugins page broken in some cases
145
+ * fixed: crop filter not applied if max width or height is equal to existing dimension
146
+ * fixed: invalid capabilities used for settings page - props @cfoellmann
147
+
148
  = 2.4.3 =
149
  * changed: default size from 2048 to 1920
150
  * fixed: WP Import plugin breaks during Media imports
169
 
170
  = Earlier versions =
171
  Please refer to the separate changelog.txt file.
172
+
173
+ == Credits ==
174
+
175
+ Originally written by Jason Hinkle (RIP). Maintained and developed by [Shane Bishop](https://ewww.io) with special thanks to my [Lord and Savior](https://www.iamsecond.com/).
settings.php CHANGED
@@ -14,11 +14,13 @@ if ( ! isset( $wpdb->imsanity_ms ) ) {
14
  // Register the plugin settings menu.
15
  add_action( 'admin_menu', 'imsanity_create_menu' );
16
  add_action( 'network_admin_menu', 'imsanity_register_network' );
17
- add_filter( 'plugin_action_links_imsanity/imsanity.php', 'imsanity_settings_link' );
 
18
  add_action( 'admin_enqueue_scripts', 'imsanity_queue_script' );
19
  add_action( 'admin_init', 'imsanity_register_settings' );
 
20
 
21
- register_activation_hook( 'imsanity/imsanity.php', 'imsanity_maybe_created_custom_table' );
22
 
23
  // settings cache.
24
  $_imsanity_multisite_settings = null;
@@ -28,8 +30,15 @@ $_imsanity_multisite_settings = null;
28
  * link it to the plugin settings page
29
  */
30
  function imsanity_create_menu() {
 
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
  /**
@@ -40,8 +49,16 @@ function imsanity_register_network() {
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
 
@@ -55,7 +72,11 @@ function imsanity_settings_link( $links ) {
55
  if ( ! is_array( $links ) ) {
56
  $links = array();
57
  }
58
- $settings_link = '<a href="' . get_admin_url( null, 'options-general.php?page=' . __FILE__ ) . '">' . esc_html__( 'Settings', 'imsanity' ) . '</a>';
 
 
 
 
59
  array_unshift( $links, $settings_link );
60
  return $links;
61
  }
@@ -84,7 +105,7 @@ function imsanity_queue_script( $hook ) {
84
  'none_found' => esc_html__( 'There are no images that need to be resized.', 'imsanity' ),
85
  )
86
  );
87
- add_action( 'admin_print_styles', 'imsanity_settings_css' );
88
  }
89
 
90
  /**
@@ -214,10 +235,28 @@ function imsanity_maybe_created_custom_table() {
214
  */
215
  function imsanity_network_settings() {
216
  $settings = imsanity_get_multisite_settings(); ?>
217
- <div class="wrap">
218
- <h1><?php esc_html_e( 'Imsanity Network Settings', 'imsanity' ); ?></h1>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
219
 
220
- <form method="post" action="settings.php?page=imsanity_network">
221
  <input type="hidden" name="update_imsanity_settings" value="1" />
222
  <?php wp_nonce_field( 'imsanity_network_options' ); ?>
223
  <table class="form-table">
@@ -228,6 +267,7 @@ function imsanity_network_settings() {
228
  <option value="0" <?php selected( $settings->imsanity_override_site, '0' ); ?> ><?php esc_html_e( 'Allow each site to configure Imsanity settings', 'imsanity' ); ?></option>
229
  <option value="1" <?php selected( $settings->imsanity_override_site, '1' ); ?> ><?php esc_html_e( 'Use global Imsanity settings (below) for all sites', 'imsanity' ); ?></option>
230
  </select>
 
231
  </td>
232
  </tr>
233
 
@@ -236,6 +276,7 @@ function imsanity_network_settings() {
236
  <td>
237
  <label for="imsanity_max_width"><?php esc_html_e( 'Max Width', 'imsanity' ); ?></label> <input type="number" step="1" min="0" class='small-text' name="imsanity_max_width" value="<?php echo (int) $settings->imsanity_max_width; ?>" />
238
  <label for="imsanity_max_height"><?php esc_html_e( 'Max Height', 'imsanity' ); ?></label> <input type="number" step="1" min="0" class="small-text" name="imsanity_max_height" value="<?php echo (int) $settings->imsanity_max_height; ?>" /> <?php esc_html_e( 'in pixels, enter 0 to disable', 'imsanity' ); ?>
 
239
  </td>
240
  </tr>
241
 
@@ -274,7 +315,7 @@ function imsanity_network_settings() {
274
  <tr>
275
  <th scope="row"><label for='imsanity_quality' ><?php esc_html_e( 'JPG image quality', 'imsanity' ); ?></th>
276
  <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' ); ?>
277
- <p class='description'><?php esc_html_e( 'WordPress default is 82', 'imsanity' ); ?></p></td>
278
  </tr>
279
 
280
  <tr>
@@ -287,9 +328,9 @@ function imsanity_network_settings() {
287
  <p class="submit"><input type="submit" class="button-primary" value="<?php esc_attr_e( 'Update Settings', 'imsanity' ); ?>" /></p>
288
 
289
  </form>
290
- <?php
291
 
292
- echo '</div>';
 
293
  }
294
 
295
  /**
@@ -404,15 +445,50 @@ function imsanity_get_option( $key, $ifnull ) {
404
  return $result;
405
  }
406
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
407
  /**
408
  * Register the configuration settings that the plugin will use
409
  */
410
  function imsanity_register_settings() {
411
- if ( ! function_exists( 'is_plugin_active_for_network' ) && is_multisite() ) {
412
- require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
413
- }
414
  // We only want to update if the form has been submitted.
415
- if ( isset( $_POST['update_imsanity_settings'] ) && is_multisite() && is_plugin_active_for_network( 'imsanity/imsanity.php' ) ) {
416
  imsanity_network_settings_update();
417
  }
418
  // Register our settings.
@@ -445,13 +521,42 @@ function imsanity_jpg_quality( $quality = null ) {
445
  }
446
  }
447
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
448
  /**
449
  * Helper function to render css styles for the settings forms
450
  * for both site and network settings page
451
  */
452
  function imsanity_settings_css() {
453
- echo '
454
- <style>
455
  #imsanity_header {
456
  border: solid 1px #c6c6c6;
457
  margin: 10px 0px;
@@ -461,7 +566,32 @@ function imsanity_settings_css() {
461
  #imsanity_header p {
462
  margin: .5em 0;
463
  }
464
- </style>';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
465
  }
466
 
467
  /**
@@ -473,6 +603,25 @@ function imsanity_settings_page() {
473
  ?>
474
  <div class="wrap">
475
  <h1><?php esc_html_e( 'Imsanity Settings', 'imsanity' ); ?></h1>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
476
  <?php
477
 
478
  $settings = imsanity_get_multisite_settings();
@@ -543,6 +692,7 @@ function imsanity_settings_page_form() {
543
  <td>
544
  <label for="imsanity_max_width"><?php esc_html_e( 'Max Width', 'imsanity' ); ?></label> <input type="number" step="1" min="0" class="small-text" name="imsanity_max_width" value="<?php echo (int) get_option( 'imsanity_max_width', IMSANITY_DEFAULT_MAX_WIDTH ); ?>" />
545
  <label for="imsanity_max_height"><?php esc_html_e( 'Max Height', 'imsanity' ); ?></label> <input type="number" step="1" min="0" class="small-text" name="imsanity_max_height" value="<?php echo (int) get_option( 'imsanity_max_height', IMSANITY_DEFAULT_MAX_HEIGHT ); ?>" /> <?php esc_html_e( 'in pixels, enter 0 to disable', 'imsanity' ); ?>
 
546
  </td>
547
  </tr>
548
 
@@ -566,7 +716,7 @@ function imsanity_settings_page_form() {
566
  <tr>
567
  <th scope="row"><label for='imsanity_quality' ><?php esc_html_e( 'JPG image quality', 'imsanity' ); ?></th>
568
  <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' ); ?>
569
- <p class='description'><?php esc_html_e( 'WordPress default is 82', 'imsanity' ); ?></p></td>
570
  </tr>
571
 
572
  <tr>
14
  // Register the plugin settings menu.
15
  add_action( 'admin_menu', 'imsanity_create_menu' );
16
  add_action( 'network_admin_menu', 'imsanity_register_network' );
17
+ add_filter( 'plugin_action_links_' . IMSANITY_PLUGIN_FILE_REL, 'imsanity_settings_link' );
18
+ add_filter( 'network_admin_plugin_action_links_' . IMSANITY_PLUGIN_FILE_REL, 'imsanity_settings_link' );
19
  add_action( 'admin_enqueue_scripts', 'imsanity_queue_script' );
20
  add_action( 'admin_init', 'imsanity_register_settings' );
21
+ add_filter( 'big_image_size_threshold', 'imsanity_adjust_default_threshold', 10, 3 );
22
 
23
+ register_activation_hook( IMSANITY_PLUGIN_FILE_REL, 'imsanity_maybe_created_custom_table' );
24
 
25
  // settings cache.
26
  $_imsanity_multisite_settings = null;
30
  * link it to the plugin settings page
31
  */
32
  function imsanity_create_menu() {
33
+ $permissions = apply_filters( 'imsanity_admin_permissions', 'manage_options' );
34
  // Create new menu for site configuration.
35
+ add_options_page(
36
+ esc_html__( 'Imsanity Plugin Settings', 'imsanity' ), // Page Title.
37
+ esc_html__( 'Imsanity', 'imsanity' ), // Menu Title.
38
+ $permissions, // Required permissions.
39
+ IMSANITY_PLUGIN_FILE_REL, // Slug.
40
+ 'imsanity_settings_page' // Function to call.
41
+ );
42
  }
43
 
44
  /**
49
  // Need to include the plugin library for the is_plugin_active function.
50
  require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
51
  }
52
+ if ( is_multisite() ) {
53
+ $permissions = apply_filters( 'imsanity_superadmin_permissions', 'manage_network_options' );
54
+ add_submenu_page(
55
+ 'settings.php',
56
+ esc_html__( 'Imsanity Network Settings', 'imsanity' ),
57
+ esc_html__( 'Imsanity', 'imsanity' ),
58
+ $permissions,
59
+ IMSANITY_PLUGIN_FILE_REL,
60
+ 'imsanity_network_settings'
61
+ );
62
  }
63
  }
64
 
72
  if ( ! is_array( $links ) ) {
73
  $links = array();
74
  }
75
+ if ( is_multisite() && is_network_admin() ) {
76
+ $settings_link = '<a href="' . network_admin_url( 'settings.php?page=' . IMSANITY_PLUGIN_FILE_REL ) . '">' . esc_html__( 'Settings', 'imsanity' ) . '</a>';
77
+ } else {
78
+ $settings_link = '<a href="' . admin_url( 'options-general.php?page=' . IMSANITY_PLUGIN_FILE_REL ) . '">' . esc_html__( 'Settings', 'imsanity' ) . '</a>';
79
+ }
80
  array_unshift( $links, $settings_link );
81
  return $links;
82
  }
105
  'none_found' => esc_html__( 'There are no images that need to be resized.', 'imsanity' ),
106
  )
107
  );
108
+ add_action( 'admin_print_scripts', 'imsanity_settings_css' );
109
  }
110
 
111
  /**
235
  */
236
  function imsanity_network_settings() {
237
  $settings = imsanity_get_multisite_settings(); ?>
238
+ <div class="wrap">
239
+ <h1><?php esc_html_e( 'Imsanity Network Settings', 'imsanity' ); ?></h1>
240
+
241
+ <div id="ewwwio-promo">
242
+ <p>
243
+ <?php
244
+ printf(
245
+ /* translators: %s: link to install EWWW Image Optimizer plugin */
246
+ esc_html__( 'Get comprehensive image optimization with %s', 'imsanity' ),
247
+ '<br><a href="' . admin_url( 'plugin-install.php?s=ewww+image+optimizer&tab=search&type=term' ) . '">EWWW Image Optimizer</a>'
248
+ );
249
+ ?>
250
+ <ul>
251
+ <li><?php esc_html_e( 'Full Compression', 'imsanity' ); ?></li>
252
+ <li><?php esc_html_e( 'Automatic Scaling', 'imsanity' ); ?></li>
253
+ <li><?php esc_html_e( 'Lazy Load', 'imsanity' ); ?></li>
254
+ <li><?php esc_html_e( 'WebP Conversion', 'imsanity' ); ?></li>
255
+ </ul>
256
+ </p>
257
+ </div>
258
 
259
+ <form method="post" action="">
260
  <input type="hidden" name="update_imsanity_settings" value="1" />
261
  <?php wp_nonce_field( 'imsanity_network_options' ); ?>
262
  <table class="form-table">
267
  <option value="0" <?php selected( $settings->imsanity_override_site, '0' ); ?> ><?php esc_html_e( 'Allow each site to configure Imsanity settings', 'imsanity' ); ?></option>
268
  <option value="1" <?php selected( $settings->imsanity_override_site, '1' ); ?> ><?php esc_html_e( 'Use global Imsanity settings (below) for all sites', 'imsanity' ); ?></option>
269
  </select>
270
+ <p class="description"><?php esc_html_e( 'If you allow per-site configuration, the settings below will be used as the defaults. Single-site defaults will be set the first time you visit the site admin after activating Imsanity.', 'imsanity' ); ?></p>
271
  </td>
272
  </tr>
273
 
276
  <td>
277
  <label for="imsanity_max_width"><?php esc_html_e( 'Max Width', 'imsanity' ); ?></label> <input type="number" step="1" min="0" class='small-text' name="imsanity_max_width" value="<?php echo (int) $settings->imsanity_max_width; ?>" />
278
  <label for="imsanity_max_height"><?php esc_html_e( 'Max Height', 'imsanity' ); ?></label> <input type="number" step="1" min="0" class="small-text" name="imsanity_max_height" value="<?php echo (int) $settings->imsanity_max_height; ?>" /> <?php esc_html_e( 'in pixels, enter 0 to disable', 'imsanity' ); ?>
279
+ <p class="description"><?php esc_html_e( 'These dimensions are used for Bulk Resizing also.', 'imsanity' ); ?></p>
280
  </td>
281
  </tr>
282
 
315
  <tr>
316
  <th scope="row"><label for='imsanity_quality' ><?php esc_html_e( 'JPG image quality', 'imsanity' ); ?></th>
317
  <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' ); ?>
318
+ <p class='description'><?php esc_html_e( 'Only used when resizing images, does not affect thumbnails.', 'imsanity' ); ?></p></td>
319
  </tr>
320
 
321
  <tr>
328
  <p class="submit"><input type="submit" class="button-primary" value="<?php esc_attr_e( 'Update Settings', 'imsanity' ); ?>" /></p>
329
 
330
  </form>
 
331
 
332
+ </div>
333
+ <?php
334
  }
335
 
336
  /**
445
  return $result;
446
  }
447
 
448
+ /**
449
+ * Run upgrade check for new version.
450
+ */
451
+ function imsanity_upgrade() {
452
+ if ( is_network_admin() ) {
453
+ return;
454
+ }
455
+ if ( -1 === version_compare( get_option( 'imsanity_version' ), IMSANITY_VERSION ) ) {
456
+ if ( wp_doing_ajax() ) {
457
+ return;
458
+ }
459
+ imsanity_set_defaults();
460
+ update_option( 'imsanity_version', IMSANITY_VERSION );
461
+ }
462
+ }
463
+
464
+ /**
465
+ * Set default options on multi-site.
466
+ */
467
+ function imsanity_set_defaults() {
468
+ $settings = imsanity_get_multisite_settings();
469
+ add_option( 'imsanity_max_width', $settings->imsanity_max_width, '', false );
470
+ add_option( 'imsanity_max_height', $settings->imsanity_max_height, '', false );
471
+ add_option( 'imsanity_max_width_library', $settings->imsanity_max_width_library, '', false );
472
+ add_option( 'imsanity_max_height_library', $settings->imsanity_max_height_library, '', false );
473
+ add_option( 'imsanity_max_width_other', $settings->imsanity_max_width_other, '', false );
474
+ add_option( 'imsanity_max_height_other', $settings->imsanity_max_height_other, '', false );
475
+ add_option( 'imsanity_png_to_jpg', $settings->imsanity_png_to_jpg, '', false );
476
+ add_option( 'imsanity_bmp_to_jpg', $settings->imsanity_bmp_to_jpg, '', false );
477
+ add_option( 'imsanity_quality', $settings->imsanity_quality, '', false );
478
+ add_option( 'imsanity_deep_scan', $settings->imsanity_deep_scan, '', false );
479
+ if ( ! get_option( 'imsanity_version' ) ) {
480
+ global $wpdb;
481
+ $wpdb->query( "UPDATE $wpdb->options SET autoload='no' WHERE option_name LIKE 'imsanity_%'" );
482
+ }
483
+ }
484
+
485
  /**
486
  * Register the configuration settings that the plugin will use
487
  */
488
  function imsanity_register_settings() {
489
+ imsanity_upgrade();
 
 
490
  // We only want to update if the form has been submitted.
491
+ if ( isset( $_POST['update_imsanity_settings'] ) && is_multisite() && is_network_admin() ) {
492
  imsanity_network_settings_update();
493
  }
494
  // Register our settings.
521
  }
522
  }
523
 
524
+ /**
525
+ * Check default WP threshold and adjust to comply with normal Imsanity behavior.
526
+ *
527
+ * @param int $size The default WP scaling size, or whatever has been filtered by other plugins.
528
+ * @param array $imagesize {
529
+ * Indexed array of the image width and height in pixels.
530
+ *
531
+ * @type int $0 The image width.
532
+ * @type int $1 The image height.
533
+ * }
534
+ * @param string $file Full path to the uploaded image file.
535
+ * @return int The proper size to use for scaling originals.
536
+ */
537
+ function imsanity_adjust_default_threshold( $size, $imagesize, $file ) {
538
+ if ( false !== strpos( $file, 'noresize' ) ) {
539
+ return false;
540
+ }
541
+ $max_size = max(
542
+ imsanity_get_option( 'imsanity_max_width', IMSANITY_DEFAULT_MAX_WIDTH ),
543
+ imsanity_get_option( 'imsanity_max_height', IMSANITY_DEFAULT_MAX_HEIGHT ),
544
+ imsanity_get_option( 'imsanity_max_width_library', IMSANITY_DEFAULT_MAX_WIDTH ),
545
+ imsanity_get_option( 'imsanity_max_height_library', IMSANITY_DEFAULT_MAX_HEIGHT ),
546
+ imsanity_get_option( 'imsanity_max_width_other', IMSANITY_DEFAULT_MAX_WIDTH ),
547
+ imsanity_get_option( 'imsanity_max_height_other', IMSANITY_DEFAULT_MAX_HEIGHT ),
548
+ (int) $size
549
+ );
550
+ return $max_size;
551
+ }
552
+
553
  /**
554
  * Helper function to render css styles for the settings forms
555
  * for both site and network settings page
556
  */
557
  function imsanity_settings_css() {
558
+ ?>
559
+ <style>
560
  #imsanity_header {
561
  border: solid 1px #c6c6c6;
562
  margin: 10px 0px;
566
  #imsanity_header p {
567
  margin: .5em 0;
568
  }
569
+ #ewwwio-promo {
570
+ display: none;
571
+ float: right;
572
+ }
573
+ #ewwwio-promo a, #ewwwio-promo a:visited {
574
+ color: #3eadc9;
575
+ }
576
+ #ewwwio-promo ul {
577
+ list-style: disc;
578
+ padding-left: 13px;
579
+ }
580
+ @media screen and (min-width: 850px) {
581
+ .form-table {
582
+ clear: left;
583
+ width: calc(100% - 210px);
584
+ }
585
+ #ewwwio-promo {
586
+ display: block;
587
+ border: 1px solid #7e8993;
588
+ padding: 13px;
589
+ margin: 13px;
590
+ width: 150px;
591
+ }
592
+ }
593
+ </style>
594
+ <?php
595
  }
596
 
597
  /**
603
  ?>
604
  <div class="wrap">
605
  <h1><?php esc_html_e( 'Imsanity Settings', 'imsanity' ); ?></h1>
606
+
607
+ <div id="ewwwio-promo">
608
+ <p>
609
+ <?php
610
+ printf(
611
+ /* translators: %s: link to install EWWW Image Optimizer plugin */
612
+ esc_html__( 'Get comprehensive image optimization with %s', 'imsanity' ),
613
+ '<br><a href="' . admin_url( 'plugin-install.php?s=ewww+image+optimizer&tab=search&type=term' ) . '">EWWW Image Optimizer</a>'
614
+ );
615
+ ?>
616
+ <ul>
617
+ <li><?php esc_html_e( 'Full Compression', 'imsanity' ); ?></li>
618
+ <li><?php esc_html_e( 'Automatic Scaling', 'imsanity' ); ?></li>
619
+ <li><?php esc_html_e( 'Lazy Load', 'imsanity' ); ?></li>
620
+ <li><?php esc_html_e( 'WebP Conversion', 'imsanity' ); ?></li>
621
+ </ul>
622
+ </p>
623
+ </div>
624
+
625
  <?php
626
 
627
  $settings = imsanity_get_multisite_settings();
692
  <td>
693
  <label for="imsanity_max_width"><?php esc_html_e( 'Max Width', 'imsanity' ); ?></label> <input type="number" step="1" min="0" class="small-text" name="imsanity_max_width" value="<?php echo (int) get_option( 'imsanity_max_width', IMSANITY_DEFAULT_MAX_WIDTH ); ?>" />
694
  <label for="imsanity_max_height"><?php esc_html_e( 'Max Height', 'imsanity' ); ?></label> <input type="number" step="1" min="0" class="small-text" name="imsanity_max_height" value="<?php echo (int) get_option( 'imsanity_max_height', IMSANITY_DEFAULT_MAX_HEIGHT ); ?>" /> <?php esc_html_e( 'in pixels, enter 0 to disable', 'imsanity' ); ?>
695
+ <p class="description"><?php esc_html_e( 'These dimensions are used for Bulk Resizing also.', 'imsanity' ); ?></p>
696
  </td>
697
  </tr>
698
 
716
  <tr>
717
  <th scope="row"><label for='imsanity_quality' ><?php esc_html_e( 'JPG image quality', 'imsanity' ); ?></th>
718
  <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' ); ?>
719
+ <p class='description'><?php esc_html_e( 'Only used when resizing images, does not affect thumbnails.', 'imsanity' ); ?></p></td>
720
  </tr>
721
 
722
  <tr>