Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] );
}
Expand Down
10 changes: 10 additions & 0 deletions includes/rest/class-endpoint-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions tests/phpunit/tests/class-test-endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down