@@ -1243,6 +1243,9 @@ protected function _prepare_comment( $comment ) {
12431243 } else {
12441244 $ comment_status = $ comment ->comment_approved ;
12451245 }
1246+
1247+ $ can_moderate_or_edit = current_user_can ( 'moderate_comments ' ) || current_user_can ( 'edit_comment ' , $ comment ->comment_ID );
1248+
12461249 $ _comment = array (
12471250 'date_created_gmt ' => $ comment_date_gmt ,
12481251 'user_id ' => $ comment ->user_id ,
@@ -1255,8 +1258,8 @@ protected function _prepare_comment( $comment ) {
12551258 'post_title ' => get_the_title ( $ comment ->comment_post_ID ),
12561259 'author ' => $ comment ->comment_author ,
12571260 'author_url ' => $ comment ->comment_author_url ,
1258- 'author_email ' => $ comment ->comment_author_email ,
1259- 'author_ip ' => $ comment ->comment_author_IP ,
1261+ 'author_email ' => $ can_moderate_or_edit ? $ comment ->comment_author_email : '' ,
1262+ 'author_ip ' => $ can_moderate_or_edit ? $ comment ->comment_author_IP : '' ,
12601263 'type ' => $ comment ->comment_type ,
12611264 );
12621265
@@ -3784,7 +3787,9 @@ public function wp_getComments( $args ) {
37843787
37853788 $ status = $ struct ['status ' ] ?? '' ;
37863789
3787- if ( ! current_user_can ( 'moderate_comments ' ) && 'approve ' !== $ status ) {
3790+ $ can_moderate = current_user_can ( 'moderate_comments ' );
3791+
3792+ if ( ! $ can_moderate && 'approve ' !== $ status ) {
37883793 return new IXR_Error ( 401 , __ ( 'Invalid comment status. ' ) );
37893794 }
37903795
@@ -3793,6 +3798,17 @@ public function wp_getComments( $args ) {
37933798 $ post_id = absint ( $ struct ['post_id ' ] );
37943799 }
37953800
3801+ $ has_public_status = static function ( $ post ) {
3802+ $ status = get_post_status ( $ post );
3803+ $ status_object = $ status ? get_post_status_object ( $ status ) : null ;
3804+
3805+ return $ status_object && $ status_object ->public ;
3806+ };
3807+
3808+ if ( $ post_id && ! $ can_moderate && ! $ has_public_status ( $ post_id ) && ! current_user_can ( 'read_post ' , $ post_id ) ) {
3809+ return new IXR_Error ( 401 , __ ( 'Invalid post ID. ' ) );
3810+ }
3811+
37963812 $ post_type = '' ;
37973813 if ( isset ( $ struct ['post_type ' ] ) ) {
37983814 $ post_type_object = get_post_type_object ( $ struct ['post_type ' ] );
@@ -3814,17 +3830,31 @@ public function wp_getComments( $args ) {
38143830
38153831 $ comments = get_comments (
38163832 array (
3817- 'status ' => $ status ,
3818- 'post_id ' => $ post_id ,
3819- 'offset ' => $ offset ,
3820- 'number ' => $ number ,
3821- 'post_type ' => $ post_type ,
3833+ 'status ' => $ status ,
3834+ 'post_id ' => $ post_id ,
3835+ 'offset ' => $ offset ,
3836+ 'number ' => $ number ,
3837+ 'post_type ' => $ post_type ,
3838+ 'update_comment_post_cache ' => true ,
38223839 )
38233840 );
38243841
38253842 $ comments_struct = array ();
38263843 if ( is_array ( $ comments ) ) {
38273844 foreach ( $ comments as $ comment ) {
3845+ if ( ! $ can_moderate && ! current_user_can ( 'edit_comment ' , $ comment ->comment_ID ) ) {
3846+ $ post = get_post ( $ comment ->comment_post_ID );
3847+
3848+ if (
3849+ ! $ post
3850+ || ! is_post_type_viewable ( $ post ->post_type )
3851+ || post_password_required ( $ post )
3852+ || ( ! $ has_public_status ( $ post ) && ! current_user_can ( 'read_post ' , $ post ->ID ) )
3853+ ) {
3854+ continue ;
3855+ }
3856+ }
3857+
38283858 $ comments_struct [] = $ this ->_prepare_comment ( $ comment );
38293859 }
38303860 }
0 commit comments