Skip to content

Commit

Permalink
Modified property is returned when created.
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd committed Jun 9, 2023
1 parent 1dd2805 commit be9285f
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,58 @@ public function update_item( $request ) {
return rest_ensure_response( $response );
}

/**
* Creates a single template.
*
* @since 5.8.0
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function create_item( $request ) {
$prepared_post = $this->prepare_item_for_database( $request );

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

$prepared_post->post_name = $request['slug'];
$post_id = wp_insert_post( wp_slash( (array) $prepared_post ), true );
if ( is_wp_error( $post_id ) ) {
if ( 'db_insert_error' === $post_id->get_error_code() ) {
$post_id->add_data( array( 'status' => 500 ) );
} else {
$post_id->add_data( array( 'status' => 400 ) );
}

return $post_id;
}
$posts = gutenberg_get_block_templates( array( 'wp_id' => $post_id ), $this->post_type );
if ( ! count( $posts ) ) {
return new WP_Error( 'rest_template_insert_error', __( 'No templates exist with that id.' ), array( 'status' => 400 ) );
}
$id = $posts[0]->id;
$post = get_post( $post_id );
$template = gutenberg_get_block_template( $id, $this->post_type );
$fields_update = $this->update_additional_fields_for_object( $template, $request );
if ( is_wp_error( $fields_update ) ) {
return $fields_update;
}

/** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
do_action( "rest_after_insert_{$this->post_type}", $post, $request, true );

wp_after_insert_post( $post, false, null );

$response = $this->prepare_item_for_response( $template, $request );
$response = rest_ensure_response( $response );

$response->set_status( 201 );
$response->header( 'Location', rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $template->id ) ) );

return $response;
}

/**
* Retrieves the block type' schema, conforming to JSON Schema.
*
Expand Down
47 changes: 44 additions & 3 deletions phpunit/class-gutenberg-rest-templates-controller-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,55 @@ public function test_update_item() {
}

/**
* @doesNotPerformAssertions
* @covers WP_REST_Templates_Controller::create_item
*/
public function test_context_param() {}
public function test_create_item() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'POST', '/wp/v2/templates' );
$request->set_body_params(
array(
'slug' => 'my_custom_template',
'description' => 'Just a description',
'title' => 'My Template',
'content' => 'Content',
'author' => self::$admin_id,
)
);
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
unset( $data['_links'] );
unset( $data['wp_id'] );

$this->assertSame(
array(
'id' => 'emptytheme//my_custom_template',
'theme' => 'emptytheme',
'content' => array(
'raw' => 'Content',
),
'slug' => 'my_custom_template',
'source' => 'custom',
'origin' => null,
'type' => 'wp_template',
'description' => 'Just a description',
'title' => array(
'raw' => 'My Template',
'rendered' => 'My Template',
),
'status' => 'publish',
'has_theme_file' => false,
'is_custom' => true,
'author' => self::$admin_id,
'modified' => $data['modified'],
),
$data
);
}

/**
* @doesNotPerformAssertions
*/
public function test_create_item() {}
public function test_context_param() {}

/**
* @doesNotPerformAssertions
Expand Down

0 comments on commit be9285f

Please sign in to comment.