Skip to content

Commit

Permalink
Add padding server-side block support (#30332)
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad authored Mar 30, 2021
1 parent 8985353 commit 12c2326
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 48 deletions.
10 changes: 2 additions & 8 deletions lib/block-supports/align.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
* @param WP_Block_Type $block_type Block Type.
*/
function gutenberg_register_alignment_support( $block_type ) {
$has_align_support = false;
if ( property_exists( $block_type, 'supports' ) ) {
$has_align_support = _wp_array_get( $block_type->supports, array( 'align' ), false );
}
$has_align_support = gutenberg_block_has_support( $block_type, array( 'align' ), false );
if ( $has_align_support ) {
if ( ! $block_type->attributes ) {
$block_type->attributes = array();
Expand All @@ -40,10 +37,7 @@ function gutenberg_register_alignment_support( $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 = _wp_array_get( $block_type->supports, array( 'align' ), false );
}
$has_align_support = gutenberg_block_has_support( $block_type, array( 'align' ), false );
if ( $has_align_support ) {
$has_block_alignment = array_key_exists( 'align', $block_attributes );

Expand Down
23 changes: 3 additions & 20 deletions lib/block-supports/border.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
function gutenberg_register_border_support( $block_type ) {
// Determine border related features supported.
// Border width, style etc can be added in the future.
$has_border_radius_support = gutenberg_has_border_support( $block_type, 'radius' );
$has_border_radius_support = gutenberg_block_has_support( $block_type, array( '__experimentalBorder', 'radius' ), false );

// Setup attributes and styles within that if needed.
if ( ! $block_type->attributes ) {
Expand Down Expand Up @@ -52,7 +52,8 @@ function gutenberg_apply_border_support( $block_type, $block_attributes ) {
$styles = array();

// Border Radius.
if ( gutenberg_has_border_support( $block_type, 'radius' ) ) {
$has_border_radius_support = gutenberg_block_has_support( $block_type, array( '__experimentalBorder', 'radius' ), false );
if ( $has_border_radius_support ) {
if ( isset( $block_attributes['style']['border']['radius'] ) ) {
$border_radius = intval( $block_attributes['style']['border']['radius'] );
$styles[] = sprintf( 'border-radius: %dpx;', $border_radius );
Expand All @@ -71,24 +72,6 @@ function gutenberg_apply_border_support( $block_type, $block_attributes ) {
return $attributes;
}

/**
* Checks whether the current block type supports the feature requested.
*
* @param WP_Block_Type $block_type Block type to check for support.
* @param string $feature Name of the feature to check support for.
* @param mixed $default Fallback value for feature support, defaults to false.
*
* @return boolean Whether or not the feature is supported.
*/
function gutenberg_has_border_support( $block_type, $feature, $default = false ) {
$block_support = false;
if ( property_exists( $block_type, 'supports' ) ) {
$block_support = _wp_array_get( $block_type->supports, array( '__experimentalBorder' ), $default );
}

return true === $block_support || ( is_array( $block_support ) && _wp_array_get( $block_support, array( $feature ), false ) );
}

// Register the block support.
WP_Block_Supports::get_instance()->register(
'border',
Expand Down
11 changes: 3 additions & 8 deletions lib/block-supports/custom-classname.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
* @param WP_Block_Type $block_type Block Type.
*/
function gutenberg_register_custom_classname_support( $block_type ) {
$has_custom_classname_support = true;
if ( property_exists( $block_type, 'supports' ) ) {
$has_custom_classname_support = _wp_array_get( $block_type->supports, array( 'customClassName' ), true );
}
$has_custom_classname_support = gutenberg_block_has_support( $block_type, array( 'customClassName' ), true );

if ( $has_custom_classname_support ) {
if ( ! $block_type->attributes ) {
$block_type->attributes = array();
Expand All @@ -37,11 +35,8 @@ function gutenberg_register_custom_classname_support( $block_type ) {
* @return array Block CSS classes and inline styles.
*/
function gutenberg_apply_custom_classname_support( $block_type, $block_attributes ) {
$has_custom_classname_support = true;
$has_custom_classname_support = gutenberg_block_has_support( $block_type, array( 'customClassName' ), true );
$attributes = array();
if ( property_exists( $block_type, 'supports' ) ) {
$has_custom_classname_support = _wp_array_get( $block_type->supports, array( 'customClassName' ), true );
}
if ( $has_custom_classname_support ) {
$has_custom_classnames = array_key_exists( 'className', $block_attributes );

Expand Down
5 changes: 1 addition & 4 deletions lib/block-supports/generated-classname.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,8 @@ function gutenberg_get_block_default_classname( $block_name ) {
* @return array Block CSS classes and inline styles.
*/
function gutenberg_apply_generated_classname_support( $block_type ) {
$has_generated_classname_support = true;
$attributes = array();
if ( property_exists( $block_type, 'supports' ) ) {
$has_generated_classname_support = _wp_array_get( $block_type->supports, array( 'className' ), true );
}
$has_generated_classname_support = gutenberg_block_has_support( $block_type, array( 'className' ), true );
if ( $has_generated_classname_support ) {
$block_classname = gutenberg_get_block_default_classname( $block_type->name );

Expand Down
10 changes: 2 additions & 8 deletions lib/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
* @param WP_Block_Type $block_type Block Type.
*/
function gutenberg_register_layout_support( $block_type ) {
$support_layout = false;
if ( property_exists( $block_type, 'supports' ) ) {
$support_layout = _wp_array_get( $block_type->supports, array( '__experimentalLayout' ), false );
}
$support_layout = gutenberg_block_has_support( $block_type, array( '__experimentalLayout' ), false );
if ( $support_layout ) {
if ( ! $block_type->attributes ) {
$block_type->attributes = array();
Expand All @@ -37,10 +34,7 @@ function gutenberg_register_layout_support( $block_type ) {
*/
function gutenberg_render_layout_support_flag( $block_content, $block ) {
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
$support_layout = false;
if ( $block_type && property_exists( $block_type, 'supports' ) ) {
$support_layout = _wp_array_get( $block_type->supports, array( '__experimentalLayout' ), false );
}
$support_layout = gutenberg_block_has_support( $block_type, array( '__experimentalLayout' ), false );
if ( ! $support_layout || ! isset( $block['attrs']['layout'] ) ) {
return $block_content;
}
Expand Down
59 changes: 59 additions & 0 deletions lib/block-supports/padding.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Padding block support flag.
*
* @package gutenberg
*/

/**
* Registers the style block attribute for block types that support it.
*
* @param WP_Block_Type $block_type Block Type.
*/
function gutenberg_register_padding_support( $block_type ) {
$has_padding_support = gutenberg_block_has_support( $block_type, array( 'spacing', 'padding' ), false );

// Setup attributes and styles within that if needed.
if ( ! $block_type->attributes ) {
$block_type->attributes = array();
}

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

/**
* Add CSS classes for block padding to the incoming attributes array.
* This will be applied to the block markup in the front-end.
*
* @param WP_Block_Type $block_type Block Type.
* @param array $block_attributes Block attributes.
*
* @return array Block padding CSS classes and inline styles.
*/
function gutenberg_apply_padding_support( $block_type, $block_attributes ) {
$has_padding_support = gutenberg_block_has_support( $block_type, array( 'spacing', 'padding' ), false );
$styles = array();
if ( $has_padding_support ) {
$padding_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'padding' ), null );
if ( null !== $padding_value ) {
foreach ( $padding_value as $key => $value ) {
$styles[] = sprintf( 'padding-%s: %s;', $key, $value );
}
}
}

return empty( $styles ) ? array() : array( 'style' => implode( ' ', $styles ) );
}

// Register the block support.
WP_Block_Supports::get_instance()->register(
'padding',
array(
'register_attribute' => 'gutenberg_register_padding_support',
'apply' => 'gutenberg_apply_padding_support',
)
);
18 changes: 18 additions & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,3 +375,21 @@ function gutenberg_register_theme_block_category( $categories ) {
}

add_filter( 'block_categories', 'gutenberg_register_theme_block_category' );

/**
* Checks whether the current block type supports the feature requested.
*
* @param WP_Block_Type $block_type Block type to check for support.
* @param string $feature Name of the feature to check support for.
* @param mixed $default Fallback value for feature support, defaults to false.
*
* @return boolean Whether or not the feature is supported.
*/
function gutenberg_block_has_support( $block_type, $feature, $default = false ) {
$block_support = $default;
if ( $block_type && property_exists( $block_type, 'supports' ) ) {
$block_support = _wp_array_get( $block_type->supports, $feature, $default );
}

return true === $block_support || is_array( $block_support );
}
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,4 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/block-supports/custom-classname.php';
require __DIR__ . '/block-supports/border.php';
require __DIR__ . '/block-supports/layout.php';
require __DIR__ . '/block-supports/padding.php';

0 comments on commit 12c2326

Please sign in to comment.