diff --git a/includes/functions.php b/includes/functions.php index 2dc7c2d..c3543a1 100755 --- a/includes/functions.php +++ b/includes/functions.php @@ -299,7 +299,7 @@ function is_micropub_error( $obj ) { function micropub_wp_error( $error ) { if ( is_wp_error( $error ) ) { $data = $error->get_error_data(); - $status = isset( $data['status'] ) ? $data['status'] : 200; + $status = isset( $data['status'] ) ? $data['status'] : 400; if ( is_array( $data ) ) { unset( $data['status'] ); } diff --git a/includes/rest/class-endpoint-controller.php b/includes/rest/class-endpoint-controller.php index e5ded30..2220486 100644 --- a/includes/rest/class-endpoint-controller.php +++ b/includes/rest/class-endpoint-controller.php @@ -522,6 +522,11 @@ protected function handle_create( $user_id ) { } $this->insert_post( $args ); + + if ( \is_micropub_error( $args['ID'] ) ) { + return $args['ID']; + } + $this->default_file_handler( $args['ID'] ); return $args; @@ -640,6 +645,11 @@ protected function handle_update( $input ) { } $this->update_post( $args ); + + if ( \is_micropub_error( $args['ID'] ) ) { + return $args['ID']; + } + $this->default_file_handler( $post_id ); return $args; diff --git a/tests/phpunit/tests/class-test-endpoint.php b/tests/phpunit/tests/class-test-endpoint.php index 50a1378..9fd8ee1 100644 --- a/tests/phpunit/tests/class-test-endpoint.php +++ b/tests/phpunit/tests/class-test-endpoint.php @@ -231,6 +231,25 @@ public function test_create_post_subscriber_id() { self::check( $response, 403, 'insufficient_scope' ); } + public function test_create_returns_error_when_wp_insert_post_fails() { + // Force wp_insert_post to return a WP_Error with the empty-content + // code, mimicking the situation reported in issue #319. + add_filter( 'wp_insert_post_empty_content', '__return_true' ); + + $input = array( + 'h' => 'entry', + 'repost-of' => 'https://example.com/post', + 'post-status' => 'draft', + ); + $response = $this->dispatch( self::create_form_request( $input ), static::$author_id ); + + remove_filter( 'wp_insert_post_empty_content', '__return_true' ); + + // The response must surface the error (not silently return 201 with the + // error nested inside the body). + self::check( $response, 400, 'empty_content' ); + } + public function test_form_to_json_encode() { $controller = new \Micropub\Rest\Endpoint_Controller(); $output = $controller->form_to_json( static::$post );