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

Map Block: Use the A8C's Mapbox access token if the site doesn't provide one #14307

Merged
merged 16 commits into from
Jan 23, 2020

Conversation

Copons
Copy link
Contributor

@Copons Copons commented Jan 8, 2020

Fix https://github.com/Automattic/Dotcom-roadmap/issues/74

As this is a proof of concept, I haven't included the real A8C Mapbox access token yet.
For testing purpose, the MAPBOX_A8C_ACCESS_TOKEN constant can be locally updated with the real A8C token (a12s know where to find it), or with a personal one.

Changes proposed in this Pull Request:

  • Use the Automattic's Mapbox access token if the site doesn't provide one.
  • Hide the Mapbox Access Token field when using Automattic's.
  • Add some spacing in the Map block placeholder text.

Is this a new feature or does it add/remove features to an existing part of Jetpack?

  • Enhancement of an existing feature.

Internal reference: pb5gDS-ev-p2

Testing instructions:

Prerequisites

  • Have a Simple, Atomic, and Jetpack test sites ready.
  • Make sure none of them has a Mapbox token saved. In that case, before applying this PR, please remove it in the Mapbox Access Token field in the Map block sidebar.

Jetpack

  • Open the editor and insert a Map block.
  • Make sure the Map block loads in the placeholder state, and asks for a token.
  • Provide a token, and make sure the map loads correctly.
  • Make sure it's possible to change and remove the token in the Mapbox Access Token field in the block sidebar.
  • Save the post and check the front end.
  • Make sure the Map block is rendered correctly.
  • Update the token in the Mapbox Access Token field in the block sidebar with a random incorrect string.
  • Wait a bit for the API call to complete and then make sure the block reverted to the placeholder state, with an "incorrect token" error notice.

Simple

  • Apply D37418-code and sandbox the API.
  • Open the editor and insert a Map block.
  • Make sure the Map block loads without asking for an access token.
  • Make sure there is no Mapbox Access Token field in the block sidebar.
  • Save the post and check the front end.
  • Make sure the Map block is rendered correctly.

Atomic

  • Install and activate the Jetpack Beta plugin.
  • In wp-admin, navigate to Jetpack -> Jetpack Beta; search for the update/mapbox-access-token feature branch, and activate it.
  • Repeat the same testing steps for a Simple site (skip the first step).

Proposed changelog entry for your changes:

  • Map Block: Use the Automattic's Mapbox access token on WordPress.com sites.

