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
← All changes | modules/settings/module.php +136 -10 1.4.11.7.7 View file →
@@ -1,9 +1,19 @@
1 1 <?php
2 2
3 3 namespace ImageOptimization\Modules\Settings;
4 4
5 +use ImageOptimization\Classes\Image\Image_Conversion_Option;
5 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;
6 16
7 17 if ( ! defined( 'ABSPATH' ) ) {
8 18 exit; // Exit if accessed directly
9 19 }
@@ -17,15 +27,15 @@
17 27 public function get_name(): string {
18 28 return 'settings';
19 29 }
20 30
21 - public static function component_list() : array {
31 + public static function component_list(): array {
22 32 return [
23 33 'Settings_Pointer',
24 34 ];
25 35 }
26 36
27 - public static function get_options() : array {
37 + public static function get_options(): array {
28 38 return [
29 39 'compression_level' => [ 'default' => 'lossy' ],
30 40 'optimize_on_upload' => [
31 41 'type' => 'boolean',
@@ -46,16 +56,29 @@
46 56 'original_images' => [
47 57 'type' => 'boolean',
48 58 'default' => true,
49 59 ],
50 - 'convert_to_webp' => [
51 - 'type' => 'boolean',
52 - 'default' => true,
60 + 'convert_to_format' => [
61 + 'type' => 'string',
62 + 'default' => Image_Conversion_Option::WEBP,
53 63 ],
54 64 'custom_sizes' => [
55 65 'type' => 'string',
56 66 'default' => 'all',
57 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 + ],
58 81 ];
59 82 }
60 83
61 84 public function register_options() {
@@ -78,8 +101,11 @@
78 101 }
79 102
80 103 public function render_app() {
81 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 +
82 108 <!-- The hack required to wrap WP notifications -->
83 109 <div class="wrap">
84 110 <h1 style="display: none;" role="presentation"></h1>
85 111 </div>
@@ -88,22 +114,122 @@
88 114 <?php
89 115 }
90 116
91 117 public function register_page() {
92 - add_media_page(
93 - __( 'Image Optimizer', 'image-optimization' ),
94 - __( 'Image Optimizer', 'image-optimization' ),
118 + add_submenu_page(
119 + 'elementor-home',
120 + __( 'Image Optimization', 'image-optimization' ),
121 + __( 'Image Optimization', 'image-optimization' ),
95 122 self::SETTING_CAPABILITY,
96 123 self::SETTING_BASE_SLUG,
97 124 [ $this, 'render_app' ],
98 - 6
125 + 50
99 126 );
127 +
128 + $this->add_menu_item_class( 'elementor-home', self::SETTING_BASE_SLUG, 'image-optimizer-menu' );
100 129 }
101 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 +
102 214 public function __construct() {
103 215 $this->register_components();
104 216
105 217 add_action( 'admin_init', [ $this, 'register_options' ] );
106 218 add_action( 'rest_api_init', [ $this, 'register_options' ] );
107 - add_action( 'admin_menu', [ $this, 'register_page' ] );
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 + } );
108 234 }
109 235 }