diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index 142752dbc8..ec96749f46 100644 --- a/src/wp-admin/includes/post.php +++ b/src/wp-admin/includes/post.php @@ -177,6 +177,7 @@ function _wp_translate_postdata( $update = false, $post_data = null ) { * @return int Post ID. */ function edit_post( $post_data = null ) { + global $wpdb; if ( empty($post_data) ) $post_data = &$_POST; @@ -317,7 +318,19 @@ function edit_post( $post_data = null ) { update_post_meta( $post_ID, '_edit_last', get_current_user_id() ); - wp_update_post( $post_data ); + $success = wp_update_post( $post_data ); + // If the save failed, see if we can sanity check the main fields and try again + if ( ! $success && is_callable( array( $wpdb, 'strip_invalid_text_for_column' ) ) ) { + $fields = array( 'post_title', 'post_content', 'post_excerpt' ); + + foreach( $fields as $field ) { + if ( isset( $post_data[ $field ] ) ) { + $post_data[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->posts, $field, $post_data[ $field ] ); + } + } + + wp_update_post( $post_data ); + } // Now that we have an ID we can fix any attachment anchor hrefs _fix_attachment_links( $post_ID ); diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php index a4c68223bf..94f6817463 100644 --- a/tests/phpunit/tests/post.php +++ b/tests/phpunit/tests/post.php @@ -1016,4 +1016,41 @@ class Tests_Post extends WP_UnitTestCase { _unregister_post_type( 'post-type-1' ); _unregister_post_type( 'post-type-2' ); } + + /** + * @ticket 21212 + */ + function test_utf8mb3_post_saves_with_emoji() { + global $wpdb; + $_wpdb = new wpdb_exposed_methods_for_testing(); + + if ( 'utf8' !== $_wpdb->get_col_charset( $wpdb->posts, 'post_title' ) ) { + $this->markTestSkipped( 'This test is only useful with the utf8 character set' ); + } + + require_once( ABSPATH . '/wp-admin/includes/post.php' ); + + $post_id = $this->factory->post->create(); + + $data = array( + 'post_ID' => $post_id, + 'post_title' => "foo\xf0\x9f\x98\x88bar", + 'post_content' => "foo\xf0\x9f\x98\x8ebaz", + 'post_excerpt' => "foo\xf0\x9f\x98\x90bat" + ); + + $expected = array( + 'post_title' => "foobar", + 'post_content' => "foobaz", + 'post_excerpt' => "foobat" + ); + + edit_post( $data ); + + $post = get_post( $post_id ); + + foreach( $expected as $field => $value ) { + $this->assertEquals( $post->$field, $value ); + } + } }