Page.php
84.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
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
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
<?php
namespace WP_Rocket\Engine\Admin\Settings;
use WP_Rocket\Engine\Admin\Database\Optimization;
use WP_Rocket\Engine\Admin\Beacon\Beacon;
use WP_Rocket\Engine\License\API\UserClient;
use WP_Rocket\Interfaces\Render_Interface;
use WP_Rocket\Engine\Optimization\DelayJS\Admin\Settings as DelayJSSettings;
/**
* Registers the admin page and WP Rocket settings.
*
* @since 3.5.5 Moves into the new architecture.
* @since 3.0
*/
class Page {
/**
* Plugin slug.
*
* @since 3.0
*
* @var string
*/
private $slug;
/**
* Plugin page title.
*
* @since 3.0
*
* @var string
*/
private $title;
/**
* Required capability to access the page.
*
* @since 3.0
*
* @var string
*/
private $capability;
/**
* Settings instance.
*
* @since 3.0
*
* @var Settings
*/
private $settings;
/**
* Render implementation.
*
* @since 3.0
*
* @var Render
*/
private $render;
/**
* Beacon instance.
*
* @since 3.2
*
* @var Beacon
*/
private $beacon;
/**
* Database optimization instance.
*
* @since 3.3
*
* @var Optimization
*/
private $optimize;
/**
* User client instance.
*
* @var UserClient
*/
private $user_client;
/**
* Creates an instance of the Page object.
*
* @since 3.0
*
* @param array $args Array of required arguments to add the admin page.
* @param Settings $settings Instance of Settings class.
* @param Render_Interface $render Implementation of Render interface.
* @param Beacon $beacon Beacon instance.
* @param Optimization $optimize Database optimization instance.
* @param UserClient $user_client User client instance.
*/
public function __construct( array $args, Settings $settings, Render_Interface $render, Beacon $beacon, Optimization $optimize, UserClient $user_client ) {
$args = array_merge(
[
'slug' => 'wprocket',
'title' => 'WP Rocket',
'capability' => 'rocket_manage_options',
],
$args
);
$this->slug = $args['slug'];
$this->title = $args['title'];
$this->capability = $args['capability'];
$this->settings = $settings;
$this->render = $render;
$this->beacon = $beacon;
$this->optimize = $optimize;
$this->user_client = $user_client;
}
/**
* Returns the settings page title.
*
* @since 3.3
*
* @return string
*/
public function get_title() {
return $this->title;
}
/**
* Returns the settings page slug.
*
* @since 3.3
*
* @return string
*/
public function get_slug() {
return $this->slug;
}
/**
* Returns the settings page capability.
*
* @since 3.3
*
* @return string
*/
public function get_capability() {
return $this->capability;
}
/**
* Registers the settings, page sections, fields sections and fields.
*
* @since 3.0
*/
public function configure() {
register_setting( $this->slug, WP_ROCKET_SLUG, [ $this->settings, 'sanitize_callback' ] );
}
/**
* Renders the settings page.
*
* @since 3.0
*/
public function render_page() {
$rocket_valid_key = rocket_valid_key();
if ( $rocket_valid_key ) {
$this->dashboard_section();
$this->cache_section();
$this->assets_section();
$this->media_section();
$this->preload_section();
$this->advanced_cache_section();
$this->database_section();
$this->cdn_section();
$this->heartbeat_section();
$this->addons_section();
$this->cloudflare_section();
$this->sucuri_section();
} else {
$this->license_section();
}
$this->render->set_settings( $this->settings->get_settings() );
$this->hidden_fields();
$this->render->set_hidden_settings( $this->settings->get_hidden_settings() );
$btn_submit_text = $rocket_valid_key ? __( 'Save Changes', 'rocket' ) : __( 'Validate License', 'rocket' );
echo $this->render->generate( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
'page',
[
'slug' => $this->slug, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
'btn_submit_text' => $btn_submit_text, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
]
);
}
/**
* Enqueues WP Rocket scripts on the settings page
*
* @since 3.6.1
*
* @param string $hook The current admin page.
*
* @return void
*/
public function enqueue_rocket_scripts( $hook ) {
if ( 'settings_page_wprocket' !== $hook ) {
return;
}
wp_enqueue_script( 'wistia-e-v1', 'https://fast.wistia.com/assets/external/E-v1.js', [], null, true ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
}
/**
* Adds the async attribute to the Wistia script
*
* @since 3.6.1
*
* @param string $tag The <script> tag for the enqueued script.
* @param string $handle The script's registered handle.
*
* @return string
*/
public function async_wistia_script( $tag, $handle ) {
if ( 'wistia-e-v1' !== $handle ) {
return $tag;
}
return str_replace( ' src', ' async src', $tag );
}
/**
* Returns the customer data to display on the dashboard
*
* @since 3.7.3 Update to use the user client class to get the data
* @since 3.0
*
* @return object
*/
public function customer_data() {
$user = $this->user_client->get_user_data();
$data = [
'license_type' => __( 'Unavailable', 'rocket' ),
'license_expiration' => __( 'Unavailable', 'rocket' ),
'license_class' => 'wpr-isInvalid',
'is_from_one_dot_com' => false,
];
if (
false === $user
||
! isset( $user->licence_account, $user->licence_expiration )
) {
return $data;
}
if (
1 <= $user->licence_account
&&
$user->licence_account < 3
) {
$data['license_type'] = 'Single';
} elseif ( -1 === (int) $user->licence_account ) {
$data['license_type'] = 'Infinite';
} else {
$data['license_type'] = 'Plus';
}
$data['license_class'] = time() < $user->licence_expiration ? 'wpr-isValid' : 'wpr-isInvalid';
$data['license_expiration'] = date_i18n( get_option( 'date_format' ), (int) $user->licence_expiration );
$data['is_from_one_dot_com'] = (bool) $user->{'has_one-com_account'};
return $data;
}
/**
* Toggle sliding checkboxes option value.
*
* @since 3.0
*/
public function toggle_option() {
check_ajax_referer( 'rocket-ajax' );
if ( ! current_user_can( 'rocket_manage_options' ) ) {
wp_die();
}
$allowed = [
'analytics_enabled' => 1,
'debug_enabled' => 1,
'varnish_auto_purge' => 1,
'do_cloudflare' => 1,
'cloudflare_protocol_rewrite' => 1,
'sucury_waf_cache_sync' => 1,
'sucury_waf_api_key' => 1,
'cache_webp' => 1,
];
if ( ! isset( $_POST['option']['name'] ) || ! isset( $allowed[ $_POST['option']['name'] ] ) ) {
wp_die();
}
$value = (int) ! empty( $_POST['option']['value'] );
update_rocket_option( sanitize_key( $_POST['option']['name'] ), $value );
wp_die();
}
/**
* Forces the value for the mobile options if a mobile plugin is active.
*
* @since 3.0
* @since 3.2 Not used anymore.
* @see \WP_Rocket\Subscriber\Third_Party\Plugins\Mobile_Subscriber::is_mobile_plugin_active_callback()
*
* @param mixed $value Option value.
*
* @return mixed
*/
public function is_mobile_plugin_active( $value ) {
if ( rocket_is_mobile_plugin_active() ) {
return 1;
}
return $value;
}
/**
* Registers License section.
*
* @since 3.0
*/
private function license_section() {
$this->settings->add_page_section(
'license',
[
'title' => __( 'License', 'rocket' ),
]
);
$this->settings->add_settings_sections(
[
'license_section' => [
'type' => 'nocontainer',
'page' => 'license',
],
]
);
$this->settings->add_settings_fields(
[
'consumer_key' => [
'type' => 'text',
'label' => __( 'API key', 'rocket' ),
'default' => '',
'container_class' => [
'wpr-field--split',
'wpr-isDisabled',
],
'section' => 'license_section',
'page' => 'license',
'sanitize_callback' => 'sanitize_text_field',
'input_attr' => [
'disabled' => 1,
],
],
'consumer_email' => [
'type' => 'text',
'label' => __( 'Email address', 'rocket' ),
'default' => '',
'container_class' => [
'wpr-field--split',
'wpr-isDisabled',
],
'section' => 'license_section',
'page' => 'license',
'sanitize_callback' => 'sanitize_email',
'input_attr' => [
'disabled' => 1,
],
],
]
);
}
/**
* Registers Dashboard section.
*
* @since 3.0
*/
private function dashboard_section() {
$this->settings->add_page_section(
'dashboard',
[
'title' => __( 'Dashboard', 'rocket' ),
'menu_description' => __( 'Get help, account info', 'rocket' ),
'faq' => $this->beacon->get_suggest( 'faq' ),
'customer_data' => $this->customer_data(),
]
);
$this->settings->add_settings_sections(
[
'status' => [
'title' => __( 'My Status', 'rocket' ),
'page' => 'dashboard',
],
]
);
$this->settings->add_settings_fields(
[
'analytics_enabled' => [
'type' => 'sliding_checkbox',
'label' => __( 'Rocket Analytics', 'rocket' ),
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
'description' => sprintf( __( 'I agree to share anonymous data with the development team to help improve WP Rocket. %1$sWhat info will we collect?%2$s', 'rocket' ), '<button class="wpr-js-popin">', '</button>' ),
'section' => 'status',
'page' => 'dashboard',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
],
]
);
}
/**
* Registers Cache section.
*
* @since 3.0
*/
private function cache_section() {
$mobile_cache_beacon = $this->beacon->get_suggest( 'mobile_cache' );
$user_cache_beacon = $this->beacon->get_suggest( 'user_cache' );
$nonce_beacon = $this->beacon->get_suggest( 'nonce' );
$cache_life_beacon = $this->beacon->get_suggest( 'cache_lifespan' );
$this->settings->add_page_section(
'cache',
[
'title' => __( 'Cache', 'rocket' ),
'menu_description' => __( 'Basic cache options', 'rocket' ),
]
);
$this->settings->add_settings_sections(
[
'mobile_cache_section' => [
'title' => __( 'Mobile Cache', 'rocket' ),
'type' => 'fields_container',
'description' => __( 'Speed up your site for mobile visitors.', 'rocket' ),
'help' => [
'url' => $mobile_cache_beacon['url'],
'id' => $this->beacon->get_suggest( 'mobile_cache_section' ),
],
'helper' => rocket_is_mobile_plugin_active() ? __( 'We detected you use a plugin that requires a separate cache for mobile, and automatically enabled this option for compatibility.', 'rocket' ) : '',
'page' => 'cache',
],
'user_cache_section' => [
'title' => __( 'User Cache', 'rocket' ),
'type' => 'fields_container',
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
'description' => sprintf( __( '%1$sUser cache%2$s is great when you have user-specific or restricted content on your website.', 'rocket' ), '<a href="' . esc_url( $user_cache_beacon['url'] ) . '" data-beacon-article="' . esc_attr( $user_cache_beacon['id'] ) . '" target="_blank">', '</a>' ),
'help' => [
'url' => $user_cache_beacon['url'],
'id' => $this->beacon->get_suggest( 'user_cache_section' ),
],
'page' => 'cache',
],
'cache_lifespan' => [
'title' => __( 'Cache Lifespan', 'rocket' ),
'type' => 'fields_container',
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
'description' => sprintf( __( 'Cache files older than the specified lifespan will be deleted.<br>Enable %1$spreloading%2$s for the cache to be rebuilt automatically after lifespan expiration.', 'rocket' ), '<a href="#preload">', '</a>' ),
'help' => [
'url' => $cache_life_beacon['url'],
'id' => $this->beacon->get_suggest( 'cache_lifespan_section' ),
],
'page' => 'cache',
],
]
);
$this->settings->add_settings_fields(
[
'cache_logged_user' => [
'type' => 'checkbox',
'label' => __( 'Enable caching for logged-in WordPress users', 'rocket' ),
'section' => 'user_cache_section',
'page' => 'cache',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
],
'cache_mobile' => [
'type' => 'checkbox',
'label' => __( 'Enable caching for mobile devices', 'rocket' ),
'container_class' => [
rocket_is_mobile_plugin_active() ? 'wpr-isDisabled' : '',
'wpr-isParent',
],
'section' => 'mobile_cache_section',
'page' => 'cache',
'default' => 1,
'sanitize_callback' => 'sanitize_checkbox',
'input_attr' => [
'disabled' => rocket_is_mobile_plugin_active() ? 1 : 0,
],
],
'do_caching_mobile_files' => [
'type' => 'checkbox',
'label' => __( 'Separate cache files for mobile devices', 'rocket' ),
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
'description' => sprintf( __( 'Most modern themes are responsive and should work without a separate cache. Enable this only if you have a dedicated mobile theme or plugin. %1$sMore info%2$s', 'rocket' ), '<a href="' . esc_url( $mobile_cache_beacon['url'] ) . '" data-beacon-article="' . esc_attr( $mobile_cache_beacon['id'] ) . '" target="_blank">', '</a>' ),
'container_class' => [
rocket_is_mobile_plugin_active() ? 'wpr-isDisabled' : '',
'wpr-field--children',
],
'parent' => 'cache_mobile',
'section' => 'mobile_cache_section',
'page' => 'cache',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
'input_attr' => [
'disabled' => rocket_is_mobile_plugin_active() ? 1 : 0,
],
],
'purge_cron_interval' => [
'type' => 'cache_lifespan',
'label' => __( 'Specify time after which the global cache is cleared<br>(0 = unlimited )', 'rocket' ),
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
'description' => sprintf( __( 'Reduce lifespan to 10 hours or less if you notice issues that seem to appear periodically. %1$sWhy?%2$s', 'rocket' ), '<a href="' . esc_url( $nonce_beacon['url'] ) . '" data-beacon-article="' . esc_attr( $nonce_beacon['id'] ) . '" target="_blank">', '</a>' ),
'section' => 'cache_lifespan',
'page' => 'cache',
'default' => 10,
'sanitize_callback' => 'sanitize_cache_lifespan',
'choices' => [
'HOUR_IN_SECONDS' => __( 'Hours', 'rocket' ),
'DAY_IN_SECONDS' => __( 'Days', 'rocket' ),
],
],
]
);
}
/**
* Registers CSS & Javascript section.
*
* @since 3.0
*/
private function assets_section() {
$combine_beacon = $this->beacon->get_suggest( 'combine' );
$defer_js_beacon = $this->beacon->get_suggest( 'defer_js' );
$async_beacon = $this->beacon->get_suggest( 'async' );
$files_beacon = $this->beacon->get_suggest( 'file_optimization' );
$inline_js_beacon = $this->beacon->get_suggest( 'exclude_inline_js' );
$exclude_js_beacon = $this->beacon->get_suggest( 'exclude_js' );
$exclude_css_beacon = $this->beacon->get_suggest( 'exclude_css' );
$delay_js_beacon = $this->beacon->get_suggest( 'delay_js' );
$delay_js_exclusions_beacon = $this->beacon->get_suggest( 'delay_js_exclusions' );
$exclude_defer_js = $this->beacon->get_suggest( 'exclude_defer_js' );
$rucss_beacon = $this->beacon->get_suggest( 'remove_unused_css' );
$offline_beacon = $this->beacon->get_suggest( 'offline' );
$fallback_css_beacon = $this->beacon->get_suggest( 'fallback_css' );
$disable_combine_js = $this->disable_combine_js();
$disable_combine_css = $this->disable_combine_css();
$disable_ocd = 'local' === wp_get_environment_type();
$invalid_license = get_transient( 'wp_rocket_no_licence' );
$this->settings->add_page_section(
'file_optimization',
[
'title' => __( 'File Optimization', 'rocket' ),
'menu_description' => __( 'Optimize CSS & JS', 'rocket' ),
]
);
$this->settings->add_settings_sections(
[
'css' => [
'title' => __( 'CSS Files', 'rocket' ),
'help' => [
'id' => $this->beacon->get_suggest( 'css_section' ),
'url' => $files_beacon['url'],
],
'page' => 'file_optimization',
// translators: %1$s = type of minification (HTML, CSS or JS), %2$s = “WP Rocket”.
'helper' => rocket_maybe_disable_minify_css() ? sprintf( __( '%1$s Minification is currently activated in <strong>Autoptimize</strong>. If you want to use %2$s’s minification, disable those options in Autoptimize.', 'rocket' ), 'CSS', WP_ROCKET_PLUGIN_NAME ) : '',
],
'js' => [
'title' => __( 'JavaScript Files', 'rocket' ),
'help' => [
'id' => $this->beacon->get_suggest( 'js_section' ),
'url' => $files_beacon['url'],
],
'page' => 'file_optimization',
// translators: %1$s = type of minification (HTML, CSS or JS), %2$s = “WP Rocket”.
'helper' => rocket_maybe_disable_minify_js() ? sprintf( __( '%1$s Minification is currently activated in <strong>Autoptimize</strong>. If you want to use %2$s’s minification, disable those options in Autoptimize.', 'rocket' ), 'JS', WP_ROCKET_PLUGIN_NAME ) : '',
],
]
);
$delay_js_list_helper = sprintf(
// translators: %1$s = exclusion list, %2$s = opening </a> tag, %3$s = closing </a> tag.
__( 'If you have problems after activating this option, copy and paste the default exclusions to quickly resolve issues:<br><pre><code>%1$s</code></pre><br>Also, please check our %2$sdocumentation%3$s for a list of compatibility exclusions.', 'rocket' ),
implode( '<br>', DelayJSSettings::get_delay_js_default_exclusions() ),
'<a href="' . esc_url( $delay_js_exclusions_beacon['url'] ) . '" target="_blank" rel="noopener">',
'</a>'
);
$delay_js_found_list_helper = sprintf(
// translators: %1$s = opening </a> tag, %2$s = closing </a> tag.
__( 'Internal scripts are excluded by default to prevent issues. Remove them to take full advantage of this option.<br>If this causes trouble, restore the default exclusions, found %1$shere%2$s', 'rocket' ),
'<a href="' . esc_url( $delay_js_beacon['url'] ) . '" target="_blank" rel="noopener">',
'</a>'
);
$this->settings->add_settings_fields(
[
'minify_css' => [
'type' => 'checkbox',
'label' => __( 'Minify CSS files', 'rocket' ),
'description' => __( 'Minify CSS removes whitespace and comments to reduce the file size.', 'rocket' ),
'container_class' => [
rocket_maybe_disable_minify_css() ? 'wpr-isDisabled' : '',
'wpr-field--parent',
],
'section' => 'css',
'page' => 'file_optimization',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
'input_attr' => [
'disabled' => rocket_maybe_disable_minify_css() ? 1 : 0,
],
'warning' => [
'title' => __( 'This could break things!', 'rocket' ),
'description' => __( 'If you notice any errors on your website after having activated this setting, just deactivate it again, and your site will be back to normal.', 'rocket' ),
'button_label' => __( 'Activate minify CSS', 'rocket' ),
],
],
'minify_concatenate_css' => [
'type' => 'checkbox',
'label' => __( 'Combine CSS files <em>(Enable Minify CSS files to select)</em>', 'rocket' ),
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
'description' => sprintf( __( 'Combine CSS merges all your files into 1, reducing HTTP requests. Not recommended if your site uses HTTP/2. %1$sMore info%2$s', 'rocket' ), '<a href="' . esc_url( $combine_beacon['url'] ) . '" data-beacon-article="' . esc_attr( $combine_beacon['id'] ) . '" target="_blank">', '</a>' ),
'helper' => get_rocket_option( 'remove_unused_css' ) ? __( 'For compatibility and best results, this option is disabled when Remove unused CSS is enabled.', 'rocket' ) : '',
'container_class' => [
$disable_combine_css ? 'wpr-isDisabled' : '',
'wpr-field--parent',
'wpr-NoPaddingBottom',
],
'section' => 'css',
'page' => 'file_optimization',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
'input_attr' => [
'disabled' => $disable_combine_css ? 1 : 0,
],
'warning' => [
'title' => __( 'This could break things!', 'rocket' ),
'description' => __( 'If you notice any errors on your website after having activated this setting, just deactivate it again, and your site will be back to normal.', 'rocket' ),
'button_label' => __( 'Activate combine CSS', 'rocket' ),
],
],
'exclude_css' => [
'type' => 'textarea',
'label' => __( 'Excluded CSS Files', 'rocket' ),
'description' => __( 'Specify URLs of CSS files to be excluded from minification and concatenation (one per line).', 'rocket' ),
'helper' => __( '<strong>Internal:</strong> The domain part of the URL will be stripped automatically. Use (.*).css wildcards to exclude all CSS files located at a specific path.', 'rocket' ) . '<br>' .
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
sprintf( __( '<strong>3rd Party:</strong> Use either the full URL path or only the domain name, to exclude external CSS. %1$sMore info%2$s', 'rocket' ), '<a href="' . esc_url( $exclude_css_beacon['url'] ) . '" data-beacon-article="' . esc_attr( $exclude_css_beacon['id'] ) . '" rel="noopener noreferrer" target="_blank">', '</a>' ),
'container_class' => [
'wpr-field--children',
],
'placeholder' => '/wp-content/plugins/some-plugin/(.*).css',
'parent' => 'minify_css',
'section' => 'css',
'page' => 'file_optimization',
'default' => [],
'sanitize_callback' => 'sanitize_textarea',
],
'optimize_css_delivery' => [
'type' => 'checkbox',
'label' => __( 'Optimize CSS delivery', 'rocket' ),
'container_class' => [
$disable_ocd ? 'wpr-isDisabled' : '',
'wpr-isParent',
],
'description' => $invalid_license ? __( 'Optimize CSS delivery eliminates render-blocking CSS on your website. Only one method can be selected. Remove Unused CSS is recommended for optimal performance, but limited only to the users with active license.', 'rocket' ) : __( 'Optimize CSS delivery eliminates render-blocking CSS on your website. Only one method can be selected. Remove Unused CSS is recommended for optimal performance.', 'rocket' ),
'section' => 'css',
'page' => 'file_optimization',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
'input_attr' => [
'disabled' => $disable_ocd ? 1 : 0,
],
'helper' => $disable_ocd ? sprintf(
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
__( 'Optimize CSS Delivery features are disabled on local environments. %1$sLearn more%2$s', 'rocket' ),
'<a href="' . esc_url( $offline_beacon['url'] ) . '" data-beacon-article="' . esc_attr( $offline_beacon['id'] ) . '" target="_blank">',
'</a>'
) : '',
],
'optimize_css_delivery_method' => [
'type' => 'radio_buttons',
'label' => __( 'Optimize CSS delivery', 'rocket' ),
'container_class' => [
'wpr-field--children',
'wpr-field--optimize-css-delivery',
],
'buttons_container_class' => '',
'parent' => 'optimize_css_delivery',
'section' => 'css',
'page' => 'file_optimization',
'default' => 'remove_unused_css',
'sanitize_callback' => 'sanitize_checkbox',
'options' => [
'remove_unused_css' => [
'label' => __( 'Remove Unused CSS', 'rocket' ),
'disabled' => $invalid_license ? 'disabled' : false,
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
'description' => sprintf( __( 'Removes unused CSS per page and helps to reduce page size and HTTP requests. Recommended for best performance. Test thoroughly! %1$sMore info%2$s', 'rocket' ), '<a href="' . esc_url( $rucss_beacon['url'] ) . '" data-beacon-article="' . esc_attr( $rucss_beacon['id'] ) . '" target="_blank">', '</a>' ),
'warning' => $invalid_license ? [] : [
'title' => __( 'This could break things!', 'rocket' ),
'description' => __( 'If you notice any errors on your website after having activated this setting, just deactivate it again, and your site will be back to normal.', 'rocket' ),
'button_label' => __( 'Activate Remove Unused CSS', 'rocket' ),
],
'sub_fields' => $invalid_license ? [] : [
'remove_unused_css_safelist' =>
[
'type' => 'textarea',
'label' => __( 'CSS safelist', 'rocket' ),
'description' => __( 'Specify CSS filenames, IDs or classes that should not be removed (one per line).', 'rocket' ),
'placeholder' => "/wp-content/plugins/some-plugin/(.*).css\n.css-class\n#css_id\ntag",
'default' => [],
'value' => [],
'sanitize_callback' => 'sanitize_textarea',
'parent' => '',
'section' => 'css',
'page' => 'file_optimization',
'input_attr' => [
'disabled' => get_rocket_option( 'remove_unused_css' ) ? 0 : 1,
],
],
],
],
'async_css' => [
'label' => __( 'Load CSS asynchronously', 'rocket' ),
'description' => is_plugin_active( 'wp-criticalcss/wp-criticalcss.php' ) ?
// translators: %1$s = plugin name.
sprintf( _x( 'Load CSS asynchronously is currently handled by the %1$s plugin. If you want to use WP Rocket’s load CSS asynchronously option, disable the %1$s plugin.', 'WP Critical CSS compatibility', 'rocket' ), 'WP Critical CSS' ) :
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
sprintf( __( 'Generates critical path CSS and loads CSS asynchronously. %1$sMore info%2$s', 'rocket' ), '<a href="' . esc_url( $async_beacon['url'] ) . '" data-beacon-article="' . esc_attr( $async_beacon['id'] ) . '" target="_blank">', '</a>' ),
'disabled' => is_plugin_active( 'wp-criticalcss/wp-criticalcss.php' ) ? 'disabled' : '',
'sub_fields' => [
'critical_css' =>
[
'type' => 'textarea',
'label' => __( 'Fallback critical CSS', 'rocket' ),
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
'helper' => sprintf( __( 'Provides a fallback if auto-generated critical path CSS is incomplete. %1$sMore info%2$s', 'rocket' ), '<a href="' . esc_url( $fallback_css_beacon['url'] ) . '#fallback" data-beacon-article="' . esc_attr( $fallback_css_beacon['id'] ) . '" target="_blank">', '</a>' ),
'sanitize_callback' => 'sanitize_textarea',
'parent' => '',
'section' => 'css',
'page' => 'file_optimization',
'placeholder' => '',
'default' => [],
'value' => [],
],
],
],
],
],
'minify_js' => [
'type' => 'checkbox',
'label' => __( 'Minify JavaScript files', 'rocket' ),
'description' => __( 'Minify JavaScript removes whitespace and comments to reduce the file size.', 'rocket' ),
'container_class' => [
rocket_maybe_disable_minify_js() ? 'wpr-isDisabled' : '',
'wpr-field--parent',
],
'section' => 'js',
'page' => 'file_optimization',
'default' => 0,
'input_attr' => [
'disabled' => rocket_maybe_disable_minify_js() ? 1 : 0,
],
'sanitize_callback' => 'sanitize_checkbox',
'warning' => [
'title' => __( 'This could break things!', 'rocket' ),
'description' => __( 'If you notice any errors on your website after having activated this setting, just deactivate it again, and your site will be back to normal.', 'rocket' ),
'button_label' => __( 'Activate minify JavaScript', 'rocket' ),
],
],
'minify_concatenate_js' => [
'type' => 'checkbox',
'label' => __( 'Combine JavaScript files <em>(Enable Minify JavaScript files to select)</em>', 'rocket' ),
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
'description' => sprintf( __( 'Combine JavaScript files combines your site’s internal, 3rd party and inline JS reducing HTTP requests. Not recommended if your site uses HTTP/2. %1$sMore info%2$s', 'rocket' ), '<a href="' . esc_url( $combine_beacon['url'] ) . '" data-beacon-article="' . esc_attr( $combine_beacon['id'] ) . '" target="_blank">', '</a>' ),
'helper' => get_rocket_option( 'delay_js' ) ? __( 'For compatibility and best results, this option is disabled when delay javascript execution is enabled.', 'rocket' ) : '',
'container_class' => [
$disable_combine_js ? 'wpr-isDisabled' : '',
'wpr-field--parent',
'wpr-NoPaddingBottom',
],
'section' => 'js',
'page' => 'file_optimization',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
'input_attr' => [
'disabled' => $disable_combine_js ? 1 : 0,
],
'warning' => [
'title' => __( 'This could break things!', 'rocket' ),
'description' => __( 'If you notice any errors on your website after having activated this setting, just deactivate it again, and your site will be back to normal.', 'rocket' ),
'button_label' => __( 'Activate combine JavaScript', 'rocket' ),
],
],
'exclude_inline_js' => [
'type' => 'textarea',
'label' => __( 'Excluded Inline JavaScript', 'rocket' ),
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
'description' => sprintf( __( 'Specify patterns of inline JavaScript to be excluded from concatenation (one per line). %1$sMore info%2$s', 'rocket' ), '<a href="' . esc_url( $inline_js_beacon['url'] ) . '" data-beacon-article="' . esc_attr( $inline_js_beacon['id'] ) . '" rel="noopener noreferrer" target="_blank">', '</a>' ),
'container_class' => [
'wpr-field--children',
],
'placeholder' => 'recaptcha',
'parent' => 'minify_concatenate_js',
'section' => 'js',
'page' => 'file_optimization',
'default' => [],
'sanitize_callback' => 'sanitize_textarea',
'input_attr' => [
'disabled' => get_rocket_option( 'minify_concatenate_js' ) ? 0 : 1,
],
],
'exclude_js' => [
'type' => 'textarea',
'label' => __( 'Excluded JavaScript Files', 'rocket' ),
'description' => __( 'Specify URLs of JavaScript files to be excluded from minification and concatenation (one per line).', 'rocket' ),
'helper' => __( '<strong>Internal:</strong> The domain part of the URL will be stripped automatically. Use (.*).js wildcards to exclude all JS files located at a specific path.', 'rocket' ) . '<br>' .
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
sprintf( __( '<strong>3rd Party:</strong> Use either the full URL path or only the domain name, to exclude external JS. %1$sMore info%2$s', 'rocket' ), '<a href="' . esc_url( $exclude_js_beacon['url'] ) . '" data-beacon-article="' . esc_attr( $exclude_js_beacon['id'] ) . '" rel="noopener noreferrer" target="_blank">', '</a>' ),
'container_class' => [
'wpr-field--children',
],
'placeholder' => '/wp-content/themes/some-theme/(.*).js',
'parent' => 'minify_js',
'section' => 'js',
'page' => 'file_optimization',
'default' => [],
'sanitize_callback' => 'sanitize_textarea',
],
'defer_all_js' => [
'container_class' => [
'wpr-isParent',
],
'type' => 'checkbox',
'label' => __( 'Load JavaScript deferred', 'rocket' ),
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
'description' => sprintf( __( 'Load JavaScript deferred eliminates render-blocking JS on your site and can improve load time. %1$sMore info%2$s', 'rocket' ), '<a href="' . esc_url( $defer_js_beacon['url'] ) . '" data-beacon-article="' . esc_attr( $defer_js_beacon['id'] ) . '" target="_blank">', '</a>' ),
'section' => 'js',
'page' => 'file_optimization',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
],
'exclude_defer_js' => [
'container_class' => [
'wpr-field--children',
],
'type' => 'textarea',
'label' => __( 'Excluded JavaScript Files', 'rocket' ),
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
'description' => sprintf( __( 'Specify URLs or keywords of JavaScript files to be excluded from defer (one per line). %1$sMore info%2$s', 'rocket' ), '<a href="' . esc_url( $exclude_defer_js['url'] ) . '" data-beacon-article="' . esc_attr( $exclude_defer_js['id'] ) . '" target="_blank">', '</a>' ),
'placeholder' => '/wp-content/themes/some-theme/(.*).js',
'parent' => 'defer_all_js',
'section' => 'js',
'page' => 'file_optimization',
'default' => [],
'sanitize_callback' => 'sanitize_textarea',
],
'delay_js' => apply_filters(
'rocket_delay_js_settings_field',
[
'container_class' => [
'wpr-isParent',
'wpr-Delayjs',
],
'type' => 'checkbox',
'label' => __( 'Delay JavaScript execution', 'rocket' ),
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
'description' => sprintf( __( 'Improves performance by delaying the loading of JavaScript files until user interaction (e.g. scroll, click). %1$sMore info%2$s', 'rocket' ), '<a href="' . esc_url( $delay_js_beacon['url'] ) . '" data-beacon-article="' . esc_attr( $delay_js_beacon['id'] ) . '" target="_blank">', '</a>' ),
'section' => 'js',
'page' => 'file_optimization',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
]
),
'delay_js_exclusions' => [
'type' => 'textarea',
'label' => __( 'Excluded JavaScript Files', 'rocket' ),
'description' => __( 'Specify URLs or keywords that can identify inline or JavaScript files to be excluded from delaying execution (one per line).', 'rocket' ),
'container_class' => [
'wpr-field--children',
],
'parent' => 'delay_js',
'section' => 'js',
'page' => 'file_optimization',
'default' => [],
'sanitize_callback' => 'sanitize_textarea',
'input_attr' => [
'disabled' => get_rocket_option( 'delay_js' ) ? 0 : 1,
],
'helper' => DelayJSSettings::exclusion_list_has_default() ? $delay_js_found_list_helper : $delay_js_list_helper,
],
]
);
}
/**
* Registers Media section.
*
* @since 3.0
*/
private function media_section() {
$lazyload_beacon = $this->beacon->get_suggest( 'lazyload' );
$exclude_lazyload = $this->beacon->get_suggest( 'exclude_lazyload' );
$dimensions = $this->beacon->get_suggest( 'image_dimensions' );
$this->settings->add_page_section(
'media',
[
'title' => __( 'Media', 'rocket' ),
'menu_description' => __( 'LazyLoad, image dimensions', 'rocket' ),
]
);
$disable_images_lazyload = [];
$disable_iframes_lazyload = [];
$disable_youtube_lazyload = [];
if ( rocket_maybe_disable_lazyload() ) {
$disable_images_lazyload[] = __( 'Autoptimize', 'rocket' );
}
/**
* Lazyload Helper filter which disables WPR lazyload functionality for images.
*
* @since 3.4.2
*
* @param array $disable_images_lazyload Will return the array with all plugin names which should disable LazyLoad
*/
$disable_images_lazyload = (array) apply_filters( 'rocket_maybe_disable_lazyload_helper', $disable_images_lazyload );
$disable_images_lazyload = $this->sanitize_and_format_list( $disable_images_lazyload );
/**
* Lazyload Helper filter which disables WPR lazyload functionality for iframes.
*
* @since 3.5.5
*
* @param array $disable_iframes_lazyload Will return the array with all plugin names which should disable LazyLoad
*/
$disable_iframes_lazyload = (array) apply_filters( 'rocket_maybe_disable_iframes_lazyload_helper', $disable_iframes_lazyload );
$disable_iframes_lazyload = $this->sanitize_and_format_list( $disable_iframes_lazyload );
/**
* Lazyload Helper filter which disables WPR lazyload functionality to replace YouTube iframe with preview image.
*
* @since 3.6.3
*
* @param array $disable_youtube_lazyload Will return the array with all plugin/themes names which should disable replace YouTube iframe with preview image
*/
$disable_youtube_lazyload = (array) apply_filters( 'rocket_maybe_disable_youtube_lazyload_helper', $disable_youtube_lazyload );
$disable_youtube_lazyload = $this->sanitize_and_format_list( $disable_youtube_lazyload );
$disable_youtube_lazyload = array_merge( $disable_youtube_lazyload, $disable_iframes_lazyload );
$disable_youtube_lazyload = array_unique( $disable_youtube_lazyload );
$disable_lazyload = array_merge( $disable_images_lazyload, $disable_iframes_lazyload );
$disable_lazyload = array_unique( $disable_lazyload );
$disable_lazyload = wp_sprintf_l( '%l', $disable_lazyload );
$disable_images_lazyload = wp_sprintf_l( '%l', $disable_images_lazyload );
$disable_youtube_lazyload = wp_sprintf_l( '%l', $disable_youtube_lazyload );
$this->settings->add_settings_sections(
[
'lazyload_section' => [
'title' => __( 'LazyLoad', 'rocket' ),
'type' => 'fields_container',
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
'description' => sprintf( __( 'It can improve actual and perceived loading time as images, iframes, and videos will be loaded only as they enter (or about to enter) the viewport and reduces the number of HTTP requests. %1$sMore Info%2$s', 'rocket' ), '<a href="' . esc_url( $lazyload_beacon['url'] ) . '" data-beacon-article="' . esc_attr( $lazyload_beacon['id'] ) . '" target="_blank" rel="noopener noreferrer">', '</a>' ),
'help' => [
'id' => $this->beacon->get_suggest( 'lazyload_section' ),
'url' => $lazyload_beacon['url'],
],
'page' => 'media',
// translators: %1$s = “WP Rocket”, %2$s = a list of plugin names.
'helper' => ! empty( $disable_lazyload ) ? sprintf( __( 'LazyLoad is currently activated in %2$s. If you want to use WP Rocket’s LazyLoad, disable this option in %2$s.', 'rocket' ), WP_ROCKET_PLUGIN_NAME, $disable_lazyload ) : '',
],
'dimensions_section' => [
'title' => __( 'Image Dimensions', 'rocket' ),
'type' => 'fields_container',
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
'description' => sprintf( __( 'Add missing width and height attributes to images. Helps prevent layout shifts and improve the reading experience for your visitors. %1$sMore info%2$s', 'rocket' ), '<a href="' . esc_url( $dimensions['url'] ) . '" data-beacon-article="' . esc_attr( $dimensions['id'] ) . '" target="_blank" rel="noopener noreferrer">', '</a>' ),
'help' => $dimensions,
'page' => 'media',
],
]
);
/**
* Add more content to the 'cache_webp' setting field.
*
* @since 3.4
*
* @param array $cache_webp_field Data to be added to the setting field.
*/
$this->settings->add_settings_fields(
[
'lazyload' => [
'type' => 'checkbox',
'label' => __( 'Enable for images', 'rocket' ),
'section' => 'lazyload_section',
'page' => 'media',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
'container_class' => [
! empty( $disable_images_lazyload ) ? 'wpr-isDisabled' : '',
],
'input_attr' => [
'disabled' => ! empty( $disable_images_lazyload ) ? 1 : 0,
],
// translators: %1$s = “WP Rocket”, %2$s = a list of plugin names.
'description' => ! empty( $disable_images_lazyload ) ? sprintf( __( 'LazyLoad for images is currently activated in %2$s. If you want to use %1$s’s LazyLoad, disable this option in %2$s.', 'rocket' ), WP_ROCKET_PLUGIN_NAME, $disable_images_lazyload ) : '',
],
'lazyload_iframes' => [
'container_class' => [
! empty( $disable_iframes_lazyload ) ? 'wpr-isDisabled' : '',
'wpr-isParent',
],
'type' => 'checkbox',
'label' => __( 'Enable for iframes and videos', 'rocket' ),
'section' => 'lazyload_section',
'page' => 'media',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
'input_attr' => [
'disabled' => ! empty( $disable_iframes_lazyload ) ? 1 : 0,
],
],
'lazyload_youtube' => [
'container_class' => [
! empty( $disable_youtube_lazyload ) ? 'wpr-isDisabled' : '',
'wpr-field--children',
],
'type' => 'checkbox',
'label' => __( 'Replace YouTube iframe with preview image', 'rocket' ),
// translators: %1$s = “WP Rocket”, %2$s = a list of plugin or themes names.
'description' => ! empty( $disable_youtube_lazyload ) ? sprintf( __( 'Replace YouTube iframe with preview image is not compatible with %2$s.', 'rocket' ), WP_ROCKET_PLUGIN_NAME, $disable_youtube_lazyload ) : __( 'This can significantly improve your loading time if you have a lot of YouTube videos on a page.', 'rocket' ),
'parent' => 'lazyload_iframes',
'section' => 'lazyload_section',
'page' => 'media',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
'input_attr' => [
'disabled' => ! empty( $disable_youtube_lazyload ) ? 1 : 0,
],
],
'exclude_lazyload' => [
'container_class' => [
'wpr-Delayjs',
],
'type' => 'textarea',
'label' => __( 'Excluded images or iframes', 'rocket' ),
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
'description' => sprintf( __( 'Specify keywords (e.g. image filename, CSS class, domain) from the image or iframe code to be excluded (one per line). %1$sMore info%2$s', 'rocket' ), '<a href="' . esc_url( $exclude_lazyload['url'] ) . '" data-beacon-article="' . esc_attr( $exclude_lazyload['id'] ) . '" target="_blank" rel="noopener noreferrer">', '</a>' ),
'section' => 'lazyload_section',
'page' => 'media',
'default' => [],
'placeholder' => "example-image.jpg\nslider-image",
],
'image_dimensions' => [
'type' => 'checkbox',
'label' => __( 'Add missing image dimensions', 'rocket' ),
'section' => 'dimensions_section',
'page' => 'media',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
],
]
);
}
/**
* Registers Preload section.
*
* @since 3.0
*/
private function preload_section() {
$this->settings->add_page_section(
'preload',
[
'title' => __( 'Preload', 'rocket' ),
'menu_description' => __( 'Generate cache files, preload fonts', 'rocket' ),
]
);
$bot_beacon = $this->beacon->get_suggest( 'bot' );
$fonts_preload = $this->beacon->get_suggest( 'fonts_preload' );
$preload_links = $this->beacon->get_suggest( 'preload_links' );
$exclusions = $this->beacon->get_suggest( 'preload_exclusions' );
$this->settings->add_settings_sections(
[
'preload_section' => [
'title' => __( 'Preload Cache', 'rocket' ),
'type' => 'fields_container',
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
'description' => __( 'When you enable preloading WP Rocket will automatically detect your sitemaps and save all URLs to the database. The plugin will make sure that your cache is always preloaded.', 'rocket' ),
'help' => [
'id' => $this->beacon->get_suggest( 'sitemap_preload' ),
'url' => $bot_beacon['url'],
],
'page' => 'preload',
],
'preload_links_section' => [
'title' => __( 'Preload Links', 'rocket' ),
'type' => 'fields_container',
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
'description' => sprintf( __( 'Link preloading improves the perceived load time by downloading a page when a user hovers over the link. %1$sMore info%2$s', 'rocket' ), '<a href="' . esc_url( $preload_links['url'] ) . '" data-beacon-article="' . esc_attr( $preload_links['id'] ) . '" target="_blank">', '</a>' ),
'help' => [
'id' => $preload_links['id'],
'url' => $preload_links['url'],
],
'page' => 'preload',
],
'dns_prefetch_section' => [
'title' => __( 'Prefetch DNS Requests', 'rocket' ),
'type' => 'fields_container',
'description' => __( 'DNS prefetching can make external files load faster, especially on mobile networks', 'rocket' ),
'help' => $this->beacon->get_suggest( 'dns_prefetch' ),
'page' => 'preload',
],
'preload_fonts_section' => [
'title' => __( 'Preload Fonts', 'rocket' ),
'type' => 'fields_container',
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
'description' => sprintf( __( 'Improves performance by helping browsers discover fonts in CSS files. %1$sMore info%2$s', 'rocket' ), '<a href="' . esc_url( $fonts_preload['url'] ) . '" data-beacon-article="' . esc_attr( $fonts_preload['id'] ) . '" target="_blank">', '</a>' ),
'help' => [
'id' => $fonts_preload['id'],
'url' => $fonts_preload['url'],
],
'page' => 'preload',
],
]
);
$this->settings->add_settings_fields(
[
'manual_preload' => [
'type' => 'checkbox',
'label' => __( 'Activate Preloading', 'rocket' ),
'section' => 'preload_section',
'page' => 'preload',
'default' => 1,
'sanitize_callback' => 'sanitize_checkbox',
'container_class' => [
'wpr-isParent',
],
],
'preload_excluded_uri' => [
'type' => 'textarea',
'label' => __( 'Exclude URLs', 'rocket' ),
'container_class' => [
'wpr-field--children',
],
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
'description' => sprintf( __( 'Specify URLs to be excluded from the preload feature (one per line). %1$sMore info%2$s', 'rocket' ), '<a href="' . esc_url( $exclusions['url'] ) . '" data-beacon-article="' . esc_attr( $exclusions['id'] ) . '" target="_blank">', '</a>' ),
'placeholder' => '/author/(.*)',
'helper' => 'Use (.*) wildcards to address multiple URLs under a given path.',
'parent' => 'manual_preload',
'section' => 'preload_section',
'page' => 'preload',
'default' => [],
'sanitize_callback' => 'sanitize_textarea',
],
'dns_prefetch' => [
'type' => 'textarea',
'label' => __( 'URLs to prefetch', 'rocket' ),
'description' => __( 'Specify external hosts to be prefetched (no <code>http:</code>, one per line)', 'rocket' ),
'placeholder' => '//example.com',
'section' => 'dns_prefetch_section',
'page' => 'preload',
'default' => [],
'sanitize_callback' => 'sanitize_textarea',
],
'preload_fonts' => [
'type' => 'textarea',
'label' => __( 'Fonts to preload', 'rocket' ),
'description' => __( 'Specify urls of the font files to be preloaded (one per line). Fonts must be hosted on your own domain, or the domain you have specified on the CDN tab.', 'rocket' ),
'helper' => __( 'The domain part of the URL will be stripped automatically.<br/>Allowed font extensions: otf, ttf, svg, woff, woff2.', 'rocket' ),
'placeholder' => '/wp-content/themes/your-theme/assets/fonts/font-file.woff',
'section' => 'preload_fonts_section',
'page' => 'preload',
'default' => [],
'sanitize_callback' => 'sanitize_textarea',
],
'preload_links' => [
'type' => 'checkbox',
'label' => __( 'Enable link preloading', 'rocket' ),
'section' => 'preload_links_section',
'page' => 'preload',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
],
]
);
}
/**
* Registers Advanced Cache section.
*
* @since 3.0
*/
private function advanced_cache_section() {
$this->settings->add_page_section(
'advanced_cache',
[
'title' => __( 'Advanced Rules', 'rocket' ),
'menu_description' => __( 'Fine-tune cache rules', 'rocket' ),
]
);
$ecommerce_beacon = $this->beacon->get_suggest( 'ecommerce' );
$cache_query_strings_beacon = $this->beacon->get_suggest( 'cache_query_strings' );
$never_cache_beacon = $this->beacon->get_suggest( 'exclude_cache' );
$never_cache_cookie_beacon = $this->beacon->get_suggest( 'exclude_cookie' );
$exclude_user_agent_beacon = $this->beacon->get_suggest( 'exclude_user_agent' );
$always_purge_beacon = $this->beacon->get_suggest( 'always_purge' );
$ecommerce_plugin = '';
$reject_uri_desc = __( 'Sensitive pages like custom login/logout URLs should be excluded from cache.', 'rocket' );
if ( function_exists( 'WC' ) && function_exists( 'wc_get_page_id' ) ) {
$ecommerce_plugin = _x( 'WooCommerce', 'plugin name', 'rocket' );
} elseif ( function_exists( 'EDD' ) ) {
$ecommerce_plugin = _x( 'Easy Digital Downloads', 'plugin name', 'rocket' );
} elseif ( function_exists( 'it_exchange_get_page_type' ) && function_exists( 'it_exchange_get_page_url' ) ) {
$ecommerce_plugin = _x( 'iThemes Exchange', 'plugin name', 'rocket' );
} elseif ( defined( 'JIGOSHOP_VERSION' ) && function_exists( 'jigoshop_get_page_id' ) ) {
$ecommerce_plugin = _x( 'Jigoshop', 'plugin name', 'rocket' );
} elseif ( defined( 'WPSHOP_VERSION' ) && class_exists( 'wpshop_tools' ) && method_exists( 'wpshop_tools', 'get_page_id' ) ) {
$ecommerce_plugin = _x( 'WP-Shop', 'plugin name', 'rocket' );
}
if ( ! empty( $ecommerce_plugin ) ) {
$reject_uri_desc .= sprintf(
// translators: %1$s = opening <a> tag, %2$s = plugin name, %3$s closing </a> tag.
__( '<br>Cart, checkout and "my account" pages set in <strong>%1$s%2$s%3$s</strong> will be detected and never cached by default.', 'rocket' ),
'<a href="' . esc_url( $ecommerce_beacon['url'] ) . '" data-beacon-article="' . esc_attr( $ecommerce_beacon['id'] ) . '" target="_blank">',
$ecommerce_plugin,
'</a>'
);
}
$this->settings->add_settings_sections(
[
'cache_reject_uri_section' => [
'title' => __( 'Never Cache URL(s)', 'rocket' ),
'type' => 'fields_container',
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
'description' => $reject_uri_desc,
'help' => $never_cache_beacon,
'page' => 'advanced_cache',
],
'cache_reject_cookies_section' => [
'title' => __( 'Never Cache Cookies', 'rocket' ),
'type' => 'fields_container',
'page' => 'advanced_cache',
'help' => $never_cache_cookie_beacon,
],
'cache_reject_ua_section' => [
'title' => __( 'Never Cache User Agent(s)', 'rocket' ),
'type' => 'fields_container',
'help' => $exclude_user_agent_beacon,
'page' => 'advanced_cache',
],
'cache_purge_pages_section' => [
'title' => __( 'Always Purge URL(s)', 'rocket' ),
'type' => 'fields_container',
'help' => $always_purge_beacon,
'page' => 'advanced_cache',
],
'cache_query_strings_section' => [
'title' => __( 'Cache Query String(s)', 'rocket' ),
'type' => 'fields_container',
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
'description' => sprintf( __( '%1$sCache for query strings%2$s enables you to force caching for specific GET parameters.', 'rocket' ), '<a href="' . esc_url( $cache_query_strings_beacon['url'] ) . '" data-beacon-article="' . esc_attr( $cache_query_strings_beacon['id'] ) . '" target="_blank">', '</a>' ),
'help' => $cache_query_strings_beacon,
'page' => 'advanced_cache',
],
]
);
$this->settings->add_settings_fields(
[
'cache_reject_uri' => [
'type' => 'textarea',
'description' => __( 'Specify URLs of pages or posts that should never be cached (one per line)', 'rocket' ),
'helper' => __( 'The domain part of the URL will be stripped automatically.<br>Use (.*) wildcards to address multiple URLs under a given path.', 'rocket' ),
'placeholder' => '/example/(.*)',
'section' => 'cache_reject_uri_section',
'page' => 'advanced_cache',
'default' => [],
'sanitize_callback' => 'sanitize_textarea',
],
'cache_reject_cookies' => [
'type' => 'textarea',
'description' => __( 'Specify full or partial IDs of cookies that, when set in the visitor\'s browser, should prevent a page from getting cached (one per line)', 'rocket' ),
'section' => 'cache_reject_cookies_section',
'page' => 'advanced_cache',
'default' => [],
'sanitize_callback' => 'sanitize_textarea',
],
'cache_reject_ua' => [
'type' => 'textarea',
'description' => __( 'Specify user agent strings that should never see cached pages (one per line)', 'rocket' ),
'helper' => __( 'Use (.*) wildcards to detect parts of UA strings.', 'rocket' ),
'placeholder' => '(.*)Mobile(.*)Safari(.*)',
'section' => 'cache_reject_ua_section',
'page' => 'advanced_cache',
'default' => [],
'sanitize_callback' => 'sanitize_textarea',
],
'cache_purge_pages' => [
'type' => 'textarea',
'description' => __( 'Specify URLs you always want purged from cache whenever you update any post or page (one per line)', 'rocket' ),
'helper' => __( 'The domain part of the URL will be stripped automatically.<br>Use (.*) wildcards to address multiple URLs under a given path.', 'rocket' ),
'section' => 'cache_purge_pages_section',
'page' => 'advanced_cache',
'default' => [],
'sanitize_callback' => 'sanitize_textarea',
],
'cache_query_strings' => [
'type' => 'textarea',
'description' => __( 'Specify query strings for caching (one per line)', 'rocket' ),
'section' => 'cache_query_strings_section',
'page' => 'advanced_cache',
'default' => [],
'sanitize_callback' => 'sanitize_textarea',
],
]
);
}
/**
* Registers Database section.
*
* @since 3.0
*/
private function database_section() {
$total = [];
foreach ( array_keys( $this->optimize->get_options() ) as $key ) {
$total[ $key ] = $this->optimize->count_cleanup_items( $key );
}
$this->settings->add_page_section(
'database',
[
'title' => __( 'Database', 'rocket' ),
'menu_description' => __( 'Optimize, reduce bloat', 'rocket' ),
]
);
$this->settings->add_settings_sections(
[
'post_cleanup_section' => [
'title' => __( 'Post Cleanup', 'rocket' ),
'type' => 'fields_container',
'description' => __( 'Post revisions and drafts will be permanently deleted. Do not use this option if you need to retain revisions or drafts.', 'rocket' ),
'help' => $this->beacon->get_suggest( 'db_optimization' ),
'page' => 'database',
],
'comments_cleanup_section' => [
'title' => __( 'Comments Cleanup', 'rocket' ),
'type' => 'fields_container',
'description' => __( 'Spam and trashed comments will be permanently deleted.', 'rocket' ),
'page' => 'database',
],
'transients_cleanup_section' => [
'title' => __( 'Transients Cleanup', 'rocket' ),
'type' => 'fields_container',
'description' => __( 'Transients are temporary options; they are safe to remove. They will be automatically regenerated as your plugins require them.', 'rocket' ),
'page' => 'database',
],
'database_cleanup_section' => [
'title' => __( 'Database Cleanup', 'rocket' ),
'type' => 'fields_container',
'description' => __( 'Reduces overhead of database tables', 'rocket' ),
'page' => 'database',
],
'schedule_cleanup_section' => [
'title' => __( 'Automatic Cleanup', 'rocket' ),
'type' => 'fields_container',
'page' => 'database',
],
]
);
$this->settings->add_settings_fields(
[
'database_revisions' => [
'type' => 'checkbox',
'label' => __( 'Revisions', 'rocket' ),
// translators: %s is the number of revisions found in the database. It's a formatted number, don't use %d.
'description' => sprintf( _n( '%s revision in your database.', '%s revisions in your database.', $total['database_revisions'], 'rocket' ), number_format_i18n( $total['database_revisions'] ) ),
'section' => 'post_cleanup_section',
'page' => 'database',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
],
'database_auto_drafts' => [
'type' => 'checkbox',
'label' => __( 'Auto Drafts', 'rocket' ),
// translators: %s is the number of revisions found in the database. It's a formatted number, don't use %d.
'description' => sprintf( _n( '%s draft in your database.', '%s drafts in your database.', $total['database_auto_drafts'], 'rocket' ), number_format_i18n( $total['database_auto_drafts'] ) ),
'section' => 'post_cleanup_section',
'page' => 'database',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
],
'database_trashed_posts' => [
'type' => 'checkbox',
'label' => __( 'Trashed Posts', 'rocket' ),
// translators: %s is the number of revisions found in the database. It's a formatted number, don't use %d.
'description' => sprintf( _n( '%s trashed post in your database.', '%s trashed posts in your database.', $total['database_trashed_posts'], 'rocket' ), $total['database_trashed_posts'] ),
'section' => 'post_cleanup_section',
'page' => 'database',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
],
'database_spam_comments' => [
'type' => 'checkbox',
'label' => __( 'Spam Comments', 'rocket' ),
// translators: %s is the number of revisions found in the database. It's a formatted number, don't use %d.
'description' => sprintf( _n( '%s spam comment in your database.', '%s spam comments in your database.', $total['database_spam_comments'], 'rocket' ), number_format_i18n( $total['database_spam_comments'] ) ),
'section' => 'comments_cleanup_section',
'page' => 'database',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
],
'database_trashed_comments' => [
'type' => 'checkbox',
'label' => __( 'Trashed Comments', 'rocket' ),
// translators: %s is the number of revisions found in the database. It's a formatted number, don't use %d.
'description' => sprintf( _n( '%s trashed comment in your database.', '%s trashed comments in your database.', $total['database_trashed_comments'], 'rocket' ), number_format_i18n( $total['database_trashed_comments'] ) ),
'section' => 'comments_cleanup_section',
'page' => 'database',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
],
'database_all_transients' => [
'type' => 'checkbox',
'label' => __( 'All transients', 'rocket' ),
// translators: %s is the number of revisions found in the database. It's a formatted number, don't use %d.
'description' => sprintf( _n( '%s transient in your database.', '%s transients in your database.', $total['database_all_transients'], 'rocket' ), number_format_i18n( $total['database_all_transients'] ) ),
'section' => 'transients_cleanup_section',
'page' => 'database',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
],
'database_optimize_tables' => [
'type' => 'checkbox',
'label' => __( 'Optimize Tables', 'rocket' ),
// translators: %s is the number of revisions found in the database. It's a formatted number, don't use %d.
'description' => sprintf( _n( '%s table to optimize in your database.', '%s tables to optimize in your database.', $total['database_optimize_tables'], 'rocket' ), number_format_i18n( $total['database_optimize_tables'] ) ),
'section' => 'database_cleanup_section',
'page' => 'database',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
],
'schedule_automatic_cleanup' => [
'container_class' => [
'wpr-isParent',
],
'type' => 'checkbox',
'label' => __( 'Schedule Automatic Cleanup', 'rocket' ),
'description' => '',
'section' => 'schedule_cleanup_section',
'page' => 'database',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
],
'automatic_cleanup_frequency' => [
'container_class' => [
'wpr-field--children',
],
'type' => 'select',
'label' => __( 'Frequency', 'rocket' ),
'description' => '',
'parent' => 'schedule_automatic_cleanup',
'section' => 'schedule_cleanup_section',
'page' => 'database',
'default' => 'daily',
'sanitize_callback' => 'sanitize_text_field',
'choices' => [
'daily' => __( 'Daily', 'rocket' ),
'weekly' => __( 'Weekly', 'rocket' ),
'monthly' => __( 'Monthly', 'rocket' ),
],
],
]
);
}
/**
* Registers CDN section
*
* @since 3.0
*/
private function cdn_section() {
$this->settings->add_page_section(
'page_cdn',
[
'title' => __( 'CDN', 'rocket' ),
'menu_description' => __( 'Integrate your CDN', 'rocket' ),
]
);
$cdn_beacon = $this->beacon->get_suggest( 'cdn' );
$cdn_exclude_beacon = $this->beacon->get_suggest( 'exclude_cdn' );
$this->settings->add_settings_sections(
[
'cdn_section' => [
'title' => __( 'CDN', 'rocket' ),
'type' => 'fields_container',
'description' => __( 'All URLs of static files (CSS, JS, images) will be rewritten to the CNAME(s) you provide.', 'rocket' ) . '<br><em>' . sprintf(
// translators: %1$s = opening link tag, %2$s = closing link tag.
__( 'Not required for services like Cloudflare and Sucuri. Please see our available %1$sAdd-ons%2$s.', 'rocket' ),
'<a href="#addons">',
'</a>'
) . '</em>',
'help' => [
'id' => $this->beacon->get_suggest( 'cdn_section' ),
'url' => $cdn_beacon['url'],
],
'page' => 'page_cdn',
],
'cnames_section' => [
'type' => 'nocontainer',
'page' => 'page_cdn',
],
'exclude_cdn_section' => [
'title' => __( 'Exclude files from CDN', 'rocket' ),
'type' => 'fields_container',
'help' => [
'id' => $cdn_exclude_beacon['id'],
'url' => $cdn_exclude_beacon['url'],
],
'page' => 'page_cdn',
],
]
);
$maybe_display_cdn_helper = '';
$addons = [];
if ( get_rocket_option( 'do_cloudflare' ) ) {
$addons[] = 'Cloudflare';
}
if ( get_rocket_option( 'sucury_waf_cache_sync' ) ) {
$addons[] = 'Sucuri';
}
if ( ! empty( $addons ) ) {
$maybe_display_cdn_helper = wp_sprintf(
// translators: %1$s = opening em tag, %2$l = list of add-on name(s), %3$s = closing em tag.
_n(
'%1$s%2$l Add-on%3$s is currently enabled. Configuration of the CDN settings is not required for %2$l to work on your site.',
'%1$s%2$l Add-ons%3$s are currently enabled. Configuration of the CDN settings is not required for %2$l to work on your site.',
count( $addons ),
'rocket'
),
'<em>',
$addons,
'</em>'
) . '<br>';
}
$this->settings->add_settings_fields(
/**
* Filters the fields for the CDN section.
*
* @since 3.5
* @author Remy Perona
*
* @param array $cdn_settings_fields Data to be added to the CDN section.
*/
apply_filters(
'rocket_cdn_settings_fields',
[
'cdn' => [
'type' => 'checkbox',
'label' => __( 'Enable Content Delivery Network', 'rocket' ),
'helper' => $maybe_display_cdn_helper,
'section' => 'cdn_section',
'page' => 'page_cdn',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
],
'cdn_cnames' => [
'type' => 'cnames',
'label' => __( 'CDN CNAME(s)', 'rocket' ),
'description' => __( 'Specify the CNAME(s) below', 'rocket' ),
'default' => [],
'section' => 'cnames_section',
'page' => 'page_cdn',
],
'cdn_reject_files' => [
'type' => 'textarea',
'description' => __( 'Specify URL(s) of files that should not get served via CDN (one per line).', 'rocket' ),
'helper' => __( 'The domain part of the URL will be stripped automatically.<br>Use (.*) wildcards to exclude all files of a given file type located at a specific path.', 'rocket' ),
'placeholder' => '/wp-content/plugins/some-plugins/(.*).css',
'section' => 'exclude_cdn_section',
'page' => 'page_cdn',
'default' => [],
'sanitize_callback' => 'sanitize_textarea',
],
]
)
);
}
/**
* Registers Heartbeat section.
*
* @since 3.2
*/
private function heartbeat_section() {
$heartbeat_beacon = $this->beacon->get_suggest( 'heartbeat_settings' );
$this->settings->add_page_section(
'heartbeat',
[
'title' => __( 'Heartbeat', 'rocket' ),
'menu_description' => __( 'Control WordPress Heartbeat API', 'rocket' ),
]
);
$this->settings->add_settings_sections(
[
'heartbeat_section' => [
'title' => __( 'Heartbeat', 'rocket' ),
'description' => __( 'Reducing or disabling the Heartbeat API’s activity can help save some of your server’s resources.', 'rocket' ),
'type' => 'fields_container',
'page' => 'heartbeat',
'help' => $heartbeat_beacon,
],
'heartbeat_settings' => [
'title' => __( 'Reduce or disable Heartbeat activity', 'rocket' ),
'description' => __( 'Reducing activity will change Heartbeat frequency from one hit each minute to one hit every 2 minutes.', 'rocket' ) . '<br/>' . __( 'Disabling Heartbeat entirely may break plugins and themes using this API.', 'rocket' ),
'type' => 'fields_container',
'page' => 'heartbeat',
],
]
);
$fields_default = [
'type' => 'select',
'page' => 'heartbeat',
'section' => 'heartbeat_settings',
'sanitize_callback' => 'sanitize_text_field',
'default' => 'reduce_periodicity',
'choices' => [
'' => __( 'Do not limit', 'rocket' ),
'reduce_periodicity' => __( 'Reduce activity', 'rocket' ),
'disable' => __( 'Disable', 'rocket' ),
],
];
$this->settings->add_settings_fields(
[
'control_heartbeat' => [
'type' => 'checkbox',
'label' => __( 'Control Heartbeat', 'rocket' ),
'page' => 'heartbeat',
'section' => 'heartbeat_section',
'sanitize_callback' => 'sanitize_checkbox',
'default' => 0,
],
'heartbeat_admin_behavior' => array_merge(
$fields_default,
[
'label' => __( 'Behavior in backend', 'rocket' ),
'description' => '',
]
),
'heartbeat_editor_behavior' => array_merge(
$fields_default,
[
'label' => __( 'Behavior in post editor', 'rocket' ),
]
),
'heartbeat_site_behavior' => array_merge(
$fields_default,
[
'label' => __( 'Behavior in frontend', 'rocket' ),
]
),
]
);
}
/**
* Registers Add-ons section.
*
* @since 3.0
*/
private function addons_section() {
$webp_beacon = $this->beacon->get_suggest( 'webp' );
$this->settings->add_page_section(
'addons',
[
'title' => __( 'Add-ons', 'rocket' ),
'menu_description' => __( 'Add more features', 'rocket' ),
]
);
$this->settings->add_settings_sections(
[
'one_click' => [
'title' => __( 'One-click Rocket Add-ons', 'rocket' ),
'description' => __( 'One-Click Add-ons are features extending available options without configuration needed. Switch the option "on" to enable from this screen.', 'rocket' ),
'type' => 'addons_container',
'page' => 'addons',
],
]
);
$this->settings->add_settings_sections(
[
'addons' => [
'title' => __( 'Rocket Add-ons', 'rocket' ),
'description' => __( 'Rocket Add-ons are complementary features extending available options.', 'rocket' ),
'type' => 'addons_container',
'page' => 'addons',
],
]
);
$this->settings->add_settings_fields(
[
'do_cloudflare' => [
'type' => 'rocket_addon',
'label' => __( 'Cloudflare', 'rocket' ),
'logo' => [
'url' => WP_ROCKET_ASSETS_IMG_URL . 'logo-cloudflare2.svg',
'width' => 153,
'height' => 51,
],
'title' => __( 'Integrate your Cloudflare account with this add-on.', 'rocket' ),
'description' => __( 'Provide your account email, global API key, and domain to use options such as clearing the Cloudflare cache and enabling optimal settings with WP Rocket.', 'rocket' ),
'section' => 'addons',
'page' => 'addons',
'settings_page' => 'cloudflare',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
],
]
);
/**
* Allow to display the "Varnish" tab in the settings page
*
* @since 2.7
*
* @param bool $display true will display the "Varnish" tab.
*/
if ( apply_filters( 'rocket_display_varnish_options_tab', true ) ) {
$varnish_beacon = $this->beacon->get_suggest( 'varnish' );
$this->settings->add_settings_fields(
/**
* Filters the Varnish field settings data
*
* @since 3.0
* @author Remy Perona
*
* @param array $settings Field settings data.
*/
apply_filters(
'rocket_varnish_field_settings',
[
'varnish_auto_purge' => [
'type' => 'one_click_addon',
'label' => __( 'Varnish', 'rocket' ),
'logo' => [
'url' => WP_ROCKET_ASSETS_IMG_URL . 'logo-varnish.svg',
'width' => 152,
'height' => 135,
],
'title' => __( 'If Varnish runs on your server, you must activate this add-on.', 'rocket' ),
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
'description' => sprintf( __( 'Varnish cache will be purged each time WP Rocket clears its cache to ensure content is always up-to-date.<br>%1$sLearn more%2$s', 'rocket' ), '<a href="' . esc_url( $varnish_beacon['url'] ) . '" data-beacon-article="' . esc_attr( $varnish_beacon['id'] ) . '" target="_blank">', '</a>' ),
'section' => 'one_click',
'page' => 'addons',
'settings_page' => 'varnish',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
],
]
)
);
}
$webp_beacon = $this->beacon->get_suggest( 'webp' );
if ( rocket_valid_key() && ! \Imagify_Partner::has_imagify_api_key() ) {
$imagify_link = '<a href="#imagify">';
} else {
$imagify_link = '<a href="https://wordpress.org/plugins/imagify/" target="_blank" rel="noopener noreferrer">';
}
$this->settings->add_settings_fields(
[
'cache_webp' =>
/**
* Add more content to the 'cache_webp' setting field.
*
* @since 3.10 moved to add-on section
* @since 3.4
*
* @param array $cache_webp_field Data to be added to the setting field.
*/
apply_filters(
'rocket_cache_webp_setting_field',
[
'type' => 'one_click_addon',
'label' => __( 'WebP Compatibility', 'rocket' ),
'logo' => [
'url' => WP_ROCKET_ASSETS_IMG_URL . 'logo-webp.svg',
'width' => 152,
'height' => 135,
],
'title' => __( 'Improve browser compatibility for WebP images.', 'rocket' ),
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
'description' => sprintf(
// translators: %1$s and %3$s = opening <a> tag, %2$s = closing </a> tag.
__( 'Enable this option if you would like WP Rocket to serve WebP images to compatible browsers. Please note that WP Rocket cannot create WebP images for you. To create WebP images we recommend %1$sImagify%2$s. %3$sMore info%2$s', 'rocket' ),
$imagify_link,
'</a>',
'<a href="' . esc_url( $webp_beacon['url'] ) . '" data-beacon-article="' . esc_attr( $webp_beacon['id'] ) . '" target="_blank" rel="noopener noreferrer">'
),
'section' => 'one_click',
'page' => 'addons',
'settings_page' => 'webp',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
'container_class' => [
'wpr-webp-addon',
],
]
),
]
);
if ( defined( 'WP_ROCKET_SUCURI_API_KEY_HIDDEN' ) && WP_ROCKET_SUCURI_API_KEY_HIDDEN ) {
// No need to display the dedicated tab if there is nothing to display on it.
$description = __( 'Clear the Sucuri cache when WP Rocket’s cache is cleared.', 'rocket' );
$settings_page = false;
} else {
$description = __( 'Provide your API key to clear the Sucuri cache when WP Rocket’s cache is cleared.', 'rocket' );
$settings_page = 'sucuri';
}
$this->settings->add_settings_fields(
[
'sucury_waf_cache_sync' => [
'type' => 'rocket_addon',
'label' => __( 'Sucuri', 'rocket' ),
'logo' => [
'url' => WP_ROCKET_ASSETS_IMG_URL . 'logo-sucuri.png',
'width' => 152,
'height' => 56,
],
'title' => __( 'Synchronize Sucuri cache with this add-on.', 'rocket' ),
'description' => $description,
'section' => 'addons',
'page' => 'addons',
'settings_page' => $settings_page,
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
],
]
);
}
/**
* Registers Cloudflare section.
*
* @since 3.0
*/
private function cloudflare_section() {
$this->settings->add_page_section(
'cloudflare',
[
'title' => __( 'Cloudflare', 'rocket' ),
'menu_description' => '',
'class' => [
'wpr-subMenuItem',
'wpr-addonSubMenuItem',
],
]
);
$beacon_cf_credentials = $this->beacon->get_suggest( 'cloudflare_credentials' );
$beacon_cf_settings = $this->beacon->get_suggest( 'cloudflare_settings' );
$beacon_cf_credentials_api = $this->beacon->get_suggest( 'cloudflare_credentials_api' );
$this->settings->add_settings_sections(
[
'cloudflare_credentials' => [
'type' => 'fields_container',
'title' => __( 'Cloudflare credentials', 'rocket' ),
'help' => [
'id' => $beacon_cf_credentials['id'],
'url' => $beacon_cf_credentials['url'],
],
'page' => 'cloudflare',
],
'cloudflare_settings' => [
'type' => 'fields_container',
'title' => __( 'Cloudflare settings', 'rocket' ),
'help' => [
'id' => $beacon_cf_settings['id'],
'url' => $beacon_cf_settings['url'],
],
'page' => 'cloudflare',
],
]
);
if ( ! defined( 'WP_ROCKET_CF_API_KEY_HIDDEN' ) || ! WP_ROCKET_CF_API_KEY_HIDDEN ) {
$this->settings->add_settings_fields(
[
'cloudflare_api_key' => [
'label' => _x( 'Global API key:', 'Cloudflare', 'rocket' ),
'description' => sprintf( '<a href="%1$s" target="_blank">%2$s</a>', esc_url( $beacon_cf_credentials_api['url'] ), _x( 'Find your API key', 'Cloudflare', 'rocket' ) ),
'default' => '',
'section' => 'cloudflare_credentials',
'page' => 'cloudflare',
],
]
);
}
$this->settings->add_settings_fields(
[
'cloudflare_email' => [
'label' => _x( 'Account email', 'Cloudflare', 'rocket' ),
'default' => '',
'container_class' => [
'wpr-field--split',
],
'section' => 'cloudflare_credentials',
'page' => 'cloudflare',
],
'cloudflare_zone_id' => [
'label' => _x( 'Zone ID', 'Cloudflare', 'rocket' ),
'default' => '',
'container_class' => [
'wpr-field--split',
],
'section' => 'cloudflare_credentials',
'page' => 'cloudflare',
],
'cloudflare_devmode' => [
'type' => 'sliding_checkbox',
'label' => __( 'Development mode', 'rocket' ),
// translators: %1$s = link opening tag, %2$s = link closing tag.
'description' => sprintf( __( 'Temporarily activate development mode on your website. This setting will automatically turn off after 3 hours. %1$sLearn more%2$s', 'rocket' ), '<a href="https://support.cloudflare.com/hc/en-us/articles/200168246" target="_blank">', '</a>' ),
'default' => 0,
'section' => 'cloudflare_settings',
'page' => 'cloudflare',
'sanitize_callback' => 'sanitize_checkbox',
],
'cloudflare_auto_settings' => [
'type' => 'sliding_checkbox',
'label' => __( 'Optimal settings', 'rocket' ),
'description' => __( 'Automatically enhances your Cloudflare configuration for speed, performance grade and compatibility.', 'rocket' ),
'default' => 0,
'section' => 'cloudflare_settings',
'page' => 'cloudflare',
'sanitize_callback' => 'sanitize_checkbox',
],
'cloudflare_protocol_rewrite' => [
'type' => 'sliding_checkbox',
'label' => __( 'Relative protocol', 'rocket' ),
'description' => __( 'Should only be used with Cloudflare\'s flexible SSL feature. URLs of static files (CSS, JS, images) will be rewritten to use // instead of http:// or https://.', 'rocket' ),
'default' => 0,
'section' => 'cloudflare_settings',
'page' => 'cloudflare',
'sanitize_callback' => 'sanitize_checkbox',
],
]
);
}
/**
* Registers Sucuri cache section.
*
* @since 3.2
*/
private function sucuri_section() {
if ( defined( 'WP_ROCKET_SUCURI_API_KEY_HIDDEN' ) && WP_ROCKET_SUCURI_API_KEY_HIDDEN ) {
return;
}
$sucuri_beacon = $this->beacon->get_suggest( 'sucuri_credentials' );
$this->settings->add_page_section(
'sucuri',
[
'title' => __( 'Sucuri', 'rocket' ),
'menu_description' => '',
'class' => [
'wpr-subMenuItem',
'wpr-addonSubMenuItem',
],
]
);
$this->settings->add_settings_sections(
[
'sucuri_credentials' => [
'type' => 'fields_container',
'title' => __( 'Sucuri credentials', 'rocket' ),
'page' => 'sucuri',
'help' => [
'id' => $sucuri_beacon['id'],
'url' => $sucuri_beacon['url'],
],
],
]
);
$this->settings->add_settings_fields(
[
'sucury_waf_api_key' => [
'label' => _x( 'Firewall API key (for plugin), must be in format <code>{32 characters}/{32 characters}</code>:', 'Sucuri', 'rocket' ),
'description' => sprintf( '<a href="%1$s" target="_blank">%2$s</a>', 'https://kb.sucuri.net/firewall/Performance/clearing-cache', _x( 'Find your API key', 'Sucuri', 'rocket' ) ),
'default' => '',
'section' => 'sucuri_credentials',
'page' => 'sucuri',
],
]
);
}
/**
* Sets hidden fields.
*
* @since 3.0
*/
private function hidden_fields() {
$this->settings->add_hidden_settings_fields(
/**
* Filters the hidden settings fields
*
* @since 3.5
* @author Remy Perona
*
* @param array $hidden_settings_fields An array of hidden settings fields ID
*/
apply_filters(
'rocket_hidden_settings_fields',
[
'consumer_key',
'consumer_email',
'secret_key',
'license',
'secret_cache_key',
'minify_css_key',
'minify_js_key',
'version',
'cloudflare_old_settings',
'cache_ssl',
'minify_google_fonts',
'emoji',
'remove_unused_css',
'async_css',
]
)
);
}
/**
* Sanitize and format a list.
*
* @since 3.5.5
*
* @param array $list A list of strings.
* @param string $tag_name Name of the HTML tag that will wrap each element of the list.
* @return array
*/
private function sanitize_and_format_list( $list, $tag_name = 'strong' ) {
if ( ! is_array( $list ) || empty( $list ) ) {
return [];
}
$list = array_filter( $list );
if ( empty( $list ) ) {
return [];
}
$list = array_unique( $list );
if ( empty( $tag_name ) ) {
return $list;
}
$format = "<$tag_name>%s</$tag_name>";
return array_map( 'sprintf', array_fill( 0, count( $list ), $format ), $list );
}
/**
* Checks if combine JS option should be disabled
*
* @since 3.9
*
* @return bool
*/
private function disable_combine_js(): bool {
if ( (bool) get_rocket_option( 'delay_js', 0 ) ) {
return true;
}
return ! (bool) get_rocket_option( 'minify_js', 0 );
}
/**
* Checks if combine CSS option should be disabled
*
* @since 3.11
*
* @return bool
*/
private function disable_combine_css(): bool {
if ( (bool) get_rocket_option( 'remove_unused_css', 0 ) ) {
return true;
}
return ! (bool) get_rocket_option( 'minify_css', 0 );
}
/**
* Render radio options sub fields.
*
* @since 3.10
*
* @param array $sub_fields Array of fields to display.
*/
public function display_radio_options_sub_fields( $sub_fields ) {
$sub_fields = $this->settings->set_radio_buttons_sub_fields_value( $sub_fields );
$this->render->render_fields( $sub_fields );
}
}