Skip to content
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

Add a WP_Style_Engine_CSS_Declarations object #42043

Merged
merged 26 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a0c2c36
Add a new CSS_Rules object in the style engine
aristath Jun 29, 2022
9f0b4ba
Allow named instances
aristath Jun 29, 2022
477f330
styles should be minified
aristath Jun 29, 2022
55aad4b
use sanitize_key
aristath Jun 29, 2022
2a4f88d
minify CSS in more tests
aristath Jun 29, 2022
d9e6790
use the renamed classname
aristath Jun 29, 2022
efd1f0c
Revert "use the renamed classname"
aristath Jun 29, 2022
aff4162
require the class in tests
aristath Jun 29, 2022
54c7e51
Add back spaces
aristath Jun 30, 2022
8f4c704
Revert "minify CSS in more tests"
aristath Jun 29, 2022
20e4c95
Revert "styles should be minified"
aristath Jun 29, 2022
e3a6018
more tweaks
aristath Jun 30, 2022
3cdf5b1
remove unused methods
aristath Jun 30, 2022
2cd623a
Rename class to WP_Style_Engine_CSS_Declarations
aristath Jun 30, 2022
723a218
rename file
aristath Jun 30, 2022
fe9f6bf
fix class description
aristath Jun 30, 2022
9e1c2cc
Explain that the implementation is temporary pending a store
aristath Jun 30, 2022
a11409f
rename method for consistency
aristath Jun 30, 2022
00d872c
add missing space
aristath Jun 30, 2022
852154b
account for zero value
aristath Jun 30, 2022
d9af533
remove unnecessary check
aristath Jun 30, 2022
2dc98e1
Only add style if it passes safecss checks
aristath Jun 30, 2022
8a2914f
simplify
aristath Jun 30, 2022
db0dfe0
remove primitive store
aristath Jul 1, 2022
03ca712
Add object constructor
aristath Jul 1, 2022
26d71a5
replace/rename the WP_Style_Engine_CSS_Declarations class on build
aristath Jul 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ function gutenberg_is_experiment_enabled( $name ) {
if ( file_exists( __DIR__ . '/../build/style-engine/class-wp-style-engine-gutenberg.php' ) ) {
require_once __DIR__ . '/../build/style-engine/class-wp-style-engine-gutenberg.php';
}
if ( file_exists( __DIR__ . '/../build/style-engine/class-wp-style-engine-css-declarations-gutenberg.php' ) ) {
require_once __DIR__ . '/../build/style-engine/class-wp-style-engine-css-declarations-gutenberg.php';
}

// Block supports overrides.
require __DIR__ . '/block-supports/utils.php';
Expand Down
119 changes: 119 additions & 0 deletions packages/style-engine/class-wp-style-engine-css-declarations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php
/**
* WP_Style_Engine_CSS_Declarations
*
* Holds, sanitizes and prints CSS rules declarations
*
* @package Gutenberg
*/

if ( class_exists( 'WP_Style_Engine_CSS_Declarations' ) ) {
return;
}

/**
* Holds, sanitizes, processes and prints CSS styles rules declarations for the style engine.
*
* @access private
*/
class WP_Style_Engine_CSS_Declarations {

/**
* An array of styles (property => value pairs).
*
* @var array
*/
protected $styles = array();

/**
* Contructor for this object.
*
* If a `$styles` array is passed, it will be used to populate
* the initial $styles prop of the object by calling add_declarations().
*
* @param array $styles An array of styles (property => value pairs).
*/
public function __construct( $styles = array() ) {
if ( empty( $styles ) ) {
return;
}
$this->add_declarations( $styles );
}

/**
* Add a single declaration.
*
* @param string $property The CSS property.
* @param string $value The CSS value.
*
* @return void
*/
public function add_declaration( $property, $value ) {

// Sanitize the property.
$property = $this->sanitize_property( $property );
// Bail early if the property is empty.
if ( empty( $property ) ) {
return;
}

// Trim the value. If empty, bail early.
$value = trim( $value );
if ( '' === $value ) {
return;
}

// Add the style.
$this->styles[ $property ] = $value;
}

/**
* Add multiple declarations.
*
* @param array $declarations An array of declarations.
*
* @return void
*/
public function add_declarations( $declarations ) {
foreach ( $declarations as $property => $value ) {
$this->add_declaration( $property, $value );
}
}

/**
* Get the styles array.
*
* @return array
*/
public function get_styles() {
return $this->styles;
}

/**
* Get the CSS styles.
*
* @return string The CSS styles.
*/
public function get_styles_string() {
$styles_array = $this->get_styles();
$styles = '';
foreach ( $styles_array as $property => $value ) {
$css = esc_html( safecss_filter_attr( "{$property}: {$value}" ) );
if ( $css ) {
$styles .= $css . '; ';
}
}
return rtrim( $styles );
}

/**
* Sanitize property names.
*
* @param string $property The CSS property.
*
* @return string The sanitized property name.
*/
protected function sanitize_property( $property ) {
return sanitize_key( $property );
}
}
26 changes: 6 additions & 20 deletions packages/style-engine/class-wp-style-engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,32 +396,19 @@ public function generate( $block_styles, $options ) {
}

// Build CSS rules output.
$css_selector = isset( $options['selector'] ) ? $options['selector'] : null;
$filtered_css_declarations = array();

if ( ! empty( $css_declarations ) ) {
// Generate inline style declarations.
foreach ( $css_declarations as $css_property => $css_value ) {
$filtered_css_declaration = esc_html( safecss_filter_attr( "{$css_property}: {$css_value}" ) );
if ( ! empty( $filtered_css_declaration ) ) {
$filtered_css_declarations[] = $filtered_css_declaration . ';';
}
}
}
$css_selector = isset( $options['selector'] ) ? $options['selector'] : null;
$style_rules = new WP_Style_Engine_CSS_Declarations( $css_declarations );

// The return object.
$styles_output = array();
$css = $style_rules->get_styles_string();

// Return css, if any.
if ( ! empty( $filtered_css_declarations ) ) {
if ( ! empty( $css ) ) {
$styles_output['css'] = $css;
// Return an entire rule if there is a selector.
if ( $css_selector ) {
$css_rule = "$css_selector { ";
$css_rule .= implode( ' ', $filtered_css_declarations );
$css_rule .= ' }';
$styles_output['css'] = $css_rule;
} else {
$styles_output['css'] = implode( ' ', $filtered_css_declarations );
$styles_output['css'] = $css_selector . ' { ' . $css . ' }';
}
}

Expand All @@ -433,7 +420,6 @@ public function generate( $block_styles, $options ) {
return $styles_output;
}


/**
* Style value parser that returns a CSS definition array comprising style properties
* that have keys representing individual style properties, otherwise known as longhand CSS properties.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @subpackage style-engine
*/

require __DIR__ . '/../class-wp-style-engine-css-declarations.php';
require __DIR__ . '/../class-wp-style-engine.php';

/**
Expand Down
5 changes: 4 additions & 1 deletion tools/webpack/packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ const bundledPackagesPhpConfig = [
{
from: './packages/style-engine/',
to: 'build/style-engine/',
replaceClasses: [ 'WP_Style_Engine' ],
replaceClasses: [
'WP_Style_Engine_CSS_Declarations',
'WP_Style_Engine',
],
},
].map( ( { from, to, replaceClasses } ) => ( {
from: `${ from }/*.php`,
Expand Down