[wp-trac] [WordPress Trac] #38821: REST API: Consider removing `karma` from Comment endpoint

WordPress Trac noreply at wordpress.org
Wed Nov 16 22:35:27 UTC 2016


#38821: REST API: Consider removing `karma` from Comment endpoint
-------------------------------------------------+-------------------------
 Reporter:  dd32                                 |       Owner:
     Type:  defect (bug)                         |      Status:  new
 Priority:  normal                               |   Milestone:  4.7
Component:  Comments                             |     Version:
 Severity:  normal                               |  Resolution:
 Keywords:  2nd-opinion dev-feedback needs-      |     Focuses:  rest-api
  unit-tests                                     |
-------------------------------------------------+-------------------------
Changes (by jnylen0):

 * keywords:  2nd-opinion dev-feedback => 2nd-opinion dev-feedback needs-
     unit-tests


Comment:

 It makes sense to me to remove this field.

 [attachment:38821.diff] needs test suite fixes and no longer applies
 cleanly.

 > If the field is removed, would adding it back be as easy as using
 `register_rest_field()`?

 Yes, here is the code needed to do that:

 {{{#!php
 add_action( 'rest_api_init', function() {
     register_rest_field( 'comment', 'karma', array(
         'get_callback' => function( $comment_arr ) {
             $comment_obj = get_comment( $comment_arr['id'] );
             return (int) $comment_obj->comment_karma;
         },
         'update_callback' => function( $karma, $comment_obj ) {
             $ret = wp_update_comment( array(
                 'comment_ID'    => $comment_obj->comment_ID,
                 'comment_karma' => $karma
             ) );
             if ( false === $ret ) {
                 return new WP_Error( 'rest_comment_karma_failed', __(
 'Failed to update comment karma.' ), array( 'status' => 500 ) );
             }
             return true;
         },
         'schema' => array(
             'description' => __( 'Comment karma.' ),
             'type'        => 'integer'
         ),
     ) );
 } );
 }}}

 Caveats:

 - permissions checks '''not''' included
 - `false === $ret` may not catch all `wp_update_comment` error cases; see
 also #38700

--
Ticket URL: <https://core.trac.wordpress.org/ticket/38821#comment:4>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list