PluginProbe
Image Optimization – Compress Images and Convert to WebP or AVIF / 1.6.5
Image Optimization – Compress Images and Convert to WebP or AVIF v1.6.5
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 1.6.6 All 32 releases
image-optimization / modules / settings / module.php

module.php in Image Optimization – Compress Images and Convert to WebP or AVIF 1.6.5, at modules/settings/module.php

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