@Copons Copons added [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it [Status] In Progress [Focus] Blocks Issues related to the block editor, aka Gutenberg, and its extensions developed in Jetpack [Block] Map labels Jan 8, 2020
@Copons Copons requested a review from a team January 8, 2020 15:44
@Copons Copons self-assigned this Jan 8, 2020
@matticbot
Copy link
Contributor

Caution: This PR has changes that must be merged to WordPress.com
Hello Copons! These changes need to be synced to WordPress.com - If you 're an a11n, please commandeer, review, and approve D37418-code before merging this PR. Thank you!

@jetpackbot
Copy link

jetpackbot commented Jan 8, 2020

Thank you for the great PR description!

When this PR is ready for review, please apply the [Status] Needs Review label. If you are an a11n, please have someone from your team review the code if possible. The Jetpack team will also review this PR and merge it to be included in the next Jetpack release.

Scheduled Jetpack release: February 11, 2020.
Scheduled code freeze: February 4, 2020

Generated by 🚫 dangerJS against 9c25745

@creativecoder creativecoder self-requested a review January 8, 2020 17:07
@creativecoder
Copy link
Contributor

@Copons It's possible we might want to rotate this key at times. What do you think of providing the key through the wpcom API in some way so that we can rotate it when needed without a Jetpack release?

@matticbot
Copy link
Contributor

Copons, Your synced wpcom patch D37418-code has been updated.

@Copons
Copy link
Contributor Author

Copons commented Jan 8, 2020

@creativecoder good point. I've created D37434-code and updated this PR accordingly.
It needs more polish though.

@Copons Copons added [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. and removed [Status] In Progress labels Jan 9, 2020
@Copons
Copy link
Contributor Author

Copons commented Jan 9, 2020

I'm putting this in review, even though I'm not super happy about how it provides the access token to the editor.
I'd rather fetch it directly in JS than doing so in PHP and sending the token to JS via wp_localize_script, but it always fails because of the CORS.
At the same time, I don't find such improvement a blocker for this change.

@matticbot
Copy link
Contributor

Copons, Your synced wpcom patch D37418-code has been updated.

@jeherve jeherve added this to the 8.2 milestone Jan 9, 2020
@matticbot
Copy link
Contributor

Copons, Your synced wpcom patch D37418-code has been updated.

Copy link
Member

@jeherve jeherve left a comment

Choose a reason for hiding this comment

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

I'd rather fetch it directly in JS than doing so in PHP and sending the token to JS via wp_localize_script, but it always fails because of the CORS.

Do you think it may be possible to change things at a higher level, and query WordPress.com for an API key here?

public static function get_service_api_key( $request ) {

In doing so you would be able to reuse the existing elements used to query /wpcom/v2/service-api-keys/mapbox.

Maybe that would mean adding an extra element to the response returned by get_service_api_key (in addition to service_api_key) so that we know that the key returned comes from Automattic and the whole token settings sidebar interface should not be displayed?

extensions/blocks/map/map.php Outdated Show resolved Hide resolved
extensions/blocks/map/map.php Outdated Show resolved Hide resolved
@jeherve jeherve added [Status] Needs Author Reply We would need you to make some changes or provide some more details about your PR. Thank you! and removed [Status] Needs Review To request a review from fellow Jetpack developers. Label will be renamed soon. labels Jan 10, 2020
@Copons
Copy link
Contributor Author

Copons commented Jan 14, 2020

Thanks for the review @jeherve!

Do you think it may be possible to change things at a higher level, and query WordPress.com for an API key here?

public static function get_service_api_key( $request ) {

In doing so you would be able to reuse the existing elements used to query /wpcom/v2/service-api-keys/mapbox.

Maybe that would mean adding an extra element to the response returned by get_service_api_key (in addition to service_api_key) so that we know that the key returned comes from Automattic and the whole token settings sidebar interface should not be displayed?

That was my initial approach, but I found a few blockers and went for the WPCOM endpoint instead.
I will try again now with the WPCOM endpoint backing the whole thing! 👍

@matticbot
Copy link
Contributor

Copons, Your synced wpcom patch D37418-code has been updated.

@Copons Copons added [Status] In Progress 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 Jan 14, 2020
_inc/lib/core-api/wpcom-endpoints/service-api-keys.php Outdated Show resolved Hide resolved
extensions/blocks/map/map.php Outdated Show resolved Hide resolved
@matticbot
Copy link
Contributor

Copons, Your synced wpcom patch D37418-code has been updated.

2 similar comments
@matticbot
Copy link
Contributor

Copons, Your synced wpcom patch D37418-code has been updated.

@matticbot
Copy link
Contributor

Copons, Your synced wpcom patch D37418-code has been updated.

@jeherve jeherve force-pushed the update/mapbox-access-token branch from 9336c14 to 9c25745 Compare January 23, 2020 11:50
Copy link
Member

@jeherve jeherve left a comment

Choose a reason for hiding this comment

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

I am merging this for now, this tests well for me.

Quick note that we'll probably need to revisit how we fetch the API key here:
https://github.com/Automattic/jetpack/blob/update/mapbox-access-token/_inc/lib/core-api/wpcom-endpoints/service-api-keys.php#L315

While this works well for public sites, it wont't work for private sites on WordPress.com as we need to be authenticated to make requests to those sites.

We'll consequently probably need to go through wpcom_json_api_request_as_blog there.

@jeherve jeherve added [Status] Ready to Merge Go ahead, you can push that green button! 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 Jan 23, 2020
@jeherve jeherve merged commit 56165fe into master Jan 23, 2020
@jeherve jeherve deleted the update/mapbox-access-token branch January 23, 2020 13:48
@matticbot matticbot added [Status] Needs Changelog and removed [Status] Ready to Merge Go ahead, you can push that green button! labels Jan 23, 2020
@jeherve jeherve added the [Status] Needs Testing We need to add this change to the testing call for this month's release label Jan 23, 2020
@jeherve
Copy link
Member

jeherve commented Jan 23, 2020

r202027-wpcom

@creativecoder
Copy link
Contributor

Quick note that we'll probably need to revisit how we fetch the API key here:
https://github.com/Automattic/jetpack/blob/update/mapbox-access-token/_inc/lib/core-api/wpcom-endpoints/service-api-keys.php#L315

While this works well for public sites, it won't work for private sites on WordPress.com as we need to be authenticated to make requests to those sites.

Because of new sites on WordPress.com being "private by default", we'll need the api key request to be authenticated. Using the map block as part of scaffolding content for new sites (which are private) is a primary reason for this update to the map block.

Tracking in #14475

@jeherve jeherve added [Status] Has Changelog and removed [Status] Needs Changelog [Status] Needs Testing We need to add this change to the testing call for this month's release labels Jan 27, 2020
jeherve added a commit that referenced this pull request Jan 27, 2020
jeherve added a commit that referenced this pull request Jan 28, 2020
* [not verified] Remove empty readme section

* Initial changelog for 8.2

* Changelog: add #14220

* Changelog: add #14252

* Changelog: add #14291

* Changelog: add #14309

* Changelog: add #14304

* Changelog: add general connection log.

* Changelog: add #14275

* Changelog: add #14313

* Changelog: add #14213

* Changelog: add #14357

* Add sync testing instructions

* Add 8.1.1 changelog back

See eeaafab and 61757eb

* Changelog: add #14371

* Changelog: add #14386

* Changelog: add #14471

* Changelog: add #14325

* Changelog: add #14194

* Changelog: add #14340

* Changelog: add #14418

* Changelog: add #14417

* Changelog: add #14075

* Changelog: add #14467

* Changelog: add #14307

* Changelog: add #14326
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Map [Focus] Blocks Issues related to the block editor, aka Gutenberg, and its extensions developed in Jetpack Touches WP.com Files [Type] Enhancement Changes to an existing feature — removing, adding, or changing parts of it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants