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
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
|
<?php namespace Imagify\Optimization\Process;
use Imagify\Job\MediaOptimization; use Imagify\Optimization\Data\DataInterface; use Imagify\Optimization\File;
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
/** * Abstract class used to optimize medias. * * @since 1.9 * @author Grégory Viguier */ abstract class AbstractProcess implements ProcessInterface { use \Imagify\Deprecated\Traits\Optimization\Process\AbstractProcessDeprecatedTrait;
/** * The suffix used in the thumbnail size name. * * @var string * @since 1.9 * @author Grégory Viguier */ const WEBP_SUFFIX = '@imagify-webp';
/** * The suffix used in file name to create a temporary copy of the full size. * * @var string * @since 1.9 * @author Grégory Viguier */ const TMP_SUFFIX = '@imagify-tmp';
/** * Used for the name of the transient telling if a media is locked. * %1$s is the context, %2$s is the media ID. * * @var string * @since 1.9 * @author Grégory Viguier */ const LOCK_NAME = 'imagify_%1$s_%2$s_process_locked';
/** * The data optimization object. * * @var DataInterface * @since 1.9 * @access protected * @author Grégory Viguier */ protected $data;
/** * The optimization data format. * * @var array * @since 1.9 * @access protected * @author Grégory Viguier */ protected $data_format = [ 'level' => null, 'status' => null, 'success' => null, 'error' => null, 'original_size' => null, 'optimized_size' => null, ];
/** * A File instance. * * @var File * @since 1.9 * @access protected * @author Grégory Viguier */ protected $file;
/** * Filesystem object. * * @var Imagify_Filesystem * @since 1.9 * @access protected * @author Grégory Viguier */ protected $filesystem;
/** * Used to cache the plugin’s options. * * @var array * @since 1.9 * @access protected * @author Grégory Viguier */ protected $options = [];
/** * The constructor. * * @since 1.9 * @access public * @see self::constructor_accepts() * @author Grégory Viguier * * @param mixed $id An ID, or whatever type the constructor accepts. */ public function __construct( $id ) { if ( $id instanceof DataInterface ) { $this->data = $id; } elseif ( static::constructor_accepts( $id ) ) { $data_class = str_replace( '\\Optimization\\Process\\', '\\Optimization\\Data\\', get_called_class() ); $data_class = '\\' . ltrim( $data_class, '\\' ); $this->data = new $data_class( $id ); } else { $this->data = false; }
$this->filesystem = \Imagify_Filesystem::get_instance(); }
/** * Tell if the given entry can be accepted in the constructor. * * @since 1.9 * @access public * @author Grégory Viguier * * @param mixed $id Whatever. * @return bool */ public static function constructor_accepts( $id ) { if ( $id instanceof DataInterface ) { return true; }
$data_class = str_replace( '\\Optimization\\Process\\', '\\Optimization\\Data\\', get_called_class() ); $data_class = '\\' . ltrim( $data_class, '\\' );
return $data_class::constructor_accepts( $id ); }
/** * Get the data instance. * * @since 1.9 * @access public * @author Grégory Viguier * * @return DataInterface|false */ public function get_data() { return $this->data; }
/** * Get the media instance. * * @since 1.9 * @access public * @author Grégory Viguier * * @return MediaInterface|false */ public function get_media() { if ( ! $this->get_data() ) { return false; }
return $this->get_data()->get_media(); }
/** * Get the File instance of the original file. * * @since 1.9.8 * @access public * @author Grégory Viguier * * @return File|false */ public function get_original_file() { if ( isset( $this->file ) ) { return $this->file; }
$this->file = false;
if ( $this->get_media() ) { $this->file = new File( $this->get_media()->get_raw_original_path() ); }
return $this->file; }
/** * Get the File instance of the full size file. * * @since 1.9.8 * @access public * @author Grégory Viguier * * @return File|false */ public function get_fullsize_file() { if ( isset( $this->file ) ) { return $this->file; }
$this->file = false;
if ( $this->get_media() ) { $this->file = new File( $this->get_media()->get_raw_fullsize_path() ); }
return $this->file; }
/** * Tell if the current media is valid. * * @since 1.9 * @access public * @author Grégory Viguier * * @return bool */ public function is_valid() { return $this->get_media() && $this->get_media()->is_valid(); }
/** * Tell if the current user is allowed to operate Imagify in this context. * * @since 1.9 * @access public * @author Grégory Viguier * * @param string $describer Capacity describer. See \Imagify\Context\ContextInterface->get_capacity() for possible values. Can also be a "real" user capacity. * @return bool */ public function current_user_can( $describer ) { if ( ! $this->is_valid() ) { return false; }
$media = $this->get_media();
return $media->get_context_instance()->current_user_can( $describer, $media->get_id() ); }
/** ----------------------------------------------------------------------------------------- */ /** OPTIMIZATION ============================================================================ */ /** ----------------------------------------------------------------------------------------- */
/** * Optimize a media files. * * @since 1.9 * @access public * @author Grégory Viguier * * @param int $optimization_level The optimization level (0=normal, 1=aggressive, 2=ultra). * @param array $args An array of optionnal arguments. * @return bool|WP_Error True if successfully launched. A \WP_Error instance on failure. */ public function optimize( $optimization_level = null, $args = [] ) { if ( ! $this->is_valid() ) { return new \WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) ); }
$media = $this->get_media();
if ( ! $media->is_supported() ) { return new \WP_Error( 'media_not_supported', __( 'This media is not supported.', 'imagify' ) ); }
$data = $this->get_data();
if ( $data->is_optimized() ) { return new \WP_Error( 'optimized', __( 'This media has already been optimized by Imagify.', 'imagify' ) ); }
if ( $data->is_already_optimized() && $this->has_webp() ) { // If already optimized but has webp, delete webp versions and optimization data. $data->delete_optimization_data(); $deleted = $this->delete_webp_files();
if ( is_wp_error( $deleted ) ) { return new \WP_Error( 'webp_not_deleted', __( 'Previous webp files could not be deleted.', 'imagify' ) ); } }
$sizes = $media->get_media_files(); $args = is_array( $args ) ? $args : [];
$args['hook_suffix'] = 'optimize_media';
// Optimize. return $this->optimize_sizes( array_keys( $sizes ), $optimization_level, $args ); }
/** * Re-optimize a media files with a different level. * * @since 1.9 * @access public * @author Grégory Viguier * * @param int $optimization_level The optimization level (0=normal, 1=aggressive, 2=ultra). * @param array $args An array of optionnal arguments. * @return bool|WP_Error True if successfully launched. A \WP_Error instance on failure. */ public function reoptimize( $optimization_level = null, $args = [] ) { if ( ! $this->is_valid() ) { return new \WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) ); }
$media = $this->get_media();
if ( ! $media->is_supported() ) { return new \WP_Error( 'media_not_supported', __( 'This media is not supported.', 'imagify' ) ); }
$data = $this->get_data();
if ( ! $data->get_optimization_status() ) { return new \WP_Error( 'not_processed_yet', __( 'This media has not been processed yet.', 'imagify' ) ); }
$optimization_level = $this->sanitize_optimization_level( $optimization_level );
if ( $data->get_optimization_level() === $optimization_level ) { return new \WP_Error( 'identical_optimization_level', __( 'This media is already optimized with this level.', 'imagify' ) ); }
$this->restore();
$sizes = $media->get_media_files(); $args = is_array( $args ) ? $args : [];
$args['hook_suffix'] = 'reoptimize_media';
// Optimize. return $this->optimize_sizes( array_keys( $sizes ), $optimization_level, $args ); }
/** * Optimize several file sizes by pushing tasks into the queue. * * @since 1.9 * @access public * @see MediaOptimization->task_before() * @see MediaOptimization->task_after() * @author Grégory Viguier * * @param array $sizes An array of media sizes (strings). Use "full" for the size of the main file. * @param int $optimization_level The optimization level (0=normal, 1=aggressive, 2=ultra). * @param array $args { * An array of optionnal arguments. * * @type string $hook_suffix Suffix used to trigger hooks before and after optimization. * } * @return bool|WP_Error True if successfully launched. A \WP_Error instance on failure. */ public function optimize_sizes( $sizes, $optimization_level = null, $args = [] ) { if ( ! $this->is_valid() ) { return new \WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) ); }
$media = $this->get_media();
if ( ! $media->is_supported() ) { return new \WP_Error( 'media_not_supported', __( 'This media is not supported.', 'imagify' ) ); }
if ( ! $sizes ) { return new \WP_Error( 'no_sizes', __( 'No sizes given to be optimized.', 'imagify' ) ); }
if ( empty( $args['locked'] ) ) { if ( $this->is_locked() ) { return new \WP_Error( 'media_locked', __( 'This media is already being processed.', 'imagify' ) ); }
$this->lock(); }
if ( $media->is_image() ) { if ( $this->get_option( 'convert_to_webp' ) ) { // Add webp convertion. $files = $media->get_media_files();
foreach ( $sizes as $size_name ) { if ( empty( $files[ $size_name ] ) ) { continue; } if ( 'image/webp' === $files[ $size_name ]['mime-type'] ) { continue; } if ( in_array( $size_name . static::WEBP_SUFFIX, $sizes, true ) ) { continue; }
array_unshift( $sizes, $size_name . static::WEBP_SUFFIX ); } }
if ( ! $media->get_context_instance()->can_backup() && ! $media->get_backup_path() && ! $this->get_data()->get_size_data( 'full', 'success' ) ) { /** * Backup is NOT activated, and a backup file does NOT exist yet, and the full size is NOT optimized yet. * Webp conversion needs a backup file, even a temporary one: we’ll create one. */ $webp = false;
foreach ( $sizes as $size_name ) { if ( $this->is_size_webp( $size_name ) ) { $webp = true; break; } }
if ( $webp ) { // We have at least one webp conversion to do: create a temporary backup. $backuped = $this->get_original_file()->backup( $media->get_raw_backup_path() );
if ( $backuped ) { // See \Imagify\Job\MediaOptimization->delete_backup(). $args['delete_backup'] = true; } } } }
$sizes = array_unique( $sizes ); $optimization_level = $this->sanitize_optimization_level( $optimization_level );
/** * Filter the data sent to the optimization process. * * @since 1.9 * @author Grégory Viguier * * @param array $new_args Additional data to send to the optimization process. * @param array $args Current data sent to the process. * @param ProcessInterface $process The current optimization process. * @param array $sizes Sizes being processed. * @param int $optimization_level Optimization level. */ $new_args = apply_filters( 'imagify_optimize_sizes_args', [], $args, $this, $sizes, $optimization_level );
if ( $new_args && is_array( $new_args ) ) { $args = array_merge( $new_args, $args ); }
/** * Push the item to the queue, save the queue in the DB, empty the queue. * A "batch" is then created in the DB with this unique item, it is then free to loop through its steps (files) without another item interfering (each media optimization has its own dedicated batch/queue). */ MediaOptimization::get_instance()->push_to_queue( [ 'id' => $media->get_id(), 'sizes' => $sizes, 'optimization_level' => $optimization_level, 'process_class' => get_class( $this ), 'data' => $args, ] )->save();
return true; }
/** * Optimize one file with Imagify directly. * * @since 1.9 * @access public * @author Grégory Viguier * * @param string $size The media size. * @param int $optimization_level The optimization level (0=normal, 1=aggressive, 2=ultra). * @return array|\WP_Error Optimized image data. A \WP_Error object on error. */ public function optimize_size( $size, $optimization_level = null ) { if ( ! $this->is_valid() ) { // Bail out. return new \WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) ); }
$media = $this->get_media(); $sizes = $media->get_media_files(); $thumb_size = $size; $webp = $this->is_size_webp( $size ); $path_is_temp = false;
if ( $webp ) { // We'll make sure the file is an image later. $thumb_size = $webp; // Contains the name of the non-webp size. $webp = true; }
if ( empty( $sizes[ $thumb_size ]['path'] ) ) { // Bail out. // This size is not in our list. return new \WP_Error( 'unknown_size', sprintf( /* translators: %s is a size name. */ __( 'The size %s is unknown.', 'imagify' ), '<code>' . esc_html( $thumb_size ) . '</code>' ) ); }
if ( $this->get_data()->get_size_data( $size, 'success' ) ) { // Bail out. // This size is already optimized with Imagify, and must not be optimized again. if ( $webp ) { return new \WP_Error( 'size_is_successfully_optimized', sprintf( /* translators: %s is a size name. */ __( 'The webp format for the size %s already exists.', 'imagify' ), '<code>' . esc_html( $thumb_size ) . '</code>' ) ); } else { return new \WP_Error( 'size_is_successfully_optimized', sprintf( /* translators: %s is a size name. */ __( 'The size %s is already optimized by Imagify.', 'imagify' ), '<code>' . esc_html( $thumb_size ) . '</code>' ) ); } }
/** * Starting from here, errors will be stored in the optimization data of the size. */ $path = $sizes[ $thumb_size ]['path'];
$optimization_level = $this->sanitize_optimization_level( $optimization_level );
if ( $webp && $this->get_data()->get_size_data( $thumb_size, 'success' ) ) { // We want a webp version but the source file is already optimized by Imagify. $result = $this->create_temporary_copy( $thumb_size, $sizes );
if ( ! $result ) { // Bail out. // Could not create a copy of the non-webp version. $response = new \WP_Error( 'non_webp_copy_failed', sprintf( /* translators: %s is a size name. */ __( 'Could not create an unoptimized copy of the size %s.', 'imagify' ), '<code>' . esc_html( $thumb_size ) . '</code>' ) );
$this->update_size_optimization_data( $response, $size, $optimization_level );
return $response; }
/** * $path now targets a temporary file. */ $path = $this->get_temporary_copy_path( $thumb_size, $sizes ); $path_is_temp = true; }
$file = new File( $path ); // Original file or temporary copy.
if ( ! $file->is_supported( $media->get_allowed_mime_types() ) ) { // Bail out. // This file type is not supported. $extension = $file->get_extension();
if ( '' === $extension ) { $response = new \WP_Error( 'no_extension', __( 'With no extension, this file cannot be optimized.', 'imagify' ) ); } else { $response = new \WP_Error( 'extension_not_supported', sprintf( /* translators: %s is a file extension. */ __( '%s cannot be optimized.', 'imagify' ), '<code>' . esc_html( strtolower( $extension ) ) . '</code>' ) ); }
if ( $path_is_temp ) { $this->filesystem->delete( $path ); }
$this->update_size_optimization_data( $response, $size, $optimization_level );
return $response; }
if ( $webp && ! $file->is_image() ) { // Bail out. if ( $path_is_temp ) { $this->filesystem->delete( $path ); }
$response = new \WP_Error( 'no_webp', __( 'This file is not an image and cannot be converted to webp format.', 'imagify' ) );
$this->update_size_optimization_data( $response, $size, $optimization_level );
return $response; }
$is_disabled = ! empty( $sizes[ $thumb_size ]['disabled'] );
/** * Fires before optimizing a file. * Return a \WP_Error object to prevent the optimization. * * @since 1.9 * @author Grégory Viguier * * @param null|\WP_Error $response Null by default. Return a \WP_Error object to prevent optimization. * @param ProcessInterface $process The optimization process instance. * @param File $file The file instance. If $webp is true, $file references the non-webp file. * @param string $thumb_size The media size. * @param int $optimization_level The optimization level (0=normal, 1=aggressive, 2=ultra). * @param bool $webp The image will be converted to webp. * @param bool $is_disabled Tell if this size is disabled from optimization. */ $response = apply_filters( 'imagify_before_optimize_size', null, $this, $file, $thumb_size, $optimization_level, $webp, $is_disabled );
if ( ! is_wp_error( $response ) ) { if ( $is_disabled ) { // This size must not be optimized. $response = new \WP_Error( 'unauthorized_size', sprintf( /* translators: %s is a size name. */ __( 'The size %s is not authorized to be optimized. Update your Imagify settings if you want to optimize it.', 'imagify' ), '<code>' . esc_html( $thumb_size ) . '</code>' ) ); } elseif ( ! $this->filesystem->exists( $file->get_path() ) ) { $response = new \WP_Error( 'file_not_exists', sprintf( /* translators: %s is a file path. */ __( 'The file %s does not seem to exist.', 'imagify' ), '<code>' . esc_html( $this->filesystem->make_path_relative( $file->get_path() ) ) . '</code>' ) ); } elseif ( $webp && ! $this->can_create_webp_version( $file->get_path() ) ) { $response = new \WP_Error( 'is_animated_gif', __( 'This file is an animated gif: since Imagify does not support animated webp, webp creation for animated gif is disabled.', 'imagify' ) ); } elseif ( ! $this->filesystem->is_writable( $file->get_path() ) ) { $response = new \WP_Error( 'file_not_writable', sprintf( /* translators: %s is a file path. */ __( 'The file %s does not seem to be writable.', 'imagify' ), '<code>' . esc_html( $this->filesystem->make_path_relative( $file->get_path() ) ) . '</code>' ) ); } else { // Maybe resize the file. $response = $this->maybe_resize( $thumb_size, $file );
if ( ! is_wp_error( $response ) ) { // Resizing succeeded: optimize the file. $response = $file->optimize( [ 'backup' => ! $response['backuped'] && $this->can_backup( $size ), 'backup_path' => $media->get_raw_backup_path(), 'backup_source' => 'full' === $thumb_size ? $media->get_original_path() : null, 'optimization_level' => $optimization_level, 'convert' => $webp ? 'webp' : '', 'keep_exif' => $this->can_keep_exif( $size ), 'context' => $media->get_context(), 'original_size' => $response['file_size'], ] );
$response = $this->compare_webp_file_size( [ 'response' => $response, 'file' => $file, 'is_webp' => $webp, 'non_webp_thumb_size' => $thumb_size, 'non_webp_file_path' => $sizes[ $thumb_size ]['path'], // Don't use $path nor $file->get_path(), it may return the path to a temporary file. 'optimization_level' => $optimization_level, ] ); } } }
$data = $this->update_size_optimization_data( $response, $size, $optimization_level );
/** * Fires after optimizing a file. * * @since 1.9 * @author Grégory Viguier * * @param ProcessInterface $process The optimization process instance. * @param File $file The file instance. * @param string $thumb_size The media size. * @param int $optimization_level The optimization level (0=normal, 1=aggressive, 2=ultra). * @param bool $webp The image was supposed to be converted to webp. * @param bool $is_disabled Tell if this size is disabled from optimization. */ do_action( 'imagify_after_optimize_size', $this, $file, $thumb_size, $optimization_level, $webp, $is_disabled );
if ( ! $path_is_temp ) { return $data; }
// Delete the temporary copy. $this->filesystem->delete( $path );
if ( is_wp_error( $response ) ) { return $data; }
// Rename the optimized file. $destination_path = str_replace( static::TMP_SUFFIX . '.', '.', $file->get_path() );
$this->filesystem->move( $file->get_path(), $destination_path, true );
return $data; }
/** * Compare the file size of a file and its webp version: if the webp version is heavier than the non-webp file, delete it. * * @since 1.9.4 * @access protected * @author Grégory Viguier * * @param array $args { * A list of mandatory arguments. * * @type \sdtClass|\WP_Error $response Optimized image data. A \WP_Error object on error. * @type File $file The File instance of the file currently being optimized. * @type bool $is_webp Tell if we're requesting a webp file. * @type string $non_webp_thumb_size Name of the corresponding non-webp thumbnail size. If we're not creating a webp file, this corresponds to the current thumbnail size. * @type string $non_webp_file_path Path to the corresponding non-webp file. If we're not creating a webp file, this corresponds to the current file path. * @type string $optimization_level The optimization level. * } * @return \sdtClass|\WP_Error Optimized image data. A \WP_Error object on error. */ protected function compare_webp_file_size( $args ) { static $keep_large_webp;
if ( ! isset( $keep_large_webp ) ) { /** * Allow to not store webp images that are larger than their non-webp version. * * @since 1.9.4 * @author Grégory Viguier * * @param bool $keep_large_webp Set to false if you prefer your visitors over your Pagespeed score. Default value is true. */ $keep_large_webp = apply_filters( 'imagify_keep_large_webp', true ); }
if ( $keep_large_webp || is_wp_error( $args['response'] ) || ! $args['file']->is_image() ) { return $args['response']; }
// Optimization succeeded. if ( $args['is_webp'] ) { /** * We just created a webp version: * Check if it is lighter than the (maybe optimized) non-webp file. */ $data = $this->get_data()->get_size_data( $args['non_webp_thumb_size'] );
if ( ! $data ) { // We haven’t tried to optimize the non-webp size yet. return $args['response']; }
if ( ! empty( $data['optimized_size'] ) ) { // The non-webp size is optimized, we know the file size. $non_webp_file_size = $data['optimized_size']; } else { // The non-webp size is "already optimized" or "error": grab the file size directly from the file. $non_webp_file_size = $this->filesystem->size( $args['non_webp_file_path'] ); }
if ( ! $non_webp_file_size || $non_webp_file_size > $args['response']->new_size ) { // The new webp file is lighter. return $args['response']; }
// The new webp file is heavier than the non-webp file: delete it and return an error. $this->filesystem->delete( $args['file']->get_path() );
return new \WP_Error( 'webp_heavy', sprintf( /* translators: %s is a size name. */ __( 'The webp version of the size %s is heavier than its non-webp version.', 'imagify' ), '<code>' . esc_html( $args['non_webp_thumb_size'] ) . '</code>' ) ); }
/** * We just created a non-webp version: * Check if its webp version file is lighter than this one. */ $webp_size = $args['non_webp_thumb_size'] . static::WEBP_SUFFIX; $webp_file_size = $this->get_data()->get_size_data( $webp_size, 'optimized_size' );
if ( ! $webp_file_size || $webp_file_size < $args['response']->new_size ) { // The webp file is lighter than this one. return $args['response']; }
// The new optimized file is lighter than the webp file: delete the webp file and store an error. $webp_path = $args['file']->get_path_to_webp();
if ( $webp_path && $this->filesystem->is_writable( $webp_path ) ) { $this->filesystem->delete( $webp_path ); }
$webp_response = new \WP_Error( 'webp_heavy', sprintf( /* translators: %s is a size name. */ __( 'The webp version of the size %s is heavier than its non-webp version.', 'imagify' ), '<code>' . esc_html( $args['non_webp_thumb_size'] ) . '</code>' ) );
$this->update_size_optimization_data( $webp_response, $webp_size, $args['optimization_level'] );
return $args['response']; }
/** * Restore the media files from the backup file. * * @since 1.9 * @access public * @author Grégory Viguier * * @return bool|WP_Error True on success. A \WP_Error instance on failure. */ public function restore() { if ( ! $this->is_valid() ) { return new \WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) ); }
$media = $this->get_media();
if ( ! $media->is_supported() ) { return new \WP_Error( 'media_not_supported', __( 'This media is not supported.', 'imagify' ) ); }
if ( ! $media->has_backup() ) { return new \WP_Error( 'no_backup', __( 'This media has no backup file.', 'imagify' ) ); }
if ( $this->is_locked() ) { return new \WP_Error( 'media_locked', __( 'This media is already being processed.', 'imagify' ) ); }
$this->lock( 'restoring' );
$backup_path = $media->get_backup_path(); $original_path = $media->get_raw_original_path();
if ( $backup_path === $original_path ) { // Uh?! $this->unlock(); return new \WP_Error( 'same_path', __( 'Image path and backup path are identical.', 'imagify' ) ); }
$dest_dir = $this->filesystem->dir_path( $original_path );
if ( ! $this->filesystem->exists( $dest_dir ) ) { $this->filesystem->make_dir( $dest_dir ); }
$dest_file_is_writable = ! $this->filesystem->exists( $original_path ) || $this->filesystem->is_writable( $original_path );
if ( ! $dest_file_is_writable || ! $this->filesystem->is_writable( $dest_dir ) ) { $this->unlock(); return new \WP_Error( 'destination_not_writable', __( 'The image to replace is not writable.', 'imagify' ) ); }
// Get some data before doing anything. $data = $this->get_data()->get_optimization_data(); $files = $media->get_media_files();
/** * Fires before restoring a media. * Return a \WP_Error object to prevent the restoration. * * @since 1.9 * @author Grégory Viguier * * @param null|\WP_Error $response Null by default. Return a \WP_Error object to prevent optimization. * @param ProcessInterface $process Instance of this process. */ $response = apply_filters( 'imagify_before_restore_media', null, $this );
if ( ! is_wp_error( $response ) ) { // Create the original image from the backup. $response = $this->filesystem->copy( $backup_path, $original_path, true );
if ( ! $response ) { // Failure. $response = new \WP_Error( 'copy_failed', __( 'The backup file could not be copied over the optimized one.', 'imagify' ) ); } else { // Backup successfully copied. $this->filesystem->chmod_file( $original_path );
// Remove old optimization data. $this->get_data()->delete_optimization_data();
if ( $media->is_image() ) { // Restore the original dimensions in the database. $media->update_dimensions();
// Delete the webp version. $this->delete_webp_file( $original_path );
// Restore the thumbnails. $response = $this->restore_thumbnails(); } } }
/** * Fires after restoring a media. * * @since 1.9 * @author Grégory Viguier * * @param ProcessInterface $process Instance of this process. * @param bool|WP_Error $response The result of the operation: true on success, a WP_Error object on failure. * @param array $files The list of files, before restoring them. * @param array $data The optimization data, before deleting it. */ do_action( 'imagify_after_restore_media', $this, $response, $files, $data );
$this->unlock();
return $response; }
/** * Restore the thumbnails. * * @since 1.9 * @access protected * @author Grégory Viguier * * @return bool|WP_Error True on success. A \WP_Error instance on failure. */ protected function restore_thumbnails() { $media = $this->get_media();
/** * Delete the webp versions. * If the full size file and the original file are not the same, the full size is considered like a thumbnail. * In that case we must also delete the webp file associated to the full size. */ $keep_full_webp = $media->get_raw_original_path() === $media->get_raw_fullsize_path(); $this->delete_webp_files( $keep_full_webp );
// Generate new thumbnails. return $media->generate_thumbnails(); }
/** ----------------------------------------------------------------------------------------- */ /** BACKUP FILE ============================================================================= */ /** ----------------------------------------------------------------------------------------- */
/** * Delete the backup file. * * @since 1.9 * @access public * @author Grégory Viguier */ public function delete_backup() { if ( ! $this->is_valid() ) { return; }
$backup_path = $this->get_media()->get_backup_path();
if ( $backup_path ) { $this->filesystem->delete( $backup_path ); } }
/** ----------------------------------------------------------------------------------------- */ /** TEMPORARY COPY OF A SIZE FILE =========================================================== */ /** ----------------------------------------------------------------------------------------- */
/** * If we need to create a webp version, we must create it from an unoptimized image. * The full size is always optimized before the webp version creation, and in some cases it’s the same for the thumbnails. * Then we use the backup file to create temporary files. */
/** * Create a temporary copy of a size file. * * @since 1.9 * @access protected * @author Grégory Viguier * * @param string $size The image size name. * @param array $sizes A list of thumbnail sizes being optimized. * @return bool True if the file exists/is created. False on failure. */ protected function create_temporary_copy( $size, $sizes = null ) { $media = $this->get_media();
if ( ! isset( $sizes ) ) { $sizes = $media->get_media_files(); }
if ( empty( $sizes[ $size ] ) ) { // What? return false; }
$tmp_path = $this->get_temporary_copy_path( $size, $sizes );
if ( $tmp_path && $this->filesystem->exists( $tmp_path ) ) { // The temporary file already exists. return true; }
$tmp_file = new File( $tmp_path );
if ( ! $tmp_file->is_image() ) { // The file is not an image. return false; }
if ( ! $tmp_file->is_supported( $media->get_allowed_mime_types() ) ) { // The file is not supported. return false; }
/** * Use the backup file as source. */ $backup_path = $media->get_backup_path();
if ( ! $backup_path ) { // No backup, no hope for you. return false; }
/** * In all cases we must make a copy of the backup file, and not use the backup directly: * sometimes the backup image does not have a valid file extension (yes I’m looking at you NextGEN Gallery). */ $copied = $this->filesystem->copy( $backup_path, $tmp_path, true );
if ( ! $copied ) { return false; }
if ( 'full' === $size ) { /** * We create a copy of the backup to be able to create a webp version from it. * That means the optimization process will resize the file if needed, so there is nothing more to do here. */ return true; }
// We need to create a thumbnail from it. $size_data = $sizes[ $size ]; $context_sizes = $media->get_context_instance()->get_thumbnail_sizes();
if ( ! empty( $context_sizes[ $size ] ) ) { // Not a dynamic size, yay! $size_data = array_merge( $size_data, $context_sizes[ $size ] ); }
if ( empty( $size_data['path'] ) ) { // Should not happen. return false; }
if ( ! isset( $size_data['crop'] ) ) { /** * In case of a dynamic thumbnail we don’t know if the image must be croped or resized. * * @since 1.9 * @author Grégory Viguier * * @param bool $crop True to crop the thumbnail, false to resize. Null by default. * @param string $size Name of the thumbnail size. * @param array $size_data Data of the thumbnail being processed. Contains at least 'width', 'height', and 'path'. * @param MediaInterface $media The MediaInterface instance corresponding to the image being processed. */ $crop = apply_filters( 'imagify_crop_thumbnail', null, $size, $size_data, $media );
if ( null !== $crop ) { $size_data['crop'] = (bool) $crop; } }
if ( ! isset( $size_data['crop'] ) ) { // We don't have the 'crop' data in that case: let’s try to guess it. if ( ! $size_data['height'] || ! $size_data['width'] ) { // One of the size dimensions is 0, that means crop is probably disabled. $size_data['crop'] = false; } else { if ( ! $this->filesystem->exists( $size_data['path'] ) ) { // Screwed. return false; }
$thumb_dimensions = $this->filesystem->get_image_size( $size_data['path'] );
if ( ! $thumb_dimensions || ! $thumb_dimensions['width'] || ! $thumb_dimensions['height'] ) { // ( ; Д ; ) return false; }
// Compare dimensions. $new_height = $thumb_dimensions['width'] * $size_data['height'] / $size_data['width']; // If the difference is > to 1px, let's assume that crop is enabled. $size_data['crop'] = abs( $thumb_dimensions['height'] - $new_height ) > 1; } }
$resized = $tmp_file->create_thumbnail( [ 'path' => $tmp_path, 'width' => $size_data['width'], 'height' => $size_data['height'], 'crop' => $size_data['crop'], 'adjust_filename' => false, ] );
if ( is_wp_error( $resized ) ) { return false; }
// Make sure the new file has the expected name. $new_tmp_path = $this->filesystem->dir_path( $tmp_path ) . $resized['file'];
if ( $new_tmp_path === $tmp_path ) { return true; }
return $this->filesystem->move( $new_tmp_path, $tmp_path, true ); }
/** * Get the path to a temporary copy of a size file. * * @since 1.9 * @access protected * @author Grégory Viguier * * @param string $size The image size name. * @param array $sizes A list of thumbnail sizes being optimized. * @return string|bool An image path. False on failure. */ protected function get_temporary_copy_path( $size, $sizes = null ) { if ( 'full' === $size ) { $path = $this->get_media()->get_raw_fullsize_path(); } else { if ( ! isset( $sizes ) ) { $sizes = $this->get_media()->get_media_files(); }
$path = ! empty( $sizes[ $size ]['path'] ) ? $sizes[ $size ]['path'] : false; }
if ( ! $path ) { return false; }
$info = $this->filesystem->path_info( $path );
if ( ! $info['file_base'] ) { return false; }
return $info['dir_path'] . $info['file_base'] . static::TMP_SUFFIX . '.' . $info['extension']; }
/** ----------------------------------------------------------------------------------------- */ /** RESIZE FILE ============================================================================= */ /** ----------------------------------------------------------------------------------------- */
/** * Maybe resize an image. * * @since 1.9 * @access public * @author Grégory Viguier * * @param string $size The size name. * @param File $file A File instance. * @return array|WP_Error A \WP_Error instance on failure, an array on success as follow: { * @type bool $resized True when the image has been resized. * @type bool $backuped True when the image has been backuped. * @type int $file_size The file size in bytes. * } */ public function maybe_resize( $size, $file ) { if ( ! $this->can_resize( $size, $file ) ) { // This file should not be resized. return [ 'resized' => false, 'backuped' => false, 'file_size' => 0, ]; }
$media = $this->get_media(); $dimensions = $media->get_dimensions();
if ( ! $dimensions['width'] ) { // The dimensions don't seem to be in the database anymore: try to get them directly from the file. $dimensions = $file->get_dimensions(); }
if ( ! $dimensions['width'] ) { // Could not get the image dimensions. return new \WP_Error( 'no_dimensions', sprintf( /* translators: %s is an error message. */ __( 'Resizing failed: %s', 'imagify' ), __( 'Imagify could not get the image dimensions.', 'imagify' ) ) ); }
$resize_width = $media->get_context_instance()->get_resizing_threshold();
if ( $resize_width >= $dimensions['width'] ) { // No need to resize. return [ 'resized' => false, 'backuped' => false, 'file_size' => 0, ]; }
$resized_path = $file->resize( $dimensions, $resize_width );
if ( is_wp_error( $resized_path ) ) { // The resizement failed. return new \WP_Error( 'resize_failure', sprintf( /* translators: %s is an error message. */ __( 'Resizing failed: %s', 'imagify' ), $resized_path->get_error_message() ) ); }
if ( $this->can_backup( $size ) ) { $source = 'full' === $size ? $media->get_original_path() : null; $backuped = $file->backup( $media->get_raw_backup_path(), $source );
if ( is_wp_error( $backuped ) ) { // The backup failed. return new \WP_Error( 'backup_failure', sprintf( /* translators: %s is an error message. */ __( 'Backup failed: %s', 'imagify' ), $backuped->get_error_message() ) ); } } else { $backuped = false; }
$file_size = (int) $this->filesystem->size( $file->get_path() ); $resized = $this->filesystem->move( $resized_path, $file->get_path(), true );
if ( ! $resized ) { // The resizement failed. return new \WP_Error( 'resize_move_failure', __( 'The image could not be replaced by the resized one.', 'imagify' ) ); }
// Store the new dimensions. $media->update_dimensions();
return [ 'resized' => true, 'backuped' => $backuped, 'file_size' => $file_size, ]; }
/** * Tell if a size should be resized. * * @since 1.9 * @access protected * @author Grégory Viguier * * @param string $size The size name. * @param File $file A File instance. * @return bool */ protected function can_resize( $size, $file ) { if ( ! $this->is_valid() ) { return false; }
if ( 'full' !== $size && 'full' . static::WEBP_SUFFIX !== $size ) { // We resize only the main file and its webp version. return false; }
if ( ! $file->is_image() ) { return false; }
return $this->get_media()->get_context_instance()->can_resize(); }
/** * Tell if a size should be backuped. * * @since 1.9 * @access protected * @author Grégory Viguier * * @param string $size The size name. * @return bool */ protected function can_backup( $size ) { if ( ! $this->is_valid() ) { return false; }
if ( 'full' !== $size ) { // We backup only the main file. return false; }
return $this->get_media()->get_context_instance()->can_backup(); }
/** * Tell if a size should keep exif. * * @since 1.9 * @access protected * @author Grégory Viguier * * @param string $size The size name. * @return bool */ protected function can_keep_exif( $size ) { if ( ! $this->is_valid() ) { return false; }
if ( 'full' !== $size && 'full' . static::WEBP_SUFFIX !== $size ) { // We keep exif only on the main file and its webp version. return false; }
return $this->get_media()->get_context_instance()->can_keep_exif(); }
/** ----------------------------------------------------------------------------------------- */ /** WEBP ==================================================================================== */ /** ----------------------------------------------------------------------------------------- */
/** * Generate webp images if they are missing. * * @since 1.9 * @access public * @author Grégory Viguier * * @return bool|WP_Error True if successfully launched. A \WP_Error instance on failure. */ public function generate_webp_versions() { if ( ! $this->is_valid() ) { return new \WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) ); }
$media = $this->get_media();
if ( ! $media->is_image() ) { return new \WP_Error( 'no_webp', __( 'This media is not an image and cannot be converted to webp format.', 'imagify' ) ); }
if ( ! $media->has_backup() ) { return new \WP_Error( 'no_backup', __( 'This media has no backup file.', 'imagify' ) ); }
$data = $this->get_data();
if ( ! $data->is_optimized() && ! $data->is_already_optimized() ) { return new \WP_Error( 'not_optimized', __( 'This media has not been optimized by Imagify yet.', 'imagify' ) ); }
if ( $this->has_webp() ) { return new \WP_Error( 'has_webp', __( 'This media already has webp versions.', 'imagify' ) ); }
$files = $media->get_media_files(); $sizes = []; $args = [ 'hook_suffix' => 'generate_webp_versions', ];
foreach ( $files as $size_name => $file ) { if ( 'image/webp' !== $files[ $size_name ]['mime-type'] ) { array_unshift( $sizes, $size_name . static::WEBP_SUFFIX ); } }
if ( ! $sizes ) { return new \WP_Error( 'no_sizes', __( 'This media does not have files that can be converted to webp format.', 'imagify' ) ); }
$optimization_level = $data->get_optimization_level();
// Optimize. return $this->optimize_sizes( $sizes, $optimization_level, $args ); }
/** * Delete the webp images. * This doesn't delete the related optimization data. * * @since 1.9 * @since 1.9.6 Return WP_Error or true. * @access public * @author Grégory Viguier * * @param bool $keep_full Set to true to keep the full size. * @return bool|\WP_Error True on success. A \WP_Error object on failure. */ public function delete_webp_files( $keep_full = false ) { if ( ! $this->is_valid() ) { return new \WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) ); }
$media = $this->get_media();
if ( ! $media->is_image() ) { return new \WP_Error( 'media_not_an_image', __( 'This media is not an image.', 'imagify' ) ); }
$files = $media->get_media_files();
if ( $keep_full ) { unset( $files['full'] ); }
if ( ! $files ) { return true; }
$error_count = 0;
foreach ( $files as $file ) { if ( 0 === strpos( $file['mime-type'], 'image/' ) ) { $deleted = $this->delete_webp_file( $file['path'] );
if ( is_wp_error( $deleted ) ) { ++$error_count; } } }
if ( $error_count ) { return new \WP_Error( 'files_not_deleted', sprintf( /* translators: %s is a formatted number, don’t use %d. */ _n( '%s file could not be deleted.', '%s files could not be deleted.', $error_count, 'imagify' ), number_format_i18n( $error_count ) ) ); }
return true; }
/** * Delete a webp image, given its non-webp version's path. * This doesn't delete the related optimization data. * * @since 1.9 * @since 1.9.6 Return WP_Error or true. * @access protected * @author Grégory Viguier * * @param string $file_path Path to the non-webp file. * @return bool|\WP_Error True on success. A \WP_Error object on failure. */ protected function delete_webp_file( $file_path ) { if ( ! $file_path ) { return new \WP_Error( 'no_path', __( 'Path to non-webp file not provided.', 'imagify' ) ); }
$webp_file = new File( $file_path ); $webp_path = $webp_file->get_path_to_webp();
if ( ! $webp_path ) { return new \WP_Error( 'no_webp_path', __( 'Could not get the path to the webp file.', 'imagify' ) ); }
if ( ! $this->filesystem->exists( $webp_path ) ) { return true; }
if ( ! $this->filesystem->is_writable( $webp_path ) ) { return new \WP_Error( 'file_not_writable', sprintf( /* translators: %s is a file path. */ __( 'The file %s does not seem to be writable.', 'imagify' ), '<code>' . esc_html( $this->filesystem->make_path_relative( $webp_path ) ) . '</code>' ) ); }
if ( ! $this->filesystem->is_file( $webp_path ) ) { return new \WP_Error( 'not_a_file', sprintf( /* translators: %s is a file path. */ __( 'This does not seem to be a file: %s.', 'imagify' ), '<code>' . esc_html( $this->filesystem->make_path_relative( $webp_path ) ) . '</code>' ) ); }
$deleted = $this->filesystem->delete( $webp_path, false, 'f' );
if ( ! $deleted ) { return new \WP_Error( 'file_not_deleted', sprintf( /* translators: %s is a file path. */ __( 'The file %s could not be deleted.', 'imagify' ), '<code>' . esc_html( $this->filesystem->make_path_relative( $webp_path ) ) . '</code>' ) ); }
return true; }
/** * Tell if a thumbnail size is an "Imagify webp" size. * * @since 1.9 * @access public * @author Grégory Viguier * * @param string $size_name The size name. * @return string|bool The unsuffixed name of the size if webp. False if not webp. */ public function is_size_webp( $size_name ) { static $suffix;
if ( ! isset( $suffix ) ) { $suffix = preg_quote( static::WEBP_SUFFIX, '/' ); }
if ( preg_match( '/^(?<size>.+)' . $suffix . '$/', $size_name, $matches ) ) { return $matches['size']; }
return false; }
/** * Tell if the media has webp versions. * * @since 1.9 * @access public * @author Grégory Viguier * * @return bool */ public function has_webp() { if ( ! $this->is_valid() ) { return false; }
if ( ! $this->get_media()->is_image() ) { return false; }
$data = $this->get_data()->get_optimization_data();
if ( empty( $data['sizes'] ) ) { return false; }
$needle = static::WEBP_SUFFIX . '";a:4:{s:7:"success";b:1;'; $data = maybe_serialize( $data['sizes'] );
return is_string( $data ) && strpos( $data, $needle ); }
/** * Tell if a webp version can be created for the given file. * Make sure the file is an image before using this method. * * @since 1.9.5 * @access public * @author Grégory Viguier * * @param string $file_path Path to the file. * @return bool */ public function can_create_webp_version( $file_path ) { if ( ! $file_path ) { return false; }
/** * Tell if a webp version can be created for the given file. * The file is an image. * * @since 1.9.5 * @author Grégory Viguier * * @param bool $can True to create a webp version, false otherwise. Null by default. * @param string $file_path Path to the file. */ $can = apply_filters( 'imagify_pre_can_create_webp_version', null, $file_path );
if ( isset( $can ) ) { return (bool) $can; }
$is_animated_gif = $this->filesystem->is_animated_gif( $file_path );
if ( is_bool( $is_animated_gif ) ) { // Ok if it’s not an animated gif. return ! $is_animated_gif; }
// At this point $is_animated_gif is null, which means the file cannot be read (yet). return true; }
/** ----------------------------------------------------------------------------------------- */ /** PROCESS STATUS ========================================================================== */ /** ----------------------------------------------------------------------------------------- */
/** * Tell if a process is running for this media. * * @since 1.9 * @access public * @author Grégory Viguier * * @return string|bool The action if locked ('optimizing' or 'restoring'). False if not locked. */ public function is_locked() { $name = $this->get_lock_name();
if ( ! $name ) { return false; }
$callback = $this->get_media()->get_context_instance()->is_network_wide() ? 'get_site_transient' : 'get_transient'; $action = call_user_func( $callback, $name );
if ( ! $action ) { return false; }
return $this->validate_lock_action( $action ); }
/** * Set the running status to "running" for 10 minutes. * * @since 1.9 * @access public * @author Grégory Viguier * * @param string $action The action performed behind this lock: 'optimizing' or 'restoring'. */ public function lock( $action = 'optimizing' ) { $name = $this->get_lock_name();
if ( ! $name ) { return; }
$action = $this->validate_lock_action( $action ); $media = $this->get_media(); $callback = $media->get_context_instance()->is_network_wide() ? 'set_site_transient' : 'set_transient';
call_user_func( $callback, $name, $action, 10 * MINUTE_IN_SECONDS ); }
/** * Unset the running status. * * @since 1.9 * @access public * @author Grégory Viguier */ public function unlock() { $name = $this->get_lock_name();
if ( ! $name ) { return false; }
$callback = $this->get_media()->get_context_instance()->is_network_wide() ? 'delete_site_transient' : 'delete_transient';
call_user_func( $callback, $name ); }
/** * Get the name of the transient that stores the lock status. * * @since 1.9 * @access protected * @author Grégory Viguier * * @return string|bool The name on success. False on failure. */ protected function get_lock_name() { $media = $this->get_media();
if ( ! $media ) { return false; }
/** * Note that the site transient used by WP Background is named '*_process_lock'. * That would give something like 'imagify_optimize_media_process_lock' for the optimization process, while here it would be 'imagify_wp_42_process_locked'. */ return sprintf( static::LOCK_NAME, $media->get_context(), $media->get_id() ); }
/** * Validate the lock action. * * @since 1.9 * @access protected * @author Grégory Viguier * * @param string $action The action performed behind this lock: 'optimizing' or 'restoring'. * @return string The valid action. */ protected function validate_lock_action( $action ) { switch ( $action ) { case 'restore': case 'restoring': $action = 'restoring'; break;
default: $action = 'optimizing'; }
return $action; }
/** ----------------------------------------------------------------------------------------- */ /** DATA ==================================================================================== */ /** ----------------------------------------------------------------------------------------- */
/** * Tell if a size already has optimization data. * * @since 1.9 * @access public * @author Grégory Viguier * * @param string $size The size name. * @return bool */ public function size_has_optimization_data( $size ) { $data = $this->get_data()->get_optimization_data();
return ! empty( $data['sizes'][ $size ] ); }
/** * Update the optimization data for a size. * * @since 1.9 * @access public * @author Grégory Viguier * * @param object $response The API response. * @param string $size The size name. * @param int $level The optimization level (0=normal, 1=aggressive, 2=ultra). * @return array { * The optimization data. * * @type int $level The optimization level. * @type string $status The status: 'success', 'already_optimized', 'error'. * @type bool $success True if successfully optimized. False on error or if already optimized. * @type string $error An error message. * @type int $original_size The weight of the file, before optimization. * @type int $optimized_size The weight of the file, once optimized. * } */ public function update_size_optimization_data( $response, $size, $level ) { $disabled = false; $data = $this->data_format;
$data['level'] = is_numeric( $level ) ? (int) $level : $this->get_option( 'optimization_level' );
if ( is_wp_error( $response ) ) { /** * Error. */ $disabled = 'unauthorized_size' === $response->get_error_code();
// Size data. $data['success'] = false; $data['error'] = $response->get_error_message();
// Status. if ( false !== strpos( $data['error'], 'This image is already compressed' ) ) { $data['status'] = 'already_optimized'; } else { $data['status'] = 'error'; } } else { /** * Success. */ $response = (object) array_merge( [ 'original_size' => 0, 'new_size' => 0, 'percent' => 0, ], (array) $response );
// Status. $data['status'] = 'success'; $data['error'] = null;
// Size data. $data['success'] = true; $data['original_size'] = $response->original_size; $data['optimized_size'] = $response->new_size; }
$_unauthorized = $disabled ? '_unauthorized' : '';
/** * Filter the optimization data. * * @since 1.9 * @author Grégory Viguier * * @param array $data { * The optimization data. * * @type int $level The optimization level. * @type string $status The status: 'success', 'already_optimized', 'error'. * @type bool $success True if successfully optimized. False on error or if already optimized. * @type string $error An error message. * @type int $original_size The weight of the file, before optimization. * @type int $optimized_size The weight of the file, once optimized. * } * @param object $response The API response. * @param string $size The size name. * @param int $level The optimization level. * @param object $media_data The DataInterface instance of the media. */ $data = (array) apply_filters( "imagify{$_unauthorized}_file_optimization_data", $data, $response, $size, $level, $this->get_data() );
// Store. $this->get_data()->update_size_optimization_data( $size, $data );
return $data; }
/** ----------------------------------------------------------------------------------------- */ /** VARIOUS TOOLS =========================================================================== */ /** ----------------------------------------------------------------------------------------- */
/** * Get a plugin’s option. * * @since 1.9 * @access protected * @author Grégory Viguier * * @param string $option_name The option nme. * @return mixed */ protected function get_option( $option_name ) { if ( isset( $this->options[ $option_name ] ) ) { return $this->options[ $option_name ]; }
$this->options[ $option_name ] = get_imagify_option( $option_name );
return $this->options[ $option_name ]; }
/** * Sanitize and validate an optimization level. * If not provided (false, null), fallback to the level set in the plugin's settings. * * @since 1.9 * @access protected * @author Grégory Viguier * * @param mixed $optimization_level The optimization level. * @return int */ protected function sanitize_optimization_level( $optimization_level ) { if ( ! is_numeric( $optimization_level ) ) { return $this->get_option( 'optimization_level' ); }
return \Imagify_Options::get_instance()->sanitize_and_validate( 'optimization_level', $optimization_level ); } }
|