diff --git a/packages/style-engine/class-wp-style-engine-renderer.php b/packages/style-engine/class-wp-style-engine-renderer.php
new file mode 100644
index 0000000000000..fb9d5885f228a
--- /dev/null
+++ b/packages/style-engine/class-wp-style-engine-renderer.php
@@ -0,0 +1,78 @@
+enqueue_block_support_styles
+ */
+ public function render_registered_styles( $styles ) {
+ if ( empty( $styles ) ) {
+ return;
+ }
+
+ $output = '';
+
+ foreach ( $styles as $selector => $rules ) {
+ $output .= "\t$selector { ";
+ $output .= implode( ' ', $rules );
+ $output .= " }\n";
+ }
+
+ echo "\n";
+ }
+
+ /**
+ * Taken from gutenberg_enqueue_block_support_styles()
+ *
+ * This function takes care of adding inline styles
+ * in the proper place, depending on the theme in use.
+ *
+ * For block themes, it's loaded in the head.
+ * For classic ones, it's loaded in the body
+ * because the wp_head action happens before
+ * the render_block.
+ *
+ * @see gutenberg_enqueue_block_support_styles()
+ */
+ private function enqueue_block_support_styles() {
+ $action_hook_name = 'wp_footer';
+ if ( wp_is_block_theme() ) {
+ $action_hook_name = 'wp_head';
+ }
+ add_action(
+ $action_hook_name,
+ array( $this, 'render_registered_styles' )
+ );
+ }
+}
+
diff --git a/packages/style-engine/class-wp-style-engine-store.php b/packages/style-engine/class-wp-style-engine-store.php
index 7a6fb90173524..df0d1d1b65796 100644
--- a/packages/style-engine/class-wp-style-engine-store.php
+++ b/packages/style-engine/class-wp-style-engine-store.php
@@ -7,39 +7,20 @@
* @package Gutenberg
*/
-// Probably don't need this, but it was just helped to design things.
-if ( ! interface_exists( 'WP_Style_Engine_Store_Interface' ) ) {
- /**
- * Registers and retrieves stored styles.
- *
- * @access private
- */
- interface WP_Style_Engine_Store_Interface {
- /**
- * Register a style
- *
- * @param string $key Unique key for a $style_data object.
- * @param array $style_data Associative array of style information.
- * @return void
- */
- public function register( $key, $style_data );
-
- /**
- * Retrieves style data from the store.
- *
- * @param string $key Unique key for a $style_data object.
- * @return void
- */
- public function get( $key );
- }
+if ( class_exists( 'WP_Style_Engine_Store' ) ) {
+ return;
}
/**
* Registers and stores styles to be processed or rendered on the frontend.
*
+ * For each style category we could have a separate object, e.g.,
+ * $global_style_store = new WP_Style_Engine_Store();
+ * $block_supports_style_store = new WP_Style_Engine_Store();
+ *
* @access private
*/
-class WP_Style_Engine_Store implements WP_Style_Engine_Store_Interface {
+class WP_Style_Engine_Store {
/**
* Registered styles.
*
@@ -47,6 +28,25 @@ class WP_Style_Engine_Store implements WP_Style_Engine_Store_Interface {
*/
private $registered_styles = array();
+ /**
+ * A key to identify the store. Default value is 'global'.
+ *
+ * @var WP_Style_Engine_Store|null
+ */
+ private $store_key = 'global-styles';
+
+ /**
+ * Constructor.
+ *
+ * @param string $store_key A key/name/id to identify the store.
+ * @return void
+ */
+ public function __construct( $store_key = null ) {
+ if ( ! empty( $store_key ) ) {
+ $this->store_key = $store_key;
+ }
+ }
+
/**
* Register a style
*
@@ -70,6 +70,8 @@ public function register( $key, $style_data ) {
*
* @param string $key Optional unique key for a $style_data object to return a single style object.
* @param array? $style_data Associative array of style information.
+ *
+ * @return array Registered styles
*/
public function get( $key = null ) {
if ( isset( $this->registered_styles[ $key ] ) ) {
@@ -78,63 +80,3 @@ public function get( $key = null ) {
return $this->registered_styles;
}
}
-
-/*
-
-For each style category we could have a separate object:
-e.g.,
-
-$global_style_store = new WP_Style_Engine_Store();
-$block_supports_style_store = new WP_Style_Engine_Store();
-
-
-@TODO
-
-Work out enqueuing and rendering
-*/
- /**
- * Prints registered styles in the page head or footer.
- *
- * @see $this->enqueue_block_support_styles
-
- public function output_registered_block_support_styles() {
- if ( empty( $this->registered_block_support_styles ) ) {
- return;
- }
-
- $output = '';
-
- foreach ( $this->registered_block_support_styles as $selector => $rules ) {
- $output .= "\t$selector { ";
- $output .= implode( ' ', $rules );
- $output .= " }\n";
- }
-
- echo "\n";
- } */
-
- /**
- * Taken from gutenberg_enqueue_block_support_styles()
- *
- * This function takes care of adding inline styles
- * in the proper place, depending on the theme in use.
- *
- * For block themes, it's loaded in the head.
- * For classic ones, it's loaded in the body
- * because the wp_head action happens before
- * the render_block.
- *
- * @see gutenberg_enqueue_block_support_styles()
-
- private function enqueue_block_support_styles() {
- $action_hook_name = 'wp_footer';
- if ( wp_is_block_theme() ) {
- $action_hook_name = 'wp_head';
- }
- add_action(
- $action_hook_name,
- array( $this, 'output_registered_block_support_styles' )
- );
- } */
-
-
diff --git a/packages/style-engine/class-wp-style-engine.php b/packages/style-engine/class-wp-style-engine.php
index 5687a2f422186..f7745460916d0 100644
--- a/packages/style-engine/class-wp-style-engine.php
+++ b/packages/style-engine/class-wp-style-engine.php
@@ -30,6 +30,13 @@ class WP_Style_Engine {
*/
private static $instance = null;
+ /**
+ * Registered styles.
+ *
+ * @var WP_Style_Engine_Store|null
+ */
+ private $block_supports_store = null;
+
/**
* Style definitions that contain the instructions to
* parse/output valid Gutenberg styles from a block's attributes.
@@ -119,6 +126,13 @@ class WP_Style_Engine {
),
);
+ /**
+ * Gather internals.
+ */
+ public function __construct() {
+ $this->block_supports_store = new WP_Style_Engine_Store( 'block_supports' );
+ }
+
/**
* Utility method to retrieve the main instance of the class.
*
@@ -134,6 +148,19 @@ public static function get_instance() {
return self::$instance;
}
+ /**
+ * Global public interface to register block support styles support.
+ *
+ * @access private
+ *
+ * @param string $key Unique key for a $style_data object.
+ * @param array $style_data Associative array of style information.
+ * @return void
+ */
+ public function register_block_support_styles( $key, $style_data ) {
+ $this->block_supports_store->register( $key, $style_data );
+ }
+
/**
* Extracts the slug in kebab case from a preset string, e.g., "heavenly-blue" from 'var:preset|color|heavenlyBlue'.
*
@@ -204,20 +231,21 @@ protected static function get_css( $style_value, $style_definition ) {
* Styles are bundled based on the instructions in BLOCK_STYLE_DEFINITIONS_METADATA.
*
* @param array $block_styles An array of styles from a block's attributes.
- *
+ * @param array $options array(
+ * 'selector' => (string) When a selector is passed, `generate()` will return a full CSS rule `$selector { ...rules }`, otherwise a concatenated string of properties and values.
+ * );.
* @return array|null array(
* 'styles' => (string) A CSS ruleset formatted to be placed in an HTML `style` attribute or tag.
* 'classnames' => (string) Classnames separated by a space.
* );
*/
- public function generate( $block_styles ) {
+ public function generate( $block_styles, $options ) {
if ( empty( $block_styles ) || ! is_array( $block_styles ) ) {
return null;
}
$css_rules = array();
$classnames = array();
- $styles_output = array();
// Collect CSS and classnames.
foreach ( self::BLOCK_STYLE_DEFINITIONS_METADATA as $definition_group ) {
@@ -234,7 +262,10 @@ public function generate( $block_styles ) {
}
// Build CSS rules output.
- $css_output = '';
+ $selector = isset( $options['selector'] ) ? $options['selector'] : null;
+ $css = array();
+ $styles_output = array();
+
if ( ! empty( $css_rules ) ) {
// Generate inline style rules.
// In the future there might be a flag in the option to output
@@ -242,13 +273,22 @@ public function generate( $block_styles ) {
foreach ( $css_rules as $rule => $value ) {
$filtered_css = esc_html( safecss_filter_attr( "{$rule}: {$value}" ) );
if ( ! empty( $filtered_css ) ) {
- $css_output .= $filtered_css . '; ';
+ $css[] = $filtered_css . ';';
}
}
}
- if ( ! empty( $css_output ) ) {
- $styles_output['css'] = trim( $css_output );
+ // Return css, if any.
+ if ( ! empty( $css ) ) {
+ // Return an entire rule if there is a selector.
+ if ( $selector ) {
+ $style_block = "$selector { ";
+ $style_block .= implode( ' ', $css );
+ $style_block .= ' }';
+ $styles_output['css'] = $style_block;
+ } else {
+ $styles_output['css'] = implode( ' ', $css );
+ }
}
if ( ! empty( $classnames ) ) {
@@ -294,6 +334,8 @@ protected static function get_css_rules( $style_value, $style_property ) {
* Returns an CSS ruleset.
* Styles are bundled based on the instructions in BLOCK_STYLE_DEFINITIONS_METADATA.
*
+ * @access public
+ *
* @param array $block_styles An array of styles from a block's attributes.
*
* @return array|null array(
@@ -308,3 +350,21 @@ function wp_style_engine_generate( $block_styles ) {
}
return null;
}
+
+// @TODO Just testing!
+/**
+ * Global public interface to register block support styles support.
+ *
+ * @access public
+ *
+ * @param string $key Unique key for a $style_data object.
+ * @param array $style_data Associative array of style information.
+ * @return void
+ */
+function wp_style_engine_register_block_support_styles( $key, $style_data ) {
+ if ( class_exists( 'WP_Style_Engine' ) ) {
+ $style_engine = WP_Style_Engine::get_instance();
+ $style_engine->register_block_support_styles( $key, $style_data );
+ }
+}
+