Created
September 1, 2020 16:33
-
-
Save jasonbahl/ad4eb8875624e984c1346aa99e1b380e to your computer and use it in GitHub Desktop.
This was some initial work done to test Gutenberg support with WordPress 5.5 server registry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$blocks = \WP_Block_Type_Registry::get_instance(); | |
$registered_blocks = $blocks->get_all_registered(); | |
if ( ! empty( $registered_blocks ) ) { | |
register_graphql_interface_type( 'ContentEditorBlock', [ | |
'description' => __( 'Blocks used by the content editor', 'wp-graphql' ), | |
'resolveType' => function( $block ) { | |
return isset( $block['name'] ) ? $this->get_type( graphql_format_type_name( $block['name'] ) ) : null; | |
}, | |
'fields' => [ | |
'name' => [ | |
'type' => 'String', | |
'description' => __( 'Name of the content editor block', 'wp-graphql' ), | |
], | |
'title' => [ | |
'type' => 'String', | |
'description' => __( 'The title of the content editor block', 'wp-graphql' ), | |
], | |
'category' => [ | |
'type' => 'String', | |
'description' => __( 'The category the content editor block belongs to', 'wp-graphql' ), | |
], | |
'parent' => [ | |
'type' => 'ContentEditorBlock', | |
'description' => __( 'The parent content editor block that the content editor block can be used within', 'wp-graphql' ), | |
], | |
'icon' => [ | |
'type' => 'String', | |
'description' => __( 'The icon that represents the content editor block', 'wp-graphql' ), | |
], | |
'description' => [ | |
'type' => 'String', | |
'description' => __( 'The description of the content editor block', 'wp-graphql' ), | |
], | |
'keywords' => [ | |
'type' => [ 'list_of' => 'String' ], | |
'description' => __( 'Keywords describing the intended use of the content editor block', 'wp-graphql' ), | |
], | |
'textdomain' => [ | |
'type' => 'String', | |
'description' => __( 'The textdomain for the content editor block. Used for internationalization.', 'wp-graphql' ), | |
], | |
'styles' => [ | |
'type' => [ 'list_of' => 'String' ], | |
'description' => __( 'Stylesheets associated with the content editor block', 'wp-graphql' ), | |
], | |
'supports' => [ | |
'type' => [ 'list_of' => 'String' ], | |
'description' => __( 'List of features the content editor block supports', 'wp-graphql' ), | |
], | |
'example' => [ | |
'type' => 'String', | |
'description' => __( 'Example use of the content editor block', 'wp-graphql' ), | |
], | |
'rendered' => [ | |
'type' => 'String', | |
'description' => __( 'Rendered instance of the content editor block', 'wp-graphql' ), | |
], | |
'innerHTML' => [ | |
'type' => 'String', | |
'description' => __( 'The inner HTML the content editor block renders', 'wp-graphql' ), | |
], | |
'innerBlocks' => [ | |
'type' => [ 'list_of' => 'ContentEditorBlock' ], | |
'description' => __( 'Inner blocks nested within the content editor block', 'wp-graphql' ), | |
'resolve' => function( $block ) { | |
$prepared_blocks = []; | |
$blocks = isset( $block['innerBlocks'] ) ? $block['innerBlocks'] : null; | |
if ( empty( $blocks ) ) { | |
return null; | |
} | |
foreach ( $blocks as $block ) { | |
if ( ! empty( $block['blockName'] ) ) { | |
$prepared_blocks[] = $this->prepare_content_block( $block ); | |
} | |
} | |
return $prepared_blocks; | |
} | |
], | |
'innerContent' => [ | |
'type' => [ 'list_of' => 'String' ], | |
'description' => __( 'List of inner content strings for the content editor block', 'wp-graphql' ), | |
], | |
] | |
]); | |
foreach ( $registered_blocks as $registered_block ) { | |
$type_name = graphql_format_type_name( $registered_block->name ); | |
register_graphql_object_type( $type_name, [ | |
'interfaces' => [ 'ContentEditorBlock' ], | |
'fields' => [ | |
'name' => [ | |
'type' => 'String', | |
'description' => __( 'Name of the content block', 'wp-graphql' ), | |
], | |
], | |
] ); | |
} | |
register_graphql_field( 'ContentNode', 'contentBlocks', [ | |
'type' => [ 'list_of' => 'ContentEditorBlock' ], | |
'description' => __( 'Content blocks saved to the content node', 'wp-graphql' ), | |
'resolve' => function( Post $post, $args, $context, $info ) use ( $registered_blocks ) { | |
$content = $post->contentRaw; | |
$blocks = $content ? parse_blocks( $content ) : null; | |
$prepared_blocks = []; | |
foreach ( $blocks as $block ) { | |
if ( ! empty( $block['blockName'] ) ) { | |
$prepared_blocks[] = $this->prepare_content_block( $block ); | |
} | |
} | |
return $prepared_blocks; | |
} | |
] ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment