Skip to content

Commit

Permalink
Add optimization for Contact Form 7 (#6215)
Browse files Browse the repository at this point in the history
Co-authored-by: Ahmed Saed <[email protected]>
Co-authored-by: Rémy Perona <[email protected]>
Co-authored-by: Weston Ruter <[email protected]>
Co-authored-by: WordPress Fan <[email protected]>
  • Loading branch information
5 people authored Nov 29, 2023
1 parent c1bce62 commit f588be9
Show file tree
Hide file tree
Showing 3 changed files with 65 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 @@ -381,6 +381,7 @@ private function init_common_subscribers() {
'lazyload_css_subscriber',
'shoptimizer',
'weglot',
'contactform7',
];

$host_type = HostResolver::get_host_service();
Expand Down
61 changes: 61 additions & 0 deletions inc/ThirdParty/Plugins/ContactForm7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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', 10 ],
];
}

/**
* 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', PHP_INT_MAX );
add_filter( 'wpcf7_load_css', '__return_false', PHP_INT_MAX );

// 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.
wpcf7_enqueue_scripts();
}
}

/**
* Enqueue styles if not already enqueued.
*/
public function conditionally_enqueue_styles() {
if ( ! did_action( 'wpcf7_enqueue_styles' ) ) { // Prevent double-enqueueing when multiple forms present.
wpcf7_enqueue_styles();
}
}
}
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 @@ -103,6 +104,7 @@ class ServiceProvider extends AbstractServiceProvider {
'translatepress',
'wpgeotargeting',
'weglot',
'contactform7',
];

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

0 comments on commit f588be9

Please sign in to comment.