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