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

221 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 ImageOptimizer\Modules\Core;
3
4 use ImageOptimizer\Modules\Oauth\{
5 Classes\Data,
6 Components\Connect,
7 Rest\Activate,
8 Rest\ConnectInit,
9 Rest\Deactivate,
10 Rest\Disconnect,
11 Rest\GetSubscriptions,
12 };
13 use ImageOptimizer\Modules\Optimization\{
14 Rest\Cancel_Bulk_Optimization,
15 Rest\Optimize_Bulk,
16 };
17 use ImageOptimizer\Modules\Backups\Rest\{
18 Restore_All,
19 Remove_Backups,
20 };
21 use ImageOptimizer\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-optimizer-top-bar"></div>
45 <?php
46 }
47
48 private function render_app() {
49 ?>
50 <div class="clear"></div>
51 <div id="image-optimizer-app"></div>
52 <?php
53 }
54
55 public function add_plugin_links( $links, $plugin_file_name ): array {
56 if ( false === strpos( $plugin_file_name, '/image-optimization.php' ) ) {
57 return (array) $links;
58 }
59
60 $custom_links = [
61 'settings' => sprintf(
62 '<a href="%s">%s</a>',
63 admin_url( 'admin.php?page=' . \ImageOptimizer\Modules\Settings\Module::SETTING_BASE_SLUG ),
64 esc_html__( 'Settings', 'image-optimizer' )
65 ),
66 ];
67
68 if ( ! Connect::is_connected() ) {
69 $custom_links['connect'] = sprintf(
70 '<a href="%s" style="color: #524CFF; font-weight: 700;">%s</a>',
71 admin_url( 'admin.php?page=' . \ImageOptimizer\Modules\Settings\Module::SETTING_BASE_SLUG . '&action=connect' ),
72 esc_html__( 'Connect', 'image-optimizer' )
73 );
74 }
75
76 if ( Connect::is_connected() && ! Connect::is_activated() ) {
77 $custom_links['activate'] = sprintf(
78 '<a href="%s" style="color: #524CFF; font-weight: 700;">%s</a>',
79 admin_url( 'admin.php?page=' . \ImageOptimizer\Modules\Settings\Module::SETTING_BASE_SLUG ),
80 esc_html__( 'Activate', 'image-optimizer' )
81 );
82 }
83
84 if ( Connect::is_connected() && Connect::is_activated() ) {
85 $plan_data = Connect::check_connect_status();
86 $usage_percentage = 0;
87
88 if ( ! empty( $plan_data ) ) {
89 $usage_percentage = $plan_data->used_quota / $plan_data->quota * 100;
90 }
91
92 if ( $usage_percentage >= 80 ) {
93 $custom_links['upgrade'] = sprintf(
94 '<a href="%s" style="color: #524CFF; font-weight: 700;">%s</a>',
95 'https://go.elementor.com/io-panel-upgrade/',
96 esc_html__( 'Upgrade', 'image-optimizer' )
97 );
98 }
99 }
100
101 return array_merge( $custom_links, $links );
102 }
103
104 /**
105 * Enqueue fonts
106 */
107 public function enqueue_fonts() {
108 $screen = get_current_screen();
109
110 if ( ! $this->should_render() && ( 'attachment' !== $screen->id ) ) {
111 return;
112 }
113
114 wp_enqueue_style(
115 'image-optimizer-admin-fonts',
116 'https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap',
117 [],
118 IMAGE_OPTIMIZER_VERSION
119 );
120 }
121
122 /**
123 * Enqueue styles and scripts
124 */
125 private function enqueue_scripts() {
126 $asset_file = include IMAGE_OPTIMIZER_ASSETS_PATH . 'build/admin.asset.php';
127
128 foreach ( $asset_file['dependencies'] as $style ) {
129 wp_enqueue_style( $style );
130 }
131
132 wp_enqueue_script(
133 'image-optimizer-admin',
134 $this->get_js_assets_url( 'admin' ),
135 array_merge( $asset_file['dependencies'], [ 'wp-util' ] ),
136 IMAGE_OPTIMIZER_VERSION,
137 true
138 );
139
140 wp_localize_script(
141 'image-optimizer-admin',
142 'imageOptimizerAppSettings',
143 [
144 'siteUrl' => wp_parse_url( get_site_url(), PHP_URL_HOST ),
145 'thumbnailSizes' => wp_get_registered_image_subsizes(),
146 'isDevelopment' => defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG,
147 ]
148 );
149
150 $connect_data = Data::get_connect_data();
151
152 wp_localize_script(
153 'image-optimizer-admin',
154 'imageOptimizerUserData',
155 [
156 'isConnected' => Connect::is_connected(),
157 'isActivated' => Connect::is_activated(),
158 'planData' => Connect::is_activated() ? Connect::check_connect_status() : null,
159 'licenseKey' => Connect::is_activated() ? Data::get_activation_state() : null,
160 'imagesLeft' => Connect::is_activated() ? Data::images_left() : null,
161 'isOwner' => Connect::is_connected() ? Data::user_is_subscription_owner() : null,
162 'subscriptionEmail' => $connect_data['user']['email'] ?? null,
163
164 'wpRestNonce' => wp_create_nonce( 'wp_rest' ),
165 'disconnect' => wp_create_nonce( 'wp_rest' ),
166 'authInitNonce' => wp_create_nonce( ConnectInit::NONCE_NAME ),
167 'authDisconnectNonce' => wp_create_nonce( Disconnect::NONCE_NAME ),
168 'authDeactivateNonce' => wp_create_nonce( Deactivate::NONCE_NAME ),
169 'authGetSubscriptionsNonce' => wp_create_nonce( GetSubscriptions::NONCE_NAME ),
170 'authActivateNonce' => wp_create_nonce( Activate::NONCE_NAME ),
171 'removeBackupsNonce' => wp_create_nonce( Remove_Backups::NONCE_NAME ),
172 'restoreAllImagesNonce' => wp_create_nonce( Restore_All::NONCE_NAME ),
173 'optimizeBulkNonce' => wp_create_nonce( Optimize_Bulk::NONCE_NAME ),
174 'cancelBulkOptimizationNonce' => wp_create_nonce( Cancel_Bulk_Optimization::NONCE_NAME ),
175 ]
176 );
177
178 wp_set_script_translations( 'image-optimizer-admin', 'image-optimizer' );
179 }
180
181 private function should_render(): bool {
182 return ( Utils::is_media_page() || Utils::is_plugin_page() ) && Utils::user_is_admin();
183 }
184
185 /**
186 * Module constructor.
187 */
188 public function __construct() {
189 $this->register_components();
190
191 add_filter( 'plugin_action_links', [ $this, 'add_plugin_links' ], 10, 2 );
192 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_fonts' ] );
193
194 add_action('current_screen', function () {
195 if ( ! $this->should_render() ) {
196 return;
197 }
198
199 if ( Utils::is_media_page() ) {
200 add_action('in_admin_header', function () {
201 $this->render_top_bar();
202 });
203
204 add_action('all_admin_notices', function () {
205 $this->render_app();
206 });
207 }
208
209 if ( Utils::is_plugin_page() ) {
210 add_action('in_admin_header', function () {
211 $this->render_top_bar();
212 });
213 }
214
215 add_action('admin_enqueue_scripts', function () {
216 $this->enqueue_scripts();
217 });
218 });
219 }
220 }
221