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 / core / module.php

module.php in Image Optimization – Compress Images and Convert to WebP or AVIF 1.6.6, at modules/core/module.php

353 lines 10.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace ImageOptimization\Modules\Core;
3
4 use ImageOptimization\Modules\Optimization\{
5 Classes\Validate_Image,
6 Rest\Cancel_Bulk_Optimization,
7 Rest\Optimize_Bulk,
8 };
9 use ImageOptimization\Modules\Backups\Rest\{
10 Restore_All,
11 Remove_Backups,
12 };
13 use ImageOptimization\Classes\{
14 Async_Operation\Async_Operation,
15 Async_Operation\Async_Operation_Queue,
16 Async_Operation\Queries\Operation_Query,
17 Image\Image_Meta,
18 Image\Image_Optimization_Error_Type,
19 Image\Image_Status,
20 Migration\Migration_Manager,
21 Module_Base,
22 Utils,
23 };
24
25 use ImageOptimization\Plugin;
26
27 if ( ! defined( 'ABSPATH' ) ) {
28 exit; // Exit if accessed directly.
29 }
30
31 class Module extends Module_Base {
32 public function get_name(): string {
33 return 'core';
34 }
35
36 public static function component_list() : array {
37 return [
38 'Pointers',
39 'Migrations',
40 'Conflicts',
41 'User_Feedback',
42 'Not_Connected',
43 'Not_Connected_Modal',
44 'Renewal_Notice',
45 ];
46 }
47
48 private function render_top_bar() {
49 ?>
50 <div id="image-optimization-top-bar"></div>
51 <?php
52 }
53
54 private function render_app() {
55 ?>
56 <div class="clear"></div>
57 <div id="image-optimization-app"></div>
58 <?php
59 }
60
61 public function maybe_add_quota_reached_notice() {
62
63 // @var ImageOptimizer/Modules/ConnectManager/Module
64 $module = Plugin::instance()->modules_manager->get_modules( 'connect-manager' );
65
66 if ( ! $module->connect_instance->get_connect_status() || $module->connect_instance->images_left() > 0 ) {
67 return;
68 }
69
70 ?>
71 <div class="notice notice-warning notice image-optimizer__notice image-optimizer__notice--warning">
72 <p>
73 <b>
74 <?php esc_html_e(
75 'You’ve reached your plan quota.',
76 'image-optimization'
77 ); ?>
78 </b>
79
80 <span>
81 <?php esc_html_e(
82 'You have no images left to optimize in your current plan.',
83 'image-optimization'
84 ); ?>
85
86 <a href="https://go.elementor.com/io-quota-upgrade/">
87 <?php esc_html_e(
88 'Upgrade plan now',
89 'image-optimization'
90 ); ?>
91 </a>
92 </span>
93 </p>
94 </div>
95 <?php
96 }
97
98 public function maybe_add_url_mismatch_notice() {
99 // @var ImageOptimizer/Modules/ConnectManager/Module
100 $module = Plugin::instance()->modules_manager->get_modules( 'connect-manager' );
101
102 if ( $module->connect_instance->is_valid_home_url() ) {
103 return;
104 }
105
106 ?>
107 <div class="notice notice-error notice image-optimizer__notice image-optimizer__notice--error">
108 <p>
109 <b>
110 <?php esc_html_e(
111 'Your license key does not match your current domain, causing a mismatch.',
112 'image-optimization'
113 ); ?>
114 </b>
115
116 <span>
117 <?php esc_html_e(
118 'This is most likely due to a change in the domain URL of your site (including HTTP/SSL migration).',
119 'image-optimization'
120 ); ?>
121
122 <button type="button" onclick="document.dispatchEvent( new Event( 'image-optimizer/auth/url-mismatch-modal/open' ) );">
123 <?php esc_html_e(
124 'Fix mismatched URL',
125 'image-optimization'
126 ); ?>
127 </button>
128 </span>
129 </p>
130 </div>
131 <?php
132 }
133
134 public function add_plugin_links( $links, $plugin_file_name ): array {
135 if ( ! str_ends_with( $plugin_file_name, '/image-optimization.php' ) ) {
136 return (array) $links;
137 }
138
139 $custom_links = [
140 'settings' => sprintf(
141 '<a href="%s">%s</a>',
142 admin_url( 'admin.php?page=' . \ImageOptimization\Modules\Settings\Module::SETTING_BASE_SLUG ),
143 esc_html__( 'Settings', 'image-optimization' )
144 ),
145 ];
146 // @var ImageOptimizer/Modules/ConnectManager/Module
147 $module = Plugin::instance()->modules_manager->get_modules( 'connect-manager' );
148
149 if ( $module->connect_instance->is_connected() ) {
150 $custom_links['upgrade'] = sprintf(
151 '<a href="%s" style="color: #524CFF; font-weight: 700;" target="_blank" rel="noopener noreferrer">%s</a>',
152 'https://go.elementor.com/io-plugins-upgrade/',
153 esc_html__( 'Upgrade', 'image-optimization' )
154 );
155 } else {
156 $custom_links['connect'] = sprintf(
157 '<a href="%s" style="color: #524CFF; font-weight: 700;">%s</a>',
158 admin_url( 'admin.php?page=' . \ImageOptimization\Modules\Settings\Module::SETTING_BASE_SLUG ),
159 esc_html__( 'Connect', 'image-optimization' )
160 );
161 }
162
163 return array_merge( $custom_links, $links );
164 }
165
166 public function enqueue_global_assets() {
167 wp_enqueue_style(
168 'image-optimization-admin-fonts',
169 'https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap',
170 [],
171 IMAGE_OPTIMIZATION_VERSION
172 );
173
174 wp_enqueue_style(
175 'image-optimization-core-style-admin',
176 $this->get_css_assets_url( 'style-admin', 'assets/build/' ),
177 [],
178 IMAGE_OPTIMIZATION_VERSION,
179 );
180 }
181
182 /**
183 * Enqueue styles and scripts
184 */
185 private function enqueue_scripts() {
186 $asset_file = require IMAGE_OPTIMIZATION_ASSETS_PATH . 'build/admin.asset.php';
187
188 foreach ( $asset_file['dependencies'] as $style ) {
189 wp_enqueue_style( $style );
190 }
191
192 wp_enqueue_style( 'thickbox' );
193
194 wp_enqueue_script(
195 'image-optimization-admin',
196 $this->get_js_assets_url( 'admin' ),
197 array_merge( $asset_file['dependencies'], [ 'wp-util' ] ),
198 $asset_file['version'],
199 true
200 );
201
202 wp_localize_script(
203 'image-optimization-admin',
204 'imageOptimizerAppSettings',
205 [
206 'siteUrl' => wp_parse_url( get_site_url(), PHP_URL_HOST ),
207 'thumbnailSizes' => wp_get_registered_image_subsizes(),
208 'isDevelopment' => defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG,
209 ]
210 );
211
212 /**
213 * @var ImageOptimizer\Modules\ConnectManager\Module $module
214 */
215 $module = Plugin::instance()->modules_manager->get_modules( 'connect-manager' );
216 $is_connect_on_fly = $module->connect_instance->get_is_connect_on_fly();
217 $connect_email = $module->connect_instance->get_connect_data()['user']['email'] ?? null;
218 $show_reset = ! $module->connect_instance->is_connected()
219 && ( $module->connect_instance->get_client_id() || $module->connect_instance->get_client_secret() );
220
221 wp_localize_script(
222 'image-optimization-admin',
223 'imageOptimizerUserData',
224 [
225 'isConnectOnFly' => $is_connect_on_fly,
226 'isConnected' => $module->connect_instance->is_connected(),
227 'isActivated' => $module->connect_instance->is_activated(),
228 'isUrlMismatch' => ! $module->connect_instance->is_valid_home_url(),
229 'planData' => $module->connect_instance->is_activated() ? $module->connect_instance->get_connect_status() : null,
230 'licenseKey' => $module->connect_instance->is_activated() ? $module->connect_instance->get_activation_state() : null,
231 'imagesLeft' => $module->connect_instance->is_activated() ? $module->connect_instance->images_left() : null,
232 'isOwner' => $module->connect_instance->is_connected() ? $module->connect_instance->user_is_subscription_owner() : null,
233 'subscriptionEmail' => $connect_email ? $connect_email : null,
234 'showResetButton' => $show_reset,
235 'maxFileSize' => Validate_Image::get_max_file_size(),
236
237 'wpRestNonce' => wp_create_nonce( 'wp_rest' ),
238 'disconnect' => wp_create_nonce( 'wp_rest' ),
239 'authInitNonce' => wp_create_nonce( $module->connect_instance->connect_init_nonce() ),
240 'authDisconnectNonce' => wp_create_nonce( $module->connect_instance->disconnect_nonce() ),
241 'authDeactivateNonce' => wp_create_nonce( $module->connect_instance->deactivate_nonce() ),
242 'authGetSubscriptionsNonce' => wp_create_nonce( $module->connect_instance->get_subscriptions_nonce() ),
243 'authActivateNonce' => wp_create_nonce( $module->connect_instance->activate_nonce() ),
244 'versionNonce' => wp_create_nonce( $module->connect_instance->version_nonce() ),
245 'removeBackupsNonce' => wp_create_nonce( Remove_Backups::NONCE_NAME ),
246 'restoreAllImagesNonce' => wp_create_nonce( Restore_All::NONCE_NAME ),
247 'optimizeBulkNonce' => wp_create_nonce( Optimize_Bulk::NONCE_NAME ),
248 'cancelBulkOptimizationNonce' => wp_create_nonce( Cancel_Bulk_Optimization::NONCE_NAME ),
249 ]
250 );
251
252 wp_set_script_translations( 'image-optimization-admin', 'image-optimization' );
253 }
254
255 private function should_render(): bool {
256 return ( Utils::is_media_page() || Utils::is_plugin_page() ) && Utils::user_is_admin();
257 }
258
259 public static function on_deactivation(): void {
260 $optimization_query = ( new Operation_Query() )
261 ->set_queue( Async_Operation_Queue::OPTIMIZE )
262 ->set_status( [ Async_Operation::OPERATION_STATUS_PENDING, Async_Operation::OPERATION_STATUS_RUNNING ] )
263 ->set_limit( -1 );
264
265 $restoring_query = ( new Operation_Query() )
266 ->set_queue( Async_Operation_Queue::RESTORE )
267 ->set_status( [ Async_Operation::OPERATION_STATUS_PENDING, Async_Operation::OPERATION_STATUS_RUNNING ] )
268 ->set_limit( -1 );
269
270 $optimization_operations = Async_Operation::get( $optimization_query );
271 $restoring_operations = Async_Operation::get( $restoring_query );
272
273 foreach ( $optimization_operations as $operation ) {
274 $image_id = $operation->get_args()['attachment_id'];
275
276 if ( ! $image_id ) {
277 continue;
278 }
279
280 Async_Operation::remove( [ $operation->get_id() ] );
281
282 $image_meta = new Image_Meta( $image_id );
283
284 if ( empty( $image_meta->get_optimized_sizes() ) ) {
285 $image_meta->delete();
286 } else {
287 $image_meta
288 ->set_status( Image_Status::OPTIMIZATION_FAILED )
289 ->set_error_type( Image_Optimization_Error_Type::PLUGIN_DEACTIVATION )
290 ->save();
291 }
292 }
293
294 foreach ( $restoring_operations as $operation ) {
295 $image_id = $operation->get_args()['attachment_id'];
296
297 if ( ! $image_id ) {
298 continue;
299 }
300
301 Async_Operation::remove( [ $operation->get_id() ] );
302
303 $image_meta = new Image_Meta( $image_id );
304
305 $image_meta
306 ->set_status( Image_Status::RESTORING_FAILED )
307 ->set_error_type( Image_Optimization_Error_Type::PLUGIN_DEACTIVATION )
308 ->save();
309 }
310 }
311
312 /**
313 * Module constructor.
314 */
315 public function __construct() {
316 $this->register_components();
317
318 add_action( 'action_scheduler_init', [ Migration_Manager::class, 'init' ] );
319
320 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_global_assets' ] );
321 add_filter( 'plugin_action_links', [ $this, 'add_plugin_links' ], 10, 2 );
322
323 add_action('current_screen', function () {
324 if ( ! $this->should_render() ) {
325 return;
326 }
327
328 add_action( 'admin_notices', [ $this, 'maybe_add_quota_reached_notice' ] );
329 add_action( 'admin_notices', [ $this, 'maybe_add_url_mismatch_notice' ] );
330
331 if ( Utils::is_media_page() ) {
332 add_action('in_admin_header', function () {
333 $this->render_top_bar();
334 });
335
336 add_action('all_admin_notices', function () {
337 $this->render_app();
338 });
339 }
340
341 if ( Utils::is_plugin_page() ) {
342 add_action('in_admin_header', function () {
343 $this->render_top_bar();
344 });
345 }
346
347 add_action('admin_enqueue_scripts', function () {
348 $this->enqueue_scripts();
349 });
350 });
351 }
352 }
353