From 711741138a0f4fe84fc8db995078e658b8ea002a Mon Sep 17 00:00:00 2001 From: Titouan Mathis Date: Fri, 8 Mar 2024 17:10:33 +0100 Subject: [PATCH 1/5] Add methods to easily enqueue assets --- src/Managers/AssetsManager.php | 77 +++++++++++++++++++++++++--------- 1 file changed, 58 insertions(+), 19 deletions(-) diff --git a/src/Managers/AssetsManager.php b/src/Managers/AssetsManager.php index 050892a..02137ea 100644 --- a/src/Managers/AssetsManager.php +++ b/src/Managers/AssetsManager.php @@ -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; } @@ -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; } @@ -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. * @@ -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); } } } @@ -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); } } } @@ -206,13 +233,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); } ); } @@ -220,13 +247,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); } } } @@ -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-`. * From 7d3521be528d8b39a8c87eedc609ae94ddee63c6 Mon Sep 17 00:00:00 2001 From: Titouan Mathis Date: Fri, 8 Mar 2024 17:20:27 +0100 Subject: [PATCH 2/5] Add tests --- tests/Managers/AssetsManagerTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/Managers/AssetsManagerTest.php b/tests/Managers/AssetsManagerTest.php index a23cc4b..16ec741 100644 --- a/tests/Managers/AssetsManagerTest.php +++ b/tests/Managers/AssetsManagerTest.php @@ -10,6 +10,8 @@ */ class AssetsManagerTest extends WP_UnitTestCase { + public AssetsManager $assets_manager; + public function set_up():void { parent::set_up(); @@ -94,4 +96,15 @@ public function test_entries_are_enqueued_by_template() $this->assertTrue(in_array('theme-editor', wp_styles()->queue, true)); $this->assertTrue(in_array('theme-post', wp_styles()->queue, true)); } + + + public function test_entries_are_enqeued_by_method() { + $this->assets_manager->enqueue_script( 'method-app', 'app.js' ); + $this->assertTrue(in_array('theme-method-app', wp_scripts()->queue, true)); + $this->assertTrue(str_ends_with(wp_scripts()->query('theme-method-app')->src, '/app.1234.js')); + + $this->assets_manager->enqueue_style( 'method-styles', 'styles.css' ); + $this->assertTrue(in_array('theme-method-styles', wp_styles()->queue, true)); + $this->assertTrue(str_ends_with(wp_styles()->query('theme-method-styles')->src, '/styles.1234.css')); + } } From b0464753135a3b1fcf1b1d0efd3cb6b7ae989728 Mon Sep 17 00:00:00 2001 From: Titouan Mathis Date: Fri, 8 Mar 2024 17:21:36 +0100 Subject: [PATCH 3/5] Update the changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 790ea77..bfc6879 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ### Added - Add an `EmailManager` to configure `PHPMailer` via environment variables ([#22](https://github.com/studiometa/wp-toolkit/pull/22)) +- Add `enqueue_script($handle, $path)` and `enqueue_style($handle, $path)` method to the `AssetsManager` class ([#23](https://github.com/studiometa/wp-toolkit/pull/23)) - Add a `Plugin::disable` method to the `Plugin` helper class ([#26](https://github.com/studiometa/wp-toolkit/pull/26)) - Add a `request` helper function ([#26](https://github.com/studiometa/wp-toolkit/pull/26)) - Add a `Request` helper class ([#26](https://github.com/studiometa/wp-toolkit/pull/26)) From 765468f71bf93cffeaa65c5542010a04d936ee5c Mon Sep 17 00:00:00 2001 From: Titouan Mathis Date: Fri, 8 Mar 2024 17:21:58 +0100 Subject: [PATCH 4/5] Lint files --- tests/Managers/AssetsManagerTest.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/Managers/AssetsManagerTest.php b/tests/Managers/AssetsManagerTest.php index 16ec741..fa569c9 100644 --- a/tests/Managers/AssetsManagerTest.php +++ b/tests/Managers/AssetsManagerTest.php @@ -98,12 +98,13 @@ public function test_entries_are_enqueued_by_template() } - public function test_entries_are_enqeued_by_method() { - $this->assets_manager->enqueue_script( 'method-app', 'app.js' ); + public function test_entries_are_enqeued_by_method() + { + $this->assets_manager->enqueue_script('method-app', 'app.js'); $this->assertTrue(in_array('theme-method-app', wp_scripts()->queue, true)); $this->assertTrue(str_ends_with(wp_scripts()->query('theme-method-app')->src, '/app.1234.js')); - $this->assets_manager->enqueue_style( 'method-styles', 'styles.css' ); + $this->assets_manager->enqueue_style('method-styles', 'styles.css'); $this->assertTrue(in_array('theme-method-styles', wp_styles()->queue, true)); $this->assertTrue(str_ends_with(wp_styles()->query('theme-method-styles')->src, '/styles.1234.css')); } From 0d61749a924a6b3267d694233d63fe3a86e8e9ed Mon Sep 17 00:00:00 2001 From: Titouan Mathis Date: Fri, 8 Mar 2024 17:24:50 +0100 Subject: [PATCH 5/5] Fix tests --- src/TransientCleaner.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TransientCleaner.php b/src/TransientCleaner.php index 04f0d91..1019c86 100644 --- a/src/TransientCleaner.php +++ b/src/TransientCleaner.php @@ -202,7 +202,7 @@ public function set_stored_transients($value) : TransientCleaner * * @return array|bool New value */ - public function merge_stored_transients_option_values($value, $old_value, $option) + public function merge_stored_transients_option_values($value, $old_value, ?string $option = '') { // Return `$value` if no previous value. if (false === $old_value) {