Skip to content

Commit

Permalink
Update Enqueue.php
Browse files Browse the repository at this point in the history
removed addAsyncScript method (right now its just not needed) and updated jquery-ui url
  • Loading branch information
Jazz-Man authored Oct 1, 2024
1 parent 3fc1350 commit 8733e46
Showing 1 changed file with 34 additions and 67 deletions.
101 changes: 34 additions & 67 deletions src/Optimization/Enqueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public function load(): void {
add_action( 'wp_enqueue_scripts', self::jsToFooter( ... ) );
add_action( 'wp_enqueue_scripts', self::jqueryFromCdn( ... ) );
add_filter( 'style_loader_tag', self::addAsyncStyle( ... ), 10, 4 );
add_filter( 'script_loader_tag', self::addAsyncScript( ... ), 10, 2 );

if ( ! is_admin() ) {
add_filter( 'script_loader_src', self::setScriptVersion( ... ), 15, 2 );
Expand All @@ -24,7 +23,7 @@ public function load(): void {

public static function addAsyncStyle( string $tag, string $handle, string $href, string $media ): string {
if ( 'print' === $media ) {
return sprintf(
return \sprintf(
'<link rel="%s" id="%s-css" href="%s" media="%s" onload="this.media=\'all\'; this.onload=null;" />',
'stylesheet',
$handle,
Expand All @@ -36,34 +35,6 @@ public static function addAsyncStyle( string $tag, string $handle, string $href,
return $tag;
}

public static function addAsyncScript( string $tag, string $handle ): string {
if ( is_admin() ) {
return $tag;
}

$methods = [
'async' => ['polyfill.io'],
'defer' => ['google-recaptcha'],
];

foreach ( $methods as $method => $handlers ) {
/** @var string[] $validHandlers */
$validHandlers = apply_filters( "app_{$method}_scripts_handlers", $handlers );

if ( empty( $validHandlers ) ) {
continue;
}

if ( ! \in_array( $handle, $validHandlers, true ) ) {
continue;
}

$tag = str_replace( ' src', " {$method} src", $tag );
}

return $tag;
}

public static function setScriptVersion( string $scriptSrc, string $handle ): string {
if ( ! filter_var( $scriptSrc, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED ) ) {
return $scriptSrc;
Expand Down Expand Up @@ -93,57 +64,47 @@ public static function setScriptVersion( string $scriptSrc, string $handle ): st
public static function jqueryFromCdn(): void {
$registered = wp_scripts()->registered;

$suffix = SCRIPT_DEBUG ? '' : '.min';

$jqCore = $registered['jquery-core'];

$jsdelivrUrl = 'https://cdn.jsdelivr.net/npm/[email protected]';
$jqUiCore = $registered['jquery-ui-core'];
$jqMigrate = $registered['jquery-migrate'];

foreach ( array_keys( $registered ) as $handle ) {
if ( str_starts_with( (string) $handle, 'jquery-effects-' ) ) {
$isCore = 'jquery-effects-core' === $handle;
$jqVer = trim( (string) $jqCore->ver, '-wp' );
$jqUiVer = trim( (string) $jqUiCore->ver, '-wp' );
$jqMigrateVer = trim( (string) $jqMigrate->ver, '-wp' );

$newUrl = sprintf(
'%s/ui/effect%s.min.js',
$jsdelivrUrl,
$isCore ? '' : str_replace( 'jquery-effects-', '-', (string) $handle )
);
foreach ( $registered as $handle => $dependency ) {

self::deregisterScript( (string) $handle, $newUrl );
if ( empty( $dependency->src ) ) {
continue;
}

if ( str_starts_with( (string) $handle, 'jquery-ui-' ) ) {
$newUrl = match ( $handle ) {
'jquery-ui-core' => sprintf( '%s/ui/core.min.js', $jsdelivrUrl ),
'jquery-ui-widget' => sprintf( '%s/ui/widget.min.js', $jsdelivrUrl ),
default => sprintf(
'%s/ui/widgets/%s.min.js',
$jsdelivrUrl,
str_replace( 'jquery-ui-', '', (string) $handle )
),
};

self::deregisterScript( (string) $handle, $newUrl );
if ( ! str_contains( $dependency->src, '/'.WPINC.'/' ) ) {
continue;
}
}

$jqVer = trim( (string) $jqCore->ver, '-wp' );
$isJqueryUiCore = str_starts_with( (string) $handle, 'jquery-effects-' ) || str_starts_with( (string) $handle, 'jquery-ui-' );

self::deregisterScript( $jqCore->handle, sprintf( 'https://code.jquery.com/jquery-%s.min.js', esc_attr( $jqVer ) ) );
self::deregisterScript( 'jquery' );
if ( ! $isJqueryUiCore ) {
continue;
}

wp_register_script( 'jquery', false, [$jqCore->handle], $jqVer, true );
}
$dependency->src = false;
}

self::deregisterScript( $jqCore->handle, \sprintf( 'https://cdn.jsdelivr.net/npm/jquery@%s/dist/jquery%s.js', esc_attr( $jqVer ), $suffix ) );
self::deregisterScript( $jqMigrate->handle, \sprintf( 'https://cdn.jsdelivr.net/npm/jquery-migrate@%s/dist/jquery-migrate%s.js', esc_attr( $jqMigrateVer ), $suffix ) );
self::deregisterScript( $jqUiCore->handle, \sprintf( 'https://cdn.jsdelivr.net/npm/jquery-ui-dist@%s/jquery-ui%s.js', esc_attr( $jqUiVer ), $suffix ) );

public static function jsToFooter(): void {
remove_action( 'wp_head', 'wp_print_scripts' );
remove_action( 'wp_head', 'wp_print_head_scripts', 9 );
remove_action( 'wp_head', 'wp_enqueue_scripts', 1 );
}

public static function deregisterScript( string $handle, ?string $newUrl = null, bool $enqueue = false ): void {
$registered = wp_scripts()->registered;

if ( ! empty( $registered[$handle] ) ) {
$jsLib = $registered[$handle];
if ( ! empty( $registered[ $handle ] ) ) {
$jsLib = $registered[ $handle ];

wp_dequeue_script( $jsLib->handle );
wp_deregister_script( $jsLib->handle );
Expand All @@ -160,8 +121,8 @@ public static function deregisterScript( string $handle, ?string $newUrl = null,
public static function deregisterStyle( string $handle, ?string $newUrl = null, bool $enqueue = false ): void {
$registered = wp_styles()->registered;

if ( ! empty( $registered[$handle] ) ) {
$cssLib = $registered[$handle];
if ( ! empty( $registered[ $handle ] ) ) {
$cssLib = $registered[ $handle ];

wp_dequeue_style( $cssLib->handle );
wp_deregister_style( $cssLib->handle );
Expand All @@ -174,6 +135,12 @@ public static function deregisterStyle( string $handle, ?string $newUrl = null,
}
}

private static function jsToFooter(): void {
remove_action( 'wp_head', 'wp_print_scripts' );
remove_action( 'wp_head', 'wp_print_head_scripts', 9 );
remove_action( 'wp_head', 'wp_enqueue_scripts', 1 );
}

private static function prepareScriptFilePath( string $scriptSrc ): bool|string {
$rootDir = app_locate_root_dir();

Expand All @@ -183,7 +150,7 @@ private static function prepareScriptFilePath( string $scriptSrc ): bool|string

$path = ltrim( (string) parse_url( $scriptSrc, PHP_URL_PATH ), '/' );

$file = sprintf( '%s/%s', $rootDir, self::fixMultiSitePath( $path ) );
$file = \sprintf( '%s/%s', $rootDir, self::fixMultiSitePath( $path ) );

return is_readable( $file ) ? $file : false;
}
Expand Down

0 comments on commit 8733e46

Please sign in to comment.