-
Notifications
You must be signed in to change notification settings - Fork 800
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 form responses v2 endpoint #29043
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
b6e7c31
[not verified] add form-responses endpoint to be handled by forms pac…
CGastrell 81fee64
[not verified] changelog
CGastrell 3e9e495
fix typo on class name
CGastrell e109544
[not verified] add backend code for v2/v4 endpoint
CGastrell 874df2c
changelog
CGastrell 13681a6
fixup project versions
CGastrell 6b5c7d8
use wpcom/v2 instead of jetpack/v4
CGastrell e0426c9
update apiRoot backend config JS
CGastrell d31808d
remove unnecessary plugin endpoint file, add api-fetch dependency for…
CGastrell 618e08a
rename controller file to reflect forms base path
CGastrell 8200b86
instance the right class name after change
CGastrell b2c1e07
fix route register handler name
CGastrell 601f371
edit comments/docblocks
CGastrell 2738848
use simpler validation through args props
CGastrell d12459e
use Jetpack\Connection\Manager to get site ID, remove unneeded code a…
CGastrell a5627a7
get the date from post, not from parsed content
CGastrell 795347d
add capability check to the endpoint permission callback
CGastrell ba13141
Merge branch 'trunk' into add/form-responses-v2-endpoint
CGastrell 671aded
Merge branch 'trunk' into add/form-responses-v2-endpoint
CGastrell 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
4 changes: 4 additions & 0 deletions
4
projects/packages/forms/changelog/add-form-responses-v2-endpoint
This file contains 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 v2/v4 endpoint for form responses inbox |
This file contains 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
167 changes: 167 additions & 0 deletions
167
projects/packages/forms/src/class-wpcom-rest-api-v2-endpoint-forms.php
This file contains 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,167 @@ | ||
<?php | ||
/** | ||
* The Forms Rest Controller class. | ||
* Registers the REST routes for Jetpack Forms (taken from stats-admin). | ||
* | ||
* @package automattic/jetpack-forms | ||
*/ | ||
|
||
namespace Automattic\Jetpack\Forms; | ||
|
||
use Automattic\Jetpack\Connection\Manager; | ||
use WP_Error; | ||
use WP_REST_Controller; | ||
use WP_REST_Server; | ||
|
||
/** | ||
* Handles the REST routes for Form Responses, aka Feedback. | ||
*/ | ||
class WPCOM_REST_API_V2_Endpoint_Forms extends WP_REST_Controller { | ||
/** | ||
* Constructor. | ||
*/ | ||
public function __construct() { | ||
$this->namespace = 'wpcom/v2'; | ||
$this->rest_base = 'forms'; | ||
|
||
add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) ); | ||
} | ||
|
||
/** | ||
* Registers the REST routes. | ||
* | ||
* @access public | ||
*/ | ||
public function register_rest_routes() { | ||
// Stats for single resource type. | ||
|
||
register_rest_route( | ||
$this->namespace, | ||
$this->rest_base . '/responses', | ||
array( | ||
'methods' => WP_REST_Server::READABLE, | ||
'callback' => array( $this, 'get_responses' ), | ||
'permissions_check' => array( $this, 'get_responses_permission_check' ), | ||
'args' => array( | ||
'limit' => array( | ||
'default' => 20, | ||
'type' => 'integer', | ||
'required' => false, | ||
'minimum' => 1, | ||
), | ||
'offset' => array( | ||
'default' => 0, | ||
'type' => 'integer', | ||
'required' => false, | ||
'minimum' => 0, | ||
), | ||
'form_id' => array( | ||
'type' => 'integer', | ||
'required' => false, | ||
'minimum' => 1, | ||
), | ||
'search' => array( | ||
'type' => 'string', | ||
'required' => false, | ||
), | ||
), | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Returns Jetpack Forms responses. | ||
* | ||
* @param WP_REST_Request $request The request sent to the WP REST API. | ||
* | ||
* @return WP_REST_Response A response object containing Jetpack Forms responses. | ||
*/ | ||
public function get_responses( $request ) { | ||
$args = array( | ||
'post_type' => 'feedback', | ||
'post_status' => array( 'publish', 'draft' ), | ||
); | ||
|
||
if ( isset( $request['form_id'] ) ) { | ||
$args['post_parent'] = $request['form_id']; | ||
} | ||
|
||
if ( isset( $request['limit'] ) ) { | ||
$args['posts_per_page'] = $request['limit']; | ||
} | ||
|
||
if ( isset( $request['offset'] ) ) { | ||
$args['offset'] = $request['offset']; | ||
} | ||
|
||
if ( isset( $request['search'] ) ) { | ||
$args['s'] = $request['search']; | ||
} | ||
|
||
$query = new \WP_Query( $args ); | ||
|
||
$responses = array_map( | ||
function ( $response ) { | ||
$data = \Automattic\Jetpack\Forms\ContactForm\Contact_Form_Plugin::parse_fields_from_content( $response->ID ); | ||
|
||
return array( | ||
'id' => $response->ID, | ||
'uid' => $data['all_fields']['feedback_id'], | ||
'date' => get_the_date( 'c', $response ), | ||
'author_name' => $data['_feedback_author'], | ||
'author_email' => $data['_feedback_author_email'], | ||
'author_url' => $data['_feedback_author_url'], | ||
'author_avatar' => empty( $data['_feedback_author_email'] ) ? '' : get_avatar_url( $data['_feedback_author_email'] ), | ||
'email_marketing_consent' => $data['all_fields']['email_marketing_consent'], | ||
'ip' => $data['_feedback_ip'], | ||
'entry_title' => $data['all_fields']['entry_title'], | ||
'entry_permalink' => $data['all_fields']['entry_permalink'], | ||
'subject' => $data['_feedback_subject'], | ||
'fields' => array_diff_key( | ||
$data['all_fields'], | ||
array( | ||
'email_marketing_consent' => '', | ||
'entry_title' => '', | ||
'entry_permalink' => '', | ||
'feedback_id' => '', | ||
) | ||
), | ||
); | ||
}, | ||
$query->posts | ||
); | ||
|
||
return rest_ensure_response( | ||
array( | ||
'responses' => $responses, | ||
'total' => $query->found_posts, | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Verifies that the current user has the requird capability for viewing form responses. | ||
* | ||
* @return true|WP_Error Returns true if the user has the required capability, else a WP_Error object. | ||
*/ | ||
public function get_responses_permission_check() { | ||
$site_id = Manager::get_site_id(); | ||
if ( is_wp_error( $site_id ) ) { | ||
return $site_id; | ||
} | ||
|
||
if ( ! current_user_can( 'manage_options' ) || ! is_user_member_of_blog( get_current_user_id(), $site_id ) ) { | ||
return new WP_Error( | ||
'invalid_user_permission_jetpack_form_responses', | ||
'unauthorized', | ||
array( 'status' => rest_authorization_required_code() ) | ||
); | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
|
||
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { | ||
wpcom_rest_api_v2_load_plugin( 'Automattic\Jetpack\Forms\WPCOM_REST_API_V2_Endpoint_Forms' ); | ||
} |
This file contains 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 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
4 changes: 4 additions & 0 deletions
4
projects/plugins/jetpack/changelog/add-form-responses-v2-endpoint
This file contains 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: other | ||
|
||
Add wpcom/v2/form-responses endpoint, mapped from .com |
5 changes: 5 additions & 0 deletions
5
projects/plugins/jetpack/changelog/add-form-responses-v2-endpoint#2
This file contains 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,5 @@ | ||
Significance: patch | ||
Type: other | ||
Comment: Updated composer.lock. | ||
|
||
|
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.
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.
With
current_user_can( 'manage_options' )
in place, thenis_user_member_of_blog( get_current_user_id(), $site_id )
becomes redundant, right?In that case we don't need the
$site_id
as well I guess.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.
User might be member of blog but not have management capabilities? Is that not a possible scenario?
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.
No worries, doesn't hurt anyway :)
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 followed up on this conversation in #35571 after discussion in p1707435111778979-slack-C05PV073SG3