Make WordPress Core


Ignore:
Timestamp:
11/09/2021 02:15:23 AM (2 years ago)
Author:
noisysocks
Message:

Add Site Editor and PHP changes from Gutenberg 10.1 - 11.9

  • First pass at adding the site editor from the Gutenberg plugin to wp-admin/site-editor.php.
  • Adds miscellaneous PHP changes from Gutenberg 10.1 - 11.9.

Follows [52042].
See #54337.
Props youknowriad, aristath, hellofromtonya, gziolo.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-supports/spacing.php

    r51298 r52069  
    22/**
    33 * Spacing block support flag.
     4
     5 * For backwards compatibility, this remains separate to the dimensions.php
     6 * block support despite both belonging under a single panel in the editor.
    47 *
    58 * @package WordPress
     
    4346 */
    4447function wp_apply_spacing_support( $block_type, $block_attributes ) {
    45     $has_padding_support = wp_has_spacing_feature_support( $block_type, 'padding' );
    46     $has_margin_support  = wp_has_spacing_feature_support( $block_type, 'margin' );
     48    if ( wp_skip_spacing_serialization( $block_type ) ) {
     49        return array();
     50    }
     51
     52    $has_padding_support = block_has_support( $block_type, array( 'spacing', 'padding' ), false );
     53    $has_margin_support  = block_has_support( $block_type, array( 'spacing', 'margin' ), false );
    4754    $styles              = array();
    4855
    4956    if ( $has_padding_support ) {
    5057        $padding_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'padding' ), null );
    51         if ( null !== $padding_value ) {
     58        if ( is_array( $padding_value ) ) {
    5259            foreach ( $padding_value as $key => $value ) {
    5360                $styles[] = sprintf( 'padding-%s: %s;', $key, $value );
    5461            }
     62        } elseif ( null !== $padding_value ) {
     63            $styles[] = sprintf( 'padding: %s;', $padding_value );
    5564        }
    5665    }
     
    5867    if ( $has_margin_support ) {
    5968        $margin_value = _wp_array_get( $block_attributes, array( 'style', 'spacing', 'margin' ), null );
    60         if ( null !== $margin_value ) {
     69        if ( is_array( $margin_value ) ) {
    6170            foreach ( $margin_value as $key => $value ) {
    6271                $styles[] = sprintf( 'margin-%s: %s;', $key, $value );
    6372            }
     73        } elseif ( null !== $margin_value ) {
     74            $styles[] = sprintf( 'margin: %s;', $margin_value );
    6475        }
    6576    }
     
    6980
    7081/**
    71  * Checks whether the current block type supports the spacing feature requested.
     82 * Checks whether serialization of the current block's spacing properties should
     83 * occur.
    7284 *
    73  * @since 5.8.0
     85 * @since 5.9.0
    7486 * @access private
    7587 *
    76  * @param WP_Block_Type $block_type Block type to check for support.
    77  * @param string        $feature    Name of the feature to check support for.
    78  * @param mixed         $default    Fallback value for feature support. Default false.
    79  * @return bool Whether the feature is supported.
     88 * @param WP_Block_Type $block_type Block type.
     89 *
     90 * @return boolean Whether to serialize spacing support styles & classes.
    8091 */
    81 function wp_has_spacing_feature_support( $block_type, $feature, $default = false ) {
    82     // Check if the specific feature has been opted into individually
    83     // via nested flag under `spacing`.
    84     return block_has_support( $block_type, array( 'spacing', $feature ), $default );
     92function wp_skip_spacing_serialization( $block_type ) {
     93    $spacing_support = _wp_array_get( $block_type->supports, array( 'spacing' ), false );
     94
     95    return is_array( $spacing_support ) &&
     96        array_key_exists( '__experimentalSkipSerialization', $spacing_support ) &&
     97        $spacing_support['__experimentalSkipSerialization'];
     98}
     99
     100/**
     101 * Renders the spacing gap support to the block wrapper, to ensure
     102 * that the CSS variable is rendered in all environments.
     103 *
     104 * @since 5.9.0
     105 * @access private
     106 *
     107 * @param  string $block_content Rendered block content.
     108 * @param  array  $block         Block object.
     109 * @return string                Filtered block content.
     110 */
     111function wp_render_spacing_gap_support( $block_content, $block ) {
     112    $block_type      = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
     113    $has_gap_support = block_has_support( $block_type, array( 'spacing', 'blockGap' ), false );
     114    if ( ! $has_gap_support || ! isset( $block['attrs']['style']['spacing']['blockGap'] ) ) {
     115        return $block_content;
     116    }
     117
     118    $gap_value = $block['attrs']['style']['spacing']['blockGap'];
     119
     120    // Skip if gap value contains unsupported characters.
     121    // Regex for CSS value borrowed from `safecss_filter_attr`, and used here
     122    // because we only want to match against the value, not the CSS attribute.
     123    if ( preg_match( '%[\\\(&=}]|/\*%', $gap_value ) ) {
     124        return $block_content;
     125    }
     126
     127    $style = sprintf(
     128        '--wp--style--block-gap: %s',
     129        esc_attr( $gap_value )
     130    );
     131
     132    // Attempt to update an existing style attribute on the wrapper element.
     133    $injected_style = preg_replace(
     134        '/^([^>.]+?)(' . preg_quote( 'style="', '/' ) . ')(?=.+?>)/',
     135        '$1$2' . $style . '; ',
     136        $block_content,
     137        1
     138    );
     139
     140    // If there is no existing style attribute, add one to the wrapper element.
     141    if ( $injected_style === $block_content ) {
     142        $injected_style = preg_replace(
     143            '/<([a-zA-Z0-9]+)([ >])/',
     144            '<$1 style="' . $style . '"$2',
     145            $block_content,
     146            1
     147        );
     148    };
     149
     150    return $injected_style;
    85151}
    86152
     
    93159    )
    94160);
     161
     162add_filter( 'render_block', 'wp_render_spacing_gap_support', 10, 2 );
Note: See TracChangeset for help on using the changeset viewer.