class-bulk.php
2 years ago
class-cdn.php
2 years ago
class-dashboard.php
2 years ago
class-directory.php
2 years ago
class-integrations.php
2 years ago
class-lazy.php
2 years ago
class-nextgen.php
2 years ago
class-settings.php
2 years ago
class-tutorials.php
2 years ago
class-upgrade.php
2 years ago
class-webp.php
2 years ago
class-dashboard.php
407 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Dashboard page class: Dashboard extends Abstract_Page. |
| 4 | * |
| 5 | * @since 3.8.6 |
| 6 | * @package Smush\App\Pages |
| 7 | */ |
| 8 | |
| 9 | namespace Smush\App\Pages; |
| 10 | |
| 11 | use Smush\App\Abstract_Summary_Page; |
| 12 | use Smush\App\Interface_Page; |
| 13 | use Smush\Core\Array_Utils; |
| 14 | use Smush\Core\Resize\Resize_Optimization; |
| 15 | use Smush\Core\Settings; |
| 16 | use Smush\Core\Stats\Global_Stats; |
| 17 | use WP_Smush; |
| 18 | |
| 19 | if ( ! defined( 'WPINC' ) ) { |
| 20 | die; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Class Dashboard |
| 25 | */ |
| 26 | class Dashboard extends Abstract_Summary_Page implements Interface_Page { |
| 27 | |
| 28 | /** |
| 29 | * Function triggered when the page is loaded before render any content. |
| 30 | */ |
| 31 | public function on_load() {} |
| 32 | |
| 33 | /** |
| 34 | * Enqueue scripts. |
| 35 | * |
| 36 | * @since 3.9.0 |
| 37 | * |
| 38 | * @param string $hook Hook from where the call is made. |
| 39 | */ |
| 40 | public function enqueue_scripts( $hook ) { |
| 41 | // Scripts for Configs. |
| 42 | $this->enqueue_configs_scripts(); |
| 43 | |
| 44 | // Scripts for Tutorials. |
| 45 | $this->enqueue_tutorials_scripts(); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Register meta boxes. |
| 50 | */ |
| 51 | public function register_meta_boxes() { |
| 52 | if ( ! is_multisite() || ( is_multisite() && ! is_network_admin() ) ) { |
| 53 | $this->add_meta_box( |
| 54 | 'dashboard/summary', |
| 55 | null, |
| 56 | array( $this, 'summary_meta_box' ), |
| 57 | null, |
| 58 | null, |
| 59 | 'summary', |
| 60 | array( |
| 61 | 'box_class' => 'sui-box sui-summary sui-summary-smush-metabox', |
| 62 | 'box_content_class' => false, |
| 63 | ) |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Meta boxes on the left side. |
| 69 | */ |
| 70 | |
| 71 | if ( self::should_render( 'bulk' ) ) { |
| 72 | $this->add_meta_box( |
| 73 | 'dashboard/bulk', |
| 74 | __( 'Bulk Smush', 'wp-smushit' ), |
| 75 | array( $this, 'bulk_compress_meta_box' ), |
| 76 | null, |
| 77 | null, |
| 78 | 'box-dashboard-left' |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | if ( self::should_render( 'integrations' ) ) { |
| 83 | $this->add_meta_box( |
| 84 | 'dashboard/integrations', |
| 85 | __( 'Integrations', 'wp-smushit' ), |
| 86 | array( $this, 'integrations_meta_box' ), |
| 87 | null, |
| 88 | null, |
| 89 | 'box-dashboard-left' |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | if ( self::should_render( 'cdn' ) ) { |
| 94 | $this->add_meta_box( |
| 95 | 'dashboard/cdn', |
| 96 | __( 'CDN', 'wp-smushit' ), |
| 97 | array( $this, 'cdn_meta_box' ), |
| 98 | array( $this, 'cdn_meta_box_header' ), |
| 99 | null, |
| 100 | 'box-dashboard-left' |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Meta boxes on the right side. |
| 106 | */ |
| 107 | if ( ! WP_Smush::is_pro() ) { |
| 108 | $this->add_meta_box( |
| 109 | 'dashboard/upsell/upsell', |
| 110 | __( 'Smush Pro', 'wp-smushit' ), |
| 111 | array( $this, 'upsell_meta_box' ), |
| 112 | array( $this, 'upsell_meta_box_header' ), |
| 113 | null, |
| 114 | 'box-dashboard-right' |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | if ( self::should_render( 'directory' ) ) { |
| 119 | $this->add_meta_box( |
| 120 | 'dashboard/directory', |
| 121 | __( 'Directory Smush', 'wp-smushit' ), |
| 122 | array( $this, 'directory_compress_meta_box' ), |
| 123 | null, |
| 124 | null, |
| 125 | 'box-dashboard-right' |
| 126 | ); |
| 127 | } |
| 128 | |
| 129 | if ( self::should_render( 'lazy_load' ) ) { |
| 130 | $this->add_meta_box( |
| 131 | 'dashboard/lazy-load', |
| 132 | __( 'Lazy Load', 'wp-smushit' ), |
| 133 | array( $this, 'lazy_load_meta_box' ), |
| 134 | null, |
| 135 | null, |
| 136 | 'box-dashboard-right' |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | if ( self::should_render( 'webp' ) ) { |
| 141 | $this->add_meta_box( |
| 142 | 'dashboard/webp', |
| 143 | __( 'Local WebP', 'wp-smushit' ), |
| 144 | array( $this, 'local_webp_meta_box' ), |
| 145 | array( $this, 'local_webp_meta_box_header' ), |
| 146 | null, |
| 147 | 'box-dashboard-right' |
| 148 | ); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Summary meta box. |
| 154 | * |
| 155 | * @since 3.8.6 |
| 156 | */ |
| 157 | public function summary_meta_box() { |
| 158 | $upsell_url_cdn = add_query_arg( |
| 159 | array( |
| 160 | 'utm_source' => 'smush', |
| 161 | 'utm_medium' => 'plugin', |
| 162 | 'utm_campaign' => 'summary_cdn', |
| 163 | ), |
| 164 | $this->upgrade_url |
| 165 | ); |
| 166 | |
| 167 | $upsell_url_webp = add_query_arg( |
| 168 | array( |
| 169 | 'utm_source' => 'smush', |
| 170 | 'utm_medium' => 'plugin', |
| 171 | 'utm_campaign' => 'summary_local_webp', |
| 172 | ), |
| 173 | $this->upgrade_url |
| 174 | ); |
| 175 | |
| 176 | $core = WP_Smush::get_instance()->core(); |
| 177 | $array_utils = new Array_Utils(); |
| 178 | $global_stats = $core->get_global_stats(); |
| 179 | |
| 180 | $args = array( |
| 181 | 'human_bytes' => $array_utils->get_array_value( $global_stats, 'human_bytes' ), |
| 182 | 'cdn_status' => WP_Smush::get_instance()->core()->mod->cdn->status(), |
| 183 | 'is_cdn' => $this->settings->get( 'cdn' ), |
| 184 | 'is_lazy_load' => $this->settings->get( 'lazy_load' ), |
| 185 | 'is_local_webp' => $this->settings->get( 'webp_mod' ), |
| 186 | 'resize_count' => $array_utils->get_array_value( $global_stats, 'count_resize' ), |
| 187 | 'total_optimized' => $array_utils->get_array_value( $global_stats, 'count_images' ), |
| 188 | 'stats_percent' => $array_utils->get_array_value( $global_stats, 'savings_percent' ), |
| 189 | 'upsell_url_cdn' => $upsell_url_cdn, |
| 190 | 'upsell_url_webp' => $upsell_url_webp, |
| 191 | 'webp_configured' => true === WP_Smush::get_instance()->core()->mod->webp->is_configured(), |
| 192 | 'percent_grade' => $array_utils->get_array_value( $global_stats, 'percent_grade' ), |
| 193 | 'percent_metric' => $array_utils->get_array_value( $global_stats, 'percent_metric' ), |
| 194 | 'percent_optimized' => $array_utils->get_array_value( $global_stats, 'percent_optimized' ), |
| 195 | ); |
| 196 | |
| 197 | $this->view( 'dashboard/summary-meta-box', $args ); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Bulk compress meta box. |
| 202 | * |
| 203 | * @since 3.8.6 |
| 204 | */ |
| 205 | public function bulk_compress_meta_box() { |
| 206 | $array_utils = new Array_Utils(); |
| 207 | $core = WP_Smush::get_instance()->core(); |
| 208 | $global_stats = $core->get_global_stats(); |
| 209 | $upsell_url = add_query_arg( |
| 210 | array( |
| 211 | 'utm_source' => 'smush', |
| 212 | 'utm_medium' => 'plugin', |
| 213 | 'utm_campaign' => 'dashboard_bulk_smush', |
| 214 | ), |
| 215 | $this->upgrade_url |
| 216 | ); |
| 217 | |
| 218 | $bg_optimization = WP_Smush::get_instance()->core()->mod->bg_optimization; |
| 219 | $background_processing_enabled = $bg_optimization->should_use_background(); |
| 220 | $background_in_processing = $background_processing_enabled && $bg_optimization->is_in_processing(); |
| 221 | |
| 222 | $args = array( |
| 223 | 'total_count' => (int) $array_utils->get_array_value( $global_stats, 'count_total' ), |
| 224 | 'uncompressed' => (int) $array_utils->get_array_value( $global_stats, 'remaining_count' ), |
| 225 | 'upsell_url' => $upsell_url, |
| 226 | 'background_processing_enabled' => $background_processing_enabled, |
| 227 | 'background_in_processing' => $background_in_processing, |
| 228 | 'background_in_processing_notice' => $bg_optimization->get_in_process_notice(), |
| 229 | ); |
| 230 | |
| 231 | $this->view( 'dashboard/bulk/meta-box', $args ); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Integrations meta box. |
| 236 | * |
| 237 | * @since 3.8.6 |
| 238 | */ |
| 239 | public function integrations_meta_box() { |
| 240 | $upsell_url = add_query_arg( |
| 241 | array( |
| 242 | 'utm_source' => 'smush', |
| 243 | 'utm_medium' => 'plugin', |
| 244 | 'utm_campaign' => 'dashboard_integrations', |
| 245 | ), |
| 246 | $this->upgrade_url |
| 247 | ); |
| 248 | |
| 249 | $integration_fields = $this->settings->get_integrations_fields(); |
| 250 | |
| 251 | $key = array_search( 'js_builder', $integration_fields, true ); |
| 252 | if ( $key ) { |
| 253 | unset( $integration_fields[ $key ] ); |
| 254 | } |
| 255 | |
| 256 | $args = array( |
| 257 | 'basic_features' => Settings::$basic_features, |
| 258 | 'fields' => $integration_fields, |
| 259 | 'is_pro' => WP_Smush::is_pro(), |
| 260 | 'settings' => $this->settings->get(), |
| 261 | 'upsell_url' => $upsell_url, |
| 262 | ); |
| 263 | |
| 264 | $this->view( 'dashboard/integrations-meta-box', $args ); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Local WebP meta box. |
| 269 | * |
| 270 | * @since 3.8.6 |
| 271 | */ |
| 272 | public function local_webp_meta_box() { |
| 273 | $upsell_url = add_query_arg( |
| 274 | array( |
| 275 | 'utm_source' => 'smush', |
| 276 | 'utm_medium' => 'plugin', |
| 277 | 'utm_campaign' => 'smush-dashboard-local-webp-upsell', |
| 278 | ), |
| 279 | $this->upgrade_url |
| 280 | ); |
| 281 | |
| 282 | $webp = WP_Smush::get_instance()->core()->mod->webp; |
| 283 | |
| 284 | $args = array( |
| 285 | 'is_configured' => $webp->get_is_configured_with_error_message(), |
| 286 | 'is_webp_active' => $this->settings->get( 'webp_mod' ), |
| 287 | 'upsell_url' => $upsell_url, |
| 288 | ); |
| 289 | |
| 290 | $this->view( 'dashboard/webp/meta-box', $args ); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Local WebP meta box footer. |
| 295 | * |
| 296 | * @since 3.8.6 |
| 297 | */ |
| 298 | public function local_webp_meta_box_header() { |
| 299 | $title = __( 'Local WebP', 'wp-smushit' ); |
| 300 | $this->view( 'dashboard/webp/meta-box-header', compact( 'title' ) ); |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Toole meta box. |
| 305 | * |
| 306 | * @since 3.8.6 |
| 307 | */ |
| 308 | public function tools_meta_box() { |
| 309 | $is_resize_detection = $this->settings->get( 'detection' ); |
| 310 | $this->view( 'dashboard/tools-meta-box', compact( 'is_resize_detection' ) ); |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Directory compress meta box. |
| 315 | * |
| 316 | * @since 3.8.6 |
| 317 | */ |
| 318 | public function directory_compress_meta_box() { |
| 319 | $images = WP_Smush::get_instance()->core()->mod->dir->get_image_errors(); |
| 320 | |
| 321 | $args = array( |
| 322 | 'images' => array_slice( $images, 0, 4 ), |
| 323 | 'errors' => WP_Smush::get_instance()->core()->mod->dir->get_image_errors_count(), |
| 324 | ); |
| 325 | |
| 326 | $this->view( 'dashboard/directory-meta-box', $args ); |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Upsell meta box. |
| 331 | * |
| 332 | * @since 3.8.6 |
| 333 | */ |
| 334 | public function upsell_meta_box() { |
| 335 | $upsell_url = add_query_arg( |
| 336 | array( |
| 337 | 'utm_source' => 'smush', |
| 338 | 'utm_medium' => 'plugin', |
| 339 | 'utm_campaign' => 'smush-dashboard-upsell', |
| 340 | ), |
| 341 | $this->upgrade_url |
| 342 | ); |
| 343 | |
| 344 | $this->view( 'dashboard/upsell/meta-box', compact( 'upsell_url' ) ); |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Upsell meta box header. |
| 349 | * |
| 350 | * @since 3.8.6 |
| 351 | */ |
| 352 | public function upsell_meta_box_header() { |
| 353 | $title = esc_html__( 'Smush Pro', 'wp-smushit' ); |
| 354 | $this->view( 'dashboard/upsell/meta-box-header', compact( 'title' ) ); |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Lazy load meta box. |
| 359 | * |
| 360 | * @since 3.8.6 |
| 361 | */ |
| 362 | public function lazy_load_meta_box() { |
| 363 | $settings = $this->settings->get_setting( 'wp-smush-lazy_load' ); |
| 364 | |
| 365 | $args = array( |
| 366 | 'is_lazy_load' => $this->settings->get( 'lazy_load' ), |
| 367 | 'media_types' => isset( $settings['format'] ) ? $settings['format'] : array(), |
| 368 | ); |
| 369 | |
| 370 | $this->view( 'dashboard/lazy-load-meta-box', $args ); |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * CDN meta box. |
| 375 | * |
| 376 | * @since 3.8.6 |
| 377 | */ |
| 378 | public function cdn_meta_box() { |
| 379 | $upsell_url = add_query_arg( |
| 380 | array( |
| 381 | 'utm_source' => 'smush', |
| 382 | 'utm_medium' => 'plugin', |
| 383 | 'utm_campaign' => 'smush-dashboard-cdn-upsell', |
| 384 | ), |
| 385 | $this->upgrade_url |
| 386 | ); |
| 387 | |
| 388 | $args = array( |
| 389 | 'cdn_status' => WP_Smush::get_instance()->core()->mod->cdn->status(), |
| 390 | 'is_webp' => $this->settings->get( 'webp' ), |
| 391 | 'upsell_url' => $upsell_url, |
| 392 | ); |
| 393 | |
| 394 | $this->view( 'dashboard/cdn/meta-box', $args ); |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * CDN meta box header. |
| 399 | * |
| 400 | * @since 3.8.6 |
| 401 | */ |
| 402 | public function cdn_meta_box_header() { |
| 403 | $title = esc_html__( 'CDN', 'wp-smushit' ); |
| 404 | $this->view( 'dashboard/cdn/meta-box-header', compact( 'title' ) ); |
| 405 | } |
| 406 | } |
| 407 |