forked from tinify/wordpress-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass-tiny-plugin.php
More file actions
892 lines (763 loc) · 25.1 KB
/
class-tiny-plugin.php
File metadata and controls
892 lines (763 loc) · 25.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
<?php
/*
* Tiny Compress Images - WordPress plugin.
* Copyright (C) 2015-2023 Tinify B.V.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
class Tiny_Plugin extends Tiny_WP_Base {
const VERSION = '3.6.8';
const MEDIA_COLUMN = self::NAME;
const DATETIME_FORMAT = 'Y-m-d G:i:s';
private static $version;
private $settings;
private $twig;
public static function jpeg_quality() {
return 85;
}
public static function version() {
/* Avoid using get_plugin_data() because it is not loaded early enough
in xmlrpc.php. */
return self::VERSION;
}
public function __construct() {
parent::__construct();
$this->settings = new Tiny_Settings();
new Tiny_Conversion( $this->settings );
}
public function set_compressor( $compressor ) {
$this->settings->set_compressor( $compressor );
}
public function init() {
add_filter(
'jpeg_quality',
$this->get_static_method( 'jpeg_quality' )
);
add_filter(
'wp_editor_set_quality',
$this->get_static_method( 'jpeg_quality' )
);
add_filter(
'wp_generate_attachment_metadata',
$this->get_method( 'process_attachment' ),
10,
2
);
add_action( 'delete_attachment', $this->get_method( 'clean_attachment' ), 10, 2 );
load_plugin_textdomain(
self::NAME,
false,
dirname( plugin_basename( __FILE__ ) ) . '/languages'
);
$this->tiny_compatibility();
}
public function cli_init() {
Tiny_CLI::register_command( $this->settings );
}
public function ajax_init() {
add_filter(
'wp_ajax_tiny_async_optimize_upload_new_media',
$this->get_method( 'compress_on_upload' )
);
add_action(
'wp_ajax_tiny_compress_image_from_library',
$this->get_method( 'compress_image_from_library' )
);
add_action(
'wp_ajax_tiny_compress_image_for_bulk',
$this->get_method( 'compress_image_for_bulk' )
);
add_action(
'wp_ajax_tiny_get_optimization_statistics',
$this->get_method( 'ajax_optimization_statistics' )
);
add_action(
'wp_ajax_tiny_get_compression_status',
$this->get_method( 'ajax_compression_status' )
);
add_action(
'wp_ajax_tiny_mark_image_as_compressed',
$this->get_method( 'mark_image_as_compressed' )
);
/* When touching any functionality linked to image compressions when
uploading images make sure it also works with XML-RPC. See README. */
add_filter(
'wp_ajax_nopriv_tiny_rpc',
$this->get_method( 'process_rpc_request' )
);
if ( $this->settings->compress_wr2x_images() ) {
add_action(
'wr2x_upload_retina',
$this->get_method( 'compress_original_retina_image' ),
10,
2
);
add_action(
'wr2x_retina_file_added',
$this->get_method( 'compress_retina_image' ),
10,
3
);
add_action(
'wr2x_retina_file_removed',
$this->get_method( 'remove_retina_image' ),
10,
2
);
}
}
public function admin_init() {
add_action(
'wp_dashboard_setup',
$this->get_method( 'add_dashboard_widget' )
);
add_action(
'admin_enqueue_scripts',
$this->get_method( 'enqueue_scripts' )
);
add_action(
'admin_action_tiny_bulk_action',
$this->get_method( 'media_library_bulk_action' )
);
add_action(
'admin_action_-1',
$this->get_method( 'media_library_bulk_action' )
);
add_action(
'admin_action_tiny_bulk_mark_compressed',
$this->get_method( 'media_library_bulk_action' )
);
add_filter(
'manage_media_columns',
$this->get_method( 'add_media_columns' )
);
add_action(
'manage_media_custom_column',
$this->get_method( 'render_media_column' ),
10,
2
);
add_action(
'attachment_submitbox_misc_actions',
$this->get_method( 'show_media_info' )
);
$plugin = plugin_basename(
dirname( __DIR__ ) . '/tiny-compress-images.php'
);
add_filter(
"plugin_action_links_$plugin",
$this->get_method( 'add_plugin_links' )
);
$this->tiny_compatibility();
add_thickbox();
Tiny_Logger::init();
}
public function admin_menu() {
add_media_page(
__( 'Bulk Optimization', 'tiny-compress-images' ),
esc_html__( 'Bulk TinyPNG', 'tiny-compress-images' ),
'upload_files',
'tiny-bulk-optimization',
$this->get_method( 'render_bulk_optimization_page' )
);
}
public function add_plugin_links( $current_links ) {
$additional = array(
'settings' => sprintf(
'<a href="options-general.php?page=tinify">%s</a>',
esc_html__( 'Settings', 'tiny-compress-images' )
),
'bulk' => sprintf(
'<a href="upload.php?page=tiny-bulk-optimization">%s</a>',
esc_html__( 'Bulk TinyPNG', 'tiny-compress-images' )
),
);
return array_merge( $additional, $current_links );
}
public function tiny_compatibility() {
if ( defined( 'ICL_SITEPRESS_VERSION' ) ) {
new Tiny_WPML();
}
if ( Tiny_AS3CF::is_active() ) {
new Tiny_AS3CF( $this->settings );
}
new Tiny_WooCommerce();
}
public function compress_original_retina_image( $attachment_id, $path ) {
$tiny_image = new Tiny_Image( $this->settings, $attachment_id );
$tiny_image->compress_retina( 'original_wr2x', $path );
}
public function compress_retina_image( $attachment_id, $path, $size_name ) {
$tiny_image = new Tiny_Image( $this->settings, $attachment_id );
$tiny_image->compress_retina( $size_name . '_wr2x', $path );
}
public function remove_retina_image( $attachment_id, $path ) {
$tiny_image = new Tiny_Image( $this->settings, $attachment_id );
$tiny_image->remove_retina_metadata();
}
public function enqueue_scripts( $hook ) {
wp_enqueue_style(
self::NAME . '_admin',
plugins_url( '/css/admin.css', __FILE__ ),
array(),
self::version()
);
wp_enqueue_style(
self::NAME . '_chart',
plugins_url( '/css/optimization-chart.css', __FILE__ ),
array(),
self::version()
);
wp_register_script(
self::NAME . '_admin',
plugins_url( '/js/admin.js', __FILE__ ),
array(),
self::version(),
true
);
// WordPress < 3.3 does not handle multidimensional arrays
wp_localize_script(
self::NAME . '_admin',
'tinyCompress',
array(
'nonce' => wp_create_nonce( 'tiny-compress' ),
'wpVersion' => self::wp_version(),
'pluginVersion' => self::version(),
'L10nAllDone' => __(
'All images are processed',
'tiny-compress-images'
),
'L10nNoActionTaken' => __(
'No action taken',
'tiny-compress-images'
),
'L10nDuplicate' => __(
'Image was already processed',
'tiny-compress-images'
),
'L10nBulkAction' => __( 'Compress Images', 'tiny-compress-images' ),
'L10nBulkMarkCompressed' => __(
'Mark as Compressed',
'tiny-compress-images'
),
'L10nCancelled' => __( 'Cancelled', 'tiny-compress-images' ),
'L10nCompressing' => __( 'Compressing', 'tiny-compress-images' ),
'L10nCompressed' => __( 'compressed', 'tiny-compress-images' ),
'L10nConverted' => __( 'converted', 'tiny-compress-images' ),
'L10nFile' => __( 'File', 'tiny-compress-images' ),
'L10nSizesOptimized' => __(
'Sizes optimized',
'tiny-compress-images'
),
'L10nInitialSize' => __( 'Initial size', 'tiny-compress-images' ),
'L10nCurrentSize' => __( 'Current size', 'tiny-compress-images' ),
'L10nSavings' => __( 'Savings', 'tiny-compress-images' ),
'L10nStatus' => __( 'Status', 'tiny-compress-images' ),
'L10nShowMoreDetails' => __(
'Show more details',
'tiny-compress-images'
),
'L10nError' => __( 'Error', 'tiny-compress-images' ),
'L10nLatestError' => __( 'Latest error', 'tiny-compress-images' ),
'L10nInternalError' => __( 'Internal error', 'tiny-compress-images' ),
'L10nOutOf' => __( 'out of', 'tiny-compress-images' ),
'L10nWaiting' => __( 'Waiting', 'tiny-compress-images' ),
)
);
wp_enqueue_script( self::NAME . '_admin' );
if ( 'media_page_tiny-bulk-optimization' == $hook ) {
wp_enqueue_style(
self::NAME . '_tiny_bulk_optimization',
plugins_url( '/css/bulk-optimization.css', __FILE__ ),
array(),
self::version()
);
wp_enqueue_style(
self::NAME . '_chart',
plugins_url( '/css/optimization-chart.css', __FILE__ ),
array(),
self::version()
);
wp_register_script(
self::NAME . '_tiny_bulk_optimization',
plugins_url( '/js/bulk-optimization.js', __FILE__ ),
array(),
self::version(),
true
);
wp_enqueue_script( self::NAME . '_tiny_bulk_optimization' );
}
}
public function process_attachment( $metadata, $attachment_id ) {
if ( $this->settings->auto_compress_enabled() ) {
if (
$this->settings->background_compress_enabled()
) {
$this->async_compress_on_upload( $metadata, $attachment_id );
} else {
return $this->blocking_compress_on_upload( $metadata, $attachment_id );
}
}
return $metadata;
}
public function blocking_compress_on_upload( $metadata, $attachment_id ) {
if ( ! empty( $metadata ) ) {
$tiny_image = new Tiny_Image( $this->settings, $attachment_id, $metadata );
Tiny_Logger::debug(
'blocking compress on upload',
array(
'image_id' => $attachment_id,
)
);
$result = $tiny_image->compress();
return $tiny_image->get_wp_metadata();
} else {
return $metadata;
}
}
public function async_compress_on_upload( $metadata, $attachment_id ) {
$context = 'wp';
$action = 'tiny_async_optimize_upload_new_media';
$_ajax_nonce = wp_create_nonce( 'new_media-' . $attachment_id );
$body = compact( 'action', '_ajax_nonce', 'metadata', 'attachment_id', 'context' );
$args = array(
'timeout' => 0.01,
'blocking' => false,
'body' => $body,
'cookies' => isset( $_COOKIE ) && is_array( $_COOKIE ) ? $_COOKIE : array(),
'sslverify' => apply_filters( 'https_local_ssl_verify', false ),
);
if ( defined( 'XMLRPC_REQUEST' ) && get_current_user_id() ) {
/* We generate a hash to be used for the transient we use to store the current user. */
$rpc_hash = md5( maybe_serialize( $body ) );
$args['body']['tiny_rpc_action'] = $args['body']['action'];
/* We set a different action to make sure that all RPC requests are first validated. */
$args['body']['action'] = 'tiny_rpc';
$args['body']['tiny_rpc_hash'] = $rpc_hash;
$args['body']['tiny_rpc_nonce'] = wp_create_nonce( 'tiny_rpc_' . $rpc_hash );
/*
We can't use cookies here, so we save the user id in a transient
so that we can retrieve it again when processing the RPC request.
We should be able to use a relatively short timeout, as the request
should be processed directly afterwards.
*/
set_transient( 'tiny_rpc_' . $rpc_hash, get_current_user_id(), 10 );
}
Tiny_Logger::debug(
'remote post',
array(
'image_id' => $attachment_id,
)
);
if ( getenv( 'WORDPRESS_HOST' ) !== false ) {
wp_remote_post( getenv( 'WORDPRESS_HOST' ) . '/wp-admin/admin-ajax.php', $args );
} else {
wp_remote_post( admin_url( 'admin-ajax.php' ), $args );
}
}
public function process_rpc_request() {
if (
empty( $_POST['tiny_rpc_action'] ) ||
empty( $_POST['tiny_rpc_hash'] ) ||
32 !== strlen( $_POST['tiny_rpc_hash'] )
) {
exit();
}
$rpc_hash = sanitize_key( $_POST['tiny_rpc_hash'] );
$user_id = absint( get_transient( 'tiny_rpc_' . $rpc_hash ) );
$user = $user_id ? get_userdata( $user_id ) : false;
/* We no longer need the transient. */
delete_transient( 'tiny_rpc_' . $rpc_hash );
if ( ! $user || ! $user->exists() ) {
exit();
}
wp_set_current_user( $user_id );
if ( ! check_ajax_referer( 'tiny_rpc_' . $rpc_hash, 'tiny_rpc_nonce', false ) ) {
exit();
}
/* Now that everything is checked, perform the actual action. */
$action = $_POST['tiny_rpc_action'];
unset(
$_POST['action'],
$_POST['tiny_rpc_action'],
$_POST['tiny_rpc_id'],
$_POST['tiny_rpc_nonce']
);
do_action( 'wp_ajax_' . $action );
}
public function compress_on_upload() {
if ( ! wp_verify_nonce( $_POST['_ajax_nonce'], 'new_media-' . $_POST['attachment_id'] ) ) {
exit;
}
if ( current_user_can( 'upload_files' ) ) {
$attachment_id = intval( $_POST['attachment_id'] );
$metadata = $_POST['metadata'];
if ( is_array( $metadata ) ) {
$tiny_image = new Tiny_Image( $this->settings, $attachment_id, $metadata );
Tiny_Logger::debug(
'compress on upload',
array(
'image_id' => $attachment_id,
)
);
$result = $tiny_image->compress();
// The wp_update_attachment_metadata call is thrown because the
// dimensions of the original image can change. This will then
// trigger other plugins and can result in unexpected behaviour and
// further changes to the image. This may require another approach.
// Note that as of WP 5.3 it is advised to not hook into this filter
// anymore, so other plugins are less likely to be triggered.
wp_update_attachment_metadata( $attachment_id, $tiny_image->get_wp_metadata() );
}
}
exit();
}
/**
* Validates AJAX request for attachment operations.
*
* @since 3.0.0
*
* @return array Either error array ['error' => 'message']
* or success array ['data' => [$id, $metadata]]
*/
private function validate_ajax_attachment_request() {
if ( ! $this->check_ajax_referer() ) {
exit();
}
if ( ! current_user_can( 'upload_files' ) ) {
return array(
'error' => esc_html__(
"You don't have permission to upload files.",
'tiny-compress-images'
),
);
}
if ( empty( $_POST['id'] ) ) {
return array(
'error' => esc_html__(
'Not a valid media file.',
'tiny-compress-images'
),
);
}
$id = intval( $_POST['id'] );
$metadata = wp_get_attachment_metadata( $id );
if ( ! is_array( $metadata ) ) {
return array(
'error' => esc_html__(
'Could not find metadata of media file.',
'tiny-compress-images'
),
);
}
return array(
'data' => array( $id, $metadata ),
);
}
public function compress_image_from_library() {
$response = $this->validate_ajax_attachment_request();
if ( isset( $response['error'] ) ) {
echo $response['error'];
exit();
}
list($id, $metadata) = $response['data'];
Tiny_Logger::debug(
'compress from library',
array(
'image_id' => $id,
)
);
$tiny_image = new Tiny_Image( $this->settings, $id, $metadata );
$result = $tiny_image->compress();
// The wp_update_attachment_metadata call is thrown because the
// dimensions of the original image can change. This will then
// trigger other plugins and can result in unexpected behaviour and
// further changes to the image. This may require another approach.
// Note that as of WP 5.3 it is advised to not hook into this filter
// anymore, so other plugins are less likely to be triggered.
wp_update_attachment_metadata( $id, $tiny_image->get_wp_metadata() );
echo $this->render_compress_details( $tiny_image );
exit();
}
public function compress_image_for_bulk() {
$response = $this->validate_ajax_attachment_request();
if ( isset( $response['error'] ) ) {
echo json_encode( $response );
exit();
}
list($id, $metadata) = $response['data'];
$tiny_image_before = new Tiny_Image( $this->settings, $id, $metadata );
$image_statistics_before = $tiny_image_before->get_statistics(
$this->settings->get_sizes(),
$this->settings->get_active_tinify_sizes()
);
$size_before = $image_statistics_before['compressed_total_size'];
$tiny_image = new Tiny_Image( $this->settings, $id, $metadata );
Tiny_Logger::debug(
'compress from bulk',
array(
'image_id' => $id,
)
);
$result = $tiny_image->compress();
$image_statistics = $tiny_image->get_statistics(
$this->settings->get_sizes(),
$this->settings->get_active_tinify_sizes()
);
wp_update_attachment_metadata( $id, $tiny_image->get_wp_metadata() );
$current_library_size = intval( $_POST['current_size'] );
$size_after = $image_statistics['compressed_total_size'];
$new_library_size = $current_library_size + $size_after - $size_before;
$result['message'] = $tiny_image->get_latest_error();
$result['image_sizes_compressed'] = $image_statistics['image_sizes_compressed'];
$result['image_sizes_converted'] = $image_statistics['image_sizes_converted'];
$result['image_sizes_optimized'] = $image_statistics['image_sizes_compressed'];
$result['initial_total_size'] = size_format(
$image_statistics['initial_total_size'],
1
);
$result['optimized_total_size'] = size_format(
$image_statistics['compressed_total_size'],
1
);
$result['savings'] = $tiny_image->get_savings( $image_statistics );
$result['status'] = $this->settings->get_status();
$result['thumbnail'] = wp_get_attachment_image(
$id,
array( '30', '30' ),
true,
array(
'class' => 'pinkynail',
'alt' => '',
)
);
$result['size_change'] = $size_after - $size_before;
$result['human_readable_library_size'] = size_format( $new_library_size, 2 );
echo json_encode( $result );
exit();
}
public function ajax_optimization_statistics() {
if ( $this->check_ajax_referer() && current_user_can( 'upload_files' ) ) {
$stats = Tiny_Bulk_Optimization::get_optimization_statistics( $this->settings );
echo json_encode( $stats );
}
exit();
}
public function ajax_compression_status() {
$response = $this->validate_ajax_attachment_request();
if ( isset( $response['error'] ) ) {
echo $response['error'];
exit();
}
list($id, $metadata) = $response['data'];
$tiny_image = new Tiny_Image( $this->settings, $id, $metadata );
echo $this->render_compress_details( $tiny_image );
exit();
}
public function media_library_bulk_action() {
$valid_actions = array( 'tiny_bulk_action', 'tiny_bulk_mark_compressed' );
$action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : '';
$action2 = isset( $_REQUEST['action2'] ) ? $_REQUEST['action2'] : '';
if (
! in_array( $action, $valid_actions, true ) &&
! in_array( $action2, $valid_actions, true )
) {
return;
}
if ( empty( $_REQUEST['media'] ) || ( ! $_REQUEST['media'] ) ) {
$_REQUEST['action'] = '';
return;
}
check_admin_referer( 'bulk-media' );
$ids = implode( '-', array_map( 'intval', $_REQUEST['media'] ) );
$location = 'upload.php?mode=list&ids=' . $ids;
$location = add_query_arg( 'action', $_REQUEST['action'], $location );
if ( ! empty( $_REQUEST['paged'] ) ) {
$location = add_query_arg( 'paged', absint( $_REQUEST['paged'] ), $location );
}
if ( ! empty( $_REQUEST['s'] ) ) {
$location = add_query_arg( 's', $_REQUEST['s'], $location );
}
if ( ! empty( $_REQUEST['m'] ) ) {
$location = add_query_arg( 'm', $_REQUEST['m'], $location );
}
wp_redirect( admin_url( $location ) );
exit();
}
public function add_media_columns( $columns ) {
$columns[ self::MEDIA_COLUMN ] = esc_html__( 'Compression', 'tiny-compress-images' );
return $columns;
}
public function render_media_column( $column, $id ) {
if ( self::MEDIA_COLUMN === $column ) {
$tiny_image = new Tiny_Image( $this->settings, $id );
if ( $tiny_image->file_type_allowed() ) {
echo '<div class="tiny-ajax-container">';
$this->render_compress_details( $tiny_image );
echo '</div>';
}
}
}
public function show_media_info() {
global $post;
$tiny_image = new Tiny_Image( $this->settings, $post->ID );
if ( $tiny_image->file_type_allowed() ) {
echo '<div class="misc-pub-section tiny-compress-images">';
echo '<h4>';
esc_html_e( 'JPEG, PNG, & WebP optimization', 'tiny-compress-images' );
echo '</h4>';
echo '<div class="tiny-ajax-container">';
$this->render_compress_details( $tiny_image );
echo '</div>';
echo '</div>';
}
}
private function render_compress_details( $tiny_image ) {
$in_progress = $tiny_image->filter_image_sizes( 'in_progress' );
if ( count( $in_progress ) > 0 ) {
include __DIR__ . '/views/compress-details-processing.php';
} else {
include __DIR__ . '/views/compress-details.php';
}
}
public function get_estimated_bulk_cost( $estimated_credit_use ) {
return Tiny_Compress::estimate_cost(
$estimated_credit_use,
$this->settings->get_compression_count()
);
}
public function render_bulk_optimization_page() {
$stats = Tiny_Bulk_Optimization::get_optimization_statistics( $this->settings );
$estimated_costs = $this->get_estimated_bulk_cost( $stats['estimated_credit_use'] );
$admin_colors = self::retrieve_admin_colors();
/* This makes sure that up to date information is retrieved from the API. */
$this->settings->get_compressor()->get_status();
$active_tinify_sizes = $this->settings->get_active_tinify_sizes();
$remaining_credits = $this->settings->get_remaining_credits();
$is_on_free_plan = $this->settings->is_on_free_plan();
$email_address = $this->settings->get_email_address();
include __DIR__ . '/views/bulk-optimization.php';
}
public function add_dashboard_widget() {
wp_enqueue_style(
self::NAME . '_chart',
plugins_url( '/css/optimization-chart.css', __FILE__ ),
array(),
self::version()
);
wp_enqueue_style(
self::NAME . '_dashboard_widget',
plugins_url( '/css/dashboard-widget.css', __FILE__ ),
array(),
self::version()
);
wp_register_script(
self::NAME . '_dashboard_widget',
plugins_url( '/js/dashboard-widget.js', __FILE__ ),
array(),
self::version(),
true
);
/* This might be deduplicated with the admin script localization, but
the order of including scripts is sometimes different. So in that
case we need to make sure that the order of inclusion is correc.t */
wp_localize_script(
self::NAME . '_dashboard_widget',
'tinyCompressDashboard',
array(
'nonce' => wp_create_nonce( 'tiny-compress' ),
)
);
wp_enqueue_script( self::NAME . '_dashboard_widget' );
wp_add_dashboard_widget(
$this->get_prefixed_name( 'dashboard_widget' ),
esc_html__( 'TinyPNG - JPEG, PNG & WebP image compression', 'tiny-compress-images' ),
$this->get_method( 'add_widget_view' )
);
}
public function add_widget_view() {
$admin_colors = self::retrieve_admin_colors();
include __DIR__ . '/views/dashboard-widget.php';
}
private static function retrieve_admin_colors() {
global $_wp_admin_css_colors;
$admin_colour_scheme = get_user_option( 'admin_color', get_current_user_id() );
$admin_colors = array( '#0074aa', '#1685b5', '#78ca44', '#0086ba' ); // default
if ( isset( $_wp_admin_css_colors[ $admin_colour_scheme ] ) ) {
if ( isset( $_wp_admin_css_colors[ $admin_colour_scheme ]->colors ) ) {
$admin_colors = $_wp_admin_css_colors[ $admin_colour_scheme ]->colors;
}
}
if ( '#e5e5e5' == $admin_colors[0] && '#999' == $admin_colors[1] ) {
$admin_colors[0] = '#bbb';
}
if ( '#5589aa' == $admin_colors[0] && '#cfdfe9' == $admin_colors[1] ) {
$admin_colors[1] = '#85aec5';
}
if ( '#7c7976' == $admin_colors[0] && '#c6c6c6' == $admin_colors[1] ) {
$admin_colors[1] = '#adaba9';
$admin_colors[2] = '#adaba9';
}
if ( self::wp_version() > 3.7 ) {
if ( 'fresh' == $admin_colour_scheme ) {
$admin_colors = array( '#0074aa', '#1685b5', '#78ca44', '#0086ba' ); // better
}
}
return $admin_colors;
}
public function friendly_user_name() {
$user = wp_get_current_user();
$name = ucfirst( empty( $user->first_name ) ? $user->display_name : $user->first_name );
return $name;
}
/**
* Will clean up converted files (if any) when the original is deleted
*
* Hooked to the `delete_attachment` action.
* @see https://developer.wordpress.org/reference/hooks/deleted_post/
*
* @param [int] $post_id
*
* @return void
*/
public function clean_attachment( $post_id ) {
$tiny_image = new Tiny_Image( $this->settings, $post_id );
$tiny_image->delete_converted_image();
}
public static function request_review() {
$review_url =
'https://wordpress.org/support/plugin/tiny-compress-images/reviews/#new-post';
$review_block = esc_html__( 'Enjoying TinyPNG?', 'tiny-compress-images' );
$review_block .= ' ';
$review_block .= sprintf(
'<a href="%s" target="_blank">%s</a>',
esc_url( $review_url ),
esc_html__( 'Write a review', 'tiny-compress-images' )
);
return $review_block;
}
public function mark_image_as_compressed() {
$response = $this->validate_ajax_attachment_request();
if ( isset( $response['error'] ) ) {
echo $response['error'];
exit();
}
list($id, $metadata) = $response['data'];
$tiny_image = new Tiny_Image( $this->settings, $id, $metadata );
$tiny_image->mark_as_compressed();
echo $this->render_compress_details( $tiny_image );
exit();
}
}