Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 54 additions & 41 deletions includes/Blocks/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace WPGraphQL\ContentBlocks\Blocks;

use WPGraphQL\ContentBlocks\Data\BlockAttributeResolver;
use WPGraphQL\ContentBlocks\GraphQL\WPGraphQLRegisterConfig;
use WPGraphQL\ContentBlocks\Registry\Registry;
use WPGraphQL\ContentBlocks\Type\Scalar\Scalar;
use WPGraphQL\ContentBlocks\Utilities\WPGraphQLHelpers;
Expand Down Expand Up @@ -121,30 +122,36 @@ private function register_block_attributes_as_fields(): void {
$block_attribute_type_name = $this->type_name . 'Attributes';
register_graphql_object_type(
$block_attribute_type_name,
[
'description' => sprintf(
// @TODO - Remove when WPGraphQL min version is 2.3.0
WPGraphQLRegisterConfig::resolve_graphql_config(
[
'description' => sprintf(
// translators: %s is the block type name.
__( 'Attributes of the %s Block Type', 'wp-graphql-content-blocks' ),
$this->type_name
),
'interfaces' => $this->get_block_attributes_interfaces(),
'fields' => $block_attribute_fields,
]
__( 'Attributes of the %s Block Type', 'wp-graphql-content-blocks' ),
$this->type_name
),
'interfaces' => $this->get_block_attributes_interfaces(),
'fields' => $block_attribute_fields,
]
)
);
register_graphql_field(
$this->type_name,
'attributes',
[
'type' => $block_attribute_type_name,
'description' => sprintf(
// translators: %s is the block type name.
__( 'Attributes of the %s Block Type', 'wp-graphql-content-blocks' ),
$this->type_name
),
'resolve' => static function ( $block ) {
return $block;
},
]
// @TODO - Remove when WPGraphQL min version is 2.3.0
WPGraphQLRegisterConfig::resolve_graphql_config(
[
'type' => $block_attribute_type_name,
'description' => sprintf(
// translators: %s is the block type name.
__( 'Attributes of the %s Block Type', 'wp-graphql-content-blocks' ),
$this->type_name
),
'resolve' => static function ( $block ) {
return $block;
},
]
)
);
}

