diff --git a/CHANGELOG.md b/CHANGELOG.md index c4ea5bf45d..b44f6fa690 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 +- 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 diff --git a/class.json-api-endpoints.php b/class.json-api-endpoints.php index 794bd049d6..1bdbe1ef25 100644 --- a/class.json-api-endpoints.php +++ b/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/json-endpoints/class.wpcom-json-api-list-comments-endpoint.php b/json-endpoints/class.wpcom-json-api-list-comments-endpoint.php index 1239030228..993e14303c 100644 --- a/json-endpoints/class.wpcom-json-api-list-comments-endpoint.php +++ b/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', + ), ) ); }