Skip to content
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 19 commits into from
Feb 24, 2023
Merged

Add form responses v2 endpoint #29043

merged 19 commits into from
Feb 24, 2023

Conversation

CGastrell
Copy link
Contributor

@CGastrell CGastrell commented Feb 20, 2023

Proposed changes:

This PR adds wpcom/v2/form-responses endpoint as the target for WPCOM's D102076-code

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

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

Testing instructions:

On docker, add these 2 lines on plugins/jetpack/modules/contact-form.php:

add_filter( 'jetpack_contact_form_use_package', '__return_true' );
add_filter( 'jetpack_forms_dashboard_enable', '__return_true' );

Then head to http://localhost/wp-admin/admin.php?page=jetpack-forms to get a mock of the new inbox page. Open devtools and type this on the console:

wp.apiFetch({path: '/wpcom/v2/forms/responses'}).then(r=>console.log(r))

You should get a list of form responses.

@CGastrell CGastrell self-assigned this Feb 20, 2023
@github-actions github-actions bot added the [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ label Feb 20, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Feb 20, 2023

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.
  • ⚠️ All commits were linted before commit.
  • ✅ Add a "[Status]" label (In Progress, Needs Team 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 🤖


The e2e test report can be found here. Please note that it can take a few minutes after the e2e tests checks are complete for the report to be available.


Once your PR is ready for review, check one last time that all required checks (other than "Required review") appearing at the bottom of this PR are passing or skipped.
Then, add the "[Status] Needs Team review" label and ask someone from your team review the code.
Once you’ve done so, switch to the "[Status] Needs Review" label; someone from Jetpack Crew will then review this PR and merge it to be included in the next Jetpack release.


Jetpack plugin:

  • Next scheduled release: March 7, 2023.
  • Scheduled code freeze: February 28, 2023.

@github-actions github-actions bot added the [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! label Feb 20, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Feb 20, 2023

Are you an Automattician? You can now test your Pull Request on WordPress.com. On your sandbox, run

bin/jetpack-downloader test jetpack add/form-responses-v2-endpoint

to get started. More details: p9dueE-5Nn-p2

@CGastrell CGastrell added [Status] Needs Team Review and removed [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! labels Feb 20, 2023
@CGastrell CGastrell force-pushed the add/form-responses-v2-endpoint branch from 342844c to 3e9e495 Compare February 21, 2023 14:23
@fgiannar fgiannar self-requested a review February 22, 2023 09:43
ice9js
ice9js previously approved these changes Feb 22, 2023
Copy link
Member

@ice9js ice9js left a comment

Choose a reason for hiding this comment

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

Works for me! Code looks clean too, just left one comment but nothing blocking 👍

@CGastrell CGastrell added [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. and removed [Status] Needs Team Review labels Feb 22, 2023
Copy link
Contributor

@fgiannar fgiannar left a comment

Choose a reason for hiding this comment

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

Nice job, Christian!

Left a couple inline comments. Many thanks for working on this!

@CGastrell
Copy link
Contributor Author

@fgiannar all wonderful suggestions, thanks! I think I've addressed them all.

I did search for more explicit docs on those validate_callback params (though they seem very simple) but couldn't find any. Shall you have some links on that, appreciated (.org docs didn't mention much).

@CGastrell CGastrell requested a review from fgiannar February 23, 2023 17:45
@fgiannar
Copy link
Contributor

@fgiannar all wonderful suggestions, thanks! I think I've addressed them all.

Excellent work, thank you! I only have two more suggestions:

  1. Regarding the usage of is_user_member_of_blog : This will always return true if Multisite is not enabled so maybe it's best to use a capabilities check instead?
  2. Not a blocker: How would you feel about adding a few unit tests for the endpoint you introduced?

I did search for more explicit docs on those validate_callback params (though they seem very simple) but couldn't find any. Shall you have some links on that, appreciated (.org docs didn't mention much).

https://shawnhooper.ca/2017/02/15/wp-rest-secrets-found-reading-core-code/

@CGastrell
Copy link
Contributor Author

  1. Regarding the usage of is_user_member_of_blog : This will always return true if Multisite is not enabled so maybe it's best to use a capabilities check instead?

Oh boy, there was a current_user_can( 'manage_options' ) there, I don't know when it went out the window 😬 Reinstated now. manage_options might sound too restrictive, but we'll start there and see how it goes.

Not a blocker: How would you feel about adding a few unit tests for the endpoint you introduced?

I've been scouting how to do so, but a lot of the rest testing libs reside only on WPCOM. We're looking to have better/more testing on the package, maybe even bring some of the libs to the package to be able to do so, but I think we can keep at it as continuous effort on further iterations. I'll look on other packages to see how they got it, but for now I'd like to move this forward.

Thanks for the reviews!

return $site_id;
}

if ( ! current_user_can( 'manage_options' ) || ! is_user_member_of_blog( get_current_user_id(), $site_id ) ) {
Copy link
Contributor

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, then is_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.

Copy link
Contributor Author

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?

Copy link
Contributor

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 :)

Copy link
Member

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

Copy link
Contributor

@fgiannar fgiannar left a comment

Choose a reason for hiding this comment

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

Thanks for addressing all my comments! Appreciate your effort 👍

@fgiannar fgiannar added [Status] Ready to Merge Go ahead, you can push that green button! and removed [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. labels Feb 24, 2023
@CGastrell CGastrell enabled auto-merge (squash) February 24, 2023 17:43
@CGastrell CGastrell merged commit 5bf1755 into trunk Feb 24, 2023
@CGastrell CGastrell deleted the add/form-responses-v2-endpoint branch February 24, 2023 17:54
@github-actions github-actions bot removed the [Status] Ready to Merge Go ahead, you can push that green button! label Feb 24, 2023
@github-actions github-actions bot added this to the jetpack/11.9 milestone Feb 24, 2023
jhnstn pushed a commit that referenced this pull request Feb 24, 2023
* [not verified] add form-responses endpoint to be handled by forms package. Mapped through from .com

* [not verified] changelog

* fix typo on class name

* [not verified] add backend code for v2/v4 endpoint

* changelog

* fixup project versions

* use wpcom/v2 instead of jetpack/v4

* update apiRoot backend config JS

* remove unnecessary plugin endpoint file, add api-fetch dependency for testing

* rename controller file to reflect forms base path

* instance the right class name after change

* fix route register handler name

* edit comments/docblocks

* use simpler validation through args props

* use Jetpack\Connection\Manager to get site ID, remove unneeded code and is_wpcom var

* get the date from post, not from parsed content

* add capability check to the endpoint permission callback
jhnstn pushed a commit that referenced this pull request Feb 24, 2023
* [not verified] add form-responses endpoint to be handled by forms package. Mapped through from .com

* [not verified] changelog

* fix typo on class name

* [not verified] add backend code for v2/v4 endpoint

* changelog

* fixup project versions

* use wpcom/v2 instead of jetpack/v4

* update apiRoot backend config JS

* remove unnecessary plugin endpoint file, add api-fetch dependency for testing

* rename controller file to reflect forms base path

* instance the right class name after change

* fix route register handler name

* edit comments/docblocks

* use simpler validation through args props

* use Jetpack\Connection\Manager to get site ID, remove unneeded code and is_wpcom var

* get the date from post, not from parsed content

* add capability check to the endpoint permission callback
donnchawp pushed a commit that referenced this pull request Feb 27, 2023
* [not verified] add form-responses endpoint to be handled by forms package. Mapped through from .com

* [not verified] changelog

* fix typo on class name

* [not verified] add backend code for v2/v4 endpoint

* changelog

* fixup project versions

* use wpcom/v2 instead of jetpack/v4

* update apiRoot backend config JS

* remove unnecessary plugin endpoint file, add api-fetch dependency for testing

* rename controller file to reflect forms base path

* instance the right class name after change

* fix route register handler name

* edit comments/docblocks

* use simpler validation through args props

* use Jetpack\Connection\Manager to get site ID, remove unneeded code and is_wpcom var

* get the date from post, not from parsed content

* add capability check to the endpoint permission callback
jeherve added a commit that referenced this pull request Mar 1, 2023
This is a follow-up to #29043. When registering the endpoint, we used the wrong parameter to define permissions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Forms [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants