PluginProbe
Image Optimization – Compress Images and Convert to WebP or AVIF / 1.6.6
Image Optimization – Compress Images and Convert to WebP or AVIF v1.6.6
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.6, at modules/settings/module.php

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