Make WordPress Core

Changeset 30346


Ignore:
Timestamp:
11/14/2014 09:33:50 PM (12 years ago)
Author:
pento
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/post.php

    r30202 r30346  
    178178 */
    179179function edit_post( $post_data = null ) {
     180        global $wpdb;
    180181
    181182        if ( empty($post_data) )
     
    318319        update_post_meta( $post_ID, '_edit_last', get_current_user_id() );
    319320
    320         wp_update_post( $post_data );
     321        $success = wp_update_post( $post_data );
     322        // If the save failed, see if we can sanity check the main fields and try again
     323        if ( ! $success && is_callable( array( $wpdb, 'strip_invalid_text_for_column' ) ) ) {
     324                $fields = array( 'post_title', 'post_content', 'post_excerpt' );
     325
     326                foreach( $fields as $field ) {
     327                        if ( isset( $post_data[ $field ] ) ) {
     328                                $post_data[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->posts, $field, $post_data[ $field ] );
     329                        }
     330                }
     331
     332                wp_update_post( $post_data );
     333        }
    321334
    322335        // Now that we have an ID we can fix any attachment anchor hrefs
  • trunk/tests/phpunit/tests/post.php

    r30158 r30346  
    10171017                _unregister_post_type( 'post-type-2' );
    10181018        }
     1019
     1020        /**
     1021         * @ticket 21212
     1022         */
     1023        function test_utf8mb3_post_saves_with_emoji() {
     1024                global $wpdb;
     1025                $_wpdb = new wpdb_exposed_methods_for_testing();
     1026
     1027                if ( 'utf8' !== $_wpdb->get_col_charset( $wpdb->posts, 'post_title' ) ) {
     1028                        $this->markTestSkipped( 'This test is only useful with the utf8 character set' );
     1029                }
     1030
     1031                require_once( ABSPATH . '/wp-admin/includes/post.php' );
     1032
     1033                $post_id = $this->factory->post->create();
     1034
     1035                $data = array(
     1036                        'post_ID'      => $post_id,
     1037                        'post_title'   => "foo\xf0\x9f\x98\x88bar",
     1038                        'post_content' => "foo\xf0\x9f\x98\x8ebaz",
     1039                        'post_excerpt' => "foo\xf0\x9f\x98\x90bat"
     1040                );
     1041
     1042                $expected = array(
     1043                        'post_title'   => "foobar",
     1044                        'post_content' => "foobaz",
     1045                        'post_excerpt' => "foobat"
     1046                );
     1047
     1048                edit_post( $data );
     1049
     1050                $post = get_post( $post_id );
     1051
     1052                foreach( $expected as $field => $value ) {
     1053                        $this->assertEquals( $post->$field, $value );
     1054                }
     1055        }
    10191056}
Note: See TracChangeset for help on using the changeset viewer.