-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add link color as an element mechanism.
- Loading branch information
1 parent
3dd8409
commit 30e1f1f
Showing
7 changed files
with
180 additions
and
31 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
/** | ||
* Elements styles block support. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Render out the duotone stylesheet and SVG. | ||
* | ||
* @param string $block_content Rendered block content. | ||
* @param array $block Block object. | ||
* @return string Filtered block content. | ||
*/ | ||
function gutenberg_render_elements_support( $block_content, $block ) { | ||
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); | ||
$link_color = _wp_array_get( $block['attrs'], array( 'style', 'elements', 'link', 'color', 'text' ), null ); | ||
|
||
/* | ||
* For now we only care about link color. | ||
* This code in the future when we have a public API | ||
* should take advantage of WP_Theme_JSON::compute_style_properties | ||
* and work for any element and style. | ||
*/ | ||
if ( null === $link_color ) { | ||
return $block_content; | ||
} | ||
$class_name = 'wp-block-elements-container-' . uniqid(); | ||
|
||
if ( strpos( $link_color, 'var:preset|color|' ) !== false ) { | ||
// Get the name from the string and add proper styles. | ||
$index_to_splice = strrpos( $link_color, '|' ) + 1; | ||
$link_color_name = substr( $link_color, $index_to_splice ); | ||
$link_color = "var(--wp--preset--color--$link_color_name)"; | ||
} | ||
|
||
$style = implode( | ||
"\n", | ||
array( | ||
'<style>', | ||
".$class_name a{", | ||
"\tcolor: $link_color;", | ||
'}', | ||
"</style>\n", | ||
) | ||
); | ||
|
||
// Like the layout hook this assumes the hook only applies to blocks with a single wrapper. | ||
$content = preg_replace( | ||
'/' . preg_quote( 'class="', '/' ) . '/', | ||
'class="' . $class_name . ' ', | ||
$block_content, | ||
1 | ||
); | ||
return $content . $style; | ||
|
||
} | ||
|
||
|
||
add_filter( 'render_block', 'gutenberg_render_elements_support', 10, 2 ); |
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
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
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