Skip to content

Commit 8cdae0e

Browse files
authored
Merge pull request #3196 from justlevine/feat/asset-group-cherrypick
feat: expose `EnqueuedAsset.group` and `EnqueuedScript.location` to schema
2 parents 0c4ee0c + cd6c302 commit 8cdae0e

File tree

6 files changed

+73
-5
lines changed

6 files changed

+73
-5
lines changed

src/Registry/TypeRegistry.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
use WPGraphQL\Type\Enum\PostObjectsConnectionOrderbyEnum;
5555
use WPGraphQL\Type\Enum\PostStatusEnum;
5656
use WPGraphQL\Type\Enum\RelationEnum;
57+
use WPGraphQL\Type\Enum\ScriptLoadingGroupLocationEnum;
5758
use WPGraphQL\Type\Enum\ScriptLoadingStrategyEnum;
5859
use WPGraphQL\Type\Enum\TaxonomyEnum;
5960
use WPGraphQL\Type\Enum\TaxonomyIdTypeEnum;
@@ -356,6 +357,7 @@ public function init_type_registry( self $type_registry ) {
356357
PostStatusEnum::register_type();
357358
RelationEnum::register_type();
358359
ScriptLoadingStrategyEnum::register_type();
360+
ScriptLoadingGroupLocationEnum::register_type();
359361
TaxonomyEnum::register_type();
360362
TaxonomyIdTypeEnum::register_type();
361363
TermNodeIdTypeEnum::register_type();
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Register the ScriptLoadingGroupLocationEnum Type to the Schema
4+
*
5+
* @package WPGraphQL\Type\Enum
6+
* @since TBD
7+
*/
8+
9+
namespace WPGraphQL\Type\Enum;
10+
11+
/**
12+
* Class ScriptLoadingGroupLocationEnum
13+
*/
14+
class ScriptLoadingGroupLocationEnum {
15+
16+
/**
17+
* Register the ScriptLoadingStrategy Enum Type to the Schema
18+
*
19+
* @return void
20+
*/
21+
public static function register_type() {
22+
register_graphql_enum_type(
23+
'ScriptLoadingGroupLocationEnum',
24+
[
25+
'description' => __( 'Location in the document where the script to be loaded', 'wp-graphql' ),
26+
'values' => [
27+
'HEADER' => [
28+
'value' => 0,
29+
'description' => __( 'A script to be loaded in document `<head>` tag', 'wp-graphql' ),
30+
],
31+
'FOOTER' => [
32+
'value' => 1,
33+
'description' => __( 'A script to be loaded in document at right before the closing `<body>` tag', 'wp-graphql' ),
34+
],
35+
],
36+
]
37+
);
38+
}
39+
}

src/Type/InterfaceType/EnqueuedAsset.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ static function ( $before ) {
124124
return isset( $asset->extra['data'] ) ? $asset->extra['data'] : null;
125125
},
126126
],
127+
'group' => [
128+
'type' => 'Int',
129+
'description' => __( 'The loading group to which this asset belongs.', 'wp-graphql' ),
130+
'resolve' => static function ( $asset ) {
131+
return isset( $asset->extra['group'] ) ? (int) $asset->extra['group'] : null;
132+
},
133+
],
127134
],
128135
]
129136
);

