PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.7.5
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.7.5
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 / modules / settings / module.php

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

232 lines 6.3 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\Sale_Banner,
9 Banners\Elementor_Birthday_Banner,
10 Classes\Settings,
11 };
12 use ImageOptimization\Modules\Stats\Classes\Optimization_Stats;
13 use ImageOptimization\Classes\Client\Client;
14 use ImageOptimization\Modules\ConnectManager\Components\Connect as Connect_Manager_Connect;
15 use ImageOptimization\Modules\Connect\Classes\Config;
16
17 if ( ! defined( 'ABSPATH' ) ) {
18 exit; // Exit if accessed directly
19 }
20
21 class Module extends Module_Base {
22 const SETTING_PREFIX = 'image_optimizer_';
23 const SETTING_GROUP = 'image_optimizer_settings';
24 const SETTING_BASE_SLUG = 'image-optimization-settings';
25 const SETTING_CAPABILITY = 'manage_options';
26
27 public function get_name(): string {
28 return 'settings';
29 }
30
31 public static function component_list(): array {
32 return [
33 'Settings_Pointer',
34 ];
35 }
36
37 public static function get_options(): array {
38 return [
39 'compression_level' => [ 'default' => 'lossy' ],
40 'optimize_on_upload' => [
41 'type' => 'boolean',
42 'default' => true,
43 ],
44 'resize_larger_images' => [
45 'type' => 'boolean',
46 'default' => true,
47 ],
48 'resize_larger_images_size' => [
49 'type' => 'integer',
50 'default' => 1920,
51 ],
52 'exif_metadata' => [
53 'type' => 'boolean',
54 'default' => true,
55 ],
56 'original_images' => [
57 'type' => 'boolean',
58 'default' => true,
59 ],
60 'convert_to_format' => [
61 'type' => 'string',
62 'default' => Image_Conversion_Option::WEBP,
63 ],
64 'custom_sizes' => [
65 'type' => 'string',
66 'default' => 'all',
67 ],
68 'help_videos' => [
69 'type' => 'object',
70 'show_in_rest' => [
71 'schema' => [
72 'type' => 'object',
73 'additionalProperties' => true,
74 ],
75 ],
76 ],
77 ];
78 }
79
80 public function register_options() {
81 $options = $this->get_options();
82
83 foreach ( $options as $key => &$args ) {
84 $args['type'] = $args['type'] ?? 'string';
85 $args['show_in_rest'] = $args['show_in_rest'] ?? true;
86 $args['default'] = $args['default'] ?? '';
87
88 register_setting(
89 self::SETTING_GROUP,
90 self::SETTING_PREFIX . $key,
91 $args
92 );
93
94 // Set defaults
95 add_option( self::SETTING_PREFIX . $key, $args['default'] );
96 }
97 }
98
99 public function render_app() {
100 ?>
101 <?php Sale_Banner::get_banner( 'https://go.elementor.com/IO-BF-sale' ); ?>
102 <?php Elementor_Birthday_Banner::get_banner( 'https://go.elementor.com/IO-10th-bd-sale' ); ?>
103
104 <!-- The hack required to wrap WP notifications -->
105 <div class="wrap">
106 <h1 style="display: none;" role="presentation"></h1>
107 </div>
108
109 <div id="image-optimization-app"></div>
110 <?php
111 }
112
113 public function register_page() {
114 add_submenu_page(
115 'elementor-home',
116 __( 'Image Optimization', 'image-optimization' ),
117 __( 'Image Optimization', 'image-optimization' ),
118 self::SETTING_CAPABILITY,
119 self::SETTING_BASE_SLUG,
120 [ $this, 'render_app' ],
121 50
122 );
123
124 $this->add_menu_item_class( 'elementor-home', self::SETTING_BASE_SLUG, 'image-optimizer-menu' );
125 }
126
127 private function add_menu_item_class( string $parent_slug, string $menu_slug, string $class ) {
128 global $submenu;
129
130 if ( ! isset( $submenu[ $parent_slug ] ) ) {
131 return;
132 }
133
134 foreach ( $submenu[ $parent_slug ] as &$item ) {
135 if ( $item[2] === $menu_slug ) {
136 $item[4] = isset( $item[4] ) ? $item[4] . ' ' . $class : $class;
137 break;
138 }
139 }
140 }
141
142 /**
143 * The handler converts an old CONVERT_TO_WEBP option to the new CONVERT_TO_FORMAT option.
144 * TODO: [Stability] Remove this fallback after all users updated
145 *
146 * @return void
147 */
148 public function maybe_migrate_legacy_conversion_option() {
149 $legacy_convert_to_webp = get_option( Settings::CONVERT_TO_WEBP_OPTION_NAME, null );
150
151 if ( is_null( $legacy_convert_to_webp ) ) {
152 return;
153 }
154
155 if ( '1' === $legacy_convert_to_webp ) {
156 update_option( Settings::CONVERT_TO_FORMAT_OPTION_NAME, Image_Conversion_Option::WEBP, false );
157 }
158
159 if ( '0' === $legacy_convert_to_webp ) {
160 update_option( Settings::CONVERT_TO_FORMAT_OPTION_NAME, Image_Conversion_Option::ORIGINAL, false );
161 }
162
163 delete_option( Settings::CONVERT_TO_WEBP_OPTION_NAME );
164 }
165
166 /**
167 * The handler triggers stats recalculation on custom sizes update.
168 *
169 * @param $result
170 * @param $name
171 *
172 * @return void
173 */
174 public function recalculate_stats_on_custom_sizes_update( $result, $name ) {
175 if ( Settings::CUSTOM_SIZES_OPTION_NAME === $name ) {
176 Optimization_Stats::get_image_stats( null, true );
177 }
178 }
179
180 public function cleanup_data() {
181 delete_transient( Client::SITE_INFO_TRANSIENT );
182 delete_transient( Connect_Manager_Connect::STATUS_CHECK_TRANSIENT );
183 }
184
185 /**
186 * Register or update site data for One connect
187 * @throws Exception
188 */
189 public function on_migration_run() {
190 $old_options = [
191 'image_optimizer_client_id',
192 'image_optimizer_client_secret',
193 'image_optimizer_home_url',
194 'image_optimizer_access_token',
195 'image_optimizer_token_id',
196 'image_optimizer_refresh_token',
197 'image_optimizer_user_access_token',
198 'image_optimizer_owner_user_id',
199 'image_optimizer_subscription_id',
200 Settings::SUBSCRIPTION_ID,
201 ];
202
203 $this->cleanup_data();
204
205 foreach ( $old_options as $option ) {
206 delete_option( $option );
207 }
208 }
209
210 public function __construct() {
211 $this->register_components();
212
213 add_action( 'admin_init', [ $this, 'register_options' ] );
214 add_action( 'rest_api_init', [ $this, 'register_options' ] );
215 add_action( 'admin_init', [ $this, 'maybe_migrate_legacy_conversion_option' ] );
216 add_action( 'admin_menu', [ $this, 'register_page' ], 99 );
217 add_action( 'rest_pre_update_setting', [ $this, 'recalculate_stats_on_custom_sizes_update' ], 10, 2 );
218
219 add_action( 'elementor_one/' . Config::APP_PREFIX . '_connected', [ $this, 'cleanup_data' ] );
220 add_action( 'elementor_one/' . Config::APP_PREFIX . '_disconnected', [ $this, 'cleanup_data' ] );
221 add_action( 'elementor_one/' . Config::APP_PREFIX . '_migration_run', [ $this, 'on_migration_run' ] );
222
223 // Add action on switch domain for update access token
224 add_action( 'elementor_one/' . Config::APP_PREFIX . '_switched_domain', function( $facade ) {
225 $facade->service()->renew_access_token();
226 } );
227 add_action( 'elementor_one/switched_domain', function( $facade ) {
228 $facade->service()->renew_access_token();
229 } );
230 }
231 }
232