Customize: Prevent edge case fatal error when attempting to save changes to a changeset that had previously been corrupted.

Check return value of `WP_Customize_Manager::get_changeset_post_data()` and return if error instead of assuming it is an array.

Amends [38810].
See #30937.
Fixes #41252.


git-svn-id: https://develop.svn.wordpress.org/trunk@41012 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Weston Ruter 2017-07-06 07:04:11 +00:00
parent 8b5c4ae745
commit f67018c059
2 changed files with 43 additions and 0 deletions

View File

@ -2263,6 +2263,9 @@ final class WP_Customize_Manager {
}
$existing_changeset_data = $this->get_changeset_post_data( $changeset_post_id );
if ( is_wp_error( $existing_changeset_data ) ) {
return $existing_changeset_data;
}
}
// Fail if attempting to publish but publish hook is missing.

View File

@ -1291,6 +1291,46 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase {
$this->assertEquals( 'Unfiltered<script>evil</script>', get_option( 'scratchpad' ) );
}
/**
* Ensure that save_changeset_post method bails updating an underlying changeset which is invalid.
*
* @ticket 41252
* @covers WP_Customize_Manager::save_changeset_post
* @covers WP_Customize_Manager::get_changeset_post_data
*/
function test_save_changeset_post_for_bad_changeset() {
$uuid = wp_generate_uuid4();
$post_id = wp_insert_post( array(
'post_type' => 'customize_changeset',
'post_content' => 'INVALID_JSON',
'post_name' => $uuid,
'post_status' => 'auto-draft',
'post_date' => gmdate( 'Y-m-d H:i:s', strtotime( '-3 days' ) ),
) );
$manager = $this->create_test_manager( $uuid );
$args = array(
'data' => array(
'blogname' => array(
'value' => 'Test',
),
),
);
$r = $manager->save_changeset_post( $args );
$this->assertInstanceOf( 'WP_Error', $r );
if ( function_exists( 'json_last_error' ) ) {
$this->assertEquals( 'json_parse_error', $r->get_error_code() );
}
wp_update_post( array(
'ID' => $post_id,
'post_content' => 'null',
) );
$r = $manager->save_changeset_post( $args );
$this->assertInstanceOf( 'WP_Error', $r );
$this->assertEquals( 'expected_array', $r->get_error_code() );
}
/**
* Register scratchpad setting.
*