PluginProbe ʕ •ᴥ•ʔ
TinyPNG – JPEG, PNG & WebP image compression / 3.4.4
TinyPNG – JPEG, PNG & WebP image compression v3.4.4
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 3 years ago config 3 years ago css 3 years ago data 3 years ago images 3 years ago js 3 years ago vendor 2 years ago views 2 years ago class-tiny-bulk-optimization.php 2 years ago class-tiny-compress-client.php 3 years ago class-tiny-compress-fopen.php 3 years ago class-tiny-compress.php 3 years ago class-tiny-exception.php 3 years ago class-tiny-image-size.php 3 years ago class-tiny-image.php 3 years ago class-tiny-notices.php 3 years ago class-tiny-php.php 3 years ago class-tiny-plugin.php 1 year ago class-tiny-settings.php 1 year ago class-tiny-wp-base.php 3 years ago
class-tiny-plugin.php
717 lines
1 <?php
2 /*
3 * Tiny Compress Images - WordPress plugin.
4 * Copyright (C) 2015-2023 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.4.3';
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();
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 (!wp_verify_nonce($_POST['_ajax_nonce'], 'new_media-' . $_POST['attachment_id'])) {
369 exit;
370 }
371 if ( current_user_can( 'upload_files' ) ) {
372 $attachment_id = intval( $_POST['attachment_id'] );
373 $metadata = $_POST['metadata'];
374 if ( is_array( $metadata ) ) {
375 $tiny_image = new Tiny_Image( $this->settings, $attachment_id, $metadata );
376 $result = $tiny_image->compress();
377 // The wp_update_attachment_metadata call is thrown because the
378 // dimensions of the original image can change. This will then
379 // trigger other plugins and can result in unexpected behaviour and
380 // further changes to the image. This may require another approach.
381 // Note that as of WP 5.3 it is advised to not hook into this filter
382 // anymore, so other plugins are less likely to be triggered.
383 wp_update_attachment_metadata( $attachment_id, $tiny_image->get_wp_metadata() );
384 }
385 }
386 exit();
387 }
388
389 public function compress_image_from_library() {
390 if ( ! $this->check_ajax_referer() ) {
391 exit();
392 }
393 if ( ! current_user_can( 'upload_files' ) ) {
394 $message = esc_html__(
395 "You don't have permission to upload files.",
396 'tiny-compress-images'
397 );
398 echo $message;
399 exit();
400 }
401 if ( empty( $_POST['id'] ) ) {
402 $message = esc_html__(
403 'Not a valid media file.',
404 'tiny-compress-images'
405 );
406 echo $message;
407 exit();
408 }
409 $id = intval( $_POST['id'] );
410 $metadata = wp_get_attachment_metadata( $id );
411 if ( ! is_array( $metadata ) ) {
412 $message = esc_html__(
413 'Could not find metadata of media file.',
414 'tiny-compress-images'
415 );
416 echo $message;
417 exit;
418 }
419
420 $tiny_image = new Tiny_Image( $this->settings, $id, $metadata );
421 $result = $tiny_image->compress();
422
423 // The wp_update_attachment_metadata call is thrown because the
424 // dimensions of the original image can change. This will then
425 // trigger other plugins and can result in unexpected behaviour and
426 // further changes to the image. This may require another approach.
427 // Note that as of WP 5.3 it is advised to not hook into this filter
428 // anymore, so other plugins are less likely to be triggered.
429 wp_update_attachment_metadata( $id, $tiny_image->get_wp_metadata() );
430
431 echo $this->render_compress_details( $tiny_image );
432
433 exit();
434 }
435
436 public function compress_image_for_bulk() {
437 if ( ! $this->check_ajax_referer() ) {
438 exit();
439 }
440 if ( ! current_user_can( 'upload_files' ) ) {
441 $message = esc_html__(
442 "You don't have permission to upload files.",
443 'tiny-compress-images'
444 );
445 echo json_encode( array(
446 'error' => $message,
447 ) );
448 exit();
449 }
450 if ( empty( $_POST['id'] ) ) {
451 $message = esc_html__(
452 'Not a valid media file.',
453 'tiny-compress-images'
454 );
455 echo json_encode( array(
456 'error' => $message,
457 ) );
458 exit();
459 }
460 $id = intval( $_POST['id'] );
461 $metadata = wp_get_attachment_metadata( $id );
462 if ( ! is_array( $metadata ) ) {
463 $message = esc_html__(
464 'Could not find metadata of media file.',
465 'tiny-compress-images'
466 );
467 echo json_encode( array(
468 'error' => $message,
469 ) );
470 exit;
471 }
472
473 $tiny_image_before = new Tiny_Image( $this->settings, $id, $metadata );
474 $image_statistics_before = $tiny_image_before->get_statistics(
475 $this->settings->get_sizes(),
476 $this->settings->get_active_tinify_sizes()
477 );
478 $size_before = $image_statistics_before['optimized_total_size'];
479
480 $tiny_image = new Tiny_Image( $this->settings, $id, $metadata );
481 $result = $tiny_image->compress();
482 $image_statistics = $tiny_image->get_statistics(
483 $this->settings->get_sizes(),
484 $this->settings->get_active_tinify_sizes()
485 );
486 wp_update_attachment_metadata( $id, $tiny_image->get_wp_metadata() );
487
488 $current_library_size = intval( $_POST['current_size'] );
489 $size_after = $image_statistics['optimized_total_size'];
490 $new_library_size = $current_library_size + $size_after - $size_before;
491
492 $result['message'] = $tiny_image->get_latest_error();
493 $result['image_sizes_optimized'] = $image_statistics['image_sizes_optimized'];
494
495 $result['initial_total_size'] = size_format(
496 $image_statistics['initial_total_size'], 1
497 );
498
499 $result['optimized_total_size'] = size_format(
500 $image_statistics['optimized_total_size'], 1
501 );
502
503 $result['savings'] = $tiny_image->get_savings( $image_statistics );
504 $result['status'] = $this->settings->get_status();
505 $result['thumbnail'] = wp_get_attachment_image(
506 $id, array( '30', '30' ), true, array(
507 'class' => 'pinkynail',
508 'alt' => '',
509 )
510 );
511 $result['size_change'] = $size_after - $size_before;
512 $result['human_readable_library_size'] = size_format( $new_library_size, 2 );
513
514 echo json_encode( $result );
515
516 exit();
517 }
518
519 public function ajax_optimization_statistics() {
520 if ( $this->check_ajax_referer() && current_user_can( 'upload_files' ) ) {
521 $stats = Tiny_Bulk_Optimization::get_optimization_statistics( $this->settings );
522 echo json_encode( $stats );
523 }
524 exit();
525 }
526
527 public function ajax_compression_status() {
528 if ( ! $this->check_ajax_referer() ) {
529 exit();
530 }
531 if ( ! current_user_can( 'upload_files' ) ) {
532 exit();
533 }
534 if ( empty( $_POST['id'] ) ) {
535 $message = esc_html__(
536 'Not a valid media file.',
537 'tiny-compress-images'
538 );
539 echo $message;
540 exit();
541 }
542 $id = intval( $_POST['id'] );
543 $metadata = wp_get_attachment_metadata( $id );
544 if ( ! is_array( $metadata ) ) {
545 $message = esc_html__(
546 'Could not find metadata of media file.',
547 'tiny-compress-images'
548 );
549 echo $message;
550 exit;
551 }
552
553 $tiny_image = new Tiny_Image( $this->settings, $id, $metadata );
554
555 echo $this->render_compress_details( $tiny_image );
556
557 exit();
558 }
559
560 public function media_library_bulk_action() {
561 if ( empty( $_REQUEST['action'] ) || (
562 'tiny_bulk_action' != $_REQUEST['action'] &&
563 'tiny_bulk_action' != $_REQUEST['action2'] ) ) {
564 return;
565 }
566 if ( empty( $_REQUEST['media'] ) || ( ! $_REQUEST['media'] ) ) {
567 $_REQUEST['action'] = '';
568 return;
569 }
570 check_admin_referer( 'bulk-media' );
571 $ids = implode( '-', array_map( 'intval', $_REQUEST['media'] ) );
572 $location = 'upload.php?mode=list&ids=' . $ids;
573
574 if ( ! empty( $_REQUEST['paged'] ) ) {
575 $location = add_query_arg( 'paged', absint( $_REQUEST['paged'] ), $location );
576 }
577 if ( ! empty( $_REQUEST['s'] ) ) {
578 $location = add_query_arg( 's', $_REQUEST['s'], $location );
579 }
580 if ( ! empty( $_REQUEST['m'] ) ) {
581 $location = add_query_arg( 'm', $_REQUEST['m'], $location );
582 }
583
584 wp_redirect( admin_url( $location ) );
585 exit();
586 }
587
588 public function add_media_columns( $columns ) {
589 $columns[ self::MEDIA_COLUMN ] = esc_html__( 'Compression', 'tiny-compress-images' );
590 return $columns;
591 }
592
593 public function render_media_column( $column, $id ) {
594 if ( self::MEDIA_COLUMN === $column ) {
595 $tiny_image = new Tiny_Image( $this->settings, $id );
596 if ( $tiny_image->file_type_allowed() ) {
597 echo '<div class="tiny-ajax-container">';
598 $this->render_compress_details( $tiny_image );
599 echo '</div>';
600 }
601 }
602 }
603
604 public function show_media_info() {
605 global $post;
606 $tiny_image = new Tiny_Image( $this->settings, $post->ID );
607 if ( $tiny_image->file_type_allowed() ) {
608 echo '<div class="misc-pub-section tiny-compress-images">';
609 echo '<h4>';
610 esc_html_e( 'JPEG, PNG, & WebP optimization', 'tiny-compress-images' );
611 echo '</h4>';
612 echo '<div class="tiny-ajax-container">';
613 $this->render_compress_details( $tiny_image );
614 echo '</div>';
615 echo '</div>';
616 }
617 }
618
619 private function render_compress_details( $tiny_image ) {
620 $in_progress = $tiny_image->filter_image_sizes( 'in_progress' );
621 if ( count( $in_progress ) > 0 ) {
622 include( dirname( __FILE__ ) . '/views/compress-details-processing.php' );
623 } else {
624 include( dirname( __FILE__ ) . '/views/compress-details.php' );
625 }
626 }
627
628 public function render_bulk_optimization_page() {
629 $stats = Tiny_Bulk_Optimization::get_optimization_statistics( $this->settings );
630 $estimated_costs = Tiny_Compress::estimate_cost(
631 $stats['available-unoptimised-sizes'],
632 $this->settings->get_compression_count()
633 );
634 $admin_colors = self::retrieve_admin_colors();
635
636 /* This makes sure that up to date information is retrieved from the API. */
637 $this->settings->get_compressor()->get_status();
638
639 $active_tinify_sizes = $this->settings->get_active_tinify_sizes();
640 $remaining_credits = $this->settings->get_remaining_credits();
641 $is_on_free_plan = $this->settings->is_on_free_plan();
642 $email_address = $this->settings->get_email_address();
643
644 include( dirname( __FILE__ ) . '/views/bulk-optimization.php' );
645 }
646
647 public function add_dashboard_widget() {
648 wp_enqueue_style( self::NAME . '_chart',
649 plugins_url( '/css/optimization-chart.css', __FILE__ ),
650 array(), self::version()
651 );
652
653 wp_enqueue_style( self::NAME . '_dashboard_widget',
654 plugins_url( '/css/dashboard-widget.css', __FILE__ ),
655 array(), self::version()
656 );
657
658 wp_register_script( self::NAME . '_dashboard_widget',
659 plugins_url( '/js/dashboard-widget.js', __FILE__ ),
660 array(), self::version(), true
661 );
662
663 /* This might be deduplicated with the admin script localization, but
664 the order of including scripts is sometimes different. So in that
665 case we need to make sure that the order of inclusion is correc.t */
666 wp_localize_script( self::NAME . '_dashboard_widget', 'tinyCompressDashboard', array(
667 'nonce' => wp_create_nonce( 'tiny-compress' ),
668 ));
669
670 wp_enqueue_script( self::NAME . '_dashboard_widget' );
671
672 wp_add_dashboard_widget(
673 $this->get_prefixed_name( 'dashboard_widget' ),
674 esc_html__( 'TinyPNG - JPEG, PNG & WebP image compression', 'tiny-compress-images' ),
675 $this->get_method( 'add_widget_view' )
676 );
677 }
678
679 function add_widget_view() {
680 $admin_colors = self::retrieve_admin_colors();
681 include( dirname( __FILE__ ) . '/views/dashboard-widget.php' );
682 }
683
684 private static function retrieve_admin_colors() {
685 global $_wp_admin_css_colors;
686 $admin_colour_scheme = get_user_option( 'admin_color', get_current_user_id() );
687 $admin_colors = array( '#0074aa', '#1685b5', '#78ca44', '#0086ba' ); // default
688 if ( isset( $_wp_admin_css_colors[ $admin_colour_scheme ] ) ) {
689 if ( isset( $_wp_admin_css_colors[ $admin_colour_scheme ]->colors ) ) {
690 $admin_colors = $_wp_admin_css_colors[ $admin_colour_scheme ]->colors;
691 }
692 }
693 if ( '#e5e5e5' == $admin_colors[0] && '#999' == $admin_colors[1] ) {
694 $admin_colors[0] = '#bbb';
695 }
696 if ( '#5589aa' == $admin_colors[0] && '#cfdfe9' == $admin_colors[1] ) {
697 $admin_colors[1] = '#85aec5';
698 }
699 if ( '#7c7976' == $admin_colors[0] && '#c6c6c6' == $admin_colors[1] ) {
700 $admin_colors[1] = '#adaba9';
701 $admin_colors[2] = '#adaba9';
702 }
703 if ( self::wp_version() > 3.7 ) {
704 if ( 'fresh' == $admin_colour_scheme ) {
705 $admin_colors = array( '#0074aa', '#1685b5', '#78ca44', '#0086ba' ); // better
706 }
707 }
708 return $admin_colors;
709 }
710
711 function friendly_user_name() {
712 $user = wp_get_current_user();
713 $name = ucfirst( empty( $user->first_name ) ? $user->display_name : $user->first_name );
714 return $name;
715 }
716 }
717