PluginProbe ʕ •ᴥ•ʔ
TinyPNG – JPEG, PNG & WebP image compression / 3.0.0
TinyPNG – JPEG, PNG & WebP image compression v3.0.0
3.7.0 3.6.14 trunk 1.0.0 1.1.0 1.2.0 1.2.1 1.3.0 1.3.1 1.3.2 1.4.0 1.5.0 1.6.0 1.7.0 1.7.1 1.7.2 2.0.0 2.0.1 2.0.2 2.1.0 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 3.0.0 3.0.1 3.1.0 3.2.0 3.2.1 3.3 3.4 3.4.1 3.4.2 3.4.4 3.4.5 3.4.6 3.5.0 3.5.1 3.5.2 3.6.0 3.6.1 3.6.10 3.6.11 3.6.12 3.6.13 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.6.9
tiny-compress-images / src / class-tiny-plugin.php
tiny-compress-images / src Last commit date
config 7 years ago css 7 years ago data 10 years ago images 9 years ago js 7 years ago vendor 7 years ago views 7 years ago class-tiny-bulk-optimization.php 7 years ago class-tiny-compress-client.php 7 years ago class-tiny-compress-fopen.php 7 years ago class-tiny-compress.php 7 years ago class-tiny-exception.php 7 years ago class-tiny-image-size.php 7 years ago class-tiny-image.php 7 years ago class-tiny-notices.php 7 years ago class-tiny-php.php 7 years ago class-tiny-plugin.php 7 years ago class-tiny-settings.php 7 years ago class-tiny-wp-base.php 7 years ago
class-tiny-plugin.php
680 lines
1 <?php
2 /*
3 * Tiny Compress Images - WordPress plugin.
4 * Copyright (C) 2015-2018 Tinify B.V.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc., 51
18 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20 class Tiny_Plugin extends Tiny_WP_Base {
21 const VERSION = '3.0.0';
22 const MEDIA_COLUMN = self::NAME;
23 const DATETIME_FORMAT = 'Y-m-d G:i:s';
24
25 private static $version;
26
27 private $settings;
28 private $twig;
29
30 public static function jpeg_quality() {
31 return 85;
32 }
33
34 public static function version() {
35 /* Avoid using get_plugin_data() because it is not loaded early enough
36 in xmlrpc.php. */
37 return self::VERSION;
38 }
39
40 public function __construct() {
41 parent::__construct();
42
43 $this->settings = new Tiny_Settings();
44 }
45
46 public function set_compressor( $compressor ) {
47 $this->settings->set_compressor( $compressor );
48 }
49
50 public function init() {
51 add_filter( 'jpeg_quality',
52 $this->get_static_method( 'jpeg_quality' )
53 );
54
55 add_filter( 'wp_editor_set_quality',
56 $this->get_static_method( 'jpeg_quality' )
57 );
58
59 add_filter( 'wp_generate_attachment_metadata',
60 $this->get_method( 'process_attachment' ),
61 10, 2
62 );
63
64 add_filter( 'wp_ajax_nopriv_tiny_rpc',
65 $this->get_method( 'process_rpc_request' )
66 );
67
68 load_plugin_textdomain( self::NAME, false,
69 dirname( plugin_basename( __FILE__ ) ) . '/languages'
70 );
71 }
72
73 public function admin_init() {
74 add_action('wp_dashboard_setup',
75 $this->get_method( 'add_dashboard_widget' )
76 );
77
78 add_action( 'admin_enqueue_scripts',
79 $this->get_method( 'enqueue_scripts' )
80 );
81
82 add_action( 'admin_action_tiny_bulk_action',
83 $this->get_method( 'media_library_bulk_action' )
84 );
85
86 add_action( 'admin_action_-1',
87 $this->get_method( 'media_library_bulk_action' )
88 );
89
90 add_filter( 'manage_media_columns',
91 $this->get_method( 'add_media_columns' )
92 );
93
94 add_action( 'manage_media_custom_column',
95 $this->get_method( 'render_media_column' ),
96 10, 2
97 );
98
99 add_action( 'attachment_submitbox_misc_actions',
100 $this->get_method( 'show_media_info' )
101 );
102
103 add_filter( 'wp_ajax_tiny_async_optimize_upload_new_media',
104 $this->get_method( 'compress_on_upload' )
105 );
106
107 add_action( 'wp_ajax_tiny_compress_image_from_library',
108 $this->get_method( 'compress_image_from_library' )
109 );
110
111 add_action( 'wp_ajax_tiny_compress_image_for_bulk',
112 $this->get_method( 'compress_image_for_bulk' )
113 );
114
115 add_action( 'wp_ajax_tiny_get_optimization_statistics',
116 $this->get_method( 'ajax_optimization_statistics' )
117 );
118
119 add_action( 'wp_ajax_tiny_get_compression_status',
120 $this->get_method( 'ajax_compression_status' )
121 );
122
123 $plugin = plugin_basename(
124 dirname( dirname( __FILE__ ) ) . '/tiny-compress-images.php'
125 );
126
127 add_filter( "plugin_action_links_$plugin",
128 $this->get_method( 'add_plugin_links' )
129 );
130
131 add_action( 'wr2x_retina_file_added',
132 $this->get_method( 'compress_retina_image' ),
133 10, 3
134 );
135
136 add_action( 'wr2x_retina_file_removed',
137 $this->get_method( 'remove_retina_image' ),
138 10, 2
139 );
140
141 add_thickbox();
142 }
143
144 public function admin_menu() {
145 add_media_page(
146 __( 'Bulk Optimization', 'tiny-compress-images' ),
147 esc_html__( 'Bulk Optimization', 'tiny-compress-images' ),
148 'upload_files',
149 'tiny-bulk-optimization',
150 $this->get_method( 'render_bulk_optimization_page' )
151 );
152 }
153
154 public function add_plugin_links( $current_links ) {
155 $additional = array(
156 'settings' => sprintf(
157 '<a href="options-general.php?page=tinify">%s</a>',
158 esc_html__( 'Settings', 'tiny-compress-images' )
159 ),
160 'bulk' => sprintf(
161 '<a href="upload.php?page=tiny-bulk-optimization">%s</a>',
162 esc_html__( 'Bulk Optimization', 'tiny-compress-images' )
163 ),
164 );
165 return array_merge( $additional, $current_links );
166 }
167
168 public function compress_retina_image( $attachment_id, $path, $size_name ) {
169 if ( $this->settings->compress_wr2x_images() ) {
170 $tiny_image = new Tiny_Image( $this->settings, $attachment_id );
171 $tiny_image->compress_retina( $size_name . '_wr2x', $path );
172 }
173 }
174
175 public function remove_retina_image( $attachment_id, $path ) {
176 $tiny_image = new Tiny_Image( $this->settings, $attachment_id );
177 $tiny_image->remove_retina_metadata();
178 }
179
180 public function enqueue_scripts( $hook ) {
181 wp_enqueue_style( self::NAME . '_admin',
182 plugins_url( '/css/admin.css', __FILE__ ),
183 array(), self::version()
184 );
185
186 wp_enqueue_style( self::NAME . '_chart',
187 plugins_url( '/css/chart.css', __FILE__ ),
188 array(), self::version()
189 );
190
191 wp_register_script( self::NAME . '_admin',
192 plugins_url( '/js/admin.js', __FILE__ ),
193 array(), self::version(), true
194 );
195
196 // WordPress < 3.3 does not handle multidimensional arrays
197 wp_localize_script( self::NAME . '_admin', 'tinyCompress', array(
198 'nonce' => wp_create_nonce( 'tiny-compress' ),
199 'wpVersion' => self::wp_version(),
200 'pluginVersion' => self::version(),
201 'L10nAllDone' => __( 'All images are processed', 'tiny-compress-images' ),
202 'L10nNoActionTaken' => __( 'No action taken', 'tiny-compress-images' ),
203 'L10nBulkAction' => __( 'Compress Images', 'tiny-compress-images' ),
204 'L10nCancelled' => __( 'Cancelled', 'tiny-compress-images' ),
205 'L10nCompressing' => __( 'Compressing', 'tiny-compress-images' ),
206 'L10nCompressed' => __( 'compressed', 'tiny-compress-images' ),
207 'L10nFile' => __( 'File', 'tiny-compress-images' ),
208 'L10nSizesOptimized' => __( 'Sizes optimized', 'tiny-compress-images' ),
209 'L10nInitialSize' => __( 'Initial size', 'tiny-compress-images' ),
210 'L10nCurrentSize' => __( 'Current size', 'tiny-compress-images' ),
211 'L10nSavings' => __( 'Savings', 'tiny-compress-images' ),
212 'L10nStatus' => __( 'Status', 'tiny-compress-images' ),
213 'L10nShowMoreDetails' => __( 'Show more details', 'tiny-compress-images' ),
214 'L10nError' => __( 'Error', 'tiny-compress-images' ),
215 'L10nLatestError' => __( 'Latest error', 'tiny-compress-images' ),
216 'L10nInternalError' => __( 'Internal error', 'tiny-compress-images' ),
217 'L10nOutOf' => __( 'out of', 'tiny-compress-images' ),
218 'L10nWaiting' => __( 'Waiting', 'tiny-compress-images' ),
219 ));
220
221 wp_enqueue_script( self::NAME . '_admin' );
222
223 if ( 'media_page_tiny-bulk-optimization' == $hook ) {
224 wp_enqueue_style(
225 self::NAME . '_tiny_bulk_optimization',
226 plugins_url( '/css/bulk-optimization.css', __FILE__ ),
227 array(), self::version()
228 );
229
230 wp_enqueue_style( self::NAME . '_chart',
231 plugins_url( '/css/chart.css', __FILE__ ),
232 array(), self::version()
233 );
234
235 wp_register_script(
236 self::NAME . '_tiny_bulk_optimization',
237 plugins_url( '/js/bulk-optimization.js', __FILE__ ),
238 array(), self::version(), true
239 );
240
241 wp_enqueue_script( self::NAME . '_tiny_bulk_optimization' );
242 }
243 }
244
245 public function process_attachment( $metadata, $attachment_id ) {
246 if ( $this->settings->auto_compress_enabled() ) {
247 if (
248 $this->settings->background_compress_enabled() &&
249 ! $this->settings->remove_local_files_setting_enabled()
250 ) {
251 $this->async_compress_on_upload( $metadata, $attachment_id );
252 } else {
253 return $this->blocking_compress_on_upload( $metadata, $attachment_id );
254 }
255 }
256
257 return $metadata;
258 }
259
260 public function blocking_compress_on_upload( $metadata, $attachment_id ) {
261 if ( ! empty( $metadata ) ) {
262 $tiny_image = new Tiny_Image( $this->settings, $attachment_id, $metadata );
263 $result = $tiny_image->compress( $this->settings );
264 return $tiny_image->get_wp_metadata();
265 } else {
266 return $metadata;
267 }
268 }
269
270 public function async_compress_on_upload( $metadata, $attachment_id ) {
271 $context = 'wp';
272 $action = 'tiny_async_optimize_upload_new_media';
273 $_ajax_nonce = wp_create_nonce( 'new_media-' . $attachment_id );
274 $body = compact( 'action', '_ajax_nonce', 'metadata', 'attachment_id', 'context' );
275
276 $args = array(
277 'timeout' => 0.01,
278 'blocking' => false,
279 'body' => $body,
280 'cookies' => isset( $_COOKIE ) && is_array( $_COOKIE ) ? $_COOKIE : array(),
281 'sslverify' => apply_filters( 'https_local_ssl_verify', false ),
282 );
283
284 if ( defined( 'XMLRPC_REQUEST' ) && get_current_user_id() ) {
285 /* We generate a hash to be used for the transient we use to store the current user. */
286 $rpc_hash = md5( maybe_serialize( $body ) );
287
288 $args['body']['tiny_rpc_action'] = $args['body']['action'];
289 /* We set a different action to make sure that all RPC requests are first validated. */
290 $args['body']['action'] = 'tiny_rpc';
291 $args['body']['tiny_rpc_hash'] = $rpc_hash;
292 $args['body']['tiny_rpc_nonce'] = wp_create_nonce( 'tiny_rpc_' . $rpc_hash );
293
294 /*
295 We can't use cookies here, so we save the user id in a transient
296 so that we can retrieve it again when processing the RPC request.
297 We should be able to use a relatively short timeout, as the request
298 should be processed directly afterwards.
299 */
300 set_transient( 'tiny_rpc_' . $rpc_hash, get_current_user_id(), 10 );
301 }
302
303 if ( getenv( 'WORDPRESS_HOST' ) !== false ) {
304 wp_remote_post( getenv( 'WORDPRESS_HOST' ) . '/wp-admin/admin-ajax.php', $args );
305 } else {
306 wp_remote_post( admin_url( 'admin-ajax.php' ), $args );
307 }
308 }
309
310 public function process_rpc_request() {
311 if (
312 empty( $_POST['tiny_rpc_action'] ) ||
313 empty( $_POST['tiny_rpc_hash'] ) ||
314 32 !== strlen( $_POST['tiny_rpc_hash'] )
315 ) {
316 exit();
317 }
318
319 $rpc_hash = sanitize_key( $_POST['tiny_rpc_hash'] );
320 $user_id = absint( get_transient( 'tiny_rpc_' . $rpc_hash ) );
321 $user = $user_id ? get_userdata( $user_id ) : false;
322
323 /* We no longer need the transient. */
324 delete_transient( 'tiny_rpc_' . $rpc_hash );
325
326 if ( ! $user || ! $user->exists() ) {
327 exit();
328 }
329 wp_set_current_user( $user_id );
330
331 if ( ! check_ajax_referer( 'tiny_rpc_' . $rpc_hash, 'tiny_rpc_nonce', false ) ) {
332 exit();
333 }
334
335 /* Now that everything is checked, perform the actual action. */
336 $action = $_POST['tiny_rpc_action'];
337 unset(
338 $_POST['action'],
339 $_POST['tiny_rpc_action'],
340 $_POST['tiny_rpc_id'],
341 $_POST['tiny_rpc_nonce']
342 );
343 do_action( 'wp_ajax_' . $action );
344 }
345
346 public function compress_on_upload() {
347 $attachment_id = intval( $_POST['attachment_id'] );
348 $metadata = $_POST['metadata'];
349 if ( is_array( $metadata ) ) {
350 $tiny_image = new Tiny_Image( $this->settings, $attachment_id, $metadata );
351 $result = $tiny_image->compress( $this->settings );
352 // The wp_update_attachment_metadata call is thrown because the
353 // dimensions of the original image can change. This will then
354 // trigger other plugins and can result in unexpected behaviour and
355 // further changes to the image. This may require another approach.
356 wp_update_attachment_metadata( $attachment_id, $tiny_image->get_wp_metadata() );
357 }
358 exit();
359 }
360
361 public function compress_image_from_library() {
362 if ( ! $this->check_ajax_referer() ) {
363 exit();
364 }
365 if ( ! current_user_can( 'upload_files' ) ) {
366 $message = esc_html__(
367 "You don't have permission to upload files.",
368 'tiny-compress-images'
369 );
370 echo $message;
371 exit();
372 }
373 if ( empty( $_POST['id'] ) ) {
374 $message = esc_html__(
375 'Not a valid media file.',
376 'tiny-compress-images'
377 );
378 echo $message;
379 exit();
380 }
381 $id = intval( $_POST['id'] );
382 $metadata = wp_get_attachment_metadata( $id );
383 if ( ! is_array( $metadata ) ) {
384 $message = esc_html__(
385 'Could not find metadata of media file.',
386 'tiny-compress-images'
387 );
388 echo $message;
389 exit;
390 }
391
392 $tiny_image = new Tiny_Image( $this->settings, $id, $metadata );
393 $result = $tiny_image->compress( $this->settings );
394
395 // The wp_update_attachment_metadata call is thrown because the
396 // dimensions of the original image can change. This will then
397 // trigger other plugins and can result in unexpected behaviour and
398 // further changes to the image. This may require another approach.
399 wp_update_attachment_metadata( $id, $tiny_image->get_wp_metadata() );
400
401 echo $this->render_compress_details( $tiny_image );
402
403 exit();
404 }
405
406 public function compress_image_for_bulk() {
407 if ( ! $this->check_ajax_referer() ) {
408 exit();
409 }
410 if ( ! current_user_can( 'upload_files' ) ) {
411 $message = esc_html__(
412 "You don't have permission to upload files.",
413 'tiny-compress-images'
414 );
415 echo json_encode( array(
416 'error' => $message,
417 ) );
418 exit();
419 }
420 if ( empty( $_POST['id'] ) ) {
421 $message = esc_html__(
422 'Not a valid media file.',
423 'tiny-compress-images'
424 );
425 echo json_encode( array(
426 'error' => $message,
427 ) );
428 exit();
429 }
430 $id = intval( $_POST['id'] );
431 $metadata = wp_get_attachment_metadata( $id );
432 if ( ! is_array( $metadata ) ) {
433 $message = esc_html__(
434 'Could not find metadata of media file.',
435 'tiny-compress-images'
436 );
437 echo json_encode( array(
438 'error' => $message,
439 ) );
440 exit;
441 }
442
443 $tiny_image_before = new Tiny_Image( $this->settings, $id, $metadata );
444 $image_statistics_before = $tiny_image_before->get_statistics(
445 $this->settings->get_sizes(),
446 $this->settings->get_active_tinify_sizes()
447 );
448 $size_before = $image_statistics_before['optimized_total_size'];
449
450 $tiny_image = new Tiny_Image( $this->settings, $id, $metadata );
451 $result = $tiny_image->compress( $this->settings );
452 $image_statistics = $tiny_image->get_statistics(
453 $this->settings->get_sizes(),
454 $this->settings->get_active_tinify_sizes()
455 );
456 wp_update_attachment_metadata( $id, $tiny_image->get_wp_metadata() );
457
458 $current_library_size = intval( $_POST['current_size'] );
459 $size_after = $image_statistics['optimized_total_size'];
460 $new_library_size = $current_library_size + $size_after - $size_before;
461
462 $result['message'] = $tiny_image->get_latest_error();
463 $result['image_sizes_optimized'] = $image_statistics['image_sizes_optimized'];
464
465 $result['initial_total_size'] = size_format(
466 $image_statistics['initial_total_size'], 1
467 );
468
469 $result['optimized_total_size'] = size_format(
470 $image_statistics['optimized_total_size'], 1
471 );
472
473 $result['savings'] = $tiny_image->get_savings( $image_statistics );
474 $result['status'] = $this->settings->get_status();
475 $result['thumbnail'] = wp_get_attachment_image(
476 $id, array( '30', '30' ), true, array(
477 'class' => 'pinkynail',
478 'alt' => '',
479 )
480 );
481 $result['size_change'] = $size_after - $size_before;
482 $result['human_readable_library_size'] = size_format( $new_library_size, 2 );
483
484 echo json_encode( $result );
485
486 exit();
487 }
488
489 public function ajax_optimization_statistics() {
490 if ( ! $this->check_ajax_referer() ) {
491 exit();
492 }
493 $stats = Tiny_Bulk_Optimization::get_optimization_statistics( $this->settings );
494 echo json_encode( $stats );
495 exit();
496 }
497
498 public function ajax_compression_status() {
499 if ( ! $this->check_ajax_referer() ) {
500 exit();
501 }
502
503 if ( empty( $_POST['id'] ) ) {
504 $message = esc_html__(
505 'Not a valid media file.',
506 'tiny-compress-images'
507 );
508 echo $message;
509 exit();
510 }
511 $id = intval( $_POST['id'] );
512 $metadata = wp_get_attachment_metadata( $id );
513 if ( ! is_array( $metadata ) ) {
514 $message = esc_html__(
515 'Could not find metadata of media file.',
516 'tiny-compress-images'
517 );
518 echo $message;
519 exit;
520 }
521
522 $tiny_image = new Tiny_Image( $this->settings, $id, $metadata );
523
524 echo $this->render_compress_details( $tiny_image );
525
526 exit();
527 }
528
529 public function media_library_bulk_action() {
530 if ( empty( $_REQUEST['action'] ) || (
531 'tiny_bulk_action' != $_REQUEST['action'] &&
532 'tiny_bulk_action' != $_REQUEST['action2'] ) ) {
533 return;
534 }
535 if ( empty( $_REQUEST['media'] ) || ( ! $_REQUEST['media'] ) ) {
536 $_REQUEST['action'] = '';
537 return;
538 }
539 check_admin_referer( 'bulk-media' );
540 $ids = implode( '-', array_map( 'intval', $_REQUEST['media'] ) );
541 $location = 'upload.php?mode=list&ids=' . $ids;
542
543 if ( ! empty( $_REQUEST['paged'] ) ) {
544 $location = add_query_arg( 'paged', absint( $_REQUEST['paged'] ), $location );
545 }
546 if ( ! empty( $_REQUEST['s'] ) ) {
547 $location = add_query_arg( 's', $_REQUEST['s'], $location );
548 }
549 if ( ! empty( $_REQUEST['m'] ) ) {
550 $location = add_query_arg( 'm', $_REQUEST['m'], $location );
551 }
552
553 wp_redirect( admin_url( $location ) );
554 exit();
555 }
556
557 public function add_media_columns( $columns ) {
558 $columns[ self::MEDIA_COLUMN ] = esc_html__( 'Compression', 'tiny-compress-images' );
559 return $columns;
560 }
561
562 public function render_media_column( $column, $id ) {
563 if ( self::MEDIA_COLUMN === $column ) {
564 $tiny_image = new Tiny_Image( $this->settings, $id );
565 if ( $tiny_image->file_type_allowed() ) {
566 echo '<div class="tiny-ajax-container">';
567 $this->render_compress_details( $tiny_image );
568 echo '</div>';
569 }
570 }
571 }
572
573 public function show_media_info() {
574 global $post;
575 $tiny_image = new Tiny_Image( $this->settings, $post->ID );
576 if ( $tiny_image->file_type_allowed() ) {
577 echo '<div class="misc-pub-section tiny-compress-images">';
578 echo '<h4>';
579 esc_html_e( 'JPEG and PNG optimization', 'tiny-compress-images' );
580 echo '</h4>';
581 echo '<div class="tiny-ajax-container">';
582 $this->render_compress_details( $tiny_image );
583 echo '</div>';
584 echo '</div>';
585 }
586 }
587
588 private function render_compress_details( $tiny_image ) {
589 $in_progress = $tiny_image->filter_image_sizes( 'in_progress' );
590 if ( count( $in_progress ) > 0 ) {
591 include( dirname( __FILE__ ) . '/views/compress-details-processing.php' );
592 } else {
593 include( dirname( __FILE__ ) . '/views/compress-details.php' );
594 }
595 }
596
597 public function render_bulk_optimization_page() {
598 $stats = Tiny_Bulk_Optimization::get_optimization_statistics( $this->settings );
599 $estimated_costs = Tiny_Compress::estimate_cost(
600 $stats['available-unoptimised-sizes'],
601 $this->settings->get_compression_count()
602 );
603 $admin_colors = self::retrieve_admin_colors();
604
605 $active_tinify_sizes = $this->settings->get_active_tinify_sizes();
606
607 include( dirname( __FILE__ ) . '/views/bulk-optimization.php' );
608 }
609
610 public function add_dashboard_widget() {
611 wp_enqueue_style( self::NAME . '_chart',
612 plugins_url( '/css/chart.css', __FILE__ ),
613 array(), self::version()
614 );
615
616 wp_enqueue_style( self::NAME . '_dashboard_widget',
617 plugins_url( '/css/dashboard-widget.css', __FILE__ ),
618 array(), self::version()
619 );
620
621 wp_register_script( self::NAME . '_dashboard_widget',
622 plugins_url( '/js/dashboard-widget.js', __FILE__ ),
623 array(), self::version(), true
624 );
625
626 /* This might be deduplicated with the admin script localization, but
627 the order of including scripts is sometimes different. So in that
628 case we need to make sure that the order of inclusion is correc.t */
629 wp_localize_script( self::NAME . '_dashboard_widget', 'tinyCompressDashboard', array(
630 'nonce' => wp_create_nonce( 'tiny-compress' ),
631 ));
632
633 wp_enqueue_script( self::NAME . '_dashboard_widget' );
634
635 wp_add_dashboard_widget(
636 $this->get_prefixed_name( 'dashboard_widget' ),
637 esc_html__( 'Compress JPEG & PNG images', 'tiny-compress-images' ),
638 $this->get_method( 'add_widget_view' )
639 );
640 }
641
642 function add_widget_view() {
643 $admin_colors = self::retrieve_admin_colors();
644 include( dirname( __FILE__ ) . '/views/dashboard-widget.php' );
645 }
646
647 private static function retrieve_admin_colors() {
648 global $_wp_admin_css_colors;
649 $admin_colour_scheme = get_user_option( 'admin_color', get_current_user_id() );
650 $admin_colors = array( '#0074aa', '#1685b5', '#78ca44', '#0086ba' ); // default
651 if ( isset( $_wp_admin_css_colors[ $admin_colour_scheme ] ) ) {
652 if ( isset( $_wp_admin_css_colors[ $admin_colour_scheme ]->colors ) ) {
653 $admin_colors = $_wp_admin_css_colors[ $admin_colour_scheme ]->colors;
654 }
655 }
656 if ( '#e5e5e5' == $admin_colors[0] && '#999' == $admin_colors[1] ) {
657 $admin_colors[0] = '#bbb';
658 }
659 if ( '#5589aa' == $admin_colors[0] && '#cfdfe9' == $admin_colors[1] ) {
660 $admin_colors[1] = '#85aec5';
661 }
662 if ( '#7c7976' == $admin_colors[0] && '#c6c6c6' == $admin_colors[1] ) {
663 $admin_colors[1] = '#adaba9';
664 $admin_colors[2] = '#adaba9';
665 }
666 if ( self::wp_version() > 3.7 ) {
667 if ( 'fresh' == $admin_colour_scheme ) {
668 $admin_colors = array( '#0074aa', '#1685b5', '#78ca44', '#0086ba' ); // better
669 }
670 }
671 return $admin_colors;
672 }
673
674 function friendly_user_name() {
675 $user = wp_get_current_user();
676 $name = ucfirst( empty( $user->first_name ) ? $user->display_name : $user->first_name );
677 return $name;
678 }
679 }
680