mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
Revisions: slash meta values for autosave (preview) revisions.
Correct an issue where meta values containing characters like quote `”` could not be previewed on published posts. The function `update_metadata` expects data to be slashed. Also, add a test to confirm that storing JSON data which requires slashing in autosave meta works as expected, and improve naming for a data provider added in [56714]. Follow up to [56714]. Props mukesh27, spacedmonkey. Fixes #20564. git-svn-id: https://develop.svn.wordpress.org/trunk@56745 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
345fb48865
commit
1f914ecb2f
@ -421,7 +421,7 @@ class WP_REST_Autosaves_Controller extends WP_REST_Revisions_Controller {
|
||||
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 ] ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -341,6 +341,76 @@ class WP_Test_REST_Autosaves_Controller extends WP_Test_REST_Post_Type_Controlle
|
||||
$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 );
|
||||
|
||||
|
||||
@ -3364,7 +3364,7 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase {
|
||||
* 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 ) {
|
||||
|
||||
@ -3451,7 +3451,7 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase {
|
||||
/**
|
||||
* 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',
|
||||
@ -3468,7 +3468,6 @@ class WP_Test_REST_Post_Meta_Fields extends WP_Test_REST_TestCase {
|
||||
false,
|
||||
'cpt',
|
||||
),
|
||||
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user