Skip to content

Forms: add admin abilities for form CRUD and bulk responses#47102

Open
lezama wants to merge 4 commits intotrunkfrom
add/admin-form-abilities
Open

Forms: add admin abilities for form CRUD and bulk responses#47102
lezama wants to merge 4 commits intotrunkfrom
add/admin-form-abilities

Conversation

@lezama
Copy link
Contributor

@lezama lezama commented Feb 12, 2026

Summary

Adds 5 new admin-level abilities to the Jetpack Forms Abilities API:

  • list-forms — List forms with entries count, edit URLs, pagination, search, and status filter. Delegates to Jetpack_Form_Endpoint with jetpack_forms_context=dashboard.
  • get-form — Get a single form's full details including extracted field definitions from block content.
  • create-form — Create a new jetpack_form CPT post with optional block content (defaults to empty form with submit button).
  • delete-form — Soft-delete (trash) a form.
  • bulk-update-responses — Mark multiple responses as spam/not-spam via Contact_Form_Endpoint::bulk_actions().

All require edit_pages capability. Independent of the public abilities PR (#46821).

Test plan

  • composer test-php tests/php/abilities/Forms_Abilities_Test.php — 21 tests, 59 assertions pass
  • Full suite: composer test-php — 648 tests pass
  • Verify abilities register correctly on a WP 6.9+ site with Abilities API
  • Verify list-forms returns form data with entries_count
  • Verify create-form creates a jetpack_form post
  • Verify delete-form trashes the form (soft delete)

Copilot AI review requested due to automatic review settings February 12, 2026 20:49
@github-actions
Copy link
Contributor

github-actions bot commented Feb 12, 2026

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack), and enable the add/admin-form-abilities branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack add/admin-form-abilities

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@github-actions
Copy link
Contributor

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • 🔴 Add a "[Status]" label (In Progress, Needs Review, ...).
  • 🔴 Add testing instructions.
  • 🔴 Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

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 🤖


🔴 Action required: Please include detailed testing steps, explaining how to test your change, like so:

## Testing instructions:

* Go to '..'
*

🔴 Action required: We would recommend that you add a section to the PR description to specify whether this PR includes any changes to data or privacy, like so:

## Does this pull request change what data or activity we track or use?

My PR adds *x* and *y*.

Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!

@github-actions github-actions bot added the [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. label Feb 12, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds five new admin-level abilities to the Jetpack Forms Abilities API for managing forms and their responses. It builds upon the existing abilities infrastructure to provide CRUD operations for forms and bulk response management.

Changes:

  • Added 5 new admin abilities: list-forms, get-form, create-form, delete-form, and bulk-update-responses
  • All abilities require edit_pages capability for admin-level access
  • Implemented helper methods for extracting field definitions from block content
  • Added comprehensive test coverage for new abilities (21 tests total)

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 7 comments.

File Description
projects/packages/forms/src/abilities/class-forms-abilities.php Added 5 new ability registration methods and their corresponding execution callbacks, plus helper methods for field extraction
projects/packages/forms/tests/php/abilities/Forms_Abilities_Test.php Added 10 new tests for the CRUD operations and updated expected abilities list
projects/packages/forms/changelog/add-admin-form-abilities Added changelog entry for the new admin abilities

Comment on lines 733 to 736
$result = wp_trash_post( $form_post->ID );
if ( ! $result ) {
return new \WP_Error( 'delete_failed', __( 'Failed to delete form.', 'jetpack-forms' ) );
}
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

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

The delete_form function doesn't check if the form is already in the trash before attempting to trash it. If a form is already trashed, wp_trash_post will return false, and this will be reported as 'delete_failed' which could be confusing. Consider adding a check for the current post status and returning a more specific error message if the form is already trashed, or returning success if it's already in the desired state.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Resolved by the rest_do_request() refactor — delete_form now delegates to DELETE /wp/v2/jetpack-forms/{id}, which handles already-trashed posts internally.

Comment on lines 694 to 700
$status = $args['status'] ?? 'publish';
$post_id = wp_insert_post(
array(
'post_type' => Contact_Form::POST_TYPE,
'post_title' => sanitize_text_field( $args['title'] ),
'post_content' => $content,
'post_status' => $status,
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

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

The create_form function doesn't validate the 'status' parameter against the allowed values defined in the input schema (publish, draft). While the schema validation should prevent invalid values from reaching this function when called through the abilities API, the function can be called directly and could accept any status value. Consider adding explicit validation to ensure the status is one of the allowed values, or document that the function relies on schema validation.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Resolved by the rest_do_request() refactor — create_form now delegates to POST /wp/v2/jetpack-forms, which validates status against the post type's allowed statuses via schema validation.


$this->assertInstanceOf( \WP_Error::class, $result );
$this->assertEquals( 'missing_params', $result->get_error_code() );
}
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

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

Missing test for successful bulk_update_responses execution. Consider adding a test that creates feedback posts, calls bulk_update_responses with a valid action and IDs, and verifies that the responses were updated correctly. This would ensure the integration with Contact_Form_Endpoint::bulk_actions() works as expected.

Suggested change
}
}
/**
* Test bulk_update_responses success.
*/
public function test_bulk_update_responses_success() {
wp_set_current_user( self::$user_id );
$post_ids = array();
$post_ids[] = wp_insert_post(
array(
'post_type' => 'feedback',
'post_title' => 'Feedback to update 1',
'post_content' => 'First feedback content.',
'post_status' => 'publish',
)
);
$post_ids[] = wp_insert_post(
array(
'post_type' => 'feedback',
'post_title' => 'Feedback to update 2',
'post_content' => 'Second feedback content.',
'post_status' => 'publish',
)
);
$result = Forms_Abilities::bulk_update_responses(
array(
'action' => 'trash',
'ids' => $post_ids,
)
);
$this->assertNotInstanceOf( \WP_Error::class, $result );
foreach ( $post_ids as $post_id ) {
$this->assertEquals( 'trash', get_post_status( $post_id ) );
}
}

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The bulk actions endpoint has its own test coverage in Contact_Form_Endpoint_Test.php. The ability test validates input validation (missing params). A full integration test would require creating feedback posts with proper meta structure — out of scope for this thin adapter layer.

Comment on lines 137 to 146
$expected_abilities = array(
'jetpack-forms/list-forms',
'jetpack-forms/get-form',
'jetpack-forms/create-form',
'jetpack-forms/delete-form',
'jetpack-forms/get-responses',
'jetpack-forms/update-response',
'jetpack-forms/bulk-update-responses',
'jetpack-forms/get-status-counts',
);
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

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

Missing test coverage for list_forms ability. Consider adding tests that verify:

  1. Empty list when no forms exist
  2. Pagination and per_page parameters work correctly
  3. Search parameter filters by form title
  4. Status parameter filters by form status
  5. The dashboard context properly includes entries_count and edit_url fields

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

list_forms delegates to the REST endpoint via rest_do_request() — pagination, search, and status filtering are tested in Jetpack_Form_Endpoint_Test.php. Testing every REST parameter here would duplicate endpoint tests.

