-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conditionally enqueue scripts and styles only if there is a contact form rendered #1279
Changes from 4 commits
6efd7c9
98d8a53
50c901c
3194c1a
9d8fee2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,11 +29,11 @@ function wpcf7_control_init() { | |
|
||
|
||
/** | ||
* Registers main scripts and styles. | ||
* Registers main scripts. | ||
*/ | ||
add_action( | ||
'wp_enqueue_scripts', | ||
static function () { | ||
'wp_default_scripts', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that the use of |
||
static function ( WP_Scripts $scripts ) { | ||
$assets = array(); | ||
$asset_file = wpcf7_plugin_path( 'includes/js/index.asset.php' ); | ||
|
||
|
@@ -46,46 +46,52 @@ static function () { | |
'version' => WPCF7_VERSION, | ||
) ); | ||
|
||
wp_register_script( | ||
$scripts->add( | ||
'contact-form-7', | ||
wpcf7_plugin_url( 'includes/js/index.js' ), | ||
array_merge( | ||
$assets['dependencies'], | ||
array( 'swv' ) | ||
), | ||
$assets['version'], | ||
true | ||
1 // in_footer | ||
); | ||
|
||
wp_register_script( | ||
$scripts->add( | ||
'contact-form-7-html5-fallback', | ||
wpcf7_plugin_url( 'includes/js/html5-fallback.js' ), | ||
array( 'jquery-ui-datepicker' ), | ||
WPCF7_VERSION, | ||
true | ||
1 // in_footer | ||
); | ||
}, | ||
10, 1 | ||
); | ||
|
||
if ( wpcf7_load_js() ) { | ||
wpcf7_enqueue_scripts(); | ||
} | ||
|
||
wp_register_style( | ||
/** | ||
* Registers main styles. | ||
*/ | ||
add_action( | ||
'wp_default_styles', | ||
static function ( WP_Styles $styles ) { | ||
$styles->add( | ||
'contact-form-7', | ||
wpcf7_plugin_url( 'includes/css/styles.css' ), | ||
array(), | ||
WPCF7_VERSION, | ||
'all' | ||
); | ||
|
||
wp_register_style( | ||
$styles->add( | ||
'contact-form-7-rtl', | ||
wpcf7_plugin_url( 'includes/css/styles-rtl.css' ), | ||
array( 'contact-form-7' ), | ||
WPCF7_VERSION, | ||
'all' | ||
); | ||
|
||
wp_register_style( | ||
$styles->add( | ||
'jquery-ui-smoothness', | ||
wpcf7_plugin_url( | ||
'includes/js/jquery-ui/themes/smoothness/jquery-ui.min.css' | ||
|
@@ -94,19 +100,19 @@ static function () { | |
'1.12.1', | ||
'screen' | ||
); | ||
|
||
if ( wpcf7_load_css() ) { | ||
wpcf7_enqueue_styles(); | ||
} | ||
}, | ||
10, 0 | ||
10, 1 | ||
); | ||
|
||
|
||
/** | ||
* Enqueues scripts. | ||
*/ | ||
function wpcf7_enqueue_scripts() { | ||
if ( did_action( 'wpcf7_enqueue_scripts' ) ) { | ||
return; | ||
} | ||
|
||
wp_enqueue_script( 'contact-form-7' ); | ||
|
||
$wpcf7 = array( | ||
|
@@ -138,6 +144,10 @@ function wpcf7_script_is() { | |
* Enqueues styles. | ||
*/ | ||
function wpcf7_enqueue_styles() { | ||
if ( did_action( 'wpcf7_enqueue_styles' ) ) { | ||
return; | ||
} | ||
|
||
wp_enqueue_style( 'contact-form-7' ); | ||
|
||
if ( wpcf7_is_rtl() ) { | ||
|
@@ -157,7 +167,7 @@ function wpcf7_style_is() { | |
|
||
|
||
add_action( | ||
'wp_enqueue_scripts', | ||
'wpcf7_enqueue_scripts', | ||
'wpcf7_html5_fallback', | ||
20, 0 | ||
); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that these styles only affect the response related markup that is only triggered by an AJAX submission, it should be fine to load the CSS later - i.e. no layout shifts 👍