Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revisions: slash meta values for previews #5344

Closed
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ public function create_post_autosave( $post_data, array $meta = array() ) {
if ( ! empty( $meta ) ) {
foreach ( $revisioned_meta_keys as $meta_key ) {
if ( isset( $meta[ $meta_key ] ) ) {
update_metadata( 'post', $revision_id, $meta_key, $meta[ $meta_key ] );
update_metadata( 'post', $revision_id, $meta_key, wp_slash( $meta[ $meta_key ] ) );
}
}
}
Expand Down
70 changes: 70 additions & 0 deletions tests/phpunit/tests/rest-api/rest-autosaves-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,76 @@ public function test_update_item() {
$this->check_create_autosave_response( $response );
}

public function test_update_item_with_meta() {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . self::$post_id . '/autosaves' );
$request->add_header( 'Content-Type', 'application/x-www-form-urlencoded' );
register_post_meta(
'post',
'foo',
array(
'show_in_rest' => true,
'revisions_enabled' => true,
'single' => true,
)
);
$params = $this->set_post_data(
array(
'id' => self::$post_id,
'author' => self::$contributor_id,
'meta' => array(
'foo' => 'bar',
),
)
);

$request->set_body_params( $params );
$response = rest_get_server()->dispatch( $request );

$this->check_create_autosave_response( $response );

$data = $response->get_data();
$this->assertArrayHasKey( 'meta', $data );
$this->assertArrayHasKey( 'foo', $data['meta'] );
$this->assertSame( 'bar', $data['meta']['foo'] );
}

public function test_update_item_with_json_meta() {
$meta = '[{\"content\":\"foot 1\",\"id\":\"fa97a10d-7401-42b9-ac54-df8f4510749a\"},{\"content\":\"fdddddoot 2\\\"\",\"id\":\"2216d0aa-34b8-42b4-b441-84dedc0406e0\"}]';
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'POST', '/wp/v2/posts/' . self::$post_id . '/autosaves' );
$request->add_header( 'Content-Type', 'application/x-www-form-urlencoded' );
register_post_meta(
'post',
'foo',
array(
'show_in_rest' => true,
'revisions_enabled' => true,
'single' => true,
)
);
$params = $this->set_post_data(
array(
'id' => self::$post_id,
'author' => self::$contributor_id,
'meta' => array(
'foo' => $meta,
),
)
);

$request->set_body_params( $params );
$response = rest_get_server()->dispatch( $request );

$this->check_create_autosave_response( $response );

$data = $response->get_data();
$this->assertArrayHasKey( 'meta', $data );
$this->assertArrayHasKey( 'foo', $data['meta'] );
$values = json_decode( wp_unslash( $data['meta']['foo'] ), true );
$this->assertNotNull( $values );
}

public function test_update_item_nopriv() {
wp_set_current_user( self::$contributor_id );

Expand Down
5 changes: 2 additions & 3 deletions tests/phpunit/tests/rest-api/rest-post-meta-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -3364,7 +3364,7 @@ public function test_revisioned_multiple_post_meta_with_posts_endpoint() {
* Test post meta revisions with a custom post type and the page post type.
*
* @group revision
* @dataProvider test_revisioned_single_post_meta_with_posts_endpoint_page_and_cpt_data_provider
* @dataProvider data_revisioned_single_post_meta_with_posts_endpoint_page_and_cpt_data_provider
*/
public function test_revisioned_single_post_meta_with_posts_endpoint_page_and_cpt( $passed, $expected, $post_type ) {

Expand Down Expand Up @@ -3451,7 +3451,7 @@ public function test_revisioned_single_post_meta_with_posts_endpoint_page_and_cp
/**
* Provide data for the meta revision checks.
*/
public function test_revisioned_single_post_meta_with_posts_endpoint_page_and_cpt_data_provider() {
public function data_revisioned_single_post_meta_with_posts_endpoint_page_and_cpt_data_provider() {
return array(
array(
'Test string',
Expand All @@ -3468,7 +3468,6 @@ public function test_revisioned_single_post_meta_with_posts_endpoint_page_and_cp
false,
'cpt',
),

);
}
}