Skip to content

Instantly share code, notes, and snippets.

@xlplugins
Last active April 14, 2025 08:32
Show Gist options
  • Save xlplugins/6c76875a42641c6e990eb44ae7d81831 to your computer and use it in GitHub Desktop.
Save xlplugins/6c76875a42641c6e990eb44ae7d81831 to your computer and use it in GitHub Desktop.
Funnelkit Checkout Dequeue Theme Assets by using Handler
class WFACP_Dequeue_Theme_Asset_by_Slug {
/**
* Array of asset handles that should be dequeued if they exist
* These will be checked against both JS and CSS registrations
*/
private $assets_to_dequeue = [
'bootstrap',
'select2',
'consulting-style',
'consulting-layout'
];
/**
* Constructor - sets up all necessary hooks
*/
public function __construct() {
// Handle broader theme assets control
add_action( 'wfacp_after_checkout_page_found', [ $this, 'override_theme_assets_handling' ] );
// Hook to selectively dequeue specific JS/CSS files
add_filter( 'wfacp_css_js_deque', [ $this, 'filter_theme_assets' ], 10, 4 );
// Hook to dequeue files from upload directory
add_filter( 'wfacp_css_js_removal_paths', [ $this, 'add_upload_paths_to_dequeue' ], 10, 2 );
// Hook to add custom CSS
add_action( 'wfacp_internal_css', [ $this, 'add_custom_css' ] );
}
/**
* Dequeue specific assets by handle if they are registered
* This runs late in the wp_enqueue_scripts hook to ensure assets are registered first
*/
public function dequeue_specific_assets() {
// Skip if not on the checkout page
if ( ! function_exists( 'WFACP_Common' ) || ! WFACP_Common::is_checkout_page() ) {
return;
}
global $wp_styles, $wp_scripts;
// Check and dequeue registered styles
if ( ! empty( $wp_styles ) && is_object( $wp_styles ) ) {
foreach ( $this->assets_to_dequeue as $handle ) {
if ( wp_style_is( $handle, 'registered' ) ) {
wp_dequeue_style( $handle );
wp_deregister_style( $handle );
error_log( "WFACP: Dequeued CSS: {$handle}" );
}
}
}
// Check and dequeue registered scripts
if ( ! empty( $wp_scripts ) && is_object( $wp_scripts ) ) {
foreach ( $this->assets_to_dequeue as $handle ) {
if ( wp_script_is( $handle, 'registered' ) ) {
wp_dequeue_script( $handle );
wp_deregister_script( $handle );
error_log( "WFACP: Dequeued JS: {$handle}" );
}
}
}
}
/**
* Override the default theme assets handling
*/
public function override_theme_assets_handling() {
if ( ! function_exists( 'wfacp_template' ) ) {
return;
}
$instance = wfacp_template();
if ( is_null( $instance ) || ! method_exists( $instance, 'remove_theme_styling' ) ) {
return;
}
// Add our own action to remove theme styles
add_action( 'wp_print_styles', [ $instance, 'remove_theme_css_and_scripts' ], 100 );
// Remove the default filter
remove_filter( 'wfacp_css_js_deque', [ $instance, 'remove_theme_styling' ], 10, 4 );
// Check and dequeue specific styles (CSS)
add_action( 'wp_enqueue_scripts', [ $this, 'dequeue_specific_assets' ], 999 );
add_action( 'wp_enqueue_scripts', [ $this, 'select2_wc' ] );
}
/**
* Filter theme assets based on file path/URL
*
* @param bool $should_keep Whether the asset should be kept
* @param string $path File path
* @param string $url Full URL of the asset
* @param object $current Current asset object
*
* @return bool Returns false if the asset should be dequeued, true otherwise
*/
public function filter_theme_assets( $should_keep, $path, $url, $current ) {
// Get the handle from the current object if available
$handle = isset( $current->handle ) ? $current->handle : '';
// If we have a handle and it's in our list to dequeue, remove it
if ( ! empty( $handle ) && in_array( $handle, $this->assets_to_dequeue ) ) {
return false;
}
// If the handle check didn't work, we can still check the URL
foreach ( $this->assets_to_dequeue as $asset_name ) {
// Check if the asset name is in the URL (more generic approach)
if ( ! empty( $asset_name ) && strpos( $url, $asset_name ) !== false ) {
return false;
}
}
// If no match found, keep the original decision
return $should_keep;
}
/**
* Add paths from the uploads directory that should be dequeued
*
* @param array $paths Existing paths to dequeue
*
* @return array Modified array of paths
*/
public function add_upload_paths_to_dequeue( $paths ) {
// Add upload directory paths that should be dequeued
$paths[] = 'uploads/wp-scss-cache/pix-woo-style.css';
$paths[] = 'uploads/the7-css/compatibility';
return $paths;
}
/**
* Add custom CSS to the checkout page
*/
public function add_custom_css() {
echo "<style>";
echo 'body #wfacp-sec-wrapper .wfacp_main_form #payment .payment_methods li>.input-radio {
min-width: 1px !important;
width: 16px !important;
margin: 0 10px 0 0 !important;
}';
echo '#wfacp-e-form .woocommerce-checkout-review-order.wfacp-oder-detail {
margin: 0;
padding: 0;
background: transparent;
}';
echo "</style>";
}
public function select2_wc() {
if ( ! defined( 'WC_PLUGIN_FILE' ) ) {
return;
}
$path = plugin_dir_url( WC_PLUGIN_FILE );
wp_register_style( 'select2-css', $path . 'assets/css/select2.css', false, '1.0', 'all' );
wp_register_script( 'select2-js', $path . 'assets/js/select2/select2.js', array( 'jquery' ), '1.0', true );
wp_enqueue_style( 'select2-css' );
wp_enqueue_script( 'select2-js' );
}
}
// Initialize the class
new WFACP_Dequeue_Theme_Asset_by_Slug();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment