Skip to content

Commit af7b9d3

Browse files
committed
feat: add methods to easily get all css or js handles from assets
1 parent 549ab10 commit af7b9d3

File tree

2 files changed

+111
-18
lines changed

2 files changed

+111
-18
lines changed

inc/Enqueue.php

+41
Original file line numberDiff line numberDiff line change
@@ -486,4 +486,45 @@ public function getPrimaryJsHandle( $assets ) {
486486
public function getPrimaryCssHandle( $assets ) {
487487
return $this->getPrimaryHandle( $assets, 'css' );
488488
}
489+
490+
/**
491+
* Get handles of a particular type from enqueued/registered assets.
492+
*
493+
* @param mixed $assets Assets array as returned from enqueue or register.
494+
* @param string $type Type of asset, either `js` or `css`.
495+
* @return array Array of handles. Would return an empty handle if not found.
496+
*/
497+
public function getHandles( $assets, $type = 'js' ) {
498+
$handles = [];
499+
if (
500+
\is_array( $assets )
501+
&& isset( $assets[ $type ] )
502+
&& \count( $assets[ $type ] )
503+
) {
504+
foreach ( $assets[ $type ] as $asset ) {
505+
$handles[] = $asset['handle'];
506+
}
507+
}
508+
return $handles;
509+
}
510+
511+
/**
512+
* Get all css handles from enqueued/registered assets.
513+
*
514+
* @param mixed $assets Assets array as returned from enqueue or register.
515+
* @return array Array of css handles. Would return an empty handle if not found.
516+
*/
517+
public function getCssHandles( $assets ) {
518+
return $this->getHandles( $assets, 'css' );
519+
}
520+
521+
/**
522+
* Get all js handles from enqueued/registered assets.
523+
*
524+
* @param mixed $assets Assets array as returned from enqueue or register.
525+
* @return array Array of js handles. Would return an empty handle if not found.
526+
*/
527+
public function getJsHandles( $assets ) {
528+
return $this->getHandles( $assets, 'js' );
529+
}
489530
}

tests/testcases/EnqueueTest.php

+70-18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77

88
namespace WPackioTest;
99

10+
use WPackio\Enqueue;
11+
use UnexpectedValueException;
12+
use Brain\Monkey\Expectation\Exception\Exception;
13+
use Brain\Monkey\Expectation\Exception\MissedPatchworkReplace;
14+
use LogicException;
15+
use PHPUnit\Framework\ExpectationFailedException;
16+
use SebastianBergmann\RecursionContext\InvalidArgumentException;
17+
1018
class EnqueueTest extends TestCase {
1119
/**
1220
* Mocked Template Directory Uri
@@ -452,6 +460,7 @@ public function test_enqueue() {
452460
}
453461

454462
// Act
463+
/** @var Enqueue $enqueue */
455464
$enqueue->enqueue( 'app', 'main', $config );
456465
}
457466

@@ -465,9 +474,11 @@ public function test_getHandle_throws_exception() {
465474
}
466475

