-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Style Engine: add typography to backend #40082
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a90494e
Initial commit: add typography to style engine
ramonjd 886caff
Adding whitespace to inline output
ramonjd 4e9ed63
Introduce spacing to inline styles
ramonjd e1cda2c
Removing generating styles based on an options path since we're not u…
ramonjd 9dff885
Perform kebab case normalization in the style engine.
ramonjd 3123b02
Remove unused arg $options
ramonjd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,16 +36,50 @@ class WP_Style_Engine { | |
* For example, `'padding' => 'array( 'top' => '1em' )` will return `array( 'padding-top' => '1em' )` | ||
*/ | ||
const BLOCK_STYLE_DEFINITIONS_METADATA = array( | ||
'spacing' => array( | ||
'spacing' => array( | ||
'padding' => array( | ||
'property_key' => 'padding', | ||
'path' => array( 'spacing', 'padding' ), | ||
'value_func' => 'static::get_css_box_rules', | ||
), | ||
'margin' => array( | ||
'property_key' => 'margin', | ||
'path' => array( 'spacing', 'margin' ), | ||
'value_func' => 'static::get_css_box_rules', | ||
), | ||
), | ||
'typography' => array( | ||
'fontSize' => array( | ||
'property_key' => 'font-size', | ||
'path' => array( 'typography', 'fontSize' ), | ||
'classname_schema' => 'has-%s-font-size', | ||
), | ||
'fontFamily' => array( | ||
'property_key' => 'font-family', | ||
'path' => array( 'typography', 'fontFamily' ), | ||
'classname_schema' => 'has-%s-font-family', | ||
), | ||
'fontStyle' => array( | ||
'property_key' => 'font-style', | ||
'path' => array( 'typography', 'fontStyle' ), | ||
), | ||
'fontWeight' => array( | ||
'property_key' => 'font-weight', | ||
'path' => array( 'typography', 'fontWeight' ), | ||
), | ||
'lineHeight' => array( | ||
'property_key' => 'line-height', | ||
'path' => array( 'typography', 'lineHeight' ), | ||
), | ||
'textDecoration' => array( | ||
'property_key' => 'text-decoration', | ||
'path' => array( 'typography', 'textDecoration' ), | ||
), | ||
'textTransform' => array( | ||
'property_key' => 'text-transform', | ||
'path' => array( 'typography', 'textTransform' ), | ||
), | ||
'letterSpacing' => array( | ||
'property_key' => 'letter-spacing', | ||
'path' => array( 'typography', 'letterSpacing' ), | ||
), | ||
), | ||
); | ||
|
@@ -77,17 +111,57 @@ protected function get_block_style_css_rules( $style_value, $path ) { | |
$style_definition = _wp_array_get( static::BLOCK_STYLE_DEFINITIONS_METADATA, $path, null ); | ||
|
||
if ( ! empty( $style_definition ) ) { | ||
// Style definitions can define a function to generate custom CSS rules. | ||
if ( | ||
isset( $style_definition['value_func'] ) && | ||
is_callable( $style_definition['value_func'] ) | ||
) { | ||
return call_user_func( $style_definition['value_func'], $style_value, $style_definition['property_key'] ); | ||
} else { | ||
return static::get_css_rules( $style_value, $style_definition['property_key'] ); | ||
} | ||
} | ||
|
||
return array(); | ||
} | ||
|
||
/** | ||
* Returns a classname built using a provided schema. | ||
* | ||
* @param array $block_styles An array of styles from a block's attributes. | ||
* Some values may contain slugs that need to be parsed using a schema. | ||
* | ||
* @return string A CSS classname. | ||
*/ | ||
public function get_classnames( $block_styles ) { | ||
$output = ''; | ||
|
||
if ( empty( $block_styles ) ) { | ||
return $output; | ||
} | ||
|
||
$classnames = array(); | ||
foreach ( self::BLOCK_STYLE_DEFINITIONS_METADATA as $definition_group ) { | ||
foreach ( $definition_group as $style_definition ) { | ||
$classname_value = _wp_array_get( $block_styles, $style_definition['path'], null ); | ||
|
||
if ( empty( $classname_value ) ) { | ||
continue; | ||
} | ||
|
||
$classname_value = _wp_to_kebab_case( $classname_value ); | ||
$style_definition = _wp_array_get( static::BLOCK_STYLE_DEFINITIONS_METADATA, $style_definition['path'], null ); | ||
// Right now we expect a classname pattern to be stored in BLOCK_STYLE_DEFINITIONS_METADATA. | ||
// One day, if there are no stored schemata, we could allow custom patterns or | ||
// generate classnames based on other properties | ||
// such as a path or a value or a prefix passed in options. | ||
$classnames[] = isset( $style_definition['classname_schema'] ) ? sprintf( $style_definition['classname_schema'], $classname_value ) : $classname_value; | ||
} | ||
} | ||
|
||
return implode( ' ', $classnames ); | ||
} | ||
|
||
/** | ||
* Returns an CSS ruleset. | ||
* Styles are bundled based on the instructions in BLOCK_STYLE_DEFINITIONS_METADATA. | ||
|
@@ -109,56 +183,49 @@ public function generate( $block_styles, $options = array() ) { | |
|
||
$rules = array(); | ||
|
||
// If a path to a specific block style is defined, only return rules for that style. | ||
if ( isset( $options['path'] ) && is_array( $options['path'] ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't actually use these feature, so I'm removing it. |
||
$style_value = _wp_array_get( $block_styles, $options['path'], null ); | ||
if ( empty( $style_value ) ) { | ||
return $output; | ||
} | ||
$rules = array_merge( $rules, $this->get_block_style_css_rules( $style_value, $options['path'] ) ); | ||
} else { | ||
// Otherwise build them all. | ||
foreach ( self::BLOCK_STYLE_DEFINITIONS_METADATA as $definition_group ) { | ||
foreach ( $definition_group as $style_definition ) { | ||
$style_value = _wp_array_get( $block_styles, $style_definition['path'], null ); | ||
if ( empty( $style_value ) ) { | ||
continue; | ||
} | ||
$rules = array_merge( $rules, $this->get_block_style_css_rules( $style_value, $style_definition['path'] ) ); | ||
foreach ( self::BLOCK_STYLE_DEFINITIONS_METADATA as $definition_group ) { | ||
foreach ( $definition_group as $style_definition ) { | ||
$style_value = _wp_array_get( $block_styles, $style_definition['path'], null ); | ||
if ( empty( $style_value ) ) { | ||
continue; | ||
} | ||
$rules = array_merge( $rules, $this->get_block_style_css_rules( $style_value, $style_definition['path'] ) ); | ||
} | ||
} | ||
|
||
if ( ! empty( $rules ) ) { | ||
// Generate inline style rules. | ||
if ( isset( $options['inline'] ) && true === $options['inline'] ) { | ||
foreach ( $rules as $rule => $value ) { | ||
$filtered_css = esc_html( safecss_filter_attr( "{$rule}:{$value}" ) ); | ||
$filtered_css = esc_html( safecss_filter_attr( "{$rule}: {$value}" ) ); | ||
if ( ! empty( $filtered_css ) ) { | ||
$output .= $filtered_css . ';'; | ||
$output .= $filtered_css . '; '; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return $output; | ||
return trim( $output ); | ||
} | ||
|
||
/** | ||
* Returns a CSS ruleset for box model styles such as margins, padding, and borders. | ||
* Default style value parser that returns a CSS ruleset. | ||
* If the input contains an array, it will treated like a box model | ||
* for styles such as margins, padding, and borders | ||
* | ||
* @param string|array $style_value A single raw Gutenberg style attributes value for a CSS property. | ||
* @param string $style_property The CSS property for which we're creating a rule. | ||
* | ||
* @return array The class name for the added style. | ||
*/ | ||
public static function get_css_box_rules( $style_value, $style_property ) { | ||
public static function get_css_rules( $style_value, $style_property ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To match the clientside naming. |
||
$rules = array(); | ||
|
||
if ( ! $style_value ) { | ||
return $rules; | ||
} | ||
|
||
// We assume box model-like properties. | ||
if ( is_array( $style_value ) ) { | ||
foreach ( $style_value as $key => $value ) { | ||
$rules[ "$style_property-$key" ] = $value; | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: check what this means
cc @ockham Do you know if this comment is still valid? Grabbed your name from 1e17b5a#diff-2c54d3c8305590cf826f83511a9fd85d5b405470addd62edc3183ff9118177fbR115
Or would we need the code permanently for backwards compatibility?