summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/jetpack/json-endpoints/class.wpcom-json-api-update-comment-endpoint.php')
-rw-r--r--plugins/jetpack/json-endpoints/class.wpcom-json-api-update-comment-endpoint.php395
1 files changed, 231 insertions, 164 deletions
diff --git a/plugins/jetpack/json-endpoints/class.wpcom-json-api-update-comment-endpoint.php b/plugins/jetpack/json-endpoints/class.wpcom-json-api-update-comment-endpoint.php
index a5056472..49117c8d 100644
--- a/plugins/jetpack/json-endpoints/class.wpcom-json-api-update-comment-endpoint.php
+++ b/plugins/jetpack/json-endpoints/class.wpcom-json-api-update-comment-endpoint.php
@@ -1,149 +1,185 @@
-<?php
-
-new WPCOM_JSON_API_Update_Comment_Endpoint( array(
- 'description' => 'Create a comment on a post.',
- 'group' => 'comments',
- 'stat' => 'posts:1:replies:new',
-
- 'method' => 'POST',
- 'path' => '/sites/%s/posts/%d/replies/new',
- 'path_labels' => array(
- '$site' => '(int|string) Site ID or domain',
- '$post_ID' => '(int) The post ID'
- ),
-
- 'request_format' => array(
- // explicitly document all input
- 'content' => '(HTML) The comment text.',
-// @todo Should we open this up to unauthenticated requests too?
-// 'author' => '(author object) The author of the comment.',
- ),
-
- 'pass_wpcom_user_details' => true,
-
- 'allow_fallback_to_jetpack_blog_token' => true,
-
- 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/posts/843/replies/new/',
- 'example_request_data' => array(
- 'headers' => array(
- 'authorization' => 'Bearer YOUR_API_TOKEN'
+<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
+/**
+ * Manage comments via the WordPress.com REST API.
+ *
+ * Endpoints;
+ * Create a comment on a post: /sites/%s/posts/%d/replies/new
+ * Create a comment as a reply to another comment: /sites/%s/comments/%d/replies/new
+ * Edit a comment: /sites/%s/comments/%d
+ * Delete a comment: /sites/%s/comments/%d/delete
+ */
+
+new WPCOM_JSON_API_Update_Comment_Endpoint(
+ array(
+ 'description' => 'Create a comment on a post.',
+ 'group' => 'comments',
+ 'stat' => 'posts:1:replies:new',
+
+ 'method' => 'POST',
+ 'path' => '/sites/%s/posts/%d/replies/new',
+ 'path_labels' => array(
+ '$site' => '(int|string) Site ID or domain',
+ '$post_ID' => '(int) The post ID',
+ ),
+
+ 'request_format' => array(
+ // explicitly document all input.
+ 'content' => '(HTML) The comment text.',
+ // @todo Should we open this up to unauthenticated requests too?
+ // 'author' => '(author object) The author of the comment.',
+ ),
+
+ 'pass_wpcom_user_details' => true,
+
+ 'allow_fallback_to_jetpack_blog_token' => true,
+
+ 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/posts/843/replies/new/',
+ 'example_request_data' => array(
+ 'headers' => array(
+ 'authorization' => 'Bearer YOUR_API_TOKEN',
+ ),
+ 'body' => array(
+ 'content' => 'Your reply is very interesting. This is a reply.',
+ ),
),
- 'body' => array(
- 'content' => 'Your reply is very interesting. This is a reply.'
- )
)
-) );
-
-new WPCOM_JSON_API_Update_Comment_Endpoint( array(
- 'description' => 'Create a comment as a reply to another comment.',
- 'group' => 'comments',
- 'stat' => 'comments:1:replies:new',
-
- 'method' => 'POST',
- 'path' => '/sites/%s/comments/%d/replies/new',
- 'path_labels' => array(
- '$site' => '(int|string) Site ID or domain',
- '$comment_ID' => '(int) The comment ID'
- ),
-
- 'request_format' => array(
- 'content' => '(HTML) The comment text.',
-// @todo Should we open this up to unauthenticated requests too?
-// 'author' => '(author object) The author of the comment.',
- ),
-
- 'pass_wpcom_user_details' => true,
-
- 'allow_fallback_to_jetpack_blog_token' => true,
-
- 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/comments/29/replies/new',
- 'example_request_data' => array(
- 'headers' => array(
- 'authorization' => 'Bearer YOUR_API_TOKEN'
+);
+
+new WPCOM_JSON_API_Update_Comment_Endpoint(
+ array(
+ 'description' => 'Create a comment as a reply to another comment.',
+ 'group' => 'comments',
+ 'stat' => 'comments:1:replies:new',
+
+ 'method' => 'POST',
+ 'path' => '/sites/%s/comments/%d/replies/new',
+ 'path_labels' => array(
+ '$site' => '(int|string) Site ID or domain',
+ '$comment_ID' => '(int) The comment ID',
+ ),
+
+ 'request_format' => array(
+ 'content' => '(HTML) The comment text.',
+ // @todo Should we open this up to unauthenticated requests too?
+ // 'author' => '(author object) The author of the comment.',
+ ),
+
+ 'pass_wpcom_user_details' => true,
+
+ 'allow_fallback_to_jetpack_blog_token' => true,
+
+ 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/comments/29/replies/new',
+ 'example_request_data' => array(
+ 'headers' => array(
+ 'authorization' => 'Bearer YOUR_API_TOKEN',
+ ),
+ 'body' => array(
+ 'content' => 'This reply is very interesting. This is editing a comment reply via the API.',
+ ),
),
- 'body' => array(
- 'content' => 'This reply is very interesting. This is editing a comment reply via the API.',
- )
)
-) );
-
-new WPCOM_JSON_API_Update_Comment_Endpoint( array(
- 'description' => 'Edit a comment.',
- 'group' => 'comments',
- 'stat' => 'comments:1:POST',
-
- 'method' => 'POST',
- 'path' => '/sites/%s/comments/%d',
- 'path_labels' => array(
- '$site' => '(int|string) Site ID or domain',
- '$comment_ID' => '(int) The comment ID'
- ),
-
- 'request_format' => array(
- 'author' => "(string) The comment author's name.",
- 'author_email' => "(string) The comment author's email.",
- 'author_url' => "(string) The comment author's URL.",
- 'content' => '(HTML) The comment text.',
- 'date' => "(ISO 8601 datetime) The comment's creation time.",
- 'status' => array(
- 'approved' => 'Approve the comment.',
- 'unapproved' => 'Remove the comment from public view and send it to the moderation queue.',
- 'spam' => 'Mark the comment as spam.',
- 'unspam' => 'Unmark the comment as spam. Will attempt to set it to the previous status.',
- 'trash' => 'Send a comment to the trash if trashing is enabled (see constant: EMPTY_TRASH_DAYS).',
- 'untrash' => 'Untrash a comment. Only works when the comment is in the trash.',
+);
+
+new WPCOM_JSON_API_Update_Comment_Endpoint(
+ array(
+ 'description' => 'Edit a comment.',
+ 'group' => 'comments',
+ 'stat' => 'comments:1:POST',
+
+ 'method' => 'POST',
+ 'path' => '/sites/%s/comments/%d',
+ 'path_labels' => array(
+ '$site' => '(int|string) Site ID or domain',
+ '$comment_ID' => '(int) The comment ID',
+ ),
+
+ 'request_format' => array(
+ 'author' => "(string) The comment author's name.",
+ 'author_email' => "(string) The comment author's email.",
+ 'author_url' => "(string) The comment author's URL.",
+ 'content' => '(HTML) The comment text.',
+ 'date' => "(ISO 8601 datetime) The comment's creation time.",
+ 'status' => array(
+ 'approved' => 'Approve the comment.',
+ 'unapproved' => 'Remove the comment from public view and send it to the moderation queue.',
+ 'spam' => 'Mark the comment as spam.',
+ 'unspam' => 'Unmark the comment as spam. Will attempt to set it to the previous status.',
+ 'trash' => 'Send a comment to the trash if trashing is enabled (see constant: EMPTY_TRASH_DAYS).',
+ 'untrash' => 'Untrash a comment. Only works when the comment is in the trash.',
+ ),
),
- ),
- 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/comments/29',
- 'example_request_data' => array(
- 'headers' => array(
- 'authorization' => 'Bearer YOUR_API_TOKEN'
+ 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/comments/29',
+ 'example_request_data' => array(
+ 'headers' => array(
+ 'authorization' => 'Bearer YOUR_API_TOKEN',
+ ),
+ 'body' => array(
+ 'content' => 'This reply is now edited via the API.',
+ 'status' => 'approved',
+ ),
),
- 'body' => array(
- 'content' => 'This reply is now edited via the API.',
- 'status' => 'approved',
- )
)
-) );
-
-new WPCOM_JSON_API_Update_Comment_Endpoint( array(
- 'description' => 'Delete a comment.',
- 'group' => 'comments',
- 'stat' => 'comments:1:delete',
-
- 'method' => 'POST',
- 'path' => '/sites/%s/comments/%d/delete',
- 'path_labels' => array(
- '$site' => '(int|string) Site ID or domain',
- '$comment_ID' => '(int) The comment ID'
- ),
-
- 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/comments/$comment_ID/delete',
- 'example_request_data' => array(
- 'headers' => array(
- 'authorization' => 'Bearer YOUR_API_TOKEN'
- )
+);
+
+new WPCOM_JSON_API_Update_Comment_Endpoint(
+ array(
+ 'description' => 'Delete a comment.',
+ 'group' => 'comments',
+ 'stat' => 'comments:1:delete',
+
+ 'method' => 'POST',
+ 'path' => '/sites/%s/comments/%d/delete',
+ 'path_labels' => array(
+ '$site' => '(int|string) Site ID or domain',
+ '$comment_ID' => '(int) The comment ID',
+ ),
+
+ 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/comments/$comment_ID/delete',
+ 'example_request_data' => array(
+ 'headers' => array(
+ 'authorization' => 'Bearer YOUR_API_TOKEN',
+ ),
+ ),
)
-) );
+);
+/**
+ * Update comments endpoint class.
+ */
class WPCOM_JSON_API_Update_Comment_Endpoint extends WPCOM_JSON_API_Comment_Endpoint {
- function __construct( $args ) {
+ /**
+ * WPCOM_JSON_API_Update_Comment_Endpoint constructor.
+ *
+ * @param array $args - Args.
+ */
+ public function __construct( $args ) {
parent::__construct( $args );
if ( $this->api->ends_with( $this->path, '/delete' ) ) {
$this->comment_object_format['status']['deleted'] = 'The comment has been deleted permanently.';
}
}
- // /sites/%s/posts/%d/replies/new -> $blog_id, $post_id
- // /sites/%s/comments/%d/replies/new -> $blog_id, $comment_id
- // /sites/%s/comments/%d -> $blog_id, $comment_id
- // /sites/%s/comments/%d/delete -> $blog_id, $comment_id
- function callback( $path = '', $blog_id = 0, $object_id = 0 ) {
- if ( $this->api->ends_with( $path, '/new' ) )
+ /**
+ * Update comment API callback.
+ *
+ * /sites/%s/posts/%d/replies/new -> $blog_id, $post_id
+ * /sites/%s/comments/%d/replies/new -> $blog_id, $comment_id
+ * /sites/%s/comments/%d -> $blog_id, $comment_id
+ * /sites/%s/comments/%d/delete -> $blog_id, $comment_id
+ *
+ * @param string $path API path.
+ * @param int $blog_id The blog ID.
+ * @param int $object_id The object ID.
+ *
+ * @return bool|WP_Error|array
+ */
+ public function callback( $path = '', $blog_id = 0, $object_id = 0 ) {
+ if ( $this->api->ends_with( $path, '/new' ) ) {
$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ), false );
- else
+ } else {
$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
+ }
if ( is_wp_error( $blog_id ) ) {
return $blog_id;
}
@@ -161,12 +197,23 @@ class WPCOM_JSON_API_Update_Comment_Endpoint extends WPCOM_JSON_API_Comment_Endp
return $this->update_comment( $path, $blog_id, $object_id );
}
- // /sites/%s/posts/%d/replies/new -> $blog_id, $post_id
- // /sites/%s/comments/%d/replies/new -> $blog_id, $comment_id
- function new_comment( $path, $blog_id, $post_id, $comment_parent_id ) {
- if ( !$post_id ) {
+ /**
+ * Add a new comment to a post or as a reply to another comment.
+ *
+ * /sites/%s/posts/%d/replies/new -> $blog_id, $post_id
+ * /sites/%s/comments/%d/replies/new -> $blog_id, $comment_id
+ *
+ * @param string $path API path.
+ * @param int $blog_id The blog ID.
+ * @param int $post_id The post ID.
+ * @param int $comment_parent_id The comment parent ID.
+ *
+ * @return bool|WP_Error|array
+ */
+ public function new_comment( $path, $blog_id, $post_id, $comment_parent_id ) {
+ if ( ! $post_id ) {
$comment_parent = get_comment( $comment_parent_id );
- if ( !$comment_parent_id || !$comment_parent || is_wp_error( $comment_parent ) ) {
+ if ( ! $comment_parent_id || ! $comment_parent || is_wp_error( $comment_parent ) ) {
return new WP_Error( 'unknown_comment', 'Unknown comment', 404 );
}
@@ -174,12 +221,12 @@ class WPCOM_JSON_API_Update_Comment_Endpoint extends WPCOM_JSON_API_Comment_Endp
}
$post = get_post( $post_id );
- if ( !$post || is_wp_error( $post ) ) {
+ if ( ! $post || is_wp_error( $post ) ) {
return new WP_Error( 'unknown_post', 'Unknown post', 404 );
}
if (
- -1 == get_option( 'blog_public' ) &&
+ -1 == get_option( 'blog_public' ) && // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
/**
* Filter allowing non-registered users on the site to comment.
*
@@ -200,34 +247,34 @@ class WPCOM_JSON_API_Update_Comment_Endpoint extends WPCOM_JSON_API_Comment_Endp
}
$can_view = $this->user_can_view_post( $post->ID );
- if ( !$can_view || is_wp_error( $can_view ) ) {
+ if ( ! $can_view || is_wp_error( $can_view ) ) {
return $can_view;
}
$post_status = get_post_status_object( get_post_status( $post ) );
- if ( !$post_status->public && !$post_status->private ) {
+ if ( ! $post_status->public && ! $post_status->private ) {
return new WP_Error( 'unauthorized', 'Comments on drafts are not allowed', 403 );
}
$args = $this->query_args();
$input = $this->input();
- if ( !is_array( $input ) || !$input || !strlen( $input['content'] ) ) {
+ if ( ! is_array( $input ) || ! $input || ! strlen( $input['content'] ) ) {
return new WP_Error( 'invalid_input', 'Invalid request input', 400 );
}
$user = wp_get_current_user();
- if ( !$user || is_wp_error( $user ) || !$user->ID ) {
+ if ( ! $user || is_wp_error( $user ) || ! $user->ID ) {
$auth_required = false;
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
$auth_required = true;
} elseif ( isset( $this->api->token_details['user'] ) ) {
$user = (object) $this->api->token_details['user'];
foreach ( array( 'display_name', 'user_email', 'user_url' ) as $user_datum ) {
- if ( !isset( $user->$user_datum ) ) {
+ if ( ! isset( $user->$user_datum ) ) {
$auth_required = true;
}
}
- if ( !isset( $user->ID ) ) {
+ if ( ! isset( $user->ID ) ) {
$user->ID = 0;
}
@@ -261,7 +308,7 @@ class WPCOM_JSON_API_Update_Comment_Endpoint extends WPCOM_JSON_API_Comment_Endp
);
if ( $comment_parent_id ) {
- if ( $comment_parent->comment_approved === '0' && current_user_can( 'edit_comment', $comment_parent->comment_ID ) ) {
+ if ( '0' === $comment_parent->comment_approved && current_user_can( 'edit_comment', $comment_parent->comment_ID ) ) {
wp_set_comment_status( $comment_parent->comment_ID, 'approve' );
}
}
@@ -271,7 +318,7 @@ class WPCOM_JSON_API_Update_Comment_Endpoint extends WPCOM_JSON_API_Comment_Endp
$this->api->trap_wp_die( null );
$return = $this->get_comment( $comment_id, $args['context'] );
- if ( !$return ) {
+ if ( ! $return ) {
return new WP_Error( 400, __( 'Comment cache problem?', 'jetpack' ) );
}
if ( is_wp_error( $return ) ) {
@@ -283,61 +330,71 @@ class WPCOM_JSON_API_Update_Comment_Endpoint extends WPCOM_JSON_API_Comment_Endp
return $return;
}
- // /sites/%s/comments/%d -> $blog_id, $comment_id
- function update_comment( $path, $blog_id, $comment_id ) {
+ /**
+ * Update a comment.
+ *
+ * /sites/%s/comments/%d -> $blog_id, $comment_id
+ *
+ * @param string $path API path.
+ * @param int $blog_id Blog ID.
+ * @param int $comment_id Comment ID.
+ *
+ * @return bool|WP_Error|array
+ */
+ public function update_comment( $path, $blog_id, $comment_id ) {
$comment = get_comment( $comment_id );
- if ( !$comment || is_wp_error( $comment ) ) {
+ if ( ! $comment || is_wp_error( $comment ) ) {
return new WP_Error( 'unknown_comment', 'Unknown comment', 404 );
}
- if ( !current_user_can( 'edit_comment', $comment->comment_ID ) ) {
+ if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) {
return new WP_Error( 'unauthorized', 'User cannot edit comment', 403 );
}
$args = $this->query_args();
$input = $this->input( false );
- if ( !is_array( $input ) || !$input ) {
+ if ( ! is_array( $input ) || ! $input ) {
return new WP_Error( 'invalid_input', 'Invalid request input', 400 );
}
$update = array();
foreach ( $input as $key => $value ) {
- $update["comment_$key"] = $value;
+ $update[ "comment_$key" ] = $value;
}
$comment_status = wp_get_comment_status( $comment->comment_ID );
if ( isset( $update['comment_status'] ) ) {
switch ( $update['comment_status'] ) {
- case 'approved' :
+ case 'approved':
if ( 'approve' !== $comment_status ) {
wp_set_comment_status( $comment->comment_ID, 'approve' );
}
break;
- case 'unapproved' :
+ case 'unapproved':
if ( 'hold' !== $comment_status ) {
wp_set_comment_status( $comment->comment_ID, 'hold' );
}
break;
- case 'spam' :
+ case 'spam':
if ( 'spam' !== $comment_status ) {
wp_spam_comment( $comment->comment_ID );
}
break;
- case 'unspam' :
+ case 'unspam':
if ( 'spam' === $comment_status ) {
wp_unspam_comment( $comment->comment_ID );
}
break;
- case 'trash' :
+ case 'trash':
if ( ! EMPTY_TRASH_DAYS ) {
return new WP_Error( 'trash_disabled', 'Cannot trash comment', 403 );
}
if ( 'trash' !== $comment_status ) {
- wp_trash_comment( $comment_id );
- }
- break;
- case 'untrash' :
+ wp_trash_comment( $comment_id );
+ }
+ break;
+ case 'untrash':
if ( 'trash' === $comment_status ) {
wp_untrash_comment( $comment->comment_ID );
}
@@ -355,7 +412,7 @@ class WPCOM_JSON_API_Update_Comment_Endpoint extends WPCOM_JSON_API_Comment_Endp
}
$return = $this->get_comment( $comment->comment_ID, $args['context'] );
- if ( !$return || is_wp_error( $return ) ) {
+ if ( ! $return || is_wp_error( $return ) ) {
return $return;
}
@@ -364,20 +421,30 @@ class WPCOM_JSON_API_Update_Comment_Endpoint extends WPCOM_JSON_API_Comment_Endp
return $return;
}
- // /sites/%s/comments/%d/delete -> $blog_id, $comment_id
- function delete_comment( $path, $blog_id, $comment_id ) {
+ /**
+ * Delete a comment.
+ *
+ * /sites/%s/comments/%d/delete -> $blog_id, $comment_id
+ *
+ * @param string $path API path.
+ * @param int $blog_id Blog ID.
+ * @param int $comment_id Comment ID.
+ *
+ * @return bool|WP_Error|array
+ */
+ public function delete_comment( $path, $blog_id, $comment_id ) {
$comment = get_comment( $comment_id );
- if ( !$comment || is_wp_error( $comment ) ) {
+ if ( ! $comment || is_wp_error( $comment ) ) {
return new WP_Error( 'unknown_comment', 'Unknown comment', 404 );
}
- if ( !current_user_can( 'edit_comment', $comment->comment_ID ) ) { // [sic] There is no delete_comment cap
+ if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) { // [sic] There is no delete_comment cap
return new WP_Error( 'unauthorized', 'User cannot delete comment', 403 );
}
- $args = $this->query_args();
+ $args = $this->query_args();
$return = $this->get_comment( $comment->comment_ID, $args['context'] );
- if ( !$return || is_wp_error( $return ) ) {
+ if ( ! $return || is_wp_error( $return ) ) {
return $return;
}