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

236 lines 6.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\{
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 'migration_popup_dismissed' => [
78 'type' => 'boolean',
79 'default' => false,
80 ],
81 ];
82 }
83
84 public function register_options() {
85 $options = $this->get_options();
86
87 foreach ( $options as $key => &$args ) {
88 $args['type'] = $args['type'] ?? 'string';
89 $args['show_in_rest'] = $args['show_in_rest'] ?? true;
90 $args['default'] = $args['default'] ?? '';
91
92 register_setting(
93 self::SETTING_GROUP,
94 self::SETTING_PREFIX . $key,
95 $args
96 );
97
98 // Set defaults
99 add_option( self::SETTING_PREFIX . $key, $args['default'] );
100 }
101 }
102
103 public function render_app() {
104 ?>
105 <?php Sale_Banner::get_banner( 'https://go.elementor.com/IO-BF-sale' ); ?>
106 <?php Elementor_Birthday_Banner::get_banner( 'https://go.elementor.com/IO-10th-bd-sale' ); ?>
107
108 <!-- The hack required to wrap WP notifications -->
109 <div class="wrap">
110 <h1 style="display: none;" role="presentation"></h1>
111 </div>
112
113 <div id="image-optimization-app"></div>
114 <?php
115 }
116
117 public function register_page() {
118 add_submenu_page(
119 'elementor-home',
120 __( 'Image Optimization', 'image-optimization' ),
121 __( 'Image Optimization', 'image-optimization' ),
122 self::SETTING_CAPABILITY,
123 self::SETTING_BASE_SLUG,
124 [ $this, 'render_app' ],
125 50
126 );
127
128 $this->add_menu_item_class( 'elementor-home', self::SETTING_BASE_SLUG, 'image-optimizer-menu' );
129 }
130
131 private function add_menu_item_class( string $parent_slug, string $menu_slug, string $class ) {
132 global $submenu;
133
134 if ( ! isset( $submenu[ $parent_slug ] ) ) {
135 return;
136 }
137
138 foreach ( $submenu[ $parent_slug ] as &$item ) {
139 if ( $item[2] === $menu_slug ) {
140 $item[4] = isset( $item[4] ) ? $item[4] . ' ' . $class : $class;
141 break;
142 }
143 }
144 }
145
146 /**
147 * The handler converts an old CONVERT_TO_WEBP option to the new CONVERT_TO_FORMAT option.
148 * TODO: [Stability] Remove this fallback after all users updated
149 *
150 * @return void
151 */
152 public function maybe_migrate_legacy_conversion_option() {
153 $legacy_convert_to_webp = get_option( Settings::CONVERT_TO_WEBP_OPTION_NAME, null );
154
155 if ( is_null( $legacy_convert_to_webp ) ) {
156 return;
157 }
158
159 if ( '1' === $legacy_convert_to_webp ) {
160 update_option( Settings::CONVERT_TO_FORMAT_OPTION_NAME, Image_Conversion_Option::WEBP, false );
161 }
162
163 if ( '0' === $legacy_convert_to_webp ) {
164 update_option( Settings::CONVERT_TO_FORMAT_OPTION_NAME, Image_Conversion_Option::ORIGINAL, false );
165 }
166
167 delete_option( Settings::CONVERT_TO_WEBP_OPTION_NAME );
168 }
169
170 /**
171 * The handler triggers stats recalculation on custom sizes update.
172 *
173 * @param $result
174 * @param $name
175 *
176 * @return void
177 */
178 public function recalculate_stats_on_custom_sizes_update( $result, $name ) {
179 if ( Settings::CUSTOM_SIZES_OPTION_NAME === $name ) {
180 Optimization_Stats::get_image_stats( null, true );
181 }
182 }
183
184 public function cleanup_data() {
185 delete_transient( Client::SITE_INFO_TRANSIENT );
186 delete_transient( Connect_Manager_Connect::STATUS_CHECK_TRANSIENT );
187 }
188
189 /**
190 * Register or update site data for One connect
191 * @throws Exception
192 */
193 public function on_migration_run() {
194 $old_options = [
195 'image_optimizer_client_id',
196 'image_optimizer_client_secret',
197 'image_optimizer_home_url',
198 'image_optimizer_access_token',
199 'image_optimizer_token_id',
200 'image_optimizer_refresh_token',
201 'image_optimizer_user_access_token',
202 'image_optimizer_owner_user_id',
203 'image_optimizer_subscription_id',
204 Settings::SUBSCRIPTION_ID,
205 ];
206
207 $this->cleanup_data();
208
209 foreach ( $old_options as $option ) {
210 delete_option( $option );
211 }
212 }
213
214 public function __construct() {
215 $this->register_components();
216
217 add_action( 'admin_init', [ $this, 'register_options' ] );
218 add_action( 'rest_api_init', [ $this, 'register_options' ] );
219 add_action( 'admin_init', [ $this, 'maybe_migrate_legacy_conversion_option' ] );
220 add_action( 'admin_menu', [ $this, 'register_page' ], 99 );
221 add_action( 'rest_pre_update_setting', [ $this, 'recalculate_stats_on_custom_sizes_update' ], 10, 2 );
222
223 add_action( 'elementor_one/' . Config::APP_PREFIX . '_connected', [ $this, 'cleanup_data' ] );
224 add_action( 'elementor_one/' . Config::APP_PREFIX . '_disconnected', [ $this, 'cleanup_data' ] );
225 add_action( 'elementor_one/' . Config::APP_PREFIX . '_migration_run', [ $this, 'on_migration_run' ] );
226
227 // Add action on switch domain for update access token
228 add_action( 'elementor_one/' . Config::APP_PREFIX . '_switched_domain', function( $facade ) {
229 $facade->service()->renew_access_token();
230 } );
231 add_action( 'elementor_one/switched_domain', function( $facade ) {
232 $facade->service()->renew_access_token();
233 } );
234 }
235 }
236