-
Notifications
You must be signed in to change notification settings - Fork 808
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
Contact Form Block: Displays expected success message when used in FSE template, FSE template part, or widget block #24727
Contact Form Block: Displays expected success message when used in FSE template, FSE template part, or widget block #24727
Conversation
Refactors form generation and processing to use an approach similar to the contact form widget.
Caution: This PR has changes that must be merged to WordPress.com |
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 The e2e test report can be found here. Please note that it can take a few minutes after the e2e tests checks are complete for the report to be available. Once your PR is ready for review, check one last time that all required checks (other than "Required review") appearing at the bottom of this PR are passing or skipped. Jetpack plugin:
|
@danielbachhuber I tested it on my sandbox after synchronizing the Then I reread the testing instructions and realized I should use FSE to place the form in the footer. I changed the theme to Twenty Twenty Two and used FSE to add a form as a block into the home page footer, and I still couldn’t make it work. It seems that Any idea what I’m doing wrong here? I understand how you approach that. However, I still miss some context on how to run this particular use case. |
@wojtekn @danielbachhuber here is the diff related to this PR: D82492-code , I've also tested it using the sandbox, and I can confirm that the issue is still there for simple sites. I think #62431 is a dup of #60774, we should close one of them. |
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.
@danielbachhuber The PR looks great.
I've tested it and it's working in every case: on simple sites, atomic sites, jetpack sites with the form inside the templates and also in template parts.
Great work!!
I've added a suggestion about showing a different title on the Form Responses page.
$default_subject = sprintf( _x( '%1$s Block Template', '%1$s = blog name', 'jetpack' ), $default_subject ); | ||
} elseif ( ! empty( $attributes['block_template_part'] ) && $attributes['block_template_part'] ) { | ||
$default_to .= get_option( 'admin_email' ); | ||
$attributes['id'] = 'block-template-part-' . $attributes['block_template_part']; | ||
// translators: the blog name. | ||
$default_subject = sprintf( _x( '%1$s Block Template Part', '%1$s = blog name', 'jetpack' ), $default_subject ); |
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.
It would be great if we can modify the saved title to keep the context.
The user cannot track from which page was sent the message.
Before the fix, the title was: Site Title] Hello World!
.
After applying the fix it says: [Site Title] Block Template
or [Site Title] Block Template Part
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.
It would be great if we can modify the saved title to keep the context.
That would be a great improvement 😄
Unfortunately, I don't know of a good way to identify a name for the context (aside from inventing our own system). Do you know of any ways? Alternatively, maybe there's a more generic title we could use?
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.
Before our changes, when there was no separate handling for block_template
and block_template_part
, we were using $post
context in such a case - it was caught by the last elseif
condition.
What if we remove $default_subject
from all those conditions and then use this below?
if ( $post ) {
// translators: the blog name and post title.
$default_subject = sprintf( _x( '%1$s %2$s', '%1$s = blog name, %2$s = post title', 'jetpack' ), $default_subject, Grunion_Contact_Form_Plugin::strip_tags( $post->post_title ) );
}
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.
What if we remove
$default_subject
from all those conditions and then use this below?
While we'd have a blog post title, it may or may not be accurate:
- On the homepage,
$post
would be the last post in the main query, unless... - Anywhere there's a
WP_Query
immediately before the contact form,$post
would be the last post in that query.
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.
@danielbachhuber isn't it how it worked before the change? I've roughly tested that case and it was not using the last post, but the homepage's main $post.
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.
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.
@danielbachhuber it looks solid and works well for me in all cases now.
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.
Did some extensive testing on this one and everything worked well! Noting that this fixes #24519
Great news! One last step: head over to your WordPress.com diff, D82492-code, and deploy it. Thank you! |
r247680-wpcom |
Refactors Contact Form Block generation and processing so that the expected success message displays when the block is used in a FSE template, FSE template part, or block widget.
See Automattic/wp-calypso#60774
See Automattic/wp-calypso#62431
Proposed changes
The refactor follows a pattern similar to how the Contact Form Widget special-case is handled.
For block templates:
$_wp_current_template_content
is filtered ontemplate_include
to add ablock_template=canvas
attribute to all contact form blocks in the block template.Grunion_Contact_Form
class interprets theblock_template
attribute accordingly when present.::process_form_submission()
method properly interpretscontact-form-id=canvas
. It has to hackily reproduce part ofwp-includes/template-loader.php
in order to hydrate$_wp_current_template_content
(because$_wp_current_template_content
needs to be rendered in order to populateGrunion_Contact_Form::$last
.For block template parts:
$GLOBALS['grunion_block_template_part_id']
global variable is set when thewp:core/template-part
block is rendered.$GLOBALS['grunion_block_template_part_id']
is set when the contact form block is rendered, ablock_template_part=<part-id>
attribute is added to the block attributes.Grunion_Contact_Form
class interprets theblock_template_part
attribute accordingly when present.::process_form_submission()
method properly interpretscontact-form-id=block-template-part-<part-id>
.For block widgets:
widget_block_content
is filtered to applywidget=<widget-id>
as an attribute to each block within the block widget.Does this pull request change what data or activity we track or use?
No.
Testing instructions
To test the block template integration:
To test the block template part integration:
To test the widget block integration: