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

227 lines 5.9 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 'User_Feedback',
40 ];
41 }
42
43 private function render_top_bar() {
44 ?>
45 <div id="image-optimization-top-bar"></div>
46 <?php
47 }
48
49 private function render_app() {
50 ?>
51 <div class="clear"></div>
52 <div id="image-optimization-app"></div>
53 <?php
54 }
55
56 public function maybe_add_quota_reached_notice() {
57 if ( ! Connect::is_activated() || Data::images_left() > 0 ) {
58 return;
59 }
60
61 ?>
62 <div class="notice notice-warning notice image-optimizer__notice image-optimizer__notice--warning">
63 <p>
64 <b>
65 <?php esc_html_e(
66 'You’ve reached your plan quota.',
67 'image-optimization'
68 ); ?>
69 </b>
70
71 <span>
72 <?php esc_html_e(
73 'You have no images left to optimize in your current plan.',
74 'image-optimization'
75 ); ?>
76
77 <a href="https://go.elementor.com/io-panel-upgrade/">
78 <?php esc_html_e(
79 'Upgrade plan now',
80 'image-optimization'
81 ); ?>
82 </a>
83 </span>
84 </p>
85 </div>
86 <?php
87 }
88
89 public function add_plugin_links( $links, $plugin_file_name ): array {
90 if ( ! str_ends_with( $plugin_file_name, '/image-optimization.php' ) ) {
91 return (array) $links;
92 }
93
94 $custom_links = [
95 'settings' => sprintf(
96 '<a href="%s">%s</a>',
97 admin_url( 'admin.php?page=' . \ImageOptimization\Modules\Settings\Module::SETTING_BASE_SLUG ),
98 esc_html__( 'Settings', 'image-optimization' )
99 ),
100 'upgrade' => sprintf(
101 '<a href="%s" style="color: #524CFF; font-weight: 700;" target="_blank" rel="noopener noreferrer">%s</a>',
102 'https://go.elementor.com/io-panel-upgrade/',
103 esc_html__( 'Upgrade', 'image-optimization' )
104 ),
105 ];
106
107 return array_merge( $custom_links, $links );
108 }
109
110 public function enqueue_global_assets() {
111 wp_enqueue_style(
112 'image-optimization-admin-fonts',
113 'https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap',
114 [],
115 IMAGE_OPTIMIZATION_VERSION
116 );
117
118 wp_enqueue_style(
119 'image-optimization-core-style-admin',
120 $this->get_css_assets_url( 'style-admin', 'assets/build/' ),
121 [],
122 IMAGE_OPTIMIZATION_VERSION,
123 );
124 }
125
126 /**
127 * Enqueue styles and scripts
128 */
129 private function enqueue_scripts() {
130 $asset_file = require IMAGE_OPTIMIZATION_ASSETS_PATH . 'build/admin.asset.php';
131
132 foreach ( $asset_file['dependencies'] as $style ) {
133 wp_enqueue_style( $style );
134 }
135
136 wp_enqueue_script(
137 'image-optimization-admin',
138 $this->get_js_assets_url( 'admin' ),
139 array_merge( $asset_file['dependencies'], [ 'wp-util' ] ),
140 $asset_file['version'],
141 true
142 );
143
144 wp_localize_script(
145 'image-optimization-admin',
146 'imageOptimizerAppSettings',
147 [
148 'siteUrl' => wp_parse_url( get_site_url(), PHP_URL_HOST ),
149 'thumbnailSizes' => wp_get_registered_image_subsizes(),
150 'isDevelopment' => defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG,
151 ]
152 );
153
154 $connect_data = Data::get_connect_data();
155
156 wp_localize_script(
157 'image-optimization-admin',
158 'imageOptimizerUserData',
159 [
160 'isConnected' => Connect::is_connected(),
161 'isActivated' => Connect::is_activated(),
162 'planData' => Connect::is_activated() ? Connect::get_connect_status() : null,
163 'licenseKey' => Connect::is_activated() ? Data::get_activation_state() : null,
164 'imagesLeft' => Connect::is_activated() ? Data::images_left() : null,
165 'isOwner' => Connect::is_connected() ? Data::user_is_subscription_owner() : null,
166 'subscriptionEmail' => $connect_data['user']['email'] ?? null,
167
168 'wpRestNonce' => wp_create_nonce( 'wp_rest' ),
169 'disconnect' => wp_create_nonce( 'wp_rest' ),
170 'authInitNonce' => wp_create_nonce( Connect_Init::NONCE_NAME ),
171 'authDisconnectNonce' => wp_create_nonce( Disconnect::NONCE_NAME ),
172 'authDeactivateNonce' => wp_create_nonce( Deactivate::NONCE_NAME ),
173 'authGetSubscriptionsNonce' => wp_create_nonce( Get_Subscriptions::NONCE_NAME ),
174 'authActivateNonce' => wp_create_nonce( Activate::NONCE_NAME ),
175 'removeBackupsNonce' => wp_create_nonce( Remove_Backups::NONCE_NAME ),
176 'restoreAllImagesNonce' => wp_create_nonce( Restore_All::NONCE_NAME ),
177 'optimizeBulkNonce' => wp_create_nonce( Optimize_Bulk::NONCE_NAME ),
178 'cancelBulkOptimizationNonce' => wp_create_nonce( Cancel_Bulk_Optimization::NONCE_NAME ),
179 ]
180 );
181
182 wp_set_script_translations( 'image-optimization-admin', 'image-optimization' );
183 }
184
185 private function should_render(): bool {
186 return ( Utils::is_media_page() || Utils::is_plugin_page() ) && Utils::user_is_admin();
187 }
188
189 /**
190 * Module constructor.
191 */
192 public function __construct() {
193 $this->register_components();
194
195 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_global_assets' ] );
196 add_filter( 'plugin_action_links', [ $this, 'add_plugin_links' ], 10, 2 );
197
198 add_action('current_screen', function () {
199 if ( ! $this->should_render() ) {
200 return;
201 }
202
203 add_action( 'admin_notices', [ $this, 'maybe_add_quota_reached_notice' ] );
204
205 if ( Utils::is_media_page() ) {
206 add_action('in_admin_header', function () {
207 $this->render_top_bar();
208 });
209
210 add_action('all_admin_notices', function () {
211 $this->render_app();
212 });
213 }
214
215 if ( Utils::is_plugin_page() ) {
216 add_action('in_admin_header', function () {
217 $this->render_top_bar();
218 });
219 }
220
221 add_action('admin_enqueue_scripts', function () {
222 $this->enqueue_scripts();
223 });
224 });
225 }
226 }
227