080600
This commit is contained in:
@@ -1,65 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
* Align block support flag.
|
||||
*
|
||||
* @package WordPress
|
||||
* @since 5.6.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Registers the align block attribute for block types that support it.
|
||||
*
|
||||
* @since 5.6.0
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block Type.
|
||||
*/
|
||||
function wp_register_alignment_support( $block_type ) {
|
||||
$has_align_support = block_has_support( $block_type, 'align', false );
|
||||
if ( $has_align_support ) {
|
||||
if ( ! $block_type->attributes ) {
|
||||
$block_type->attributes = array();
|
||||
}
|
||||
|
||||
if ( ! array_key_exists( 'align', $block_type->attributes ) ) {
|
||||
$block_type->attributes['align'] = array(
|
||||
'type' => 'string',
|
||||
'enum' => array( 'left', 'center', 'right', 'wide', 'full', '' ),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds CSS classes for block alignment to the incoming attributes array.
|
||||
* This will be applied to the block markup in the front-end.
|
||||
*
|
||||
* @since 5.6.0
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block Type.
|
||||
* @param array $block_attributes Block attributes.
|
||||
* @return array Block alignment CSS classes and inline styles.
|
||||
*/
|
||||
function wp_apply_alignment_support( $block_type, $block_attributes ) {
|
||||
$attributes = array();
|
||||
$has_align_support = block_has_support( $block_type, 'align', false );
|
||||
if ( $has_align_support ) {
|
||||
$has_block_alignment = array_key_exists( 'align', $block_attributes );
|
||||
|
||||
if ( $has_block_alignment ) {
|
||||
$attributes['class'] = sprintf( 'align%s', $block_attributes['align'] );
|
||||
}
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
// Register the block support.
|
||||
WP_Block_Supports::get_instance()->register(
|
||||
'align',
|
||||
array(
|
||||
'register_attribute' => 'wp_register_alignment_support',
|
||||
'apply' => 'wp_apply_alignment_support',
|
||||
)
|
||||
);
|
||||
<?php
|
||||
/**
|
||||
* Align block support flag.
|
||||
*
|
||||
* @package WordPress
|
||||
* @since 5.6.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Registers the align block attribute for block types that support it.
|
||||
*
|
||||
* @since 5.6.0
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block Type.
|
||||
*/
|
||||
function wp_register_alignment_support( $block_type ) {
|
||||
$has_align_support = block_has_support( $block_type, 'align', false );
|
||||
if ( $has_align_support ) {
|
||||
if ( ! $block_type->attributes ) {
|
||||
$block_type->attributes = array();
|
||||
}
|
||||
|
||||
if ( ! array_key_exists( 'align', $block_type->attributes ) ) {
|
||||
$block_type->attributes['align'] = array(
|
||||
'type' => 'string',
|
||||
'enum' => array( 'left', 'center', 'right', 'wide', 'full', '' ),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds CSS classes for block alignment to the incoming attributes array.
|
||||
* This will be applied to the block markup in the front-end.
|
||||
*
|
||||
* @since 5.6.0
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block Type.
|
||||
* @param array $block_attributes Block attributes.
|
||||
* @return array Block alignment CSS classes and inline styles.
|
||||
*/
|
||||
function wp_apply_alignment_support( $block_type, $block_attributes ) {
|
||||
$attributes = array();
|
||||
$has_align_support = block_has_support( $block_type, 'align', false );
|
||||
if ( $has_align_support ) {
|
||||
$has_block_alignment = array_key_exists( 'align', $block_attributes );
|
||||
|
||||
if ( $has_block_alignment ) {
|
||||
$attributes['class'] = sprintf( 'align%s', $block_attributes['align'] );
|
||||
}
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
// Register the block support.
|
||||
WP_Block_Supports::get_instance()->register(
|
||||
'align',
|
||||
array(
|
||||
'register_attribute' => 'wp_register_alignment_support',
|
||||
'apply' => 'wp_apply_alignment_support',
|
||||
)
|
||||
);
|
||||
|
||||
@@ -1,138 +1,138 @@
|
||||
<?php
|
||||
/**
|
||||
* Background block support flag.
|
||||
*
|
||||
* @package WordPress
|
||||
* @since 6.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Registers the style block attribute for block types that support it.
|
||||
*
|
||||
* @since 6.4.0
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block Type.
|
||||
*/
|
||||
function wp_register_background_support( $block_type ) {
|
||||
// Setup attributes and styles within that if needed.
|
||||
if ( ! $block_type->attributes ) {
|
||||
$block_type->attributes = array();
|
||||
}
|
||||
|
||||
// Check for existing style attribute definition e.g. from block.json.
|
||||
if ( array_key_exists( 'style', $block_type->attributes ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$has_background_support = block_has_support( $block_type, array( 'background' ), false );
|
||||
|
||||
if ( $has_background_support ) {
|
||||
$block_type->attributes['style'] = array(
|
||||
'type' => 'object',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the background styles to the block wrapper.
|
||||
* This block support uses the `render_block` hook to ensure that
|
||||
* it is also applied to non-server-rendered blocks.
|
||||
*
|
||||
* @since 6.4.0
|
||||
* @since 6.5.0 Added support for `backgroundPosition` and `backgroundRepeat` output.
|
||||
* @access private
|
||||
*
|
||||
* @param string $block_content Rendered block content.
|
||||
* @param array $block Block object.
|
||||
* @return string Filtered block content.
|
||||
*/
|
||||
function wp_render_background_support( $block_content, $block ) {
|
||||
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
|
||||
$block_attributes = ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) ? $block['attrs'] : array();
|
||||
$has_background_image_support = block_has_support( $block_type, array( 'background', 'backgroundImage' ), false );
|
||||
|
||||
if (
|
||||
! $has_background_image_support ||
|
||||
wp_should_skip_block_supports_serialization( $block_type, 'background', 'backgroundImage' )
|
||||
) {
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
$background_image_source = isset( $block_attributes['style']['background']['backgroundImage']['source'] )
|
||||
? $block_attributes['style']['background']['backgroundImage']['source']
|
||||
: null;
|
||||
$background_image_url = isset( $block_attributes['style']['background']['backgroundImage']['url'] )
|
||||
? $block_attributes['style']['background']['backgroundImage']['url']
|
||||
: null;
|
||||
|
||||
if ( ! $background_image_source && ! $background_image_url ) {
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
$background_size = isset( $block_attributes['style']['background']['backgroundSize'] )
|
||||
? $block_attributes['style']['background']['backgroundSize']
|
||||
: 'cover';
|
||||
$background_position = isset( $block_attributes['style']['background']['backgroundPosition'] )
|
||||
? $block_attributes['style']['background']['backgroundPosition']
|
||||
: null;
|
||||
$background_repeat = isset( $block_attributes['style']['background']['backgroundRepeat'] )
|
||||
? $block_attributes['style']['background']['backgroundRepeat']
|
||||
: null;
|
||||
|
||||
$background_block_styles = array();
|
||||
|
||||
if (
|
||||
'file' === $background_image_source &&
|
||||
$background_image_url
|
||||
) {
|
||||
// Set file based background URL.
|
||||
$background_block_styles['backgroundImage']['url'] = $background_image_url;
|
||||
// Only output the background size and repeat when an image url is set.
|
||||
$background_block_styles['backgroundSize'] = $background_size;
|
||||
$background_block_styles['backgroundRepeat'] = $background_repeat;
|
||||
$background_block_styles['backgroundPosition'] = $background_position;
|
||||
|
||||
// If the background size is set to `contain` and no position is set, set the position to `center`.
|
||||
if ( 'contain' === $background_size && ! isset( $background_position ) ) {
|
||||
$background_block_styles['backgroundPosition'] = 'center';
|
||||
}
|
||||
}
|
||||
|
||||
$styles = wp_style_engine_get_styles( array( 'background' => $background_block_styles ) );
|
||||
|
||||
if ( ! empty( $styles['css'] ) ) {
|
||||
// Inject background styles to the first element, presuming it's the wrapper, if it exists.
|
||||
$tags = new WP_HTML_Tag_Processor( $block_content );
|
||||
|
||||
if ( $tags->next_tag() ) {
|
||||
$existing_style = $tags->get_attribute( 'style' );
|
||||
$updated_style = '';
|
||||
|
||||
if ( ! empty( $existing_style ) ) {
|
||||
$updated_style = $existing_style;
|
||||
if ( ! str_ends_with( $existing_style, ';' ) ) {
|
||||
$updated_style .= ';';
|
||||
}
|
||||
}
|
||||
|
||||
$updated_style .= $styles['css'];
|
||||
$tags->set_attribute( 'style', $updated_style );
|
||||
$tags->add_class( 'has-background' );
|
||||
}
|
||||
|
||||
return $tags->get_updated_html();
|
||||
}
|
||||
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
// Register the block support.
|
||||
WP_Block_Supports::get_instance()->register(
|
||||
'background',
|
||||
array(
|
||||
'register_attribute' => 'wp_register_background_support',
|
||||
)
|
||||
);
|
||||
|
||||
add_filter( 'render_block', 'wp_render_background_support', 10, 2 );
|
||||
<?php
|
||||
/**
|
||||
* Background block support flag.
|
||||
*
|
||||
* @package WordPress
|
||||
* @since 6.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Registers the style block attribute for block types that support it.
|
||||
*
|
||||
* @since 6.4.0
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block Type.
|
||||
*/
|
||||
function wp_register_background_support( $block_type ) {
|
||||
// Setup attributes and styles within that if needed.
|
||||
if ( ! $block_type->attributes ) {
|
||||
$block_type->attributes = array();
|
||||
}
|
||||
|
||||
// Check for existing style attribute definition e.g. from block.json.
|
||||
if ( array_key_exists( 'style', $block_type->attributes ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$has_background_support = block_has_support( $block_type, array( 'background' ), false );
|
||||
|
||||
if ( $has_background_support ) {
|
||||
$block_type->attributes['style'] = array(
|
||||
'type' => 'object',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the background styles to the block wrapper.
|
||||
* This block support uses the `render_block` hook to ensure that
|
||||
* it is also applied to non-server-rendered blocks.
|
||||
*
|
||||
* @since 6.4.0
|
||||
* @since 6.5.0 Added support for `backgroundPosition` and `backgroundRepeat` output.
|
||||
* @access private
|
||||
*
|
||||
* @param string $block_content Rendered block content.
|
||||
* @param array $block Block object.
|
||||
* @return string Filtered block content.
|
||||
*/
|
||||
function wp_render_background_support( $block_content, $block ) {
|
||||
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
|
||||
$block_attributes = ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) ? $block['attrs'] : array();
|
||||
$has_background_image_support = block_has_support( $block_type, array( 'background', 'backgroundImage' ), false );
|
||||
|
||||
if (
|
||||
! $has_background_image_support ||
|
||||
wp_should_skip_block_supports_serialization( $block_type, 'background', 'backgroundImage' )
|
||||
) {
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
$background_image_source = isset( $block_attributes['style']['background']['backgroundImage']['source'] )
|
||||
? $block_attributes['style']['background']['backgroundImage']['source']
|
||||
: null;
|
||||
$background_image_url = isset( $block_attributes['style']['background']['backgroundImage']['url'] )
|
||||
? $block_attributes['style']['background']['backgroundImage']['url']
|
||||
: null;
|
||||
|
||||
if ( ! $background_image_source && ! $background_image_url ) {
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
$background_size = isset( $block_attributes['style']['background']['backgroundSize'] )
|
||||
? $block_attributes['style']['background']['backgroundSize']
|
||||
: 'cover';
|
||||
$background_position = isset( $block_attributes['style']['background']['backgroundPosition'] )
|
||||
? $block_attributes['style']['background']['backgroundPosition']
|
||||
: null;
|
||||
$background_repeat = isset( $block_attributes['style']['background']['backgroundRepeat'] )
|
||||
? $block_attributes['style']['background']['backgroundRepeat']
|
||||
: null;
|
||||
|
||||
$background_block_styles = array();
|
||||
|
||||
if (
|
||||
'file' === $background_image_source &&
|
||||
$background_image_url
|
||||
) {
|
||||
// Set file based background URL.
|
||||
$background_block_styles['backgroundImage']['url'] = $background_image_url;
|
||||
// Only output the background size and repeat when an image url is set.
|
||||
$background_block_styles['backgroundSize'] = $background_size;
|
||||
$background_block_styles['backgroundRepeat'] = $background_repeat;
|
||||
$background_block_styles['backgroundPosition'] = $background_position;
|
||||
|
||||
// If the background size is set to `contain` and no position is set, set the position to `center`.
|
||||
if ( 'contain' === $background_size && ! isset( $background_position ) ) {
|
||||
$background_block_styles['backgroundPosition'] = 'center';
|
||||
}
|
||||
}
|
||||
|
||||
$styles = wp_style_engine_get_styles( array( 'background' => $background_block_styles ) );
|
||||
|
||||
if ( ! empty( $styles['css'] ) ) {
|
||||
// Inject background styles to the first element, presuming it's the wrapper, if it exists.
|
||||
$tags = new WP_HTML_Tag_Processor( $block_content );
|
||||
|
||||
if ( $tags->next_tag() ) {
|
||||
$existing_style = $tags->get_attribute( 'style' );
|
||||
$updated_style = '';
|
||||
|
||||
if ( ! empty( $existing_style ) ) {
|
||||
$updated_style = $existing_style;
|
||||
if ( ! str_ends_with( $existing_style, ';' ) ) {
|
||||
$updated_style .= ';';
|
||||
}
|
||||
}
|
||||
|
||||
$updated_style .= $styles['css'];
|
||||
$tags->set_attribute( 'style', $updated_style );
|
||||
$tags->add_class( 'has-background' );
|
||||
}
|
||||
|
||||
return $tags->get_updated_html();
|
||||
}
|
||||
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
// Register the block support.
|
||||
WP_Block_Supports::get_instance()->register(
|
||||
'background',
|
||||
array(
|
||||
'register_attribute' => 'wp_register_background_support',
|
||||
)
|
||||
);
|
||||
|
||||
add_filter( 'render_block', 'wp_render_background_support', 10, 2 );
|
||||
|
||||
@@ -1,176 +1,176 @@
|
||||
<?php
|
||||
/**
|
||||
* Border block support flag.
|
||||
*
|
||||
* @package WordPress
|
||||
* @since 5.8.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Registers the style attribute used by the border feature if needed for block
|
||||
* types that support borders.
|
||||
*
|
||||
* @since 5.8.0
|
||||
* @since 6.1.0 Improved conditional blocks optimization.
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block Type.
|
||||
*/
|
||||
function wp_register_border_support( $block_type ) {
|
||||
// Setup attributes and styles within that if needed.
|
||||
if ( ! $block_type->attributes ) {
|
||||
$block_type->attributes = array();
|
||||
}
|
||||
|
||||
if ( block_has_support( $block_type, '__experimentalBorder' ) && ! array_key_exists( 'style', $block_type->attributes ) ) {
|
||||
$block_type->attributes['style'] = array(
|
||||
'type' => 'object',
|
||||
);
|
||||
}
|
||||
|
||||
if ( wp_has_border_feature_support( $block_type, 'color' ) && ! array_key_exists( 'borderColor', $block_type->attributes ) ) {
|
||||
$block_type->attributes['borderColor'] = array(
|
||||
'type' => 'string',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds CSS classes and inline styles for border styles to the incoming
|
||||
* attributes array. This will be applied to the block markup in the front-end.
|
||||
*
|
||||
* @since 5.8.0
|
||||
* @since 6.1.0 Implemented the style engine to generate CSS and classnames.
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block type.
|
||||
* @param array $block_attributes Block attributes.
|
||||
* @return array Border CSS classes and inline styles.
|
||||
*/
|
||||
function wp_apply_border_support( $block_type, $block_attributes ) {
|
||||
if ( wp_should_skip_block_supports_serialization( $block_type, 'border' ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$border_block_styles = array();
|
||||
$has_border_color_support = wp_has_border_feature_support( $block_type, 'color' );
|
||||
$has_border_width_support = wp_has_border_feature_support( $block_type, 'width' );
|
||||
|
||||
// Border radius.
|
||||
if (
|
||||
wp_has_border_feature_support( $block_type, 'radius' ) &&
|
||||
isset( $block_attributes['style']['border']['radius'] ) &&
|
||||
! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'radius' )
|
||||
) {
|
||||
$border_radius = $block_attributes['style']['border']['radius'];
|
||||
|
||||
if ( is_numeric( $border_radius ) ) {
|
||||
$border_radius .= 'px';
|
||||
}
|
||||
|
||||
$border_block_styles['radius'] = $border_radius;
|
||||
}
|
||||
|
||||
// Border style.
|
||||
if (
|
||||
wp_has_border_feature_support( $block_type, 'style' ) &&
|
||||
isset( $block_attributes['style']['border']['style'] ) &&
|
||||
! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' )
|
||||
) {
|
||||
$border_block_styles['style'] = $block_attributes['style']['border']['style'];
|
||||
}
|
||||
|
||||
// Border width.
|
||||
if (
|
||||
$has_border_width_support &&
|
||||
isset( $block_attributes['style']['border']['width'] ) &&
|
||||
! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' )
|
||||
) {
|
||||
$border_width = $block_attributes['style']['border']['width'];
|
||||
|
||||
// This check handles original unitless implementation.
|
||||
if ( is_numeric( $border_width ) ) {
|
||||
$border_width .= 'px';
|
||||
}
|
||||
|
||||
$border_block_styles['width'] = $border_width;
|
||||
}
|
||||
|
||||
// Border color.
|
||||
if (
|
||||
$has_border_color_support &&
|
||||
! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' )
|
||||
) {
|
||||
$preset_border_color = array_key_exists( 'borderColor', $block_attributes ) ? "var:preset|color|{$block_attributes['borderColor']}" : null;
|
||||
$custom_border_color = isset( $block_attributes['style']['border']['color'] ) ? $block_attributes['style']['border']['color'] : null;
|
||||
$border_block_styles['color'] = $preset_border_color ? $preset_border_color : $custom_border_color;
|
||||
}
|
||||
|
||||
// Generates styles for individual border sides.
|
||||
if ( $has_border_color_support || $has_border_width_support ) {
|
||||
foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) {
|
||||
$border = isset( $block_attributes['style']['border'][ $side ] ) ? $block_attributes['style']['border'][ $side ] : null;
|
||||
$border_side_values = array(
|
||||
'width' => isset( $border['width'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ) ? $border['width'] : null,
|
||||
'color' => isset( $border['color'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' ) ? $border['color'] : null,
|
||||
'style' => isset( $border['style'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' ) ? $border['style'] : null,
|
||||
);
|
||||
$border_block_styles[ $side ] = $border_side_values;
|
||||
}
|
||||
}
|
||||
|
||||
// Collect classes and styles.
|
||||
$attributes = array();
|
||||
$styles = wp_style_engine_get_styles( array( 'border' => $border_block_styles ) );
|
||||
|
||||
if ( ! empty( $styles['classnames'] ) ) {
|
||||
$attributes['class'] = $styles['classnames'];
|
||||
}
|
||||
|
||||
if ( ! empty( $styles['css'] ) ) {
|
||||
$attributes['style'] = $styles['css'];
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the current block type supports the border feature requested.
|
||||
*
|
||||
* If the `__experimentalBorder` support flag is a boolean `true` all border
|
||||
* support features are available. Otherwise, the specific feature's support
|
||||
* flag nested under `experimentalBorder` must be enabled for the feature
|
||||
* to be opted into.
|
||||
*
|
||||
* @since 5.8.0
|
||||
* @access private
|
||||
*
|
||||
* @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_value Fallback value for feature support, defaults to false.
|
||||
* @return bool Whether the feature is supported.
|
||||
*/
|
||||
function wp_has_border_feature_support( $block_type, $feature, $default_value = false ) {
|
||||
// Check if all border support features have been opted into via `"__experimentalBorder": true`.
|
||||
if ( $block_type instanceof WP_Block_Type ) {
|
||||
$block_type_supports_border = isset( $block_type->supports['__experimentalBorder'] )
|
||||
? $block_type->supports['__experimentalBorder']
|
||||
: $default_value;
|
||||
if ( true === $block_type_supports_border ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the specific feature has been opted into individually
|
||||
// via nested flag under `__experimentalBorder`.
|
||||
return block_has_support( $block_type, array( '__experimentalBorder', $feature ), $default_value );
|
||||
}
|
||||
|
||||
// Register the block support.
|
||||
WP_Block_Supports::get_instance()->register(
|
||||
'border',
|
||||
array(
|
||||
'register_attribute' => 'wp_register_border_support',
|
||||
'apply' => 'wp_apply_border_support',
|
||||
)
|
||||
);
|
||||
<?php
|
||||
/**
|
||||
* Border block support flag.
|
||||
*
|
||||
* @package WordPress
|
||||
* @since 5.8.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Registers the style attribute used by the border feature if needed for block
|
||||
* types that support borders.
|
||||
*
|
||||
* @since 5.8.0
|
||||
* @since 6.1.0 Improved conditional blocks optimization.
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block Type.
|
||||
*/
|
||||
function wp_register_border_support( $block_type ) {
|
||||
// Setup attributes and styles within that if needed.
|
||||
if ( ! $block_type->attributes ) {
|
||||
$block_type->attributes = array();
|
||||
}
|
||||
|
||||
if ( block_has_support( $block_type, '__experimentalBorder' ) && ! array_key_exists( 'style', $block_type->attributes ) ) {
|
||||
$block_type->attributes['style'] = array(
|
||||
'type' => 'object',
|
||||
);
|
||||
}
|
||||
|
||||
if ( wp_has_border_feature_support( $block_type, 'color' ) && ! array_key_exists( 'borderColor', $block_type->attributes ) ) {
|
||||
$block_type->attributes['borderColor'] = array(
|
||||
'type' => 'string',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds CSS classes and inline styles for border styles to the incoming
|
||||
* attributes array. This will be applied to the block markup in the front-end.
|
||||
*
|
||||
* @since 5.8.0
|
||||
* @since 6.1.0 Implemented the style engine to generate CSS and classnames.
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block type.
|
||||
* @param array $block_attributes Block attributes.
|
||||
* @return array Border CSS classes and inline styles.
|
||||
*/
|
||||
function wp_apply_border_support( $block_type, $block_attributes ) {
|
||||
if ( wp_should_skip_block_supports_serialization( $block_type, 'border' ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$border_block_styles = array();
|
||||
$has_border_color_support = wp_has_border_feature_support( $block_type, 'color' );
|
||||
$has_border_width_support = wp_has_border_feature_support( $block_type, 'width' );
|
||||
|
||||
// Border radius.
|
||||
if (
|
||||
wp_has_border_feature_support( $block_type, 'radius' ) &&
|
||||
isset( $block_attributes['style']['border']['radius'] ) &&
|
||||
! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'radius' )
|
||||
) {
|
||||
$border_radius = $block_attributes['style']['border']['radius'];
|
||||
|
||||
if ( is_numeric( $border_radius ) ) {
|
||||
$border_radius .= 'px';
|
||||
}
|
||||
|
||||
$border_block_styles['radius'] = $border_radius;
|
||||
}
|
||||
|
||||
// Border style.
|
||||
if (
|
||||
wp_has_border_feature_support( $block_type, 'style' ) &&
|
||||
isset( $block_attributes['style']['border']['style'] ) &&
|
||||
! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' )
|
||||
) {
|
||||
$border_block_styles['style'] = $block_attributes['style']['border']['style'];
|
||||
}
|
||||
|
||||
// Border width.
|
||||
if (
|
||||
$has_border_width_support &&
|
||||
isset( $block_attributes['style']['border']['width'] ) &&
|
||||
! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' )
|
||||
) {
|
||||
$border_width = $block_attributes['style']['border']['width'];
|
||||
|
||||
// This check handles original unitless implementation.
|
||||
if ( is_numeric( $border_width ) ) {
|
||||
$border_width .= 'px';
|
||||
}
|
||||
|
||||
$border_block_styles['width'] = $border_width;
|
||||
}
|
||||
|
||||
// Border color.
|
||||
if (
|
||||
$has_border_color_support &&
|
||||
! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' )
|
||||
) {
|
||||
$preset_border_color = array_key_exists( 'borderColor', $block_attributes ) ? "var:preset|color|{$block_attributes['borderColor']}" : null;
|
||||
$custom_border_color = isset( $block_attributes['style']['border']['color'] ) ? $block_attributes['style']['border']['color'] : null;
|
||||
$border_block_styles['color'] = $preset_border_color ? $preset_border_color : $custom_border_color;
|
||||
}
|
||||
|
||||
// Generates styles for individual border sides.
|
||||
if ( $has_border_color_support || $has_border_width_support ) {
|
||||
foreach ( array( 'top', 'right', 'bottom', 'left' ) as $side ) {
|
||||
$border = isset( $block_attributes['style']['border'][ $side ] ) ? $block_attributes['style']['border'][ $side ] : null;
|
||||
$border_side_values = array(
|
||||
'width' => isset( $border['width'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ) ? $border['width'] : null,
|
||||
'color' => isset( $border['color'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'color' ) ? $border['color'] : null,
|
||||
'style' => isset( $border['style'] ) && ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' ) ? $border['style'] : null,
|
||||
);
|
||||
$border_block_styles[ $side ] = $border_side_values;
|
||||
}
|
||||
}
|
||||
|
||||
// Collect classes and styles.
|
||||
$attributes = array();
|
||||
$styles = wp_style_engine_get_styles( array( 'border' => $border_block_styles ) );
|
||||
|
||||
if ( ! empty( $styles['classnames'] ) ) {
|
||||
$attributes['class'] = $styles['classnames'];
|
||||
}
|
||||
|
||||
if ( ! empty( $styles['css'] ) ) {
|
||||
$attributes['style'] = $styles['css'];
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the current block type supports the border feature requested.
|
||||
*
|
||||
* If the `__experimentalBorder` support flag is a boolean `true` all border
|
||||
* support features are available. Otherwise, the specific feature's support
|
||||
* flag nested under `experimentalBorder` must be enabled for the feature
|
||||
* to be opted into.
|
||||
*
|
||||
* @since 5.8.0
|
||||
* @access private
|
||||
*
|
||||
* @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_value Fallback value for feature support, defaults to false.
|
||||
* @return bool Whether the feature is supported.
|
||||
*/
|
||||
function wp_has_border_feature_support( $block_type, $feature, $default_value = false ) {
|
||||
// Check if all border support features have been opted into via `"__experimentalBorder": true`.
|
||||
if ( $block_type instanceof WP_Block_Type ) {
|
||||
$block_type_supports_border = isset( $block_type->supports['__experimentalBorder'] )
|
||||
? $block_type->supports['__experimentalBorder']
|
||||
: $default_value;
|
||||
if ( true === $block_type_supports_border ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Check if the specific feature has been opted into individually
|
||||
// via nested flag under `__experimentalBorder`.
|
||||
return block_has_support( $block_type, array( '__experimentalBorder', $feature ), $default_value );
|
||||
}
|
||||
|
||||
// Register the block support.
|
||||
WP_Block_Supports::get_instance()->register(
|
||||
'border',
|
||||
array(
|
||||
'register_attribute' => 'wp_register_border_support',
|
||||
'apply' => 'wp_apply_border_support',
|
||||
)
|
||||
);
|
||||
|
||||
@@ -1,144 +1,144 @@
|
||||
<?php
|
||||
/**
|
||||
* Colors block support flag.
|
||||
*
|
||||
* @package WordPress
|
||||
* @since 5.6.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Registers the style and colors block attributes for block types that support it.
|
||||
*
|
||||
* @since 5.6.0
|
||||
* @since 6.1.0 Improved $color_support assignment optimization.
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block Type.
|
||||
*/
|
||||
function wp_register_colors_support( $block_type ) {
|
||||
$color_support = false;
|
||||
if ( $block_type instanceof WP_Block_Type ) {
|
||||
$color_support = isset( $block_type->supports['color'] ) ? $block_type->supports['color'] : false;
|
||||
}
|
||||
$has_text_colors_support = true === $color_support ||
|
||||
( isset( $color_support['text'] ) && $color_support['text'] ) ||
|
||||
( is_array( $color_support ) && ! isset( $color_support['text'] ) );
|
||||
$has_background_colors_support = true === $color_support ||
|
||||
( isset( $color_support['background'] ) && $color_support['background'] ) ||
|
||||
( is_array( $color_support ) && ! isset( $color_support['background'] ) );
|
||||
$has_gradients_support = isset( $color_support['gradients'] ) ? $color_support['gradients'] : false;
|
||||
$has_link_colors_support = isset( $color_support['link'] ) ? $color_support['link'] : false;
|
||||
$has_button_colors_support = isset( $color_support['button'] ) ? $color_support['button'] : false;
|
||||
$has_heading_colors_support = isset( $color_support['heading'] ) ? $color_support['heading'] : false;
|
||||
$has_color_support = $has_text_colors_support ||
|
||||
$has_background_colors_support ||
|
||||
$has_gradients_support ||
|
||||
$has_link_colors_support ||
|
||||
$has_button_colors_support ||
|
||||
$has_heading_colors_support;
|
||||
|
||||
if ( ! $block_type->attributes ) {
|
||||
$block_type->attributes = array();
|
||||
}
|
||||
|
||||
if ( $has_color_support && ! array_key_exists( 'style', $block_type->attributes ) ) {
|
||||
$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',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds CSS classes and inline styles for colors to the incoming attributes array.
|
||||
* This will be applied to the block markup in the front-end.
|
||||
*
|
||||
* @since 5.6.0
|
||||
* @since 6.1.0 Implemented the style engine to generate CSS and classnames.
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block type.
|
||||
* @param array $block_attributes Block attributes.
|
||||
*
|
||||
* @return array Colors CSS classes and inline styles.
|
||||
*/
|
||||
function wp_apply_colors_support( $block_type, $block_attributes ) {
|
||||
$color_support = isset( $block_type->supports['color'] ) ? $block_type->supports['color'] : false;
|
||||
|
||||
if (
|
||||
is_array( $color_support ) &&
|
||||
wp_should_skip_block_supports_serialization( $block_type, 'color' )
|
||||
) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$has_text_colors_support = true === $color_support ||
|
||||
( isset( $color_support['text'] ) && $color_support['text'] ) ||
|
||||
( is_array( $color_support ) && ! isset( $color_support['text'] ) );
|
||||
$has_background_colors_support = true === $color_support ||
|
||||
( isset( $color_support['background'] ) && $color_support['background'] ) ||
|
||||
( is_array( $color_support ) && ! isset( $color_support['background'] ) );
|
||||
$has_gradients_support = isset( $color_support['gradients'] ) ? $color_support['gradients'] : false;
|
||||
$color_block_styles = array();
|
||||
|
||||
// Text colors.
|
||||
if ( $has_text_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'text' ) ) {
|
||||
$preset_text_color = array_key_exists( 'textColor', $block_attributes ) ? "var:preset|color|{$block_attributes['textColor']}" : null;
|
||||
$custom_text_color = isset( $block_attributes['style']['color']['text'] ) ? $block_attributes['style']['color']['text'] : null;
|
||||
$color_block_styles['text'] = $preset_text_color ? $preset_text_color : $custom_text_color;
|
||||
}
|
||||
|
||||
// Background colors.
|
||||
if ( $has_background_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'background' ) ) {
|
||||
$preset_background_color = array_key_exists( 'backgroundColor', $block_attributes ) ? "var:preset|color|{$block_attributes['backgroundColor']}" : null;
|
||||
$custom_background_color = isset( $block_attributes['style']['color']['background'] ) ? $block_attributes['style']['color']['background'] : null;
|
||||
$color_block_styles['background'] = $preset_background_color ? $preset_background_color : $custom_background_color;
|
||||
}
|
||||
|
||||
// Gradients.
|
||||
if ( $has_gradients_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'gradients' ) ) {
|
||||
$preset_gradient_color = array_key_exists( 'gradient', $block_attributes ) ? "var:preset|gradient|{$block_attributes['gradient']}" : null;
|
||||
$custom_gradient_color = isset( $block_attributes['style']['color']['gradient'] ) ? $block_attributes['style']['color']['gradient'] : null;
|
||||
$color_block_styles['gradient'] = $preset_gradient_color ? $preset_gradient_color : $custom_gradient_color;
|
||||
}
|
||||
|
||||
$attributes = array();
|
||||
$styles = wp_style_engine_get_styles( array( 'color' => $color_block_styles ), array( 'convert_vars_to_classnames' => true ) );
|
||||
|
||||
if ( ! empty( $styles['classnames'] ) ) {
|
||||
$attributes['class'] = $styles['classnames'];
|
||||
}
|
||||
|
||||
if ( ! empty( $styles['css'] ) ) {
|
||||
$attributes['style'] = $styles['css'];
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
// Register the block support.
|
||||
WP_Block_Supports::get_instance()->register(
|
||||
'colors',
|
||||
array(
|
||||
'register_attribute' => 'wp_register_colors_support',
|
||||
'apply' => 'wp_apply_colors_support',
|
||||
)
|
||||
);
|
||||
<?php
|
||||
/**
|
||||
* Colors block support flag.
|
||||
*
|
||||
* @package WordPress
|
||||
* @since 5.6.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Registers the style and colors block attributes for block types that support it.
|
||||
*
|
||||
* @since 5.6.0
|
||||
* @since 6.1.0 Improved $color_support assignment optimization.
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block Type.
|
||||
*/
|
||||
function wp_register_colors_support( $block_type ) {
|
||||
$color_support = false;
|
||||
if ( $block_type instanceof WP_Block_Type ) {
|
||||
$color_support = isset( $block_type->supports['color'] ) ? $block_type->supports['color'] : false;
|
||||
}
|
||||
$has_text_colors_support = true === $color_support ||
|
||||
( isset( $color_support['text'] ) && $color_support['text'] ) ||
|
||||
( is_array( $color_support ) && ! isset( $color_support['text'] ) );
|
||||
$has_background_colors_support = true === $color_support ||
|
||||
( isset( $color_support['background'] ) && $color_support['background'] ) ||
|
||||
( is_array( $color_support ) && ! isset( $color_support['background'] ) );
|
||||
$has_gradients_support = isset( $color_support['gradients'] ) ? $color_support['gradients'] : false;
|
||||
$has_link_colors_support = isset( $color_support['link'] ) ? $color_support['link'] : false;
|
||||
$has_button_colors_support = isset( $color_support['button'] ) ? $color_support['button'] : false;
|
||||
$has_heading_colors_support = isset( $color_support['heading'] ) ? $color_support['heading'] : false;
|
||||
$has_color_support = $has_text_colors_support ||
|
||||
$has_background_colors_support ||
|
||||
$has_gradients_support ||
|
||||
$has_link_colors_support ||
|
||||
$has_button_colors_support ||
|
||||
$has_heading_colors_support;
|
||||
|
||||
if ( ! $block_type->attributes ) {
|
||||
$block_type->attributes = array();
|
||||
}
|
||||
|
||||
if ( $has_color_support && ! array_key_exists( 'style', $block_type->attributes ) ) {
|
||||
$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',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Adds CSS classes and inline styles for colors to the incoming attributes array.
|
||||
* This will be applied to the block markup in the front-end.
|
||||
*
|
||||
* @since 5.6.0
|
||||
* @since 6.1.0 Implemented the style engine to generate CSS and classnames.
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block type.
|
||||
* @param array $block_attributes Block attributes.
|
||||
*
|
||||
* @return array Colors CSS classes and inline styles.
|
||||
*/
|
||||
function wp_apply_colors_support( $block_type, $block_attributes ) {
|
||||
$color_support = isset( $block_type->supports['color'] ) ? $block_type->supports['color'] : false;
|
||||
|
||||
if (
|
||||
is_array( $color_support ) &&
|
||||
wp_should_skip_block_supports_serialization( $block_type, 'color' )
|
||||
) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$has_text_colors_support = true === $color_support ||
|
||||
( isset( $color_support['text'] ) && $color_support['text'] ) ||
|
||||
( is_array( $color_support ) && ! isset( $color_support['text'] ) );
|
||||
$has_background_colors_support = true === $color_support ||
|
||||
( isset( $color_support['background'] ) && $color_support['background'] ) ||
|
||||
( is_array( $color_support ) && ! isset( $color_support['background'] ) );
|
||||
$has_gradients_support = isset( $color_support['gradients'] ) ? $color_support['gradients'] : false;
|
||||
$color_block_styles = array();
|
||||
|
||||
// Text colors.
|
||||
if ( $has_text_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'text' ) ) {
|
||||
$preset_text_color = array_key_exists( 'textColor', $block_attributes ) ? "var:preset|color|{$block_attributes['textColor']}" : null;
|
||||
$custom_text_color = isset( $block_attributes['style']['color']['text'] ) ? $block_attributes['style']['color']['text'] : null;
|
||||
$color_block_styles['text'] = $preset_text_color ? $preset_text_color : $custom_text_color;
|
||||
}
|
||||
|
||||
// Background colors.
|
||||
if ( $has_background_colors_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'background' ) ) {
|
||||
$preset_background_color = array_key_exists( 'backgroundColor', $block_attributes ) ? "var:preset|color|{$block_attributes['backgroundColor']}" : null;
|
||||
$custom_background_color = isset( $block_attributes['style']['color']['background'] ) ? $block_attributes['style']['color']['background'] : null;
|
||||
$color_block_styles['background'] = $preset_background_color ? $preset_background_color : $custom_background_color;
|
||||
}
|
||||
|
||||
// Gradients.
|
||||
if ( $has_gradients_support && ! wp_should_skip_block_supports_serialization( $block_type, 'color', 'gradients' ) ) {
|
||||
$preset_gradient_color = array_key_exists( 'gradient', $block_attributes ) ? "var:preset|gradient|{$block_attributes['gradient']}" : null;
|
||||
$custom_gradient_color = isset( $block_attributes['style']['color']['gradient'] ) ? $block_attributes['style']['color']['gradient'] : null;
|
||||
$color_block_styles['gradient'] = $preset_gradient_color ? $preset_gradient_color : $custom_gradient_color;
|
||||
}
|
||||
|
||||
$attributes = array();
|
||||
$styles = wp_style_engine_get_styles( array( 'color' => $color_block_styles ), array( 'convert_vars_to_classnames' => true ) );
|
||||
|
||||
if ( ! empty( $styles['classnames'] ) ) {
|
||||
$attributes['class'] = $styles['classnames'];
|
||||
}
|
||||
|
||||
if ( ! empty( $styles['css'] ) ) {
|
||||
$attributes['style'] = $styles['css'];
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
// Register the block support.
|
||||
WP_Block_Supports::get_instance()->register(
|
||||
'colors',
|
||||
array(
|
||||
'register_attribute' => 'wp_register_colors_support',
|
||||
'apply' => 'wp_apply_colors_support',
|
||||
)
|
||||
);
|
||||
|
||||
@@ -1,65 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
* Custom classname block support flag.
|
||||
*
|
||||
* @package WordPress
|
||||
* @since 5.6.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Registers the custom classname block attribute for block types that support it.
|
||||
*
|
||||
* @since 5.6.0
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block Type.
|
||||
*/
|
||||
function wp_register_custom_classname_support( $block_type ) {
|
||||
$has_custom_classname_support = block_has_support( $block_type, 'customClassName', true );
|
||||
|
||||
if ( $has_custom_classname_support ) {
|
||||
if ( ! $block_type->attributes ) {
|
||||
$block_type->attributes = array();
|
||||
}
|
||||
|
||||
if ( ! array_key_exists( 'className', $block_type->attributes ) ) {
|
||||
$block_type->attributes['className'] = array(
|
||||
'type' => 'string',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the custom classnames to the output.
|
||||
*
|
||||
* @since 5.6.0
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block Type.
|
||||
* @param array $block_attributes Block attributes.
|
||||
*
|
||||
* @return array Block CSS classes and inline styles.
|
||||
*/
|
||||
function wp_apply_custom_classname_support( $block_type, $block_attributes ) {
|
||||
$has_custom_classname_support = block_has_support( $block_type, 'customClassName', true );
|
||||
$attributes = array();
|
||||
if ( $has_custom_classname_support ) {
|
||||
$has_custom_classnames = array_key_exists( 'className', $block_attributes );
|
||||
|
||||
if ( $has_custom_classnames ) {
|
||||
$attributes['class'] = $block_attributes['className'];
|
||||
}
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
// Register the block support.
|
||||
WP_Block_Supports::get_instance()->register(
|
||||
'custom-classname',
|
||||
array(
|
||||
'register_attribute' => 'wp_register_custom_classname_support',
|
||||
'apply' => 'wp_apply_custom_classname_support',
|
||||
)
|
||||
);
|
||||
<?php
|
||||
/**
|
||||
* Custom classname block support flag.
|
||||
*
|
||||
* @package WordPress
|
||||
* @since 5.6.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Registers the custom classname block attribute for block types that support it.
|
||||
*
|
||||
* @since 5.6.0
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block Type.
|
||||
*/
|
||||
function wp_register_custom_classname_support( $block_type ) {
|
||||
$has_custom_classname_support = block_has_support( $block_type, 'customClassName', true );
|
||||
|
||||
if ( $has_custom_classname_support ) {
|
||||
if ( ! $block_type->attributes ) {
|
||||
$block_type->attributes = array();
|
||||
}
|
||||
|
||||
if ( ! array_key_exists( 'className', $block_type->attributes ) ) {
|
||||
$block_type->attributes['className'] = array(
|
||||
'type' => 'string',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the custom classnames to the output.
|
||||
*
|
||||
* @since 5.6.0
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block Type.
|
||||
* @param array $block_attributes Block attributes.
|
||||
*
|
||||
* @return array Block CSS classes and inline styles.
|
||||
*/
|
||||
function wp_apply_custom_classname_support( $block_type, $block_attributes ) {
|
||||
$has_custom_classname_support = block_has_support( $block_type, 'customClassName', true );
|
||||
$attributes = array();
|
||||
if ( $has_custom_classname_support ) {
|
||||
$has_custom_classnames = array_key_exists( 'className', $block_attributes );
|
||||
|
||||
if ( $has_custom_classnames ) {
|
||||
$attributes['class'] = $block_attributes['className'];
|
||||
}
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
// Register the block support.
|
||||
WP_Block_Supports::get_instance()->register(
|
||||
'custom-classname',
|
||||
array(
|
||||
'register_attribute' => 'wp_register_custom_classname_support',
|
||||
'apply' => 'wp_apply_custom_classname_support',
|
||||
)
|
||||
);
|
||||
|
||||
@@ -1,173 +1,173 @@
|
||||
<?php
|
||||
/**
|
||||
* Dimensions block support flag.
|
||||
*
|
||||
* This does not include the `spacing` block support even though that visually
|
||||
* appears under the "Dimensions" panel in the editor. It remains in its
|
||||
* original `spacing.php` file for compatibility with core.
|
||||
*
|
||||
* @package WordPress
|
||||
* @since 5.9.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Registers the style block attribute for block types that support it.
|
||||
*
|
||||
* @since 5.9.0
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block Type.
|
||||
*/
|
||||
function wp_register_dimensions_support( $block_type ) {
|
||||
// Setup attributes and styles within that if needed.
|
||||
if ( ! $block_type->attributes ) {
|
||||
$block_type->attributes = array();
|
||||
}
|
||||
|
||||
// Check for existing style attribute definition e.g. from block.json.
|
||||
if ( array_key_exists( 'style', $block_type->attributes ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$has_dimensions_support = block_has_support( $block_type, 'dimensions', false );
|
||||
|
||||
if ( $has_dimensions_support ) {
|
||||
$block_type->attributes['style'] = array(
|
||||
'type' => 'object',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds CSS classes for block dimensions to the incoming attributes array.
|
||||
* This will be applied to the block markup in the front-end.
|
||||
*
|
||||
* @since 5.9.0
|
||||
* @since 6.2.0 Added `minHeight` support.
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block Type.
|
||||
* @param array $block_attributes Block attributes.
|
||||
* @return array Block dimensions CSS classes and inline styles.
|
||||
*/
|
||||
function wp_apply_dimensions_support( $block_type, $block_attributes ) {
|
||||
if ( wp_should_skip_block_supports_serialization( $block_type, 'dimensions' ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$attributes = array();
|
||||
|
||||
// Width support to be added in near future.
|
||||
|
||||
$has_min_height_support = block_has_support( $block_type, array( 'dimensions', 'minHeight' ), false );
|
||||
$block_styles = isset( $block_attributes['style'] ) ? $block_attributes['style'] : null;
|
||||
|
||||
if ( ! $block_styles ) {
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
$skip_min_height = wp_should_skip_block_supports_serialization( $block_type, 'dimensions', 'minHeight' );
|
||||
$dimensions_block_styles = array();
|
||||
$dimensions_block_styles['minHeight'] = null;
|
||||
if ( $has_min_height_support && ! $skip_min_height ) {
|
||||
$dimensions_block_styles['minHeight'] = isset( $block_styles['dimensions']['minHeight'] )
|
||||
? $block_styles['dimensions']['minHeight']
|
||||
: null;
|
||||
}
|
||||
$styles = wp_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) );
|
||||
|
||||
if ( ! empty( $styles['css'] ) ) {
|
||||
$attributes['style'] = $styles['css'];
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders server-side dimensions styles to the block wrapper.
|
||||
* This block support uses the `render_block` hook to ensure that
|
||||
* it is also applied to non-server-rendered blocks.
|
||||
*
|
||||
* @since 6.5.0
|
||||
* @access private
|
||||
*
|
||||
* @param string $block_content Rendered block content.
|
||||
* @param array $block Block object.
|
||||
* @return string Filtered block content.
|
||||
*/
|
||||
function wp_render_dimensions_support( $block_content, $block ) {
|
||||
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
|
||||
$block_attributes = ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) ? $block['attrs'] : array();
|
||||
$has_aspect_ratio_support = block_has_support( $block_type, array( 'dimensions', 'aspectRatio' ), false );
|
||||
|
||||
if (
|
||||
! $has_aspect_ratio_support ||
|
||||
wp_should_skip_block_supports_serialization( $block_type, 'dimensions', 'aspectRatio' )
|
||||
) {
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
$dimensions_block_styles = array();
|
||||
$dimensions_block_styles['aspectRatio'] = $block_attributes['style']['dimensions']['aspectRatio'] ?? null;
|
||||
|
||||
// To ensure the aspect ratio does not get overridden by `minHeight` unset any existing rule.
|
||||
if (
|
||||
isset( $dimensions_block_styles['aspectRatio'] )
|
||||
) {
|
||||
$dimensions_block_styles['minHeight'] = 'unset';
|
||||
} elseif (
|
||||
isset( $block_attributes['style']['dimensions']['minHeight'] ) ||
|
||||
isset( $block_attributes['minHeight'] )
|
||||
) {
|
||||
$dimensions_block_styles['aspectRatio'] = 'unset';
|
||||
}
|
||||
|
||||
$styles = wp_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) );
|
||||
|
||||
if ( ! empty( $styles['css'] ) ) {
|
||||
// Inject dimensions styles to the first element, presuming it's the wrapper, if it exists.
|
||||
$tags = new WP_HTML_Tag_Processor( $block_content );
|
||||
|
||||
if ( $tags->next_tag() ) {
|
||||
$existing_style = $tags->get_attribute( 'style' );
|
||||
$updated_style = '';
|
||||
|
||||
if ( ! empty( $existing_style ) ) {
|
||||
$updated_style = $existing_style;
|
||||
if ( ! str_ends_with( $existing_style, ';' ) ) {
|
||||
$updated_style .= ';';
|
||||
}
|
||||
}
|
||||
|
||||
$updated_style .= $styles['css'];
|
||||
$tags->set_attribute( 'style', $updated_style );
|
||||
|
||||
if ( ! empty( $styles['classnames'] ) ) {
|
||||
foreach ( explode( ' ', $styles['classnames'] ) as $class_name ) {
|
||||
if (
|
||||
str_contains( $class_name, 'aspect-ratio' ) &&
|
||||
! isset( $block_attributes['style']['dimensions']['aspectRatio'] )
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
$tags->add_class( $class_name );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $tags->get_updated_html();
|
||||
}
|
||||
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
add_filter( 'render_block', 'wp_render_dimensions_support', 10, 2 );
|
||||
|
||||
// Register the block support.
|
||||
WP_Block_Supports::get_instance()->register(
|
||||
'dimensions',
|
||||
array(
|
||||
'register_attribute' => 'wp_register_dimensions_support',
|
||||
'apply' => 'wp_apply_dimensions_support',
|
||||
)
|
||||
);
|
||||
<?php
|
||||
/**
|
||||
* Dimensions block support flag.
|
||||
*
|
||||
* This does not include the `spacing` block support even though that visually
|
||||
* appears under the "Dimensions" panel in the editor. It remains in its
|
||||
* original `spacing.php` file for compatibility with core.
|
||||
*
|
||||
* @package WordPress
|
||||
* @since 5.9.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Registers the style block attribute for block types that support it.
|
||||
*
|
||||
* @since 5.9.0
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block Type.
|
||||
*/
|
||||
function wp_register_dimensions_support( $block_type ) {
|
||||
// Setup attributes and styles within that if needed.
|
||||
if ( ! $block_type->attributes ) {
|
||||
$block_type->attributes = array();
|
||||
}
|
||||
|
||||
// Check for existing style attribute definition e.g. from block.json.
|
||||
if ( array_key_exists( 'style', $block_type->attributes ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$has_dimensions_support = block_has_support( $block_type, 'dimensions', false );
|
||||
|
||||
if ( $has_dimensions_support ) {
|
||||
$block_type->attributes['style'] = array(
|
||||
'type' => 'object',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds CSS classes for block dimensions to the incoming attributes array.
|
||||
* This will be applied to the block markup in the front-end.
|
||||
*
|
||||
* @since 5.9.0
|
||||
* @since 6.2.0 Added `minHeight` support.
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block Type.
|
||||
* @param array $block_attributes Block attributes.
|
||||
* @return array Block dimensions CSS classes and inline styles.
|
||||
*/
|
||||
function wp_apply_dimensions_support( $block_type, $block_attributes ) {
|
||||
if ( wp_should_skip_block_supports_serialization( $block_type, 'dimensions' ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$attributes = array();
|
||||
|
||||
// Width support to be added in near future.
|
||||
|
||||
$has_min_height_support = block_has_support( $block_type, array( 'dimensions', 'minHeight' ), false );
|
||||
$block_styles = isset( $block_attributes['style'] ) ? $block_attributes['style'] : null;
|
||||
|
||||
if ( ! $block_styles ) {
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
$skip_min_height = wp_should_skip_block_supports_serialization( $block_type, 'dimensions', 'minHeight' );
|
||||
$dimensions_block_styles = array();
|
||||
$dimensions_block_styles['minHeight'] = null;
|
||||
if ( $has_min_height_support && ! $skip_min_height ) {
|
||||
$dimensions_block_styles['minHeight'] = isset( $block_styles['dimensions']['minHeight'] )
|
||||
? $block_styles['dimensions']['minHeight']
|
||||
: null;
|
||||
}
|
||||
$styles = wp_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) );
|
||||
|
||||
if ( ! empty( $styles['css'] ) ) {
|
||||
$attributes['style'] = $styles['css'];
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders server-side dimensions styles to the block wrapper.
|
||||
* This block support uses the `render_block` hook to ensure that
|
||||
* it is also applied to non-server-rendered blocks.
|
||||
*
|
||||
* @since 6.5.0
|
||||
* @access private
|
||||
*
|
||||
* @param string $block_content Rendered block content.
|
||||
* @param array $block Block object.
|
||||
* @return string Filtered block content.
|
||||
*/
|
||||
function wp_render_dimensions_support( $block_content, $block ) {
|
||||
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
|
||||
$block_attributes = ( isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) ? $block['attrs'] : array();
|
||||
$has_aspect_ratio_support = block_has_support( $block_type, array( 'dimensions', 'aspectRatio' ), false );
|
||||
|
||||
if (
|
||||
! $has_aspect_ratio_support ||
|
||||
wp_should_skip_block_supports_serialization( $block_type, 'dimensions', 'aspectRatio' )
|
||||
) {
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
$dimensions_block_styles = array();
|
||||
$dimensions_block_styles['aspectRatio'] = $block_attributes['style']['dimensions']['aspectRatio'] ?? null;
|
||||
|
||||
// To ensure the aspect ratio does not get overridden by `minHeight` unset any existing rule.
|
||||
if (
|
||||
isset( $dimensions_block_styles['aspectRatio'] )
|
||||
) {
|
||||
$dimensions_block_styles['minHeight'] = 'unset';
|
||||
} elseif (
|
||||
isset( $block_attributes['style']['dimensions']['minHeight'] ) ||
|
||||
isset( $block_attributes['minHeight'] )
|
||||
) {
|
||||
$dimensions_block_styles['aspectRatio'] = 'unset';
|
||||
}
|
||||
|
||||
$styles = wp_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) );
|
||||
|
||||
if ( ! empty( $styles['css'] ) ) {
|
||||
// Inject dimensions styles to the first element, presuming it's the wrapper, if it exists.
|
||||
$tags = new WP_HTML_Tag_Processor( $block_content );
|
||||
|
||||
if ( $tags->next_tag() ) {
|
||||
$existing_style = $tags->get_attribute( 'style' );
|
||||
$updated_style = '';
|
||||
|
||||
if ( ! empty( $existing_style ) ) {
|
||||
$updated_style = $existing_style;
|
||||
if ( ! str_ends_with( $existing_style, ';' ) ) {
|
||||
$updated_style .= ';';
|
||||
}
|
||||
}
|
||||
|
||||
$updated_style .= $styles['css'];
|
||||
$tags->set_attribute( 'style', $updated_style );
|
||||
|
||||
if ( ! empty( $styles['classnames'] ) ) {
|
||||
foreach ( explode( ' ', $styles['classnames'] ) as $class_name ) {
|
||||
if (
|
||||
str_contains( $class_name, 'aspect-ratio' ) &&
|
||||
! isset( $block_attributes['style']['dimensions']['aspectRatio'] )
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
$tags->add_class( $class_name );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $tags->get_updated_html();
|
||||
}
|
||||
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
add_filter( 'render_block', 'wp_render_dimensions_support', 10, 2 );
|
||||
|
||||
// Register the block support.
|
||||
WP_Block_Supports::get_instance()->register(
|
||||
'dimensions',
|
||||
array(
|
||||
'register_attribute' => 'wp_register_dimensions_support',
|
||||
'apply' => 'wp_apply_dimensions_support',
|
||||
)
|
||||
);
|
||||
|
||||
@@ -1,59 +1,59 @@
|
||||
<?php
|
||||
/**
|
||||
* Duotone block support flag.
|
||||
*
|
||||
* Parts of this source were derived and modified from TinyColor,
|
||||
* released under the MIT license.
|
||||
*
|
||||
* https://github.com/bgrins/TinyColor
|
||||
*
|
||||
* Copyright (c), Brian Grinstead, http://briangrinstead.com
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* @package WordPress
|
||||
* @since 5.8.0
|
||||
*/
|
||||
|
||||
// Register the block support.
|
||||
WP_Block_Supports::get_instance()->register(
|
||||
'duotone',
|
||||
array(
|
||||
'register_attribute' => array( 'WP_Duotone', 'register_duotone_support' ),
|
||||
)
|
||||
);
|
||||
|
||||
// Add classnames to blocks using duotone support.
|
||||
add_filter( 'render_block', array( 'WP_Duotone', 'render_duotone_support' ), 10, 3 );
|
||||
|
||||
// Enqueue styles.
|
||||
// Block styles (core-block-supports-inline-css) before the style engine (wp_enqueue_stored_styles).
|
||||
// Global styles (global-styles-inline-css) after the other global styles (wp_enqueue_global_styles).
|
||||
add_action( 'wp_enqueue_scripts', array( 'WP_Duotone', 'output_block_styles' ), 9 );
|
||||
add_action( 'wp_enqueue_scripts', array( 'WP_Duotone', 'output_global_styles' ), 11 );
|
||||
|
||||
// Add SVG filters to the footer. Also, for classic themes, output block styles (core-block-supports-inline-css).
|
||||
add_action( 'wp_footer', array( 'WP_Duotone', 'output_footer_assets' ), 10 );
|
||||
|
||||
// Add styles and SVGs for use in the editor via the EditorStyles component.
|
||||
add_filter( 'block_editor_settings_all', array( 'WP_Duotone', 'add_editor_settings' ), 10 );
|
||||
|
||||
// Migrate the old experimental duotone support flag.
|
||||
add_filter( 'block_type_metadata_settings', array( 'WP_Duotone', 'migrate_experimental_duotone_support_flag' ), 10, 2 );
|
||||
<?php
|
||||
/**
|
||||
* Duotone block support flag.
|
||||
*
|
||||
* Parts of this source were derived and modified from TinyColor,
|
||||
* released under the MIT license.
|
||||
*
|
||||
* https://github.com/bgrins/TinyColor
|
||||
*
|
||||
* Copyright (c), Brian Grinstead, http://briangrinstead.com
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* @package WordPress
|
||||
* @since 5.8.0
|
||||
*/
|
||||
|
||||
// Register the block support.
|
||||
WP_Block_Supports::get_instance()->register(
|
||||
'duotone',
|
||||
array(
|
||||
'register_attribute' => array( 'WP_Duotone', 'register_duotone_support' ),
|
||||
)
|
||||
);
|
||||
|
||||
// Add classnames to blocks using duotone support.
|
||||
add_filter( 'render_block', array( 'WP_Duotone', 'render_duotone_support' ), 10, 3 );
|
||||
|
||||
// Enqueue styles.
|
||||
// Block styles (core-block-supports-inline-css) before the style engine (wp_enqueue_stored_styles).
|
||||
// Global styles (global-styles-inline-css) after the other global styles (wp_enqueue_global_styles).
|
||||
add_action( 'wp_enqueue_scripts', array( 'WP_Duotone', 'output_block_styles' ), 9 );
|
||||
add_action( 'wp_enqueue_scripts', array( 'WP_Duotone', 'output_global_styles' ), 11 );
|
||||
|
||||
// Add SVG filters to the footer. Also, for classic themes, output block styles (core-block-supports-inline-css).
|
||||
add_action( 'wp_footer', array( 'WP_Duotone', 'output_footer_assets' ), 10 );
|
||||
|
||||
// Add styles and SVGs for use in the editor via the EditorStyles component.
|
||||
add_filter( 'block_editor_settings_all', array( 'WP_Duotone', 'add_editor_settings' ), 10 );
|
||||
|
||||
// Migrate the old experimental duotone support flag.
|
||||
add_filter( 'block_type_metadata_settings', array( 'WP_Duotone', 'migrate_experimental_duotone_support_flag' ), 10, 2 );
|
||||
|
||||
@@ -1,232 +1,232 @@
|
||||
<?php
|
||||
/**
|
||||
* Elements styles block support.
|
||||
*
|
||||
* @package WordPress
|
||||
* @since 5.8.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Gets the elements class names.
|
||||
*
|
||||
* @since 6.0.0
|
||||
* @access private
|
||||
*
|
||||
* @param array $block Block object.
|
||||
* @return string The unique class name.
|
||||
*/
|
||||
function wp_get_elements_class_name( $block ) {
|
||||
return 'wp-elements-' . md5( serialize( $block ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the block content with elements class names.
|
||||
*
|
||||
* @since 5.8.0
|
||||
* @since 6.4.0 Added support for button and heading element styling.
|
||||
* @access private
|
||||
*
|
||||
* @param string $block_content Rendered block content.
|
||||
* @param array $block Block object.
|
||||
* @return string Filtered block content.
|
||||
*/
|
||||
function wp_render_elements_support( $block_content, $block ) {
|
||||
if ( ! $block_content || ! isset( $block['attrs']['style']['elements'] ) ) {
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
|
||||
if ( ! $block_type ) {
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
$element_color_properties = array(
|
||||
'button' => array(
|
||||
'skip' => wp_should_skip_block_supports_serialization( $block_type, 'color', 'button' ),
|
||||
'paths' => array(
|
||||
array( 'button', 'color', 'text' ),
|
||||
array( 'button', 'color', 'background' ),
|
||||
array( 'button', 'color', 'gradient' ),
|
||||
),
|
||||
),
|
||||
'link' => array(
|
||||
'skip' => wp_should_skip_block_supports_serialization( $block_type, 'color', 'link' ),
|
||||
'paths' => array(
|
||||
array( 'link', 'color', 'text' ),
|
||||
array( 'link', ':hover', 'color', 'text' ),
|
||||
),
|
||||
),
|
||||
'heading' => array(
|
||||
'skip' => wp_should_skip_block_supports_serialization( $block_type, 'color', 'heading' ),
|
||||
'paths' => array(
|
||||
array( 'heading', 'color', 'text' ),
|
||||
array( 'heading', 'color', 'background' ),
|
||||
array( 'heading', 'color', 'gradient' ),
|
||||
array( 'h1', 'color', 'text' ),
|
||||
array( 'h1', 'color', 'background' ),
|
||||
array( 'h1', 'color', 'gradient' ),
|
||||
array( 'h2', 'color', 'text' ),
|
||||
array( 'h2', 'color', 'background' ),
|
||||
array( 'h2', 'color', 'gradient' ),
|
||||
array( 'h3', 'color', 'text' ),
|
||||
array( 'h3', 'color', 'background' ),
|
||||
array( 'h3', 'color', 'gradient' ),
|
||||
array( 'h4', 'color', 'text' ),
|
||||
array( 'h4', 'color', 'background' ),
|
||||
array( 'h4', 'color', 'gradient' ),
|
||||
array( 'h5', 'color', 'text' ),
|
||||
array( 'h5', 'color', 'background' ),
|
||||
array( 'h5', 'color', 'gradient' ),
|
||||
array( 'h6', 'color', 'text' ),
|
||||
array( 'h6', 'color', 'background' ),
|
||||
array( 'h6', 'color', 'gradient' ),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$skip_all_element_color_serialization = $element_color_properties['button']['skip'] &&
|
||||
$element_color_properties['link']['skip'] &&
|
||||
$element_color_properties['heading']['skip'];
|
||||
|
||||
if ( $skip_all_element_color_serialization ) {
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
$elements_style_attributes = $block['attrs']['style']['elements'];
|
||||
|
||||
foreach ( $element_color_properties as $element_config ) {
|
||||
if ( $element_config['skip'] ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ( $element_config['paths'] as $path ) {
|
||||
if ( null !== _wp_array_get( $elements_style_attributes, $path, null ) ) {
|
||||
/*
|
||||
* It only takes a single custom attribute to require that the custom
|
||||
* class name be added to the block, so once one is found there's no
|
||||
* need to continue looking for others.
|
||||
*
|
||||
* As is done with the layout hook, this code assumes that the block
|
||||
* contains a single wrapper and that it's the first element in the
|
||||
* rendered output. That first element, if it exists, gets the class.
|
||||
*/
|
||||
$tags = new WP_HTML_Tag_Processor( $block_content );
|
||||
if ( $tags->next_tag() ) {
|
||||
$tags->add_class( wp_get_elements_class_name( $block ) );
|
||||
}
|
||||
|
||||
return $tags->get_updated_html();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If no custom attributes were found then there's nothing to modify.
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the elements stylesheet.
|
||||
*
|
||||
* In the case of nested blocks we want the parent element styles to be rendered before their descendants.
|
||||
* This solves the issue of an element (e.g.: link color) being styled in both the parent and a descendant:
|
||||
* we want the descendant style to take priority, and this is done by loading it after, in DOM order.
|
||||
*
|
||||
* @since 6.0.0
|
||||
* @since 6.1.0 Implemented the style engine to generate CSS and classnames.
|
||||
* @access private
|
||||
*
|
||||
* @param string|null $pre_render The pre-rendered content. Default null.
|
||||
* @param array $block The block being rendered.
|
||||
* @return null
|
||||
*/
|
||||
function wp_render_elements_support_styles( $pre_render, $block ) {
|
||||
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
|
||||
$element_block_styles = isset( $block['attrs']['style']['elements'] ) ? $block['attrs']['style']['elements'] : null;
|
||||
|
||||
if ( ! $element_block_styles ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$skip_link_color_serialization = wp_should_skip_block_supports_serialization( $block_type, 'color', 'link' );
|
||||
$skip_heading_color_serialization = wp_should_skip_block_supports_serialization( $block_type, 'color', 'heading' );
|
||||
$skip_button_color_serialization = wp_should_skip_block_supports_serialization( $block_type, 'color', 'button' );
|
||||
$skips_all_element_color_serialization = $skip_link_color_serialization &&
|
||||
$skip_heading_color_serialization &&
|
||||
$skip_button_color_serialization;
|
||||
|
||||
if ( $skips_all_element_color_serialization ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$class_name = wp_get_elements_class_name( $block );
|
||||
|
||||
$element_types = array(
|
||||
'button' => array(
|
||||
'selector' => ".$class_name .wp-element-button, .$class_name .wp-block-button__link",
|
||||
'skip' => $skip_button_color_serialization,
|
||||
),
|
||||
'link' => array(
|
||||
'selector' => ".$class_name a:where(:not(.wp-element-button))",
|
||||
'hover_selector' => ".$class_name a:where(:not(.wp-element-button)):hover",
|
||||
'skip' => $skip_link_color_serialization,
|
||||
),
|
||||
'heading' => array(
|
||||
'selector' => ".$class_name h1, .$class_name h2, .$class_name h3, .$class_name h4, .$class_name h5, .$class_name h6",
|
||||
'skip' => $skip_heading_color_serialization,
|
||||
'elements' => array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ),
|
||||
),
|
||||
);
|
||||
|
||||
foreach ( $element_types as $element_type => $element_config ) {
|
||||
if ( $element_config['skip'] ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$element_style_object = isset( $element_block_styles[ $element_type ] ) ? $element_block_styles[ $element_type ] : null;
|
||||
|
||||
// Process primary element type styles.
|
||||
if ( $element_style_object ) {
|
||||
wp_style_engine_get_styles(
|
||||
$element_style_object,
|
||||
array(
|
||||
'selector' => $element_config['selector'],
|
||||
'context' => 'block-supports',
|
||||
)
|
||||
);
|
||||
|
||||
if ( isset( $element_style_object[':hover'] ) ) {
|
||||
wp_style_engine_get_styles(
|
||||
$element_style_object[':hover'],
|
||||
array(
|
||||
'selector' => $element_config['hover_selector'],
|
||||
'context' => 'block-supports',
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Process related elements e.g. h1-h6 for headings.
|
||||
if ( isset( $element_config['elements'] ) ) {
|
||||
foreach ( $element_config['elements'] as $element ) {
|
||||
$element_style_object = isset( $element_block_styles[ $element ] )
|
||||
? $element_block_styles[ $element ]
|
||||
: null;
|
||||
|
||||
if ( $element_style_object ) {
|
||||
wp_style_engine_get_styles(
|
||||
$element_style_object,
|
||||
array(
|
||||
'selector' => ".$class_name $element",
|
||||
'context' => 'block-supports',
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
add_filter( 'render_block', 'wp_render_elements_support', 10, 2 );
|
||||
add_filter( 'pre_render_block', 'wp_render_elements_support_styles', 10, 2 );
|
||||
<?php
|
||||
/**
|
||||
* Elements styles block support.
|
||||
*
|
||||
* @package WordPress
|
||||
* @since 5.8.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Gets the elements class names.
|
||||
*
|
||||
* @since 6.0.0
|
||||
* @access private
|
||||
*
|
||||
* @param array $block Block object.
|
||||
* @return string The unique class name.
|
||||
*/
|
||||
function wp_get_elements_class_name( $block ) {
|
||||
return 'wp-elements-' . md5( serialize( $block ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the block content with elements class names.
|
||||
*
|
||||
* @since 5.8.0
|
||||
* @since 6.4.0 Added support for button and heading element styling.
|
||||
* @access private
|
||||
*
|
||||
* @param string $block_content Rendered block content.
|
||||
* @param array $block Block object.
|
||||
* @return string Filtered block content.
|
||||
*/
|
||||
function wp_render_elements_support( $block_content, $block ) {
|
||||
if ( ! $block_content || ! isset( $block['attrs']['style']['elements'] ) ) {
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
|
||||
if ( ! $block_type ) {
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
$element_color_properties = array(
|
||||
'button' => array(
|
||||
'skip' => wp_should_skip_block_supports_serialization( $block_type, 'color', 'button' ),
|
||||
'paths' => array(
|
||||
array( 'button', 'color', 'text' ),
|
||||
array( 'button', 'color', 'background' ),
|
||||
array( 'button', 'color', 'gradient' ),
|
||||
),
|
||||
),
|
||||
'link' => array(
|
||||
'skip' => wp_should_skip_block_supports_serialization( $block_type, 'color', 'link' ),
|
||||
'paths' => array(
|
||||
array( 'link', 'color', 'text' ),
|
||||
array( 'link', ':hover', 'color', 'text' ),
|
||||
),
|
||||
),
|
||||
'heading' => array(
|
||||
'skip' => wp_should_skip_block_supports_serialization( $block_type, 'color', 'heading' ),
|
||||
'paths' => array(
|
||||
array( 'heading', 'color', 'text' ),
|
||||
array( 'heading', 'color', 'background' ),
|
||||
array( 'heading', 'color', 'gradient' ),
|
||||
array( 'h1', 'color', 'text' ),
|
||||
array( 'h1', 'color', 'background' ),
|
||||
array( 'h1', 'color', 'gradient' ),
|
||||
array( 'h2', 'color', 'text' ),
|
||||
array( 'h2', 'color', 'background' ),
|
||||
array( 'h2', 'color', 'gradient' ),
|
||||
array( 'h3', 'color', 'text' ),
|
||||
array( 'h3', 'color', 'background' ),
|
||||
array( 'h3', 'color', 'gradient' ),
|
||||
array( 'h4', 'color', 'text' ),
|
||||
array( 'h4', 'color', 'background' ),
|
||||
array( 'h4', 'color', 'gradient' ),
|
||||
array( 'h5', 'color', 'text' ),
|
||||
array( 'h5', 'color', 'background' ),
|
||||
array( 'h5', 'color', 'gradient' ),
|
||||
array( 'h6', 'color', 'text' ),
|
||||
array( 'h6', 'color', 'background' ),
|
||||
array( 'h6', 'color', 'gradient' ),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$skip_all_element_color_serialization = $element_color_properties['button']['skip'] &&
|
||||
$element_color_properties['link']['skip'] &&
|
||||
$element_color_properties['heading']['skip'];
|
||||
|
||||
if ( $skip_all_element_color_serialization ) {
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
$elements_style_attributes = $block['attrs']['style']['elements'];
|
||||
|
||||
foreach ( $element_color_properties as $element_config ) {
|
||||
if ( $element_config['skip'] ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ( $element_config['paths'] as $path ) {
|
||||
if ( null !== _wp_array_get( $elements_style_attributes, $path, null ) ) {
|
||||
/*
|
||||
* It only takes a single custom attribute to require that the custom
|
||||
* class name be added to the block, so once one is found there's no
|
||||
* need to continue looking for others.
|
||||
*
|
||||
* As is done with the layout hook, this code assumes that the block
|
||||
* contains a single wrapper and that it's the first element in the
|
||||
* rendered output. That first element, if it exists, gets the class.
|
||||
*/
|
||||
$tags = new WP_HTML_Tag_Processor( $block_content );
|
||||
if ( $tags->next_tag() ) {
|
||||
$tags->add_class( wp_get_elements_class_name( $block ) );
|
||||
}
|
||||
|
||||
return $tags->get_updated_html();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If no custom attributes were found then there's nothing to modify.
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the elements stylesheet.
|
||||
*
|
||||
* In the case of nested blocks we want the parent element styles to be rendered before their descendants.
|
||||
* This solves the issue of an element (e.g.: link color) being styled in both the parent and a descendant:
|
||||
* we want the descendant style to take priority, and this is done by loading it after, in DOM order.
|
||||
*
|
||||
* @since 6.0.0
|
||||
* @since 6.1.0 Implemented the style engine to generate CSS and classnames.
|
||||
* @access private
|
||||
*
|
||||
* @param string|null $pre_render The pre-rendered content. Default null.
|
||||
* @param array $block The block being rendered.
|
||||
* @return null
|
||||
*/
|
||||
function wp_render_elements_support_styles( $pre_render, $block ) {
|
||||
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
|
||||
$element_block_styles = isset( $block['attrs']['style']['elements'] ) ? $block['attrs']['style']['elements'] : null;
|
||||
|
||||
if ( ! $element_block_styles ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$skip_link_color_serialization = wp_should_skip_block_supports_serialization( $block_type, 'color', 'link' );
|
||||
$skip_heading_color_serialization = wp_should_skip_block_supports_serialization( $block_type, 'color', 'heading' );
|
||||
$skip_button_color_serialization = wp_should_skip_block_supports_serialization( $block_type, 'color', 'button' );
|
||||
$skips_all_element_color_serialization = $skip_link_color_serialization &&
|
||||
$skip_heading_color_serialization &&
|
||||
$skip_button_color_serialization;
|
||||
|
||||
if ( $skips_all_element_color_serialization ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$class_name = wp_get_elements_class_name( $block );
|
||||
|
||||
$element_types = array(
|
||||
'button' => array(
|
||||
'selector' => ".$class_name .wp-element-button, .$class_name .wp-block-button__link",
|
||||
'skip' => $skip_button_color_serialization,
|
||||
),
|
||||
'link' => array(
|
||||
'selector' => ".$class_name a:where(:not(.wp-element-button))",
|
||||
'hover_selector' => ".$class_name a:where(:not(.wp-element-button)):hover",
|
||||
'skip' => $skip_link_color_serialization,
|
||||
),
|
||||
'heading' => array(
|
||||
'selector' => ".$class_name h1, .$class_name h2, .$class_name h3, .$class_name h4, .$class_name h5, .$class_name h6",
|
||||
'skip' => $skip_heading_color_serialization,
|
||||
'elements' => array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ),
|
||||
),
|
||||
);
|
||||
|
||||
foreach ( $element_types as $element_type => $element_config ) {
|
||||
if ( $element_config['skip'] ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$element_style_object = isset( $element_block_styles[ $element_type ] ) ? $element_block_styles[ $element_type ] : null;
|
||||
|
||||
// Process primary element type styles.
|
||||
if ( $element_style_object ) {
|
||||
wp_style_engine_get_styles(
|
||||
$element_style_object,
|
||||
array(
|
||||
'selector' => $element_config['selector'],
|
||||
'context' => 'block-supports',
|
||||
)
|
||||
);
|
||||
|
||||
if ( isset( $element_style_object[':hover'] ) ) {
|
||||
wp_style_engine_get_styles(
|
||||
$element_style_object[':hover'],
|
||||
array(
|
||||
'selector' => $element_config['hover_selector'],
|
||||
'context' => 'block-supports',
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Process related elements e.g. h1-h6 for headings.
|
||||
if ( isset( $element_config['elements'] ) ) {
|
||||
foreach ( $element_config['elements'] as $element ) {
|
||||
$element_style_object = isset( $element_block_styles[ $element ] )
|
||||
? $element_block_styles[ $element ]
|
||||
: null;
|
||||
|
||||
if ( $element_style_object ) {
|
||||
wp_style_engine_get_styles(
|
||||
$element_style_object,
|
||||
array(
|
||||
'selector' => ".$class_name $element",
|
||||
'context' => 'block-supports',
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
add_filter( 'render_block', 'wp_render_elements_support', 10, 2 );
|
||||
add_filter( 'pre_render_block', 'wp_render_elements_support_styles', 10, 2 );
|
||||
|
||||
@@ -1,71 +1,71 @@
|
||||
<?php
|
||||
/**
|
||||
* Generated classname block support flag.
|
||||
*
|
||||
* @package WordPress
|
||||
* @since 5.6.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Gets the generated classname from a given block name.
|
||||
*
|
||||
* @since 5.6.0
|
||||
*
|
||||
* @access private
|
||||
*
|
||||
* @param string $block_name Block Name.
|
||||
* @return string Generated classname.
|
||||
*/
|
||||
function wp_get_block_default_classname( $block_name ) {
|
||||
// Generated HTML classes for blocks follow the `wp-block-{name}` nomenclature.
|
||||
// Blocks provided by WordPress drop the prefixes 'core/' or 'core-' (historically used in 'core-embed/').
|
||||
$classname = 'wp-block-' . preg_replace(
|
||||
'/^core-/',
|
||||
'',
|
||||
str_replace( '/', '-', $block_name )
|
||||
);
|
||||
|
||||
/**
|
||||
* Filters the default block className for server rendered blocks.
|
||||
*
|
||||
* @since 5.6.0
|
||||
*
|
||||
* @param string $class_name The current applied classname.
|
||||
* @param string $block_name The block name.
|
||||
*/
|
||||
$classname = apply_filters( 'block_default_classname', $classname, $block_name );
|
||||
|
||||
return $classname;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the generated classnames to the output.
|
||||
*
|
||||
* @since 5.6.0
|
||||
*
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block Type.
|
||||
* @return array Block CSS classes and inline styles.
|
||||
*/
|
||||
function wp_apply_generated_classname_support( $block_type ) {
|
||||
$attributes = array();
|
||||
$has_generated_classname_support = block_has_support( $block_type, 'className', true );
|
||||
if ( $has_generated_classname_support ) {
|
||||
$block_classname = wp_get_block_default_classname( $block_type->name );
|
||||
|
||||
if ( $block_classname ) {
|
||||
$attributes['class'] = $block_classname;
|
||||
}
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
// Register the block support.
|
||||
WP_Block_Supports::get_instance()->register(
|
||||
'generated-classname',
|
||||
array(
|
||||
'apply' => 'wp_apply_generated_classname_support',
|
||||
)
|
||||
);
|
||||
<?php
|
||||
/**
|
||||
* Generated classname block support flag.
|
||||
*
|
||||
* @package WordPress
|
||||
* @since 5.6.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Gets the generated classname from a given block name.
|
||||
*
|
||||
* @since 5.6.0
|
||||
*
|
||||
* @access private
|
||||
*
|
||||
* @param string $block_name Block Name.
|
||||
* @return string Generated classname.
|
||||
*/
|
||||
function wp_get_block_default_classname( $block_name ) {
|
||||
// Generated HTML classes for blocks follow the `wp-block-{name}` nomenclature.
|
||||
// Blocks provided by WordPress drop the prefixes 'core/' or 'core-' (historically used in 'core-embed/').
|
||||
$classname = 'wp-block-' . preg_replace(
|
||||
'/^core-/',
|
||||
'',
|
||||
str_replace( '/', '-', $block_name )
|
||||
);
|
||||
|
||||
/**
|
||||
* Filters the default block className for server rendered blocks.
|
||||
*
|
||||
* @since 5.6.0
|
||||
*
|
||||
* @param string $class_name The current applied classname.
|
||||
* @param string $block_name The block name.
|
||||
*/
|
||||
$classname = apply_filters( 'block_default_classname', $classname, $block_name );
|
||||
|
||||
return $classname;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the generated classnames to the output.
|
||||
*
|
||||
* @since 5.6.0
|
||||
*
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block Type.
|
||||
* @return array Block CSS classes and inline styles.
|
||||
*/
|
||||
function wp_apply_generated_classname_support( $block_type ) {
|
||||
$attributes = array();
|
||||
$has_generated_classname_support = block_has_support( $block_type, 'className', true );
|
||||
if ( $has_generated_classname_support ) {
|
||||
$block_classname = wp_get_block_default_classname( $block_type->name );
|
||||
|
||||
if ( $block_classname ) {
|
||||
$attributes['class'] = $block_classname;
|
||||
}
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
// Register the block support.
|
||||
WP_Block_Supports::get_instance()->register(
|
||||
'generated-classname',
|
||||
array(
|
||||
'apply' => 'wp_apply_generated_classname_support',
|
||||
)
|
||||
);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,151 +1,151 @@
|
||||
<?php
|
||||
/**
|
||||
* Position block support flag.
|
||||
*
|
||||
* @package WordPress
|
||||
* @since 6.2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Registers the style block attribute for block types that support it.
|
||||
*
|
||||
* @since 6.2.0
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block Type.
|
||||
*/
|
||||
function wp_register_position_support( $block_type ) {
|
||||
$has_position_support = block_has_support( $block_type, 'position', false );
|
||||
|
||||
// Set up attributes and styles within that if needed.
|
||||
if ( ! $block_type->attributes ) {
|
||||
$block_type->attributes = array();
|
||||
}
|
||||
|
||||
if ( $has_position_support && ! array_key_exists( 'style', $block_type->attributes ) ) {
|
||||
$block_type->attributes['style'] = array(
|
||||
'type' => 'object',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders position styles to the block wrapper.
|
||||
*
|
||||
* @since 6.2.0
|
||||
* @access private
|
||||
*
|
||||
* @param string $block_content Rendered block content.
|
||||
* @param array $block Block object.
|
||||
* @return string Filtered block content.
|
||||
*/
|
||||
function wp_render_position_support( $block_content, $block ) {
|
||||
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
|
||||
$has_position_support = block_has_support( $block_type, 'position', false );
|
||||
|
||||
if (
|
||||
! $has_position_support ||
|
||||
empty( $block['attrs']['style']['position'] )
|
||||
) {
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
$global_settings = wp_get_global_settings();
|
||||
$theme_has_sticky_support = isset( $global_settings['position']['sticky'] ) ? $global_settings['position']['sticky'] : false;
|
||||
$theme_has_fixed_support = isset( $global_settings['position']['fixed'] ) ? $global_settings['position']['fixed'] : false;
|
||||
|
||||
// Only allow output for position types that the theme supports.
|
||||
$allowed_position_types = array();
|
||||
if ( true === $theme_has_sticky_support ) {
|
||||
$allowed_position_types[] = 'sticky';
|
||||
}
|
||||
if ( true === $theme_has_fixed_support ) {
|
||||
$allowed_position_types[] = 'fixed';
|
||||
}
|
||||
|
||||
$style_attribute = isset( $block['attrs']['style'] ) ? $block['attrs']['style'] : null;
|
||||
$class_name = wp_unique_id( 'wp-container-' );
|
||||
$selector = ".$class_name";
|
||||
$position_styles = array();
|
||||
$position_type = isset( $style_attribute['position']['type'] ) ? $style_attribute['position']['type'] : '';
|
||||
$wrapper_classes = array();
|
||||
|
||||
if (
|
||||
in_array( $position_type, $allowed_position_types, true )
|
||||
) {
|
||||
$wrapper_classes[] = $class_name;
|
||||
$wrapper_classes[] = 'is-position-' . $position_type;
|
||||
$sides = array( 'top', 'right', 'bottom', 'left' );
|
||||
|
||||
foreach ( $sides as $side ) {
|
||||
$side_value = isset( $style_attribute['position'][ $side ] ) ? $style_attribute['position'][ $side ] : null;
|
||||
if ( null !== $side_value ) {
|
||||
/*
|
||||
* For fixed or sticky top positions,
|
||||
* ensure the value includes an offset for the logged in admin bar.
|
||||
*/
|
||||
if (
|
||||
'top' === $side &&
|
||||
( 'fixed' === $position_type || 'sticky' === $position_type )
|
||||
) {
|
||||
// Ensure 0 values can be used in `calc()` calculations.
|
||||
if ( '0' === $side_value || 0 === $side_value ) {
|
||||
$side_value = '0px';
|
||||
}
|
||||
|
||||
// Ensure current side value also factors in the height of the logged in admin bar.
|
||||
$side_value = "calc($side_value + var(--wp-admin--admin-bar--position-offset, 0px))";
|
||||
}
|
||||
|
||||
$position_styles[] =
|
||||
array(
|
||||
'selector' => $selector,
|
||||
'declarations' => array(
|
||||
$side => $side_value,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$position_styles[] =
|
||||
array(
|
||||
'selector' => $selector,
|
||||
'declarations' => array(
|
||||
'position' => $position_type,
|
||||
'z-index' => '10',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if ( ! empty( $position_styles ) ) {
|
||||
/*
|
||||
* Add to the style engine store to enqueue and render position styles.
|
||||
*/
|
||||
wp_style_engine_get_stylesheet_from_css_rules(
|
||||
$position_styles,
|
||||
array(
|
||||
'context' => 'block-supports',
|
||||
'prettify' => false,
|
||||
)
|
||||
);
|
||||
|
||||
// Inject class name to block container markup.
|
||||
$content = new WP_HTML_Tag_Processor( $block_content );
|
||||
$content->next_tag();
|
||||
foreach ( $wrapper_classes as $class ) {
|
||||
$content->add_class( $class );
|
||||
}
|
||||
return (string) $content;
|
||||
}
|
||||
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
// Register the block support.
|
||||
WP_Block_Supports::get_instance()->register(
|
||||
'position',
|
||||
array(
|
||||
'register_attribute' => 'wp_register_position_support',
|
||||
)
|
||||
);
|
||||
add_filter( 'render_block', 'wp_render_position_support', 10, 2 );
|
||||
<?php
|
||||
/**
|
||||
* Position block support flag.
|
||||
*
|
||||
* @package WordPress
|
||||
* @since 6.2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Registers the style block attribute for block types that support it.
|
||||
*
|
||||
* @since 6.2.0
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block Type.
|
||||
*/
|
||||
function wp_register_position_support( $block_type ) {
|
||||
$has_position_support = block_has_support( $block_type, 'position', false );
|
||||
|
||||
// Set up attributes and styles within that if needed.
|
||||
if ( ! $block_type->attributes ) {
|
||||
$block_type->attributes = array();
|
||||
}
|
||||
|
||||
if ( $has_position_support && ! array_key_exists( 'style', $block_type->attributes ) ) {
|
||||
$block_type->attributes['style'] = array(
|
||||
'type' => 'object',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders position styles to the block wrapper.
|
||||
*
|
||||
* @since 6.2.0
|
||||
* @access private
|
||||
*
|
||||
* @param string $block_content Rendered block content.
|
||||
* @param array $block Block object.
|
||||
* @return string Filtered block content.
|
||||
*/
|
||||
function wp_render_position_support( $block_content, $block ) {
|
||||
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
|
||||
$has_position_support = block_has_support( $block_type, 'position', false );
|
||||
|
||||
if (
|
||||
! $has_position_support ||
|
||||
empty( $block['attrs']['style']['position'] )
|
||||
) {
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
$global_settings = wp_get_global_settings();
|
||||
$theme_has_sticky_support = isset( $global_settings['position']['sticky'] ) ? $global_settings['position']['sticky'] : false;
|
||||
$theme_has_fixed_support = isset( $global_settings['position']['fixed'] ) ? $global_settings['position']['fixed'] : false;
|
||||
|
||||
// Only allow output for position types that the theme supports.
|
||||
$allowed_position_types = array();
|
||||
if ( true === $theme_has_sticky_support ) {
|
||||
$allowed_position_types[] = 'sticky';
|
||||
}
|
||||
if ( true === $theme_has_fixed_support ) {
|
||||
$allowed_position_types[] = 'fixed';
|
||||
}
|
||||
|
||||
$style_attribute = isset( $block['attrs']['style'] ) ? $block['attrs']['style'] : null;
|
||||
$class_name = wp_unique_id( 'wp-container-' );
|
||||
$selector = ".$class_name";
|
||||
$position_styles = array();
|
||||
$position_type = isset( $style_attribute['position']['type'] ) ? $style_attribute['position']['type'] : '';
|
||||
$wrapper_classes = array();
|
||||
|
||||
if (
|
||||
in_array( $position_type, $allowed_position_types, true )
|
||||
) {
|
||||
$wrapper_classes[] = $class_name;
|
||||
$wrapper_classes[] = 'is-position-' . $position_type;
|
||||
$sides = array( 'top', 'right', 'bottom', 'left' );
|
||||
|
||||
foreach ( $sides as $side ) {
|
||||
$side_value = isset( $style_attribute['position'][ $side ] ) ? $style_attribute['position'][ $side ] : null;
|
||||
if ( null !== $side_value ) {
|
||||
/*
|
||||
* For fixed or sticky top positions,
|
||||
* ensure the value includes an offset for the logged in admin bar.
|
||||
*/
|
||||
if (
|
||||
'top' === $side &&
|
||||
( 'fixed' === $position_type || 'sticky' === $position_type )
|
||||
) {
|
||||
// Ensure 0 values can be used in `calc()` calculations.
|
||||
if ( '0' === $side_value || 0 === $side_value ) {
|
||||
$side_value = '0px';
|
||||
}
|
||||
|
||||
// Ensure current side value also factors in the height of the logged in admin bar.
|
||||
$side_value = "calc($side_value + var(--wp-admin--admin-bar--position-offset, 0px))";
|
||||
}
|
||||
|
||||
$position_styles[] =
|
||||
array(
|
||||
'selector' => $selector,
|
||||
'declarations' => array(
|
||||
$side => $side_value,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$position_styles[] =
|
||||
array(
|
||||
'selector' => $selector,
|
||||
'declarations' => array(
|
||||
'position' => $position_type,
|
||||
'z-index' => '10',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if ( ! empty( $position_styles ) ) {
|
||||
/*
|
||||
* Add to the style engine store to enqueue and render position styles.
|
||||
*/
|
||||
wp_style_engine_get_stylesheet_from_css_rules(
|
||||
$position_styles,
|
||||
array(
|
||||
'context' => 'block-supports',
|
||||
'prettify' => false,
|
||||
)
|
||||
);
|
||||
|
||||
// Inject class name to block container markup.
|
||||
$content = new WP_HTML_Tag_Processor( $block_content );
|
||||
$content->next_tag();
|
||||
foreach ( $wrapper_classes as $class ) {
|
||||
$content->add_class( $class );
|
||||
}
|
||||
return (string) $content;
|
||||
}
|
||||
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
// Register the block support.
|
||||
WP_Block_Supports::get_instance()->register(
|
||||
'position',
|
||||
array(
|
||||
'register_attribute' => 'wp_register_position_support',
|
||||
)
|
||||
);
|
||||
add_filter( 'render_block', 'wp_render_position_support', 10, 2 );
|
||||
|
||||
@@ -1,152 +1,152 @@
|
||||
<?php
|
||||
/**
|
||||
* Block level presets support.
|
||||
*
|
||||
* @package WordPress
|
||||
* @since 6.2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Get the class name used on block level presets.
|
||||
*
|
||||
* @internal
|
||||
*
|
||||
* @since 6.2.0
|
||||
* @access private
|
||||
*
|
||||
* @param array $block Block object.
|
||||
* @return string The unique class name.
|
||||
*/
|
||||
function _wp_get_presets_class_name( $block ) {
|
||||
return 'wp-settings-' . md5( serialize( $block ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the block content with block level presets class name.
|
||||
*
|
||||
* @internal
|
||||
*
|
||||
* @since 6.2.0
|
||||
* @access private
|
||||
*
|
||||
* @param string $block_content Rendered block content.
|
||||
* @param array $block Block object.
|
||||
* @return string Filtered block content.
|
||||
*/
|
||||
function _wp_add_block_level_presets_class( $block_content, $block ) {
|
||||
if ( ! $block_content ) {
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
// return early if the block doesn't have support for settings.
|
||||
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
|
||||
if ( ! block_has_support( $block_type, '__experimentalSettings', false ) ) {
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
// return early if no settings are found on the block attributes.
|
||||
$block_settings = isset( $block['attrs']['settings'] ) ? $block['attrs']['settings'] : null;
|
||||
if ( empty( $block_settings ) ) {
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
// Like the layout hook this assumes the hook only applies to blocks with a single wrapper.
|
||||
// Add the class name to the first element, presuming it's the wrapper, if it exists.
|
||||
$tags = new WP_HTML_Tag_Processor( $block_content );
|
||||
if ( $tags->next_tag() ) {
|
||||
$tags->add_class( _wp_get_presets_class_name( $block ) );
|
||||
}
|
||||
|
||||
return $tags->get_updated_html();
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the block level presets stylesheet.
|
||||
*
|
||||
* @internal
|
||||
*
|
||||
* @since 6.2.0
|
||||
* @since 6.3.0 Updated preset styles to use Selectors API.
|
||||
* @access private
|
||||
*
|
||||
* @param string|null $pre_render The pre-rendered content. Default null.
|
||||
* @param array $block The block being rendered.
|
||||
*
|
||||
* @return null
|
||||
*/
|
||||
function _wp_add_block_level_preset_styles( $pre_render, $block ) {
|
||||
// Return early if the block has not support for descendent block styles.
|
||||
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
|
||||
if ( ! block_has_support( $block_type, '__experimentalSettings', false ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// return early if no settings are found on the block attributes.
|
||||
$block_settings = isset( $block['attrs']['settings'] ) ? $block['attrs']['settings'] : null;
|
||||
if ( empty( $block_settings ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$class_name = '.' . _wp_get_presets_class_name( $block );
|
||||
|
||||
// the root selector for preset variables needs to target every possible block selector
|
||||
// in order for the general setting to override any bock specific setting of a parent block or
|
||||
// the site root.
|
||||
$variables_root_selector = '*,[class*="wp-block"]';
|
||||
$registry = WP_Block_Type_Registry::get_instance();
|
||||
$blocks = $registry->get_all_registered();
|
||||
foreach ( $blocks as $block_type ) {
|
||||
/*
|
||||
* We only want to append selectors for blocks using custom selectors
|
||||
* i.e. not `wp-block-<name>`.
|
||||
*/
|
||||
$has_custom_selector =
|
||||
( isset( $block_type->supports['__experimentalSelector'] ) && is_string( $block_type->supports['__experimentalSelector'] ) ) ||
|
||||
( isset( $block_type->selectors['root'] ) && is_string( $block_type->selectors['root'] ) );
|
||||
|
||||
if ( $has_custom_selector ) {
|
||||
$variables_root_selector .= ',' . wp_get_block_css_selector( $block_type );
|
||||
}
|
||||
}
|
||||
$variables_root_selector = WP_Theme_JSON::scope_selector( $class_name, $variables_root_selector );
|
||||
|
||||
// Remove any potentially unsafe styles.
|
||||
$theme_json_shape = WP_Theme_JSON::remove_insecure_properties(
|
||||
array(
|
||||
'version' => WP_Theme_JSON::LATEST_SCHEMA,
|
||||
'settings' => $block_settings,
|
||||
)
|
||||
);
|
||||
$theme_json_object = new WP_Theme_JSON( $theme_json_shape );
|
||||
|
||||
$styles = '';
|
||||
|
||||
// include preset css variables declaration on the stylesheet.
|
||||
$styles .= $theme_json_object->get_stylesheet(
|
||||
array( 'variables' ),
|
||||
null,
|
||||
array(
|
||||
'root_selector' => $variables_root_selector,
|
||||
'scope' => $class_name,
|
||||
)
|
||||
);
|
||||
|
||||
// include preset css classes on the the stylesheet.
|
||||
$styles .= $theme_json_object->get_stylesheet(
|
||||
array( 'presets' ),
|
||||
null,
|
||||
array(
|
||||
'root_selector' => $class_name . ',' . $class_name . ' *',
|
||||
'scope' => $class_name,
|
||||
)
|
||||
);
|
||||
|
||||
if ( ! empty( $styles ) ) {
|
||||
wp_enqueue_block_support_styles( $styles );
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
add_filter( 'render_block', '_wp_add_block_level_presets_class', 10, 2 );
|
||||
add_filter( 'pre_render_block', '_wp_add_block_level_preset_styles', 10, 2 );
|
||||
<?php
|
||||
/**
|
||||
* Block level presets support.
|
||||
*
|
||||
* @package WordPress
|
||||
* @since 6.2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Get the class name used on block level presets.
|
||||
*
|
||||
* @internal
|
||||
*
|
||||
* @since 6.2.0
|
||||
* @access private
|
||||
*
|
||||
* @param array $block Block object.
|
||||
* @return string The unique class name.
|
||||
*/
|
||||
function _wp_get_presets_class_name( $block ) {
|
||||
return 'wp-settings-' . md5( serialize( $block ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the block content with block level presets class name.
|
||||
*
|
||||
* @internal
|
||||
*
|
||||
* @since 6.2.0
|
||||
* @access private
|
||||
*
|
||||
* @param string $block_content Rendered block content.
|
||||
* @param array $block Block object.
|
||||
* @return string Filtered block content.
|
||||
*/
|
||||
function _wp_add_block_level_presets_class( $block_content, $block ) {
|
||||
if ( ! $block_content ) {
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
// return early if the block doesn't have support for settings.
|
||||
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
|
||||
if ( ! block_has_support( $block_type, '__experimentalSettings', false ) ) {
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
// return early if no settings are found on the block attributes.
|
||||
$block_settings = isset( $block['attrs']['settings'] ) ? $block['attrs']['settings'] : null;
|
||||
if ( empty( $block_settings ) ) {
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
// Like the layout hook this assumes the hook only applies to blocks with a single wrapper.
|
||||
// Add the class name to the first element, presuming it's the wrapper, if it exists.
|
||||
$tags = new WP_HTML_Tag_Processor( $block_content );
|
||||
if ( $tags->next_tag() ) {
|
||||
$tags->add_class( _wp_get_presets_class_name( $block ) );
|
||||
}
|
||||
|
||||
return $tags->get_updated_html();
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the block level presets stylesheet.
|
||||
*
|
||||
* @internal
|
||||
*
|
||||
* @since 6.2.0
|
||||
* @since 6.3.0 Updated preset styles to use Selectors API.
|
||||
* @access private
|
||||
*
|
||||
* @param string|null $pre_render The pre-rendered content. Default null.
|
||||
* @param array $block The block being rendered.
|
||||
*
|
||||
* @return null
|
||||
*/
|
||||
function _wp_add_block_level_preset_styles( $pre_render, $block ) {
|
||||
// Return early if the block has not support for descendent block styles.
|
||||
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
|
||||
if ( ! block_has_support( $block_type, '__experimentalSettings', false ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// return early if no settings are found on the block attributes.
|
||||
$block_settings = isset( $block['attrs']['settings'] ) ? $block['attrs']['settings'] : null;
|
||||
if ( empty( $block_settings ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$class_name = '.' . _wp_get_presets_class_name( $block );
|
||||
|
||||
// the root selector for preset variables needs to target every possible block selector
|
||||
// in order for the general setting to override any bock specific setting of a parent block or
|
||||
// the site root.
|
||||
$variables_root_selector = '*,[class*="wp-block"]';
|
||||
$registry = WP_Block_Type_Registry::get_instance();
|
||||
$blocks = $registry->get_all_registered();
|
||||
foreach ( $blocks as $block_type ) {
|
||||
/*
|
||||
* We only want to append selectors for blocks using custom selectors
|
||||
* i.e. not `wp-block-<name>`.
|
||||
*/
|
||||
$has_custom_selector =
|
||||
( isset( $block_type->supports['__experimentalSelector'] ) && is_string( $block_type->supports['__experimentalSelector'] ) ) ||
|
||||
( isset( $block_type->selectors['root'] ) && is_string( $block_type->selectors['root'] ) );
|
||||
|
||||
if ( $has_custom_selector ) {
|
||||
$variables_root_selector .= ',' . wp_get_block_css_selector( $block_type );
|
||||
}
|
||||
}
|
||||
$variables_root_selector = WP_Theme_JSON::scope_selector( $class_name, $variables_root_selector );
|
||||
|
||||
// Remove any potentially unsafe styles.
|
||||
$theme_json_shape = WP_Theme_JSON::remove_insecure_properties(
|
||||
array(
|
||||
'version' => WP_Theme_JSON::LATEST_SCHEMA,
|
||||
'settings' => $block_settings,
|
||||
)
|
||||
);
|
||||
$theme_json_object = new WP_Theme_JSON( $theme_json_shape );
|
||||
|
||||
$styles = '';
|
||||
|
||||
// include preset css variables declaration on the stylesheet.
|
||||
$styles .= $theme_json_object->get_stylesheet(
|
||||
array( 'variables' ),
|
||||
null,
|
||||
array(
|
||||
'root_selector' => $variables_root_selector,
|
||||
'scope' => $class_name,
|
||||
)
|
||||
);
|
||||
|
||||
// include preset css classes on the the stylesheet.
|
||||
$styles .= $theme_json_object->get_stylesheet(
|
||||
array( 'presets' ),
|
||||
null,
|
||||
array(
|
||||
'root_selector' => $class_name . ',' . $class_name . ' *',
|
||||
'scope' => $class_name,
|
||||
)
|
||||
);
|
||||
|
||||
if ( ! empty( $styles ) ) {
|
||||
wp_enqueue_block_support_styles( $styles );
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
add_filter( 'render_block', '_wp_add_block_level_presets_class', 10, 2 );
|
||||
add_filter( 'pre_render_block', '_wp_add_block_level_preset_styles', 10, 2 );
|
||||
|
||||
@@ -1,81 +1,81 @@
|
||||
<?php
|
||||
/**
|
||||
* Shadow block support flag.
|
||||
*
|
||||
* @package WordPress
|
||||
* @since 6.3.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Registers the style and shadow block attributes for block types that support it.
|
||||
*
|
||||
* @since 6.3.0
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block Type.
|
||||
*/
|
||||
function wp_register_shadow_support( $block_type ) {
|
||||
$has_shadow_support = block_has_support( $block_type, 'shadow', false );
|
||||
|
||||
if ( ! $has_shadow_support ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! $block_type->attributes ) {
|
||||
$block_type->attributes = array();
|
||||
}
|
||||
|
||||
if ( array_key_exists( 'style', $block_type->attributes ) ) {
|
||||
$block_type->attributes['style'] = array(
|
||||
'type' => 'object',
|
||||
);
|
||||
}
|
||||
|
||||
if ( array_key_exists( 'shadow', $block_type->attributes ) ) {
|
||||
$block_type->attributes['shadow'] = array(
|
||||
'type' => 'string',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add CSS classes and inline styles for shadow features to the incoming attributes array.
|
||||
* This will be applied to the block markup in the front-end.
|
||||
*
|
||||
* @since 6.3.0
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block type.
|
||||
* @param array $block_attributes Block attributes.
|
||||
* @return array Shadow CSS classes and inline styles.
|
||||
*/
|
||||
function wp_apply_shadow_support( $block_type, $block_attributes ) {
|
||||
$has_shadow_support = block_has_support( $block_type, 'shadow', false );
|
||||
|
||||
if ( ! $has_shadow_support ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$shadow_block_styles = array();
|
||||
|
||||
$custom_shadow = $block_attributes['style']['shadow'] ?? null;
|
||||
$shadow_block_styles['shadow'] = $custom_shadow;
|
||||
|
||||
$attributes = array();
|
||||
$styles = wp_style_engine_get_styles( $shadow_block_styles );
|
||||
|
||||
if ( ! empty( $styles['css'] ) ) {
|
||||
$attributes['style'] = $styles['css'];
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
// Register the block support.
|
||||
WP_Block_Supports::get_instance()->register(
|
||||
'shadow',
|
||||
array(
|
||||
'register_attribute' => 'wp_register_shadow_support',
|
||||
'apply' => 'wp_apply_shadow_support',
|
||||
)
|
||||
);
|
||||
<?php
|
||||
/**
|
||||
* Shadow block support flag.
|
||||
*
|
||||
* @package WordPress
|
||||
* @since 6.3.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Registers the style and shadow block attributes for block types that support it.
|
||||
*
|
||||
* @since 6.3.0
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block Type.
|
||||
*/
|
||||
function wp_register_shadow_support( $block_type ) {
|
||||
$has_shadow_support = block_has_support( $block_type, 'shadow', false );
|
||||
|
||||
if ( ! $has_shadow_support ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! $block_type->attributes ) {
|
||||
$block_type->attributes = array();
|
||||
}
|
||||
|
||||
if ( array_key_exists( 'style', $block_type->attributes ) ) {
|
||||
$block_type->attributes['style'] = array(
|
||||
'type' => 'object',
|
||||
);
|
||||
}
|
||||
|
||||
if ( array_key_exists( 'shadow', $block_type->attributes ) ) {
|
||||
$block_type->attributes['shadow'] = array(
|
||||
'type' => 'string',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add CSS classes and inline styles for shadow features to the incoming attributes array.
|
||||
* This will be applied to the block markup in the front-end.
|
||||
*
|
||||
* @since 6.3.0
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block type.
|
||||
* @param array $block_attributes Block attributes.
|
||||
* @return array Shadow CSS classes and inline styles.
|
||||
*/
|
||||
function wp_apply_shadow_support( $block_type, $block_attributes ) {
|
||||
$has_shadow_support = block_has_support( $block_type, 'shadow', false );
|
||||
|
||||
if ( ! $has_shadow_support ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$shadow_block_styles = array();
|
||||
|
||||
$custom_shadow = $block_attributes['style']['shadow'] ?? null;
|
||||
$shadow_block_styles['shadow'] = $custom_shadow;
|
||||
|
||||
$attributes = array();
|
||||
$styles = wp_style_engine_get_styles( $shadow_block_styles );
|
||||
|
||||
if ( ! empty( $styles['css'] ) ) {
|
||||
$attributes['style'] = $styles['css'];
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
// Register the block support.
|
||||
WP_Block_Supports::get_instance()->register(
|
||||
'shadow',
|
||||
array(
|
||||
'register_attribute' => 'wp_register_shadow_support',
|
||||
'apply' => 'wp_apply_shadow_support',
|
||||
)
|
||||
);
|
||||
|
||||
@@ -1,89 +1,89 @@
|
||||
<?php
|
||||
/**
|
||||
* Spacing block support flag.
|
||||
*
|
||||
* For backwards compatibility, this remains separate to the dimensions.php
|
||||
* block support despite both belonging under a single panel in the editor.
|
||||
*
|
||||
* @package WordPress
|
||||
* @since 5.8.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Registers the style block attribute for block types that support it.
|
||||
*
|
||||
* @since 5.8.0
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block Type.
|
||||
*/
|
||||
function wp_register_spacing_support( $block_type ) {
|
||||
$has_spacing_support = block_has_support( $block_type, 'spacing', false );
|
||||
|
||||
// Setup attributes and styles within that if needed.
|
||||
if ( ! $block_type->attributes ) {
|
||||
$block_type->attributes = array();
|
||||
}
|
||||
|
||||
if ( $has_spacing_support && ! array_key_exists( 'style', $block_type->attributes ) ) {
|
||||
$block_type->attributes['style'] = array(
|
||||
'type' => 'object',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds CSS classes for block spacing to the incoming attributes array.
|
||||
* This will be applied to the block markup in the front-end.
|
||||
*
|
||||
* @since 5.8.0
|
||||
* @since 6.1.0 Implemented the style engine to generate CSS and classnames.
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block Type.
|
||||
* @param array $block_attributes Block attributes.
|
||||
* @return array Block spacing CSS classes and inline styles.
|
||||
*/
|
||||
function wp_apply_spacing_support( $block_type, $block_attributes ) {
|
||||
if ( wp_should_skip_block_supports_serialization( $block_type, 'spacing' ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$attributes = array();
|
||||
$has_padding_support = block_has_support( $block_type, array( 'spacing', 'padding' ), false );
|
||||
$has_margin_support = block_has_support( $block_type, array( 'spacing', 'margin' ), false );
|
||||
$block_styles = isset( $block_attributes['style'] ) ? $block_attributes['style'] : null;
|
||||
|
||||
if ( ! $block_styles ) {
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
$skip_padding = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'padding' );
|
||||
$skip_margin = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'margin' );
|
||||
$spacing_block_styles = array(
|
||||
'padding' => null,
|
||||
'margin' => null,
|
||||
);
|
||||
if ( $has_padding_support && ! $skip_padding ) {
|
||||
$spacing_block_styles['padding'] = isset( $block_styles['spacing']['padding'] ) ? $block_styles['spacing']['padding'] : null;
|
||||
}
|
||||
if ( $has_margin_support && ! $skip_margin ) {
|
||||
$spacing_block_styles['margin'] = isset( $block_styles['spacing']['margin'] ) ? $block_styles['spacing']['margin'] : null;
|
||||
}
|
||||
$styles = wp_style_engine_get_styles( array( 'spacing' => $spacing_block_styles ) );
|
||||
|
||||
if ( ! empty( $styles['css'] ) ) {
|
||||
$attributes['style'] = $styles['css'];
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
// Register the block support.
|
||||
WP_Block_Supports::get_instance()->register(
|
||||
'spacing',
|
||||
array(
|
||||
'register_attribute' => 'wp_register_spacing_support',
|
||||
'apply' => 'wp_apply_spacing_support',
|
||||
)
|
||||
);
|
||||
<?php
|
||||
/**
|
||||
* Spacing block support flag.
|
||||
*
|
||||
* For backwards compatibility, this remains separate to the dimensions.php
|
||||
* block support despite both belonging under a single panel in the editor.
|
||||
*
|
||||
* @package WordPress
|
||||
* @since 5.8.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Registers the style block attribute for block types that support it.
|
||||
*
|
||||
* @since 5.8.0
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block Type.
|
||||
*/
|
||||
function wp_register_spacing_support( $block_type ) {
|
||||
$has_spacing_support = block_has_support( $block_type, 'spacing', false );
|
||||
|
||||
// Setup attributes and styles within that if needed.
|
||||
if ( ! $block_type->attributes ) {
|
||||
$block_type->attributes = array();
|
||||
}
|
||||
|
||||
if ( $has_spacing_support && ! array_key_exists( 'style', $block_type->attributes ) ) {
|
||||
$block_type->attributes['style'] = array(
|
||||
'type' => 'object',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds CSS classes for block spacing to the incoming attributes array.
|
||||
* This will be applied to the block markup in the front-end.
|
||||
*
|
||||
* @since 5.8.0
|
||||
* @since 6.1.0 Implemented the style engine to generate CSS and classnames.
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block Type.
|
||||
* @param array $block_attributes Block attributes.
|
||||
* @return array Block spacing CSS classes and inline styles.
|
||||
*/
|
||||
function wp_apply_spacing_support( $block_type, $block_attributes ) {
|
||||
if ( wp_should_skip_block_supports_serialization( $block_type, 'spacing' ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$attributes = array();
|
||||
$has_padding_support = block_has_support( $block_type, array( 'spacing', 'padding' ), false );
|
||||
$has_margin_support = block_has_support( $block_type, array( 'spacing', 'margin' ), false );
|
||||
$block_styles = isset( $block_attributes['style'] ) ? $block_attributes['style'] : null;
|
||||
|
||||
if ( ! $block_styles ) {
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
$skip_padding = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'padding' );
|
||||
$skip_margin = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'margin' );
|
||||
$spacing_block_styles = array(
|
||||
'padding' => null,
|
||||
'margin' => null,
|
||||
);
|
||||
if ( $has_padding_support && ! $skip_padding ) {
|
||||
$spacing_block_styles['padding'] = isset( $block_styles['spacing']['padding'] ) ? $block_styles['spacing']['padding'] : null;
|
||||
}
|
||||
if ( $has_margin_support && ! $skip_margin ) {
|
||||
$spacing_block_styles['margin'] = isset( $block_styles['spacing']['margin'] ) ? $block_styles['spacing']['margin'] : null;
|
||||
}
|
||||
$styles = wp_style_engine_get_styles( array( 'spacing' => $spacing_block_styles ) );
|
||||
|
||||
if ( ! empty( $styles['css'] ) ) {
|
||||
$attributes['style'] = $styles['css'];
|
||||
}
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
// Register the block support.
|
||||
WP_Block_Supports::get_instance()->register(
|
||||
'spacing',
|
||||
array(
|
||||
'register_attribute' => 'wp_register_spacing_support',
|
||||
'apply' => 'wp_apply_spacing_support',
|
||||
)
|
||||
);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,36 +1,36 @@
|
||||
<?php
|
||||
/**
|
||||
* Block support utility functions.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Block Supports
|
||||
* @since 6.0.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Checks whether serialization of the current block's supported properties
|
||||
* should occur.
|
||||
*
|
||||
* @since 6.0.0
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block type.
|
||||
* @param string $feature_set Name of block support feature set..
|
||||
* @param string $feature Optional name of individual feature to check.
|
||||
*
|
||||
* @return bool Whether to serialize block support styles & classes.
|
||||
*/
|
||||
function wp_should_skip_block_supports_serialization( $block_type, $feature_set, $feature = null ) {
|
||||
if ( ! is_object( $block_type ) || ! $feature_set ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$path = array( $feature_set, '__experimentalSkipSerialization' );
|
||||
$skip_serialization = _wp_array_get( $block_type->supports, $path, false );
|
||||
|
||||
if ( is_array( $skip_serialization ) ) {
|
||||
return in_array( $feature, $skip_serialization, true );
|
||||
}
|
||||
|
||||
return $skip_serialization;
|
||||
}
|
||||
<?php
|
||||
/**
|
||||
* Block support utility functions.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Block Supports
|
||||
* @since 6.0.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Checks whether serialization of the current block's supported properties
|
||||
* should occur.
|
||||
*
|
||||
* @since 6.0.0
|
||||
* @access private
|
||||
*
|
||||
* @param WP_Block_Type $block_type Block type.
|
||||
* @param string $feature_set Name of block support feature set..
|
||||
* @param string $feature Optional name of individual feature to check.
|
||||
*
|
||||
* @return bool Whether to serialize block support styles & classes.
|
||||
*/
|
||||
function wp_should_skip_block_supports_serialization( $block_type, $feature_set, $feature = null ) {
|
||||
if ( ! is_object( $block_type ) || ! $feature_set ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$path = array( $feature_set, '__experimentalSkipSerialization' );
|
||||
$skip_serialization = _wp_array_get( $block_type->supports, $path, false );
|
||||
|
||||
if ( is_array( $skip_serialization ) ) {
|
||||
return in_array( $feature, $skip_serialization, true );
|
||||
}
|
||||
|
||||
return $skip_serialization;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user