Skip to content

Commit

Permalink
Enable multiple registrations
Browse files Browse the repository at this point in the history
  • Loading branch information
dpanta94 committed Dec 17, 2024
1 parent b0432d1 commit b00e052
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 76 deletions.
116 changes: 116 additions & 0 deletions src/Assets/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace StellarWP\Assets;

use InvalidArgumentException;
use RuntimeException;

class Asset {
/**
Expand Down Expand Up @@ -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();

Check failure on line 1759 in src/Assets/Asset.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method StellarWP\Assets\Asset::print_assets().
}

// We print first, and tell the system it was enqueued, WP is smart not to do it twice.
$this->enqueue_asset();

Check failure on line 1763 in src/Assets/Asset.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method StellarWP\Assets\Asset::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() );

Check failure on line 1788 in src/Assets/Asset.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method StellarWP\Assets\Asset::register_asset().
$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.
*
Expand Down
81 changes: 5 additions & 76 deletions src/Assets/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ] );
Expand Down Expand Up @@ -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() ) ) {
Expand Down Expand Up @@ -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();

Check failure on line 729 in src/Assets/Assets.php

View workflow job for this annotation

GitHub Actions / phpstan

Cannot call method dequeue_asset() on array|StellarWP\Assets\Asset.

unset( $this->assets[ $slug ] );

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit b00e052

Please sign in to comment.