PluginProbe
Imsanity / 2.7.0
Imsanity v2.7.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.7.0, at settings.php

840 lines 34.9 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 && 'upload.php' !== $hook ) {
92 return;
93 }
94 if ( ! empty( $_REQUEST['imsanity_reset'] ) && ! empty( $_REQUEST['imsanity_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_REQUEST['imsanity_wpnonce'] ), 'imsanity-bulk-reset' ) ) {
95 update_option( 'imsanity_resume_id', 0, false );
96 }
97 $resume_id = (int) get_option( 'imsanity_resume_id' );
98 $loading_image = plugins_url( '/images/ajax-loader.gif', __FILE__ );
99 // Register the scripts that are used by the bulk resizer.
100 wp_enqueue_script( 'imsanity_script', plugins_url( '/scripts/imsanity.js', __FILE__ ), array( 'jquery' ), IMSANITY_VERSION );
101 wp_localize_script(
102 'imsanity_script',
103 'imsanity_vars',
104 array(
105 '_wpnonce' => wp_create_nonce( 'imsanity-bulk' ),
106 'resizing_complete' => esc_html__( 'Resizing Complete', 'imsanity' ) . ' - <a target="_blank" href="https://wordpress.org/support/plugin/imsanity/reviews/#new-post">' . esc_html__( 'Leave a Review', 'imsanity' ) . '</a>',
107 'resize_selected' => esc_html__( 'Resize Selected Images', 'imsanity' ),
108 'resizing' => '<p>' . esc_html__( 'Please wait...', 'imsanity' ) . "&nbsp;<img src='$loading_image' /></p>",
109 'removal_failed' => esc_html__( 'Removal Failed', 'imsanity' ),
110 'removal_succeeded' => esc_html__( 'Removal Complete', 'imsanity' ),
111 'operation_stopped' => esc_html__( 'Resizing stopped, reload page to resume.', 'imsanity' ),
112 'image' => esc_html__( 'Image', 'imsanity' ),
113 'invalid_response' => esc_html__( 'Received an invalid response, please check for errors in the Developer Tools console of your browser.', 'imsanity' ),
114 'none_found' => esc_html__( 'There are no images that need to be resized.', 'imsanity' ),
115 'resume_id' => $resume_id,
116 )
117 );
118 add_action( 'admin_notices', 'imsanity_missing_gd_admin_notice' );
119 add_action( 'network_admin_notices', 'imsanity_missing_gd_admin_notice' );
120 add_action( 'admin_print_scripts', 'imsanity_settings_css' );
121 }
122
123 /**
124 * Return true if the multi-site settings table exists
125 *
126 * @return bool True if the Imsanity table exists.
127 */
128 function imsanity_multisite_table_exists() {
129 global $wpdb;
130 return $wpdb->get_var( "SHOW TABLES LIKE '$wpdb->imsanity_ms'" ) === $wpdb->imsanity_ms;
131 }
132
133 /**
134 * Checks the schema version for the Imsanity table.
135 *
136 * @return string The version identifier for the schema.
137 */
138 function imsanity_multisite_table_schema_version() {
139 // If the table doesn't exist then there is no schema to report.
140 if ( ! imsanity_multisite_table_exists() ) {
141 return '0';
142 }
143
144 global $wpdb;
145 $version = $wpdb->get_var( "SELECT data FROM $wpdb->imsanity_ms WHERE setting = 'schema'" );
146
147 if ( ! $version ) {
148 $version = '1.0'; // This is a legacy version 1.0 installation.
149 }
150
151 return $version;
152 }
153
154 /**
155 * Returns the default network settings in the case where they are not
156 * defined in the database, or multi-site is not enabled.
157 *
158 * @return stdClass
159 */
160 function imsanity_get_default_multisite_settings() {
161 $data = new stdClass();
162
163 $data->imsanity_override_site = false;
164 $data->imsanity_max_height = IMSANITY_DEFAULT_MAX_HEIGHT;
165 $data->imsanity_max_width = IMSANITY_DEFAULT_MAX_WIDTH;
166 $data->imsanity_max_height_library = IMSANITY_DEFAULT_MAX_HEIGHT;
167 $data->imsanity_max_width_library = IMSANITY_DEFAULT_MAX_WIDTH;
168 $data->imsanity_max_height_other = IMSANITY_DEFAULT_MAX_HEIGHT;
169 $data->imsanity_max_width_other = IMSANITY_DEFAULT_MAX_WIDTH;
170 $data->imsanity_bmp_to_jpg = IMSANITY_DEFAULT_BMP_TO_JPG;
171 $data->imsanity_png_to_jpg = IMSANITY_DEFAULT_PNG_TO_JPG;
172 $data->imsanity_quality = IMSANITY_DEFAULT_QUALITY;
173 $data->imsanity_delete_originals = false;
174 return $data;
175 }
176
177
178 /**
179 * On activation create the multisite database table if necessary. this is
180 * called when the plugin is activated as well as when it is automatically
181 * updated.
182 */
183 function imsanity_maybe_created_custom_table() {
184 // If not a multi-site no need to do any custom table lookups.
185 if ( ! function_exists( 'is_multisite' ) || ( ! is_multisite() ) ) {
186 return;
187 }
188
189 global $wpdb;
190
191 $schema = imsanity_multisite_table_schema_version();
192
193 if ( '0' === $schema ) {
194 // This is an initial database setup.
195 $sql = 'CREATE TABLE IF NOT EXISTS ' . $wpdb->imsanity_ms . ' (
196 setting varchar(55),
197 data text NOT NULL,
198 PRIMARY KEY (setting)
199 );';
200
201 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
202 dbDelta( $sql );
203
204 // Add the rows to the database.
205 $data = imsanity_get_default_multisite_settings();
206 $wpdb->insert(
207 $wpdb->imsanity_ms,
208 array(
209 'setting' => 'multisite',
210 'data' => maybe_serialize( $data ),
211 )
212 );
213 $wpdb->insert(
214 $wpdb->imsanity_ms,
215 array(
216 'setting' => 'schema',
217 'data' => IMSANITY_SCHEMA_VERSION,
218 )
219 );
220 }
221
222 if ( IMSANITY_SCHEMA_VERSION !== $schema ) {
223 // This is a schema update. for the moment there is only one schema update available, from 1.0 to 1.1.
224 if ( '1.0' === $schema ) {
225 // Update from version 1.0 to 1.1.
226 $wpdb->insert(
227 $wpdb->imsanity_ms,
228 array(
229 'setting' => 'schema',
230 'data' => IMSANITY_SCHEMA_VERSION,
231 )
232 );
233 $wpdb->query( "ALTER TABLE $wpdb->imsanity_ms CHANGE COLUMN data data TEXT NOT NULL;" );
234 } else {
235 // @todo we don't have this yet
236 $wpdb->update(
237 $wpdb->imsanity_ms,
238 array( 'data' => IMSANITY_SCHEMA_VERSION ),
239 array( 'setting' => 'schema' )
240 );
241 }
242 }
243 }
244
245 /**
246 * Display the form for the multi-site settings page.
247 */
248 function imsanity_network_settings() {
249 $settings = imsanity_get_multisite_settings(); ?>
250 <div class="wrap">
251 <h1><?php esc_html_e( 'Imsanity Network Settings', 'imsanity' ); ?></h1>
252
253 <div id="ewwwio-promo">
254 <p>
255 <?php
256 printf(
257 /* translators: %s: link to install EWWW Image Optimizer plugin */
258 esc_html__( 'Get comprehensive image optimization with %s', 'imsanity' ),
259 '<br><a href="' . admin_url( 'plugin-install.php?s=ewww+image+optimizer&tab=search&type=term' ) . '">EWWW Image Optimizer</a>'
260 );
261 ?>
262 <ul>
263 <li><?php esc_html_e( 'Full Compression', 'imsanity' ); ?></li>
264 <li><?php esc_html_e( 'Automatic Scaling', 'imsanity' ); ?></li>
265 <li><?php esc_html_e( 'Lazy Load', 'imsanity' ); ?></li>
266 <li><?php esc_html_e( 'WebP Conversion', 'imsanity' ); ?></li>
267 </ul>
268 </p>
269 </div>
270
271 <form method="post" action="">
272 <input type="hidden" name="update_imsanity_settings" value="1" />
273 <?php wp_nonce_field( 'imsanity_network_options' ); ?>
274 <table class="form-table">
275 <tr>
276 <th scope="row"><label for="imsanity_override_site"><?php esc_html_e( 'Global Settings Override', 'imsanity' ); ?></label></th>
277 <td>
278 <select name="imsanity_override_site">
279 <option value="0" <?php selected( $settings->imsanity_override_site, '0' ); ?> ><?php esc_html_e( 'Allow each site to configure Imsanity settings', 'imsanity' ); ?></option>
280 <option value="1" <?php selected( $settings->imsanity_override_site, '1' ); ?> ><?php esc_html_e( 'Use global Imsanity settings (below) for all sites', 'imsanity' ); ?></option>
281 </select>
282 <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>
283 </td>
284 </tr>
285 <tr>
286 <th scope="row"><?php esc_html_e( 'Images uploaded within a Page/Post', 'imsanity' ); ?></th>
287 <td>
288 <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; ?>" />
289 <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' ); ?>
290 <p class="description"><?php esc_html_e( 'These dimensions are used for Bulk Resizing also.', 'imsanity' ); ?></p>
291 </td>
292 </tr>
293 <tr>
294 <th scope="row"><?php esc_html_e( 'Images uploaded directly to the Media Library', 'imsanity' ); ?></th>
295 <td>
296 <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; ?>" />
297 <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' ); ?>
298 </td>
299 </tr>
300 <tr>
301 <th scope="row"><?php esc_html_e( 'Images uploaded elsewhere (Theme headers, backgrounds, logos, etc)', 'imsanity' ); ?></th>
302 <td>
303 <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; ?>" />
304 <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' ); ?>
305 </td>
306 </tr>
307 <tr>
308 <th scope="row">
309 <label for='imsanity_quality'><?php esc_html_e( 'JPG image quality', 'imsanity' ); ?>
310 </th>
311 <td>
312 <input type='text' id='imsanity_quality' name='imsanity_quality' class='small-text' value='<?php echo (int) $settings->imsanity_quality; ?>' />
313 <?php esc_html_e( 'Valid values are 1-100.', 'imsanity' ); ?>
314 <p class='description'><?php esc_html_e( 'Only used when resizing images, does not affect thumbnails.', 'imsanity' ); ?></p>
315 </td>
316 </tr>
317 <tr>
318 <th scope="row">
319 <label for"imsanity_bmp_to_jpg"><?php esc_html_e( 'Convert BMP to JPG', 'imsanity' ); ?></label>
320 </th>
321 <td>
322 <input type="checkbox" id="imsanity_bmp_to_jpg" name="imsanity_bmp_to_jpg" value="true" <?php checked( $settings->imsanity_bmp_to_jpg ); ?> />
323 <?php esc_html_e( 'Only applies to new image uploads, existing BMP images cannot be converted or resized.', 'imsanity' ); ?>
324 </td>
325 </tr>
326 <tr>
327 <th scope="row">
328 <label for="imsanity_png_to_jpg"><?php esc_html_e( 'Convert PNG to JPG', 'imsanity' ); ?></label>
329 </th>
330 <td>
331 <input type="checkbox" id="imsanity_png_to_jpg" name="imsanity_png_to_jpg" value="true" <?php checked( $settings->imsanity_png_to_jpg ); ?> />
332 <?php
333 printf(
334 /* translators: %s: link to install EWWW Image Optimizer plugin */
335 esc_html__( 'Only applies to new image uploads, existing images may be converted with %s.', 'imsanity' ),
336 '<a href="' . admin_url( 'plugin-install.php?s=ewww+image+optimizer&tab=search&type=term' ) . '">EWWW Image Optimizer</a>'
337 );
338 ?>
339 </td>
340 </tr>
341 <tr>
342 <th scope="row">
343 <label for="imsanity_delete_originals"><?php esc_html_e( 'Delete Originals', 'imsanity' ); ?></label>
344 </th>
345 <td>
346 <input type="checkbox" id="imsanity_delete_originals" name="imsanity_delete_originals" value="true" <?php checked( $settings->imsanity_delete_originals ); ?> />
347 <?php esc_html_e( 'Remove the large pre-scaled originals that WordPress retains for thumbnail generation.', 'imsanity' ); ?>
348 </td>
349 </tr>
350 </table>
351
352 <p class="submit"><input type="submit" class="button-primary" value="<?php esc_attr_e( 'Update Settings', 'imsanity' ); ?>" /></p>
353
354 </form>
355
356 </div>
357 <?php
358 }
359
360 /**
361 * Process the form, update the network settings
362 * and clear the cached settings
363 */
364 function imsanity_network_settings_update() {
365 if ( ! current_user_can( 'manage_options' ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'imsanity_network_options' ) ) {
366 return;
367 }
368 global $wpdb;
369 global $_imsanity_multisite_settings;
370
371 // ensure that the custom table is created when the user updates network settings
372 // this is not ideal but it's better than checking for this table existance
373 // on every page load.
374 imsanity_maybe_created_custom_table();
375
376 $data = new stdClass();
377
378 $data->imsanity_override_site = (bool) $_POST['imsanity_override_site'];
379 $data->imsanity_max_height = sanitize_text_field( $_POST['imsanity_max_height'] );
380 $data->imsanity_max_width = sanitize_text_field( $_POST['imsanity_max_width'] );
381 $data->imsanity_max_height_library = sanitize_text_field( $_POST['imsanity_max_height_library'] );
382 $data->imsanity_max_width_library = sanitize_text_field( $_POST['imsanity_max_width_library'] );
383 $data->imsanity_max_height_other = sanitize_text_field( $_POST['imsanity_max_height_other'] );
384 $data->imsanity_max_width_other = sanitize_text_field( $_POST['imsanity_max_width_other'] );
385 $data->imsanity_bmp_to_jpg = ! empty( $_POST['imsanity_bmp_to_jpg'] );
386 $data->imsanity_png_to_jpg = ! empty( $_POST['imsanity_png_to_jpg'] );
387 $data->imsanity_quality = imsanity_jpg_quality( $_POST['imsanity_quality'] );
388 $data->imsanity_delete_originals = ! empty( $_POST['imsanity_delete_originals'] );
389
390 $success = $wpdb->update(
391 $wpdb->imsanity_ms,
392 array( 'data' => maybe_serialize( $data ) ),
393 array( 'setting' => 'multisite' )
394 );
395
396 // Clear the cache.
397 $_imsanity_multisite_settings = null;
398 add_action( 'network_admin_notices', 'imsanity_network_settings_saved' );
399 }
400
401 /**
402 * Display a message to inform the user the multi-site setting have been saved.
403 */
404 function imsanity_network_settings_saved() {
405 echo "<div id='imsanity-network-settings-saved' class='updated fade'><p><strong>" . esc_html__( 'Imsanity network settings saved.', 'imsanity' ) . '</strong></p></div>';
406 }
407
408 /**
409 * Return the multi-site settings as a standard class. If the settings are not
410 * defined in the database or multi-site is not enabled then the default settings
411 * are returned. This is cached so it only loads once per page load, unless
412 * imsanity_network_settings_update is called.
413 *
414 * @return stdClass
415 */
416 function imsanity_get_multisite_settings() {
417 global $_imsanity_multisite_settings;
418 $result = null;
419
420 if ( ! $_imsanity_multisite_settings ) {
421 if ( function_exists( 'is_multisite' ) && is_multisite() ) {
422 global $wpdb;
423 $result = $wpdb->get_var( "SELECT data FROM $wpdb->imsanity_ms WHERE setting = 'multisite'" );
424 }
425
426 // if there's no results, return the defaults instead.
427 $_imsanity_multisite_settings = $result
428 ? unserialize( $result )
429 : imsanity_get_default_multisite_settings();
430
431 // this is for backwards compatibility.
432 if ( ! isset( $_imsanity_multisite_settings->imsanity_max_height_library ) ) {
433 $_imsanity_multisite_settings->imsanity_max_height_library = $_imsanity_multisite_settings->imsanity_max_height;
434 $_imsanity_multisite_settings->imsanity_max_width_library = $_imsanity_multisite_settings->imsanity_max_width;
435 $_imsanity_multisite_settings->imsanity_max_height_other = $_imsanity_multisite_settings->imsanity_max_height;
436 $_imsanity_multisite_settings->imsanity_max_width_other = $_imsanity_multisite_settings->imsanity_max_width;
437 }
438 $_imsanity_multisite_settings->imsanity_override_site = ! empty( $_imsanity_multisite_settings->imsanity_override_site ) ? '1' : '0';
439 $_imsanity_multisite_settings->imsanity_bmp_to_jpg = ! empty( $_imsanity_multisite_settings->imsanity_bmp_to_jpg ) ? true : false;
440 $_imsanity_multisite_settings->imsanity_png_to_jpg = ! empty( $_imsanity_multisite_settings->imsanity_png_to_jpg ) ? true : false;
441 if ( ! property_exists( $_imsanity_multisite_settings, 'imsanity_delete_originals' ) ) {
442 $_imsanity_multisite_settings->imsanity_delete_originals = false;
443 }
444 }
445 return $_imsanity_multisite_settings;
446 }
447
448 /**
449 * Gets the option setting for the given key, first checking to see if it has been
450 * set globally for multi-site. Otherwise checking the site options.
451 *
452 * @param string $key The name of the option to retrieve.
453 * @param string $ifnull Value to use if the requested option returns null.
454 */
455 function imsanity_get_option( $key, $ifnull ) {
456 $result = null;
457
458 $settings = imsanity_get_multisite_settings();
459
460 if ( $settings->imsanity_override_site ) {
461 $result = $settings->$key;
462 if ( is_null( $result ) ) {
463 $result = $ifnull;
464 }
465 } else {
466 $result = get_option( $key, $ifnull );
467 }
468
469 return $result;
470 }
471
472 /**
473 * Run upgrade check for new version.
474 */
475 function imsanity_upgrade() {
476 if ( is_network_admin() ) {
477 return;
478 }
479 if ( -1 === version_compare( get_option( 'imsanity_version' ), IMSANITY_VERSION ) ) {
480 if ( wp_doing_ajax() ) {
481 return;
482 }
483 imsanity_set_defaults();
484 update_option( 'imsanity_version', IMSANITY_VERSION );
485 }
486 }
487
488 /**
489 * Set default options on multi-site.
490 */
491 function imsanity_set_defaults() {
492 $settings = imsanity_get_multisite_settings();
493 add_option( 'imsanity_max_width', $settings->imsanity_max_width, '', false );
494 add_option( 'imsanity_max_height', $settings->imsanity_max_height, '', false );
495 add_option( 'imsanity_max_width_library', $settings->imsanity_max_width_library, '', false );
496 add_option( 'imsanity_max_height_library', $settings->imsanity_max_height_library, '', false );
497 add_option( 'imsanity_max_width_other', $settings->imsanity_max_width_other, '', false );
498 add_option( 'imsanity_max_height_other', $settings->imsanity_max_height_other, '', false );
499 add_option( 'imsanity_bmp_to_jpg', $settings->imsanity_bmp_to_jpg, '', false );
500 add_option( 'imsanity_png_to_jpg', $settings->imsanity_png_to_jpg, '', false );
501 add_option( 'imsanity_quality', $settings->imsanity_quality, '', false );
502 add_option( 'imsanity_delete_originals', $settings->imsanity_delete_originals, '', false );
503 if ( ! get_option( 'imsanity_version' ) ) {
504 global $wpdb;
505 $wpdb->query( "UPDATE $wpdb->options SET autoload='no' WHERE option_name LIKE 'imsanity_%'" );
506 }
507 }
508
509 /**
510 * Register the configuration settings that the plugin will use
511 */
512 function imsanity_register_settings() {
513 imsanity_upgrade();
514 // We only want to update if the form has been submitted.
515 if ( isset( $_POST['update_imsanity_settings'] ) && is_multisite() && is_network_admin() ) {
516 imsanity_network_settings_update();
517 }
518 // Register our settings.
519 register_setting( 'imsanity-settings-group', 'imsanity_max_height', 'intval' );
520 register_setting( 'imsanity-settings-group', 'imsanity_max_width', 'intval' );
521 register_setting( 'imsanity-settings-group', 'imsanity_max_height_library', 'intval' );
522 register_setting( 'imsanity-settings-group', 'imsanity_max_width_library', 'intval' );
523 register_setting( 'imsanity-settings-group', 'imsanity_max_height_other', 'intval' );
524 register_setting( 'imsanity-settings-group', 'imsanity_max_width_other', 'intval' );
525 register_setting( 'imsanity-settings-group', 'imsanity_bmp_to_jpg', 'boolval' );
526 register_setting( 'imsanity-settings-group', 'imsanity_png_to_jpg', 'boolval' );
527 register_setting( 'imsanity-settings-group', 'imsanity_quality', 'imsanity_jpg_quality' );
528 register_setting( 'imsanity-settings-group', 'imsanity_delete_originals', 'boolval' );
529 }
530
531 /**
532 * Validate and return the JPG quality setting.
533 *
534 * @param int $quality The JPG quality currently set.
535 * @return int The (potentially) adjusted quality level.
536 */
537 function imsanity_jpg_quality( $quality = null ) {
538 if ( is_null( $quality ) ) {
539 $quality = get_option( 'imsanity_quality' );
540 }
541 if ( preg_match( '/^(100|[1-9][0-9]?)$/', $quality ) ) {
542 return (int) $quality;
543 } else {
544 return IMSANITY_DEFAULT_QUALITY;
545 }
546 }
547
548 /**
549 * Check default WP threshold and adjust to comply with normal Imsanity behavior.
550 *
551 * @param int $size The default WP scaling size, or whatever has been filtered by other plugins.
552 * @param array $imagesize {
553 * Indexed array of the image width and height in pixels.
554 *
555 * @type int $0 The image width.
556 * @type int $1 The image height.
557 * }
558 * @param string $file Full path to the uploaded image file.
559 * @return int The proper size to use for scaling originals.
560 */
561 function imsanity_adjust_default_threshold( $size, $imagesize, $file ) {
562 if ( false !== strpos( $file, 'noresize' ) ) {
563 return false;
564 }
565 $max_size = max(
566 imsanity_get_option( 'imsanity_max_width', IMSANITY_DEFAULT_MAX_WIDTH ),
567 imsanity_get_option( 'imsanity_max_height', IMSANITY_DEFAULT_MAX_HEIGHT ),
568 imsanity_get_option( 'imsanity_max_width_library', IMSANITY_DEFAULT_MAX_WIDTH ),
569 imsanity_get_option( 'imsanity_max_height_library', IMSANITY_DEFAULT_MAX_HEIGHT ),
570 imsanity_get_option( 'imsanity_max_width_other', IMSANITY_DEFAULT_MAX_WIDTH ),
571 imsanity_get_option( 'imsanity_max_height_other', IMSANITY_DEFAULT_MAX_HEIGHT ),
572 (int) $size
573 );
574 return $max_size;
575 }
576
577 /**
578 * Helper function to render css styles for the settings forms
579 * for both site and network settings page
580 */
581 function imsanity_settings_css() {
582 ?>
583 <style>
584 #imsanity_header {
585 border: solid 1px #c6c6c6;
586 margin: 10px 0px;
587 padding: 0px 10px;
588 background-color: #e1e1e1;
589 }
590 #imsanity_header p {
591 margin: .5em 0;
592 }
593 #ewwwio-promo {
594 display: none;
595 float: right;
596 }
597 #ewwwio-promo a, #ewwwio-promo a:visited {
598 color: #3eadc9;
599 }
600 #ewwwio-promo ul {
601 list-style: disc;
602 padding-left: 13px;
603 }
604 @media screen and (min-width: 850px) {
605 .form-table {
606 clear: left;
607 width: calc(100% - 210px);
608 }
609 #ewwwio-promo {
610 display: block;
611 border: 1px solid #7e8993;
612 padding: 13px;
613 margin: 13px;
614 width: 150px;
615 }
616 }
617 </style>
618 <?php
619 }
620
621 /**
622 * Render the settings page by writing directly to stdout. if multi-site is enabled
623 * and imsanity_override_site is true, then display a notice message that settings
624 * are not editable instead of the settings form
625 */
626 function imsanity_settings_page() {
627 ?>
628 <div class="wrap">
629 <h1><?php esc_html_e( 'Imsanity Settings', 'imsanity' ); ?></h1>
630 <p>
631 <a target="_blank" href="https://wordpress.org/plugins/imsanity/#faq-header"><?php esc_html_e( 'FAQ', 'imsanity' ); ?></a> |
632 <a target="_blank" href="https://wordpress.org/support/plugin/imsanity/"><?php esc_html_e( 'Support', 'imsanity' ); ?></a> |
633 <a target="_blank" href="https://wordpress.org/support/plugin/imsanity/reviews/#new-post"><?php esc_html_e( 'Leave a Review', 'imsanity' ); ?></a>
634 </p>
635
636 <div id="ewwwio-promo">
637 <p>
638 <?php
639 printf(
640 /* translators: %s: link to install EWWW Image Optimizer plugin */
641 esc_html__( 'Get comprehensive image optimization with %s', 'imsanity' ),
642 '<br><a href="' . admin_url( 'plugin-install.php?s=ewww+image+optimizer&tab=search&type=term' ) . '">EWWW Image Optimizer</a>'
643 );
644 ?>
645 <ul>
646 <li><?php esc_html_e( 'Full Compression', 'imsanity' ); ?></li>
647 <li><?php esc_html_e( 'Automatic Scaling', 'imsanity' ); ?></li>
648 <li><?php esc_html_e( 'Lazy Load', 'imsanity' ); ?></li>
649 <li><?php esc_html_e( 'WebP Conversion', 'imsanity' ); ?></li>
650 </ul>
651 </p>
652 </div>
653
654 <?php
655
656 $settings = imsanity_get_multisite_settings();
657
658 if ( $settings->imsanity_override_site ) {
659 imsanity_settings_page_notice();
660 } else {
661 imsanity_settings_page_form();
662 }
663
664 ?>
665
666 <h2 style="margin-top: 0px;"><?php esc_html_e( 'Bulk Resize Images', 'imsanity' ); ?></h2>
667
668 <div id="imsanity_header">
669 <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 (below).', 'imsanity' ); ?></p>
670 <p>
671 <?php
672 printf(
673 /* translators: 1: List View in the Media Library 2: the WP-CLI command */
674 esc_html__( 'You may also use %1$s to selectively resize images or WP-CLI to resize your images in bulk: %2$s', 'imsanity' ),
675 '<a href="' . esc_url( admin_url( 'upload.php?mode=list' ) ) . '">' . esc_html__( 'List View in the Media Library', 'imsanity' ) . '</a>',
676 '<code>wp help imsanity resize</code>'
677 );
678 ?>
679 </p>
680 </div>
681
682 <div style="border: solid 1px #ff6666; background-color: #ffbbbb; padding: 0 10px;margin-bottom:1em;">
683 <h4><?php esc_html_e( 'WARNING: Bulk Resize will alter your original images and cannot be undone!', 'imsanity' ); ?></h4>
684 <p>
685 <?php esc_html_e( 'It is HIGHLY recommended that you backup your images before proceeding.', 'imsanity' ); ?><br>
686 <?php
687 printf(
688 /* translators: %s: List View in the Media Library */
689 esc_html__( 'You may also resize 1 or 2 images using %s to verify that everything is working properly before processing your entire library.', 'imsanity' ),
690 '<a href="' . esc_url( admin_url( 'upload.php?mode=list' ) ) . '">' . esc_html__( 'List View in the Media Library', 'imsanity' ) . '</a>'
691 );
692 ?>
693 </p>
694 </div>
695
696 <?php
697 $button_text = __( 'Start Resizing All Images', 'imsanity' );
698 if ( get_option( 'imsanity_resume_id' ) ) {
699 $button_text = __( 'Continue Resizing', 'imsanity' );
700 }
701 ?>
702
703 <p class="submit" id="imsanity-examine-button">
704 <button class="button-primary" onclick="imsanity_load_images();"><?php echo esc_html( $button_text ); ?></button>
705 </p>
706 <form id="imsanity-bulk-stop" style="display:none;margin:1em 0 1em;" method="post" action="">
707 <button type="submit" class="button-secondary action"><?php esc_html_e( 'Stop Resizing', 'imsanity' ); ?></button>
708 </form>
709 <?php if ( get_option( 'imsanity_resume_id' ) ) : ?>
710 <p class="imsanity-bulk-text" style="margin-top:1em;"><?php esc_html_e( 'Would you like to start back at the beginning?', 'imsanity' ); ?></p>
711 <form class="imsanity-bulk-form" method="post" action="">
712 <?php wp_nonce_field( 'imsanity-bulk-reset', 'imsanity_wpnonce' ); ?>
713 <input type="hidden" name="imsanity_reset" value="1">
714 <button id="imsanity-bulk-reset" type="submit" class="button-secondary action"><?php esc_html_e( 'Start Over', 'imsanity' ); ?></button>
715 </form>
716 <?php endif; ?>
717 <div id="imsanity_loading" style="display: none;margin:1em 0 1em;"><img src="<?php echo plugins_url( 'images/ajax-loader.gif', __FILE__ ); ?>" style="margin-bottom: .25em; vertical-align:middle;" />
718 <?php esc_html_e( 'Searching for images. This may take a moment.', 'imsanity' ); ?>
719 </div>
720 <div id="resize_results" style="display: none; border: solid 2px #666666; padding: 10px; height: 400px; overflow: auto;">
721 <div id="bulk-resize-beginning"><?php esc_html_e( 'Resizing...', 'imsanity' ); ?> <img src="<?php echo plugins_url( 'images/ajax-loader.gif', __FILE__ ); ?>" style="margin-bottom: .25em; vertical-align:middle;" /></div>
722 </div>
723
724 <?php
725
726 echo '</div>';
727 }
728
729 /**
730 * Multi-user config file exists so display a notice
731 */
732 function imsanity_settings_page_notice() {
733 ?>
734 <div class="updated settings-error">
735 <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>
736 </div>
737 <?php
738 }
739
740 /**
741 * Check to see if GD is missing, and alert the user.
742 */
743 function imsanity_missing_gd_admin_notice() {
744 if ( imsanity_gd_support() ) {
745 return;
746 }
747 echo "<div id='imsanity-missing-gd' class='notice notice-warning'><p>" . esc_html__( 'The GD extension is not enabled in PHP, Imsanity may not function correctly. Enable GD or contact your web host for assistance.', 'imsanity' ) . '</p></div>';
748 }
749
750 /**
751 * Render the site settings form. This is processed by
752 * WordPress built-in options persistance mechanism
753 */
754 function imsanity_settings_page_form() {
755 ?>
756 <form method="post" action="options.php">
757 <?php settings_fields( 'imsanity-settings-group' ); ?>
758 <table class="form-table">
759
760 <tr>
761 <th scope="row"><?php esc_html_e( 'Images uploaded within a Page/Post', 'imsanity' ); ?></th>
762 <td>
763 <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 ); ?>" />
764 <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' ); ?>
765 <p class="description"><?php esc_html_e( 'These dimensions are used for Bulk Resizing also.', 'imsanity' ); ?></p>
766 </td>
767 </tr>
768
769 <tr>
770 <th scope="row"><?php esc_html_e( 'Images uploaded directly to the Media Library', 'imsanity' ); ?></th>
771 <td>
772 <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 ); ?>" />
773 <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' ); ?>
774 </td>
775 </tr>
776
777 <tr>
778 <th scope="row"><?php esc_html_e( 'Images uploaded elsewhere (Theme headers, backgrounds, logos, etc)', 'imsanity' ); ?></th>
779 <td>
780 <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 ); ?>" />
781 <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' ); ?>
782 </td>
783 </tr>
784
785
786 <tr>
787 <th scope="row">
788 <label for='imsanity_quality' ><?php esc_html_e( 'JPG image quality', 'imsanity' ); ?>
789 </th>
790 <td>
791 <input type='text' id='imsanity_quality' name='imsanity_quality' class='small-text' value='<?php echo imsanity_jpg_quality(); ?>' />
792 <?php esc_html_e( 'Valid values are 1-100.', 'imsanity' ); ?>
793 <p class='description'><?php esc_html_e( 'Only used when resizing images, does not affect thumbnails.', 'imsanity' ); ?></p>
794 </td>
795 </tr>
796
797 <tr>
798 <th scope="row">
799 <label for="imsanity_bmp_to_jpg"><?php esc_html_e( 'Convert BMP To JPG', 'imsanity' ); ?></label>
800 </th>
801 <td>
802 <input type="checkbox" id="imsanity_bmp_to_jpg" name="imsanity_bmp_to_jpg" value="true" <?php checked( (bool) get_option( 'imsanity_bmp_to_jpg', IMSANITY_DEFAULT_BMP_TO_JPG ) ); ?> />
803 <?php esc_html_e( 'Only applies to new image uploads, existing BMP images cannot be converted or resized.', 'imsanity' ); ?>
804 </td>
805 </tr>
806 <tr>
807 <th scope="row">
808 <label for="imsanity_png_to_jpg"><?php esc_html_e( 'Convert PNG To JPG', 'imsanity' ); ?></label>
809 </th>
810 <td>
811 <input type="checkbox" id="imsanity_png_to_jpg" name="imsanity_png_to_jpg" value="true" <?php checked( (bool) get_option( 'imsanity_png_to_jpg', IMSANITY_DEFAULT_PNG_TO_JPG ) ); ?> />
812 <?php
813 printf(
814 /* translators: %s: link to install EWWW Image Optimizer plugin */
815 esc_html__( 'Only applies to new image uploads, existing images may be converted with %s.', 'imsanity' ),
816 '<a href="' . admin_url( 'plugin-install.php?s=ewww+image+optimizer&tab=search&type=term' ) . '">EWWW Image Optimizer</a>'
817 );
818 ?>
819 </td>
820 </tr>
821 <tr>
822 <th scope="row">
823 <label for="imsanity_delete_originals"><?php esc_html_e( 'Delete Originals', 'imsanity' ); ?></label>
824 </th>
825 <td>
826 <input type="checkbox" id="imsanity_delete_originals" name="imsanity_delete_originals" value="true" <?php checked( get_option( 'imsanity_delete_originals' ) ); ?> />
827 <?php esc_html_e( 'Remove the large pre-scaled originals that WordPress retains for thumbnail generation.', 'imsanity' ); ?>
828 </td>
829 </tr>
830 </table>
831
832 <p class="submit"><input type="submit" class="button-primary" value="<?php esc_attr_e( 'Save Changes', 'imsanity' ); ?>" /></p>
833
834 </form>
835 <?php
836
837 }
838
839 ?>
840