From c7debf0130bacf9420e33e298555f1b1025f21f0 Mon Sep 17 00:00:00 2001 From: Ramon Date: Fri, 23 May 2025 16:48:17 +1000 Subject: [PATCH 1/2] Forms: Enhance class handling in contact form plugin and tests - Updated class handling in `class-contact-form-plugin.php` to filter empty block style classes. - Modified test cases in `Contact_Form_Plugin_Test.php` and `Contact_Form_Test.php` to include additional attributes and ensure proper class wrapping for various field types. - Improved assertions to validate class attributes in rendered output. --- .../src/contact-form/class-contact-form-plugin.php | 7 ++++--- .../php/contact-form/Contact_Form_Plugin_Test.php | 13 ++++++++++--- .../tests/php/contact-form/Contact_Form_Test.php | 12 ++++++------ 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/projects/packages/forms/src/contact-form/class-contact-form-plugin.php b/projects/packages/forms/src/contact-form/class-contact-form-plugin.php index e53d409cedf5..097abca5cec6 100644 --- a/projects/packages/forms/src/contact-form/class-contact-form-plugin.php +++ b/projects/packages/forms/src/contact-form/class-contact-form-plugin.php @@ -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 ) ); + return array( 'fieldwrapperclasses' => $field_wrapper_classes, 'classes' => $classes_without_block_style, diff --git a/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php b/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php index a82bfec0c12f..950527321ce5 100644 --- a/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php +++ b/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php @@ -293,11 +293,12 @@ 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, $type = 'text' ) { $block = array( 'blockName' => 'jetpack/field-name', 'attrs' => array( @@ -305,7 +306,7 @@ public function test_block_attributes_to_shortcode_attributes_with_styles( $expe ), '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,6 +430,7 @@ 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', @@ -434,13 +438,16 @@ public static function data_provider_block_attributes_to_shortcode_attributes_wi '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', + ), 'inner_blocks' => array( array( 'blockName' => 'jetpack/label', diff --git a/projects/packages/forms/tests/php/contact-form/Contact_Form_Test.php b/projects/packages/forms/tests/php/contact-form/Contact_Form_Test.php index 405d71bb5ab8..680e57de30b7 100644 --- a/projects/packages/forms/tests/php/contact-form/Contact_Form_Test.php +++ b/projects/packages/forms/tests/php/contact-form/Contact_Form_Test.php @@ -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,7 +1236,9 @@ 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"; + } } } @@ -1245,8 +1246,8 @@ public function assertFieldClasses( $wrapper_div, $attributes ) { $wrapper_div_class = $wrapper_div->getAttribute( 'class' ); $this->assertEquals( - $wrapper_div_class, $css_class, + $wrapper_div_class, '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' ); } From fccd6c63bd83cd7637cd6cebb9720ef64cfd542e Mon Sep 17 00:00:00 2001 From: Ramon Date: Fri, 23 May 2025 17:05:25 +1000 Subject: [PATCH 2/2] Make innerblocks optional. Just for linting --- .../forms/tests/php/contact-form/Contact_Form_Plugin_Test.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php b/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php index 950527321ce5..6880bb0b3ca8 100644 --- a/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php +++ b/projects/packages/forms/tests/php/contact-form/Contact_Form_Plugin_Test.php @@ -298,7 +298,7 @@ public function test_gutenblock_gutenblock_render_field_radio() { * @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, $atts = array(), $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(