Skip to content

Commit

Permalink
Enhance the admin asset API to cover loading strategy as well
Browse files Browse the repository at this point in the history
  • Loading branch information
leonidasmi authored and pls78 committed Oct 16, 2024
1 parent 6e78053 commit e2d5dab
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
11 changes: 9 additions & 2 deletions admin/class-admin-asset-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,21 @@ public function enqueue_user_language_script() {
* @return void
*/
public function register_script( WPSEO_Admin_Asset $script ) {
$url = $script->get_src() ? $this->get_url( $script, WPSEO_Admin_Asset::TYPE_JS ) : false;
$url = $script->get_src() ? $this->get_url( $script, WPSEO_Admin_Asset::TYPE_JS ) : false;
$args = [
'in_footer' => $script->is_in_footer(),
];

if ( $script->get_strategy() !== '' ) {
$args['strategy'] = $script->get_strategy();
}

wp_register_script(
$this->prefix . $script->get_name(),
$url,
$script->get_deps(),
$script->get_version(),
$script->is_in_footer()
$args
);

if ( in_array( 'wp-i18n', $script->get_deps(), true ) ) {
Expand Down
18 changes: 18 additions & 0 deletions admin/class-asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ class WPSEO_Admin_Asset {
*/
protected $in_footer;

/**
* For JS Assets. The script's async/defer strategy.
*
* @var string
*/
protected $strategy;

/**
* For CSS Assets. Whether this stylesheet is a right-to-left stylesheet.
*
Expand All @@ -147,6 +154,7 @@ class WPSEO_Admin_Asset {
'media' => 'all',
'version' => '',
'suffix' => '',
'strategy' => '',
];

/**
Expand All @@ -173,6 +181,7 @@ public function __construct( array $args ) {
$this->version = $args['version'];
$this->media = $args['media'];
$this->in_footer = $args['in_footer'];
$this->strategy = $args['strategy'];
$this->rtl = $args['rtl'];
$this->suffix = $args['suffix'];
}
Expand Down Expand Up @@ -235,6 +244,15 @@ public function is_in_footer() {
return $this->in_footer;
}

/**
* Returns the script asset's async/defer loading strategy.
*
* @return string
*/
public function get_strategy() {
return $this->strategy;
}

/**
* Returns whether this CSS has a RTL counterpart.
*
Expand Down

0 comments on commit e2d5dab

Please sign in to comment.