-
Notifications
You must be signed in to change notification settings - Fork 834
Forms: Add REST API endpoint for exporting responses #45275
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
base: trunk
Are you sure you want to change the base?
Conversation
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
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 🤖 Follow this PR Review Process:
If you have questions about anything, reach out in #jetpack-developers for guidance! Jetpack plugin: The Jetpack plugin has different release cadences depending on the platform:
If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack. |
49302ee
to
0ff05d9
Compare
Code Coverage SummaryCoverage changed in 4 files.
Full summary · PHP report · JS report If appropriate, add one of these labels to override the failing coverage check:
Covered by non-unit tests
|
projects/packages/forms/src/contact-form/class-contact-form-endpoint.php
Outdated
Show resolved
Hide resolved
Isn't it a convention that the REST endpoints always return JSON? What was the problem with AJAX method that we are trying to solve here? We could also make use of the |
Core REST API Handbook mentions
It also says
|
@manzoorwanijk Thanks for the feedback. You are right about REST conventions. In this version, we return JSON for errors and CSV on success. While |
projects/packages/forms/src/contact-form/class-contact-form-endpoint.php
Outdated
Show resolved
Hide resolved
233fb69
to
06c5d76
Compare
I tested this and it works as expected. 🥳 I tested exporting. Exporting Google Sheets ( for regressions ) It would be good to add an endpoint test for this PR. So that we have something that checks for regressions. |
If you wish to laverage the REST authentication for api-fetch, then you can return a URL to admin-post action mentioned above. So, it can pass only the Then update |
projects/packages/forms/src/contact-form/class-contact-form-endpoint.php
Show resolved
Hide resolved
Thanks for the feedback @manzoorwanijk. Updated the implementation to follow your suggestion about using admin-post for file downloads. The REST endpoint now returns JSON with a download_url that points to an admin-post handler. |
projects/packages/forms/src/contact-form/class-contact-form-endpoint.php
Outdated
Show resolved
Hide resolved
I retested this changes and it works well. It would be good to show some sort of in progress button while the URL is being returned. This will help the user not click the button multiple times. Lets add a test for the new endpoint and then I think this is ready to ship! Nice work. |
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.
Thank you for implementing the suggestions. This looks much better now. I have some further suggestions for improvement, some of which you may ignore if you prefer.
$data = $this->get_feedback_entries_from_post(); | ||
public function admin_post_feedback_export() { | ||
$feedback_ids_str = sanitize_text_field( wp_unslash( $_GET['feedback_ids'] ?? '' ) ); | ||
$post_id = sanitize_text_field( wp_unslash( $_GET['post_id'] ?? '' ) ); |
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.
We don't pass the post_id
when generating the URL from the REST endpoint. Do we?
projects/packages/forms/src/contact-form/class-contact-form-plugin.php
Outdated
Show resolved
Hide resolved
window.location.href = response.download_url; | ||
return response; | ||
} | ||
throw new Error( 'Invalid response: missing download URL' ); |
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.
Do we need to translate this?
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.
I would say not yet since it would only show up in the dev console for now.
return new WP_Error( 'no_responses', __( 'No responses found', 'jetpack-forms' ), array( 'status' => 404 ) ); | ||
} | ||
|
||
$feedback_ids = wp_list_pluck( $feedback_posts, 'ID' ); |
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.
You can add 'fields' => 'ids'
to $query_args
above to get rid of this step and make it a bit faster.
|
…ugin.php Co-authored-by: Manzoor Wani <[email protected]>
Proposed changes
This PR replaces the legacy AJAX-based CSV export with a modern REST API endpoint for Jetpack Forms responses.
Changes:
/wp/v2/feedback/export
REST endpoint for CSV exportswp_ajax_feedback_export
and thedownload_feedback_as_csv()
method@wordpress/api-fetch
Other information
Testing instructions
Product impact
Does this pull request change what data or activity we track or use?
No
Checklist