-
Notifications
You must be signed in to change notification settings - Fork 796
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
Gutenblocks: add new utility to get all classes for a given block #13302
Changes from all commits
e5a38a0
10708f1
f8914ba
b3546c7
6694fae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -689,4 +689,46 @@ public static function load_independent_blocks() { | |
} | ||
} | ||
} | ||
|
||
/** | ||
* Get CSS classes for a block. | ||
* | ||
* @since 7.7.0 | ||
* | ||
* @param string $slug Block slug. | ||
* @param array $attr Block attributes. | ||
* @param array $extra Potential extra classes you may want to provide. | ||
* | ||
* @return string $classes List of CSS classes for a block. | ||
*/ | ||
public static function block_classes( $slug = '', $attr, $extra = array() ) { | ||
if ( empty( $slug ) ) { | ||
return ''; | ||
} | ||
|
||
// Basic block name class. | ||
$classes = array( | ||
'wp-block-jetpack-' . $slug, | ||
); | ||
|
||
// Add alignment if provided. | ||
if ( | ||
! empty( $attr['align'] ) | ||
&& in_array( $attr['align'], array( 'left', 'center', 'right', 'wide', 'full' ), true ) | ||
) { | ||
array_push( $classes, 'align' . $attr['align'] ); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we even have to take care of alignment? I thought we could pretty much rely on Gutenberg's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think some of our blocks are little old in that we handled it manually. Gutenberg's support might be purely frontend side and backend side has to be handled manually like this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For reference, this is the commit I was thinking of: Automattic/wp-calypso@d675ab0 I'll see if that kind of change can be applied here... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, nevermind, that doesn't really apply to dynamic blocks/the server side/PHP. |
||
|
||
// Add custom classes if provided in the block editor. | ||
if ( ! empty( $attr['className'] ) ) { | ||
array_push( $classes, $attr['className'] ); | ||
} | ||
|
||
// Add any extra classes. | ||
if ( is_array( $extra ) && ! empty( $extra ) ) { | ||
$classes = array_merge( $classes, $extra ); | ||
} | ||
|
||
return implode( $classes, ' ' ); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -208,17 +208,18 @@ public function render_button( $attrs ) { | |
'powered_text' => __( 'Powered by WordPress.com', 'jetpack' ), | ||
); | ||
|
||
$classes = array( | ||
'wp-block-button__link', | ||
'components-button', | ||
'is-primary', | ||
'is-button', | ||
'wp-block-jetpack-' . self::$button_block_name, | ||
self::$css_classname_prefix . '-' . $data['id'], | ||
$classes = Jetpack_Gutenberg::block_classes( | ||
self::$button_block_name, | ||
$attrs, | ||
array( | ||
'wp-block-button__link', | ||
'components-button', | ||
'is-primary', | ||
'is-button', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not entirely sure classes like this belong into a top-level block element, that's pre-existing. (It might raise the question if the new util is really needed, or more of a symptom of suboptimal architectural choices, but oh well.) |
||
self::$css_classname_prefix . '-' . $data['id'], | ||
) | ||
); | ||
if ( isset( $attrs['className'] ) ) { | ||
array_push( $classes, $attrs['className'] ); | ||
} | ||
|
||
if ( isset( $attrs['submitButtonText'] ) ) { | ||
$data['button_label'] = $attrs['submitButtonText']; | ||
} | ||
|
@@ -249,7 +250,7 @@ public function render_button( $attrs ) { | |
esc_attr( $data['powered_text'] ), | ||
esc_attr( $data['id'] ), | ||
esc_attr( get_locale() ), | ||
esc_attr( implode( $classes, ' ' ) ), | ||
esc_attr( $classes ), | ||
esc_attr( $button_styles ), | ||
wp_kses( $data['button_label'], self::$tags_allowed_in_the_button ) | ||
); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just noting that (I think) setting
className:false
in block'ssupports
attributes would normally disable the primary class name as well but this always enforces it.I don't think we'll ever have any such blocks in Jetpack so it doesn't really matter, but wanted to note in case anyone looks up this PR later on for inspiration to implement this also in core Gutenberg.