Skip to content

Commit

Permalink
prep build 01/11
Browse files Browse the repository at this point in the history
  • Loading branch information
bph committed Jan 11, 2023
2 parents 09c9071 + 2d49496 commit 7fffb66
Show file tree
Hide file tree
Showing 70 changed files with 1,799 additions and 186 deletions.
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ Gather blocks in a layout container. ([Source](https://github.com/WordPress/gute

- **Name:** core/group
- **Category:** design
- **Supports:** align (full, wide), anchor, ariaLabel, color (background, gradients, link, text), dimensions (minHeight), spacing (blockGap, margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Supports:** align (full, wide), anchor, ariaLabel, color (background, gradients, link, text), dimensions (minHeight), position (sticky), spacing (blockGap, margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** tagName, templateLock

## Heading
Expand Down
143 changes: 143 additions & 0 deletions lib/block-supports/position.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php
/**
* Position block support flag.
*
* @package gutenberg
*/

/**
* Registers the style block attribute for block types that support it.
*
* @param WP_Block_Type $block_type Block Type.
*/
function gutenberg_register_position_support( $block_type ) {
$has_position_support = block_has_support( $block_type, array( '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.
*
* @param string $block_content Rendered block content.
* @param array $block Block object.
* @return string Filtered block content.
*/
function gutenberg_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, array( 'position' ), false );

if (
! $has_position_support ||
empty( $block['attrs']['style']['position'] )
) {
return $block_content;
}

$global_settings = gutenberg_get_global_settings();
$theme_has_sticky_support = _wp_array_get( $global_settings, array( 'position', 'sticky' ), false );
$theme_has_fixed_support = _wp_array_get( $global_settings, array( '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 = _wp_array_get( $block, array( 'attrs', 'style' ), null );
$class_name = wp_unique_id( 'wp-container-' );
$selector = ".$class_name";
$position_styles = array();
$position_type = _wp_array_get( $style_attribute, array( 'position', 'type' ), '' );

if (
in_array( $position_type, $allowed_position_types, true )
) {
$sides = array( 'top', 'right', 'bottom', 'left' );

foreach ( $sides as $side ) {
$side_value = _wp_array_get( $style_attribute, array( 'position', $side ) );
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--height, 0px))";
}

$position_styles[] =
array(
'selector' => $selector,
'declarations' => array(
$side => $side_value,
),
);
}
}

$position_styles[] =
array(
'selector' => $selector,
'declarations' => array(
'position' => $position_type,
'z-index' => '10', // TODO: Replace hard-coded z-index value with a z-index preset approach in theme.json.
),
);
}

if ( ! empty( $position_styles ) ) {
/*
* Add to the style engine store to enqueue and render position styles.
*/
gutenberg_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();
$content->add_class( $class_name );
return (string) $content;
}

return $block_content;
}

// Register the block support. (overrides core one).
WP_Block_Supports::get_instance()->register(
'position',
array(
'register_attribute' => 'gutenberg_register_position_support',
)
);

if ( function_exists( 'wp_render_position_support' ) ) {
remove_filter( 'render_block', 'wp_render_position_support' );
}
add_filter( 'render_block', 'gutenberg_render_position_support', 10, 2 );
102 changes: 93 additions & 9 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ class WP_Theme_JSON_Gutenberg {
* and `typography`, and renamed others according to the new schema.
* @since 6.0.0 Added `color.defaultDuotone`.
* @since 6.1.0 Added `layout.definitions` and `useRootPaddingAwareAlignments`.
* @since 6.2.0 Added `dimensions.minHeight`.
* @since 6.2.0 Added `dimensions.minHeight`, `position.fixed` and `position.sticky`.
* @var array
*/
const VALID_SETTINGS = array(
Expand Down Expand Up @@ -369,6 +369,10 @@ class WP_Theme_JSON_Gutenberg {
'definitions' => null,
'wideSize' => null,
),
'position' => array(
'fixed' => null,
'sticky' => null,
),
'spacing' => array(
'customSpacingSize' => null,
'spacingSizes' => null,
Expand Down Expand Up @@ -536,6 +540,7 @@ public static function get_element_class_name( $element ) {
* Options that settings.appearanceTools enables.
*
* @since 6.0.0
* @since 6.2.0 Added `position.fixed` and `position.sticky`.
* @var array
*/
const APPEARANCE_TOOLS_OPT_INS = array(
Expand All @@ -545,6 +550,8 @@ public static function get_element_class_name( $element ) {
array( 'border', 'width' ),
array( 'color', 'link' ),
array( 'dimensions', 'minHeight' ),
array( 'position', 'fixed' ),
array( 'position', 'sticky' ),
array( 'spacing', 'blockGap' ),
array( 'spacing', 'margin' ),
array( 'spacing', 'padding' ),
Expand Down Expand Up @@ -713,9 +720,17 @@ protected static function sanitize( $input, $valid_block_names, $valid_element_n
$schema_styles_blocks = array();
$schema_settings_blocks = array();
foreach ( $valid_block_names as $block ) {
$schema_settings_blocks[ $block ] = static::VALID_SETTINGS;
$schema_styles_blocks[ $block ] = $styles_non_top_level;
$schema_styles_blocks[ $block ]['elements'] = $schema_styles_elements;
// Build the schema for each block style variation.
$style_variation_names = isset( $input['styles']['blocks'][ $block ]['variations'] ) ? array_keys( $input['styles']['blocks'][ $block ]['variations'] ) : array();
$schema_styles_variations = array();
if ( ! empty( $style_variation_names ) ) {
$schema_styles_variations = array_fill_keys( $style_variation_names, $styles_non_top_level );
}

$schema_settings_blocks[ $block ] = static::VALID_SETTINGS;
$schema_styles_blocks[ $block ] = $styles_non_top_level;
$schema_styles_blocks[ $block ]['elements'] = $schema_styles_elements;
$schema_styles_blocks[ $block ]['variations'] = $schema_styles_variations;
}

$schema['styles'] = static::VALID_STYLES;
Expand Down Expand Up @@ -863,6 +878,16 @@ protected static function get_blocks_metadata() {
}
static::$blocks_metadata[ $block_name ]['elements'][ $el_name ] = implode( ',', $element_selector );
}

// If the block has style variations, append their selectors to the block metadata.
if ( ! empty( $block_type->styles ) ) {
$style_selectors = array();
foreach ( $block_type->styles as $style ) {
// The style variation classname is duplicated in the selector to ensure that it overrides core block styles.
$style_selectors[ $style['name'] ] = static::append_to_selector( '.is-style-' . $style['name'] . '.is-style-' . $style['name'], static::$blocks_metadata[ $block_name ]['selector'] );
}
static::$blocks_metadata[ $block_name ]['styleVariations'] = $style_selectors;
}
}

return static::$blocks_metadata;
Expand Down Expand Up @@ -2145,12 +2170,23 @@ private static function get_block_nodes( $theme_json, $selectors = array() ) {
$feature_selectors = $selectors[ $name ]['features'];
}

$variation_selectors = array();
if ( isset( $node['variations'] ) ) {
foreach ( $node['variations'] as $variation => $node ) {
$variation_selectors[] = array(
'path' => array( 'styles', 'blocks', $name, 'variations', $variation ),
'selector' => $selectors[ $name ]['styleVariations'][ $variation ],
);
}
}

$nodes[] = array(
'name' => $name,
'path' => array( 'styles', 'blocks', $name ),
'selector' => $selector,
'duotone' => $duotone_selector,
'features' => $feature_selectors,
'name' => $name,
'path' => array( 'styles', 'blocks', $name ),
'selector' => $selector,
'duotone' => $duotone_selector,
'features' => $feature_selectors,
'variations' => $variation_selectors,
);

if ( isset( $theme_json['styles']['blocks'][ $name ]['elements'] ) ) {
Expand Down Expand Up @@ -2230,6 +2266,49 @@ public function get_styles_for_block( $block_metadata ) {
}
}

// If there are style variations, generate the declarations for them, including any feature selectors the block may have.
$style_variation_declarations = array();
if ( ! empty( $block_metadata['variations'] ) ) {
foreach ( $block_metadata['variations'] as $style_variation ) {
$style_variation_node = _wp_array_get( $this->theme_json, $style_variation['path'], array() );
$style_variation_selector = $style_variation['selector'];

// If the block has feature selectors, generate the declarations for them within the current style variation.
if ( ! empty( $block_metadata['features'] ) ) {
foreach ( $block_metadata['features'] as $feature_name => $feature_selector ) {
if ( ! empty( $style_variation_node[ $feature_name ] ) ) {
// Prepend the variation selector to the feature selector.
$split_feature_selectors = explode( ',', $feature_selector );
$feature_selectors = array_map(
function( $split_feature_selector ) use ( $style_variation_selector ) {
return trim( $style_variation_selector ) . trim( $split_feature_selector );
},
$split_feature_selectors
);
$combined_feature_selectors = implode( ',', $feature_selectors );

// Compute declarations for the feature.
$new_feature_declarations = static::compute_style_properties( array( $feature_name => $style_variation_node[ $feature_name ] ), $settings, null, $this->theme_json );

// Merge new declarations with any that already exist for
// the feature selector. This may occur when multiple block
// support features use the same custom selector.
if ( isset( $style_variation_declarations[ $combined_feature_selectors ] ) ) {
$style_variation_declarations[ $combined_feature_selectors ] = array_merge( $style_variation_declarations[ $combined_feature_selectors ], $new_feature_declarations );
} else {
$style_variation_declarations[ $combined_feature_selectors ] = $new_feature_declarations;
}
// Remove the feature from the variation's node now the
// styles will be included under the feature level selector.
unset( $style_variation_node[ $feature_name ] );
}
}
}
// Compute declarations for remaining styles not covered by feature level selectors.
$style_variation_declarations[ $style_variation_selector ] = static::compute_style_properties( $style_variation_node, $settings, null, $this->theme_json );
}
}

/*
* Get a reference to element name from path.
* $block_metadata['path'] = array( 'styles','elements','link' );
Expand Down Expand Up @@ -2320,6 +2399,11 @@ function( $pseudo_selector ) use ( $selector ) {
$block_rules .= static::to_ruleset( $feature_selector, $individual_feature_declarations );
}

// 6. Generate and append the style variation rulesets.
foreach ( $style_variation_declarations as $style_variation_selector => $individual_style_variation_declarations ) {
$block_rules .= static::to_ruleset( $style_variation_selector, $individual_style_variation_declarations );
}

return $block_rules;
}

Expand Down
26 changes: 26 additions & 0 deletions lib/compat/wordpress-6.2/blocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* Temporary compatibility shims for block APIs present in Gutenberg.
*
* @package gutenberg
*/

/**
* Update allowed inline style attributes list.
*
* Note: This should be removed when the minimum required WP version is >= 6.2.
*
* @param string[] $attrs Array of allowed CSS attributes.
* @return string[] CSS attributes.
*/
function gutenberg_safe_style_attrs_6_2( $attrs ) {
$attrs[] = 'position';
$attrs[] = 'top';
$attrs[] = 'right';
$attrs[] = 'bottom';
$attrs[] = 'left';
$attrs[] = 'z-index';

return $attrs;
}
add_filter( 'safe_style_css', 'gutenberg_safe_style_attrs_6_2' );
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,6 @@ public function get_item_schema() {
'context' => array( 'mobile' ),
),

'__experimentalBlockInspectorTabs' => array(
'description' => __( 'Block inspector tab display overrides.', 'gutenberg' ),
'type' => 'object',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),

'__experimentalBlockInspectorAnimation' => array(
'description' => __( 'Whether to enable animation when showing and hiding the block inspector.', 'gutenberg' ),
'type' => 'object',
Expand Down Expand Up @@ -204,6 +198,12 @@ public function get_item_schema() {
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),

'blockInspectorTabs' => array(
'description' => __( 'Block inspector tab display overrides.', 'gutenberg' ),
'type' => 'object',
'context' => array( 'post-editor', 'site-editor', 'widgets-editor' ),
),

'disableCustomColors' => array(
'description' => __( 'Disables custom colors.', 'gutenberg' ),
'type' => 'boolean',
Expand Down
3 changes: 0 additions & 3 deletions lib/experimental/editor-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,6 @@ function gutenberg_enable_experiments() {
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-off-canvas-navigation-editor', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableOffCanvasNavigationEditor = true', 'before' );
}
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-block-inspector-tabs', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableBlockInspectorTabs = true', 'before' );
}
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-global-styles-custom-css', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableGlobalStylesCustomCSS = true', 'before' );
}
Expand Down
Loading

0 comments on commit 7fffb66

Please sign in to comment.