diff --git a/projects/packages/forms/changelog/add-form-responses-v2-endpoint b/projects/packages/forms/changelog/add-form-responses-v2-endpoint new file mode 100644 index 0000000000000..14559e12b2cce --- /dev/null +++ b/projects/packages/forms/changelog/add-form-responses-v2-endpoint @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Add v2/v4 endpoint for form responses inbox diff --git a/projects/packages/forms/src/class-jetpack-forms.php b/projects/packages/forms/src/class-jetpack-forms.php index 2b337ad35f9be..c6455912e5c8c 100644 --- a/projects/packages/forms/src/class-jetpack-forms.php +++ b/projects/packages/forms/src/class-jetpack-forms.php @@ -43,6 +43,8 @@ public static function load_contact_form() { } add_action( 'init', '\Automattic\Jetpack\Forms\ContactForm\Util::register_pattern' ); + + add_action( 'rest_api_init', array( new WPCOM_REST_API_V2_Endpoint_Forms(), 'register_rest_routes' ) ); } /** diff --git a/projects/packages/forms/src/class-wpcom-rest-api-v2-endpoint-forms.php b/projects/packages/forms/src/class-wpcom-rest-api-v2-endpoint-forms.php new file mode 100644 index 0000000000000..df8693e9369b2 --- /dev/null +++ b/projects/packages/forms/src/class-wpcom-rest-api-v2-endpoint-forms.php @@ -0,0 +1,167 @@ +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' ); +} diff --git a/projects/packages/forms/src/contact-form/class-contact-form-plugin.php b/projects/packages/forms/src/contact-form/class-contact-form-plugin.php index a7367ff5c0fc2..8df3f6c03b010 100644 --- a/projects/packages/forms/src/contact-form/class-contact-form-plugin.php +++ b/projects/packages/forms/src/contact-form/class-contact-form-plugin.php @@ -2009,6 +2009,7 @@ public static function parse_fields_from_content( $post_id ) { } $fields['_feedback_all_fields'] = $all_values; + $fields['all_fields'] = $all_values; $post_fields[ $post_id ] = $fields; diff --git a/projects/packages/forms/src/dashboard/class-dashboard.php b/projects/packages/forms/src/dashboard/class-dashboard.php index f4228283da469..38c8a68462f64 100644 --- a/projects/packages/forms/src/dashboard/class-dashboard.php +++ b/projects/packages/forms/src/dashboard/class-dashboard.php @@ -47,11 +47,22 @@ public function load_admin_scripts( $hook ) { '../../dist/dashboard/jetpack-forms-dashboard.js', __FILE__, array( - 'in_footer' => true, - 'textdomain' => 'jetpack-forms', - 'enqueue' => true, + 'in_footer' => true, + 'textdomain' => 'jetpack-forms', + 'enqueue' => true, + 'dependencies' => array( 'wp-api-fetch' ), // this here just for testing, remove when done (apiFetch will be on build) ) ); + + $api_root = defined( 'IS_WPCOM' ) && IS_WPCOM + ? sprintf( '/wpcom/v2/sites/%s/', esc_url_raw( rest_url() ) ) // should we include full URL here (public-api.wordpress.com)? + : '/wp-json/wpcom/v2/'; + + wp_add_inline_script( + 'jp-forms-dashboard', + 'window.jetpackFormsData = ' . wp_json_encode( array( 'apiRoot' => $api_root ) ) . ';', + 'before' + ); } /** diff --git a/projects/plugins/jetpack/changelog/add-form-responses-v2-endpoint b/projects/plugins/jetpack/changelog/add-form-responses-v2-endpoint new file mode 100644 index 0000000000000..f60a2e52859ad --- /dev/null +++ b/projects/plugins/jetpack/changelog/add-form-responses-v2-endpoint @@ -0,0 +1,4 @@ +Significance: minor +Type: other + +Add wpcom/v2/form-responses endpoint, mapped from .com diff --git a/projects/plugins/jetpack/changelog/add-form-responses-v2-endpoint#2 b/projects/plugins/jetpack/changelog/add-form-responses-v2-endpoint#2 new file mode 100644 index 0000000000000..a1c1831fa1ef7 --- /dev/null +++ b/projects/plugins/jetpack/changelog/add-form-responses-v2-endpoint#2 @@ -0,0 +1,5 @@ +Significance: patch +Type: other +Comment: Updated composer.lock. + +