Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add missing extra fields to EnqueuedAsset, EnqueuedScript and EnqueuedStylesheet #2988

Merged
2 changes: 2 additions & 0 deletions src/Registry/TypeRegistry.php
Expand Up @@ -52,6 +52,7 @@
use WPGraphQL\Type\Enum\PostObjectsConnectionOrderbyEnum;
use WPGraphQL\Type\Enum\PostStatusEnum;
use WPGraphQL\Type\Enum\RelationEnum;
use WPGraphQL\Type\Enum\ScriptLoadingStrategyEnum;
use WPGraphQL\Type\Enum\TaxonomyEnum;
use WPGraphQL\Type\Enum\TaxonomyIdTypeEnum;
use WPGraphQL\Type\Enum\TermNodeIdTypeEnum;
Expand Down Expand Up @@ -354,6 +355,7 @@ public function init_type_registry( self $type_registry ) {
PostObjectsConnectionOrderbyEnum::register_type();
PostStatusEnum::register_type();
RelationEnum::register_type();
ScriptLoadingStrategyEnum::register_type();
TaxonomyEnum::register_type();
TaxonomyIdTypeEnum::register_type();
TermNodeIdTypeEnum::register_type();
Expand Down
39 changes: 39 additions & 0 deletions src/Type/Enum/ScriptLoadingStrategyEnum.php
@@ -0,0 +1,39 @@
<?php
justlevine marked this conversation as resolved.
Show resolved Hide resolved
/**
* Register the ScriptLoadingStrategy Enum Type to the Schema
*
* @package WPGraphQL\Type\Enum
* @since @todo
*/

namespace WPGraphQL\Type\Enum;

/**
* Class ScriptLoadingStrategyEnum
*/
class ScriptLoadingStrategyEnum {

/**
* Register the ScriptLoadingStrategy Enum Type to the Schema
*
* @return void
*/
public static function register_type() {
register_graphql_enum_type(
'ScriptLoadingStrategyEnum',
[
'description' => __( 'The strategy to use when loading the script', 'wp-graphql' ),
'values' => [
'ASYNC' => [
'value' => 'async',
'description' => __( 'Use the script `async` attribute', 'wp-graphql' ),
],
'DEFER' => [
'value' => 'defer',
'description' => __( 'Use the script `defer` attribute', 'wp-graphql' ),
],
],
]
);
}
}
86 changes: 67 additions & 19 deletions src/Type/InterfaceType/EnqueuedAsset.php
Expand Up @@ -41,38 +41,86 @@ public static function register_type( TypeRegistry $type_registry ) {
return ! empty( $type ) ? $type : null;
},
'fields' => [
'args' => [
'type' => 'Boolean',
'description' => __( 'Deprecated', 'wp-graphql' ),
'deprecationReason' => __( 'Use `EnqueuedAsset.media` instead.', 'wp-graphql' ),
],
'after' => [
justlevine marked this conversation as resolved.
Show resolved Hide resolved
'type' => [ 'list_of' => 'String' ],
'description' => __( 'The inline code to be run after the asset is loaded.', 'wp-graphql' ),
'resolve' => static function ( \_WP_Dependency $asset ) {
if ( empty( $asset->extra['after'] ) ) {
return null;
}

$after_scripts = array_map(
static function ( $after ) {
return is_string( $after ) ? $after : null;
},
$asset->extra['after']
);

return array_filter( $after_scripts );
},
],
'before' => [
justlevine marked this conversation as resolved.
Show resolved Hide resolved
'type' => [ 'list_of' => 'String' ],
'description' => __( 'The inline code to be run before the asset is loaded.', 'wp-graphql' ),
'resolve' => static function ( \_WP_Dependency $asset ) {
if ( empty( $asset->extra['before'] ) ) {
return null;
justlevine marked this conversation as resolved.
Show resolved Hide resolved
}

$before_scripts = array_map(
static function ( $before ) {
return is_string( $before ) ? $before : null;
justlevine marked this conversation as resolved.
Show resolved Hide resolved
},
$asset->extra['before']
);

return array_filter( $before_scripts );
justlevine marked this conversation as resolved.
Show resolved Hide resolved
},
],
'conditional' => [
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 3 locations. Consider refactoring.

'type' => 'String',
'description' => __( 'The HTML conditional comment for the enqueued asset. E.g. IE 6, lte IE 7, etc', 'wp-graphql' ),
'resolve' => static function ( \_WP_Dependency $asset ) {
if ( ! isset( $asset->extra['conditional'] ) || ! is_string( $asset->extra['conditional'] ) ) {
return null;
justlevine marked this conversation as resolved.
Show resolved Hide resolved
}

return $asset->extra['conditional'];
justlevine marked this conversation as resolved.
Show resolved Hide resolved
},
],
'dependencies' => [
'type' => [ 'list_of' => 'EnqueuedAsset' ],
'description' => __( 'Dependencies needed to use this asset', 'wp-graphql' ),
],
'id' => [
'type' => [
'non_null' => 'ID',
],
'type' => [ 'non_null' => 'ID' ],
'description' => __( 'The ID of the enqueued asset', 'wp-graphql' ),
],
'handle' => [
'type' => 'String',
'description' => __( 'The handle of the enqueued asset', 'wp-graphql' ),
],
'version' => [
'type' => 'String',
'description' => __( 'The version of the enqueued asset', 'wp-graphql' ),
],
'src' => [
'type' => 'String',
'description' => __( 'The source of the asset', 'wp-graphql' ),
'resolve' => static function ( \_WP_Dependency $stylesheet ) {
return ! empty( $stylesheet->src ) && is_string( $stylesheet->src ) ? $stylesheet->src : null;
justlevine marked this conversation as resolved.
Show resolved Hide resolved
},
],
'dependencies' => [
'type' => [
'list_of' => 'EnqueuedScript',
],
'description' => __( 'Dependencies needed to use this asset', 'wp-graphql' ),
],
'args' => [
'type' => 'Boolean',
'description' => __( '@todo', 'wp-graphql' ),
'version' => [
'type' => 'String',
'description' => __( 'The version of the enqueued asset', 'wp-graphql' ),
],
'extra' => [
'type' => 'String',
'description' => __( 'Extra information needed for the script', 'wp-graphql' ),
'resolve' => static function ( $asset ) {
'type' => 'String',
'description' => __( 'Extra information needed for the script', 'wp-graphql' ),
'deprecationReason' => __( 'Use `EnqueuedScript.extraData` instead.', 'wp-graphql' ),
'resolve' => static function ( $asset ) {
return isset( $asset->extra['data'] ) ? $asset->extra['data'] : null;
},
],
Expand Down
41 changes: 31 additions & 10 deletions src/Type/ObjectType/EnqueuedScript.php
Expand Up @@ -23,21 +23,42 @@ public static function register_type() {
'description' => __( 'Script enqueued by the CMS', 'wp-graphql' ),
'interfaces' => [ 'Node', 'EnqueuedAsset' ],
'fields' => [
'id' => [
'type' => [
'non_null' => 'ID',
],
'resolve' => static function ( $asset ) {
'id' => [
'type' => [ 'non_null' => 'ID' ],
'description' => __( 'The global ID of the enqueued script', 'wp-graphql' ),
'resolve' => static function ( $asset ) {
return isset( $asset->handle ) ? Relay::toGlobalId( 'enqueued_script', $asset->handle ) : null;
},
],
'src' => [
'resolve' => static function ( \_WP_Dependency $script ) {
return ! empty( $script->src ) && is_string( $script->src ) ? $script->src : null;
'dependencies' => [
'type' => [ 'list_of' => 'EnqueuedScript' ],
'description' => __( 'Dependencies needed to use this asset', 'wp-graphql' ),
],
'extraData' => [
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 3 locations. Consider refactoring.

'type' => 'String',
'description' => __( 'Extra data supplied to the enqueued script', 'wp-graphql' ),
'resolve' => static function ( \_WP_Dependency $script ) {
if ( ! isset( $script->extra['data'] ) || ! is_string( $script->extra['data'] ) ) {
return null;
}

return $script->extra['data'];
},
],
'strategy' => [
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 3 locations. Consider refactoring.

'type' => 'ScriptLoadingStrategyEnum',
'description' => __( 'The loading strategy to use on the script tag', 'wp-graphql' ),
'resolve' => static function ( \_WP_Dependency $script ) {
if ( ! isset( $script->extra['strategy'] ) || ! is_string( $script->extra['strategy'] ) ) {
return null;
}

return $script->extra['strategy'];
justlevine marked this conversation as resolved.
Show resolved Hide resolved
},
],
'version' => [
'resolve' => static function ( \_WP_Dependency $script ) {
'version' => [
'description' => __( 'The version of the enqueued script', 'wp-graphql' ),
'resolve' => static function ( \_WP_Dependency $script ) {
global $wp_scripts;

return ! empty( $script->ver ) && is_string( $script->ver ) ? (string) $script->ver : $wp_scripts->default_version;
Expand Down
64 changes: 53 additions & 11 deletions src/Type/ObjectType/EnqueuedStylesheet.php
Expand Up @@ -23,21 +23,63 @@ public static function register_type() {
'description' => __( 'Stylesheet enqueued by the CMS', 'wp-graphql' ),
'interfaces' => [ 'Node', 'EnqueuedAsset' ],
'fields' => [
'id' => [
'type' => [
'non_null' => 'ID',
],
'resolve' => static function ( $asset ) {
return isset( $asset->handle ) ? Relay::toGlobalId( 'enqueued_stylesheet', $asset->handle ) : null;
'id' => [
'type' => [ 'non_null' => 'ID' ],
'description' => __( 'The global ID of the enqueued stylesheet', 'wp-graphql' ),
'resolve' => static function ( $asset ) {
return ! empty( $asset->handle ) ? Relay::toGlobalId( 'enqueued_stylesheet', $asset->handle ) : null;
},
],
'src' => [
'resolve' => static function ( \_WP_Dependency $stylesheet ) {
return ! empty( $stylesheet->src ) && is_string( $stylesheet->src ) ? $stylesheet->src : null;
'dependencies' => [
'type' => [ 'list_of' => 'EnqueuedStylesheet' ],
'description' => __( 'Dependencies needed to use this asset', 'wp-graphql' ),
],
'isRtl' => [
'type' => 'Boolean',
'description' => __( 'Whether the enqueued style is RTL or not', 'wp-graphql' ),
'resolve' => static function ( \_WP_Dependency $stylesheet ) {
return ! empty( $stylesheet->extra['rtl'] );
},
],
'media' => [
'type' => 'String',
'description' => __( 'The media attribute to use for the link', 'wp-graphql' ),
'resolve' => static function ( \_WP_Dependency $stylesheet ) {
return ! empty( $stylesheet->args ) && is_string( $stylesheet->args ) ? esc_attr( $stylesheet->args ) : 'all';
},
],
'path' => [
'type' => 'String',
'description' => __( 'The absolute path to the enqueued style. Set when the stylesheet is meant to load inline.', 'wp-graphql' ),
'resolve' => static function ( \_WP_Dependency $stylesheet ) {
return ! empty( $stylesheet->extra['path'] ) && is_string( $stylesheet->extra['path'] ) ? $stylesheet->extra['path'] : null;
},
],
'rel' => [
'type' => 'String',
'description' => __( 'The `rel` attribute to use for the link', 'wp-graphql' ),
'resolve' => static function ( \_WP_Dependency $stylesheet ) {
return ! empty( $stylesheet->extra['alt'] ) ? 'alternate stylesheet' : 'stylesheet';
justlevine marked this conversation as resolved.
Show resolved Hide resolved
},
],
'suffix' => [
'type' => 'String',
'description' => __( 'Optional suffix, used in combination with RTL', 'wp-graphql' ),
'resolve' => static function ( \_WP_Dependency $stylesheet ) {
return ! empty( $stylesheet->extra['suffix'] ) && is_string( $stylesheet->extra['suffix'] ) ? $stylesheet->extra['suffix'] : null;
justlevine marked this conversation as resolved.
Show resolved Hide resolved
},
],
'title' => [
'type' => 'String',
'description' => __( 'The title of the enqueued style. Used for preferred/alternate stylesheets.', 'wp-graphql' ),
'resolve' => static function ( \_WP_Dependency $stylesheet ) {
return ! empty( $stylesheet->extra['title'] ) && is_string( $stylesheet->extra['title'] ) ? $stylesheet->extra['title'] : null;
justlevine marked this conversation as resolved.
Show resolved Hide resolved
},
],
'version' => [
'resolve' => static function ( \_WP_Dependency $stylesheet ) {
'version' => [
'type' => 'String',
'description' => __( 'The version of the enqueued style', 'wp-graphql' ),
'resolve' => static function ( \_WP_Dependency $stylesheet ) {
global $wp_styles;

return ! empty( $stylesheet->ver ) && is_string( $stylesheet->ver ) ? $stylesheet->ver : $wp_styles->default_version;
Expand Down
20 changes: 18 additions & 2 deletions tests/wpunit/RegisteredScriptConnectionQueriesTest.php
@@ -1,5 +1,7 @@
<?php

use WPGraphQL\Type\WPEnumType;

class RegisteredScriptConnectionQueriesTest extends \Tests\WPGraphQL\TestCase\WPGraphQLTestCase {

private $admin;
Expand Down Expand Up @@ -31,10 +33,17 @@ public function getQuery() {
}
}
nodes {
extra
after
before
conditional
dependencies {
handle
}
extraData
handle
id
src
strategy
version
}
}
Expand Down Expand Up @@ -64,9 +73,16 @@ public function testForwardPagination() {
global $wp_scripts;
$expected = $wp_scripts->registered[ $actual['data']['registeredScripts']['nodes'][0]['handle'] ];

$this->assertEquals( $expected->extra['data'], $actual['data']['registeredScripts']['nodes'][0]['extra'] );
$expected_after = ! empty( $expected->extra['after'] ) ? ( array_filter( $expected->extra['after'], 'is_string' ) ?: null ) : null;
$expected_before = ! empty( $expected->extra['before'] ) ? ( array_filter( $expected->extra['before'], 'is_string' ) ?: null ) : null;

$this->assertEquals( $expected_after, $actual['data']['registeredScripts']['nodes'][0]['after'] );
$this->assertEquals( $expected_before, $actual['data']['registeredScripts']['nodes'][0]['before'] );
$this->assertEquals( ! empty( $expected->extra['conditional'] ) ? $expected->extra['conditional'] : null, $actual['data']['registeredScripts']['nodes'][0]['conditional'] );
$this->assertEquals( $expected->handle, $actual['data']['registeredScripts']['nodes'][0]['handle'] );
$this->assertEquals( ! empty( $expected->extra['data'] ) ? $expected->extra['data'] : null, $actual['data']['registeredScripts']['nodes'][0]['extraData'] );
$this->assertEquals( $expected->src, $actual['data']['registeredScripts']['nodes'][0]['src'] );
$this->assertEquals( ! empty( $expected->extra['strategy'] ) ? WPEnumType::get_safe_name( $expected->extra['strategy'] ) : null, $actual['data']['registeredScripts']['nodes'][0]['strategy'] );
$this->assertEquals( $expected->ver ?: $wp_scripts->default_version, $actual['data']['registeredScripts']['nodes'][0]['version'] );

// Store for use by $expected.
Expand Down
20 changes: 17 additions & 3 deletions tests/wpunit/RegisteredStylesheetConnectionQueriesTest.php
Expand Up @@ -32,10 +32,18 @@ public function getQuery() {
}
}
nodes {
extra
conditional
dependencies {
handle
}
handle
id
isRtl
media
path
rel
src
suffix
title
version
}
}
Expand Down Expand Up @@ -67,9 +75,15 @@ public function testForwardPagination() {

$expected = $wp_styles->registered[ $actual['data']['registeredStylesheets']['nodes'][0]['handle'] ];

$this->assertEquals( $expected->extra['data'] ?? null, $actual['data']['registeredStylesheets']['nodes'][0]['extra'] );
$this->assertEquals( ! empty( $expected->extra['conditional'] ) ? $expected->extra['conditional'] : null, $actual['data']['registeredStylesheets']['nodes'][0]['conditional'] );
$this->assertEquals( $expected->handle, $actual['data']['registeredStylesheets']['nodes'][0]['handle'] );
$this->assertEquals( ! empty( $expected->extra['rtl'] ), $actual['data']['registeredStylesheets']['nodes'][0]['isRtl'] );
$this->assertEquals( $expected->args ?: 'all', $actual['data']['registeredStylesheets']['nodes'][0]['media'] );
$this->assertEquals( ! empty( $expected->extra['path'] ) ? $expected->extra['path'] : null, $actual['data']['registeredStylesheets']['nodes'][0]['path'] );
$this->assertEquals( ! empty( $expected->extra['alt'] ) ? 'alternate stylesheet' : 'stylesheet', $actual['data']['registeredStylesheets']['nodes'][0]['rel'] );
$this->assertEquals( is_string( $expected->src ) ? $expected->src : null, $actual['data']['registeredStylesheets']['nodes'][0]['src'] );
$this->assertEquals( ! empty( $expected->extra['suffix'] ) ? $expected->extra['suffix'] : null, $actual['data']['registeredStylesheets']['nodes'][0]['suffix'] );
$this->assertEquals( ! empty( $expected->extra['title'] ) ? $expected->extra['title'] : null, $actual['data']['registeredStylesheets']['nodes'][0]['title'] );
$this->assertEquals( $expected->ver ?: $wp_styles->default_version, $actual['data']['registeredStylesheets']['nodes'][0]['version'] );

// Store for use by $expected.
Expand Down