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