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

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