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

Style engine:Style Engine Rules & Store objects tests #42408

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 2 additions & 5 deletions lib/block-supports/elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,13 @@ function gutenberg_render_elements_support_styles( $pre_render, $block ) {
$link_block_styles = isset( $element_block_styles['link'] ) ? $element_block_styles['link'] : null;

if ( $link_block_styles ) {
$styles = gutenberg_style_engine_get_block_supports_styles(
gutenberg_style_engine_get_block_supports_styles(
$link_block_styles,
array(
'selector' => ".$class_name a",
'enqueue' => true,
)
);

if ( ! empty( $styles['css'] ) ) {
gutenberg_enqueue_block_support_styles( $styles['css'] );
}
}

return null;
Expand Down
6 changes: 6 additions & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ function gutenberg_is_experiment_enabled( $name ) {
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';
}
if ( file_exists( __DIR__ . '/../build/style-engine/class-wp-style-engine-css-rule-gutenberg.php' ) ) {
require_once __DIR__ . '/../build/style-engine/class-wp-style-engine-css-rule-gutenberg.php';
}
if ( file_exists( __DIR__ . '/../build/style-engine/class-wp-style-engine-css-rules-store-gutenberg.php' ) ) {
require_once __DIR__ . '/../build/style-engine/class-wp-style-engine-css-rules-store-gutenberg.php';
}

// Block supports overrides.
require __DIR__ . '/block-supports/utils.php';
Expand Down
113 changes: 113 additions & 0 deletions packages/style-engine/class-wp-style-engine-css-rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
/**
* WP_Style_Engine_CSS_Rule
*
* An object for CSS rules.
*
* @package Gutenberg
*/

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

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

/**
* The selector.
*
* @var string
*/
protected $selector;

/**
* The selector declarations.
*
* Contains a WP_Style_Engine_CSS_Declarations object.
*
* @var WP_Style_Engine_CSS_Declarations
*/
protected $declarations;

/**
* Constructor
*
* @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.
*/
public function __construct( $selector = '', $declarations = array() ) {
$this->set_selector( $selector );
$this->set_declarations( $declarations );
}

/**
* Set the selector.
*
* @param string $selector The CSS selector.
*
* @return WP_Style_Engine_CSS_Rule Returns the object to allow chaining of methods.
*/
public function set_selector( $selector ) {
if ( empty( $selector ) ) {
return;
}
$this->selector = $selector;

return $this;
}

/**
* Set the declarations.
*
* @param array|WP_Style_Engine_CSS_Declarations $declarations An array of declarations (property => value pairs),
* or a WP_Style_Engine_CSS_Declarations object.
*
* @return WP_Style_Engine_CSS_Rule Returns the object to allow chaining of methods.
*/
public function set_declarations( $declarations ) {
$is_declarations_object = ! is_array( $declarations );
$declarations_array = $is_declarations_object ? $declarations->get_declarations() : $declarations;

if ( null === $this->declarations && $is_declarations_object ) {
$this->declarations = $declarations;
} elseif ( null === $this->declarations ) {
$this->declarations = new WP_Style_Engine_CSS_Declarations( $declarations_array );
}
$this->declarations->add_declarations( $declarations_array );

return $this;
}

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

/**
* Get the full selector.
*
* @return string
*/
public function get_selector() {
return $this->selector;
}

/**
* Get the CSS.
*
* @return string
*/
public function get_css() {
return $this->get_selector() . ' {' . $this->declarations->get_declarations_string() . '}';
}
}
80 changes: 80 additions & 0 deletions packages/style-engine/class-wp-style-engine-css-rules-store.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/**
* WP_Style_Engine_CSS_Rules_Store
*
* A store for WP_Style_Engine_CSS_Rule objects.
*
* @package Gutenberg
*/

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

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

/**
* An array of named WP_Style_Engine_CSS_Rules_Store objects.
*
* @static
*
* @var WP_Style_Engine_CSS_Rules_Store[]
*/
protected static $stores = array();

/**
* An array of CSS Rules objects assigned to the store.
*
* @var WP_Style_Engine_CSS_Rule[]
*/
protected $rules = array();

/**
* Get an instance of the store.
*
* @param string $store_name The name of the store.
*
* @return WP_Style_Engine_CSS_Rules_Store
*/
public static function get_store( $store_name = 'default' ) {
if ( ! isset( static::$stores[ $store_name ] ) ) {
static::$stores[ $store_name ] = new static( $store_name );
}
return static::$stores[ $store_name ];
}
/**
* Get an array of all rules.
*
* @return WP_Style_Engine_CSS_Rule[]
*/
public function get_all_rules() {
return $this->rules;
}

/**
* Get a WP_Style_Engine_CSS_Rule object by its selector.
*
* @param string $selector The CSS selector.
*/
public function get_rule( $selector ) {

$selector = trim( $selector );

// Bail early if there is no selector.
if ( empty( $selector ) ) {
return;
}

// Create the rule if it doesn't exist.
if ( empty( $this->rules[ $selector ] ) ) {
$this->rules[ $selector ] = new WP_Style_Engine_CSS_Rule( $selector );
}

return $this->rules[ $selector ];
}
}
Loading