Make WordPress Core

Opened 20 months ago

Closed 16 months ago

Last modified 16 months ago

#58141 closed defect (bug) (worksforme)

`wp_insert_post` considers `"0"` as empty content.

Reported by: peterwilsoncc's profile peterwilsoncc Owned by:
Milestone: 6.4 Priority: normal
Severity: normal Version: 3.3
Component: Posts, Post Types Keywords: has-patch has-unit-tests needs-testing
Focuses: Cc:

Description

The wp_insert_post function includes a check to prevent the publishing of empty posts. A post is considered empty if the title, excerpt and content are undefined on post types that support all three items. See code reference.

As the code currently uses loose comparisons, the string "0" is considered empty content, to reproduce:

  1. Create a new post
  2. Switch to code editing view
  3. Set the title to 0
  4. Set the content to 0
  5. Attempt to publish the post
  6. WordPress with throw an error: Publishing failed. Content, title and excerpt are empty.

See the attached screen shot.

While 0 isn't great content, it is content so WordPress should allow the publishing of articles with this. Converting the tests to strict checks against an empty string should resolve the issue.

Props due: @cybr following discussion in Slack.

Attachments (1)

58141.png (9.8 KB) - added by peterwilsoncc 20 months ago.

Download all attachments as: .zip

Change History (11)

@peterwilsoncc
20 months ago

#1 @ugyensupport
20 months ago

Bug Report

Description

The wp_insert_post function includes a check to prevent the publishing of empty posts. A post is considered empty if the title, excerpt and content are undefined on post types that support all three items.

Environment

  • WordPress: 6.2
  • PHP: 8.0.0
  • Server: Apache/2.4.10 (Debian)
  • Database: mysqli (Server: 5.5.59-MariaDB-1~wheezy / Client: 5.5.62)
  • Browser: Chrome 112.0.0.0 (macOS)
  • Theme: Twenty Twenty-Three 1.1 ----> Twenty Twenty-Fourteen
  • MU-Plugins: None activated
  • Plugins:
    • Gutenberg 15.5.1

Steps to Reproduce

  1. As the code currently uses loose comparisons, the string "0" is considered empty content, to reproduce:
  2. Create a new post
  3. Switch to code editing view
  4. Set the title to 0
  5. Set the content to 0
  6. Attempt to publish the post

WordPress with throw an error: Publishing failed. Content, title and excerpt are empty.

  1. 🐞 Bug occurs.: While 0 isn't great content, it is content so WordPress should allow the publishing of articles with this. Converting the tests to strict checks against an empty string should resolve the issue. and In regard to WordPress 6.2 + Twenty Twenty-Three 1.1 It only display the content but not title.

Screencast : [WordPress 6.2 not displaying post title, if we set Post Title to 0] https://www.loom.com/share/b69bace69b1142978bad6f9bad415ec9

Expected Results

  1. ✅ Should View both the content and Post title in any versions of WordPress, importantly current version of WordPress.

Actual Results

  1. ❌ In conclusion I could see that the issue is due to the classic editor with WordPress 4.9 as I don't see major blocks with WordPress 5.0 and above as mentioned by @peterwilsoncc

Having said that I could see a minor block with WordPress 6.2 + Twenty Twenty-Three 1.1 as after we published the wordpress site with title and content "0" could only view content but not the Post Title.

#2 @markparnell
20 months ago

  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to 6.3

