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!: implement block model #31

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
Tests for WordPress version compatibility.
https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties
-->
<config name="minimum_supported_wp_version" value="5.7"/>
<config name="minimum_wp_version" value="5.7"/>

<!-- Rules: WPGraphQL Coding Standards -->
<!-- https://github.com/AxeWP/WPGraphQL-Coding-Standards/WPGraphQL/ruleset.xml -->
Expand Down
36 changes: 36 additions & 0 deletions includes/BlockSupports/AbstractBlockSupport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Handles mapping Block Supports to the GraphQL Schema.
*
* @package WPGraphQL\ContentBlocks\BlockSupports
*
* @since @next-version
*/

namespace WPGraphQL\ContentBlocks\BlockSupports;

/**
* Class AbstractBlockSupport
*/
abstract class AbstractBlockSupport {
/**
* Registers the types to WPGraphQL.
*/
abstract public static function register(): void;

/**
* Checks whether the Block Supports is enabled for the block type.
*
* @param \WP_Block_Type $block_type The block type.
*/
abstract public static function has_block_support( \WP_Block_Type $block_type ): bool;

/**
* Get the attribute GraphQL interfaces for the block type.
*
* @param \WP_Block_Type $block_type The block type.
*
* @return string[]
*/
abstract public static function get_attributes_interfaces( \WP_Block_Type $block_type ): array;
}
53 changes: 53 additions & 0 deletions includes/BlockSupports/Align.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* The BlockWithAlignSupportAttributes Interface Type.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#align
*
* @package WPGraphQL\ContentBlocks\BlockSupports
*/

namespace WPGraphQL\ContentBlocks\BlockSupports;

/**
* Class Align
*/
final class Align extends AbstractBlockSupport {
/**
* Registers the types to WPGraphQL.
*/
public static function register(): void {
register_graphql_interface_type(
'BlockWithAlignSupportAttributes',
[
'description' => __( 'Attributes for a block with Align support', 'wp-graphql-content-blocks' ),
'eagerlyLoadType' => true,
'interfaces' => [ 'EditorBlock' ],
'fields' => [
'align' => [
'type' => 'String',
'description' => __( 'The align attribute for the block.', 'wp-graphql-content-blocks' ),
],
],
]
);
}

/**
* {@inheritDoc}
*/
public static function has_block_support( \WP_Block_Type $block_type ): bool {
return block_has_support( $block_type, [ 'align' ], false );
}

/**
* {@inheritDoc}
*/
public static function get_attributes_interfaces( \WP_Block_Type $block_type ): array {
if ( ! self::has_block_support( $block_type ) ) {
return [];
}

return [ 'BlockWithAlignSupportAttributes' ];
}
}
64 changes: 64 additions & 0 deletions includes/BlockSupports/Anchor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* The BlockWithAnchorSupportAttributes Interface Type.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#anchor
*
* @package WPGraphQL\ContentBlocks\BlockSupports
*/

namespace WPGraphQL\ContentBlocks\BlockSupports;

use WPGraphQL\ContentBlocks\Utilities\DOMHelpers;

/**
* Class Anchor
*/
final class Anchor extends AbstractBlockSupport {
/**
* {@inheritDoc}
*/
public static function register(): void {
register_graphql_interface_type(
'BlockWithAnchorSupportAttributes',
[
'description' => __( 'Attributes for a Block with Anchor support.', 'wp-graphql-content-blocks' ),
'eagerlyLoadType' => true,
'interfaces' => [ 'EditorBlock' ],
'fields' => [
'anchor' => [
'type' => 'String',
'description' => __( 'The anchor attribute for the block.', 'wp-graphql-content-blocks' ),
'resolve' => static function ( $block ) {
$rendered_block = wp_unslash( $block->renderedHtml );

if ( empty( $rendered_block ) ) {
return null;
}

return DOMHelpers::parse_first_node_attribute( $rendered_block, 'id' );
},
],
],
]
);
}

/**
* {@inheritDoc}
*/
public static function has_block_support( \WP_Block_Type $block_type ): bool {
return block_has_support( $block_type, [ 'anchor' ], false );
}

/**
* {@inheritDoc}
*/
public static function get_attributes_interfaces( \WP_Block_Type $block_type ): array {
if ( ! self::has_block_support( $block_type ) ) {
return [];
}

return [ 'BlockWithAnchorSupportAttributes' ];
}
}
61 changes: 61 additions & 0 deletions includes/BlockSupports/Color.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* The BlockWithColorSupportAttributes Interface Type.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#color
*
* @package WPGraphQL\ContentBlocks\BlockSupports
*/

namespace WPGraphQL\ContentBlocks\BlockSupports;