'jetpack-forms/get-form',
array(
'label' => __( 'Get form details', 'jetpack-forms' ),
'description' => __( 'Get a single form with its full structure, field definitions, response count, status, and preview URL.', 'jetpack-forms' ),
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

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

The description states "Get a single form with its full structure, field definitions, response count, status, and preview URL" but the implementation doesn't return a response count or preview URL. The returned data only includes id, title, status, fields, date, modified, and edit_url. Consider either updating the description to match the implementation or adding the missing fields (response_count and preview_url) to the returned data.

Suggested change
'description' => __( 'Get a single form with its full structure, field definitions, response count, status, and preview URL.', 'jetpack-forms' ),
'description' => __( 'Get a single form with its full structure, field definitions, status, dates, and edit URL.', 'jetpack-forms' ),

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in the refactor commit — description now reads "field definitions, status, and edit URL" which matches the actual return value.

Comment on lines 688 to 699
$content = $args['content'] ?? '';
if ( '' === $content ) {
// Default form structure with a submit button.
$content = '<!-- wp:jetpack/contact-form --><!-- wp:jetpack/button {"element":"button","text":"Submit","lock":{"remove":true}} /--><!-- /wp:jetpack/contact-form -->';
}

$status = $args['status'] ?? 'publish';
$post_id = wp_insert_post(
array(
'post_type' => Contact_Form::POST_TYPE,
'post_title' => sanitize_text_field( $args['title'] ),
'post_content' => $content,
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

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

The 'content' parameter passed to create_form is not sanitized before being used in wp_insert_post. While WordPress does apply some sanitization internally, block content should ideally be validated to ensure it contains properly formatted block markup. Consider adding validation to ensure the content is valid block syntax or at least doesn't contain malicious script tags.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Resolved by the rest_do_request() refactor — create_form now delegates to POST /wp/v2/jetpack-forms, which runs the full REST sanitization pipeline including wp_kses_post() on content.

'jetpack-forms/create-form',
array(
'label' => __( 'Create a form', 'jetpack-forms' ),
'description' => __( 'Create a new form with a title. Optionally provide block content for the form structure. Returns the new form ID and edit URL.', 'jetpack-forms' ),
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

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

The description states that create_form "Returns the new form ID and edit URL" but the actual return value includes additional fields: 'id', 'title', 'status', and 'edit_url'. Consider updating the description to "Returns the new form data including ID, title, status, and edit URL" to accurately reflect all returned fields.

Suggested change
'description' => __( 'Create a new form with a title. Optionally provide block content for the form structure. Returns the new form ID and edit URL.', 'jetpack-forms' ),
'description' => __( 'Create a new form with a title. Optionally provide block content for the form structure. Returns the new form data including ID, title, status, and edit URL.', 'jetpack-forms' ),

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The description is a summary for AI/MCP consumers — it highlights the most useful fields (ID and edit URL) without being an exhaustive spec. The current wording is accurate and concise.

@jp-launch-control
Copy link

jp-launch-control bot commented Feb 12, 2026

Code Coverage Summary

Coverage changed in 3 files.

File Coverage Δ% Δ Uncovered
projects/packages/forms/src/abilities/class-forms-abilities.php 440/496 (88.71%) 0.32% 30 💔
projects/packages/forms/src/contact-form/class-contact-form-endpoint.php 812/959 (84.67%) 0.10% -1 💚
projects/packages/forms/src/contact-form/class-contact-form-plugin.php 570/1476 (38.62%) 0.14% -2 💚

Full summary · PHP report · JS report

If appropriate, add one of these labels to override the failing coverage check: Covered by non-unit tests Use to ignore the Code coverage requirement check when E2Es or other non-unit tests cover the code Coverage tests to be added later Use to ignore the Code coverage requirement check when tests will be added in a follow-up PR I don't care about code coverage for this PR Use this label to ignore the check for insufficient code coveage.

lezama and others added 2 commits February 12, 2026 18:07
All ability callbacks now create WP_REST_Request objects and dispatch
through the REST pipeline instead of directly calling endpoint methods.
This eliminates code duplication and inherits endpoint validation,
sanitization, and hooks.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings February 12, 2026 23:50
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

'fields' => $fields,
'date' => $data['date'],
'modified' => $data['modified'],
'edit_url' => $data['link'] ?? get_edit_post_link( $data['id'], 'raw' ),
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

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

get_form() sets edit_url from $data['link'], but the REST link field is the public permalink (view URL), not the wp-admin edit URL. This will return the wrong URL when callers expect an edit link. Prefer always using get_edit_post_link( $data['id'], 'raw' ) (or $data['edit_url'] if the endpoint provides it) and avoid falling back to link here.

Suggested change
'edit_url' => $data['link'] ?? get_edit_post_link( $data['id'], 'raw' ),
'edit_url' => get_edit_post_link( $data['id'], 'raw' ),

Copilot uses AI. Check for mistakes.
@lezama lezama requested a review from enejb February 13, 2026 18:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Contact Form [Package] Forms [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. [Tests] Includes Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant