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

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