-
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
Open
lezama
wants to merge
17
commits into
trunk
Choose a base branch
from
forms/add-rest-export-endpoint
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
06c5d76
Use rest endpoint instead of ajax
lezama f28cd8a
not deprecating
lezama 258fed2
Refactor CSV export to use admin-post
lezama 8a3ac4c
simpler
lezama b9d4bf1
add sanitize_calllbacks
lezama 9299540
fix types
lezama 2d8dae5
simpler
lezama 1149953
simpler
lezama e610b0c
Merge branch 'trunk' into forms/add-rest-export-endpoint
lezama ff69dc1
remove unused params
lezama 4a97cab
Update projects/packages/forms/src/contact-form/class-contact-form-pl…
enejb f3e2b8a
Use feedback ids instead
enejb d2eb4c2
Show error notice when there are no responses.
enejb 123c3e9
Add tests
enejb 2a66931
Fix linter errors
enejb 2c414d6
More tests
enejb 8c58151
Pass the $post_id
enejb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: added | ||
|
||
Add REST API endpoint for exporting form responses, replacing legacy AJAX implementation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -211,11 +211,12 @@ protected function __construct() { | |
add_filter( 'wp_privacy_personal_data_exporters', array( $this, 'register_personal_data_exporter' ) ); | ||
add_filter( 'wp_privacy_personal_data_erasers', array( $this, 'register_personal_data_eraser' ) ); | ||
|
||
// Export to CSV feature | ||
if ( is_admin() ) { | ||
add_action( 'wp_ajax_feedback_export', array( $this, 'download_feedback_as_csv' ) ); | ||
add_action( 'wp_ajax_create_new_form', array( $this, 'create_new_form' ) ); | ||
} | ||
|
||
// Admin-post action for CSV export | ||
add_action( 'admin_post_feedback_export', array( $this, 'admin_post_feedback_export' ) ); | ||
add_action( 'admin_menu', array( $this, 'admin_menu' ) ); | ||
add_action( 'current_screen', array( $this, 'unread_count' ) ); | ||
add_action( 'current_screen', array( $this, 'redirect_edit_feedback_to_jetpack_forms' ) ); | ||
|
@@ -2756,22 +2757,53 @@ function ( $selected ) { | |
} | ||
|
||
/** | ||
* Download exported data as CSV | ||
* Admin-post handler for CSV export | ||
*/ | ||
public function download_feedback_as_csv() { | ||
lezama marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- verification is done on get_feedback_entries_from_post function | ||
$post_data = wp_unslash( $_POST ); | ||
$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 commentThe reason will be displayed to describe this comment to others. Learn more. We don't pass the |
||
$nonce = sanitize_text_field( wp_unslash( $_GET['nonce'] ?? '' ) ); | ||
|
||
if ( empty( $feedback_ids_str ) || empty( $nonce ) ) { | ||
wp_die( esc_html__( 'Invalid request parameters.', 'jetpack-forms' ), 400 ); | ||
} | ||
|
||
$feedback_ids = explode( ',', $feedback_ids_str ); | ||
$feedback_ids = array_values( array_filter( array_map( 'intval', $feedback_ids ) ) ); | ||
|
||
if ( ! wp_verify_nonce( $nonce, 'feedback_export_' . $feedback_ids_str ) ) { | ||
wp_die( esc_html__( 'Security check failed.', 'jetpack-forms' ), 403 ); | ||
} | ||
|
||
if ( ! current_user_can( 'export' ) ) { | ||
wp_die( esc_html__( 'You do not have permission to export form responses.', 'jetpack-forms' ), 403 ); | ||
} | ||
|
||
$export_data = $this->get_export_feedback_data( $feedback_ids ); | ||
|
||
if ( empty( $export_data ) ) { | ||
wp_die( esc_html__( 'No responses found to export.', 'jetpack-forms' ), 404 ); | ||
} | ||
|
||
$this->download_feedback_as_csv( $export_data, $post_id ); | ||
} | ||
|
||
/** | ||
* Download exported data as CSV | ||
* | ||
* @param array $data Export data to generate CSV from. | ||
* @param string $post_id Optional. Post ID for filename generation. | ||
*/ | ||
public function download_feedback_as_csv( $data = null, $post_id = '' ) { | ||
if ( empty( $data ) ) { | ||
return; | ||
} | ||
|
||
// Check if we want to download all the feedbacks or just a certain contact form | ||
if ( ! empty( $post_data['post'] ) && $post_data['post'] !== 'all' ) { | ||
if ( ! empty( $post_id ) && $post_id !== 'all' ) { | ||
$filename = sprintf( | ||
'%s - %s.csv', | ||
Util::get_export_filename( get_the_title( (int) $post_data['post'] ) ), | ||
Util::get_export_filename( get_the_title( (int) $post_id ) ), | ||
gmdate( 'Y-m-d H:i' ) | ||
); | ||
} else { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.