This seems like a quick win, and supports the ongoing work to move to strict comparisons (see e.g. #57839) so moving to 6.3 for visibility.

#3 @SergeyBiryukov
20 months ago

  • Summary changed from `wp_post_insert` considers `"0"` as empty content. to `wp_insert_post` considers `"0"` as empty content.

#4 @davidmusnik
20 months ago

Hello people,

I have been investigating this issue during the contributing day organized by WPFR in Paris on April 20th, 2023. First, it seems the problem pops up only when the title is equal to « 0 ». For some reason when both the title AND the content are equal to « 0 », the post seems inserted without error, unlike what is stated in the ticket description.

In any event, after having reviewed this part of the code, I have checked the following PHP statement in wp-include/post.php :

<?php
$maybe_empty = 'attachment' !== $post_type
        && ! $post_content && ! $post_title && ! $post_excerpt
        && post_type_supports( $post_type, 'editor' )
        && post_type_supports( $post_type, 'title' )
        && post_type_supports( $post_type, 'excerpt' );

This test is somehow dubious as I have written a quick and dirty program in PHP which tests that basic IF feature :

<?php
date_default_timezone_set("Europe/Paris");
$title = "0";
print ("title=$title\n");
if ($title) {
        print ("PHP returned TRUE\n");
} else {
        print ("PHP returned FALSE\n");
}

In my understanding of PHP, I thought that the program should return true ($title variable contains a correct string, but in fact it unexpectedly does return false (NB : both in PHP7.4’ and PHP 8.2).

Back to the issue at hand and beyond the overall PHP behavior, I suggest that the above statement should be modified the following way :

<?php
        $maybe_empty = 'attachment' !== $post_type
                && ( $post_content === '' ) 
                && ( $post_title === '' )
                && ( $post_excerpt === '' )
                && post_type_supports( $post_type, 'editor' )
                && post_type_supports( $post_type, 'title' )
                && post_type_supports( $post_type, 'excerpt' );

This ticket was mentioned in PR #4352 on WordPress/wordpress-develop by lphoumpakka.


20 months ago
#5

  • Keywords has-patch has-unit-tests added; needs-patch removed

Considers "0" is not an empty value for post_title / post_content and post_excerpt

Trac ticket: https://core.trac.wordpress.org/ticket/58141

#6 @oglekler
18 months ago

  • Keywords needs-testing added

This ticket was mentioned in Slack in #core by oglekler. View the logs.


17 months ago

#8 @oglekler
17 months ago

  • Milestone changed from 6.3 to 6.4

This ticket was discussed during the bug scrub and because the new patch still needs to be tested, it was decided to move in into 6.4 milestone.

Props @audrasjb

Last edited 17 months ago by oglekler (previous) (diff)

#9 @stephenerdelyi
16 months ago

  • Resolution set to worksforme
  • Status changed from new to closed

This appears to be resolved in trunk (WP core 6.4-alpha-56267-src). Recommending this be closed at this time as the bug is no longer able to be replicated.

#10 @ugyensupport
16 months ago

Bug Report

Description

The wp_insert_post function includes a check to prevent the publishing of empty posts. A post is considered empty if the title, excerpt and content are undefined on post types that support all three items.

Environment

  • WordPress: 6.4-alpha-56442
  • PHP: 8.0.0
  • Server: Apache/2.4.10 (Debian)
  • Database: mysqli (Server: 5.5.59-MariaDB-1~wheezy / Client: 5.5.62)
  • Browser: Chrome 116.0.0.0 (macOS)
  • Theme: Twenty Twenty-One 1.9
  • MU-Plugins: None activated
  • Plugins:
    • WordPress Beta Tester 3.5.3

Steps to Reproduce

  1. As the code currently uses loose comparisons, the string "0" is considered empty content, to reproduce:
  2. Create a new post
  3. Switch to code editing view
  4. Set the title to 0
  5. Set the content to 0
  6. Attempt to publish the post
  7. WordPress with throw an error: Publishing failed. Content, title and excerpt are empty.

🐞 Bug occurs.: While 0 isn't great content, it is content so WordPress should allow the publishing of articles with this. Converting the tests to strict checks against an empty string should resolve the issue. and In regard to WordPress 6.2 + Twenty Twenty-Three 1.1 It only display the content but not title.

Expected Results

  1. ✅ Works perfectly and fixed with WordPress version 6.4-alpha-56442
Last edited 16 months ago by ugyensupport (previous) (diff)
Note: See TracTickets for help on using tickets.