Skip to content

Commit

Permalink
Toying around with a style engine store mainly.
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd committed Jun 22, 2022
1 parent 52e5549 commit 8c141e2
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 86 deletions.
78 changes: 78 additions & 0 deletions packages/style-engine/class-wp-style-engine-renderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
// @TODO we probably don't need this right now.
// Just toying around.
/**
* WP_Style_Engine_Renderer
*
* Renders CSS on the frontend.
*
* @package Gutenberg
*/

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

/**
* Renders CSS on the frontend..
*
* @access private
*/
class WP_Style_Engine_Renderer {
/**
* Constructor.
*
* @return void
*/
public function __construct() {
// @TODO some argument to determine how/where the styles are rendered.
// For example, we could enqueue specific inline styles like global styles, see: gutenberg_enqueue_global_styles().
//
}

/**
* Prints registered styles in the page head or footer.
*
* @see $this->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 "<style>\n$output</style>\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' )
);
}
}

114 changes: 28 additions & 86 deletions packages/style-engine/class-wp-style-engine-store.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,46 @@
* @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.
*
* @var WP_Style_Engine_Store|null
*/
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
*
Expand All @@ -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 ] ) ) {
Expand All @@ -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 "<style>\n$output</style>\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' )
);
} */


45 changes: 45 additions & 0 deletions packages/style-engine/class-wp-style-engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -215,6 +222,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.
*
Expand All @@ -230,6 +244,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'.
*
Expand Down Expand Up @@ -513,3 +540,21 @@ function wp_style_engine_generate( $block_styles, $options = array() ) {
}
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 );
}
}

0 comments on commit 8c141e2

Please sign in to comment.