If a saving a post fails, remove any invalid characters (such as emoji) from the primary text fields, then try to save it again.

See #21212.



git-svn-id: https://develop.svn.wordpress.org/trunk@30346 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast 2014-11-14 21:33:50 +00:00
parent 6df14c1612
commit e1ca159011
2 changed files with 51 additions and 1 deletions

View File

@ -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 );

View File

@ -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 );
}
}
}