-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable multiple registrations #29
base: main
Are you sure you want to change the base?
Changes from all commits
2cb12a5
815003e
9cb46f6
23a3b6b
520eca8
70c559a
ce549ec
1223e3b
2616c76
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,9 @@ | |
|
||
use InvalidArgumentException; | ||
|
||
/** | ||
* Class Asset. | ||
*/ | ||
class Asset { | ||
/** | ||
* @var array The asset action. | ||
|
@@ -1696,27 +1699,190 @@ public function set_as_registered() { | |
return $this; | ||
} | ||
|
||
/** | ||
* Prints the asset using wp_print_scripts or wp_print_styles. | ||
* | ||
* @since 1.4.4 | ||
* | ||
* @return static | ||
*/ | ||
public function print_asset() { | ||
$method = 'wp_print_' . $this->get_script_or_style() . 's'; | ||
$method( $this->get_slug() ); | ||
return $this; | ||
} | ||
|
||
/** | ||
* Enqueues the asset using wp_enqueue_script or wp_enqueue_style. | ||
* | ||
* @since 1.4.4 | ||
* | ||
* @return static | ||
*/ | ||
public function enqueue_asset() { | ||
$method = 'wp_enqueue_' . $this->get_script_or_style(); | ||
$method( $this->get_slug() ); | ||
return $this; | ||
} | ||
Comment on lines
+1722
to
+1726
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be confused with The question I have is this: What is broken about I totally admit that I might be missing something and there is a very very valid reason. I'm just unsure I see it without some tips/hints on what to look at. The concept behind the Assets libraryThe goal is to tell WordPress about an asset so that it can be registered and enqueued at the right moment so the dev doesn't need to go to the precise location and call register and enqueue functions directly. If the dev wants to do that, then the Asset library isn't really adding much benefit. The |
||
|
||
/** | ||
* Registers the asset using wp_register_script or wp_register_style. | ||
* | ||
* @since 1.4.4 | ||
* | ||
* @param string $url The URL to the asset. | ||
* @param array $dependencies The dependencies for the asset. | ||
* @param string $version The version of the asset. | ||
* @param bool|string $in_footer_or_media Whether to enqueue in the footer or the media type for the asset. | ||
* | ||
* @return static | ||
*/ | ||
public function register_asset( string $url = '', array $dependencies = [], string $version = null, $in_footer_or_media = 'all' ) { | ||
$url = $url ? $url : $this->get_url(); | ||
$dependencies = $dependencies ? $dependencies : $this->get_dependencies(); | ||
$version = $version ?? $this->get_version(); | ||
$in_footer_or_media = $in_footer_or_media ? $in_footer_or_media : ( $this->is_js() ? $this->is_in_footer() : $this->get_media() ); | ||
|
||
$method = 'wp_register_' . $this->get_script_or_style(); | ||
$method( $this->get_slug(), $url, $dependencies, $version, $in_footer_or_media ); | ||
return $this; | ||
} | ||
Comment on lines
+1740
to
+1749
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels redundant with |
||
|
||
/** | ||
* Dequeues the asset using wp_dequeue_script or wp_dequeue_style. | ||
* | ||
* @since 1.4.4 | ||
* | ||
* @return static | ||
*/ | ||
public function dequeue_asset() { | ||
$method = 'wp_dequeue_' . $this->get_script_or_style(); | ||
$method( $this->get_slug() ); | ||
return $this; | ||
} | ||
|
||
/** | ||
* Deregisters the asset using wp_deregister_script or wp_deregister_style. | ||
* | ||
* @since 1.4.4 | ||
* | ||
* @return static | ||
*/ | ||
public function deregister_asset() { | ||
$method = 'wp_deregister_' . $this->get_script_or_style(); | ||
$method( $this->get_slug() ); | ||
return $this; | ||
} | ||
|
||
/** | ||
* Check if the asset is something. | ||
* | ||
* In the background uses wp_script_is or wp_style_is. | ||
* | ||
* @since 1.4.4 | ||
* | ||
* @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 1.4.4 | ||
* | ||
* @return string | ||
*/ | ||
protected function get_script_or_style(): string { | ||
return 'js' === $this->get_type() ? 'script' : 'style'; | ||
} | ||
|
||
/** | ||
* Prints the asset | ||
* | ||
* @since 1.4.4 | ||
* | ||
* @return static | ||
*/ | ||
public function do_print() { | ||
if ( $this->should_print() && ! $this->is_printed() ) { | ||
$this->set_as_printed(); | ||
$this->print_asset(); | ||
} | ||
|
||
// 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 1.4.4 | ||
* | ||
* @return static | ||
*/ | ||
public function do_register() { | ||
$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. | ||
* | ||
* @since 1.0.0 | ||
* @since 1.4.4 - Actually dequeues the asset. | ||
* | ||
* @return static | ||
*/ | ||
public function set_as_unenqueued() { | ||
$this->is_enqueued = false; | ||
$this->dequeue_asset(); | ||
return $this; | ||
} | ||
|
||
/** | ||
* Set the asset registration status to false. | ||
* | ||
* @since 1.0.0 | ||
* @since 1.4.4 - Actually deregisters the asset. | ||
* | ||
* @return static | ||
*/ | ||
public function set_as_unregistered() { | ||
$this->is_registered = false; | ||
$this->deregister_asset(); | ||
return $this; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is confusing to have along with
do_print()
. Is there a reason to keep this separated fromdo_print()
? Should we change the behavior ofdo_print()
to callwp_print_*
directly so that we don't have the extra method that might get confused withprint()
?