/
class-page.php
1990 lines (1880 loc) · 74.1 KB
/
class-page.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace WP_Rocket\Admin\Settings;
use \WP_Rocket\Interfaces\Render_Interface;
use WP_Rocket\Admin\Database\Optimization;
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
/**
* Registers the admin page and WP Rocket settings
*
* @since 3.0
* @author Remy Perona
*/
class Page {
/**
* Plugin slug
*
* @since 3.0
* @author Remy Perona
*
* @var string
*/
private $slug;
/**
* Plugin page title
*
* @since 3.0
* @author Remy Perona
*
* @var string
*/
private $title;
/**
* Required capability to access the page
*
* @since 3.0
* @author Remy Perona
*
* @var string
*/
private $capability;
/**
* Settings instance
*
* @since 3.0
* @author Remy Perona
*
* @var Settings
*/
private $settings;
/**
* Render implementation
*
* @since 3.0
* @author Remy Perona
*
* @var Render_Interface
*/
private $render;
/**
* Beacon instance
*
* @since 3.2
* @author Remy Perona
*
* @var Beacon
*/
private $beacon;
/**
* Database optimization instance
*
* @since 3.3
* @author Remy Perona
*
* @var Optimization
*/
private $optimize;
/**
* Constructor
*
* @since 3.0
* @author Remy Perona
*
* @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.
*/
public function __construct( $args, Settings $settings, Render_Interface $render, Beacon $beacon, Optimization $optimize ) {
$this->slug = $args['slug'];
$this->title = $args['title'];
$this->capability = $args['capability'];
$this->settings = $settings;
$this->render = $render;
$this->beacon = $beacon;
$this->optimize = $optimize;
}
/**
* Returns the settings page title
*
* @since 3.3
* @author Remy Perona
*
* @return string
*/
public function get_title() {
return $this->title;
}
/**
* Returns the settings page slug
*
* @since 3.3
* @author Remy Perona
*
* @return string
*/
public function get_slug() {
return $this->slug;
}
/**
* Returns the settings page capability
*
* @since 3.3
* @author Remy Perona
*
* @return string
*/
public function get_capability() {
return $this->capability;
}
/**
* Registers the settings, page sections, fields sections and fields.
*
* @since 3.0
* @author Remy Perona
*
* @return void
*/
public function configure() {
register_setting( $this->slug, WP_ROCKET_SLUG, [ $this->settings, 'sanitize_callback' ] );
}
/**
* Renders the settings page
*
* @since 3.0
* @author Remy Perona
*
* @return void
*/
public function render_page() {
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() );
echo $this->render->generate( 'page', [ 'slug' => $this->slug ] );
}
/**
* Gets customer data from WP Rocket website to display it in the dashboard
*
* @since 3.0
* @author Remy Perona
*
* @return object
*/
private function get_customer_data() {
$customer_key = defined( 'WP_ROCKET_KEY' ) ? WP_ROCKET_KEY : get_rocket_option( 'consumer_key', '' );
$customer_email = defined( 'WP_ROCKET_EMAIL' ) ? WP_ROCKET_EMAIL : get_rocket_option( 'consumer_email', '' );
$response = wp_safe_remote_post(
WP_ROCKET_WEB_MAIN . 'stat/1.0/wp-rocket/user.php',
[
'body' => 'user_id=' . rawurlencode( $customer_email ) . '&consumer_key=' . $customer_key,
]
);
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
return (object) [
'licence_account' => __( 'Unavailable', 'rocket' ),
'licence_expiration' => __( 'Unavailable', 'rocket' ),
'class' => 'wpr-isInvalid',
];
}
$customer_data = json_decode( wp_remote_retrieve_body( $response ) );
if ( 1 <= $customer_data->licence_account && $customer_data->licence_account < 3 ) {
$customer_data->licence_account = 'Single';
} elseif ( '-1' === $customer_data->licence_account ) {
$customer_data->licence_account = 'Infinite';
} else {
$customer_data->licence_account = 'Plus';
}
$customer_data->class = time() < $customer_data->licence_expiration ? 'wpr-isValid' : 'wpr-isInvalid';
$customer_data->licence_expiration = date_i18n( get_option( 'date_format' ), $customer_data->licence_expiration );
return $customer_data;
}
/**
* Returns customer data from transient or request and save it if not cached
*
* @since 3.0
* @author Remy Perona
*
* @return object
*/
public function customer_data() {
if ( false !== get_transient( 'wp_rocket_customer_data' ) ) {
return get_transient( 'wp_rocket_customer_data' );
}
$customer_data = $this->get_customer_data();
set_transient( 'wp_rocket_customer_data', $customer_data, DAY_IN_SECONDS );
return $customer_data;
}
/**
* Toggle sliding checkboxes option value
*
* @since 3.0
* @author Remy Perona
*
* @return void
*/
public function toggle_option() {
check_ajax_referer( 'rocket-ajax' );
if ( ! current_user_can( apply_filters( 'rocket_capability', 'manage_options' ) ) ) {
wp_die();
}
$whitelist = [
'do_beta' => 1,
'analytics_enabled' => 1,
'debug_enabled' => 1,
'varnish_auto_purge' => 1,
'do_cloudflare' => 1,
'cloudflare_devmode' => 1,
'cloudflare_protocol_rewrite' => 1,
'cloudflare_auto_settings' => 1,
'google_analytics_cache' => 1,
'facebook_pixel_cache' => 1,
'sucury_waf_cache_sync' => 1,
'sucury_waf_api_key' => 1,
];
if ( ! isset( $_POST['option']['name'] ) || ! isset( $whitelist[ $_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()
* @author Remy Perona
*
* @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
* @author Remy Perona
*
* @return void
*/
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
* @author Remy Perona
*
* @return void
*/
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(
[
'do_beta' => [
'type' => 'sliding_checkbox',
'label' => __( 'Rocket Tester', 'rocket' ),
'description' => __( 'I am part of the WP Rocket Beta Testing Program.', 'rocket' ),
'section' => 'status',
'page' => 'dashboard',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
],
'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
* @author Remy Perona
*
* @return void
*/
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' );
$cache_ssl_beacon = $this->beacon->get_suggest( 'cache_ssl' );
$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_ssl_section' => [
'title' => __( 'SSL Cache', 'rocket' ),
'type' => 'fields_container',
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
'description' => sprintf( __( '%1$sSSL Cache%2$s works best when your entire website runs on HTTPS.', 'rocket' ), '<a href="' . esc_url( $cache_ssl_beacon['url'] ) . '" data-beacon-article="' . esc_attr( $cache_ssl_beacon['id'] ) . '" target="_blank">', '</a>' ),
'class' => [
rocket_is_ssl_website() ? 'wpr-isHidden' : '',
],
'help' => [
'url' => $cache_ssl_beacon['url'],
'id' => $cache_ssl_beacon['id'],
],
'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 lifespan is the period of time after which all cache files are removed.<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( __( '%1$sMobile cache%2$s works safest with both options enabled. When in doubt, keep both.', '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,
],
],
'cache_ssl' => [
'type' => 'checkbox',
'label' => __( 'Enable caching for pages with <code>https://</code>', 'rocket' ),
'section' => 'cache_ssl_section',
'page' => 'cache',
'default' => rocket_is_ssl_website() ? 1 : 0,
'sanitize_callback' => 'sanitize_checkbox',
],
'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' => [
'MINUTE_IN_SECONDS' => __( 'Minutes', 'rocket' ),
'HOUR_IN_SECONDS' => __( 'Hours', 'rocket' ),
'DAY_IN_SECONDS' => __( 'Days', 'rocket' ),
],
],
]
);
}
/**
* Registers CSS & Javascript section
*
* @since 3.0
* @author Remy Perona
*
* @return void
*/
private function assets_section() {
$remove_qs_beacon = $this->beacon->get_suggest( 'remove_query_strings' );
$combine_beacon = $this->beacon->get_suggest( 'combine' );
$defer_beacon = $this->beacon->get_suggest( 'defer' );
$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' );
$this->settings->add_page_section(
'file_optimization',
[
'title' => __( 'File Optimization', 'rocket' ),
'menu_description' => __( 'Optimize CSS & JS', 'rocket' ),
]
);
$this->settings->add_settings_sections(
[
'basic' => [
'title' => __( 'Basic Settings', 'rocket' ),
'help' => [
'url' => $remove_qs_beacon['url'],
'id' => $this->beacon->get_suggest( 'basic_section' ),
],
'page' => 'file_optimization',
// translators: %1$s = type of minification (HTML, CSS or JS), %2$s = “WP Rocket”.
'helper' => rocket_maybe_disable_minify_html() ? 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' ), 'HTML', WP_ROCKET_PLUGIN_NAME ) : '',
],
'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 ) : '',
],
]
);
$this->settings->add_settings_fields(
[
'minify_html' => [
'type' => 'checkbox',
'label' => __( 'Minify HTML', 'rocket' ),
'container_class' => [
rocket_maybe_disable_minify_html() ? 'wpr-isDisabled' : '',
],
'description' => __( 'Minifying HTML removes whitespace and comments to reduce the size.', 'rocket' ),
'section' => 'basic',
'page' => 'file_optimization',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
'input_attr' => [
'disabled' => rocket_maybe_disable_minify_html() ? 1 : 0,
],
],
'minify_google_fonts' => [
'type' => 'checkbox',
'label' => __( 'Combine Google Fonts files', 'rocket' ),
'description' => __( 'Combining Google Fonts will reduce the number of HTTP requests.', 'rocket' ),
'section' => 'basic',
'page' => 'file_optimization',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
],
'remove_query_strings' => [
'type' => 'checkbox',
'label' => __( 'Remove query strings from static resources', 'rocket' ),
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
'description' => sprintf( __( 'Removes the version query string from static files (e.g. style.css?ver=1.0) and encodes it into the filename instead (e.g. style-1.0.css). Can improve your GTMetrix score. %1$sMore info%2$s', 'rocket' ), '<a href="' . esc_url( $remove_qs_beacon['url'] ) . '" data-beacon-article="' . esc_attr( $remove_qs_beacon['id'] ) . '" target="_blank">', '</a>' ),
'section' => 'basic',
'page' => 'file_optimization',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
],
'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>' ),
'container_class' => [
get_rocket_option( 'minify_css' ) ? '' : 'wpr-isDisabled',
'wpr-field--parent',
],
'section' => 'css',
'page' => 'file_optimization',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
'input_attr' => [
'disabled' => get_rocket_option( 'minify_css' ) ? 0 : 1,
],
'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' => __( 'The domain part of the URL will be stripped automatically.<br>Use (.*).css wildcards to exclude all CSS files located at a specific path.', 'rocket' ),
'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',
],
'async_css' => [
'type' => 'checkbox',
'label' => __( 'Optimize CSS delivery', 'rocket' ),
'container_class' => [
is_plugin_active( 'wp-criticalcss/wp-criticalcss.php' ) ? 'wpr-isDisabled' : '',
'wpr-isParent',
],
'description' => is_plugin_active( 'wp-criticalcss/wp-criticalcss.php' ) ?
// translators: %1$s = plugin name.
sprintf( _x( 'Optimize CSS Delivery is currently handled by the %1$s plugin. If you want to use WP Rocket’s Optimize CSS Delivery 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( __( 'Optimize CSS delivery eliminates render-blocking CSS on your website for faster perceived load time. %1$sMore info%2$s', 'rocket' ), '<a href="' . esc_url( $defer_beacon['url'] ) . '" data-beacon-article="' . esc_attr( $defer_beacon['id'] ) . '" target="_blank">', '</a>' ),
'section' => 'css',
'page' => 'file_optimization',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
'input_attr' => [
'disabled' => is_plugin_active( 'wp-criticalcss/wp-criticalcss.php' ) ? 1 : 0,
],
],
'critical_css' => [
'type' => 'textarea',
'label' => __( 'Fallback critical CSS', 'rocket' ),
'container_class' => [
'wpr-field--children',
],
// 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( $defer_beacon['url'] ) . '#fallback" data-beacon-article="' . esc_attr( $defer_beacon['id'] ) . '" target="_blank">', '</a>' ),
'parent' => 'async_css',
'section' => 'css',
'page' => 'file_optimization',
'default' => [],
'sanitize_callback' => 'sanitize_textarea',
],
'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>' ),
'container_class' => [
get_rocket_option( 'minify_js' ) ? '' : 'wpr-isDisabled',
'wpr-field--parent',
],
'section' => 'js',
'page' => 'file_optimization',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
'input_attr' => [
'disabled' => get_rocket_option( 'minify_js' ) ? 0 : 1,
],
'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' => [
get_rocket_option( 'minify_concatenate_js' ) ? '' : 'wpr-isDisabled',
'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_beacon['url'] ) . '" data-beacon-article="' . esc_attr( $defer_beacon['id'] ) . '" target="_blank">', '</a>' ),
'section' => 'js',
'page' => 'file_optimization',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
],
'defer_all_js_safe' => [
'container_class' => [
'wpr-field--children',
],
'type' => 'checkbox',
'label' => __( 'Safe Mode for jQuery (recommended)', 'rocket' ),
'description' => __( 'Safe mode for jQuery for deferred JS ensures support for inline jQuery references from themes and plugins by loading jQuery at the top of the document as a render-blocking script.<br><em>Deactivating may result in broken functionality, test thoroughly!</em>', 'rocket' ),
'parent' => 'defer_all_js',
'section' => 'js',
'page' => 'file_optimization',
'default' => 1,
'sanitize_callback' => 'sanitize_checkbox',
],
]
);
}
/**
* Registers Media section
*
* @since 3.0
* @author Remy Perona
*
* @return void
*/
private function media_section() {
$lazyload_beacon = $this->beacon->get_suggest( 'lazyload' );
$this->settings->add_page_section(
'media',
[
'title' => __( 'Media', 'rocket' ),
'menu_description' => __( 'LazyLoad, emojis, embeds', 'rocket' ),
]
);
$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">', '</a>' ),
'help' => [
'id' => $this->beacon->get_suggest( 'lazyload_section' ),
'url' => $lazyload_beacon['url'],
],
'page' => 'media',
// translators: %1$s = “WP Rocket”.
'helper' => rocket_maybe_disable_lazyload() ? sprintf( __( 'Lazyload is currently activated in <strong>Autoptimize</strong>. If you want to use %1$s’s lazyload, disable this option in Autoptimize.', 'rocket' ), WP_ROCKET_PLUGIN_NAME ) : '',
],
'emoji_section' => [
'title' => __( 'Emoji 👻', 'rocket' ),
'type' => 'fields_container',
'description' => __( 'Use default emoji of visitor\'s browser instead of loading emoji from WordPress.org', 'rocket' ),
'page' => 'media',
],
'embeds_section' => [
'title' => __( 'Embeds', 'rocket' ),
'type' => 'fields_container',
'description' => __( 'Prevents others from embedding content from your site, prevents you from embedding content from other (non-whitelisted) sites, and removes JavaScript requests related to WordPress embeds', 'rocket' ),
'page' => 'media',
],
]
);
$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' => [
( rocket_avada_maybe_disable_lazyload() || rocket_maybe_disable_lazyload() ) ? 'wpr-isDisabled' : '',
],
'input_attr' => [
'disabled' => ( rocket_avada_maybe_disable_lazyload() || rocket_maybe_disable_lazyload() ) ? 1 : 0,
],
'description' => rocket_avada_maybe_disable_lazyload() ? _x('Lazyload for images is currently activated in Avada. If you want to use WP Rocket’s LazyLoad, disable this option in Avada.', 'Avada', 'rocket' ) : '',
],
'lazyload_iframes' => [
'container_class' => [
rocket_maybe_disable_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' => rocket_maybe_disable_lazyload() ? 1 : 0,
],
],
'lazyload_youtube' => [
'container_class' => [
rocket_maybe_disable_lazyload() ? 'wpr-isDisabled' : '',
'wpr-field--children',
],
'type' => 'checkbox',
'label' => __( 'Replace YouTube iframe with preview image', 'rocket' ),
'description' => __( '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' => rocket_maybe_disable_lazyload() ? 1 : 0,
],
],
'emoji' => [
'type' => 'checkbox',
'label' => __( 'Disable Emoji', 'rocket' ),
'description' => __( 'Disable Emoji will reduce the number of external HTTP requests.', 'rocket' ),
'section' => 'emoji_section',
'page' => 'media',
'default' => 1,
'sanitize_callback' => 'sanitize_checkbox',
],
'embeds' => [
'type' => 'checkbox',
'label' => __( 'Disable WordPress embeds', 'rocket' ),
'section' => 'embeds_section',
'page' => 'media',
'default' => 1,
'sanitize_callback' => 'sanitize_checkbox',
],
]
);
}
/**
* Registers Preload section
*
* @since 3.0
* @author Remy Perona
*
* @return void
*/
private function preload_section() {
$this->settings->add_page_section(
'preload',
[