| 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::check_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::check_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 |
/** |
| 217 |
* Module constructor. |
| 218 |
*/ |
| 219 |
public function __construct() { |
| 220 |
$this->register_components(); |
| 221 |
|
| 222 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_global_assets' ] ); |
| 223 |
add_filter( 'plugin_action_links', [ $this, 'add_plugin_links' ], 10, 2 ); |
| 224 |
|
| 225 |
add_action('current_screen', function () { |
| 226 |
if ( ! $this->should_render() ) { |
| 227 |
return; |
| 228 |
} |
| 229 |
|
| 230 |
add_action( 'admin_notices', [ $this, 'maybe_add_quota_reached_notice' ] ); |
| 231 |
|
| 232 |
if ( Utils::is_media_page() ) { |
| 233 |
add_action('in_admin_header', function () { |
| 234 |
$this->render_top_bar(); |
| 235 |
}); |
| 236 |
|
| 237 |
add_action('all_admin_notices', function () { |
| 238 |
$this->render_app(); |
| 239 |
}); |
| 240 |
} |
| 241 |
|
| 242 |
if ( Utils::is_plugin_page() ) { |
| 243 |
add_action('in_admin_header', function () { |
| 244 |
$this->render_top_bar(); |
| 245 |
}); |
| 246 |
} |
| 247 |
|
| 248 |
add_action('admin_enqueue_scripts', function () { |
| 249 |
$this->enqueue_scripts(); |
| 250 |
}); |
| 251 |
}); |
| 252 |
} |
| 253 |
} |
| 254 |
|