| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 |
|
| 3 |
/** |
| 4 |
* The Get comment backup endpoint class. |
| 5 |
* |
| 6 |
* /sites/%s/comments/%d/backup -> $blog_id, $comment_id |
| 7 |
*/ |
| 8 |
class Jetpack_JSON_API_Get_Comment_Backup_Endpoint extends Jetpack_JSON_API_Endpoint { |
| 9 |
|
| 10 |
/** |
| 11 |
* Needed capabilities. |
| 12 |
* |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
protected $needed_capabilities = array(); // This endpoint is only accessible using a site token |
| 16 |
|
| 17 |
/** |
| 18 |
* The comment ID. |
| 19 |
* |
| 20 |
* @var int |
| 21 |
*/ |
| 22 |
protected $comment_id; |
| 23 |
|
| 24 |
/** |
| 25 |
* Validate input |
| 26 |
* |
| 27 |
* @param int $comment_id - the comment ID. |
| 28 |
* |
| 29 |
* @return bool|WP_Error |
| 30 |
*/ |
| 31 |
public function validate_input( $comment_id ) { |
| 32 |
if ( empty( $comment_id ) || ! is_numeric( $comment_id ) ) { |
| 33 |
return new WP_Error( 'comment_id_not_specified', __( 'You must specify a Comment ID', 'jetpack' ), 400 ); |
| 34 |
} |
| 35 |
|
| 36 |
$this->comment_id = (int) $comment_id; |
| 37 |
|
| 38 |
return true; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* The result. |
| 43 |
* |
| 44 |
* @return array|WP_Error |
| 45 |
*/ |
| 46 |
protected function result() { |
| 47 |
// Disable Sync as this is a read-only operation and triggered by sync activity. |
| 48 |
\Automattic\Jetpack\Sync\Actions::mark_sync_read_only(); |
| 49 |
|
| 50 |
$comment = get_comment( $this->comment_id ); |
| 51 |
if ( empty( $comment ) ) { |
| 52 |
return new WP_Error( 'comment_not_found', __( 'Comment not found', 'jetpack' ), 404 ); |
| 53 |
} |
| 54 |
|
| 55 |
$allowed_keys = array( |
| 56 |
'comment_ID', |
| 57 |
'comment_post_ID', |
| 58 |
'comment_author', |
| 59 |
'comment_author_email', |
| 60 |
'comment_author_url', |
| 61 |
'comment_author_IP', |
| 62 |
'comment_date', |
| 63 |
'comment_date_gmt', |
| 64 |
'comment_content', |
| 65 |
'comment_karma', |
| 66 |
'comment_approved', |
| 67 |
'comment_agent', |
| 68 |
'comment_type', |
| 69 |
'comment_parent', |
| 70 |
'user_id', |
| 71 |
); |
| 72 |
|
| 73 |
$comment = array_intersect_key( $comment->to_array(), array_flip( $allowed_keys ) ); |
| 74 |
$comment_meta = get_comment_meta( $comment['comment_ID'] ); |
| 75 |
|
| 76 |
return array( |
| 77 |
'comment' => $comment, |
| 78 |
'meta' => is_array( $comment_meta ) ? $comment_meta : array(), |
| 79 |
); |
| 80 |
} |
| 81 |
} |
| 82 |
|