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

Make block supports server-side explicit #26192

Merged
merged 30 commits into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
b665b7a
wip
mcsf Oct 9, 2020
2d01a86
wip 2
mcsf Oct 12, 2020
73813c5
wip: fix inline style enqueuing
mcsf Oct 12, 2020
1f88e8e
wip: misc
mcsf Oct 12, 2020
0e2b74e
add lineHeight support
mcsf Oct 12, 2020
5401920
solve defaults for nested features like color.text
mcsf Oct 13, 2020
96dfa71
try supporting color.text
mcsf Oct 13, 2020
36aa5d5
wip: Support class injection in Cover. Bump style specificity
mcsf Oct 13, 2020
67e98d2
try injecting classes into all rendered blocks automatically
mcsf Oct 13, 2020
f74925b
clean up a bit
mcsf Oct 14, 2020
5748560
Encapsulate in new class WP_Block_Supports
mcsf Oct 14, 2020
9f32536
Delete ported logic
mcsf Oct 14, 2020
a602658
prefer calling instance method over static function
mcsf Oct 14, 2020
e387a35
added FIXME note in compat file before I forget
mcsf Oct 14, 2020
d9684b6
Extend support from `class` to more attributes (1/2)
mcsf Oct 14, 2020
329af9d
Extend support from `class` to more attributes (2/2)
mcsf Oct 14, 2020
527beb3
color.text: use proper inline styles
mcsf Oct 14, 2020
9e2cdca
WIP Support inline font sizes
mcsf Oct 15, 2020
381fe0f
Refactor to use explicit block supports
youknowriad Oct 16, 2020
a4c4660
Add get_block_wrapper_attributes call to all dynamic blocks
youknowriad Oct 16, 2020
58a214d
Remove useless render_callback check
youknowriad Oct 16, 2020
5153cbd
Fix phpcs
youknowriad Oct 16, 2020
0e5b716
restore cover block.json
youknowriad Oct 16, 2020
15e6401
get_block_wrapper_attributes: make argument optional
mcsf Oct 16, 2020
78f8137
Fix sprintf call in Navigation block
mcsf Oct 16, 2020
9a0d7ff
Fix DocBlock entry for optional @param
mcsf Oct 16, 2020
6506820
Remove checks
youknowriad Oct 19, 2020
9916777
Remove useless stylesheet
youknowriad Oct 19, 2020
30a5c4d
Remove useless checks
youknowriad Oct 19, 2020
177b003
Update compact messaging
youknowriad Oct 19, 2020
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
17 changes: 13 additions & 4 deletions lib/block-supports/align.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ function gutenberg_register_alignment_support( $block_type ) {
* Add CSS classes for block alignment to the incoming attributes array.
* This will be applied to the block markup in the front-end.
*
* @param array $attributes Comprehensive list of attributes to be applied.
* @param array $block_attributes Block attributes.
* @param WP_Block_Type $block_type Block Type.
* @param array $block_attributes Block attributes.
*
* @return array Block alignment CSS classes and inline styles.
*/
function gutenberg_apply_alignment_support( $attributes, $block_attributes, $block_type ) {
function gutenberg_apply_alignment_support( $block_type, $block_attributes ) {
$attributes = array();
$has_align_support = false;
if ( property_exists( $block_type, 'supports' ) ) {
$has_align_support = gutenberg_experimental_get( $block_type->supports, array( 'align' ), false );
Expand All @@ -48,9 +48,18 @@ function gutenberg_apply_alignment_support( $attributes, $block_attributes, $blo
$has_block_alignment = array_key_exists( 'align', $block_attributes );

if ( $has_block_alignment ) {
$attributes['css_classes'][] = sprintf( 'align%s', $block_attributes['align'] );
$attributes['class'] = sprintf( 'align%s', $block_attributes['align'] );
}
}

return $attributes;
}

// Register the block support.
WP_Block_Supports::get_instance()->register(
'align',
array(
'register_attribute' => 'gutenberg_register_alignment_support',
'apply' => 'gutenberg_apply_alignment_support',
)
);
52 changes: 35 additions & 17 deletions lib/block-supports/colors.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,19 @@ function gutenberg_register_colors_support( $block_type ) {
* 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.
*
* @param array $attributes Comprehensive list of attributes to be applied.
* @param array $block_attributes Block attributes.
* @param WP_Block_Type $block_type Block type.
* @param array $block_attributes Block attributes.
*
* @return array Colors CSS classes and inline styles.
*/
function gutenberg_apply_colors_support( $attributes, $block_attributes, $block_type ) {
function gutenberg_apply_colors_support( $block_type, $block_attributes ) {
$color_support = gutenberg_experimental_get( $block_type->supports, array( 'color' ), false );
$has_text_colors_support = true === $color_support || ( is_array( $color_support ) && gutenberg_experimental_get( $color_support, array( 'text' ), true ) );
$has_background_colors_support = true === $color_support || ( is_array( $color_support ) && gutenberg_experimental_get( $color_support, array( 'background' ), true ) );
$has_link_colors_support = gutenberg_experimental_get( $color_support, array( 'link' ), false );
$has_gradients_support = gutenberg_experimental_get( $color_support, array( 'gradients' ), false );
$classes = array();
$styles = array();

// Text Colors.
// Check support for text colors.
Expand All @@ -74,13 +75,13 @@ function gutenberg_apply_colors_support( $attributes, $block_attributes, $block_

// Apply required generic class.
if ( $has_custom_text_color || $has_named_text_color ) {
$attributes['css_classes'][] = 'has-text-color';
$classes[] = 'has-text-color';
}
// Apply color class or inline style.
if ( $has_named_text_color ) {
$attributes['css_classes'][] = sprintf( 'has-%s-color', $block_attributes['textColor'] );
$classes[] = sprintf( 'has-%s-color', $block_attributes['textColor'] );
} elseif ( $has_custom_text_color ) {
$attributes['inline_styles'][] = sprintf( 'color: %s;', $block_attributes['style']['color']['text'] );
$styles[] = sprintf( 'color: %s;', $block_attributes['style']['color']['text'] );
}
}

Expand All @@ -89,15 +90,15 @@ function gutenberg_apply_colors_support( $attributes, $block_attributes, $block_
$has_link_color = isset( $block_attributes['style']['color']['link'] );
// Apply required class and style.
if ( $has_link_color ) {
$attributes['css_classes'][] = 'has-link-color';
$classes[] = 'has-link-color';
// If link is a named color.
if ( strpos( $block_attributes['style']['color']['link'], 'var:preset|color|' ) !== false ) {
// Get the name from the string and add proper styles.
$index_to_splice = strrpos( $block_attributes['style']['color']['link'], '|' ) + 1;
$link_color_name = substr( $block_attributes['style']['color']['link'], $index_to_splice );
$attributes['inline_styles'][] = sprintf( '--wp--style--color--link:var(--wp--preset--color--%s);', $link_color_name );
$index_to_splice = strrpos( $block_attributes['style']['color']['link'], '|' ) + 1;
$link_color_name = substr( $block_attributes['style']['color']['link'], $index_to_splice );
$styles[] = sprintf( '--wp--style--color--link: var(--wp--preset--color--%s);', $link_color_name );
} else {
$attributes['inline_styles'][] = sprintf( '--wp--style--color--link: %s;', $block_attributes['style']['color']['link'] );
$styles[] = sprintf( '--wp--style--color--link: %s;', $block_attributes['style']['color']['link'] );
}
}
}
Expand All @@ -109,13 +110,13 @@ function gutenberg_apply_colors_support( $attributes, $block_attributes, $block_

// Apply required background class.
if ( $has_custom_background_color || $has_named_background_color ) {
$attributes['css_classes'][] = 'has-background';
$classes[] = 'has-background';
}
// Apply background color classes or styles.
if ( $has_named_background_color ) {
$attributes['css_classes'][] = sprintf( 'has-%s-background-color', $block_attributes['backgroundColor'] );
$classes[] = sprintf( 'has-%s-background-color', $block_attributes['backgroundColor'] );
} elseif ( $has_custom_background_color ) {
$attributes['inline_styles'][] = sprintf( 'background-color: %s;', $block_attributes['style']['color']['background'] );
$styles[] = sprintf( 'background-color: %s;', $block_attributes['style']['color']['background'] );
}
}

Expand All @@ -125,15 +126,32 @@ function gutenberg_apply_colors_support( $attributes, $block_attributes, $block_
$has_custom_gradient = isset( $block_attributes['style']['color']['gradient'] );

if ( $has_named_gradient || $has_custom_gradient ) {
$attributes['css_classes'][] = 'has-background';
$classes[] = 'has-background';
}
// Apply required background class.
if ( $has_named_gradient ) {
$attributes['css_classes'][] = sprintf( 'has-%s-gradient-background', $block_attributes['gradient'] );
$classes[] = sprintf( 'has-%s-gradient-background', $block_attributes['gradient'] );
} elseif ( $has_custom_gradient ) {
$attributes['inline_styles'][] = sprintf( 'background: %s;', $block_attributes['style']['color']['gradient'] );
$styles[] = sprintf( 'background: %s;', $block_attributes['style']['color']['gradient'] );
}
}

$attributes = array();
if ( ! empty( $classes ) ) {
$attributes['class'] = implode( ' ', $classes );
}
if ( ! empty( $styles ) ) {
$attributes['style'] = implode( ' ', $styles );
}

return $attributes;
}

// Register the block support.
WP_Block_Supports::get_instance()->register(
'colors',
array(
'register_attribute' => 'gutenberg_register_colors_support',
'apply' => 'gutenberg_apply_colors_support',
)
);
17 changes: 13 additions & 4 deletions lib/block-supports/custom-classname.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,33 @@ function gutenberg_register_custom_classname_support( $block_type ) {
/**
* Add the custom classnames to the output.
*
* @param array $attributes Comprehensive list of attributes to be applied.
* @param array $block_attributes Block attributes.
* @param WP_Block_Type $block_type Block Type.
* @param array $block_attributes Block attributes.
*
* @return array Block CSS classes and inline styles.
*/
function gutenberg_apply_custom_classname_support( $attributes, $block_attributes, $block_type ) {
function gutenberg_apply_custom_classname_support( $block_type, $block_attributes ) {
$has_custom_classname_support = true;
$attributes = array();
if ( property_exists( $block_type, 'supports' ) ) {
$has_custom_classname_support = gutenberg_experimental_get( $block_type->supports, array( 'customClassName' ), true );
}
if ( $has_custom_classname_support ) {
$has_custom_classnames = array_key_exists( 'className', $block_attributes );

if ( $has_custom_classnames ) {
$attributes['css_classes'][] = $block_attributes['className'];
$attributes['class'] = $block_attributes['className'];
}
}

return $attributes;
}

// Register the block support.
WP_Block_Supports::get_instance()->register(
'custom-classname',
array(
'register_attribute' => 'gutenberg_register_custom_classname_support',
'apply' => 'gutenberg_apply_custom_classname_support',
)
);
16 changes: 12 additions & 4 deletions lib/block-supports/generated-classname.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,32 @@ function gutenberg_get_block_default_classname( $block_name ) {
/**
* Add the generated classnames to the output.
*
* @param array $attributes Comprehensive list of attributes to be applied.
* @param array $block_attributes Block attributes.
* @param WP_Block_Type $block_type Block Type.
* @param array $block_attributes Block attributes.
*
* @return array Block CSS classes and inline styles.
*/
function gutenberg_apply_generated_classname_support( $attributes, $block_attributes, $block_type ) {
function gutenberg_apply_generated_classname_support( $block_type, $block_attributes ) {
$has_generated_classname_support = true;
$attributes = array();
if ( property_exists( $block_type, 'supports' ) ) {
$has_generated_classname_support = gutenberg_experimental_get( $block_type->supports, array( 'className' ), true );
}
if ( $has_generated_classname_support ) {
$block_classname = gutenberg_get_block_default_classname( $block_type->name );

if ( $block_classname ) {
$attributes['css_classes'][] = $block_classname;
$attributes['class'] = $block_classname;
}
}

return $attributes;
}

// Register the block support.
WP_Block_Supports::get_instance()->register(
'generated-classname',
array(
'apply' => 'gutenberg_apply_generated_classname_support',
)
);
130 changes: 0 additions & 130 deletions lib/block-supports/index.php

This file was deleted.

Loading