/**
* Class Color
*/
final class Color extends AbstractBlockSupport {
/**
* {@inheritDoc}
*/
public static function register(): void {
register_graphql_interface_type(
'BlockWithColorSupportAttributes',
[
'description' => __( 'Attributes for a Block with Color support.', 'wp-graphql-content-blocks' ),
'eagerlyLoadType' => true,
'interfaces' => [ 'EditorBlock' ],
'fields' => [
'backgroundColor' => [
'type' => 'String',
'description' => __( 'The backgroundColor attribute for the block.', 'wp-graphql-content-blocks' ),
],
'textColor' => [
'type' => 'String',
'description' => __( 'The textColor attribute for the block.', 'wp-graphql-content-blocks' ),
],
'gradient' => [
'type' => 'String',
'description' => __( 'The gradientColor attribute for the block.', 'wp-graphql-content-blocks' ),
],
],
]
);
}

/**
* {@inheritDoc}
*/
public static function has_block_support( \WP_Block_Type $block_type ): bool {
return block_has_support( $block_type, [ 'color' ], false );
}

/**
* {@inheritDoc}
*/
public static function get_attributes_interfaces( \WP_Block_Type $block_type ): array {
if ( ! self::has_block_support( $block_type ) ) {
return [];
}

return [ 'BlockWithColorSupportAttributes' ];
}
}
53 changes: 53 additions & 0 deletions includes/BlockSupports/CustomClassName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* The BlockWithCustomClassNameSupportAttributes Interface Type.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#classname
*
* @package WPGraphQL\ContentBlocks\BlockSupports
*/

namespace WPGraphQL\ContentBlocks\BlockSupports;

/**
* Class CustomClassName
*/
final class CustomClassName extends AbstractBlockSupport {
/**
* {@inheritDoc}
*/
public static function register(): void {
register_graphql_interface_type(
'BlockWithCustomClassNameSupportAttributes',
[
'description' => __( 'Attributes for a block with customClassName support', 'wp-graphql-content-blocks' ),
'eagerlyLoadType' => true,
'interfaces' => [ 'EditorBlock' ],
'fields' => [
'className' => [
'type' => 'String',
'description' => __( 'The custom CSS class name attribute for the block.', 'wp-graphql-content-blocks' ),
],
],
]
);
}

/**
* {@inheritDoc}
*/
public static function has_block_support( \WP_Block_Type $block_type ): bool {
return block_has_support( $block_type, [ 'customClassName' ], false );
}

/**
* {@inheritDoc}
*/
public static function get_attributes_interfaces( \WP_Block_Type $block_type ): array {
if ( ! self::has_block_support( $block_type ) ) {
return [];
}

return [ 'BlockWithCustomClassNameSupportAttributes' ];
}
}
53 changes: 53 additions & 0 deletions includes/BlockSupports/Shadow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* The BlockWithShadowSupportAttributes Interface Type.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#shadow
*
* @package WPGraphQL\ContentBlocks\BlockSupports
*/

namespace WPGraphQL\ContentBlocks\BlockSupports;

/**
* Class Shadow
*/
final class Shadow extends AbstractBlockSupport {
/**
* {@inheritDoc}
*/
public static function register(): void {
register_graphql_interface_type(
'BlockWithShadowSupportAttributes',
[
'description' => __( 'Attributes for a Block with Shadow support.', 'wp-graphql-content-blocks' ),
'eagerlyLoadType' => true,
'interfaces' => [ 'EditorBlock' ],
'fields' => [
'shadow' => [
'type' => 'String',
'description' => __( 'The shadow attribute for the block.', 'wp-graphql-content-blocks' ),
],
],
]
);
}

/**
* {@inheritDoc}
*/
public static function has_block_support( \WP_Block_Type $block_type ): bool {
return block_has_support( $block_type, [ 'shadow' ], false );
}

/**
* {@inheritDoc}
*/
public static function get_attributes_interfaces( \WP_Block_Type $block_type ): array {
if ( ! self::has_block_support( $block_type ) ) {
return [];
}

return [ 'BlockWithShadowSupportAttributes' ];
}
}
57 changes: 57 additions & 0 deletions includes/BlockSupports/Typography.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* The BlockWithTypographySupportAttributes Interface Type.
*
* @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#typography
*
* @package WPGraphQL\ContentBlocks\BlockSupports
*/

namespace WPGraphQL\ContentBlocks\BlockSupports;

/**
* Class Typography
*/
final class Typography extends AbstractBlockSupport {
/**
* Registers the types to WPGraphQL.
*/
public static function register(): void {
register_graphql_interface_type(
'BlockWithTypographySupportAttributes',
[
'description' => __( 'Attributes for a Block with Typography support.', 'wp-graphql-content-blocks' ),
'eagerlyLoadType' => true,
'interfaces' => [ 'EditorBlock' ],
'fields' => [
'fontSize' => [
'type' => 'String',
'description' => __( 'The fontSize attribute for the block.', 'wp-graphql-content-blocks' ),
],
'fontFamily' => [
'type' => 'String',
'description' => __( 'The fontFamily attribute for the block.', 'wp-graphql-content-blocks' ),
],
],
]
);
}

/**
* {@inheritDoc}
*/
public static function has_block_support( \WP_Block_Type $block_type ): bool {
return block_has_support( $block_type, [ 'typography' ], false );
}

/**
* {@inheritDoc}
*/
public static function get_attributes_interfaces( \WP_Block_Type $block_type ): array {
if ( ! self::has_block_support( $block_type ) ) {
return [];
}

return [ 'BlockWithTypographySupportAttributes' ];
}
}
Loading
Loading