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

Register the block attributes server-side for blocks with support flags #24400

Merged
merged 1 commit into from
Aug 11, 2020
Merged
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
12 changes: 7 additions & 5 deletions lib/block-supports/align.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

/**
* Registers the attributes block attribute for block types that support it.
* Registers the align block attribute for block types that support it.
*
* @param array $block_type Block Type.
*/
Expand All @@ -17,10 +17,12 @@ function gutenberg_register_alignment_support( $block_type ) {
$block_type->attributes = array();
}

$block_type->attributes['align'] = array(
'type' => 'string',
'enum' => array( 'left', 'center', 'right', 'wide', 'full', '' ),
);
if ( ! array_key_exists( 'align', $block_type->attributes ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes! Good addition we missed in the last one. This makes sense to not define if it is already defined.

$block_type->attributes['align'] = array(
'type' => 'string',
'enum' => array( 'left', 'center', 'right', 'wide', 'full', '' ),
);
}
}
}

Expand Down
41 changes: 41 additions & 0 deletions lib/block-supports/colors.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,47 @@
* @package gutenberg
*/

/**
* Registers the style and colors block attributes for block types that support it.
*
* @param array $block_type Block Type.
*/
function gutenberg_register_colors_support( $block_type ) {
$color_support = gutenberg_experimental_get( $block_type->supports, array( '__experimentalColor' ), false );
$has_text_colors_support = is_array( $color_support ) || $color_support;
$has_background_colors_support = $has_text_colors_support;
$has_gradients_support = $has_text_colors_support && gutenberg_experimental_get( $color_support, array( 'gradients' ), false );

if ( ! $block_type->attributes ) {
$block_type->attributes = array();
}

if ( $has_text_colors_support && ! array_key_exists( 'style', $block_type->attributes ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be more readable if we use $color_support instead of $has_text_colors_support (or a comment), since the style attribute needs to be initialized for custom values of all the different items that can have color support.

At first glance reading it I thought "why are we only defining the style object for text colors?", but then realized that none of the others can be supported without text color support anyways. So it still looks correct from a functionality standpoint!

$block_type->attributes['style'] = array(
'type' => 'object',
);
}

if ( $has_background_colors_support && ! array_key_exists( 'backgroundColor', $block_type->attributes ) ) {
$block_type->attributes['backgroundColor'] = array(
'type' => 'string',
);
}

if ( $has_text_colors_support && ! array_key_exists( 'textColor', $block_type->attributes ) ) {
$block_type->attributes['textColor'] = array(
'type' => 'string',
);
}

if ( $has_gradients_support && ! array_key_exists( 'gradient', $block_type->attributes ) ) {
$block_type->attributes['gradient'] = array(
'type' => 'string',
);
}
}


/**
* Add CSS classes and inline styles for colors to the incoming attributes array.
* This will be applied to the block markup in the front-end.
Expand Down
2 changes: 2 additions & 0 deletions lib/block-supports/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ function gutenberg_register_block_supports() {
// instead of mutating the block type.
foreach ( $registered_block_types as $block_type ) {
gutenberg_register_alignment_support( $block_type );
gutenberg_register_colors_support( $block_type );
gutenberg_register_typography_support( $block_type );
}
}

Expand Down
26 changes: 26 additions & 0 deletions lib/block-supports/typography.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,32 @@
* @package gutenberg
*/

/**
* Registers the style and typography block attributes for block types that support it.
*
* @param array $block_type Block Type.
*/
function gutenberg_register_typography_support( $block_type ) {
$has_font_size_support = gutenberg_experimental_get( $block_type->supports, array( '__experimentalFontSize' ), false );
$has_line_height_support = gutenberg_experimental_get( $block_type->supports, array( '__experimentalLineHeight' ), false );

if ( ! $block_type->attributes ) {
$block_type->attributes = array();
}

if ( ( $has_font_size_support || $has_line_height_support ) && ! array_key_exists( 'style', $block_type->attributes ) ) {
$block_type->attributes['style'] = array(
'type' => 'object',
);
}

if ( $has_font_size_support && ! array_key_exists( 'fontSize', $block_type->attributes ) ) {
$block_type->attributes['fontSize'] = array(
'type' => 'string',
);
}
}

/**
* Add CSS classes and inline styles for font sizes to the incoming attributes array.
* This will be applied to the block markup in the front-end.
Expand Down