PluginProbe
Imsanity / 2.6.0
Imsanity v2.6.0
2.9.4 2.9.3 2.9.2 trunk 2.4.3 2.5.0 2.6.0 2.7.0 2.7.2 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.9.0 2.9.1
imsanity / settings.php

settings.php in Imsanity 2.6.0, at settings.php

753 lines 31.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Imsanity settings and admin UI.
4 *
5 * @package Imsanity
6 */
7
8 // Setup custom $wpdb attribute for our image-tracking table.
9 global $wpdb;
10 if ( ! isset( $wpdb->imsanity_ms ) ) {
11 $wpdb->imsanity_ms = $wpdb->get_blog_prefix( 0 ) . 'imsanity';
12 }
13
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;
27
28 /**
29 * Create the settings menu item in the WordPress admin navigation and
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 /**
45 * Register the network settings page
46 */
47 function imsanity_register_network() {
48 if ( ! function_exists( 'is_plugin_active_for_network' ) && is_multisite() ) {
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
65 /**
66 * Settings link that appears on the plugins overview page
67 *
68 * @param array $links The plugin action links.
69 * @return array The action links, with a settings link pre-pended.
70 */
71 function imsanity_settings_link( $links ) {
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 }
83
84 /**
85 * Queues up the AJAX script and any localized JS vars we need.
86 *
87 * @param string $hook The hook name for the current page.
88 */
89 function imsanity_queue_script( $hook ) {
90 // Make sure we are being called from the settings page.
91 if ( strpos( $hook, 'settings_page_imsanity' ) !== 0 ) {
92 return;
93 }
94 // Register the scripts that are used by the bulk resizer.
95 wp_enqueue_script( 'imsanity_script', plugins_url( '/scripts/imsanity.js', __FILE__ ), array( 'jquery' ), IMSANITY_VERSION );
96 wp_localize_script(
97 'imsanity_script',
98 'imsanity_vars',
99 array(
100 '_wpnonce' => wp_create_nonce( 'imsanity-bulk' ),
101 'resizing_complete' => esc_html__( 'Resizing Complete', 'imsanity' ),
102 'resize_selected' => esc_html__( 'Resize Selected Images', 'imsanity' ),
103 'image' => esc_html__( 'Image', 'imsanity' ),
104 'invalid_response' => esc_html__( 'Received an invalid response, please check for errors in the Developer Tools console of your browser.', 'imsanity' ),
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 /**
112 * Return true if the multi-site settings table exists
113 *
114 * @return bool True if the Imsanity table exists.
115 */
116 function imsanity_multisite_table_exists() {
117 global $wpdb;
118 return $wpdb->get_var( "SHOW TABLES LIKE '$wpdb->imsanity_ms'" ) === $wpdb->imsanity_ms;
119 }
120
121 /**
122 * Checks the schema version for the Imsanity table.
123 *
124 * @return string The version identifier for the schema.
125 */
126 function imsanity_multisite_table_schema_version() {
127 // If the table doesn't exist then there is no schema to report.
128 if ( ! imsanity_multisite_table_exists() ) {
129 return '0';
130 }
131
132 global $wpdb;
133 $version = $wpdb->get_var( "SELECT data FROM $wpdb->imsanity_ms WHERE setting = 'schema'" );
134
135 if ( ! $version ) {
136 $version = '1.0'; // This is a legacy version 1.0 installation.
137 }
138
139 return $version;
140 }
141
142 /**
143 * Returns the default network settings in the case where they are not
144 * defined in the database, or multi-site is not enabled.
145 *
146 * @return stdClass
147 */
148 function imsanity_get_default_multisite_settings() {
149 $data = new stdClass();
150
151 $data->imsanity_override_site = false;
152 $data->imsanity_max_height = IMSANITY_DEFAULT_MAX_HEIGHT;
153 $data->imsanity_max_width = IMSANITY_DEFAULT_MAX_WIDTH;
154 $data->imsanity_max_height_library = IMSANITY_DEFAULT_MAX_HEIGHT;
155 $data->imsanity_max_width_library = IMSANITY_DEFAULT_MAX_WIDTH;
156 $data->imsanity_max_height_other = IMSANITY_DEFAULT_MAX_HEIGHT;
157 $data->imsanity_max_width_other = IMSANITY_DEFAULT_MAX_WIDTH;
158 $data->imsanity_bmp_to_jpg = IMSANITY_DEFAULT_BMP_TO_JPG;
159 $data->imsanity_png_to_jpg = IMSANITY_DEFAULT_PNG_TO_JPG;
160 $data->imsanity_deep_scan = false;
161 $data->imsanity_quality = IMSANITY_DEFAULT_QUALITY;
162 return $data;
163 }
164
165
166 /**
167 * On activation create the multisite database table if necessary. this is
168 * called when the plugin is activated as well as when it is automatically
169 * updated.
170 */
171 function imsanity_maybe_created_custom_table() {
172 // If not a multi-site no need to do any custom table lookups.
173 if ( ! function_exists( 'is_multisite' ) || ( ! is_multisite() ) ) {
174 return;
175 }
176
177 global $wpdb;
178
179 $schema = imsanity_multisite_table_schema_version();
180
181 if ( '0' === $schema ) {
182 // This is an initial database setup.
183 $sql = 'CREATE TABLE IF NOT EXISTS ' . $wpdb->imsanity_ms . ' (
184 setting varchar(55),
185 data text NOT NULL,
186 PRIMARY KEY (setting)
187 );';
188
189 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
190 dbDelta( $sql );
191
192 // Add the rows to the database.
193 $data = imsanity_get_default_multisite_settings();
194 $wpdb->insert(
195 $wpdb->imsanity_ms,
196 array(
197 'setting' => 'multisite',
198 'data' => maybe_serialize( $data ),
199 )
200 );
201 $wpdb->insert(
202 $wpdb->imsanity_ms,
203 array(
204 'setting' => 'schema',
205 'data' => IMSANITY_SCHEMA_VERSION,
206 )
207 );
208 }
209
210 if ( IMSANITY_SCHEMA_VERSION !== $schema ) {
211 // This is a schema update. for the moment there is only one schema update available, from 1.0 to 1.1.
212 if ( '1.0' === $schema ) {
213 // Update from version 1.0 to 1.1.
214 $wpdb->insert(
215 $wpdb->imsanity_ms,
216 array(
217 'setting' => 'schema',
218 'data' => IMSANITY_SCHEMA_VERSION,
219 )
220 );
221 $wpdb->query( "ALTER TABLE $wpdb->imsanity_ms CHANGE COLUMN data data TEXT NOT NULL;" );
222 } else {
223 // @todo we don't have this yet
224 $wpdb->update(
225 $wpdb->imsanity_ms,
226 array( 'data' => IMSANITY_SCHEMA_VERSION ),
227 array( 'setting' => 'schema' )
228 );
229 }
230 }
231 }
232
233 /**
234 * Display the form for the multi-site settings page.
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">
263 <tr>
264 <th scope="row"><label for="imsanity_override_site"><?php esc_html_e( 'Global Settings Override', 'imsanity' ); ?></label></th>
265 <td>
266 <select name="imsanity_override_site">
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
274 <tr>
275 <th scope="row"><?php esc_html_e( 'Images uploaded within a Page/Post', 'imsanity' ); ?></th>
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
283 <tr>
284 <th scope="row"><?php esc_html_e( 'Images uploaded directly to the Media Library', 'imsanity' ); ?></th>
285 <td>
286 <label for="imsanity_max_width_library"><?php esc_html_e( 'Max Width', 'imsanity' ); ?></label> <input type="number" step="1" min="0" class='small-text' name="imsanity_max_width_library" value="<?php echo (int) $settings->imsanity_max_width_library; ?>" />
287 <label for="imsanity_max_height_library"><?php esc_html_e( 'Max Height', 'imsanity' ); ?></label> <input type="number" step="1" min="0" class="small-text" name="imsanity_max_height_library" value="<?php echo (int) $settings->imsanity_max_height_library; ?>" /> <?php esc_html_e( 'in pixels, enter 0 to disable', 'imsanity' ); ?>
288 </td>
289 </tr>
290
291 <tr>
292 <th scope="row"><?php esc_html_e( 'Images uploaded elsewhere (Theme headers, backgrounds, logos, etc)', 'imsanity' ); ?></th>
293 <td>
294 <label for="imsanity_max_width_other"><?php esc_html_e( 'Max Width', 'imsanity' ); ?></label> <input type="number" step="1" min="0" class='small-text' name="imsanity_max_width_other" value="<?php echo (int) $settings->imsanity_max_width_other; ?>" />
295 <label for="imsanity_max_height_other"><?php esc_html_e( 'Max Height', 'imsanity' ); ?></label> <input type="number" step="1" min="0" class="small-text" name="imsanity_max_height_other" value="<?php echo (int) $settings->imsanity_max_height_other; ?>" /> <?php esc_html_e( 'in pixels, enter 0 to disable', 'imsanity' ); ?>
296 </td>
297 </tr>
298
299 <tr>
300 <th scope="row"><label for"imsanity_bmp_to_jpg"><?php esc_html_e( 'Convert BMP to JPG', 'imsanity' ); ?></label></th>
301 <td><select name="imsanity_bmp_to_jpg">
302 <option value="1" <?php selected( $settings->imsanity_bmp_to_jpg, '1' ); ?> ><?php esc_html_e( 'Yes', 'imsanity' ); ?></option>
303 <option value="0" <?php selected( $settings->imsanity_bmp_to_jpg, '0' ); ?> ><?php esc_html_e( 'No', 'imsanity' ); ?></option>
304 </select></td>
305 </tr>
306
307 <tr>
308 <th scope="row"><label for="imsanity_png_to_jpg"><?php esc_html_e( 'Convert PNG to JPG', 'imsanity' ); ?></label></th>
309 <td><select name="imsanity_png_to_jpg">
310 <option value="1" <?php selected( $settings->imsanity_png_to_jpg, '1' ); ?> ><?php esc_html_e( 'Yes', 'imsanity' ); ?></option>
311 <option value="0" <?php selected( $settings->imsanity_png_to_jpg, '0' ); ?> ><?php esc_html_e( 'No', 'imsanity' ); ?></option>
312 </select></td>
313 </tr>
314
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>
322 <th scope="row"><label for="imsanity_deep_scan"><?php esc_html_e( 'Deep Scan', 'imsanity' ); ?></label></th>
323 <td><input type="checkbox" id="imsanity_deep_scan" name="imsanity_deep_scan" value="true"<?php echo ( $settings->imsanity_deep_scan ) ? " checked='true'" : ''; ?> /><?php esc_html_e( 'If searching repeatedly returns the same images, deep scanning will check the actual image dimensions instead of relying on metadata from the database.', 'imsanity' ); ?></td>
324 </tr>
325
326 </table>
327
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 /**
337 * Process the form, update the network settings
338 * and clear the cached settings
339 */
340 function imsanity_network_settings_update() {
341 if ( ! current_user_can( 'manage_options' ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'imsanity_network_options' ) ) {
342 return;
343 }
344 global $wpdb;
345 global $_imsanity_multisite_settings;
346
347 // ensure that the custom table is created when the user updates network settings
348 // this is not ideal but it's better than checking for this table existance
349 // on every page load.
350 imsanity_maybe_created_custom_table();
351
352 $data = new stdClass();
353
354 $data->imsanity_override_site = (bool) $_POST['imsanity_override_site'];
355 $data->imsanity_max_height = sanitize_text_field( $_POST['imsanity_max_height'] );
356 $data->imsanity_max_width = sanitize_text_field( $_POST['imsanity_max_width'] );
357 $data->imsanity_max_height_library = sanitize_text_field( $_POST['imsanity_max_height_library'] );
358 $data->imsanity_max_width_library = sanitize_text_field( $_POST['imsanity_max_width_library'] );
359 $data->imsanity_max_height_other = sanitize_text_field( $_POST['imsanity_max_height_other'] );
360 $data->imsanity_max_width_other = sanitize_text_field( $_POST['imsanity_max_width_other'] );
361 $data->imsanity_bmp_to_jpg = (bool) $_POST['imsanity_bmp_to_jpg'];
362 $data->imsanity_png_to_jpg = (bool) $_POST['imsanity_png_to_jpg'];
363 $data->imsanity_quality = imsanity_jpg_quality( $_POST['imsanity_quality'] );
364 $data->imsanity_deep_scan = empty( $_POST['imsanity_deep_scan'] ) ? 0 : 1;
365
366 $success = $wpdb->update(
367 $wpdb->imsanity_ms,
368 array( 'data' => maybe_serialize( $data ) ),
369 array( 'setting' => 'multisite' )
370 );
371
372 // Clear the cache.
373 $_imsanity_multisite_settings = null;
374 add_action( 'network_admin_notices', 'imsanity_network_settings_saved' );
375 }
376
377 /**
378 * Display a message to inform the user the multi-site setting have been saved.
379 */
380 function imsanity_network_settings_saved() {
381 echo "<div id='imsanity-network-settings-saved' class='updated fade'><p><strong>" . esc_html__( 'Imsanity network settings saved.', 'imsanity' ) . '</strong></p></div>';
382 }
383
384 /**
385 * Return the multi-site settings as a standard class. If the settings are not
386 * defined in the database or multi-site is not enabled then the default settings
387 * are returned. This is cached so it only loads once per page load, unless
388 * imsanity_network_settings_update is called.
389 *
390 * @return stdClass
391 */
392 function imsanity_get_multisite_settings() {
393 global $_imsanity_multisite_settings;
394 $result = null;
395
396 if ( ! $_imsanity_multisite_settings ) {
397 if ( function_exists( 'is_multisite' ) && is_multisite() ) {
398 global $wpdb;
399 $result = $wpdb->get_var( "SELECT data FROM $wpdb->imsanity_ms WHERE setting = 'multisite'" );
400 }
401
402 // if there's no results, return the defaults instead.
403 $_imsanity_multisite_settings = $result
404 ? unserialize( $result )
405 : imsanity_get_default_multisite_settings();
406
407 // this is for backwards compatibility.
408 if ( ! isset( $_imsanity_multisite_settings->imsanity_max_height_library ) ) {
409 $_imsanity_multisite_settings->imsanity_max_height_library = $_imsanity_multisite_settings->imsanity_max_height;
410 $_imsanity_multisite_settings->imsanity_max_width_library = $_imsanity_multisite_settings->imsanity_max_width;
411 $_imsanity_multisite_settings->imsanity_max_height_other = $_imsanity_multisite_settings->imsanity_max_height;
412 $_imsanity_multisite_settings->imsanity_max_width_other = $_imsanity_multisite_settings->imsanity_max_width;
413 }
414 $_imsanity_multisite_settings->imsanity_override_site = ! empty( $_imsanity_multisite_settings->imsanity_override_site ) ? '1' : '0';
415 $_imsanity_multisite_settings->imsanity_bmp_to_jpg = ! empty( $_imsanity_multisite_settings->imsanity_bmp_to_jpg ) ? '1' : '0';
416 $_imsanity_multisite_settings->imsanity_png_to_jpg = ! empty( $_imsanity_multisite_settings->imsanity_png_to_jpg ) ? '1' : '0';
417 if ( ! property_exists( $_imsanity_multisite_settings, 'imsanity_deep_scan' ) ) {
418 $_imsanity_multisite_settings->imsanity_deep_scan = false;
419 }
420 }
421 return $_imsanity_multisite_settings;
422 }
423
424 /**
425 * Gets the option setting for the given key, first checking to see if it has been
426 * set globally for multi-site. Otherwise checking the site options.
427 *
428 * @param string $key The name of the option to retrieve.
429 * @param string $ifnull Value to use if the requested option returns null.
430 */
431 function imsanity_get_option( $key, $ifnull ) {
432 $result = null;
433
434 $settings = imsanity_get_multisite_settings();
435
436 if ( $settings->imsanity_override_site ) {
437 $result = $settings->$key;
438 if ( is_null( $result ) ) {
439 $result = $ifnull;
440 }
441 } else {
442 $result = get_option( $key, $ifnull );
443 }
444
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.
495 register_setting( 'imsanity-settings-group', 'imsanity_max_height' );
496 register_setting( 'imsanity-settings-group', 'imsanity_max_width' );
497 register_setting( 'imsanity-settings-group', 'imsanity_max_height_library' );
498 register_setting( 'imsanity-settings-group', 'imsanity_max_width_library' );
499 register_setting( 'imsanity-settings-group', 'imsanity_max_height_other' );
500 register_setting( 'imsanity-settings-group', 'imsanity_max_width_other' );
501 register_setting( 'imsanity-settings-group', 'imsanity_bmp_to_jpg' );
502 register_setting( 'imsanity-settings-group', 'imsanity_png_to_jpg' );
503 register_setting( 'imsanity-settings-group', 'imsanity_quality', 'imsanity_jpg_quality' );
504 register_setting( 'imsanity-settings-group', 'imsanity_deep_scan' );
505 }
506
507 /**
508 * Validate and return the JPG quality setting.
509 *
510 * @param int $quality The JPG quality currently set.
511 * @return int The (potentially) adjusted quality level.
512 */
513 function imsanity_jpg_quality( $quality = null ) {
514 if ( is_null( $quality ) ) {
515 $quality = get_option( 'imsanity_quality' );
516 }
517 if ( preg_match( '/^(100|[1-9][0-9]?)$/', $quality ) ) {
518 return (int) $quality;
519 } else {
520 return IMSANITY_DEFAULT_QUALITY;
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;
563 padding: 0px 10px;
564 background-color: #e1e1e1;
565 }
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 /**
598 * Render the settings page by writing directly to stdout. if multi-site is enabled
599 * and imsanity_override_site is true, then display a notice message that settings
600 * are not editable instead of the settings form
601 */
602 function imsanity_settings_page() {
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();
628
629 if ( $settings->imsanity_override_site ) {
630 imsanity_settings_page_notice();
631 } else {
632 imsanity_settings_page_form();
633 }
634
635 ?>
636
637 <h2 style="margin-top: 0px;"><?php esc_html_e( 'Bulk Resize Images', 'imsanity' ); ?></h2>
638
639 <div id="imsanity_header">
640 <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>
641 <?php /* translators: %s: the WP-CLI command to run */ ?>
642 <p><?php printf( esc_html__( 'You may also use WP-CLI to resize your images: %s', 'imsanity' ), '<code>wp help imsanity resize</code>' ); ?></p>
643 <?php /* translators: %d: the number of images */ ?>
644 <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>
645 </div>
646
647 <div style="border: solid 1px #ff6666; background-color: #ffbbbb; padding: 0 10px;">
648 <h4><?php esc_html_e( 'WARNING: Bulk Resize will alter your original images and cannot be undone!', 'imsanity' ); ?></h4>
649 <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>
650 <?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>
651 </div>
652
653 <p class="submit" id="imsanity-examine-button">
654 <button class="button-primary" onclick="imsanity_load_images('imsanity_image_list');"><?php esc_html_e( 'Search Images...', 'imsanity' ); ?></button>
655 </p>
656 <div id='imsanity_image_list'>
657 <div id="imsanity_target" style="display: none; border: solid 2px #666666; padding: 10px; height: 0px; overflow: auto;">
658 <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;" />
659 <?php esc_html_e( 'Scanning existing images. This may take a moment.', 'imsanity' ); ?>
660 </div>
661 </div>
662 </div>
663
664 <?php
665
666 echo '</div>';
667
668 }
669
670 /**
671 * Multi-user config file exists so display a notice
672 */
673 function imsanity_settings_page_notice() {
674 ?>
675 <div class="updated settings-error">
676 <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>
677 </div>
678 <?php
679 }
680
681 /**
682 * Render the site settings form. This is processed by
683 * WordPress built-in options persistance mechanism
684 */
685 function imsanity_settings_page_form() {
686 ?>
687 <form method="post" action="options.php">
688 <?php settings_fields( 'imsanity-settings-group' ); ?>
689 <table class="form-table">
690
691 <tr>
692 <th scope="row"><?php esc_html_e( 'Images uploaded within a Page/Post', 'imsanity' ); ?></th>
693 <td>
694 <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 ); ?>" />
695 <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' ); ?>
696 <p class="description"><?php esc_html_e( 'These dimensions are used for Bulk Resizing also.', 'imsanity' ); ?></p>
697 </td>
698 </tr>
699
700 <tr>
701 <th scope="row"><?php esc_html_e( 'Images uploaded directly to the Media Library', 'imsanity' ); ?></th>
702 <td>
703 <label for="imsanity_max_width_library"><?php esc_html_e( 'Max Width', 'imsanity' ); ?></label> <input type="number" step="1" min="0" class="small-text" name="imsanity_max_width_library" value="<?php echo (int) get_option( 'imsanity_max_width_library', IMSANITY_DEFAULT_MAX_WIDTH ); ?>" />
704 <label for="imsanity_max_height_library"><?php esc_html_e( 'Max Height', 'imsanity' ); ?></label> <input type="number" step="1" min="0" class="small-text" name="imsanity_max_height_library" value="<?php echo (int) get_option( 'imsanity_max_height_library', IMSANITY_DEFAULT_MAX_HEIGHT ); ?>" /> <?php esc_html_e( 'in pixels, enter 0 to disable', 'imsanity' ); ?>
705 </td>
706 </tr>
707
708 <tr>
709 <th scope="row"><?php esc_html_e( 'Images uploaded elsewhere (Theme headers, backgrounds, logos, etc)', 'imsanity' ); ?></th>
710 <td>
711 <label for="imsanity_max_width_other"><?php esc_html_e( 'Max Width', 'imsanity' ); ?></label> <input type="number" step="1" min="0" class="small-text" name="imsanity_max_width_other" value="<?php echo (int) get_option( 'imsanity_max_width_other', IMSANITY_DEFAULT_MAX_WIDTH ); ?>" />
712 <label for="imsanity_max_height_other"><?php esc_html_e( 'Max Height', 'imsanity' ); ?></label> <input type="number" step="1" min="0" class="small-text" name="imsanity_max_height_other" value="<?php echo (int) get_option( 'imsanity_max_height_other', IMSANITY_DEFAULT_MAX_HEIGHT ); ?>" /> <?php esc_html_e( 'in pixels, enter 0 to disable', 'imsanity' ); ?>
713 </td>
714 </tr>
715
716
717 <tr>
718 <th scope="row"><label for='imsanity_quality' ><?php esc_html_e( 'JPG image quality', 'imsanity' ); ?></th>
719 <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' ); ?>
720 <p class='description'><?php esc_html_e( 'Only used when resizing images, does not affect thumbnails.', 'imsanity' ); ?></p></td>
721 </tr>
722
723 <tr>
724 <th scope="row"><label for="imsanity_bmp_to_jpg"><?php esc_html_e( 'Convert BMP To JPG', 'imsanity' ); ?></label></th>
725 <td><select name="imsanity_bmp_to_jpg">
726 <option <?php selected( get_option( 'imsanity_bmp_to_jpg', IMSANITY_DEFAULT_BMP_TO_JPG ), '1' ); ?> value="1"><?php esc_html_e( 'Yes', 'imsanity' ); ?></option>
727 <option <?php selected( get_option( 'imsanity_bmp_to_jpg', IMSANITY_DEFAULT_BMP_TO_JPG ), '0' ); ?> value="0"><?php esc_html_e( 'No', 'imsanity' ); ?></option>
728 </select></td>
729 </tr>
730
731 <tr>
732 <th scope="row"><label for="imsanity_png_to_jpg"><?php esc_html_e( 'Convert PNG To JPG', 'imsanity' ); ?></label></th>
733 <td><select name="imsanity_png_to_jpg">
734 <option <?php selected( get_option( 'imsanity_png_to_jpg', IMSANITY_DEFAULT_PNG_TO_JPG ), '1' ); ?> value="1"><?php esc_html_e( 'Yes', 'imsanity' ); ?></option>
735 <option <?php selected( get_option( 'imsanity_png_to_jpg', IMSANITY_DEFAULT_PNG_TO_JPG ), '0' ); ?> value="0"><?php esc_html_e( 'No', 'imsanity' ); ?></option>
736 </select></td>
737 </tr>
738
739 <tr>
740 <th scope="row"><label for="imsanity_deep_scan"><?php esc_html_e( 'Deep Scan', 'imsanity' ); ?></label></th>
741 <td><input type="checkbox" id="imsanity_deep_scan" name="imsanity_deep_scan" value="true"<?php echo ( get_option( 'imsanity_deep_scan' ) ) ? " checked='true'" : ''; ?> /><?php esc_html_e( 'If searching repeatedly returns the same images, deep scanning will check the actual image dimensions instead of relying on metadata from the database.', 'imsanity' ); ?></td>
742 </tr>
743 </table>
744
745 <p class="submit"><input type="submit" class="button-primary" value="<?php esc_attr_e( 'Save Changes', 'imsanity' ); ?>" /></p>
746
747 </form>
748 <?php
749
750 }
751
752 ?>
753