-
Notifications
You must be signed in to change notification settings - Fork 12
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
Implements support for block type filter #47
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a5259aa
WIP
theodesp 3d73ca8
[MERL-783]Code: First draft of schema per post type
theodesp 888d108
MERL-783: Filter out allowed block types in response
theodesp e2a822f
Fix: Restore flat=true
theodesp cb1f0a2
Refactor: Fix test class name
theodesp f4c242a
Test: Unit tests WIP
theodesp b510e21
Test: Unit tests for get_allowed_block_types
theodesp 9619fb3
Test: Unit tests for ContentBlocksResolver
theodesp 6915972
Add warning for composer deps
blakewilson 9b1e93f
Create changeset
blakewilson aa25bec
Chore: Add Changeset
theodesp File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@wpengine/wp-graphql-content-blocks": patch | ||
--- | ||
|
||
Warn the user if they downloaded the source code .zip instead of the production ready .zip file |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@wpengine/wp-graphql-content-blocks": minor | ||
--- | ||
|
||
Add support for querying blocks per post type |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,9 @@ | |
use WPGraphQL\ContentBlocks\Interfaces\OnInit; | ||
use WPGraphQL\ContentBlocks\Type\Scalar\Scalar; | ||
use WPGraphQL\ContentBlocks\Type\InterfaceType\EditorBlockInterface; | ||
use WPGraphQL\ContentBlocks\Type\InterfaceType\PostTypeBlockInterface; | ||
use WPGraphQL\ContentBlocks\Type\ObjectType\UnknownBlock; | ||
use WPGraphQL\ContentBlocks\Utilities\WPHelpers; | ||
use WPGraphQL\Registry\TypeRegistry; | ||
use WPGraphQL\Utils\Utils; | ||
|
||
|
@@ -18,7 +21,6 @@ | |
*/ | ||
final class Registry implements OnInit { | ||
|
||
|
||
/** | ||
* @var TypeRegistry | ||
*/ | ||
|
@@ -51,56 +53,68 @@ public function __construct( TypeRegistry $type_registry, $block_type_registry ) | |
* @throws Exception | ||
*/ | ||
public function OnInit() { | ||
EditorBlockInterface::register_type( $this->type_registry ); | ||
( new Scalar() )->OnInit(); | ||
$this->pass_blocks_to_context(); | ||
$this->register_interface_types(); | ||
$this->register_scalar_types(); | ||
$this->register_block_types(); | ||
$this->register_unknown_block(); | ||
$this->add_block_fields_to_schema(); | ||
UnknownBlock::register_type(); | ||
} | ||
|
||
/** | ||
* Registers the UnknownBlock in the instance a block is not defined | ||
* in the registry. | ||
* | ||
*/ | ||
public function register_unknown_block() { | ||
register_graphql_object_type( | ||
'UnknownBlock', | ||
array( | ||
'description' => __( 'A block used for resolving blocks not found in the WordPress registry', 'wp-graphql-content-blocks' ), | ||
'interfaces' => array( 'EditorBlock' ), | ||
'eagerlyLoadType' => true, | ||
'fields' => array( | ||
'name' => array( | ||
'type' => 'String', | ||
'description' => __( 'The name of the block', 'wp-graphql-content-blocks' ), | ||
'resolve' => function () { | ||
return 'UnknownBlock'; | ||
} | ||
), | ||
), | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* This adds the WP Block Registry to AppContext | ||
* Register Interface types to the GraphQL Schema | ||
* | ||
* @return void | ||
*/ | ||
public function pass_blocks_to_context() { | ||
add_filter( 'graphql_app_context_config', array( $this, 'load_registered_editor_blocks' ) ); | ||
protected function register_interface_types() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All logic is contained in this method. |
||
// First register the NodeWithEditorBlocks interface by default | ||
EditorBlockInterface::register_type( $this->type_registry ); | ||
|
||
// Then try to register both NodeWithEditorBlocks and NodeWith[PostType]Blocks per post type | ||
$supported_post_types = WPHelpers::get_supported_post_types(); | ||
if ( empty( $supported_post_types ) ) { | ||
return; | ||
} | ||
register_graphql_interfaces_to_types( array( 'NodeWithEditorBlocks' ), $supported_post_types ); | ||
$post_id = -1; | ||
// For each Post type | ||
foreach ( $supported_post_types as $post_type ) { | ||
// Normalize the post type name | ||
$type_name = strtolower( $post_type ); | ||
|
||
// retrieve a block_editor_context for the current post type | ||
$block_editor_context = WPHelpers::get_block_editor_context( $type_name, $post_id-- ); | ||
|
||
// Fetch the list of allowed blocks for the current post type | ||
$supported_blocks_for_post_type = get_allowed_block_types( $block_editor_context ); | ||
|
||
// If there is a list of supported blocks for current post type | ||
if ( is_array( $supported_blocks_for_post_type ) ) { | ||
// Register an [PostType]Block type for the blocks using that post type | ||
PostTypeBlockInterface::register_type( $type_name, $supported_blocks_for_post_type, $this->type_registry ); | ||
|
||
// Normalize the list of supported block names | ||
$block_names = array_map( | ||
function( $supported_block ) { | ||
$block_name = preg_replace( '/\//', '', lcfirst( ucwords( $supported_block, '/' ) ) ); | ||
return \WPGraphQL\Utils\Utils::format_type_name( $block_name ); | ||
}, | ||
$supported_blocks_for_post_type | ||
); | ||
// Register [PostType]Block type to allowed block names | ||
register_graphql_interfaces_to_types( array( $type_name . 'Block' ), $block_names ); | ||
|
||
// Register the `NodeWith[PostType]Blocks` Interface to the post type | ||
register_graphql_interfaces_to_types( array( 'NodeWith' . $post_type . 'Blocks' ), array( $type_name ) ); | ||
} | ||
}//end foreach | ||
} | ||
|
||
/** | ||
* Loads registered_blocks into the app context config | ||
* Register Scalar types to the GraphQL Schema | ||
* | ||
* @return object | ||
* @return void | ||
*/ | ||
public function load_registered_editor_blocks( $config ) { | ||
$config['registered_editor_blocks'] = $this->registered_blocks; | ||
return $config; | ||
protected function register_scalar_types() { | ||
( new Scalar() )->OnInit(); | ||
} | ||
|
||
/** | ||
|
@@ -109,7 +123,7 @@ public function load_registered_editor_blocks( $config ) { | |
* @return void | ||
*/ | ||
protected function register_block_types() { | ||
$this->registered_blocks = $this->block_type_registry->get_all_registered(); | ||
$this->registered_blocks = $this->block_type_registry->get_all_registered(); | ||
|
||
if ( empty( $this->registered_blocks ) || ! is_array( $this->registered_blocks ) ) { | ||
return; | ||
|
@@ -145,59 +159,4 @@ protected function register_block_type( WP_Block_Type $block ) { | |
new Block( $block, $this ); | ||
} | ||
} | ||
|
||
/** | ||
* Adds Block Fields to the WPGraphQL Schema | ||
* | ||
* @return void | ||
*/ | ||
public function add_block_fields_to_schema() { | ||
$supported_post_types = $this->get_supported_post_types(); | ||
// If there are no supported post types, early return | ||
if ( empty( $supported_post_types ) ) { | ||
return; | ||
} | ||
|
||
// Register the `NodeWithEditorBlocks` Interface to the supported post types | ||
register_graphql_interfaces_to_types( array( 'NodeWithEditorBlocks' ), $supported_post_types ); | ||
} | ||
|
||
/** | ||
* Gets Block Editor supported post types | ||
* | ||
* @return array<string> | ||
*/ | ||
public function get_supported_post_types() { | ||
$supported_post_types = array(); | ||
// Get Post Types that are set to Show in GraphQL and Show in REST | ||
// If it doesn't show in REST, it's not block-editor enabled | ||
$block_editor_post_types = get_post_types( | ||
array( | ||
'show_in_graphql' => true, | ||
'show_in_rest' => true, | ||
), | ||
'objects' | ||
); | ||
|
||
if ( empty( $block_editor_post_types ) || ! is_array( $block_editor_post_types ) ) { | ||
return; | ||
} | ||
|
||
// Iterate over the post types | ||
foreach ( $block_editor_post_types as $block_editor_post_type ) { | ||
|
||
// If the post type doesn't support the editor, it's not block-editor enabled | ||
if ( ! post_type_supports( $block_editor_post_type->name, 'editor' ) ) { | ||
continue; | ||
} | ||
|
||
if ( ! isset( $block_editor_post_type->graphql_single_name ) ) { | ||
continue; | ||
} | ||
|
||
$supported_post_types[] = Utils::format_type_name( $block_editor_post_type->graphql_single_name ); | ||
} | ||
|
||
return $supported_post_types; | ||
} | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what happened here, but theses changes should already be in
main
. Maybe a rebase is needed?