Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
Grammar, formatting, removing class_exists checks
  • Loading branch information
ramonjd committed Sep 9, 2022
commit 57d81bc8c3032dbd27131ed2cb0ec713d8a7513b
24 changes: 7 additions & 17 deletions src/wp-includes/style-engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,12 @@
* }
*
* @return array {
* @type string $css A CSS ruleset or declarations block formatted to be placed in an HTML `style` attribute or tag.
* @type array<string, string> $declarations An array of property/value pairs representing parsed CSS declarations.
* @type string $classnames Classnames separated by a space.
* @type string $css A CSS ruleset or declarations block formatted to be placed in an HTML `style` attribute or tag.
* @type string[] $declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
* @type string $classnames Classnames separated by a space.
* }
*/
function wp_style_engine_get_styles( $block_styles, $options = array() ) {
if ( ! class_exists( 'WP_Style_Engine' ) ) {
return array();
}

$options = wp_parse_args(
$options,
array(
Expand Down Expand Up @@ -85,15 +81,14 @@ function wp_style_engine_get_styles( $block_styles, $options = array() ) {
* $css = wp_style_engine_get_stylesheet_from_css_rules( $css_rules );
* // Returns `.elephant-are-cool{color:gray;width:3em}`.
*
* @access public
* @since 6.1.0
*
* @param array $css_rules {
* Required. A collection of CSS rules.
*
* @type array ...$0 {
* @type string $selector A CSS selector.
* @type array<string, string> $declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
* @type string $selector A CSS selector.
* @type string[] $declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
* }
* }
* @param array $options {
Expand All @@ -105,10 +100,10 @@ function wp_style_engine_get_styles( $block_styles, $options = array() ) {
* @type bool $prettify Whether to add new lines and indents to output. Default is the test of whether the global constant `SCRIPT_DEBUG` is defined.
* }
*
* @return string A compiled CSS string.
* @return string A string of compiled CSS declarations, or empty string.
*/
function wp_style_engine_get_stylesheet_from_css_rules( $css_rules, $options = array() ) {
if ( ! class_exists( 'WP_Style_Engine' ) || empty( $css_rules ) ) {
if ( empty( $css_rules ) ) {
return '';
}

Expand Down Expand Up @@ -142,7 +137,6 @@ function wp_style_engine_get_stylesheet_from_css_rules( $css_rules, $options = a
/**
* Returns compiled CSS from a store, if found.
*
* @access public
* @since 6.1.0
*
* @param string $context A valid context name, corresponding to an existing store key.
Expand All @@ -156,9 +150,5 @@ function wp_style_engine_get_stylesheet_from_css_rules( $css_rules, $options = a
* @return string A compiled CSS string.
*/
function wp_style_engine_get_stylesheet_from_context( $context, $options = array() ) {
if ( ! class_exists( 'WP_Style_Engine' ) || empty( $context ) ) {
return '';
}

return WP_Style_Engine::compile_stylesheet_from_css_rules( WP_Style_Engine::get_store( $context )->get_all_rules(), $options );
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ class WP_Style_Engine_CSS_Declarations {
*
* @since 6.1.0
*
* @param array $declarations An array of declarations (property => value pairs).
* @param string[] $declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
*/
public function __construct( $declarations = array() ) {
$this->add_declarations( $declarations );
}

/**
* Add a single declaration.
* Adds a single declaration.
*
* @since 6.1.0
*
Expand All @@ -53,28 +53,27 @@ public function __construct( $declarations = array() ) {
* @return WP_Style_Engine_CSS_Declarations Returns the object to allow chaining methods.
*/
public function add_declaration( $property, $value ) {

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

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

// Add the declaration property/value pair.
// Adds the declaration property/value pair.
$this->declarations[ $property ] = $value;

return $this;
}

/**
* Remove a single declaration.
* Removes a single declaration.
*
* @since 6.1.0
*
Expand All @@ -88,7 +87,7 @@ public function remove_declaration( $property ) {
}

/**
* Add multiple declarations.
* Adds multiple declarations.
*
* @since 6.1.0
*
Expand All @@ -104,7 +103,7 @@ public function add_declarations( $declarations ) {
}

/**
* Remove multiple declarations.
* Removes multiple declarations.
*
* @since 6.1.0
*
Expand All @@ -120,7 +119,7 @@ public function remove_declarations( $properties = array() ) {
}

/**
* Get the declarations array.
* Gets the declarations array.
*
* @since 6.1.0
*
Expand All @@ -139,7 +138,7 @@ public function get_declarations() {
* @param string $value The value to be filtered.
* @param string $spacer The spacer between the colon and the value. Defaults to an empty string.
*
* @return string The filtered declaration as a single string.
* @return string The filtered declaration or an empty string.
*/
protected static function filter_declaration( $property, $value, $spacer = '' ) {
$filtered_value = wp_strip_all_tags( $value, true );
Expand Down Expand Up @@ -177,7 +176,7 @@ public function get_declarations_string( $should_prettify = false, $indent_count
}

/**
* Sanitize property names.
* Sanitizes property names.
*
* @since 6.1.0
*
Expand Down
24 changes: 12 additions & 12 deletions src/wp-includes/style-engine/class-wp-style-engine-css-rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ class WP_Style_Engine_CSS_Rule {
*
* @since 6.1.0
*
* @param string $selector The CSS selector.
* @param array|WP_Style_Engine_CSS_Declarations $declarations An array of declarations (property => value pairs),
* or a WP_Style_Engine_CSS_Declarations object.
* @param string $selector The CSS selector.
* @param string[]|WP_Style_Engine_CSS_Declarations $declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ),
* or a WP_Style_Engine_CSS_Declarations object.
*/
public function __construct( $selector = '', $declarations = array() ) {
$this->set_selector( $selector );
$this->add_declarations( $declarations );
}

/**
* Set the selector.
* Sets the selector.
*
* @since 6.1.0
*
Expand All @@ -66,7 +66,7 @@ public function set_selector( $selector ) {
}

/**
* Set the declarations.
* Sets the declarations.
*
* @since 6.1.0
*
Expand All @@ -92,18 +92,18 @@ public function add_declarations( $declarations ) {
}

/**
* Get the declarations object.
* Gets the declarations object.
*
* @since 6.1.0
*
* @return WP_Style_Engine_CSS_Declarations
* @return WP_Style_Engine_CSS_Declarations The declarations object.
*/
public function get_declarations() {
return $this->declarations;
}

/**
* Get the full selector.
* Gets the full selector.
*
* @since 6.1.0
*
Expand All @@ -114,7 +114,7 @@ public function get_selector() {
}

/**
* Get the CSS.
* Gets the CSS.
*
* @since 6.1.0
*
Expand All @@ -126,15 +126,15 @@ public function get_selector() {
public function get_css( $should_prettify = false, $indent_count = 0 ) {
$rule_indent = $should_prettify ? str_repeat( "\t", $indent_count ) : '';
$declarations_indent = $should_prettify ? $indent_count + 1 : 0;
$new_line = $should_prettify ? "\n" : '';
$space = $should_prettify ? ' ' : '';
$suffix = $should_prettify ? "\n" : '';
$spacer = $should_prettify ? ' ' : '';
$selector = $should_prettify ? str_replace( ',', ",\n", $this->get_selector() ) : $this->get_selector();
$css_declarations = $this->declarations->get_declarations_string( $should_prettify, $declarations_indent );

if ( empty( $css_declarations ) ) {
return '';
}

return "{$rule_indent}{$selector}{$space}{{$new_line}{$css_declarations}{$new_line}{$rule_indent}}";
return "{$rule_indent}{$selector}{$spacer}{{$suffix}{$css_declarations}{$suffix}{$rule_indent}}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class WP_Style_Engine_CSS_Rules_Store {
protected $rules = array();

/**
* Get an instance of the store.
* Gets an instance of the store.
*
* @since 6.1.0
*
Expand All @@ -67,7 +67,7 @@ public static function get_store( $store_name = 'default' ) {
}

/**
* Get an array of all available stores.
* Gets an array of all available stores.
*
* @since 6.1.0
*
Expand All @@ -89,7 +89,7 @@ public static function remove_all_stores() {
}

/**
* Set the store name.
* Sets the store name.
*
* @since 6.1.0
*
Expand All @@ -102,7 +102,7 @@ public function set_name( $name ) {
}

/**
* Get the store name.
* Gets the store name.
*
* @since 6.1.0
*
Expand All @@ -113,7 +113,7 @@ public function get_name() {
}

/**
* Get an array of all rules.
* Gets an array of all rules.
*
* @since 6.1.0
*
Expand All @@ -124,7 +124,7 @@ public function get_all_rules() {
}

/**
* Get a WP_Style_Engine_CSS_Rule object by its selector.
* Gets a WP_Style_Engine_CSS_Rule object by its selector.
* If the rule does not exist, it will be created.
*
* @since 6.1.0
Expand All @@ -150,7 +150,7 @@ public function add_rule( $selector ) {
}

/**
* Remove a selector from the store.
* Removes a selector from the store.
*
* @since 6.1.0
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function add_store( $store ) {
/**
* Adds rules to be processed.
*
* @since 6.1.0 a store to t
* @since 6.1.0
*
* @param WP_Style_Engine_CSS_Rule|WP_Style_Engine_CSS_Rule[] $css_rules A single, or an array of, WP_Style_Engine_CSS_Rule objects from a store or otherwise.
*
Expand Down
Loading