PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.5.2
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.5.2
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
image-optimization / trunk / modules / settings / module.php

module.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.5.2, at trunk/modules/settings/module.php

137 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ImageOptimization\Modules\Settings;
4
5 use ImageOptimization\Classes\Image\Image_Conversion_Option;
6 use ImageOptimization\Classes\Module_Base;
7 use ImageOptimization\Modules\Settings\Classes\Settings;
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit; // Exit if accessed directly
11 }
12
13 class Module extends Module_Base {
14 const SETTING_PREFIX = 'image_optimizer_';
15 const SETTING_GROUP = 'image_optimizer_settings';
16 const SETTING_BASE_SLUG = 'image-optimization-settings';
17 const SETTING_CAPABILITY = 'manage_options';
18
19 public function get_name(): string {
20 return 'settings';
21 }
22
23 public static function component_list() : array {
24 return [
25 'Settings_Pointer',
26 ];
27 }
28
29 public static function get_options() : array {
30 return [
31 'compression_level' => [ 'default' => 'lossy' ],
32 'optimize_on_upload' => [
33 'type' => 'boolean',
34 'default' => true,
35 ],
36 'resize_larger_images' => [
37 'type' => 'boolean',
38 'default' => true,
39 ],
40 'resize_larger_images_size' => [
41 'type' => 'integer',
42 'default' => 1920,
43 ],
44 'exif_metadata' => [
45 'type' => 'boolean',
46 'default' => true,
47 ],
48 'original_images' => [
49 'type' => 'boolean',
50 'default' => true,
51 ],
52 'convert_to_format' => [
53 'type' => 'string',
54 'default' => Image_Conversion_Option::WEBP,
55 ],
56 'custom_sizes' => [
57 'type' => 'string',
58 'default' => 'all',
59 ],
60 ];
61 }
62
63 public function register_options() {
64 $options = $this->get_options();
65
66 foreach ( $options as $key => &$args ) {
67 $args['type'] = $args['type'] ?? 'string';
68 $args['show_in_rest'] = $args['show_in_rest'] ?? true;
69 $args['default'] = $args['default'] ?? '';
70
71 register_setting(
72 self::SETTING_GROUP,
73 self::SETTING_PREFIX . $key,
74 $args
75 );
76
77 // Set defaults
78 add_option( self::SETTING_PREFIX . $key, $args['default'] );
79 }
80 }
81
82 public function render_app() {
83 ?>
84 <!-- The hack required to wrap WP notifications -->
85 <div class="wrap">
86 <h1 style="display: none;" role="presentation"></h1>
87 </div>
88
89 <div id="image-optimization-app"></div>
90 <?php
91 }
92
93 public function register_page() {
94 add_media_page(
95 __( 'Image Optimizer', 'image-optimization' ),
96 __( 'Image Optimizer', 'image-optimization' ),
97 self::SETTING_CAPABILITY,
98 self::SETTING_BASE_SLUG,
99 [ $this, 'render_app' ],
100 6
101 );
102 }
103
104 /**
105 * The handler converts an old CONVERT_TO_WEBP option to the new CONVERT_TO_FORMAT option.
106 * TODO: [Stability] Remove this fallback after all users updated
107 *
108 * @return void
109 */
110 public function maybe_migrate_legacy_conversion_option() {
111 $legacy_convert_to_webp = get_option( Settings::CONVERT_TO_WEBP_OPTION_NAME, null );
112
113 if ( is_null( $legacy_convert_to_webp ) ) {
114 return;
115 }
116
117 if ( '1' === $legacy_convert_to_webp ) {
118 update_option( Settings::CONVERT_TO_FORMAT_OPTION_NAME, Image_Conversion_Option::WEBP, false );
119 }
120
121 if ( '0' === $legacy_convert_to_webp ) {
122 update_option( Settings::CONVERT_TO_FORMAT_OPTION_NAME, Image_Conversion_Option::ORIGINAL, false );
123 }
124
125 delete_option( Settings::CONVERT_TO_WEBP_OPTION_NAME );
126 }
127
128 public function __construct() {
129 $this->register_components();
130
131 add_action( 'admin_init', [ $this, 'register_options' ] );
132 add_action( 'rest_api_init', [ $this, 'register_options' ] );
133 add_action( 'admin_init', [ $this, 'maybe_migrate_legacy_conversion_option' ] );
134 add_action( 'admin_menu', [ $this, 'register_page' ] );
135 }
136 }
137