WordPress.org

Make WordPress Core

Opened 4 months ago

Closed 3 months ago

Last modified 3 months ago

#39578 closed enhancement (fixed)

Enhancement for rest api comment controller create_item method to check for WP error

Reported by: dspilka Owned by: rmccue
Milestone: 4.8 Priority: normal
Severity: normal Version: 4.7
Component: REST API Keywords: needs-unit-tests good-first-bug has-patch
Focuses: rest-api Cc:

Description

In the WP_REST_Posts_Controller class, the create_item method calls the prepare_item_for_database method and checks its response for a WP error object.

<?php
    $prepared_post = $this->prepare_item_for_database( $request );

     if ( is_wp_error( $prepared_post ) ) {
          return $prepared_post;
     }

This allows for further filtering and custom validation via the rest_pre_insert_{$this->post_type} filter and for returning a WP Error if needed to stop the request and return messaging.

I'd like to propose similar in the WP_REST_Comments_Controller class. Right now when the create_item method applys the rest_pre_insert_comment filter, it doesn't check for a WP Error response.

<?php
    $prepared_comment = apply_filters( 'rest_pre_insert_comment', $prepared_comment, $request );
    $comment_id = wp_insert_comment( $prepared_comment );

I'd like to propose checking for a WP Error.

<?php
        $prepared_comment = apply_filters( 'rest_pre_insert_comment', $prepared_comment, $request );

        if ( is_wp_error( $prepared_comment ) ) {
                return $prepared_comment;
        }

        $comment_id = wp_insert_comment( $prepared_comment );

Attachments (2)

39578.diff (736 bytes) - added by dspilka 3 months ago.
Patch to check for WP Error.
39578.2.diff (1.3 KB) - added by rmccue 3 months ago.
Add phpDoc

Download all attachments as: .zip

Change History (10)

#1 follow-up: @jnylen0
4 months ago

  • Keywords needs-patch needs-unit-tests good-first-bug added
  • Milestone changed from Awaiting Review to 4.7.2
  • Version changed from 4.7.1 to 4.7

#2 in reply to: ↑ 1 @dspilka
3 months ago

Replying to jnylen0:

Good idea, thanks. Want to write a patch? https://make.wordpress.org/core/handbook/contribute/#your-first-patch

Yes definitely, I am attaching a patch now. It's my first time, so hopefully I'm doing this correctly. Let me know.

Thanks,
Dan

Last edited 3 months ago by dspilka (previous) (diff)

@dspilka
3 months ago

Patch to check for WP Error.

#3 @dspilka
3 months ago

  • Keywords has-patch added; needs-patch removed

#4 follow-up: @rmccue
3 months ago

  • Owner set to rmccue
  • Status changed from new to reviewing

@dspilka Thanks for the patch :) Just to note: your indentation is off in the patch, but seeing how it's only 3 lines, not a huge issue here. Something to keep in mind for future patches though.

@rmccue
3 months ago

Add phpDoc

#5 @rmccue
3 months ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 39922:

REST API: Allow shortcircuiting rest_pre_insert_comment

rest_pre_insert_{post_type} allows returning a WP_Error from the filter to shortcircuit actually creating the object, so it makes sense to do so for comments too.

Props dspilka.
Fixes #39578.

#6 @rmccue
3 months ago

  • Milestone changed from 4.7.2 to 4.8

I pushed this forward to 4.8 instead, as I don't see a real reason to backport to a minor release. Thanks for the patch! :)

#7 in reply to: ↑ 4 @dspilka
3 months ago

Replying to rmccue:

@dspilka Thanks for the patch :) Just to note: your indentation is off in the patch, but seeing how it's only 3 lines, not a huge issue here. Something to keep in mind for future patches though.

*EDIT* Nevermind. I clearly see it now when opening/viewing the patches online! Didn't look that way in my IDE for some reason. But I got it now. Thanks.

Thanks @rmccue. Very happy to have my first WordPress patch! Would you mind pointing out how the indentation should be or how I can check it so I can get it right for my next patch? It seems to look like the others in my IDE, so I just want to make sure I understand it and know how to do it right. Thanks, Dan.

Last edited 3 months ago by dspilka (previous) (diff)

#8 @rmccue
3 months ago

It'll look fine in your editor since your tab size is set to 4 (i.e. 1 tab character is 4 spaces wide), whereas in Trac, the tab size is 8, so the four spaces don't line up with the 8-space-width of the tab :)

Note: See TracTickets for help on using tickets.