Skip to content

Commit

Permalink
Add methods to easily enqueue assets
Browse files Browse the repository at this point in the history
  • Loading branch information
titouanmathis committed Oct 20, 2023
1 parent 46422ad commit be5de07
Showing 1 changed file with 53 additions and 17 deletions.
70 changes: 53 additions & 17 deletions src/Managers/AssetsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ public function __construct( ?string $configuration_filepath = null, ?string $we
}
}

// phpcs:ignore Generic.Commenting.DocComment.MissingShort
/**
* @inheritdoc
* {@inheritdoc}
*/
public function run() {
if ( ! file_exists( $this->configuration_filepath ) ) {
Expand Down Expand Up @@ -100,6 +99,32 @@ public function run() {
add_filter( 'template_include', array( $this, 'enqueue_all' ) );
}

/**
* Enqueue a stylesheet.
*
* @param string $handle The handle for this stylesheet.
* @param string $path The stylesheet path in the theme `src/` folder.
*
* @return void
*/
public function enqueue_style( string $handle, string $path ): void {
$this->register( 'style', $handle, $path );
$this->enqueue( 'style', $handle );

Check warning on line 112 in src/Managers/AssetsManager.php

View check run for this annotation

Codecov / codecov/patch

src/Managers/AssetsManager.php#L110-L112

Added lines #L110 - L112 were not covered by tests
}

/**
* Enqueue a script.
*
* @param string $handle The handle for this script.
* @param string $path The stylesheet path in the theme `src/` folder.
*
* @return void
*/
public function enqueue_script( string $handle, string $path ): void {
$this->register( 'script', $handle, $path );
$this->enqueue( 'script', $handle, $path );

Check failure on line 125 in src/Managers/AssetsManager.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3)

Method Studiometa\WPToolkit\Managers\AssetsManager::enqueue() invoked with 3 parameters, 2 required.

Check failure on line 125 in src/Managers/AssetsManager.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4)

Method Studiometa\WPToolkit\Managers\AssetsManager::enqueue() invoked with 3 parameters, 2 required.

Check failure on line 125 in src/Managers/AssetsManager.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0)

Method Studiometa\WPToolkit\Managers\AssetsManager::enqueue() invoked with 3 parameters, 2 required.

Check failure on line 125 in src/Managers/AssetsManager.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Method Studiometa\WPToolkit\Managers\AssetsManager::enqueue() invoked with 3 parameters, 2 required.

Check warning on line 125 in src/Managers/AssetsManager.php

View check run for this annotation

Codecov / codecov/patch

src/Managers/AssetsManager.php#L123-L125

Added lines #L123 - L125 were not covered by tests
}

/**
* Get Webpack dist folder relative to the theme.
*
Expand Down Expand Up @@ -151,7 +176,7 @@ function ( $script, $handle ) {

// Enqueue directly if the name of the config is 'all'.
if ( 'all' === $name ) {
wp_enqueue_style( $handle );
$this->enqueue_in_action( 'style', $handle );
}
}
}
Expand All @@ -162,7 +187,7 @@ function ( $script, $handle ) {

// Enqueue directly if the name of the config is 'all'.
if ( 'all' === $name ) {
wp_enqueue_script( $handle );
$this->enqueue_in_action( 'script', $handle );

Check warning on line 190 in src/Managers/AssetsManager.php

View check run for this annotation

Codecov / codecov/patch

src/Managers/AssetsManager.php#L190

Added line #L190 was not covered by tests
}
}
}
Expand Down Expand Up @@ -200,27 +225,27 @@ public function enqueue_all( $template ) {

$webpack_entry->styles->keys()->each(
function ( $handle ) {
$this->enqueue( 'style', $handle );
$this->enqueue_in_action( 'style', $handle );
}
);

$webpack_entry->scripts->keys()->each(
function ( $handle ) {
$this->enqueue( 'script', $handle );
$this->enqueue_in_action( 'script', $handle );
}
);
}
}

if ( isset( $config['css'] ) ) {
foreach ( $config['css'] as $handle => $path ) {
$this->enqueue( 'style', $handle );
$this->enqueue_in_action( 'style', $handle );
}
}

if ( isset( $config['js'] ) ) {
foreach ( $config['js'] as $handle => $path ) {
$this->enqueue( 'script', $handle );
$this->enqueue_in_action( 'script', $handle );

Check warning on line 248 in src/Managers/AssetsManager.php

View check run for this annotation

Codecov / codecov/patch

src/Managers/AssetsManager.php#L248

Added line #L248 was not covered by tests
}
}
}
Expand Down Expand Up @@ -319,27 +344,38 @@ protected function register( string $type, string $handle, $path ):void {
}

/**
* Enqueue an asset given its handle.
* Enqueue an asset given its handle in the `wp_enqueue_scripts` action.
*
* @param string $type The type of the asset: 'style' or 'script'.
* @param string $handle The asset's handle.
* @return void
*/
protected function enqueue( $type, $handle ) {
$handle = $this->format_handle( $handle );

protected function enqueue_in_action( $type, $handle ) {
add_action(
'wp_enqueue_scripts',
function () use ( $type, $handle ) {
if ( 'style' === $type ) {
wp_enqueue_style( $handle );
} else {
wp_enqueue_script( $handle );
}
$this->enqueue( $type, $handle );
}
);
}

/**
* Enqueue an asset directly given its handle.
*
* @param string $type The type of the asset: 'style' or 'script'.
* @param string $handle The asset's handle.
* @return void
*/
protected function enqueue( $type, $handle ) {
$handle = $this->format_handle( $handle );

if ( 'style' === $type ) {
wp_enqueue_style( $handle );
} else {
wp_enqueue_script( $handle );
}
}

/**
* Prefix all handles with `theme-`.
*
Expand Down

0 comments on commit be5de07

Please sign in to comment.