Skip to content

Commit

Permalink
Comments API: Add wpcom user fields option for Jetpack sites (#41254)
Browse files Browse the repository at this point in the history
* Comments API: Add wpcom_id and wpcom_login fields to response

* changelog

* Move fields to the author object

* Introduce optional author_wpcom_data parameter

* Update comments

* Update changelog

* Check for empty id

* Populate wpcom data for simple sites

* Update changelog

* Apply suggestions from code review

Simplify code.

Co-authored-by: Jeremy Herve <[email protected]>

* Check for simple site first

---------

Co-authored-by: Jeremy Herve <[email protected]>

Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/12955125722

Upstream-Ref: Automattic/jetpack@861a579
  • Loading branch information
DustyReagan authored and matticbot committed Jan 24, 2025
1 parent f4caad1 commit 3e1f6bb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This is an alpha version! The changes listed here are not final.
- Social | Fix publicize error in the editor due to malformed connections data

### Other changes <!-- Non-user-facing changes go here. This section will not be copied to readme.txt. -->
- Comments API: Add wpcom_id and wpcom_login fields to comment author responses when requested via author_wpcom_data parameter.
- Deprecate Jetpack geo location module
- Fix the rendering of jetpack google fonts font faces for classic themes.
- Forms: fixed default editor stying for textarea
Expand Down
22 changes: 22 additions & 0 deletions class.json-api-endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
*/

use Automattic\Jetpack\Connection\Client;
use Automattic\Jetpack\Connection\Manager;
use Automattic\Jetpack\Status;
use Automattic\Jetpack\Status\Host;

require_once __DIR__ . '/json-api-config.php';
require_once __DIR__ . '/sal/class.json-api-links.php';
Expand Down Expand Up @@ -771,6 +773,8 @@ public function cast_and_filter_item( &$return, $type, $key, $value, $types = ar
'is_super_admin' => '(bool)',
'roles' => '(array:string)',
'ip_address' => '(string|false)',
'wpcom_id' => '(int|null)',
'wpcom_login' => '(string|null)',
);
$return[ $key ] = (object) $this->cast_and_filter( $value, $docs, false, $for_output );
break;
Expand Down Expand Up @@ -1521,6 +1525,24 @@ public function get_author( $author, $show_email_and_ip = false ) {
$author['site_visible'] = $site_visible;
}

// Only include WordPress.com user data when author_wpcom_data is enabled.
$args = $this->query_args();

if ( ! empty( $id ) && ! empty( $args['author_wpcom_data'] ) ) {
if ( ( new Host() )->is_wpcom_simple() ) {
$user = get_user_by( 'id', $id );
$author['wpcom_id'] = isset( $user->ID ) ? (int) $user->ID : null;
$author['wpcom_login'] = $user->user_login ?? '';
} else {
// If this is a Jetpack site, use the connection manager to get the user data.
$wpcom_user_data = ( new Manager() )->get_connected_user_data( $id );
if ( $wpcom_user_data && isset( $wpcom_user_data['ID'] ) ) {
$author['wpcom_id'] = (int) $wpcom_user_data['ID'];
$author['wpcom_login'] = $wpcom_user_data['login'] ?? '';
}
}
}

return (object) $author;
}

Expand Down
22 changes: 13 additions & 9 deletions json-endpoints/class.wpcom-json-api-list-comments-endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,33 +161,37 @@ public function __construct( $args ) {
$this->query = array_merge(
$this->query,
array(
'number' => '(int=20) The number of comments to return. Limit: 100. When using hierarchical=1, number refers to the number of top-level comments returned.',
'offset' => '(int=0) 0-indexed offset. Not available if using hierarchical=1.',
'page' => '(int) Return the Nth 1-indexed page of comments. Takes precedence over the <code>offset</code> parameter. When using hierarchical=1, pagination is a bit different. See the note on the number parameter.',
'order' => array(
'number' => '(int=20) The number of comments to return. Limit: 100. When using hierarchical=1, number refers to the number of top-level comments returned.',
'offset' => '(int=0) 0-indexed offset. Not available if using hierarchical=1.',
'page' => '(int) Return the Nth 1-indexed page of comments. Takes precedence over the <code>offset</code> parameter. When using hierarchical=1, pagination is a bit different. See the note on the number parameter.',
'order' => array(
'DESC' => 'Return comments in descending order from newest to oldest.',
'ASC' => 'Return comments in ascending order from oldest to newest.',
),
'hierarchical' => array(
'hierarchical' => array(
'false' => '',
'true' => '(BETA) Order the comment list hierarchically.',
),
'after' => '(ISO 8601 datetime) Return comments dated on or after the specified datetime. Not available if using hierarchical=1.',
'before' => '(ISO 8601 datetime) Return comments dated on or before the specified datetime. Not available if using hierarchical=1.',
'type' => array(
'after' => '(ISO 8601 datetime) Return comments dated on or after the specified datetime. Not available if using hierarchical=1.',
'before' => '(ISO 8601 datetime) Return comments dated on or before the specified datetime. Not available if using hierarchical=1.',
'type' => array(
'any' => 'Return all comments regardless of type.',
'comment' => 'Return only regular comments.',
'trackback' => 'Return only trackbacks.',
'pingback' => 'Return only pingbacks.',
'pings' => 'Return both trackbacks and pingbacks.',
),
'status' => array(
'status' => array(
'approved' => 'Return only approved comments.',
'unapproved' => 'Return only comments in the moderation queue.',
'spam' => 'Return only comments marked as spam.',
'trash' => 'Return only comments in the trash.',
'all' => 'Return comments of all statuses.',
),
'author_wpcom_data' => array(
'false' => 'Do not add wpcom_id and wpcom_login fields to comment author responses (default)',
'true' => 'Add wpcom_id and wpcom_login fields to comment author responses',
),
)
);
}
Expand Down

0 comments on commit 3e1f6bb

Please sign in to comment.