WordPress.org

Make WordPress Core

Opened 2 years ago

Last modified 4 months ago

#41775 new enhancement

REST API: Add filter to allow creation of custom comment type

Reported by: langan Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: REST API Keywords: close
Focuses: Cc:
PR Number:

Description

When trying to create a comment using the REST API that has a different comment type I get the following error.

Cannot create a comment with that type.

Looks like it is coming from this code.

// Do not allow comments to be created with a non-default type.
if ( ! empty( $request['type'] ) && 'comment' !== $request['type'] ) {
        return new WP_Error( 'rest_invalid_comment_type', __( 'Cannot create a comment with that type.' ), array( 'status' => 400 ) );
}

Looks like this was added to fix defect #38820

Would it be possible to change this check to use a filter that will allow some comment types? Something like this

if ( ! empty( $request['type'] ) && false === in_array($request['type'], apply_filters( 'rest_allow_comment_types', array('comment'), $request ) ) {
        return new WP_Error( 'rest_invalid_comment_type', __( 'Cannot create a comment with that type.' ), array( 'status' => 400 ) );
}

We could then use the same filter in the update_item call as well I've attached a diff with a first draft idea

Attachments (1)

allow-comment-types.diff (3.1 KB) - added by langan 2 years ago.
Diff of changes to class-wp-rest-comments-controller.php

Download all attachments as: .zip

Change History (3)

@langan
2 years ago

Diff of changes to class-wp-rest-comments-controller.php

This ticket was mentioned in Slack in #core-restapi by timothybjacobs. View the logs.


4 months ago

#2 @TimothyBlynJacobs
4 months ago

  • Keywords close added
  • Milestone Awaiting Review deleted

Hi @langan,

I'm sorry for the delay in your ticket receiving a response.

At the moment, the recommended path forward from #38820 is to use a custom endpoint for custom comment types until #35214 lands and we can support custom comment types natively.

Adding the filter here may make it difficult to support custom comment types in the future properly, since custom comment types will likely have different schema and permission requirements.

Unfortunately, the REST API controllers are currently a bit difficult to extend/reuse which is something I'm hoping to look at soon. The way to keep track of that would be in the REST API meetings in Make Slack.

Note: See TracTickets for help on using tickets.