Skip to content

Commit

Permalink
Merge pull request #23 from studiometa/feature/assets-manager-methods
Browse files Browse the repository at this point in the history
[Feature] AssetsManager methods
  • Loading branch information
titouanmathis authored Mar 8, 2024
2 parents 90baa9e + 0d61749 commit b629436
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
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
2 changes: 1 addition & 1 deletion src/TransientCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions tests/Managers/AssetsManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/
class AssetsManagerTest extends WP_UnitTestCase
{
public AssetsManager $assets_manager;

public function set_up():void
{
parent::set_up();
Expand Down Expand Up @@ -94,4 +96,16 @@ 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'));
}
}

0 comments on commit b629436

Please sign in to comment.