From b00e052f9d83c212db6794213f19877fd4034738 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Tue, 17 Dec 2024 18:43:35 +0200 Subject: [PATCH] Enable multiple registrations --- src/Assets/Asset.php | 116 ++++++++++++++++++++++++++++++++++++++++++ src/Assets/Assets.php | 81 ++--------------------------- 2 files changed, 121 insertions(+), 76 deletions(-) diff --git a/src/Assets/Asset.php b/src/Assets/Asset.php index db5bdc8..48a74f4 100644 --- a/src/Assets/Asset.php +++ b/src/Assets/Asset.php @@ -3,6 +3,7 @@ namespace StellarWP\Assets; use InvalidArgumentException; +use RuntimeException; class Asset { /** @@ -1692,6 +1693,121 @@ public function set_as_registered() { return $this; } + /** + * Gives us the option to call any wp_*_script or wp_*_style function + * on the asset. + * + * example: $asset->register_asset( ...$args ); will call wp_register_script( $slug, ...$args ); or + * wp_register_style( $slug, ...$args ); based on if the asset is JS or CSS type. + * + * @since TBD + * + * @return static + */ + public function __call( $method, $args ) { + if ( ! strstr( $method, 'asset' ) ) { + throw new RuntimeException( "Method {$method} does not exist." ); + } + + $method = 'wp_' . str_replace( 'asset', $this->get_script_or_style(), $method ); + + if ( ! function_exists( $method ) ) { + throw new RuntimeException( "Method {$method} does not exist." ); + } + + $method( $this->get_slug(), ...$args ); + + return $this; + } + + /** + * Check if the asset is something. + * + * In the background uses wp_script_is or wp_style_is. + * + * @since TBD + * + * @param string $what The what to check against. + * + * @return bool + */ + public function asset_is( string $what ): bool { + return ( 'wp_' . $this->get_script_or_style() . '_is' )( $this->get_slug(), $what ); + } + + /** + * Get the script or style based on the asset type. + * + * @since TBD + * + * @return string + */ + protected function get_script_or_style(): string { + return 'js' === $this->get_type() ? 'script' : 'style'; + } + + /** + * Prints the asset + * + * @since TBD + * + * @return static + */ + public function do_print() { + if ( $this->should_print() && ! $this->is_printed() ) { + $this->set_as_printed(); + $this->print_assets(); + } + + // We print first, and tell the system it was enqueued, WP is smart not to do it twice. + $this->enqueue_asset(); + + if ( ! $this->is_css() ) { + return $this; + } + + foreach ( $this->get_style_data() as $key => $value ) { + wp_style_add_data( $this->get_slug(), $key, $value ); + } + + return $this; + } + + /** + * Performs the asset registration in WP. + * + * @since TBD + * + * @return static + */ + public function do_register() { + if ( $this->is_registered() ) { + return $this; + } + + $this->register_asset( $this->get_url(), $this->get_dependencies(), $this->get_version(), $this->is_js() ? $this->is_in_footer() : $this->get_media() ); + $this->set_as_registered(); + + if ( $this->is_js() ) { + if ( empty( $this->get_translation_path() ) || empty( $this->get_textdomain() ) ) { + return $this; + } + + wp_set_script_translations( $this->get_slug(), $this->get_textdomain(), $this->get_translation_path() ); + return $this; + } + + + $style_data = $this->get_style_data(); + if ( $style_data ) { + foreach ( $style_data as $datum_key => $datum_value ) { + wp_style_add_data( $this->get_slug(), $datum_key, $datum_value ); + } + } + + return $this; + } + /** * Set the asset enqueue status to false. * diff --git a/src/Assets/Assets.php b/src/Assets/Assets.php index e9f719a..0cdd42f 100755 --- a/src/Assets/Assets.php +++ b/src/Assets/Assets.php @@ -643,27 +643,7 @@ protected function do_enqueue( Asset $asset, bool $force_enqueue = false ): void return; } - if ( 'js' === $asset->get_type() ) { - if ( $asset->should_print() && ! $asset->is_printed() ) { - $asset->set_as_printed(); - wp_print_scripts( [ $slug ] ); - } - // We print first, and tell the system it was enqueued, WP is smart not to do it twice. - wp_enqueue_script( $slug ); - } else { - if ( $asset->should_print() && ! $asset->is_printed() ) { - $asset->set_as_printed(); - wp_print_styles( [ $slug ] ); - } - - // We print first, and tell the system it was enqueued, WP is smart not to do it twice. - wp_enqueue_style( $slug ); - - $style_data = $asset->get_style_data(); - foreach ( $style_data as $key => $value ) { - wp_style_add_data( $slug, $key, $value ); - } - } + $asset->do_print(); if ( ! empty( $asset->get_after_enqueue() ) && is_callable( $asset->get_after_enqueue() ) ) { call_user_func_array( $asset->get_after_enqueue(), [ $asset ] ); @@ -713,49 +693,7 @@ public function register_in_wp( $assets = null ) { continue; } - $asset_slug = $asset->get_slug(); - - if ( 'js' === $asset->get_type() ) { - // Script is already registered. - if ( wp_script_is( $asset_slug, 'registered' ) ) { - continue; - } - - wp_register_script( $asset_slug, $asset->get_url(), $asset->get_dependencies(), $asset->get_version(), $asset->is_in_footer() ); - - // Register that this asset is actually registered on the WP methods. - // @phpstan-ignore-next-line - if ( wp_script_is( $asset_slug, 'registered' ) ) { - $asset->set_as_registered(); - } - - if ( - ! empty( $asset->get_translation_path() ) - && ! empty( $asset->get_textdomain() ) - ) { - wp_set_script_translations( $asset_slug, $asset->get_textdomain(), $asset->get_translation_path() ); - } - } else { - // Style is already registered. - if ( wp_style_is( $asset_slug, 'registered' ) ) { - continue; - } - - wp_register_style( $asset_slug, $asset->get_url(), $asset->get_dependencies(), $asset->get_version(), $asset->get_media() ); - - // Register that this asset is actually registered on the WP methods. - // @phpstan-ignore-next-line - if ( wp_style_is( $asset_slug, 'registered' ) ) { - $asset->set_as_registered(); - } - - $style_data = $asset->get_style_data(); - if ( $style_data ) { - foreach ( $style_data as $datum_key => $datum_value ) { - wp_style_add_data( $asset_slug, $datum_key, $datum_value ); - } - } - } + $asset->do_register(); // If we don't have an action we don't even register the action to enqueue. if ( empty( $asset->get_action() ) ) { @@ -788,15 +726,7 @@ public function remove( $slug ) { return false; } - $type = $this->get( $slug )->get_type(); - - if ( $type === 'css' ) { - wp_dequeue_style( $slug ); - wp_deregister_style( $slug ); - } else { - wp_dequeue_script( $slug ); - wp_deregister_script( $slug ); - } + $this->get( $slug )->dequeue_asset()->deregister_asset(); unset( $this->assets[ $slug ] ); @@ -836,9 +766,8 @@ public function print_group( $group, $echo = true ) { if ( $asset->is_registered() ) { continue; } - 'js' === $asset->get_type() - ? wp_register_script( $slug, $asset->get_file(), $asset->get_dependencies(), $asset->get_version() ) - : wp_register_style( $slug, $asset->get_file(), $asset->get_dependencies(), $asset->get_version() ); + + $asset->register_asset( $asset->get_file(), $asset->get_dependencies(), $asset->get_version() ); } ob_start();