C:\xampp\htdocs\landing\wp-content\plugins\wp-optimize\includes\class-updraft-smush-manager.php


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
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
<?php
/**
 *  Extends the generic task manager to manage smush related queues
 */

if (!defined('ABSPATH')) die('Access denied.');

if (!
class_exists('Updraft_Task_Manager_1_2')) require_once(WPO_PLUGIN_MAIN_PATH 'vendor/team-updraft/common-libs/src/updraft-tasks/class-updraft-task-manager.php');

if (!
class_exists('Updraft_Smush_Manager')) :

class 
Updraft_Smush_Manager extends Updraft_Task_Manager_1_2 {

    static protected 
$_instance null;

    
/**
     * Options used for smush jobs
     *
     * @var array
     */
    
public $options;

    
/**
     * The service provider to use
     *
     * @var string
     */
    
public $webservice;

    
/**
     * The logger for this instance
     *
     * @var mixed
     */
    
public $logger;

    
/**
     * Require task-level locking
     *
     * @var integer
     */
    
protected $use_per_task_lock 60;
    
    
/**
     * The Task Manager constructor
     */
    
public function __construct() {
        
parent::__construct();

        if (!
class_exists('Updraft_Smush_Manager_Commands')) include_once('class-updraft-smush-manager-commands.php');
        if (!
class_exists('Smush_Task')) include_once('class-updraft-smush-task.php');
        if (!
class_exists('Re_Smush_It_Task')) include_once('class-updraft-resmushit-task.php');
        if (!
class_exists('Updraft_Logger_Interface')) include_once('class-updraft-logger-interface.php');
        if (!
class_exists('Updraft_Abstract_Logger')) include_once('class-updraft-abstract-logger.php');
        if (!
class_exists('Updraft_File_Logger')) include_once('class-updraft-file-logger.php');
        if (!
class_exists('WP_Optimize_Transients_Cache')) include_once('class-wp-optimize-transients-cache.php');

        
$this->commands = new Updraft_Smush_Manager_Commands($this);
        
$this->options WP_Optimize()->get_options();

        if (!isset(
$this->options)) {
            
$this->set_default_options();
        }

        
$this->webservice $this->options->get_option('compression_server''resmushit');

        
// Ensure the saved service is valid
        
if (!in_array($this->webservice$this->get_allowed_services())) {
            
$this->webservice $this->get_default_webservice();
        }
        
$this->logger = new Updraft_File_Logger($this->get_logfile_path());
        
$this->add_logger($this->logger);

        
add_action('wp_ajax_updraft_smush_ajax', array($this'updraft_smush_ajax'));
        
add_action('admin_enqueue_scripts', array($this'admin_enqueue_scripts'), 9);
        
add_action('elementor/editor/before_enqueue_scripts', array($this'admin_enqueue_scripts'));
        
add_action('add_attachment', array($this'autosmush_create_task'));
        
add_action('ud_task_initialised', array($this'set_task_logger'));
        
add_action('ud_task_started', array($this'set_task_logger'));
        
add_action('ud_task_completed', array($this'record_stats'));
        
add_action('ud_task_failed', array($this'record_stats'));
        
add_action('prune_smush_logs', array($this'prune_smush_logs'));
        
add_action('autosmush_process_queue', array($this'autosmush_process_queue'));
        if (
'show' == $this->options->get_option('show_smush_metabox''show')) {
            
add_action('add_meta_boxes_attachment', array($this'add_smush_metabox'), 102);
            
add_filter('attachment_fields_to_edit', array($this'add_compress_button_to_media_modal' ), 102);
        }
        
add_action('delete_attachment', array($this'unscheduled_original_file_deletion'));

        
add_filter('manage_media_columns', array($this'manage_media_columns'));
        
add_action('manage_media_custom_column', array($this'manage_media_custom_column'), 102);

        
// clean backup images cron action.
        
add_action('wpo_smush_clear_backup_images', array($this'clear_backup_images'));

        
// add filter for already compressed images by EWWW Image Optimizer.
        
add_filter('wpo_get_uncompressed_images_args', array($this'ewww_image_optimizer_compressed_images_args'));

        if (!
wp_next_scheduled('wpo_smush_clear_backup_images')) {
            
wp_schedule_event(time(), 'daily''wpo_smush_clear_backup_images');
        }
    }

    
/**
     * Add custom column to Media Library.
     *
     * @param array $columns
     * @return mixed
     */
    
public function manage_media_columns($columns) {
        
$columns['wpo_smush'] = 'WP-Optimize';

        return 
$columns;
    }

    
/**
     * Display content in the custom column.
     *
     * @param string $column
     * @param int    $attachment_id
     */
    
public function manage_media_custom_column($column$attachment_id) {
        if (
'wpo_smush' !== $column) return;

        if (!
$this->is_compressed($attachment_id)) return;

        
$smush_stats get_post_meta($attachment_id'smush-stats'true);

        
$original_size $smush_stats['original-size'];
        
$smushed_size $smush_stats['smushed-size'];

        if (
== $original_size) {
            
$info sprintf(__('The file was compressed to %s using WP-Optimize''wp-optimize'), WP_Optimize()->format_size($smushed_size));
        } else {
            
$saved round((($original_size $smushed_size) / $original_size 100), 2);
            
$info sprintf(__('The file was compressed from %s to %s, saving %s percent, using WP-Optimize''wp-optimize'), WP_Optimize()->format_size($original_size), WP_Optimize()->format_size($smushed_size), $saved);
        }

        echo 
htmlentities($info);
    }

    
/**
     * The Task Manager AJAX handler
     */
    
public function updraft_smush_ajax() {

        
$nonce = empty($_REQUEST['nonce']) ? '' $_REQUEST['nonce'];

        if (!
wp_verify_nonce($nonce'updraft-task-manager-ajax-nonce') || empty($_REQUEST['subaction']))
            die(
'Security check failed');

        
$subaction $_REQUEST['subaction'];

        
$allowed_commands Updraft_Smush_Manager_Commands::get_allowed_ajax_commands();
        
        if (
in_array($subaction$allowed_commands)) {

            if (isset(
$_REQUEST['data']))
                
$data $_REQUEST['data'];

            
$results call_user_func(array($this->commands$subaction), $data);
            
            if (
is_wp_error($results)) {
                
$results = array(
                    
'status' => true,
                    
'result' => false,
                    
'error_code' => $results->get_error_code(),
                    
'error_message' => $results->get_error_message(),
                    
'error_data' => $results->get_error_data(),
                );
            }
            
            echo 
json_encode($results);
        } else {
            echo 
json_encode(array('error' => 'No such command found'));
        }
        die;
    }

    
/**
     * Creates a task to auto compress an image on  upload
     *
     * @param int $post_id - id of the post
     */
    
public function autosmush_create_task($post_id) {

        
$post get_post($post_id);

        if (!
$this->options->get_option('autosmush'false))
            return;

        if (!
'image' == substr($post->post_mime_type05))
            return;

        if (
$this->task_exists($post_id))
            return;
        
        
$options = array(
            
'attachment_id' => $post_id,
            
'blog_id'       => get_current_blog_id(),
            
'image_quality' => $this->options->get_option('image_quality'96),
            
'keep_original' => $this->options->get_option('back_up_original'true),
            
'preserve_exif' => $this->options->get_option('preserve_exif'true),
            
'lossy_compression' => $this->options->get_option('lossy_compression'false)
        );

        if (
filesize(get_attached_file($post_id)) > 5242880) {
            
$options['request_timeout'] = 180;
        }

        
$server $this->options->get_option('compression_server'$this->webservice);
        
$task_name $this->get_associated_task($server);

        
$description "$task_name with attachment ID : ".$post_id.", autocreated on : ".date("F d, Y h:i:s"time());
        
$task call_user_func(array($task_name'create_task'), 'smush'$description$options$task_name);
        
        if (
$task$task->add_logger($this->logger);
        
$this->log($description);

        if (!
wp_next_scheduled('autosmush_process_queue')) {
            
wp_schedule_single_event(time() + 300'autosmush_process_queue');
        }
    }

    
/**
     * Process the autosmush queue and sets up a cron job if needed
     * for future processing
     */
    
public function autosmush_process_queue() {
        
        if (!
wp_next_scheduled('autosmush_process_queue') && !$this->is_queue_processed()) {
            
wp_schedule_single_event(time() + 600'autosmush_process_queue');
        }

        
$this->write_log_header();
        
$this->clear_cached_data();
        
$this->process_queue('smush');

        if (
$this->is_queue_processed()) {
            
$this->clean_up_old_tasks('smush');
        }

    }


    
/**
     * Process the compression of a single image
     *
     * @param int     $image   - ID of image
     * @param array  $options - options to use
     * @param string $server  - the server to process with
     *
     * @return boolean - Status of the task
     */
    
public function compress_single_image($image$options$server) {
        
$task_name $this->get_associated_task($server);
        
$description "$task_name - attachment ID : "$image", started on : "date("F d, Y h:i:s"time());

        
$task call_user_func(array($task_name'create_task'), 'smush'$description$options$task_name);
        
$task->add_logger($this->logger);
        
$this->clear_cached_data();

        if (!
wp_next_scheduled('prune_smush_logs')) {
            
wp_schedule_single_event(time() + 7200'prune_smush_logs');
        }

        return 
$this->process_task($task);
    }

    
/**
     * Restores a single image if a backup is available
     *
     * @param int $image_id - The id of the image
     * @param int $blog_id  - The id of the blog
     *
     * @return bool|WP_Error - success or failure
     */
    
public function restore_single_image($image_id$blog_id) {

        
$switched_blog false;
        if (
is_multisite() && current_user_can('manage_network_options')) {
            
switch_to_blog($blog_id);
            
$switched_blog true;
        } elseif (
is_multisite() && get_current_blog_id() != $blog_id) {
            return new 
WP_Error('restore_backup_wrong_blog_id'__('The blog ID provided does not match the current blog.''wp-optimize'));
        }

        
$error false;

        
$image_path get_attached_file($image_id);
        
$backup_path get_post_meta($image_id'original-file'true);
        
        
// If the file doesn't exist, check if it's relative
        
if (!is_file($backup_path)) {
            
$uploads_dir wp_upload_dir();
            
$uploads_basedir trailingslashit($uploads_dir['basedir']);

            if (
is_file($uploads_basedir $backup_path)) {
                
$backup_path $uploads_basedir $backup_path;
            }
        }

        
// If the file still doesn't exist, the record could be an absolute path from a migrated site
        // Use the current Uploads path
        
if (!is_file($backup_path)) {
            
$current_uploads_dir_folder trailingslashit(substr($uploads_basedirstrlen(ABSPATH)));
            
// A strict operator (!==) needs to be used, as 0 is a positive result.
            
if (false !== strpos($backup_path$current_uploads_dir_folder)) {
                
$temp_relative_backup_path substr($backup_pathstrpos($backup_path$current_uploads_dir_folder) + strlen($current_uploads_dir_folder));
            }

            if (
is_file($uploads_basedir $temp_relative_backup_path)) {
                
$backup_path $uploads_basedir $temp_relative_backup_path;
            }

        }

        
// If the file still doesn't exist, the record could be an absolute path from a migrated site
        // The current Uploads path failed, so try with the default uploads directory value
        
if (!is_file($backup_path)) {
            
// A strict operator (!==) needs to be used, as 0 is a positive result.
            
if (false !== strpos($backup_path'wp-content/uploads/')) {
                
$backup_path substr($backup_pathstrpos($backup_path'wp-content/uploads/') + strlen('wp-content/uploads/'));
                
$backup_path $uploads_basedir $backup_path;
            }
        }

        if (!
is_file($backup_path)) {
            
// Delete information about backup.
            
delete_post_meta($image_id'original-file');
            
$error = new WP_Error('restore_backup_not_found'__('The backup was not found; it may have been deleted or was already restored''wp-optimize'));
        } elseif (!
is_writable($image_path)) {
            
$error =  new WP_Error('restore_failed'__('The destination could not be written to.''wp-optimize').' '.__("Please check your folder's permissions"'wp-optimize'));
        } elseif (!
copy($backup_path$image_path)) {
            
$error =  new WP_Error('restore_failed'__('The file could not be copied; check your PHP error logs for details''wp-optimize'));
        } elseif (!
unlink($backup_path)) {
            
$error =  new WP_Error('restore_failed'sprintf(__('The backup file %s could not be deleted.''wp-optimize'), $backup_path));
        }

        if (!
$error) {
            
// if backup image deleted successfully
            // then delete from attachment meta associated smush data
            
delete_post_meta($image_id'smush-complete');
            
delete_post_meta($image_id'smush-stats');
            
delete_post_meta($image_id'original-file');
            
delete_post_meta($image_id'smush-info');
        }

        if (
$switched_blog) {
            
restore_current_blog();
        }

        if (
is_wp_error($error)) return $error;

        
$this->delete_from_cache('uncompressed_images');

        if (!
wp_next_scheduled('prune_smush_logs')) {
            
wp_schedule_single_event(time() + 7200'prune_smush_logs');
        }

        return 
true;
    }

    
/**
     * Restore compressed images for selected blog.
     *
     * @param bool $restore_backup              if true then restore images from backup otherwise just delete meta.
     * @param int  $blog_id                     blog id.
     * @param int  $images_limit                how many images process per time.
     * @param bool $delete_only_backups_meta meta fields will deleted only for images those will restored from backup.
     *
     * @return array ['completed' => (bool), 'message' => (string), 'error' => (string)]
     */
    
public function bulk_restore_compressed_images($restore_backup$blog_id 1$images_limit 100$delete_only_backups_meta false) {
        global 
$wpdb;

        if (
is_multisite()) {
            
switch_to_blog($blog_id);
        }

        
$result = array(
            
'completed' => false,
            
'message' => '',
        );

        
$processed 0;

        if (
$restore_backup) {
            
// get post ids those have backup meta field.
            
$image_ids $wpdb->get_results($wpdb->prepare("SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = 'original-file' LIMIT %d;"$images_limit), ARRAY_A);

            if (!empty(
$image_ids)) {
                
// run restore function for each found image.
                
foreach ($image_ids as $image) {
                    
$restore_result $this->restore_single_image($image['post_id'], $blog_id);

                    
// if we get an error then we stop the work, except situation when "backup already restored'.
                    
if (is_wp_error($restore_result) && 'restore_backup_not_found' != $restore_result->get_error_code()) {
                        
// we need to stop the work as we haven't restored the backup.
                        
$result['error'] = $restore_result->get_error_message();
                        
$this->options->delete_option('smush_images_restored');
                        break;
                    }

                    
$processed++;
                }
            }

            
$images_count count($image_ids);

            
// if all images processed then set flag completed to true.
            
if ($processed == $images_count && $images_count $images_limit) {
                
$this->options->delete_option('smush_images_restored');
                
$result['completed'] = true;
            } else {
                
// save into options total processed count.
                
$processed += $this->options->get_option('smush_images_restored'0);
                
$this->options->update_option('smush_images_restored'$processed);

                if (
is_multisite()) {
                    
$result['message'] = sprintf(__('%s compressed images were restored from their backup for the site %s''wp-optimize'), $processedget_site_url($blog_id));
                } else {
                    
$result['message'] = sprintf(__('%s compressed images were restored from their backup''wp-optimize'), $processed);
                }
            }

        } else {
            
// if we just delete compressed images meta then set complete flag to true.
            
$result['completed'] = true;
        }

        if (
$result['completed']) {
            if (
$delete_only_backups_meta) {
                if (
is_multisite()) {
                    
$result['message'] = sprintf(__('All the compressed images for the site %s were successfully restored.''wp-optimize'), get_site_url($blog_id));
                } else {
                    
$result['message'] = __('All the compressed images were successfully restored.''wp-optimize');
                }
            } else {
                if (
is_multisite()) {
                    
$result['message'] = sprintf(__('All the compressed images for the site %s were successfully marked as uncompressed.''wp-optimize'), get_site_url($blog_id));
                } else {
                    
$result['message'] = __('All the compressed images were successfully marked as uncompressed.''wp-optimize');
                }
            }

            
// clear all metas for smushed images after work completed.
            // if $delete_only_backup_meta set to true then all meta fields was deleted in restore_single_image()
            // and we don't need delete metas for other images.
            
if (!$delete_only_backups_meta) {
                
$wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE meta_key IN ('smush-complete', 'smush-stats', 'original-file', 'smush-info');");
            }
        }

        if (
is_multisite()) {
            
restore_current_blog();
        }

        return 
$result;
    }

    
/**
     * Process bulk smushing operation
     *
     * @param array $images - the array of images to process
     * @return bool - true if processing complete
     */
    
public function process_bulk_smush($images = array()) {
        
        
// Get a list of pending tasks so we can exclude those
        
$pending_tasks $this->get_pending_tasks();
        
$queued_images = array();

        
$this->write_log_header();

        if (!empty(
$pending_tasks)) {
            foreach (
$pending_tasks as $task) {
                
$queued_images[] = array(
                    
'attachment_id' => $task->get_option('attachment_id'),
                    
'blog_id' => $task->get_option('blog_id')
                );
            }
        }

        foreach (
$images as $image) {
            
// Skip if already in the queue
            
if (in_array($image$queued_images)) continue;

            
$options = array(
                
'attachment_id' => intval($image['attachment_id']),
                
'blog_id'       => intval($image['blog_id']),
                
'image_quality' => $this->options->get_option('image_quality'85),
                
'keep_original' => $this->options->get_option('back_up_original'true),
                
'preserve_exif' => $this->options->get_option('preserve_exif'true),
                
'lossy_compression' => $this->options->get_option('lossy_compression'false)
            );

            
$server $this->options->get_option('compression_server'$this->webservice);
            
$task_name $this->get_associated_task($server);

            
$description "$task_name - Attachment ID : "intval($image['attachment_id']) . ", Started on : "date("F d, Y h:i:s"time());
            
$task call_user_func(array($task_name'create_task'), 'smush'$description$options$task_name);
            
$task->add_logger($this->logger);
        }

        
$this->clear_cached_data();
        
$this->process_queue('smush');

        if (
$this->is_queue_processed()) {
            
$this->clean_up_old_tasks('smush');
        }

        if (!
wp_next_scheduled('prune_smush_logs')) {
            
wp_schedule_single_event(time() + 7200'prune_smush_logs');
        }

        return 
true;
    }


    
/**
     * Check if a specified server online
     *
     * @param string $server - the server to test
     * @return bool - true if yes, false otherwise
     */
    
public function check_server_online($server 'resmushit') {
        
$task $this->get_associated_task($server);
        
$online call_user_func(array($task'is_server_online'));
            
        if (
$online) {
            
update_option($taskstrtotime('now'));
        } else {
            
$this->log(get_option($task));
        }

        
$this->log(sprintf('%s server status: %s'$task$online 'Online' 'Offline'));
        return 
$online;
    }
    
    
/**
     * Checks if the queue for smushing is compleete
     *
     * @return bool - true if processed, false otherwise
     */
    
public function is_queue_processed() {

        
$active $this->get_pending_tasks();
        if (
$active && != count($active))
            return 
false;

        if (
false !== get_option('updraft_semaphore_smush'))
            return 
false;

        return 
true;
    }

    
/**
     * Logs useful data once a smush task completes or if it fails
     *
     * @param mixed $task - A task object
     */
    
public function record_stats($task) {

        
$attachment_id    $task->get_option('attachment_id');
        
$completed_task_count $this->options->get_option('completed_task_count'false);
        
$failed_task_count $this->options->get_option('failed_task_count'0);
        
$total_bytes_saved $this->options->get_option('total_bytes_saved'false);
        
$total_percent_saved $this->options->get_option('total_percent_saved'0);

        if (
'ud_task_failed' == current_action()) {
            
$this->options->update_option('failed_task_count', ++$failed_task_count);
            return;
        }

        if (
false === $completed_task_count) {
            
$completed_task_count $total_bytes_saved 0;
        }

        if (!
$total_bytes_saved) {
            
$total_bytes_saved 0;
        }

        if (
is_multisite()) {
            
switch_to_blog($task->get_option('blog_id'1));
            
$stats get_post_meta($attachment_id'smush-stats'true);
            
restore_current_blog();
        } else {
            
$stats get_post_meta($attachment_id'smush-stats'true);
        }

        
$original_size = isset($stats['original-size']) ? $stats['original-size'] : 0;
        
$compressed_size = isset($stats['smushed-size']) ? $stats['smushed-size'] : 0;
        
$percent = isset($stats['savings-percent']) ? $stats['savings-percent'] : 0;
        
$saved $original_size $compressed_size;
        
$completed_task_count++;

        
$total_bytes_saved += $saved;
        
$total_percent_saved = (($total_percent_saved * ($completed_task_count 1)) + $percent) / $completed_task_count;

        
$this->options->update_option('completed_task_count'$completed_task_count);
        
$this->options->update_option('total_bytes_saved'$total_bytes_saved);
        
$this->options->update_option('total_percent_saved'$total_percent_saved);
    }

    
/**
     * Cleans out all complete + failed tasks from the DB.
     *
     * @param String $type type of the task
     * @return bool - true if processing complete
     */
    
public function clean_up_old_tasks($type) {
        
$completed_tasks $this->get_tasks('all'$type);

        if (!
$completed_tasks) return false;

        
$this->log(sprintf('Cleaning up tasks of type (%s). A total of %d tasks will be deleted.'$typecount($completed_tasks)));

        foreach (
$completed_tasks as $task) {
            
$task->delete_meta();
            
$task->delete();
        }

        return 
true;
    }

    
/**
     * Get current smush options.
     *
     * @return array
     */
    
public function get_smush_options() {
        static 
$smush_options = array();
        if (empty(
$smush_options)) {
            
$smush_options = array(
                
'compression_server' => $this->options->get_option('compression_server'$this->get_default_webservice()),
                
'image_quality' => $this->options->get_option('image_quality''very_good'),
                
'lossy_compression' => $this->options->get_option('lossy_compression'false),
                
'back_up_original' => $this->options->get_option('back_up_original'true),
                
'back_up_delete_after' => $this->options->get_option('back_up_delete_after'true),
                
'back_up_delete_after_days' => $this->options->get_option('back_up_delete_after_days'50),
                
'preserve_exif' => $this->options->get_option('preserve_exif'false),
                
'autosmush' => $this->options->get_option('autosmush'false),
                
'show_smush_metabox' => $this->options->get_option('show_smush_metabox''show') == 'show' true false
            
);
        }
        return 
$smush_options;
    }

    
/**
     * Updates global smush options
     *
     * @param array $options - sent in via AJAX
     * @return bool - status of the update
     */
    
public function update_smush_options($options) {
        
        foreach (
$options as $option => $value) {
            
$this->options->update_option($option$value);
        }

        return 
true;
    }

    
/**
     * Clears smush related stats
     *
     * @return bool - status of the update
     */
    
public function clear_smush_stats() {
        
$this->options->update_option('failed_task_count'0);
        
$this->options->update_option('completed_task_count'false);
        
$this->options->update_option('total_bytes_saved'false);
        
$this->options->update_option('total_percent_saved'0);

        return 
true;
    }

    
/**
     * Returns array of translations used in javascript code.
     *
     * @return array - translations used in JS
     */
    
public function smush_js_translations() {
        return 
apply_filters('updraft_smush_js_translations', array(
            
'all_images_compressed'           => __('No uncompressed images were found.''wp-optimize'),
            
'error_unexpected_response'       => __('An unexpected response was received from the server. More information has been logged in the browser console.''wp-optimize'),
            
'compress_single_image_dialog'      => __('Please wait: compressing the selected image.''wp-optimize'),
            
'error_try_again_later'              => __('Please try again later.''wp-optimize'),
            
'server_check'                      => __('Connecting to the Smush API server, please wait''wp-optimize'),
            
'please_wait'                      => __('Please wait while the request is being processed''wp-optimize'),
            
'server_error'                      => __('There was an error connecting to the image compression server. This could mean either the server is temporarily unavailable or there are connectivity issues with your internet connection. Please try later.''wp-optimize'),
            
'please_select_images'                => __('Please select the images you want compressed from the "Uncompressed images" panel first''wp-optimize'),
            
'please_updating_images_info'      => __('Please wait: updating information about the selected image.''wp-optimize'),
            
'please_select_compressed_images' => __('Please select the images you want to mark as already compressed from the "Uncompressed images" panel first''wp-optimize'),
            
'view_image'                      => __('View Image''wp-optimize'),
            
'delete_image_backup_confirm'    => __('Do you really want to delete all backup images now? This action is irreversible.''wp-optimize'),
            
'mark_all_images_uncompressed'    => __('Do you really want to mark all the images as uncompressed? This action is irreversible.''wp-optimize'),
            
'restore_images_from_backup'    => __('Do you want to restore the original images from the backup (where they exist?)''wp-optimize'),
            
'restore_all_compressed_images'    => __('Do you really want to restore all the compressed images?''wp-optimize'),
        ));
    }

    
/**
     * Adds a smush metabox on the post edit screen for images
     *
     * @param WP_Post $post - a post object
     */
    
public function add_smush_metabox($post) {

        if (!
wp_attachment_is_image($post->ID)) return;

        if (!
file_exists(get_attached_file($post->ID))) {
            return;
        }

        
add_meta_box('smush-metabox'__('Compress Image''wp-optimize'), array($this'render_smush_metabox'), 'attachment''side');
    }

    
/**
     * Renders a metabox on the post edit screen for images
     *
     * @param WP_Post $post - a post object
     */
    
public function render_smush_metabox($post) {

        
$compressed get_post_meta($post->ID'smush-complete'true) ? true false;
        
$has_backup get_post_meta($post->ID'original-file'true) ? true false;

        
$smush_info get_post_meta($post->ID'smush-info'true);
        
$marked get_post_meta($post->ID'smush-marked'false);
        
        
$options Updraft_Smush_Manager()->get_smush_options();

        
$file get_attached_file($post->ID);
        
$file_size = ($file && is_file($file)) ? filesize($file) : 0;

        
$extract = array(
            
'post_id'             => $post->ID,
            
'smush_display'        => $compressed "style='display:none;'" "style='display:block;'",
            
'restore_display'     => $compressed "style='display:block;'" "style='display:none;'",
            
'restore_action'    => $has_backup "style='display:block;'" "style='display:none;'",
            
'smush_mark'        => !$compressed && !$marked "style='display:block;'" "style='display:none;'",
            
'smush_unmark'      => $marked "style='display:block;'" "style='display:none;'",
            
'smush_info'        => $smush_info $smush_info ' ',
            
'file_size'            => $file_size,
            
'smush_options'     => $options,
            
'custom'            => 100 == $options['image_quality'] || 90 == $options['image_quality'] ? false true
        
);

        
$extract['compressed_by_another_plugin'] = $this->is_image_compressed_by_another_plugin($post->ID);

        
WP_Optimize()->include_template('admin-metabox-smush.php'false$extract);
    }

    
/**
     * Check if a single image compressed by another plugin.
     *
     * @param int $image_id
     * @return bool
     */
    
private function is_image_compressed_by_another_plugin($image_id) {
        global 
$wpdb;

        
$meta $wpdb->get_results("SELECT meta_key, meta_value FROM {$wpdb->postmeta} WHERE `post_id`={$image_id}"ARRAY_A);

        if (
is_array($meta)) {
            foreach (
$meta as $row) {
                
// Smush, Imagify, Compress JPEG & PNG images by TinyPNG.
                
if (in_array($row['meta_key'], array('wp-smpro-smush-data''_imagify_optimization_level''tiny_compress_images'))) return true;
                
// ShortPixel Image Optimizer
                
if ('_shortpixel_status' == $row['meta_key'] && <= $row['meta_key'] && $row['meta_key']) return true;
            }
        }

        if (
WP_Optimize()->get_db_info()->table_exists('ewwwio_images')) {
            
$old_show_errors $wpdb->show_errors(false);
            
// EWWW Image Optimizer.
            
$ewww_image $wpdb->get_col("SELECT attachment_id FROM {$wpdb->prefix}ewwwio_images WHERE `attachment_id`={$image_id} LIMIT 1");
            if (!empty(
$ewww_image)) return true;
            
$wpdb->show_errors($old_show_errors);
        }

        return 
apply_filters('wpo_image_compressed_by_another_plugin'false);
    }

    
/**
     * Get attachment ids for images those already compressed with EWWW Image Optimizer.
     * Used with filter `wpo_get_uncompressed_images_args`.
     *
     * @param array $args WP_Query arguments.
     * @return array
     */
    
public function ewww_image_optimizer_compressed_images_args($args) {
        global 
$wpdb;

        if (!
WP_Optimize()->get_db_info()->table_exists('ewwwio_images')) return $args;

        
$old_show_errors $wpdb->show_errors(false);
        
$compressed_images $wpdb->get_col("SELECT DISTINCT(attachment_id) FROM {$wpdb->prefix}ewwwio_images");
        
$wpdb->show_errors($old_show_errors);

        if (isset(
$args['post__not_in'])) {
            
$args['post__not_in'] = array_merge($args['post__not_in'], $compressed_images);
        } else {
            
$args['post__not_in'] = $compressed_images;
        }

        return 
$args;
    }

    
/**
     * Returns a list of images for smush (from cache if available)
     *
     * @return array - uncompressed images
     */
    
public function get_uncompressed_images() {
        
        
$uncompressed_images $this->get_from_cache('uncompressed_images');

        if (
$uncompressed_images) return $uncompressed_images;

        
$uncompressed_images = array();

        
$args = array(
            
'post_type'        => 'attachment',
            
'post_mime_type' => 'image',
            
'post_status'    => 'inherit',
            
'posts_per_page' => apply_filters('updraft_smush_posts_per_page'1000),
            
'meta_query' => array(
                
'relation' => 'AND',
                array(
                    
'relation' => 'OR',
                    array(
                        
'key'     => 'smush-complete',
                        
'compare' => '!=',
                        
'value'   => '1',
                    ),
                    array(
                        
'key'     => 'smush-complete',
                        
'compare' => 'NOT EXISTS',
                        
'value'   => '',
                    ),
                ),
                
// ShortPixel Image Optimizer plugin
                
array(
                    
'relation' => 'OR',
                    array(
                        
'key'    => '_shortpixel_status',
                        
'compare' => '<',
                        
'value'   => '2',
                    ),
                    array(
                        
'key'    => '_shortpixel_status',
                        
'compare' => '>=',
                        
'value'   => '3',
                    ),
                    array(
                        
'key'     => '_shortpixel_status',
                        
'compare' => 'NOT EXISTS',
                        
'value'   => '',
                    ),
                ),
                
// Smush plugin
                
array(
                    
'key'     => 'wp-smpro-smush-data',
                    
'compare' => 'NOT EXISTS',
                    
'value'   => '',
                ),
                
// Imagify
                
array(
                    
'key'     => '_imagify_optimization_level',
                    
'compare' => 'NOT EXISTS',
                    
'value'   => '',
                ),
                
// Compress JPEG & PNG images by TinyPNG
                
array(
                    
'key'     => 'tiny_compress_images',
                    
'compare' => 'NOT EXISTS',
                    
'value'   => '',
                ),
            )
        );

        if (
is_multisite()) {

            
$sites WP_Optimize()->get_sites();

            foreach (
$sites as $site) {
                
                
switch_to_blog($site->blog_id);

                
$args apply_filters('wpo_get_uncompressed_images_args'$args);
                
$images = new WP_Query($args);

                foreach (
$images->posts as $image) {
                    if (
file_exists(get_attached_file($image->ID))) {
                        
$uncompressed_images[$site->blog_id][] = array(
                            
'id' => $image->ID,
                            
'thumb_url' => wp_get_attachment_thumb_url($image->ID),
                            
'filesize'  => filesize(get_attached_file($image->ID))
                        );
                    } else {
                        
$this->log("Could not find file for image: blog_id={$site->blog_id}, ID={$image->ID}, file=".get_attached_file($image->ID));
                    }
                }

                
restore_current_blog();
            }

        } else {
            
$args apply_filters('wpo_get_uncompressed_images_args'$args);
            
$images = new WP_Query($args);
            foreach (
$images->posts as $image) {
                if (
file_exists(get_attached_file($image->ID))) {
                    
$uncompressed_images[1][] = array(
                        
'id' => $image->ID,
                        
'thumb_url' => wp_get_attachment_thumb_url($image->ID),
                        
'filesize'  => filesize(get_attached_file($image->ID))
                    );
                } else {
                        
$this->log("Could not find file for image: ID={$image->ID}, file=".get_attached_file($image->ID));
                }
            }
        }

        
$this->save_to_cache('uncompressed_images'$uncompressed_images);
        return 
$uncompressed_images;
    }

    
/**
     * Returns a list of admin URLs. This is to prevent unnecessary bloat in the output of get_uncompressed_images() (and thus better performance over the network on sites with huge numbers of images)
     *
     * @return array - list of admin URLs
     */
    
public function get_admin_urls() {
        
        
$admin_urls $this->get_from_cache('admin_urls');

        if (
$admin_urls) return $admin_urls;

        
$admin_urls = array();

        if (
is_multisite()) {
        
            
$sites WP_Optimize()->get_sites();

            foreach (
$sites as $site) {
                
switch_to_blog($site->blog_id);
                
$admin_urls[$site->blog_id] = admin_url();
                
restore_current_blog();
            }

        } else {
            
// The pseudo-blog_id here (1) matches (and must match) what is used in get_uncompressed_images
            
$admin_urls[1] = admin_url();
        }

        
$this->save_to_cache('admin_urls'$admin_urls);
        return 
$admin_urls;
    }
    
    
/**
     * Check if a task exists for a given image
     *
     * @param string $image - The attachment ID of the image
     * @return bool - true if yes, false otherwise
     */
    
public function task_exists($image) {
        
        
$pending_tasks $this->get_active_tasks('smush');
        
$queued_images = array();

        if (!empty(
$pending_tasks)) {
            foreach (
$pending_tasks as $task) {
                
$queued_images[] = $task->get_option('attachment_id');
            }
        }
        return 
in_array($image$queued_images);
    }

    
/**
     * Returns the status of images compressed in this iteration of the bulk compress
     *
     * @param array $images - List of images in the current session
     *
     * @return array - status of the operation
     */
    
public function get_session_stats($images) {
        
$stats = array();

        foreach (
$images as $image) {
            if (
is_multisite()) {
                
switch_to_blog($image['blog_id'], 1);
                
$stats[] = get_post_meta($image['attachment_id'], 'smush-complete'true) ? 'success' 'fail';
                
restore_current_blog();
            } else {
                
$stats[] = get_post_meta($image['attachment_id'], 'smush-complete'true) ? 'success' 'fail';
            }
        }

        return 
array_count_values($stats);
    }

    
/**
     * Returns a list of images for smush (from cache if available)
     *
     * @return array - List of task objects with uncompressed images
     */
    
public function get_pending_tasks() {
        return 
$this->get_active_tasks('smush');
    }

    
/**
     * Deletes and removes any pending tasks from queue
     */
    
public function clear_pending_images() {

        
$pending_tasks $this->get_active_tasks('smush');

        foreach (
$pending_tasks as $task) {
            
$task->delete_meta();
            
$task->delete();
        }
        
        return 
true;
    }


    
/**
     * Returns a count of failed tasks
     *
     * @return int -  failed tasks
     */
    
public function get_failed_task_count() {
        return 
$this->options->get_option('failed_task_count'0);
    }

    
/**
     * Adds the required scripts and styles
     */
    
public function admin_enqueue_scripts() {
        
$current_screen get_current_screen();
        
// load scripts and styles only on WP-Optimize pages or if show_smush_metabox option enabled.
        
if (!preg_match('/wp\-optimize/i'$current_screen->id) && 'show' != $this->options->get_option('show_smush_metabox''show')) return;

        
$enqueue_version = (defined('WP_DEBUG') && WP_DEBUG) ? WPO_VERSION.'.'.time() : WPO_VERSION;
        
$min_or_not = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? '' '.min';
        
$min_or_not_internal = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? '' '-'str_replace('.''-'WPO_VERSION). '.min';
        
        
$js_variables $this->smush_js_translations();
        
$js_variables['ajaxurl'] = admin_url('admin-ajax.php');
        
$js_variables['features'] = $this->get_features();

        
$js_variables['smush_ajax_nonce'] = wp_create_nonce('updraft-task-manager-ajax-nonce');

        
wp_enqueue_script('block-ui-js'WPO_PLUGIN_URL.'js/jquery.blockUI'.$min_or_not.'.js', array('jquery'), $enqueue_version);
        
wp_enqueue_script('smush-js'WPO_PLUGIN_URL.'js/wposmush'.$min_or_not_internal.'.js', array('jquery''block-ui-js'), $enqueue_version);
        
wp_enqueue_style('smush-css'WPO_PLUGIN_URL.'css/smush'.$min_or_not_internal.'.css', array(), $enqueue_version);
        
wp_localize_script('smush-js''wposmush'$js_variables);
    }

    
/**
     * Gets default service provider for smush
     *
     * @return string - service name
     */
    
public function get_default_webservice() {
        return 
'resmushit';
    }

    
/**
     * Sets default options for smush
     */
    
public function set_default_options() {

        
$options = array(
            
'compression_server' => $this->get_default_webservice(),
            
'image_quality'         => 'very_good',
            
'lossy_compression'     => false,
            
'back_up_original'     => true,
            
'preserve_exif'         => false,
            
'autosmush'             => false,
            
'back_up_delete_after' => $this->options->get_option('back_up_delete_after'true),
            
'back_up_delete_after_days' => $this->options->get_option('back_up_delete_after_days'50),
        );
        
        
$this->update_smush_options($options);
    }

    
/**
     * Gets default service provider for smush
     *
     * @param string $server - The name of the server
     * @return string - associated task type, default if none found
     */
    
public function get_associated_task($server) {
        
$allowed $this->get_allowed_services();

        if (
key_exists($server$allowed))
            return 
$allowed[$server];

        
$default $this->get_default_webservice();
        return 
$allowed[$default];
    }

    
/**
     * Gets allowed service providers for smush
     *
     * @return array - key value pair of service name => task name
     */
    
public function get_allowed_services() {
        return array(
            
'resmushit'  => 'Re_Smush_It_Task',
        );
    }

    
/**
     * Gets allowed service provider features smush
     *
     * @return array - key value pair of service name => features exposed
     */
    
public function get_features() {
        
$features = array();
        foreach (
$this->get_allowed_services() as $service => $class_name) {
            
$features[$service] = call_user_func(array($class_name'get_features'));
        }
        return 
$features;
    }

    
/**
     * Returns the path to the logfile
     *
     * @return string - file path
     */
    
public function get_logfile_path() {
        
$upload_dir wp_upload_dir();
        
$upload_base $upload_dir['basedir'];
        return 
$upload_base '/smush-' substr(md5(wp_salt()), 020) . '.log';
    }

    
/**
     * Adds a logger to the task
     *
     * @param Mixed $task - a task object
     */
    
public function set_task_logger($task) {
        if (!
$this->logger) {
            
$this->logger = new Updraft_File_Logger($this->get_logfile_path());
        }
        
        if (!
$task->get_loggers()) {
            
$task->add_logger($this->logger);
        }
    }

    
/**
     * Writes a standardised header to the log file
     */
    
public function write_log_header() {
        
        global 
$wpdb;
        
        
// phpcs:disable
        
$wp_version $this->get_wordpress_version();
        
$mysql_version $wpdb->db_version();
        
$safe_mode $this->detect_safe_mode();
        
$max_execution_time = (int) @ini_get("max_execution_time");

        
$memory_limit ini_get('memory_limit');
        
$memory_usage round(@memory_get_usage(false)/10485761);
        
$total_memory_usage round(@memory_get_usage(true)/10485761);

        
// Attempt to raise limit
        
@set_time_limit(90);

        
$log_header = array();

        
// phpcs:enable
        
$log_header[] = "\n";
        
$log_header[] = "Header for logs at time:  ".date('r')." on ".network_site_url();
        
$log_header[] = "WP: ".$wp_version;
        
$log_header[] = "PHP: ".phpversion()." (".PHP_SAPI.", ".@php_uname().")";// phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
        
$log_header[] = "MySQL: $mysql_version";
        
$log_header[] = "WPLANG: ".get_locale();
        
$log_header[] = "Server: ".$_SERVER["SERVER_SOFTWARE"];
        
$log_header[] = "Outbound connections: ".(defined('WP_HTTP_BLOCK_EXTERNAL') ? 'Y' 'N');
        
$log_header[] = "safe_mode: $safe_mode";
        
$log_header[] = "max_execution_time: $max_execution_time";
        
$log_header[] = "memory_limit: $memory_limit (used: ${memory_usage}M | ${total_memory_usage}M)";
        
$log_header[] = "multisite: ".(is_multisite() ? 'Y' 'N');
        
$log_header[] = "openssl: ".(defined('OPENSSL_VERSION_TEXT') ? OPENSSL_VERSION_TEXT 'N');


        foreach (
$log_header as $log_entry) {
            
$this->log($log_entry);
        }

        
$memlim $this->memory_check_current();

        if (
$memlim<65 && $memlim>0) {
            
$this->log(sprintf('The amount of memory (RAM) allowed for PHP is very low (%s Mb) - you should increase it to avoid failures due to insufficient memory (consult your web hosting company for more help)'round($memlim1)), 'warning');
        }

        if (
$max_execution_time>&& $max_execution_time<20) {
            
$this->log(sprintf('The amount of time allowed for WordPress plugins to run is very low (%s seconds) - you should increase it to avoid failures due to time-outs (consult your web hosting company for more help - it is the max_execution_time PHP setting; the recommended value is %s seconds or more)'$max_execution_time90), 'warning');
        }
    }

    
/**
     * Prunes the log file
     */
    
public function prune_smush_logs() {
        
$this->log("Pruning the smush log file");
        
$this->logger->prune_logs();
    }

    
/**
     * Get the WordPress version
     *
     * @return String - the version
     */
    
public function get_wordpress_version() {
        static 
$got_wp_version false;
        
        if (!
$got_wp_version) {
            global 
$wp_version;
            @include(
ABSPATH.WPINC.'/version.php');// phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
            
$got_wp_version $wp_version;
        }

        return 
$got_wp_version;
    }

    
/**
     * Get the current memory limit
     *
     * @return String - memory limit in megabytes
     */
    
public function memory_check_current($memory_limit false) {
        
// Returns in megabytes
        
if (false == $memory_limit$memory_limit ini_get('memory_limit');
        
$memory_limit rtrim($memory_limit);
        
$memory_unit $memory_limit[strlen($memory_limit)-1];
        if (
== (int) $memory_unit && '0' !== $memory_unit) {
            
$memory_limit substr($memory_limit0strlen($memory_limit)-1);
        } else {
            
$memory_unit '';
        }
        switch (
$memory_unit) {
            case 
'':
            
$memory_limit floor($memory_limit/1048576);
                break;
            case 
'K':
            case 
'k':
            
$memory_limit floor($memory_limit/1024);
                break;
            case 
'G':
            
$memory_limit $memory_limit*1024;
                break;
            case 
'M':
            
// assumed size, no change needed
                
break;
        }
        return 
$memory_limit;
    }

    
/**
     * Detect if safe_mode is on
     *
     * @return Integer - 1 or 0
     */
    
public function detect_safe_mode() {
        return (@
ini_get('safe_mode') && strtolower(@ini_get('safe_mode')) != "off") ? 0;// phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
    
}

    
/**
     * Saves a value to the cache.
     *
     * @param string $key
     * @param mixed  $value
     * @param int    $blog_id
     */
    
public function save_to_cache($key$value$blog_id 1) {
        
$transient_limit 3600 48;
        
$key 'wpo_smush_cache_' $blog_id '_'$key;

        return 
WP_Optimize_Transients_Cache::get_instance()->set($key$value$transient_limit);
    }

    
/**
     * Gets value from the cache.
     *
     * @param string $key
     * @param int    $blog_id
     * @return mixed
     */
    
public function get_from_cache($key$blog_id 1) {
        
$key 'wpo_smush_cache_' $blog_id '_'$key;

        
$value WP_Optimize_Transients_Cache::get_instance()->get($key);

        return 
$value;
    }

    
/**
     * Deletes a value from the cache.
     *
     * @param string $key
     * @param int    $blog_id
     */
    
public function delete_from_cache($key$blog_id 1) {
        
$key 'wpo_smush_cache_' $blog_id '_'$key;

        
WP_Optimize_Transients_Cache::get_instance()->delete($key);

        
$this->delete_transient($key);
    }

    
/**
     * Wrapper for deleting a transient
     *
     * @param string $key
     */
    
public function delete_transient($key) {
        if (
$this->is_multisite_mode()) {
            
delete_site_transient($key);
        } else {
            
delete_transient($key);
        }
    }

    
/**
     * Removes all cached data
     */
    
public function clear_cached_data() {
        global 
$wpdb;

        
// get list of cached data by optimization.
        
if ($this->is_multisite_mode()) {
            
$keys $wpdb->get_col("SELECT meta_key FROM {$wpdb->sitemeta} WHERE meta_key LIKE '%wpo_smush_cache_%'");
        } else {
            
$keys $wpdb->get_col("SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE '%wpo_smush_cache_%'");
        }

        if (!empty(
$keys)) {
            
$transient_keys = array();
            foreach (
$keys as $key) {
                
preg_match('/wpo_smush_cache_.+/'$key$option_name);
                
$option_name $option_name[0];
                
$transient_keys[] = $option_name;
            }

            
// get unique keys.
            
$transient_keys array_unique($transient_keys);

            
// delete transients.
            
foreach ($transient_keys as $key) {
                
$this->delete_transient($key);
            }
        }
    }

    
/**
     * Delete recursively all smush backup files created more that $days_ago days.
     *
     * @param string $directory upload directory
     * @param int    $days_ago
     */
    
public function clear_backup_images_directory($directory$days_ago 30) {

        
$directory trailingslashit($directory);
        
$current_time time();

        if (
preg_match('/(\d{4})\/(\d{2})\/$/'$directory$match)) {

            
$check_date false;

            if (
$days_ago 0) {
                
// check if it is end directory then scan for backup images.
                
$year = (int) $match[1];
                
$month = (int) $match[2];

                
$limit strtotime('-'.$days_ago.' '.(($days_ago 1) ? 'days' 'day'));
                
$year_limit = (int) date('Y'$limit);
                
$month_limit = (int) date('m'$limit);
                
$day_limit = (int) date('j'$limit);

                
// if current directory is newer than needed then we skip it.
                
if ($year_limit $year || ($year_limit == $year && $month_limit $month)) {
                    return;
                }

                
// we will check dates only in directory that contain limit date.
                
$check_date = ($year_limit == $year && $month_limit == $month);
            }

            
// GLOB_BRACE isn't defined on some systems (Solaris, SunOS and more) > https://www.php.net/manual/en/function.glob.php
            
$files glob($directory '*-updraft-pre-smush-original.*', (defined('GLOB_BRACE') ? GLOB_BRACE 0));

            foreach (
$files as $file) {
                if (
$check_date) {
                    
$filedate_day = (int) date('j'filectime($file));
                    if (
$filedate_day >= $day_limit) continue;
                }

                
unlink($file);
            }

        } else {
            
// scan directories recursively.
            
$handle opendir($directory);

            if (
false === $handle) return;

            
$file readdir($handle);

            while (
false !== $file) {

                if (
'.' == $file || '..' == $file) {
                    
$file readdir($handle);
                    continue;
                }

                if (
is_dir($directory $file)) {
                    
$this->clear_backup_images_directory($directory $file$days_ago);
                } elseif (
is_file($directory $file) && preg_match('/^.+-updraft-pre-smush-original\.\S{3,4}/i'$file)) {
                    
// check the file time and compare with $days_ago.
                    
$filedate_day = (int) filectime($directory $file);
                    if (
$filedate_day && ($current_time $filedate_day) / 86400 >= $days_agounlink($directory $file);
                }

                
$file readdir($handle);
            }
        }

    }

    
/**
     * Clean backup smush images according to saved options.
     */
    
public function clear_backup_images() {
        
$back_up_delete_after $this->options->get_option('back_up_delete_after'false);

        if (!
$back_up_delete_after) return;

        
$back_up_delete_after_days $this->options->get_option('back_up_delete_after_days'50);

        
$upload_dir wp_upload_dir(nullfalse);
        
$base_dir $upload_dir['basedir'];

        
$this->clear_backup_images_directory($base_dir$back_up_delete_after_days);
    }

    
/**
     * Check if attachment already compressed.
     *
     * @param int $attachment_id
     *
     * @return bool
     */
    
public function is_compressed($attachment_id) {
        return (
true == get_post_meta($attachment_id'smush-complete'true));
    }

    
/**
     * @param array   $form_fields
     * @param WP_Post $post
     *
     * @return array
     */
    
public function add_compress_button_to_media_modal($form_fields$post) {

        if (!
is_admin() || !function_exists('get_current_screen')) return $form_fields;
    
        
/**
         * In media modal get_current_screen() return null or id = 'async-upload' We don't need add smush fields elsewhere.
         */
        
$current_screen get_current_screen();
        if (
null !== $current_screen && 'async-upload' != $current_screen->id) return $form_fields;

        
/**
         * Don't show additional fields for non-image attachments.
         */
        
if (!wp_attachment_is_image($post->ID)) return $form_fields;

        
ob_start();
        
$this->render_smush_metabox($post);
        
$smush_metabox ob_get_contents();
        
ob_end_clean();

        
$form_fields['wpo_compress_image'] = array(
            
'value'            => '',
            
'label'         => __('Compress image''wp-optimize'),
            
'input'         => 'html',
            
'html'          => $smush_metabox,
        );

        return 
$form_fields;
    }

    
/**
     * Returns true if multisite
     *
     * @return bool
     */
    
public function is_multisite_mode() {
        return 
WP_Optimize()->is_multisite_mode();
    }

    
/**
     * This callback function is triggered due to delete_attachment action (wp-includes/post.php) and is executed prior to deletion of post-type attachment
     *
     * @param int $post_id - WordPress Post ID
     */
    
public function unscheduled_original_file_deletion($post_id) {
        
$the_original_file get_post_meta($post_id'original-file'true);
        if (
'' != $the_original_file && file_exists($the_original_file)) {
            @
unlink($the_original_file);// phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
        
}
    }

    
/**
     * Instance of WP_Optimize_Page_Cache_Preloader.
     *
     * @return self
     */
    
public static function instance() {
        if (empty(
self::$_instance)) {
            
self::$_instance = new self();
        }

        return 
self::$_instance;
    }
}

/**
 * Returns a Updraft_Smush_Manager instance
 */
function Updraft_Smush_Manager() {
    return 
Updraft_Smush_Manager::instance();
}

endif;
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586