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 Mar 8, 2024
1 parent 90baa9e commit 7117411
Showing 1 changed file with 58 additions and 19 deletions.
77 changes: 58 additions & 19 deletions src/Managers/AssetsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,14 @@ public function __construct(?string $configuration_filepath = null, ?string $web
}
}

// phpcs:ignore Generic.Commenting.DocComment.MissingShort
/**
* @inheritdoc
* {@inheritdoc}
*/
public function run()
{
if (! file_exists($this->configuration_filepath)) {
$msg = 'No assets configuration file found.';
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
trigger_error(esc_html($msg), E_USER_NOTICE);
return;
}
Expand All @@ -88,7 +87,7 @@ public function run()
if ($this->webpack_manifest_filepath) {
if (! file_exists($this->webpack_manifest_filepath)) {
$msg = sprintf('No webpack manifest file found in `%s`.', $this->webpack_manifest_filepath);
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
trigger_error(esc_html($msg), E_USER_NOTICE);
return;
}
Expand All @@ -103,6 +102,34 @@ 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);
}

/**
* 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);
}

/**
* Get Webpack dist folder relative to the theme.
*
Expand Down Expand Up @@ -156,7 +183,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 @@ -167,7 +194,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);
}
}
}
Expand Down Expand Up @@ -206,27 +233,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);
}
}
}
Expand Down Expand Up @@ -327,28 +354,40 @@ 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)
protected function enqueue_in_action($type, $handle)
{
$handle = $this->format_handle($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 7117411

Please sign in to comment.