src/Type/ObjectType/EnqueuedScript.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ public static function register_type() {
2323
'description' => __( 'Script enqueued by the CMS', 'wp-graphql' ),
2424
'interfaces' => [ 'Node', 'EnqueuedAsset' ],
2525
'fields' => [
26-
'id' => [
26+
'id' => [
2727
'type' => [ 'non_null' => 'ID' ],
2828
'description' => __( 'The global ID of the enqueued script', 'wp-graphql' ),
2929
'resolve' => static function ( $asset ) {
3030
return isset( $asset->handle ) ? Relay::toGlobalId( 'enqueued_script', $asset->handle ) : null;
3131
},
3232
],
33-
'dependencies' => [
33+
'dependencies' => [
3434
'type' => [ 'list_of' => 'EnqueuedScript' ],
3535
'description' => __( 'Dependencies needed to use this asset', 'wp-graphql' ),
3636
],
37-
'extraData' => [
37+
'extraData' => [
3838
'type' => 'String',
3939
'description' => __( 'Extra data supplied to the enqueued script', 'wp-graphql' ),
4040
'resolve' => static function ( \_WP_Dependency $script ) {
@@ -45,7 +45,7 @@ public static function register_type() {
4545
return $script->extra['data'];
4646
},
4747
],
48-
'strategy' => [
48+
'strategy' => [
4949
'type' => 'ScriptLoadingStrategyEnum',
5050
'description' => __( 'The loading strategy to use on the script tag', 'wp-graphql' ),
5151
'resolve' => static function ( \_WP_Dependency $script ) {
@@ -56,7 +56,14 @@ public static function register_type() {
5656
return $script->extra['strategy'];
5757
},
5858
],
59-
'version' => [
59+
'groupLocation' => [
60+
'type' => 'ScriptLoadingGroupLocationEnum',
61+
'description' => __( 'The location where this script should be loaded', 'wp-graphql' ),
62+
'resolve' => static function ( \_WP_Dependency $script ) {
63+
return isset( $script->extra['group'] ) ? (int) $script->extra['group'] : 0;
64+
},
65+
],
66+
'version' => [
6067
'description' => __( 'The version of the enqueued script', 'wp-graphql' ),
6168
'resolve' => static function ( \_WP_Dependency $script ) {
6269
/** @var \WP_Scripts $wp_scripts */

tests/wpunit/RegisteredScriptConnectionQueriesTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public function getQuery() {
4040
handle
4141
}
4242
extraData
43+
group
44+
groupLocation
4345
handle
4446
id
4547
src
@@ -52,6 +54,12 @@ public function getQuery() {
5254
}
5355

5456
public function testForwardPagination() {
57+
// Mocks the values of the ScriptLoadingGroupLocationEnum.
58+
$location_enum_mock = [
59+
0 => 'HEADER',
60+
1 => 'FOOTER',
61+
];
62+
5563
wp_set_current_user( $this->admin );
5664
$query = $this->getQuery();
5765

@@ -76,11 +84,14 @@ public function testForwardPagination() {
7684
$expected_after = ! empty( $expected->extra['after'] ) ? ( array_filter( $expected->extra['after'], 'is_string' ) ?: null ) : null;
7785
$expected_before = ! empty( $expected->extra['before'] ) ? ( array_filter( $expected->extra['before'], 'is_string' ) ?: null ) : null;
7886

87+
7988
$this->assertEquals( $expected_after, $actual['data']['registeredScripts']['nodes'][0]['after'] );
8089
$this->assertEquals( $expected_before, $actual['data']['registeredScripts']['nodes'][0]['before'] );
8190
$this->assertEquals( ! empty( $expected->extra['conditional'] ) ? $expected->extra['conditional'] : null, $actual['data']['registeredScripts']['nodes'][0]['conditional'] );
8291
$this->assertEquals( $expected->handle, $actual['data']['registeredScripts']['nodes'][0]['handle'] );
8392
$this->assertEquals( ! empty( $expected->extra['data'] ) ? $expected->extra['data'] : null, $actual['data']['registeredScripts']['nodes'][0]['extraData'] );
93+
$this->assertEquals( isset( $expected->extra['group'] ) ? (int) $expected->extra['group'] : 0, $actual['data']['registeredScripts']['nodes'][0]['group'] );
94+
$this->assertEquals( $location_enum_mock[ isset( $expected->extra['group'] ) ? (int) $expected->extra['group'] : 0 ], $actual['data']['registeredScripts']['nodes'][0]['groupLocation'] );
8495
$this->assertEquals( $expected->src, $actual['data']['registeredScripts']['nodes'][0]['src'] );
8596
$this->assertEquals( ! empty( $expected->extra['strategy'] ) ? WPEnumType::get_safe_name( $expected->extra['strategy'] ) : null, $actual['data']['registeredScripts']['nodes'][0]['strategy'] );
8697
$this->assertEquals( $expected->ver ?: $wp_scripts->default_version, $actual['data']['registeredScripts']['nodes'][0]['version'] );

tests/wpunit/RegisteredStylesheetConnectionQueriesTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function getQuery() {
3636
dependencies {
3737
handle
3838
}
39+
group
3940
handle
4041
isRtl
4142
media
@@ -76,6 +77,7 @@ public function testForwardPagination() {
7677
$expected = $wp_styles->registered[ $actual['data']['registeredStylesheets']['nodes'][0]['handle'] ];
7778

7879
$this->assertEquals( ! empty( $expected->extra['conditional'] ) ? $expected->extra['conditional'] : null, $actual['data']['registeredStylesheets']['nodes'][0]['conditional'] );
80+
$this->assertEquals( isset( $expected->extra['group'] ) ? $expected->extra['group'] : null, $actual['data']['registeredStylesheets']['nodes'][0]['group'] );
7981
$this->assertEquals( $expected->handle, $actual['data']['registeredStylesheets']['nodes'][0]['handle'] );
8082
$this->assertEquals( ! empty( $expected->extra['rtl'] ), $actual['data']['registeredStylesheets']['nodes'][0]['isRtl'] );
8183
$this->assertEquals( $expected->args ?: 'all', $actual['data']['registeredStylesheets']['nodes'][0]['media'] );

0 commit comments

Comments
 (0)