Repository navigation
Forms: fix custom class handling in contact form plugin #43587
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
Changes from all commits
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 |
|---|---|---|
|
|
@@ -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' : ''; | ||
| $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
Member
Author
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. 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, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 ); | ||
|
|
@@ -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', | ||
|
|
@@ -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', | ||
|
|
@@ -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
Member
Author
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. This tests the PR's main change. |
||
| 'inner_blocks' => array( | ||
| array( | ||
| 'blockName' => 'jetpack/label', | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 ); | ||
| } | ||
|
|
||
|
|
@@ -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"; | ||
| } | ||
|
Member
Author
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. 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
Member
Author
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. A lot of these tests are mixing up the order of expectation and actual. Expectation should come first. https://docs.phpunit.de/en/10.5/assertions.html#assertequals I just changed a few here. |
||
| 'div class attribute doesn\'t match' | ||
| ); | ||
| } | ||
|
|
@@ -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' | ||
| ); | ||
| } | ||
|
|
@@ -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' | ||
| ); | ||
| } | ||
|
|
||
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.
This was an oversight. It filters out empty values.
Otherwise
" "would return" -wrap"