Make WordPress Core

Changeset 39922 for trunk


Ignore:
Timestamp:
01/17/2017 05:17:15 AM (10 years ago)
Author:
rmccue
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

    r39671 r39922  
    553553                 *
    554554                 * Allows modification of the comment right before it is inserted via wp_insert_comment().
     555                 * Returning a WP_Error value from the filter will shortcircuit insertion and allow
     556                 * skipping further processing.
    555557                 *
    556558                 * @since 4.7.0
    557                  *
    558                  * @param array           $prepared_comment The prepared comment data for wp_insert_comment().
     559                 * @since 4.8.0 $prepared_comment can now be a WP_Error to shortcircuit insertion.
     560                 *
     561                 * @param array|WP_Error  $prepared_comment The prepared comment data for wp_insert_comment().
    559562                 * @param WP_REST_Request $request          Request used to insert the comment.
    560563                 */
    561564                $prepared_comment = apply_filters( 'rest_pre_insert_comment', $prepared_comment, $request );
     565                if ( is_wp_error( $prepared_comment ) ) {
     566                        return $prepared_comment;
     567                }
    562568
    563569                $comment_id = wp_insert_comment( wp_filter_comment( wp_slash( (array) $prepared_comment ) ) );
  • trunk/tests/phpunit/tests/rest-api/rest-comments-controller.php

    r39913 r39922  
    984984                $new_comment = get_comment( $data['id'] );
    985985                $this->assertEquals( $params['content']['raw'], $new_comment->comment_content );
     986        }
     987
     988        public function test_create_item_error_from_filter() {
     989                add_filter( 'rest_pre_insert_comment', array( $this, 'return_premade_error' ) );
     990                wp_set_current_user( self::$admin_id );
     991
     992                $params = array(
     993                        'post'         => self::$post_id,
     994                        'author_name'  => 'Homer Jay Simpson',
     995                        'author_email' => 'homer@example.org',
     996                        'content'      => array(
     997                                'raw' => 'Aw, he loves beer. Here, little fella.'
     998                        ),
     999                );
     1000
     1001                $request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
     1002                $request->add_header( 'content-type', 'application/json' );
     1003                $request->set_body( wp_json_encode( $params ) );
     1004
     1005                $response = $this->server->dispatch( $request );
     1006
     1007                $this->assertErrorResponse( 'test_rest_premade_error', $response, 418 );
     1008        }
     1009
     1010        public function return_premade_error() {
     1011                return new WP_Error( 'test_rest_premade_error', "I'm sorry, I thought he was a party robot.", array( 'status' => 418 ) );
    9861012        }
    9871013
Note: See TracChangeset for help on using the changeset viewer.