-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Style Engine: Try simple approach for consolidating styles into a sin…
…gle style tag
- Loading branch information
1 parent
69c57ce
commit 55e92f5
Showing
3 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
/** | ||
* WP_Style_Engine class | ||
* | ||
* @package Gutenberg | ||
*/ | ||
|
||
/** | ||
* Singleton class representing the style engine. | ||
* | ||
* Consolidates rendering block styles to reduce duplication and streamline | ||
* CSS styles generation. | ||
* | ||
* @since 6.0.0 | ||
*/ | ||
class WP_Style_Engine_Gutenberg { | ||
/** | ||
* Registered CSS styles. | ||
* | ||
* @since 5.5.0 | ||
* @var array | ||
*/ | ||
private $registered_styles = array(); | ||
|
||
/** | ||
* Container for the main instance of the class. | ||
* | ||
* @since 5.5.0 | ||
* @var WP_Style_Engine_Gutenberg|null | ||
*/ | ||
private static $instance = null; | ||
|
||
public function __construct() { | ||
// Borrows the logic from `gutenberg_enqueue_block_support_styles`. | ||
$action_hook_name = 'wp_footer'; | ||
if ( wp_is_block_theme() ) { | ||
$action_hook_name = 'wp_enqueue_scripts'; | ||
} | ||
add_action( | ||
$action_hook_name, | ||
array( $this, 'output_styles' ) | ||
); | ||
} | ||
|
||
/** | ||
* Utility method to retrieve the main instance of the class. | ||
* | ||
* The instance will be created if it does not exist yet. | ||
* | ||
* @return WP_Style_Engine_Gutenberg The main instance. | ||
*/ | ||
public static function get_instance() { | ||
if ( null === self::$instance ) { | ||
self::$instance = new self(); | ||
} | ||
|
||
return self::$instance; | ||
} | ||
|
||
public function add_style( $key, $value ) { | ||
$this->registered_styles[ $key ] = $value; | ||
} | ||
|
||
public function output_styles() { | ||
$style = implode( "\n", $this->registered_styles ); | ||
echo "<style>$style</style>\n"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters