wp_style_engine_get_styles example

/* * For now we only care about link color. */
    $skip_link_color_serialization = wp_should_skip_block_supports_serialization( $block_type, 'color', 'link' );

    if ( $skip_link_color_serialization ) {
        return null;
    }
    $class_name        = wp_get_elements_class_name( $block );
    $link_block_styles = isset( $element_block_styles['link'] ) ? $element_block_styles['link'] : null;

    wp_style_engine_get_styles(
        $link_block_styles,
        array(
            'selector' => ".$class_name a",
            'context'  => 'block-supports',
        )
    );

    if ( isset( $link_block_styles[':hover'] ) ) {
        wp_style_engine_get_styles(
            $link_block_styles[':hover'],
            array(
                
// Individual border styles e.g. top, left etc.     foreach ( $sides as $side ) {
        $border                 = _wp_array_get( $attributes, array( 'style', 'border', $side ), null );
        $border_styles[ $side ] = array(
            'color' => isset( $border['color'] ) ? $border['color'] : null,
            'style' => isset( $border['style'] ) ? $border['style'] : null,
            'width' => isset( $border['width'] ) ? $border['width'] : null,
        );
    }

    $styles     = wp_style_engine_get_styles( array( 'border' => $border_styles ) );
    $attributes = array();
    if ( ! empty( $styles['classnames'] ) ) {
        $attributes['class'] = $styles['classnames'];
    }
    if ( ! empty( $styles['css'] ) ) {
        $attributes['style'] = $styles['css'];
    }
    return $attributes;
}

/** * Registers the `core/post-featured-image` block on the server. */
array(
                    'selector'     => "$selector > .alignwide",
                    'declarations' => array( 'max-width' => $wide_max_width_value ),
                ),
                array(
                    'selector'     => "$selector .alignfull",
                    'declarations' => array( 'max-width' => 'none' ),
                )
            );

            if ( isset( $block_spacing ) ) {
                $block_spacing_values = wp_style_engine_get_styles(
                    array(
                        'spacing' => $block_spacing,
                    )
                );

                /* * Handle negative margins for alignfull children of blocks with custom padding set. * They're added separately because padding might only be set on one side. */
                if ( isset( $block_spacing_values['declarations']['padding-right'] ) ) {
                    $padding_right   = $block_spacing_values['declarations']['padding-right'];
                    
$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          = _wp_array_get( $block_attributes, array( '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;
}

$has_letter_spacing_support &&
        ! $should_skip_letter_spacing &&
        isset( $block_attributes['style']['typography']['letterSpacing'] )
    ) {
        $typography_block_styles['letterSpacing'] = wp_typography_get_preset_inline_style_value(
            $block_attributes['style']['typography']['letterSpacing'],
            'letter-spacing'
        );
    }

    $attributes = array();
    $styles     = wp_style_engine_get_styles(
        array( 'typography' => $typography_block_styles ),
        array( 'convert_vars_to_classnames' => true )
    );

    if ( ! empty( $styles['classnames'] ) ) {
        $attributes['class'] = $styles['classnames'];
    }

    if ( ! empty( $styles['css'] ) ) {
        $attributes['style'] = $styles['css'];
    }

    

    $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'] = $has_min_height_support && ! $skip_min_height ? _wp_array_get( $block_styles, array( 'dimensions', 'minHeight' ), null ) : null;
    $styles                               = wp_style_engine_get_styles( array( 'dimensions' => $dimensions_block_styles ) );

    if ( ! empty( $styles['css'] ) ) {
        $attributes['style'] = $styles['css'];
    }

    return $attributes;
}

// Register the block support. WP_Block_Supports::get_instance()->register(
    'dimensions',
    
// Text color.     $preset_text_color          = array_key_exists( 'textColor', $attributes ) ? "var:preset|color|{$attributes['textColor']}" : null;
    $custom_text_color          = _wp_array_get( $attributes, array( 'style', 'color', 'text' ), null );
    $color_block_styles['text'] = $preset_text_color ? $preset_text_color : $custom_text_color;

    // Background Color.     $preset_background_color          = array_key_exists( 'backgroundColor', $attributes ) ? "var:preset|color|{$attributes['backgroundColor']}" : null;
    $custom_background_color          = _wp_array_get( $attributes, array( 'style', 'color', 'background' ), null );
    $color_block_styles['background'] = $preset_background_color ? $preset_background_color : $custom_background_color;

    // Generate color styles and classes.     $styles        = wp_style_engine_get_styles( array( 'color' => $color_block_styles ), array( 'convert_vars_to_classnames' => true ) );
    $inline_styles = empty( $styles['css'] ) ? '' : sprintf( ' style="%s"', esc_attr( $styles['css'] ) );
    $classnames    = empty( $styles['classnames'] ) ? '' : ' ' . esc_attr( $styles['classnames'] );
    if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
        $classnames .= ' has-link-color';
    }
    // Apply color classes and styles to the calendar.     $calendar = str_replace( '<table', '<table' . $inline_stylesget_calendar( true, false ) );
    $calendar = str_replace( 'class="wp-calendar-table', 'class="wp-calendar-table' . $classnames$calendar );

    $wrapper_attributes = get_block_wrapper_attributes();
    $output             = sprintf(
        
$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();
    $spacing_block_styles['padding'] = $has_padding_support && ! $skip_padding ? _wp_array_get( $block_styles, array( 'spacing', 'padding' ), null ) : null;
    $spacing_block_styles['margin']  = $has_margin_support && ! $skip_margin ? _wp_array_get( $block_styles, array( 'spacing', 'margin' ), null ) : 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',
    
// Individual border styles e.g. top, left etc.     foreach ( $sides as $side ) {
        $border                 = _wp_array_get( $attributes, array( 'style', 'border', $side ), null );
        $border_styles[ $side ] = array(
            'color' => isset( $border['color'] ) ? $border['color'] : null,
            'style' => isset( $border['style'] ) ? $border['style'] : null,
            'width' => isset( $border['width'] ) ? $border['width'] : null,
        );
    }

    $styles     = wp_style_engine_get_styles( array( 'border' => $border_styles ) );
    $attributes = array();
    if ( ! empty( $styles['classnames'] ) ) {
        $attributes['class'] = $styles['classnames'];
    }
    if ( ! empty( $styles['css'] ) ) {
        $attributes['style'] = $styles['css'];
    }
    return $attributes;
}

/** * Registers the `core/avatar` block on the server. */
if ( ! $has_shadow_support ) {
        return array();
    }

    $shadow_block_styles = array();

    $preset_shadow                 = array_key_exists( 'shadow', $block_attributes ) ? "var:preset|shadow|{$block_attributes['shadow']}" : null;
    $custom_shadow                 = isset( $block_attributes['style']['shadow'] ) ? $block_attributes['style']['shadow'] : null;
    $shadow_block_styles['shadow'] = $preset_shadow ? $preset_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',
    
$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;
}

Home | Imprint | This part of the site doesn't use cookies.