WordPress.org

Make WordPress Core

Ticket #47849: 47849.3.diff

File 47849.3.diff, 2.3 KB (added by donmhico, 2 years ago)
  • src/wp-includes/post.php

    diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php
    index 306e567bb9..300c50b8d9 100644
    a b function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) { 
    34193419 * @since 1.0.0
    34203420 * @since 4.2.0 Support was added for encoding emoji in the post title, content, and excerpt.
    34213421 * @since 4.4.0 A 'meta_input' array can now be passed to `$postarr` to add post meta data.
     3422 * @since 5.3.0 Post excerpt with only whitespaces is no longer saved.
    34223423 *
    34233424 * @see sanitize_post()
    34243425 * @global wpdb $wpdb WordPress database abstraction object.
    function wp_insert_post( $postarr, $wp_error = false ) { 
    35263527
    35273528        $post_title   = $postarr['post_title'];
    35283529        $post_content = $postarr['post_content'];
     3530
     3531        // Prevent `post_excerpt` with only whitespaces to be saved.
     3532        if ( empty( trim( $postarr['post_excerpt'] ) ) ) {
     3533                $postarr['post_excerpt'] = '';
     3534        }
     3535
    35293536        $post_excerpt = $postarr['post_excerpt'];
    35303537        if ( isset( $postarr['post_name'] ) ) {
    35313538                $post_name = $postarr['post_name'];
  • tests/phpunit/tests/post.php

    diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php
    index 6ef0c8449f..7ded284b6d 100644
    a b class Tests_Post extends WP_UnitTestCase { 
    13941394        function filter_pre_wp_unique_post_slug( $default, $slug, $post_ID, $post_status, $post_type, $post_parent ) {
    13951395                return 'override-slug-' . $post_type;
    13961396        }
     1397
     1398        /**
     1399         * @ticket 47849
     1400         */
     1401        function test_do_not_save_only_whitespaces_on_post_excerpt() {
     1402                $create_post_with_proper_excerpt = self::factory()->post->create( array(
     1403                        'post_title'   => 'Test post 1',
     1404                        'post_content' => 'Test post content 1',
     1405                        'post_excerpt' => ' Test post excerpt 1 '
     1406                ) );
     1407
     1408                $get_post_with_proper_excerpt = get_post( $create_post_with_proper_excerpt );
     1409
     1410                $this->assertSame( ' Test post excerpt 1 ', $get_post_with_proper_excerpt->post_excerpt );
     1411
     1412                $create_post_with_whitespaces_excerpt = self::factory()->post->create( array(
     1413                        'post_title'   => 'Test post 1',
     1414                        'post_content' => 'Test post content 1',
     1415                        'post_excerpt' => '        '
     1416                ) );
     1417
     1418                $get_post_with_whitespaces_excerpt = get_post( $create_post_with_whitespaces_excerpt );
     1419
     1420                $this->assertSame( '', $get_post_with_whitespaces_excerpt->post_excerpt );
     1421        }
     1422
    13971423}