Skip to content
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

Merged
merged 13 commits into from
Jun 20, 2022

Conversation

danielbachhuber
Copy link
Contributor

@danielbachhuber danielbachhuber commented Jun 13, 2022

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:

  1. $_wp_current_template_content is filtered on template_include to add a block_template=canvas attribute to all contact form blocks in the block template.
  2. The Grunion_Contact_Form class interprets the block_template attribute accordingly when present.
  3. The ::process_form_submission() method properly interprets contact-form-id=canvas. It has to hackily reproduce part of wp-includes/template-loader.php in order to hydrate $_wp_current_template_content(because $_wp_current_template_content needs to be rendered in order to populate Grunion_Contact_Form::$last.

For block template parts:

  1. The $GLOBALS['grunion_block_template_part_id'] global variable is set when the wp:core/template-part block is rendered.
  2. If the $GLOBALS['grunion_block_template_part_id'] is set when the contact form block is rendered, a block_template_part=<part-id> attribute is added to the block attributes.
  3. The Grunion_Contact_Form class interprets the block_template_part attribute accordingly when present.
  4. The ::process_form_submission() method properly interprets contact-form-id=block-template-part-<part-id>.

For block widgets:

  1. widget_block_content is filtered to apply widget=<widget-id> as an attribute to each block within the block widget.
  2. The rest of the existing logic for widgets is reused.

Does this pull request change what data or activity we track or use?

No.

Testing instructions

To test the block template integration:

  1. Activate a FSE theme (Twenty Twenty-Two is a good one).
  2. Add a Contact Form Block to the Single Template.
  3. Change "On Submission" to "Show a custom text message". Write a custom text message.
  4. Navigate to an individual blog post.
  5. Submit the Contact Form Block.
  6. Observe your custom success message in the Contact Form Block.
  7. Return to Full Site Editor and change your Contact Form Block to use "Show a summary of submitted fields".
  8. Navigate to an individual blog post.
  9. Submit the Contact Form Block.
  10. Observe your submitted fields in the Contact Form block.

image

image

To test the block template part integration:

  1. Activate a FSE theme (Twenty Twenty-Two is a good one).
  2. Add a Contact Form Block to the Footer template part.
  3. Change "On Submission" to "Show a custom text message". Write a custom text message.
  4. Navigate to the footer on the frontend.
  5. Submit the Contact Form Block.
  6. Observe your custom success message in the Contact Form Block.
  7. Return to Full Site Editor and change your Contact Form Block to use "Show a summary of submitted fields".
  8. Navigate to the footer on the frontend.
  9. Submit the Contact Form Block.
  10. Observe your submitted fields in the Contact Form block.

image

image

To test the widget block integration:

  1. Activate a legacy theme (Twenty Twenty is a good one).
  2. Add a Contact Form Block to a block editor-enabled sidebar.
  3. Change "On Submission" to "Show a custom text message". Write a custom text message.
  4. Navigate to the sidebar on the frontend.
  5. Submit the Contact Form Block.
  6. Observe your custom success message in the Contact Form Block.
  7. Return to widgets in the backend and change your Contact Form Block to use "Show a summary of submitted fields".
  8. Navigate to the sidebar on the frontend.
  9. Submit the Contact Form Block.
  10. Observe your submitted fields in the Contact Form block.

image

image

Refactors form generation and processing to use an approach similar to
the contact form widget.
@danielbachhuber danielbachhuber added [Type] Bug When a feature is broken and / or not performing as intended [Feature] Contact Form [Block] Contact Form Form block (also see Contact Form label) labels Jun 13, 2022
@danielbachhuber danielbachhuber requested review from a team June 13, 2022 19:32
@matticbot
Copy link
Contributor

Caution: This PR has changes that must be merged to WordPress.com
Hello danielbachhuber! These changes need to be synced to WordPress.com - If you 're an a11n, please commandeer and confirm D82492-code works as expected before merging this PR. Once this PR is merged, please commit the changes to WP.com. Thank you!
This revision will be updated with each commit to this PR

@github-actions github-actions bot added the [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ label Jun 13, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Jun 13, 2022

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ All commits were linted before commit.
  • ✅ Add a "[Status]" label (In Progress, Needs Team Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

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.
Then, add the "[Status] Needs Team review" label and ask someone from your team review the code.
Once you’ve done so, switch to the "[Status] Needs Review" label; someone from Jetpack Crew will then review this PR and merge it to be included in the next Jetpack release.


Jetpack plugin:

  • Next scheduled release: July 5, 2022.
  • Scheduled code freeze: June 28, 2022.

@github-actions github-actions bot added the [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! label Jun 13, 2022
@danielbachhuber danielbachhuber added [Status] Needs Team Review and removed [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! labels Jun 13, 2022
@github-actions github-actions bot added the [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! label Jun 13, 2022
@danielbachhuber danielbachhuber self-assigned this Jun 13, 2022
@danielbachhuber danielbachhuber removed the request for review from a team June 13, 2022 22:58
@bindlegirl bindlegirl removed the [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! label Jun 14, 2022
@wojtekn
Copy link
Contributor

wojtekn commented Jun 14, 2022

@danielbachhuber I tested it on my sandbox after synchronizing the grunion-contact-form.php file from the Jetpack repository to my sandbox. I couldn’t make it work and was still reproducing the bug. In my case, contact-form-id is set to an integer each time. I found out that render_block_core_template_part_post is never triggered. I was adding a form as a block to one of the widget areas. I was using the Blask theme.

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 grunion_contact_form_set_block_template_part_id_global is still not executed.

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.

@sejas
Copy link
Member

sejas commented Jun 14, 2022

@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.

@sejas
Copy link
Member

sejas commented Jun 14, 2022

I've tested it on an external jetpack site, and it works for template parts such as the header and footer. But it doesn't work in templates like single or home.

Maybe the issues are very similar but not the same after all.

Screenshot of templates vs template parts.
Screenshot 2022-06-14 at 15 43 44

@danielbachhuber danielbachhuber changed the title Contact Form Block: Displays expected success message when used in FSE header or footer Contact Form Block: Displays expected success message when used in template or template part Jun 14, 2022
@danielbachhuber danielbachhuber changed the title Contact Form Block: Displays expected success message when used in template or template part Contact Form Block: Displays expected success message when used in FSE template or template part Jun 14, 2022
@danielbachhuber danielbachhuber changed the title Contact Form Block: Displays expected success message when used in FSE template or template part Contact Form Block: Displays expected success message when used in FSE template, FSE template part, or widget block Jun 14, 2022
@danielbachhuber danielbachhuber removed the request for review from a team June 15, 2022 00:01
@danielbachhuber
Copy link
Contributor Author

@wojtekn @sejas Thanks for your testing! I addressed FSE templates with e694eb3 and block widgets with 4566a76

Still need to test the wpcom diff. I'll re-flag for review after I've done so.

@danielbachhuber
Copy link
Contributor Author

@wojtekn @sejas Fortunately, a simple solution for dotcom (and Gutenberg plugin) compatibility: d247dda

😅

@danielbachhuber danielbachhuber requested a review from a team June 15, 2022 12:52
sejas
sejas previously approved these changes Jun 15, 2022
Copy link
Member

@sejas sejas left a 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.

Comment on lines 2429 to 2434
$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 );
Copy link
Member

@sejas sejas Jun 15, 2022

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

Responses page:
/wp-admin/edit.php?post_type=feedback
Screenshot 2022-06-15 at 15 49 24

Copy link
Contributor Author

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?

Copy link
Contributor

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 $postcontext 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 ) );
		}

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@wojtekn Oh, fair enough. I've restored the prior (somewhat inaccurate) behavior for all contexts with 888a8ad. Here's what it looks like:

image

Copy link
Contributor

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.

@danielbachhuber danielbachhuber added [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. and removed [Status] Needs Team Review labels Jun 15, 2022
@danielbachhuber danielbachhuber requested a review from a team June 15, 2022 18:46
Copy link
Contributor

@sdixon194 sdixon194 left a 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

@sdixon194 sdixon194 linked an issue Jun 17, 2022 that may be closed by this pull request
@samiff samiff added [Status] Ready to Merge Go ahead, you can push that green button! and removed [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. labels Jun 17, 2022
@wojtekn wojtekn self-requested a review June 20, 2022 06:48
@sejas sejas merged commit 421a874 into trunk Jun 20, 2022
@sejas sejas deleted the fix/grunion-success-message-block-template-part branch June 20, 2022 14:04
@github-actions github-actions bot removed the [Status] Ready to Merge Go ahead, you can push that green button! label Jun 20, 2022
@github-actions
Copy link
Contributor

Great news! One last step: head over to your WordPress.com diff, D82492-code, and deploy it.
Once you've done so, come back to this PR and add a comment with your changeset ID.

Thank you!

@sejas
Copy link
Member

sejas commented Jun 20, 2022

r247680-wpcom

@jeherve
Copy link
Member

jeherve commented Jun 20, 2022

Nice work! I was hoping this would also fix #22749 so we wouldn't need something like #24701, but I still see the problem on trunk right now. Do you think you could take a look at it?

Thanks!

@danielbachhuber
Copy link
Contributor Author

@jeherve I've assigned myself #22749 to take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Contact Form Form block (also see Contact Form label) [Feature] Contact Form [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ Touches WP.com Files [Type] Bug When a feature is broken and / or not performing as intended Unit Tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Contact Form Block: No Confirmation on Sidebar/FSE
8 participants