-
Notifications
You must be signed in to change notification settings - Fork 12
/
PostTypeBlockInterface.php
79 lines (72 loc) · 2.08 KB
/
PostTypeBlockInterface.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/**
* The Post Type Block Interface
*
* @package WPGraphQL\ContentBlocks\Type\InterfaceType
*/
namespace WPGraphQL\ContentBlocks\Type\InterfaceType;
use WPGraphQL\ContentBlocks\Data\ContentBlocksResolver;
use WPGraphQL\ContentBlocks\Utilities\WPGraphQLHelpers;
/**
* Class PostTypeBlockInterface
*/
final class PostTypeBlockInterface {
/**
* Registers the types to WPGraphQL.
*
* @param string $post_type The post type.
* @param string[] $block_names The list of allowed block names.
*/
public static function register_type( string $post_type, $block_names ): void {
register_graphql_interface_type(
ucfirst( $post_type ) . 'EditorBlock',
array(
'interfaces' => array( 'EditorBlock' ),
'fields' => array(
'name' => array(
'type' => 'String',
),
),
'resolveType' => function ( $block ) {
if ( empty( $block['blockName'] ) ) {
$block['blockName'] = 'core/freeform';
}
$type_name = lcfirst( ucwords( $block['blockName'], '/' ) );
return WPGraphQLHelpers::format_type_name( $type_name );
},
)
);
register_graphql_interface_type(
'NodeWith' . ucfirst( $post_type ) . 'EditorBlocks',
array(
'description' => sprintf(
// translators: %s is the post type.
__( 'Node that has %s content blocks associated with it', 'wp-graphql-content-blocks' ),
$post_type
),
'eagerlyLoadType' => true,
'interfaces' => array( 'NodeWithEditorBlocks' ),
'fields' => array(
'editorBlocks' => array(
'type' => array(
'list_of' => ucfirst( $post_type ) . 'EditorBlock',
),
'args' => array(
'flat' => array(
'type' => 'Boolean',
),
),
'description' => sprintf(
// translators: %s is the post type.
__( 'List of %s editor blocks', 'wp-graphql-content-blocks' ),
$post_type
),
'resolve' => function ( $node, $args ) use ( $block_names ) {
return ContentBlocksResolver::resolve_content_blocks( $node, $args, $block_names );
},
),
),
)
);
}
}