Skip to content

Add utility for WPCOM API requests as user#9122

Merged
zinigor merged 2 commits intomasterfrom
add/jetpack-client-request-as-user
Mar 26, 2018
Merged

Add utility for WPCOM API requests as user#9122
zinigor merged 2 commits intomasterfrom
add/jetpack-client-request-as-user

Conversation

@jaswrks
Copy link
Copy Markdown
Contributor

@jaswrks jaswrks commented Mar 22, 2018

This adds a new utility method: Jetpack_Client::wpcom_json_api_request_as_user()

  • Depends on D11180-code, which has already been merged (now live).

Getting/Setting GDPR Settings via Jetpack

Get current settings:

$response = Jetpack_Client::wpcom_json_api_request_as_user( '/jetpack-user-tracking' );

if ( ! is_wp_error( $response ) ) {
    $settings = json_decode( wp_remote_retrieve_body( $response ) );
}

// var_dump( $settings->tracks_opt_out ); // (bool)

Update settings:

$response = Jetpack_Client::wpcom_json_api_request_as_user(
    '/jetpack-user-tracking',
    'v2',
    array( 'method' => 'PUT' ),
    array( 'tracks_opt_out' => true )
);

if ( ! is_wp_error( $response ) ) {
    $settings = json_decode( wp_remote_retrieve_body( $response ) );
}

// var_dump( $settings->tracks_opt_out ); // (bool)

@jaswrks jaswrks added this to the GDPR Compliant milestone Mar 22, 2018
@jaswrks jaswrks requested a review from a team as a code owner March 22, 2018 14:46
@ghost ghost removed the [Status] In Progress label Mar 22, 2018
@mattwiebe mattwiebe self-requested a review March 22, 2018 20:40
@jaswrks jaswrks added [Status] Needs Review This PR is ready for review. and removed [Status] In Progress labels Mar 22, 2018
@mattwiebe mattwiebe requested review from enejb and oskosk March 23, 2018 16:45
Copy link
Copy Markdown
Contributor

@mattwiebe mattwiebe left a comment

Choose a reason for hiding this comment

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

Good work here. Works well with the new opt-out endpoint, which is now live on wpcom.

@enejb
Copy link
Copy Markdown
Member

enejb commented Mar 23, 2018

Nicely done.

My only feedback here is to make the method more clear that we are requesting the new rest api. (WordPress.com REST API v2)

Unless this method also works with the old api. (Which I have not tested)

Also since there is already a method for requesting the old api as a blog it would be good if these 2 methods had the same order of arguments.

static function wpcom_json_api_request_as_blog( $path, $version = self::WPCOM_JSON_API_VERSION, $args = array(), $body = null, $base_api_path = 'rest' );

vs

wpcom_json_api_request_as_user( $path, $args = array(), $body = null, $root = 'wpcom', $version = '2' )

jaswrks added a commit that referenced this pull request Mar 23, 2018
- Add endpoint proxy for D11180-code
- Add `Jetpack::current_user_ip()` with `$check_all_headers` param.

Depends on: #9122
@jaswrks
Copy link
Copy Markdown
Contributor Author

jaswrks commented Mar 23, 2018

@enejb writes...

Also since there is already a method for requesting the old api as a blog it would be good if these 2 methods had the same order of arguments.

Good suggestion. Thank you. I fixed this in ddeb969 so it now matches the order of arguments to Jetpack_Client:: wpcom_json_api_request_as_blog() as you suggested.

My only feedback here is to make the method more clear that we are requesting the new rest api. (WordPress.com REST API v2) Unless this method also works with the old api. (Which I have not tested)

This method currently works only with v2 of the API. However, it does support a $version parameter like Jetpack_Client::wpcom_json_api_request_as_blog() does. So it could be updated to support older (or newer) versions in the future, yes.

@jaswrks
Copy link
Copy Markdown
Contributor Author

jaswrks commented Mar 23, 2018

Just noting that #9142 now depends on this PR.

@zinigor zinigor added [Status] Ready to Merge Go ahead, you can push that green button! and removed [Status] Needs Review This PR is ready for review. labels Mar 26, 2018
@zinigor zinigor merged commit 07e9980 into master Mar 26, 2018
@ghost ghost removed the [Status] Ready to Merge Go ahead, you can push that green button! label Mar 26, 2018
@zinigor zinigor deleted the add/jetpack-client-request-as-user branch March 26, 2018 15:19
@oskosk oskosk modified the milestones: GDPR Compliant, 6.0 Mar 26, 2018
dereksmart pushed a commit that referenced this pull request Mar 27, 2018
* Add tracking settings endpoint proxy w/ IP utility

- Add endpoint proxy for D11180-code
- Add `Jetpack::current_user_ip()` with `$check_all_headers` param.

Depends on: #9122

* Fix content-type (application/json)

* Match order of args to `wpcom_json_api_request_as_blog()`

* Endpoint rename: `/tracking/settings`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants