Skip to content

Commit

Permalink
Making the linter happy.
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd committed Mar 16, 2022
1 parent a994440 commit 50ed43b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 44 deletions.
49 changes: 28 additions & 21 deletions lib/block-supports/spacing.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,49 +45,56 @@ function gutenberg_apply_spacing_support( $block_type, $block_attributes ) {

$has_padding_support = gutenberg_block_has_support( $block_type, array( 'spacing', 'padding' ), false );
$has_margin_support = gutenberg_block_has_support( $block_type, array( 'spacing', 'margin' ), false );
$block_styles = isset( $block_attributes['style'] ) ? $block_attributes['style'] : null;
$classes = array();
$styles = array();
$style_engine = WP_Style_Engine_Gutenberg::get_instance();

if ( $has_padding_support ) {

/*
// We can also return an obfuscated classname.
// Do we need to add !important rules to the stylesheet in this case
// seeing as they're not inline?
$classes[] = $style_engine->add_style_from_attributes(
'wp-block-supports',
$block_attributes['style'],
array( 'spacing', 'padding' ),
array(
'obfuscate' => true,
)
);
*/

$styles[] = $style_engine->get_inline_styles_from_attributes(
$block_attributes['style'],
/*
// We can also return an obfuscated classname.
// Do we need to add !important rules to the stylesheet in this case
// seeing as they're not inline?
$classes[] = $style_engine->add_style_from_attributes(
'wp-block-supports',
$attributes_styles,
array( 'spacing', 'padding' ),
array(
'obfuscate' => true,
)
);
*/
$padding_styles = $style_engine->get_inline_styles_from_attributes(
$block_styles,
array( 'spacing', 'padding' )
);

if ( $padding_styles ) {
$styles[] = $padding_styles;
}
}

if ( $has_margin_support ) {
// As with padding above.
$styles[] = $style_engine->get_inline_styles_from_attributes(
$block_attributes['style'],
$margin_styles = $style_engine->get_inline_styles_from_attributes(
$block_styles,
array( 'spacing', 'margin' )
);

if ( $margin_styles ) {
$styles[] = $margin_styles;
}
}

// Collect classes and styles.
$attributes = array();

if ( ! empty( $classes ) ) {
$attributes['class'] = implode( ' ', array_filter( $classes ) );
$attributes['class'] = implode( ' ', $classes );
}

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

return $attributes;
Expand Down
57 changes: 37 additions & 20 deletions lib/style-engine/class-wp-style-engine-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
/**
* WP_Style_Engine class
*
* Generates classnames and block styles.
*
* @package Gutenberg
*/

// Load utilities.
require_once __DIR__ . '/style-definition-utils.php';

/**
Expand Down Expand Up @@ -36,13 +39,13 @@ class WP_Style_Engine_Gutenberg {
* Style definitions that contain the instructions to
* parse/output valid Gutenberg styles.
*/
const STYLE_DEFINITIONS_METADATA = array(
const BLOCK_STYLE_DEFINITIONS_METADATA = array(
'spacing' => array(
'padding' => array(
'value_key' => 'padding',
'value_func' => 'gutenberg_get_style_engine_css_box_rules',
),
'margin' => array(
'margin' => array(
'value_key' => 'margin',
'value_func' => 'gutenberg_get_style_engine_css_box_rules',
),
Expand Down Expand Up @@ -79,10 +82,18 @@ public static function get_instance() {
return self::$instance;
}

/**
* Resets the style store.
*/
public function reset() {
$this->registered_styles = array();
}

/**
* Compiles and returns styles to be included in an HTML document.
*
* @return string CSS rules to embed in a `<style />`.
*/
public function get_generated_styles() {
$output = '';
foreach ( $this->registered_styles as $selector => $rules ) {
Expand Down Expand Up @@ -133,15 +144,15 @@ public function add_style( $key, $options ) {
}

/**
* Returns a CSS ruleset based on the instructions in STYLE_DEFINITIONS_METADATA.
* Returns a CSS ruleset based on the instructions in BLOCK_STYLE_DEFINITIONS_METADATA.
*
* @param string|array $style_value A single raw Gutenberg style attributes value for a CSS property.
* @param array $path An array of strings representing a path to the style value.
*
* @return array The class name for the added style.
*/
protected function get_style_css_rules( $style_value, $path ) {
$style_definition = _wp_array_get( static::STYLE_DEFINITIONS_METADATA, $path, null );
protected function get_block_style_css_rules( $style_value, $path ) {
$style_definition = _wp_array_get( static::BLOCK_STYLE_DEFINITIONS_METADATA, $path, null );

if ( $style_definition ) {
if (
Expand All @@ -157,24 +168,30 @@ protected function get_style_css_rules( $style_value, $path ) {

/**
* Returns an CSS ruleset destined to be inserted in an HTML `style` attribute.
* Styles are bundled based on the instructions in STYLE_DEFINITIONS_METADATA.
* Styles are bundled based on the instructions in BLOCK_STYLE_DEFINITIONS_METADATA.
*
* @param array $block_attributes An array of styles from a block's attributes.
* @param array $path An array of strings representing a path to the style value.
* @param array $block_styles An array of styles from a block's attributes.
* @param array $path An array of strings representing a path to the style value.
*
* @return string A CSS ruleset formatted to be placed in an HTML `style` attribute.
*/
public function get_inline_styles_from_attributes( $block_attributes, $path ) {
if ( ! is_array( $block_attributes ) || ! is_array( $path ) ) {
return;
public function get_inline_styles_from_attributes( $block_styles, $path ) {
$output = '';

if ( empty( $block_styles ) || empty( $path ) ) {
return $output;
}

$style_value = _wp_array_get( $block_styles, $path, null );

if ( empty( $style_value ) ) {
return $output;
}

$style_value = _wp_array_get( $block_attributes, $path, null );
$rules = $this->get_style_css_rules( $style_value, $path );
$output = '';
$rules = $this->get_block_style_css_rules( $style_value, $path );

foreach ( $rules as $rule => $value ) {
$output .= "{$rule}: {$value}; ";
$output .= "{$rule}:{$value};";
}
return $output;
}
Expand All @@ -184,20 +201,20 @@ public function get_inline_styles_from_attributes( $block_attributes, $path ) {
* Stores style rules for a given CSS selector (the key) and returns an associated classname.
*
* @param string $key A class name used to construct a key.
* @param array $block_attributes An array of styles from a block's attributes.
* @param array $block_styles An array of styles from a block's attributes.
* @param array $path An array of strings representing a path to the style value.
* @param array $options An array of options that add_style() accepts.
*
* @return string The class name for the added style.
*/
public function add_style_from_attributes( $key, $block_attributes, $path, $options ) {
if ( ! is_array( $block_attributes ) || ! is_array( $path ) ) {
public function add_style_from_attributes( $key, $block_styles, $path, $options ) {
if ( empty( $block_styles ) || empty( $path ) ) {
return;
}

$style_value = _wp_array_get( $block_attributes, $path, null );
$style_value = _wp_array_get( $block_styles, $path, null );

if ( ! $style_value ) {
if ( empty( $style_value ) ) {
return;
}

Expand Down
6 changes: 3 additions & 3 deletions lib/style-engine/style-definition-utils.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* WP_Style_Engine class
* WP_Style_Engine utility methods
*
* @package Gutenberg
*/
Expand All @@ -13,7 +13,7 @@
*
* @return array The class name for the added style.
*/
function gutenberg_get_style_engine_css_box_rules ( $style_value, $style_property ) {
function gutenberg_get_style_engine_css_box_rules( $style_value, $style_property ) {
$rules = array();

if ( ! $style_value ) {
Expand All @@ -25,7 +25,7 @@ function gutenberg_get_style_engine_css_box_rules ( $style_value, $style_propert
$rules[ "$style_property-$key" ] = $value; // . ' !important'; Challenge: deal with specificity that inline styles bring us. Maybe we could pass an option.
}
} else {
$rules[] = sprintf( "$style_property: %s;", $style_value );
$rules[] = sprintf( "$style_property:%s;", $style_value );
}
return $rules;
}
Expand Down

0 comments on commit 50ed43b

Please sign in to comment.