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

Add temporary saving for form submissions #1769

Closed
wants to merge 8 commits into from

Conversation

Soare-Robert-Daniel
Copy link
Contributor

@Soare-Robert-Daniel Soare-Robert-Daniel commented Jul 14, 2023

Closes #1766 .

This will serve as infrastructure for #1778

Summary

Now we can temporarily save a Form submission. When temporary save means:

  • validation
  • file upload
  • database save

ℹ️ A form request that needs to be saved temporarily is marked by a special HTTP header called O-Form-Save-Mode which values must be temporary.

ℹ️ Temporary saving is done via a special flag. It is opt-in, which means when someone implements a filter for Form need to consider this case via is_temporary_data function of $form_data.

The state of this temporary save is shown as draft in Form Submissions.

Another API route has been implemented, whose job is to confirm the submission. When confirmed, the form data will be pulled from the database and sent to the processing.

ℹ️ When sent to re-reprocessing, a flag to mark this behavior is set via is_duplicated function of $form_data. This is also an opt-in.

For Stripe addon, we want the following workflow:

  1. User completes the form and submits it.
  2. The data is saved, and a database entry is created.
  3. A filter gets the database entry id and use it to create a Stripe session by appending it to the success URL.
  4. The stripe session URL is returned.
  5. The form gets the response and redirects the client to the Stripe page for payment.
  6. After checkout, Stripe redirects the user to the special success URL.
  7. After the page is loaded, the script autodetects the params in URL and call the API endpoint that confirm the submission.

Screenshots

Loading state when confirming the submission.

image


Test instructions

⚠️ You must have WP_DEBUG set to true and Otter Pro activated.

This test will be done via Console since it is a work of an infrastructure for a future feature.

  1. Create a form and preview it.
  2. Open Browser Console and write activateTempSave(), then press Enter. With this, the form will save the data as temporary.
  3. When you submit the form, a new page will open. It will be the same page but with a different URL. Add the end of the URL you will something like ?record_id=123. Also, you will see a confirmation message like Success.

With temporary saving, the final confirmation will be done on the page with the enhanced URL, which means you reached the last step.


Checklist before the final review

  • Included E2E or unit tests for the changes in this PR.
  • Visual elements are not affected by independent changes.
  • It is at least compatible with the minimum WordPress version.
  • It loads additional script in frontend only if it is required.
  • Does not impact the Core Web Vitals.
  • In case of deprecation, old blocks are safely migrated.
  • It is usable in Widgets and FSE.
  • Copy/Paste is working if the attributes are modified.
  • PR is following the best practices

@Soare-Robert-Daniel Soare-Robert-Daniel added the pr-checklist-skip Allow this Pull Request to skip checklist. label Jul 14, 2023
@Soare-Robert-Daniel Soare-Robert-Daniel self-assigned this Jul 14, 2023
@Soare-Robert-Daniel Soare-Robert-Daniel linked an issue Jul 14, 2023 that may be closed by this pull request
@Soare-Robert-Daniel Soare-Robert-Daniel marked this pull request as ready for review July 17, 2023 17:26
@pirate-bot
Copy link
Contributor

pirate-bot commented Jul 17, 2023

Bundle Size Diff

Package Old Size New Size Diff
Animations 272.72 KB 272.72 KB 0 B (0.00%)
Blocks 1.45 MB 1.45 MB 956 B (0.06%)
CSS 87.22 KB 87.22 KB 0 B (0.00%)
Dashboard 124.43 KB 124.43 KB 0 B (0.00%)
Export Import 85.11 KB 85.11 KB 0 B (0.00%)
Pro 328.78 KB 328.78 KB 0 B (0.00%)

@pirate-bot
Copy link
Contributor

pirate-bot commented Jul 17, 2023

Plugin build for 4227cac is ready 🛎️!

@pirate-bot
Copy link
Contributor

pirate-bot commented Jul 17, 2023

E2E Summary

Typing

Test Average Time (ms) Standard Deviation (ms) Median Time (ms) Quantile for soft limit (%) Quantile for hard limit (%)
Typing 57.1 23.87 50.3 89.47 (60ms) 94.74 (80ms)
Values above 60ms "4 - 156.98, 12 - 62.45"

@pirate-bot pirate-bot added the pr-checklist-complete The Pull Request checklist is complete. (automatic label) label Jul 18, 2023
Comment on lines +977 to +980
/**
* In this function we add a script that will activate the temporary save mode.
* This will be for the Pro users. This will be removed in the future and moved to the Pro plugin.
*/
Copy link
Member

Choose a reason for hiding this comment

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

Instead of doing this, can't we just do it based on if Stripe block exists in the form content area?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can. This is a placeholder for testing.

Comment on lines 181 to 200
register_rest_route(
$namespace,
'/form/confirm',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'confirm_form' ),
'permission_callback' => function ( $request ) {
return __return_true();
},
'args' => array(
'record_id' => array(
'validate_callback' => function( $param, $request, $key ) {
return is_numeric( $param );
},
),
),
),
)
);
Copy link
Member

Choose a reason for hiding this comment

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

If this is to confirm the submission, why is it a GET request with an open permission callback?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For fast testing. I was thinking of adding a license check; what do you think?

Copy link
Member

Choose a reason for hiding this comment

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

@Soare-Robert-Daniel I'm not sure it's a good idea to send the license from frontend for validation, we expose the license that way to visitors. Maybe we can create a nonce for that particular form submission and send it with Stripe request, along side checkout ID, and validate it with that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

By license verification, I meant at the server level. To be a PRO-only feature. I will add a dynamic nonce generation.

'permission_callback' => function ( $request ) {
$session = $request->get_param( 'session' );

if ( apply_filters( 'otter_form_session_confirmation', $session ) ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

With this step, we can verify the session id. The session can be a Stripe ID or something custom.

*/
public function verify_confirmation_session( $session ) {
// TODO: Add verification for Stripe session when adding the stripe field.
return 'test_123' === $session; // Test ONLY.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here will add the Stripe verification process when we check if the given session is still valid.

$form_data->metadata['frontend_external_confirmation_url'] = add_query_arg(
array(
'record_id' => $form_data->metadata['record_id'], // TODO: Test ONLY. This will will be extracted from Stripe payment intent metadata.
'session' => 'test_123',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

At this step, we will create the session and save the record_id in its metadata.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-checklist-complete The Pull Request checklist is complete. (automatic label) pr-checklist-skip Allow this Pull Request to skip checklist.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow temporary saving of email from Form block
3 participants