Expand Down Expand Up @@ -295,15 +302,18 @@ private function register_inner_object_type( string $name, array $config, string

register_graphql_object_type(
$type,
[
'fields' => $fields,
'description' => sprintf(
// translators: %1$s is the attribute name, %2$s is the block attributes field.
__( 'The "%1$s" field on the "%2$s" block attribute field', 'wp-graphql-content-blocks' ),
$type,
$prefix
),
]
// @TODO - Remove when WPGraphQL min version is 2.3.0
WPGraphQLRegisterConfig::resolve_graphql_config(
[
'fields' => $fields,
'description' => sprintf(
// translators: %1$s is the attribute name, %2$s is the block attributes field.
__( 'The "%1$s" field on the "%2$s" block attribute field', 'wp-graphql-content-blocks' ),
$type,
$prefix
),
]
)
);

return $type;
Expand Down Expand Up @@ -402,20 +412,23 @@ private function normalize_attribute_value( $value, $type ) {
private function register_type(): void {
register_graphql_object_type(
$this->type_name,
[
'description' => __( 'A block used for editing the site', 'wp-graphql-content-blocks' ),
'interfaces' => $this->get_block_interfaces(),
'eagerlyLoadType' => true,
'fields' => [
'name' => [
'type' => 'String',
'description' => __( 'The name of the block', 'wp-graphql-content-blocks' ),
'resolve' => static function ( $block ) {
return isset( $block['blockName'] ) ? (string) $block['blockName'] : null;
},
// @TODO - Remove when WPGraphQL min version is 2.3.0
WPGraphQLRegisterConfig::resolve_graphql_config(
[
'description' => __( 'A block used for editing the site', 'wp-graphql-content-blocks' ),
'interfaces' => $this->get_block_interfaces(),
'eagerlyLoadType' => true,
'fields' => [
'name' => [
'type' => 'String',
'description' => __( 'The name of the block', 'wp-graphql-content-blocks' ),
'resolve' => static function ( $block ) {
return isset( $block['blockName'] ) ? (string) $block['blockName'] : null;
},
],
],
],
]
]
)
);
}

Expand Down
42 changes: 23 additions & 19 deletions includes/Blocks/CoreImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace WPGraphQL\ContentBlocks\Blocks;

use WPGraphQL\ContentBlocks\GraphQL\WPGraphQLRegisterConfig;
use WPGraphQL\ContentBlocks\Registry\Registry;
use WP_Block_Type;

Expand Down Expand Up @@ -47,27 +48,30 @@ public function __construct( WP_Block_Type $block, Registry $block_registry ) {
register_graphql_field(
$this->type_name,
'mediaDetails',
[
'type' => 'MediaDetails',
'description' => sprintf(
// translators: %s is the block type name.
__( 'Media Details of the %s Block Type', 'wp-graphql-content-blocks' ),
$this->type_name
),
'resolve' => static function ( $block ) {
$attrs = $block['attrs'];
$id = $attrs['id'] ?? null;
if ( $id ) {
$media_details = wp_get_attachment_metadata( $id );
if ( ! empty( $media_details ) ) {
$media_details['ID'] = $id;
// @TODO - Remove when WPGraphQL min version is 2.3.0
WPGraphQLRegisterConfig::resolve_graphql_config(
[
'type' => 'MediaDetails',
'description' => sprintf(
// translators: %s is the block type name.
__( 'Media Details of the %s Block Type', 'wp-graphql-content-blocks' ),
$this->type_name
),
'resolve' => static function ( $block ) {
$attrs = $block['attrs'];
$id = $attrs['id'] ?? null;
if ( $id ) {
$media_details = wp_get_attachment_metadata( $id );
if ( ! empty( $media_details ) ) {
$media_details['ID'] = $id;

return $media_details;
return $media_details;
}
}
}
return null;
},
]
return null;
},
]
)
);
}
}
101 changes: 56 additions & 45 deletions includes/Blocks/CorePostTerms.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace WPGraphQL\ContentBlocks\Blocks;

use WPGraphQL\AppContext;
use WPGraphQL\ContentBlocks\GraphQL\WPGraphQLRegisterConfig;
use WPGraphQL\ContentBlocks\Registry\Registry;
use WPGraphQL\Data\Connection\TaxonomyConnectionResolver;
use WPGraphQL\Data\Connection\TermObjectConnectionResolver;
Expand All @@ -33,18 +34,22 @@ public function __construct( WP_Block_Type $block, Registry $block_registry ) {
private function register_fields(): void {
register_graphql_fields(
$this->type_name,
[
'prefix' => [
'type' => 'String',
'description' => __( 'Prefix to display before the post terms', 'wp-graphql-content-blocks' ),
'resolve' => static fn ( $block ) => isset( $block['attrs']['prefix'] ) ? (string) $block['attrs']['prefix'] : null,
],
'suffix' => [
'type' => 'String',
'description' => __( 'Suffix to display after the post terms', 'wp-graphql-content-blocks' ),
'resolve' => static fn ( $block ) => isset( $block['attrs']['suffix'] ) ? (string) $block['attrs']['suffix'] : null,
],
]
// @TODO - Remove when WPGraphQL min version is 2.3.0
// @phpstan-ignore-next-line
WPGraphQLRegisterConfig::resolve_graphql_config(
[
'prefix' => [
'type' => 'String',
'description' => __( 'Prefix to display before the post terms', 'wp-graphql-content-blocks' ),
'resolve' => static fn ( $block ) => isset( $block['attrs']['prefix'] ) ? (string) $block['attrs']['prefix'] : null,
],
'suffix' => [
'type' => 'String',
'description' => __( 'Suffix to display after the post terms', 'wp-graphql-content-blocks' ),
'resolve' => static fn ( $block ) => isset( $block['attrs']['suffix'] ) ? (string) $block['attrs']['suffix'] : null,
],
]
)
);
}

Expand All @@ -57,48 +62,54 @@ private function register_fields(): void {
protected function register_connections() {
// Register connection to terms.
register_graphql_connection(
[
'fromType' => $this->type_name,
'toType' => 'TermNode',
'fromFieldName' => 'terms',
'resolve' => static function ( $block, array $args, AppContext $context, $info ) {
$taxonomy = $block['attrs']['term'] ?? null;
if ( empty( $taxonomy ) ) {
return null;
}
// @TODO - Remove when WPGraphQL min version is 2.3.0
WPGraphQLRegisterConfig::resolve_graphql_config(
[
'fromType' => $this->type_name,
'toType' => 'TermNode',
'fromFieldName' => 'terms',
'resolve' => static function ( $block, array $args, AppContext $context, $info ) {
$taxonomy = $block['attrs']['term'] ?? null;
if ( empty( $taxonomy ) ) {
return null;
}

$post_id = get_the_ID();
if ( ! $post_id ) {
return null;
}
$post_id = get_the_ID();
if ( ! $post_id ) {
return null;
}

$args['where']['objectIds'] = $post_id;
$resolver = new TermObjectConnectionResolver( $block, $args, $context, $info, $taxonomy );
$args['where']['objectIds'] = $post_id;
$resolver = new TermObjectConnectionResolver( $block, $args, $context, $info, $taxonomy );

return $resolver->get_connection();
},
]
return $resolver->get_connection();
},
]
)
);

// Register connection to the taxonomy.
register_graphql_connection(
[
'fromType' => $this->type_name,
'toType' => 'Taxonomy',
'fromFieldName' => 'taxonomy',
'oneToOne' => true,
'resolve' => static function ( $block, array $args, AppContext $context, $info ) {
$taxonomy = $block['attrs']['term'] ?? null;
if ( empty( $taxonomy ) ) {
return null;
}
// @TODO - Remove when WPGraphQL min version is 2.3.0
WPGraphQLRegisterConfig::resolve_graphql_config(
[
'fromType' => $this->type_name,
'toType' => 'Taxonomy',
'fromFieldName' => 'taxonomy',
'oneToOne' => true,
'resolve' => static function ( $block, array $args, AppContext $context, $info ) {
$taxonomy = $block['attrs']['term'] ?? null;
if ( empty( $taxonomy ) ) {
return null;
}

$resolver = new TaxonomyConnectionResolver( $block, $args, $context, $info );
$resolver->set_query_arg( 'name', $taxonomy );
$resolver = new TaxonomyConnectionResolver( $block, $args, $context, $info );
$resolver->set_query_arg( 'name', $taxonomy );

return $resolver->one_to_one()->get_connection();
},
]
return $resolver->one_to_one()->get_connection();
},
]
)
);
}
}
Loading
Loading