#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)
Change History (10)
#1
follow-up:
↓ 2
@
8 years 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
@
8 years 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 will get a patch together!
#4
follow-up:
↓ 7
@
8 years 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.
#6
@
8 years 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
@
8 years 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.
Good idea, thanks. Want to write a patch? https://make.wordpress.org/core/handbook/contribute/#your-first-patch