Skip to content
Open
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
4 changes: 4 additions & 0 deletions projects/packages/forms/changelog/add-core-button-submit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Forms: allow using the Gutenberg Core Button block as the submit control in Contact Form. The block gets the same interactivity bindings and loading spinner as the Jetpack Button, and all form variations now insert a core button by default.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public static function register_block() {

add_filter( 'render_block_data', array( __CLASS__, 'find_nested_html_block' ), 10, 3 );
add_filter( 'render_block_core/html', array( __CLASS__, 'render_wrapped_html_block' ), 10, 2 );
add_filter( 'render_block_core/button', array( __CLASS__, 'render_submit_button' ), 10, 2 );
add_filter( 'jetpack_block_editor_feature_flags', array( __CLASS__, 'register_feature' ) );
add_filter( 'pre_render_block', array( __CLASS__, 'pre_render_contact_form' ), 10, 3 );

Expand Down Expand Up @@ -101,6 +102,35 @@ public static function render_wrapped_html_block( $content, $parsed_block ) {
return $content;
}

/**
* Add Jetpack Forms interactivity attributes to core/buttons blocks that live inside a contact form.
*
* @param string $content Rendered HTML of the core/button block.
* @param array $parsed_block Parsed block array.
* @return string Possibly modified HTML.
*/
public static function render_submit_button( $content, $parsed_block ) {
$class = $parsed_block['attrs']['className'] ?? '';
if ( ! str_contains( $class, 'jetpack-form-submit-button' ) ) {
return $content;
}

if ( ! class_exists( \WP_HTML_Tag_Processor::class ) ) {
return $content;
}

$p = new \WP_HTML_Tag_Processor( $content );
if ( ! $p->next_tag( array( 'tag_name' => 'button' ) ) ) {
return $content;
}

$p->set_attribute( 'data-wp-class--is-submitting', 'state.isSubmitting' );
$p->set_attribute( 'data-wp-bind--aria-disabled', 'state.isAriaDisabled' );
$p->set_attribute( 'data-wp-bind--disabled', 'state.isAriaDisabled' );

return $p->get_updated_html();
}

/**
* Register the Child blocks of Contact Form
* We are registering child blocks only when Contact Form plugin is Active
Expand Down
34 changes: 23 additions & 11 deletions projects/packages/forms/src/blocks/contact-form/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,13 @@ function JetpackContactFormEdit( {
[ clientId, steps ]
);

const submitButton = useFindBlockRecursively(
clientId,
block => block.name === 'jetpack/button'
const findButtonsBlock = useCallback(
block => block.name === 'core/buttons' || block.name === 'jetpack/button',
[]
);

const submitButton = useFindBlockRecursively( clientId, findButtonsBlock );

const { postTitle, hasAnyInnerBlocks, postAuthorEmail, selectedBlockClientId, onlySubmitBlock } =
useSelect(
select => {
Expand All @@ -222,6 +224,11 @@ function JetpackContactFormEdit( {
const { getUser } = select( coreStore );
const innerBlocksData = getBlocks( clientId );

const isSingleButtonBlock =
innerBlocksData.length === 1 &&
( innerBlocksData[ 0 ].name === 'core/buttons' ||
innerBlocksData[ 0 ].name === 'jetpack/button' );

const title = getEditedPostAttribute( 'title' );
const authorId = getEditedPostAttribute( 'author' );
const authorEmail = authorId && getUser( authorId )?.email;
Expand All @@ -231,8 +238,7 @@ function JetpackContactFormEdit( {
hasAnyInnerBlocks: innerBlocksData.length > 0,
postAuthorEmail: authorEmail,
selectedBlockClientId: selectedStepBlockId,
onlySubmitBlock:
innerBlocksData.length === 1 && innerBlocksData[ 0 ].name === 'jetpack/button',
onlySubmitBlock: isSingleButtonBlock,
};
},
[ clientId ]
Expand Down Expand Up @@ -330,7 +336,7 @@ function JetpackContactFormEdit( {
// Find the submit button
const submitButtonIndex = currentInnerBlocks.findIndex(
block =>
block.name === 'jetpack/button' &&
( block.name === 'core/buttons' || block.name === 'jetpack/button' ) &&
( block.attributes?.customVariant === 'submit' || block.attributes?.element === 'button' )
);

Expand Down Expand Up @@ -465,7 +471,9 @@ function JetpackContactFormEdit( {

// Helper functions
const findButtonBlock = () => {
const buttonIndex = currentInnerBlocks.findIndex( block => block.name === 'jetpack/button' );
const buttonIndex = currentInnerBlocks.findIndex(
block => block.name === 'core/buttons' || block.name === 'jetpack/button'
);
return buttonIndex !== -1
? {
block: currentInnerBlocks[ buttonIndex ],
Expand Down Expand Up @@ -706,10 +714,14 @@ function JetpackContactFormEdit( {
// Ensure we have a submit button at the end of the form.
if ( ! finalSubmitButton ) {
// Create a fresh submit button if none was found.
finalSubmitButton = createBlock( 'jetpack/button', {
element: 'button',
text: __( 'Submit', 'jetpack-forms' ),
} );
finalSubmitButton = createBlock( 'core/buttons', {}, [
createBlock( 'core/button', {
text: __( 'Submit', 'jetpack-forms' ),
type: 'submit',
tagName: 'button',
className: 'wp-block-jetpack-button jetpack-form-submit-button',
} ),
] );
}

const finalBlocks = [ ...flattenedInnerBlocks, finalSubmitButton ];
Expand Down
4 changes: 2 additions & 2 deletions projects/packages/forms/src/blocks/contact-form/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
flex: 1 1 100%;
}

.wp-block {
flex: 1 1 100%;
.wp-block:not(.wp-block-buttons):not(.wp-block-button) {
flex: 0 0 100%;
margin-top: 0;
margin-bottom: 0;
max-width: 100%;
Expand Down
120 changes: 84 additions & 36 deletions projects/packages/forms/src/blocks/contact-form/variations.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,20 @@ const variations = [
],
],
[
'jetpack/button',
{
text: __( 'Contact Us', 'jetpack-forms' ),
element: 'button',
lock: { remove: true },
},
'core/buttons',
{},
[
[
'core/button',
{
text: __( 'Contact us', 'jetpack-forms' ),
tagName: 'button',
className: 'jetpack-form-submit-button',
type: 'submit',
element: 'button',
},
],
],
],
],
attributes: {
Expand Down Expand Up @@ -145,12 +153,20 @@ const variations = [
],
],
[
'jetpack/button',
{
text: __( 'Send RSVP', 'jetpack-forms' ),
element: 'button',
lock: { remove: true },
},
'core/buttons',
{},
[
[
'core/button',
{
text: __( 'Send RSVP', 'jetpack-forms' ),
tagName: 'button',
className: 'jetpack-form-submit-button',
type: 'submit',
element: 'button',
},
],
],
],
],
attributes: {
Expand Down Expand Up @@ -298,12 +314,20 @@ const variations = [
],
],
[
'jetpack/button',
{
text: __( 'Send', 'jetpack-forms' ),
element: 'button',
lock: { remove: true },
},
'core/buttons',
{},
[
[
'core/button',
{
text: __( 'Send', 'jetpack-forms' ),
tagName: 'button',
className: 'jetpack-form-submit-button',
type: 'submit',
element: 'button',
},
],
],
],
],
attributes: {
Expand Down Expand Up @@ -487,12 +511,20 @@ const variations = [
],
],
[
'jetpack/button',
{
text: __( 'Book appointment', 'jetpack-forms' ),
element: 'button',
lock: { remove: true },
},
'core/buttons',
{},
[
[
'core/button',
{
text: __( 'Book appointment', 'jetpack-forms' ),
tagName: 'button',
className: 'jetpack-form-submit-button',
type: 'submit',
element: 'button',
},
],
],
],
],
attributes: {
Expand Down Expand Up @@ -633,12 +665,20 @@ const variations = [
],
],
[
'jetpack/button',
{
text: __( 'Send Feedback', 'jetpack-forms' ),
element: 'button',
lock: { remove: true },
},
'core/buttons',
{},
[
[
'core/button',
{
text: __( 'Send feedback', 'jetpack-forms' ),
tagName: 'button',
className: 'jetpack-form-submit-button',
type: 'submit',
element: 'button',
},
],
],
],
],
attributes: {
Expand Down Expand Up @@ -956,12 +996,20 @@ const variations = [
[ [ 'jetpack/label' ], [ 'jetpack/input', { type: 'checkbox' } ] ],
],
[
'jetpack/button',
{
text: __( 'Subscribe', 'jetpack-forms' ),
element: 'button',
lock: { remove: true },
},
'core/buttons',
{},
[
[
'core/button',
{
text: __( 'Subscribe', 'jetpack-forms' ),
tagName: 'button',
className: 'jetpack-form-submit-button',
type: 'submit',
element: 'button',
},
],
],
],
],
attributes: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ export default function useFormWrapper( { attributes, clientId, name } ) {
clientId,
createBlock( FORM_BLOCK_NAME, {}, [
createBlock( name, attributes, getBlocks( clientId ) ),
createBlock( 'jetpack/button', {
text: __( 'Submit', 'jetpack-forms' ),
element: 'button',
lock: { remove: true },
} ),
createBlock( 'core/buttons', { lock: { remove: true } }, [
createBlock( 'core/button', {
text: __( 'Submit', 'jetpack-forms' ),
type: 'submit',
tagName: 'button',
className: 'wp-block-jetpack-button jetpack-form-submit-button',
} ),
] ),
] )
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ class='{$container_classes_string}'
* @param int $id Contact Form ID.
*/
$url = apply_filters( 'grunion_contact_form_form_action', $url, $GLOBALS['post'], $id, $page );
$has_submit_button_block = str_contains( $content, 'wp-block-jetpack-button' );
$has_submit_button_block = str_contains( $content, 'wp-block-jetpack-button' ) || str_contains( $content, 'wp-block-buttons' );
$form_classes = 'contact-form commentsblock';
if ( $submission_success ) {
$form_classes .= ' submission-success';
Expand Down Expand Up @@ -1084,8 +1084,11 @@ class='" . esc_attr( $form_classes ) . "' $form_aria_label
$r = preg_replace( '/<div class="wp-block-jetpack-form-step-navigation__wrapper/', self::render_error_wrapper() . ' <div class="wp-block-jetpack-form-step-navigation__wrapper', $r, 1 );
} elseif ( $has_submit_button_block && ! $is_single_input_form ) {
// Place the error wrapper before the FIRST button block only to avoid duplicates (e.g., navigation buttons in multistep forms).
// Replace only the first occurrence.
// Replace only the first occurrence for both Jetpack and core Buttons blocks.
$r = preg_replace( '/<div class="wp-block-jetpack-button/', self::render_error_wrapper() . ' <div class="wp-block-jetpack-button', $r, 1 );
if ( str_contains( $r, 'wp-block-buttons' ) ) {
$r = preg_replace( '/<div class="wp-block-buttons/', self::render_error_wrapper() . ' <div class="wp-block-buttons', $r, 1 );
}
}

// In new versions of the contact form block the button is an inner block
Expand Down
Loading
Loading