From be5de071c7efe7f20d071c00a2961a54f38ca698 Mon Sep 17 00:00:00 2001 From: Titouan Mathis Date: Fri, 20 Oct 2023 16:08:54 +0200 Subject: [PATCH] Add methods to easily enqueue assets --- src/Managers/AssetsManager.php | 70 +++++++++++++++++++++++++--------- 1 file changed, 53 insertions(+), 17 deletions(-) diff --git a/src/Managers/AssetsManager.php b/src/Managers/AssetsManager.php index 3c337a8..04bc44c 100644 --- a/src/Managers/AssetsManager.php +++ b/src/Managers/AssetsManager.php @@ -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 ) ) { @@ -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 ); + } + + /** + * 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 ); + } + /** * Get Webpack dist folder relative to the theme. * @@ -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 ); } } } @@ -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 ); } } } @@ -200,13 +225,13 @@ 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 ); } ); } @@ -214,13 +239,13 @@ function ( $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 ); } } } @@ -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-`. *