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