Skip to content

Commit

Permalink
Add optimization for Contact Form 7
Browse files Browse the repository at this point in the history
Onload load Contact Form 7’s JavaScript if a form is present on the page.
  • Loading branch information
adamsilverstein committed Sep 27, 2023
1 parent 539ecea commit 2b82b18
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions inc/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ private function init_common_subscribers() {
'translatepress',
'themify',
'wpgeotargeting',
'contactform7',
'lazyload_css_subscriber',
'shoptimizer',
];
Expand Down
28 changes: 28 additions & 0 deletions inc/ThirdParty/Plugins/ContactForm7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
namespace WP_Rocket\ThirdParty\Plugins;

use WP_Rocket\Event_Management\Subscriber_Interface;

class ContactForm7 implements Subscriber_Interface {
/**
* Subscribed events.
*/
public static function get_subscribed_events() {
return [
'wp' => [ 'maybe_optimize_contact_form_7', 10 ],
];
}

/**
* Optimize ContactForm7 scripts.
*
* @return array

Check notice on line 19 in inc/ThirdParty/Plugins/ContactForm7.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/ThirdParty/Plugins/ContactForm7.php#L19

Function return type is not void, but function has no return statement
*/
public function maybe_optimize_contact_form_7() {
if ( defined( 'WPCF7_LOAD_JS' ) && WPCF7_LOAD_JS && ! has_action( 'wpcf7_shortcode_callback' ) ) {
// Only load the frontend scripts on the new 'wpcf7_shortcode_callback' hook.
add_filter( 'wpcf7_load_js', '__return_false' );
add_action( 'wpcf7_shortcode_callback', 'wpcf7_enqueue_scripts' );
}
}
}
3 changes: 3 additions & 0 deletions inc/ThirdParty/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use WP_Rocket\ThirdParty\Plugins\CDN\{Cloudflare,CloudflareFacade};
use WP_Rocket\ThirdParty\Plugins\Jetpack;
use WP_Rocket\ThirdParty\Plugins\WPGeotargeting;
use WP_Rocket\ThirdParty\Plugins\ContactForm7;
use WP_Rocket\ThirdParty\Plugins\SEO\RankMathSEO;
use WP_Rocket\ThirdParty\Plugins\SEO\AllInOneSEOPack;
use WP_Rocket\ThirdParty\Plugins\SEO\SEOPress;
Expand Down Expand Up @@ -101,6 +102,7 @@ class ServiceProvider extends AbstractServiceProvider {
'rapidload',
'translatepress',
'wpgeotargeting',
'contactform7'
];

/**
Expand Down Expand Up @@ -257,5 +259,6 @@ public function register() {
->share( 'rapidload', RapidLoad::class );
$this->getContainer()->share( 'translatepress', TranslatePress::class );
$this->getContainer()->share( 'wpgeotargeting', WPGeotargeting::class );
$this->getContainer()->share( 'contactform7', ContactForm7::class );
}
}

0 comments on commit 2b82b18

Please sign in to comment.