From 861a579cc92f6830af744443aa9162c5ad19d4b6 Mon Sep 17 00:00:00 2001 From: Dusty Reagan Date: Fri, 24 Jan 2025 12:10:07 -0600 Subject: [PATCH] Comments API: Add wpcom user fields option for Jetpack sites (#41254) * 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 * Check for simple site first --------- Co-authored-by: Jeremy Herve --- .../add-comment-endpoint-wpcom-user-fields | 4 ++++ .../jetpack/class.json-api-endpoints.php | 22 +++++++++++++++++++ ....wpcom-json-api-list-comments-endpoint.php | 22 +++++++++++-------- 3 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 projects/plugins/jetpack/changelog/add-comment-endpoint-wpcom-user-fields diff --git a/projects/plugins/jetpack/changelog/add-comment-endpoint-wpcom-user-fields b/projects/plugins/jetpack/changelog/add-comment-endpoint-wpcom-user-fields new file mode 100644 index 0000000000000..56e3b470c7d40 --- /dev/null +++ b/projects/plugins/jetpack/changelog/add-comment-endpoint-wpcom-user-fields @@ -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. diff --git a/projects/plugins/jetpack/class.json-api-endpoints.php b/projects/plugins/jetpack/class.json-api-endpoints.php index 794bd049d6545..1bdbe1ef254bd 100644 --- a/projects/plugins/jetpack/class.json-api-endpoints.php +++ b/projects/plugins/jetpack/class.json-api-endpoints.php @@ -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'; @@ -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; @@ -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; } diff --git a/projects/plugins/jetpack/json-endpoints/class.wpcom-json-api-list-comments-endpoint.php b/projects/plugins/jetpack/json-endpoints/class.wpcom-json-api-list-comments-endpoint.php index 12390302281f5..993e14303cbf1 100644 --- a/projects/plugins/jetpack/json-endpoints/class.wpcom-json-api-list-comments-endpoint.php +++ b/projects/plugins/jetpack/json-endpoints/class.wpcom-json-api-list-comments-endpoint.php @@ -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 offset 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 offset 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', + ), ) ); }