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 <jeremy@jeremy.hu>

* Check for simple site first

---------

Co-authored-by: Jeremy Herve <jeremy@jeremy.hu>
  • Loading branch information
DustyReagan and jeherve authored Jan 24, 2025
1 parent 3bfae29 commit 861a579
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: other

Comments API: Add wpcom_id and wpcom_login fields to comment author responses when requested via author_wpcom_data parameter.
22 changes: 22 additions & 0 deletions projects/plugins/jetpack/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
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 861a579

Please sign in to comment.