467476
/**
468-
* @testdox `getPrimaryJsHandle` returns the proper handle for proper assets
477+
* Prepare enqueue and assets.
478+
*
479+
* @return (Enqueue|array)[] Touple of Enqueue and assets.
469480
*/
470-
public function test_getPrimaryJsHandle_returns_the_proper_handle_for_proper_assets() {
481+
protected function prepare_enqueue_assets() {
471482
// Prepare
472483
$enqueue = new \WPackio\Enqueue( 'foo', 'dist', '1.0.0', 'plugin', $this->pp );
473484
\Brain\Monkey\Functions\expect( 'wp_register_script' )
@@ -486,6 +497,16 @@ public function test_getPrimaryJsHandle_returns_the_proper_handle_for_proper_ass
486497
'in_footer' => true,
487498
'media' => 'all',
488499
] );
500+
return [ $enqueue, $assets ];
501+
}
502+
503+
/**
504+
* @testdox `getPrimaryJsHandle` returns the proper handle for proper assets
505+
*/
506+
public function test_getPrimaryJsHandle_returns_the_proper_handle_for_proper_assets() {
507+
// Prepare
508+
[ $enqueue, $assets ] = $this->prepare_enqueue_assets();
509+
489510
$handle = $enqueue->getPrimaryJsHandle( $assets );
490511
$this->assertSame( 'wpackio_fooapp_app__main_js_script', $handle );
491512

@@ -502,23 +523,8 @@ public function test_getPrimaryJsHandle_returns_the_proper_handle_for_proper_ass
502523
*/
503524
public function test_getPrimaryCssHandle_returns_the_proper_handle_for_proper_assets() {
504525
// Prepare
505-
$enqueue = new \WPackio\Enqueue( 'foo', 'dist', '1.0.0', 'plugin', $this->pp );
506-
\Brain\Monkey\Functions\expect( 'wp_register_script' )
507-
->atLeast()
508-
->once();
526+
[ $enqueue, $assets ] = $this->prepare_enqueue_assets();
509527

510-
\Brain\Monkey\Functions\expect( 'wp_register_style' )
511-
->atLeast()
512-
->once();
513-
514-
$assets = $enqueue->register( 'app', 'main', [
515-
'js' => true,
516-
'css' => true,
517-
'js_dep' => [],
518-
'css_dep' => [],
519-
'in_footer' => true,
520-
'media' => 'all',
521-
] );
522528
$handle = $enqueue->getPrimaryCssHandle( $assets );
523529
$this->assertSame( 'wpackio_fooapp_app__main_css_style', $handle );
524530

@@ -529,4 +535,50 @@ public function test_getPrimaryCssHandle_returns_the_proper_handle_for_proper_as
529535
$this->assertFalse( $enqueue->getPrimaryCssHandle( [ 'js' => [] ] ) );
530536
$this->assertFalse( $enqueue->getPrimaryCssHandle( [ 'js' => [ 'bla' ] ] ) );
531537
}
538+
539+
/**
540+
* @testdox `getCssHandles` works for proper assets
541+
*/
542+
public function test_getCssHandles_works_for_proper_assets() {
543+
// Prepare
544+
[ $enqueue, $assets ] = $this->prepare_enqueue_assets();
545+
546+
$css_handles = $enqueue->getCssHandles( $assets );
547+
$this->assertEquals(
548+
[ 'wpackio_fooapp_app__main_css_style' ],
549+
$css_handles
550+
);
551+
552+
$this->assertCount( 0, $enqueue->getCssHandles( false ) );
553+
$this->assertCount( 0, $enqueue->getCssHandles( [] ) );
554+
$this->assertCount( 0, $enqueue->getCssHandles( 'foo' ) );
555+
$this->assertCount( 0, $enqueue->getCssHandles( [ 'js' => 'foo' ] ) );
556+
$this->assertCount( 0, $enqueue->getCssHandles( [ 'js' => [] ] ) );
557+
$this->assertCount( 0, $enqueue->getCssHandles( [ 'js' => [ 'bla' ] ] ) );
558+
}
559+
560+
/**
561+
* @testdox `getJsHandles` works for proper assets
562+
*/
563+
public function test_getJsHandles_works_for_proper_assets() {
564+
// Prepare
565+
[ $enqueue, $assets ] = $this->prepare_enqueue_assets();
566+
567+
$css_handles = $enqueue->getJsHandles( $assets );
568+
$this->assertEquals(
569+
[
570+
'wpackio_fooapp_app__runtime_js_script',
571+
'wpackio_fooapp_app__vendor_js_script',
572+
'wpackio_fooapp_app__main_js_script',
573+
],
574+
$css_handles
575+
);
576+
577+
$this->assertCount( 0, $enqueue->getJsHandles( false ) );
578+
$this->assertCount( 0, $enqueue->getJsHandles( [] ) );
579+
$this->assertCount( 0, $enqueue->getJsHandles( 'foo' ) );
580+
$this->assertCount( 0, $enqueue->getJsHandles( [ 'css' => 'foo' ] ) );
581+
$this->assertCount( 0, $enqueue->getJsHandles( [ 'css' => [] ] ) );
582+
$this->assertCount( 0, $enqueue->getJsHandles( [ 'css' => [ 'bla' ] ] ) );
583+
}
532584
}

0 commit comments

Comments
 (0)