-
Notifications
You must be signed in to change notification settings - Fork 220
/
ContactForm7.php
71 lines (62 loc) · 1.86 KB
/
ContactForm7.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
<?php
namespace WP_Rocket\ThirdParty\Plugins;
use WP_Rocket\Event_Management\Subscriber_Interface;
class ContactForm7 implements Subscriber_Interface {
/**
* Required CF6 version.
*
* Version in which the wpcf7_shortcode_callback action was introduced.
*
* @var string
*/
const REQUIRED_CF7_VERSION = '5.8.1';
/**
* Subscribed events.
*/
public static function get_subscribed_events() {
return [
'template_redirect' => 'maybe_optimize_contact_form_7',
];
}
/**
* Optimize ContactForm7 scripts.
*/
public function maybe_optimize_contact_form_7() {
// The wpcf7_shortcode_callback action was added in CF7 version 5.8.1.
if ( ! defined( 'WPCF7_VERSION' ) || version_compare( WPCF7_VERSION, self::REQUIRED_CF7_VERSION, '<' ) ) {
return;
}
// Force scripts and styles to not load by default.
add_filter( 'wpcf7_load_js', '__return_false' );
add_filter( 'wpcf7_load_css', '__return_false' );
// Conditionally enqueue scripts.
add_action( 'wpcf7_shortcode_callback', [ $this, 'conditionally_enqueue_scripts' ] );
add_action( 'wpcf7_shortcode_callback', [ $this, 'conditionally_enqueue_styles' ] );
}
/**
* Enqueue scripts if not already enqueued.
*/
public function conditionally_enqueue_scripts() {
if ( did_action( 'wpcf7_enqueue_scripts' ) ) { // Prevent double-enqueueing when multiple forms present.
return;
}
if ( did_action( 'wp_enqueue_scripts' ) ) {
wpcf7_enqueue_scripts();
return;
}
add_filter( 'wpcf7_load_js', '__return_true', 11 );
}
/**
* Enqueue styles if not already enqueued.
*/
public function conditionally_enqueue_styles() {
if ( did_action( 'wpcf7_enqueue_styles' ) ) { // Prevent double-enqueueing when multiple forms present.
return;
}
if ( did_action( 'wp_enqueue_scripts' ) ) {
wpcf7_enqueue_styles();
return;
}
add_filter( 'wpcf7_load_css', '__return_true', 11 );
}
}