Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -405,12 +405,13 @@ private static function get_block_style_classes( $classname = '' ) {
$block_style_classes = empty( $matches[0] ) ? '' : implode( ' ', $matches[0] );

if ( ! empty( $block_style_classes ) ) {
$wrap_classes = ! empty( $matches[0] ) ? ' ' . implode( '-wrap ', $matches[0] ) . '-wrap' : '';
$wrap_classes = ! empty( $matches[0] ) ? ' ' . implode( '-wrap ', array_filter( $matches[0] ) ) . '-wrap' : '';

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was an oversight. It filters out empty values.

Otherwise " " would return " -wrap"

$field_wrapper_classes = " $block_style_classes $wrap_classes";
// Remove block style classes from the original classname.
$classes_without_block_style = trim( preg_replace( '/is-style-([^\s]+)/i', '', $classname ) );
}

// Remove block style classes from the original classname.
$classes_without_block_style = trim( preg_replace( '/is-style-([^\s]+)/i', '', $classname ) );

Comment on lines +412 to +414

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the real meat of the PR.

Effectively we should always be returning whatever classes we can't process back to the block. It was within a condition before.

return array(
'fieldwrapperclasses' => $field_wrapper_classes,
'classes' => $classes_without_block_style,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,19 +293,20 @@ public function test_gutenblock_gutenblock_render_field_radio() {
* @dataProvider data_provider_block_attributes_to_shortcode_attributes_with_styles
*
* @param array $expected The expected shortcode attributes.
* @param array $atts The attributes of the shortcode block.
* @param array $inner_blocks The inner blocks of the block.
* @param string $type The type of the field.
*/
#[DataProvider( 'data_provider_block_attributes_to_shortcode_attributes_with_styles' )]
public function test_block_attributes_to_shortcode_attributes_with_styles( $expected, $inner_blocks, $type = 'text' ) {
public function test_block_attributes_to_shortcode_attributes_with_styles( $expected, $atts = array(), $inner_blocks = array(), $type = 'text' ) {
$block = array(
'blockName' => 'jetpack/field-name',
'attrs' => array(
'required' => false,
),
'innerBlocks' => $inner_blocks,
);
$shortcode_attributes = Contact_Form_Plugin::block_attributes_to_shortcode_attributes( array(), $type, new WP_Block( $block ) );
$shortcode_attributes = Contact_Form_Plugin::block_attributes_to_shortcode_attributes( $atts, $type, new WP_Block( $block ) );

// Sorting here so we don't have to care about the order of the attributes in the shortcode/data provider.
$expected_keys = array_keys( $expected );
Expand Down Expand Up @@ -343,6 +344,7 @@ public static function data_provider_block_attributes_to_shortcode_attributes_wi
'stylevariationattributes' => '{"border":{"color":"swamp-blue","width":"1px","style":"dashed"},"color":{"background":"swamp-red"}}',
'stylevariationstyles' => 'background-color:swamp-red; border-color:swamp-blue;border-style:dashed;border-width:1px;',
),
'atts' => array(),
'inner_blocks' => array(
array(
'blockName' => 'jetpack/label',
Expand Down Expand Up @@ -397,6 +399,7 @@ public static function data_provider_block_attributes_to_shortcode_attributes_wi
'type' => 'radio',
'fieldwrapperclasses' => 'wp-block-jetpack-field-radio',
),
'atts' => array(),
'inner_blocks' => array(
array(
'blockName' => 'jetpack/option',
Expand Down Expand Up @@ -427,20 +430,24 @@ public static function data_provider_block_attributes_to_shortcode_attributes_wi
),
'label and options' => array(
'expected' => array(
'class' => 'some-custom-class',
'labelclasses' => 'wp-block-jetpack-label has-text-color has-accent-3-color',
'labelstyles' => 'letter-spacing:0.1em;',
'options' => 'Option 1,Option 2',
'optionsdata' => '[{"label":"Option 1","class":"has-text-color has-sweet-potato-option-1-color","style":"font-size:24px;font-weight:bold;line-height:1.5;letter-spacing:0.1em;"},{"label":"Option 2","class":"has-text-color has-sweet-potato-option-2-color","style":"font-size:22px;font-weight:normal;"}]',
'label' => 'Label multiple options',
'type' => 'checkbox-multiple',
'requiredText' => 'Do it again',
'fieldwrapperclasses' => 'wp-block-jetpack-field-checkbox-multiple',
'fieldwrapperclasses' => 'wp-block-jetpack-field-checkbox-multiple is-style-button is-style-button-wrap',
'optionsclasses' => ' has-text-color has-background',
'optionsstyles' => 'color:blue-overture;background-color:green-tonight; border-top-width:2px;border-top-color:terrible-red;border-top-style:solid;',
'stylevariationclasses' => ' has-background',
'stylevariationattributes' => '{"border":{"top":{"color":"terrible-red","width":"2px","style":"solid","radius":"10px"}},"color":{"background":"green-tonight"}}',
'stylevariationstyles' => 'background-color:green-tonight; border-top-width:2px;border-top-color:terrible-red;border-top-style:solid;',
),
'atts' => array(
'className' => 'is-style-button some-custom-class',
),
Comment on lines +448 to +450

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tests the PR's main change.

'inner_blocks' => array(
array(
'blockName' => 'jetpack/label',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,6 @@ public function test_make_sure_checkbox_multiple_field_renders_as_expected() {
),
);
$expected_attributes = array_merge( $attributes, array( 'input_type' => 'checkbox' ) );

$this->assertValidFieldMultiField( $this->render_field( $attributes ), $expected_attributes );
}

Expand Down Expand Up @@ -1237,16 +1236,18 @@ public function assertFieldClasses( $wrapper_div, $attributes ) {
if ( isset( $attributes['class'] ) ) {
$wrapper_classes = explode( ' ', $attributes['class'] );
foreach ( $wrapper_classes as $wrapper_class ) {
$classes_wrap .= " {$wrapper_class}-wrap";
if ( $wrapper_class ) {
$classes_wrap .= " {$wrapper_class}-wrap";
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the changes in this file are janitorial. Here we want to ignore empty strings. Probably not needed but might avoid confusion when testing.

}
}

$css_class = "wp-block-jetpack-field-{$attributes['type']} grunion-field-{$attributes['type']}-wrap{$classes_wrap}{$input_classes_wrap}{$options_classes_wrap} grunion-field-wrap";
$wrapper_div_class = $wrapper_div->getAttribute( 'class' );

$this->assertEquals(
$wrapper_div_class,
$css_class,
$wrapper_div_class,
Comment on lines -1248 to +1250

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of these tests are mixing up the order of expectation and actual.

Expectation should come first.

assertEquals( mixed $expected, mixed $actual, string $message = '' )

https://docs.phpunit.de/en/10.5/assertions.html#assertequals

I just changed a few here.

'div class attribute doesn\'t match'
);
}
Expand Down Expand Up @@ -1294,10 +1295,9 @@ public function assertInputClasses( $input, $attributes ) {
$classes_input .= " {$input_class}";
}
}

$this->assertEquals(
$input->getAttribute( 'class' ),
$attributes['type'] . $classes_input . $input_classes_input . $options_classes_input . ' grunion-field',
$input->getAttribute( 'class' ),
'input class attribute doesn\'t match'
);
}
Expand Down Expand Up @@ -1335,8 +1335,8 @@ public function assertLabelClasses( $label, $attributes, $classes_prefix ) {
}

$this->assertEquals(
$label->getAttribute( 'class' ),
$classes_prefix . $label_classes_input . $options_classes_input,
$label->getAttribute( 'class' ),
'input class attribute doesn\'t match'
);
}
Expand Down