diff --git a/projects/packages/forms/src/contact-form/class-contact-form-field.php b/projects/packages/forms/src/contact-form/class-contact-form-field.php index db23fa5d948a..b4ab7691a9e4 100644 --- a/projects/packages/forms/src/contact-form/class-contact-form-field.php +++ b/projects/packages/forms/src/contact-form/class-contact-form-field.php @@ -103,52 +103,53 @@ class Contact_Form_Field extends Contact_Form_Shortcode { public function __construct( $attributes, $content = null, $form = null ) { $attributes = shortcode_atts( array( - 'label' => null, - 'togglelabel' => null, - 'type' => 'text', - 'required' => false, - 'requiredtext' => null, - 'options' => array(), - 'optionsdata' => array(), - 'id' => null, - 'style' => null, - 'fieldbackgroundcolor' => null, - 'buttonbackgroundcolor' => null, - 'buttonborderradius' => null, - 'buttonborderwidth' => null, - 'textcolor' => null, - 'default' => null, - 'values' => null, - 'placeholder' => null, - 'class' => null, - 'width' => null, - 'consenttype' => null, - 'dateformat' => null, - 'implicitconsentmessage' => null, - 'explicitconsentmessage' => null, - 'borderradius' => null, - 'borderwidth' => null, - 'lineheight' => null, - 'labellineheight' => null, - 'bordercolor' => null, - 'inputcolor' => null, - 'labelcolor' => null, - 'labelfontsize' => null, - 'fieldfontsize' => null, - 'labelclasses' => null, - 'labelstyles' => null, - 'inputclasses' => null, - 'inputstyles' => null, - 'optionclasses' => null, - 'optionstyles' => null, - 'min' => null, - 'max' => null, - 'maxfiles' => null, - 'fieldwrapperclasses' => null, - 'outlinestyledata' => array(), - 'outlinestyleclasses' => null, - 'optionsclasses' => null, - 'optionsstyles' => null, + 'label' => null, + 'togglelabel' => null, + 'type' => 'text', + 'required' => false, + 'requiredtext' => null, + 'options' => array(), + 'optionsdata' => array(), + 'id' => null, + 'style' => null, + 'fieldbackgroundcolor' => null, + 'buttonbackgroundcolor' => null, + 'buttonborderradius' => null, + 'buttonborderwidth' => null, + 'textcolor' => null, + 'default' => null, + 'values' => null, + 'placeholder' => null, + 'class' => null, + 'width' => null, + 'consenttype' => null, + 'dateformat' => null, + 'implicitconsentmessage' => null, + 'explicitconsentmessage' => null, + 'borderradius' => null, + 'borderwidth' => null, + 'lineheight' => null, + 'labellineheight' => null, + 'bordercolor' => null, + 'inputcolor' => null, + 'labelcolor' => null, + 'labelfontsize' => null, + 'fieldfontsize' => null, + 'labelclasses' => null, + 'labelstyles' => null, + 'inputclasses' => null, + 'inputstyles' => null, + 'optionclasses' => null, + 'optionstyles' => null, + 'min' => null, + 'max' => null, + 'maxfiles' => null, + 'fieldwrapperclasses' => null, + 'stylevariationattributes' => array(), + 'stylevariationclasses' => null, + 'stylevariationstyles' => null, + 'optionsclasses' => null, + 'optionsstyles' => null, ), $attributes, 'contact-field' @@ -830,19 +831,19 @@ public function render_radio_field( $id, $label, $value, $class, $required, $req $fieldset_id = "id='" . esc_attr( "$id-label" ) . "'"; if ( $is_outlined_style ) { - $outline_styles = $this->get_attribute( 'outlinestyledata' ); + $style_variation_attributes = $this->get_attribute( 'stylevariationattributes' ); - if ( ! empty( $outline_styles ) ) { - $outline_styles = json_decode( html_entity_decode( $outline_styles, ENT_COMPAT ), true ); + if ( ! empty( $style_variation_attributes ) ) { + $style_variation_attributes = json_decode( html_entity_decode( $style_variation_attributes, ENT_COMPAT ), true ); } // When there's an outlined style, and border radius is set, the existing inline border radius is overridden to apply // a limit of `100px` to the radius on the x axis. This achieves the same look and feel as other fields // that use the notch html (`notched-label__leading` has a max-width of `100px` to prevent it from getting too wide). // It prevents large border radius values from disrupting the look and feel of the fields. - if ( isset( $outline_styles['border']['radius'] ) ) { + if ( isset( $style_variation_attributes['border']['radius'] ) ) { $options_styles = $options_styles ?? ''; - $radius = $outline_styles['border']['radius']; + $radius = $style_variation_attributes['border']['radius']; $has_split_radius_values = is_array( $radius ); $top_left_radius = $has_split_radius_values ? $radius['topLeft'] : $radius; $top_right_radius = $has_split_radius_values ? $radius['topRight'] : $radius; @@ -1233,19 +1234,19 @@ public function render_checkbox_multiple_field( $id, $label, $value, $class, $re $fieldset_id = "id='" . esc_attr( "$id-label" ) . "'"; if ( $is_outlined_style ) { - $outline_styles = $this->get_attribute( 'outlinestyledata' ); + $style_variation_attributes = $this->get_attribute( 'stylevariationattributes' ); - if ( ! empty( $outline_styles ) ) { - $outline_styles = json_decode( html_entity_decode( $outline_styles, ENT_COMPAT ), true ); + if ( ! empty( $style_variation_attributes ) ) { + $style_variation_attributes = json_decode( html_entity_decode( $style_variation_attributes, ENT_COMPAT ), true ); } // When there's an outlined style, and border radius is set, the existing inline border radius is overridden to apply // a limit of `100px` to the radius on the x axis. This achieves the same look and feel as other fields // that use the notch html (`notched-label__leading` has a max-width of `100px` to prevent it from getting too wide). // It prevents large border radius values from disrupting the look and feel of the fields. - if ( isset( $outline_styles['border']['radius'] ) ) { + if ( isset( $style_variation_attributes['border']['radius'] ) ) { $options_styles = $options_styles ?? ''; - $radius = $outline_styles['border']['radius']; + $radius = $style_variation_attributes['border']['radius']; $has_split_radius_values = is_array( $radius ); $top_left_radius = $has_split_radius_values ? $radius['topLeft'] : $radius; $top_right_radius = $has_split_radius_values ? $radius['topRight'] : $radius; @@ -1543,23 +1544,17 @@ class=" ' . $classes . '" * } */ private function get_form_variation_style_properties( $form_style = 'outlined' ) { - $style_attrs = ''; - $css_vars = ''; - $outline_styles = $this->get_attribute( 'outlinestyledata' ); - $outline_classes = $this->get_attribute( 'outlinestyleclasses' ); - $block_name = 'jetpack/input'; + $css_vars = ''; + $variation_attributes = $this->get_attribute( 'stylevariationattributes' ); + $variation_attributes = ! empty( $variation_attributes ) ? json_decode( html_entity_decode( $variation_attributes, ENT_COMPAT ), true ) : array(); + $variation_classes = $this->get_attribute( 'stylevariationclasses' ); + $variation_style = $this->get_attribute( 'stylevariationstyles' ); + $block_name = 'jetpack/input'; if ( $this->maybe_override_type() === 'radio' || $this->maybe_override_type() === 'checkbox-multiple' ) { $block_name = 'jetpack/options'; } - $outline_styles = ! empty( $outline_styles ) ? json_decode( html_entity_decode( $outline_styles, ENT_COMPAT ), true ) : array(); - $input_styles = $this->get_attribute( 'inputstyles' ); - - if ( ! empty( $input_styles ) ) { - $style_attrs = $input_styles; - } - $global_styles = wp_get_global_styles( array( 'border' ), array( @@ -1575,28 +1570,28 @@ private function get_form_variation_style_properties( $form_style = 'outlined' ) * For newer forms that use global styles or the block supports styles, this value will be empty and is ignored. */ $border_width_attribute = $this->get_attribute( 'borderwidth' ); - $legacy_border_size = ! empty( $border_width_attribute ) || $border_width_attribute === '0' ? $border_width_attribute . 'px' : $outline_styles['border']['width'] ?? null; + $legacy_border_size = ! empty( $border_width_attribute ) || $border_width_attribute === '0' ? $border_width_attribute . 'px' : $variation_attributes['border']['width'] ?? null; $legacy_border_size = is_numeric( $legacy_border_size ) ? $legacy_border_size . 'px' : $legacy_border_size; - $legacy_border_radius = $outline_styles['border']['radius'] ?? null; + $legacy_border_radius = $variation_attributes['border']['radius'] ?? null; $legacy_border_radius = is_numeric( $legacy_border_radius ) ? $legacy_border_radius . 'px' : $legacy_border_radius; $border_top_size = $legacy_border_size ?? - $outline_styles['border']['top']['width'] ?? + $variation_attributes['border']['top']['width'] ?? $global_styles['width'] ?? $global_styles['top']['width'] ?? null; $border_right_size = $legacy_border_size ?? - $outline_styles['border']['right']['width'] ?? + $variation_attributes['border']['right']['width'] ?? $global_styles['width'] ?? $global_styles['right']['width'] ?? null; $border_bottom_size = $legacy_border_size ?? - $outline_styles['border']['bottom']['width'] ?? + $variation_attributes['border']['bottom']['width'] ?? $global_styles['width'] ?? $global_styles['bottom']['width'] ?? null; $border_left_size = $legacy_border_size ?? - $outline_styles['border']['left']['width'] ?? + $variation_attributes['border']['left']['width'] ?? $global_styles['width'] ?? $global_styles['left']['width'] ?? null; @@ -1628,9 +1623,9 @@ private function get_form_variation_style_properties( $form_style = 'outlined' ) } return array( - 'style' => $style_attrs, + 'style' => $variation_style, 'css_vars' => $css_vars, - 'class_name' => $outline_classes, + 'class_name' => $variation_classes, ); } 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 aff6bc7ed742..dfc66acbef82 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 @@ -470,9 +470,8 @@ public static function block_attributes_to_shortcode_attributes( $atts, $type, $ /* Borders for the outlined notched HTML. */ - $outlined_style_data = self::get_outlined_style_attributes( $block_name, $inner_block['attrs'] ); - $atts['outlinestyledata'] = $outlined_style_data['outlinestyledata'] ?? null; - $atts['outlinestyleclasses'] = $outlined_style_data['outlinestyleclasses'] ?? null; + $style_variation_data = self::get_style_variation_shortcode_attributes( $block_name, $inner_block['attrs'] ); + $atts = array_merge( $atts, $style_variation_data ); $add_block_style_classes_to_field_wrapper = true; } @@ -523,9 +522,8 @@ public static function block_attributes_to_shortcode_attributes( $atts, $type, $ /* Borders for the outlined notched HTML. */ - $outlined_style_data = self::get_outlined_style_attributes( $block_name, $inner_block['attrs'] ); - $atts['outlinestyledata'] = $outlined_style_data['outlinestyledata']; - $atts['outlinestyleclasses'] = $outlined_style_data['outlinestyleclasses']; + $style_variation_atts = self::get_style_variation_shortcode_attributes( $block_name, $inner_block['attrs'] ); + $atts = array_merge( $atts, $style_variation_atts ); $add_block_style_classes_to_field_wrapper = true; } } @@ -552,7 +550,7 @@ public static function block_attributes_to_shortcode_attributes( $atts, $type, $ /** * Returns the form "Outlined" style classes and styles. - * Important: The "Outlined" style is somewhat different as it uses custom HTML to create a border around the field. + * Important: The "Outlined" style is somewhat different as it uses custom HTML to create a border around the field's label. * When applying styles to the control, background and border styles are applied to the custom HTML, not the input itself. * * @param string $block_name - the block name. @@ -560,30 +558,33 @@ public static function block_attributes_to_shortcode_attributes( $atts, $type, $ * * @return array */ - protected static function get_outlined_style_attributes( $block_name, $attrs ) { - $outlined_style_data = array(); - $attributes_for_outlined_style = array(); + protected static function get_style_variation_shortcode_attributes( $block_name, $attrs ) { + $picked_attributes = array(); + // For style variations like the outlined style, we only care about porting specific attributes like background color and border + // to the custom label HTML, so we pick those attributes and ignore the rest. if ( isset( $attrs['backgroundColor'] ) ) { - $attributes_for_outlined_style['backgroundColor'] = $attrs['backgroundColor']; + $picked_attributes['backgroundColor'] = $attrs['backgroundColor']; } if ( isset( $attrs['style']['border'] ) ) { - $attributes_for_outlined_style['style']['border'] = $attrs['style']['border']; + $picked_attributes['style']['border'] = $attrs['style']['border']; } if ( isset( $attrs['borderColor'] ) ) { - $attributes_for_outlined_style['borderColor'] = $attrs['borderColor']; + $picked_attributes['borderColor'] = $attrs['borderColor']; } if ( isset( $attrs['style']['color']['background'] ) ) { - $attributes_for_outlined_style['style']['color']['background'] = $attrs['style']['color']['background']; + $picked_attributes['style']['color']['background'] = $attrs['style']['color']['background']; } - $outlined_attrs = self::get_block_support_classes_and_styles( $block_name, $attributes_for_outlined_style ); - $outlined_style_data['outlinestyledata'] = isset( $attributes_for_outlined_style['style'] ) ? \wp_json_encode( $attributes_for_outlined_style['style'] ) : ''; - $outlined_style_data['outlinestyleclasses'] = isset( $outlined_attrs['class'] ) ? ' ' . $outlined_attrs['class'] : ''; - return $outlined_style_data; + $block_support_styles = self::get_block_support_classes_and_styles( $block_name, $picked_attributes ); + return array( + 'stylevariationattributes' => isset( $picked_attributes['style'] ) ? \wp_json_encode( $picked_attributes['style'] ) : '', + 'stylevariationclasses' => isset( $block_support_styles['class'] ) ? ' ' . $block_support_styles['class'] : '', + 'stylevariationstyles' => isset( $block_support_styles['style'] ) ? $block_support_styles['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 0b26faba08bd..a82bfec0c12f 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 @@ -125,7 +125,7 @@ public function test_gutenblock_render_field_checkbox_multiple_shortcode() { // Render the shortcode. $shortcode = Contact_Form_Plugin::gutenblock_render_field_checkbox_multiple( array(), '', new WP_Block( $block ) ); - $expected = '[contact-field type="checkbox-multiple" label="Choose several options" labelclasses="wp-block-jetpack-label has-text-color has-swamp-green-color" optionsclasses="" options="truth,dare" optionsdata="[{"label":"truth","class":"has-text-color","style":"color:caramel; font-size:24px;"},{"label":"dare","class":"has-text-color","style":"color:gummy; font-size:24px;"}]" outlinestyledata="" outlinestyleclasses="" fieldwrapperclasses="wp-block-jetpack-field-checkbox-multiple"/]'; + $expected = '[contact-field type="checkbox-multiple" label="Choose several options" labelclasses="wp-block-jetpack-label has-text-color has-swamp-green-color" optionsclasses="" options="truth,dare" optionsdata="[{"label":"truth","class":"has-text-color","style":"color:caramel; font-size:24px;"},{"label":"dare","class":"has-text-color","style":"color:gummy; font-size:24px;"}]" stylevariationattributes="" stylevariationclasses="" stylevariationstyles="" fieldwrapperclasses="wp-block-jetpack-field-checkbox-multiple"/]'; $this->assertEquals( $expected, $shortcode, 'Shortcode is not as expected' ); } @@ -209,7 +209,7 @@ public function test_gutenblock_gutenblock_render_field_text_shortcode() { ), ); $shortcode = Contact_Form_Plugin::gutenblock_render_field_text( array(), '', new WP_Block( $block ) ); - $expected = '[contact-field type="text" label="Label" requiredText="Do it" labelclasses="wp-block-jetpack-label has-text-color" labelstyles="color:caramel; font-size:24px;" placeholder="hi!" min="1" max="10" inputclasses="wp-block-jetpack-input has-text-color has-border-color" inputstyles="color:toot; font-size:33rem; border-color:toot;border-width:1px;" outlinestyledata="{"border":{"color":"toot","width":"1px"}}" outlinestyleclasses=" has-border-color" fieldwrapperclasses="wp-block-jetpack-field-text"/]'; + $expected = '[contact-field type="text" label="Label" requiredText="Do it" labelclasses="wp-block-jetpack-label has-text-color" labelstyles="color:caramel; font-size:24px;" placeholder="hi!" min="1" max="10" inputclasses="wp-block-jetpack-input has-text-color has-border-color" inputstyles="color:toot; font-size:33rem; border-color:toot;border-width:1px;" stylevariationattributes="{"border":{"color":"toot","width":"1px"}}" stylevariationclasses=" has-border-color" stylevariationstyles="border-color:toot;border-width:1px;" fieldwrapperclasses="wp-block-jetpack-field-text"/]'; $this->assertEquals( $expected, $shortcode ); } @@ -282,7 +282,7 @@ public function test_gutenblock_gutenblock_render_field_radio() { // Render the shortcode. $shortcode = Contact_Form_Plugin::gutenblock_render_field_radio( array(), '', new WP_Block( $block ) ); - $expected = '[contact-field type="radio" label="Radio gaga" labelclasses="wp-block-jetpack-label has-text-color has-turmoil-purple-color" optionsclasses="" options="freddy,brian" optionsdata="[{"label":"freddy","class":"has-text-color","style":"color:reddo; font-size:24px;"},{"label":"brian","class":"has-text-color","style":"color:blueo; font-size:100rem;"}]" outlinestyledata="" outlinestyleclasses="" fieldwrapperclasses="wp-block-jetpack-field-radio"/]'; + $expected = '[contact-field type="radio" label="Radio gaga" labelclasses="wp-block-jetpack-label has-text-color has-turmoil-purple-color" optionsclasses="" options="freddy,brian" optionsdata="[{"label":"freddy","class":"has-text-color","style":"color:reddo; font-size:24px;"},{"label":"brian","class":"has-text-color","style":"color:blueo; font-size:100rem;"}]" stylevariationattributes="" stylevariationclasses="" stylevariationstyles="" fieldwrapperclasses="wp-block-jetpack-field-radio"/]'; $this->assertEquals( $expected, $shortcode, 'Shortcode is not as expected' ); } @@ -328,19 +328,20 @@ public static function data_provider_block_attributes_to_shortcode_attributes_wi return array( 'label and input' => array( 'expected' => array( - 'labelclasses' => 'wp-block-jetpack-label has-text-color has-accent-3-color', - 'labelstyles' => 'font-size:32px;', - 'inputclasses' => 'wp-block-jetpack-input has-text-color has-background has-border-color', - 'inputstyles' => 'color:swamp-green;background-color:swamp-red; font-size:24px;font-style:italic;font-weight:bold;line-height:1.5;letter-spacing:0.1em; border-color:swamp-blue;border-style:dashed;border-width:1px;', - 'label' => 'Label and Input', - 'requiredText' => 'Do it', - 'placeholder' => 'Yo', - 'min' => '1', - 'max' => '10', - 'type' => 'text', - 'fieldwrapperclasses' => 'wp-block-jetpack-field-text', - 'outlinestyleclasses' => ' has-background has-border-color', - 'outlinestyledata' => '{"border":{"color":"swamp-blue","width":"1px","style":"dashed"},"color":{"background":"swamp-red"}}', + 'labelclasses' => 'wp-block-jetpack-label has-text-color has-accent-3-color', + 'labelstyles' => 'font-size:32px;', + 'inputclasses' => 'wp-block-jetpack-input has-text-color has-background has-border-color', + 'inputstyles' => 'color:swamp-green;background-color:swamp-red; font-size:24px;font-style:italic;font-weight:bold;line-height:1.5;letter-spacing:0.1em; border-color:swamp-blue;border-style:dashed;border-width:1px;', + 'label' => 'Label and Input', + 'requiredText' => 'Do it', + 'placeholder' => 'Yo', + 'min' => '1', + 'max' => '10', + 'type' => 'text', + 'fieldwrapperclasses' => 'wp-block-jetpack-field-text', + 'stylevariationclasses' => ' has-background has-border-color', + '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;', ), 'inner_blocks' => array( array( @@ -426,18 +427,19 @@ public static function data_provider_block_attributes_to_shortcode_attributes_wi ), 'label and options' => array( 'expected' => array( - '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', - '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;', - 'outlinestyleclasses' => ' has-background', - 'outlinestyledata' => '{"border":{"top":{"color":"terrible-red","width":"2px","style":"solid","radius":"10px"}},"color":{"background":"green-tonight"}}', + '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', + '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;', ), 'inner_blocks' => array( array(