Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aaemnnosttv/2bc6fab481236fd920dd895ea8adcfd6 to your computer and use it in GitHub Desktop.
Save aaemnnosttv/2bc6fab481236fd920dd895ea8adcfd6 to your computer and use it in GitHub Desktop.
Helper plugin that excludes all Google Site Kit assets from SG optimizations
<?php
/**
* Plugin Name: Speed Optimizer - Google Site Kit Exclude
* Description: Helper plugin that excludes all Google Site Kit assets from SG optimizations.
* Author: Evan Mattson
* Version: 1.0.0
* Requires PHP: 7.4
*/
// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Create a filter callback that excludes Google Site Kit handles.
*
* @param string $type Either 'scripts' or 'styles'.
* @return callable Filter callback function.
*/
function sg_cachepress_create_exclude_callback( $type ) {
return function ( $excluded_items ) use ( $type ) {
$wp_deps = 'scripts' === $type ? wp_scripts() : wp_styles();
$googlesitekit_handles = array_filter(
array_keys( $wp_deps->registered ),
fn( $handle ) => 0 === strpos( $handle, 'googlesitekit' )
);
return array_merge( (array) $excluded_items, $googlesitekit_handles );
};
}
// JavaScript exclusions.
add_filter( 'sgo_js_minify_exclude', sg_cachepress_create_exclude_callback( 'scripts' ) );
add_filter( 'sgo_javascript_combine_exclude', sg_cachepress_create_exclude_callback( 'scripts' ) );
add_filter( 'sgo_js_async_exclude', sg_cachepress_create_exclude_callback( 'scripts' ) );
// CSS exclusions.
add_filter( 'sgo_css_minify_exclude', sg_cachepress_create_exclude_callback( 'styles' ) );
add_filter( 'sgo_css_combine_exclude', sg_cachepress_create_exclude_callback( 'styles' ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment