PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.2.0
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.2.0
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 / core / module.php

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

270 lines 7.2 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\Oauth\{
5 Classes\Data,
6 Components\Connect,
7 Rest\Activate,
8 Rest\Connect_Init,
9 Rest\Deactivate,
10 Rest\Disconnect,
11 Rest\Get_Subscriptions,
12 };
13 use ImageOptimization\Modules\Optimization\{
14 Rest\Cancel_Bulk_Optimization,
15 Rest\Optimize_Bulk,
16 };
17 use ImageOptimization\Modules\Backups\Rest\{
18 Restore_All,
19 Remove_Backups,
20 };
21 use ImageOptimization\Classes\{
22 Module_Base,
23 Utils,
24 };
25
26 if ( ! defined( 'ABSPATH' ) ) {
27 exit; // Exit if accessed directly.
28 }
29
30 class Module extends Module_Base {
31 public function get_name(): string {
32 return 'core';
33 }
34
35 public static function component_list() : array {
36 return [
37 'Pointers',
38 'Conflicts',
39 ];
40 }
41
42 private function render_top_bar() {
43 ?>
44 <div id="image-optimization-top-bar"></div>
45 <?php
46 }
47
48 private function render_app() {
49 ?>
50 <div class="clear"></div>
51 <div id="image-optimization-app"></div>
52 <?php
53 }
54
55 public function maybe_add_quota_reached_notice() {
56 if ( ! Connect::is_activated() || Data::images_left() > 0 ) {
57 return;
58 }
59
60 ?>
61 <div class="notice notice-warning notice image-optimizer__notice image-optimizer__notice--warning">
62 <p>
63 <b>
64 <?php esc_html_e(
65 'You’ve reached your plan quota.',
66 'image-optimization'
67 ); ?>
68 </b>
69
70 <span>
71 <?php esc_html_e(
72 'You have no images left to optimize in your current plan.',
73 'image-optimization'
74 ); ?>
75
76 <a href="https://go.elementor.com/io-panel-upgrade/">
77 <?php esc_html_e(
78 'Upgrade plan now',
79 'image-optimization'
80 ); ?>
81 </a>
82 </span>
83 </p>
84 </div>
85 <?php
86 }
87
88 public function add_plugin_links( $links, $plugin_file_name ): array {
89 if ( false === strpos( $plugin_file_name, '/image-optimization.php' ) ) {
90 return (array) $links;
91 }
92
93 $custom_links = [
94 'settings' => sprintf(
95 '<a href="%s">%s</a>',
96 admin_url( 'admin.php?page=' . \ImageOptimization\Modules\Settings\Module::SETTING_BASE_SLUG ),
97 esc_html__( 'Settings', 'image-optimization' )
98 ),
99 ];
100
101 if ( ! Connect::is_connected() ) {
102 $custom_links['connect'] = sprintf(
103 '<a href="%s" style="color: #524CFF; font-weight: 700;">%s</a>',
104 admin_url( 'admin.php?page=' . \ImageOptimization\Modules\Settings\Module::SETTING_BASE_SLUG . '&action=connect' ),
105 esc_html__( 'Connect', 'image-optimization' )
106 );
107 }
108
109 if ( Connect::is_connected() && ! Connect::is_activated() ) {
110 $custom_links['activate'] = sprintf(
111 '<a href="%s" style="color: #524CFF; font-weight: 700;">%s</a>',
112 admin_url( 'admin.php?page=' . \ImageOptimization\Modules\Settings\Module::SETTING_BASE_SLUG ),
113 esc_html__( 'Activate', 'image-optimization' )
114 );
115 }
116
117 if ( Connect::is_connected() && Connect::is_activated() ) {
118 $plan_data = Connect::get_connect_status();
119 $usage_percentage = 0;
120
121 if ( ! empty( $plan_data ) ) {
122 $usage_percentage = $plan_data->used_quota / $plan_data->quota * 100;
123 }
124
125 if ( $usage_percentage >= 80 ) {
126 $custom_links['upgrade'] = sprintf(
127 '<a href="%s" style="color: #524CFF; font-weight: 700;">%s</a>',
128 'https://go.elementor.com/io-panel-upgrade/',
129 esc_html__( 'Upgrade', 'image-optimization' )
130 );
131 }
132 }
133
134 return array_merge( $custom_links, $links );
135 }
136
137 public function enqueue_global_assets() {
138 wp_enqueue_style(
139 'image-optimization-admin-fonts',
140 'https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap',
141 [],
142 IMAGE_OPTIMIZATION_VERSION
143 );
144
145 wp_enqueue_style(
146 'image-optimization-core-style-admin',
147 $this->get_css_assets_url( 'style-admin', '/assets/build/' ),
148 [],
149 IMAGE_OPTIMIZATION_VERSION,
150 );
151 }
152
153 /**
154 * Enqueue styles and scripts
155 */
156 private function enqueue_scripts() {
157 $asset_file = include IMAGE_OPTIMIZATION_ASSETS_PATH . 'build/admin.asset.php';
158
159 foreach ( $asset_file['dependencies'] as $style ) {
160 wp_enqueue_style( $style );
161 }
162
163 wp_enqueue_script(
164 'image-optimization-admin',
165 $this->get_js_assets_url( 'admin' ),
166 array_merge( $asset_file['dependencies'], [ 'wp-util' ] ),
167 IMAGE_OPTIMIZATION_VERSION,
168 true
169 );
170
171 wp_localize_script(
172 'image-optimization-admin',
173 'imageOptimizerAppSettings',
174 [
175 'siteUrl' => wp_parse_url( get_site_url(), PHP_URL_HOST ),
176 'thumbnailSizes' => wp_get_registered_image_subsizes(),
177 'isDevelopment' => defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG,
178 ]
179 );
180
181 $connect_data = Data::get_connect_data();
182
183 wp_localize_script(
184 'image-optimization-admin',
185 'imageOptimizerUserData',
186 [
187 'isConnected' => Connect::is_connected(),
188 'isActivated' => Connect::is_activated(),
189 'planData' => Connect::is_activated() ? Connect::get_connect_status() : null,
190 'licenseKey' => Connect::is_activated() ? Data::get_activation_state() : null,
191 'imagesLeft' => Connect::is_activated() ? Data::images_left() : null,
192 'isOwner' => Connect::is_connected() ? Data::user_is_subscription_owner() : null,
193 'subscriptionEmail' => $connect_data['user']['email'] ?? null,
194
195 'wpRestNonce' => wp_create_nonce( 'wp_rest' ),
196 'disconnect' => wp_create_nonce( 'wp_rest' ),
197 'authInitNonce' => wp_create_nonce( Connect_Init::NONCE_NAME ),
198 'authDisconnectNonce' => wp_create_nonce( Disconnect::NONCE_NAME ),
199 'authDeactivateNonce' => wp_create_nonce( Deactivate::NONCE_NAME ),
200 'authGetSubscriptionsNonce' => wp_create_nonce( Get_Subscriptions::NONCE_NAME ),
201 'authActivateNonce' => wp_create_nonce( Activate::NONCE_NAME ),
202 'removeBackupsNonce' => wp_create_nonce( Remove_Backups::NONCE_NAME ),
203 'restoreAllImagesNonce' => wp_create_nonce( Restore_All::NONCE_NAME ),
204 'optimizeBulkNonce' => wp_create_nonce( Optimize_Bulk::NONCE_NAME ),
205 'cancelBulkOptimizationNonce' => wp_create_nonce( Cancel_Bulk_Optimization::NONCE_NAME ),
206 ]
207 );
208
209 wp_set_script_translations( 'image-optimization-admin', 'image-optimization' );
210 }
211
212 private function should_render(): bool {
213 return ( Utils::is_media_page() || Utils::is_plugin_page() ) && Utils::user_is_admin();
214 }
215
216 public function add_leave_feedback_footer_text(): void {
217 $link = 'https://wordpress.org/support/plugin/image-optimization/reviews/?filter=5#new-post';
218
219 printf(
220 __( "<b>Found Image Optimizer helpful?</b> Leave us a <a href='%1\$s' aria-label='%2\$s'>�
221 �
222 �
223 �
224 �
225 </a> rating!" ),
226 $link,
227 __( 'Five stars', 'image-optimization' )
228 );
229 }
230
231 /**
232 * Module constructor.
233 */
234 public function __construct() {
235 $this->register_components();
236
237 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_global_assets' ] );
238 add_filter( 'plugin_action_links', [ $this, 'add_plugin_links' ], 10, 2 );
239
240 add_action('current_screen', function () {
241 if ( ! $this->should_render() ) {
242 return;
243 }
244
245 add_filter( 'admin_footer_text', [ $this, 'add_leave_feedback_footer_text' ] );
246 add_action( 'admin_notices', [ $this, 'maybe_add_quota_reached_notice' ] );
247
248 if ( Utils::is_media_page() ) {
249 add_action('in_admin_header', function () {
250 $this->render_top_bar();
251 });
252
253 add_action('all_admin_notices', function () {
254 $this->render_app();
255 });
256 }
257
258 if ( Utils::is_plugin_page() ) {
259 add_action('in_admin_header', function () {
260 $this->render_top_bar();
261 });
262 }
263
264 add_action('admin_enqueue_scripts', function () {
265 $this->enqueue_scripts();
266 });
267 });
268 }
269 }
270