* @since 1.18.0 * @return void */ public function __construct() { // WP Rocket : regular expressions, matched against the whole script tag (delay), the file path (minify, // combine) and the URL of the external files (minify, combine). if ( defined( 'WP_ROCKET_VERSION' ) ) { add_filter( 'rocket_delay_js_exclusions', array( $this, 'add_exclusions' ) ); add_filter( 'rocket_exclude_js', array( $this, 'add_exclusions' ) ); add_filter( 'rocket_minify_excluded_external_js', array( $this, 'add_exclusions' ) ); } // LiteSpeed Cache : substrings of the script URL (minify / combine, defer / delay, guest mode). add_filter( 'litespeed_optimize_js_excludes', array( $this, 'add_exclusions' ) ); add_filter( 'litespeed_optm_js_defer_exc', array( $this, 'add_exclusions' ) ); add_filter( 'litespeed_optm_gm_js_exc', array( $this, 'add_exclusions' ) ); // Autoptimize : substrings of the script tag or path (aggregation), files considered as already minified. add_filter( 'autoptimize_filter_js_exclude', array( $this, 'add_autoptimize_exclusions' ) ); add_filter( 'autoptimize_filter_js_consider_minified', array( $this, 'add_exclusions' ) ); // WP-Optimize : substrings of the script URL (minify). Its "Delay JS" exclusions have no filter. add_filter( 'wp-optimize-minify-default-exclusions', array( $this, 'add_exclusions' ) ); } /** * Returns the patterns identifying the rendering script : the end of the path of the local copy (uuid/hash.js, * which also matches it behind a CDN rewrite) and the CDN URL without its scheme. * @author Affilizz * @since 1.18.0 * @return Array The patterns, filtered by "affilizz_rocket_exclusions" (return an empty array to disable). */ public static function get_exclusions() { // The path, not get_diverted_js_file_url() : it may download the script, and this runs during the page render $local_copy_path = ( Assets::get_instance() )->get_diverted_js_file_path(); $exclusions = array( basename( dirname( $local_copy_path ) ) . '/' . basename( $local_copy_path ), preg_replace( '#^https?://#i', '', Core::get_rendering_script_location() ), ); return array_values( array_filter( (array) apply_filters( 'affilizz_rocket_exclusions', $exclusions ) ) ); } /** * Adds the patterns to a list of exclusions. * @author Affilizz * @since 1.18.0 * @param $exclusions The current exclusions. * @return Array The updated exclusions. */ public function add_exclusions( $exclusions = array() ) { return array_merge( is_array( $exclusions ) ? $exclusions : array(), self::get_exclusions() ); } /** * Adds the patterns to the Autoptimize exclusions, either a comma-separated string or an array of pattern => flag. * @author Affilizz * @since 1.18.0 * @param $exclusions The current exclusions. * @return String|Array The updated exclusions. */ public function add_autoptimize_exclusions( $exclusions = '' ) { if ( is_array( $exclusions ) ) { return array_merge( $exclusions, array_fill_keys( self::get_exclusions(), '' ) ); } return implode( ', ', array_filter( array_merge( array( trim( (string) $exclusions ) ), self::get_exclusions() ) ) ); } }