WordPress.org

Make WordPress Core

Opened 12 months ago

Closed 12 months ago

Last modified 12 months ago

#49853 closed defect (bug) (fixed)

Sending post via email results in “(no title)” for the post title

Reported by: SergeyBiryukov Owned by: whyisjake
Milestone: 5.4.1 Priority: normal
Severity: normal Version:
Component: Mail Keywords: has-patch
Focuses: Cc:

Description

Reported by @markrh in 5.4: Sending Post via Email Results in “(no title)” for the post title on the forums:

Once a week I send an email to my blog to go into a category I have set for them. Anyway, starting with 5.4, the post titles were all “(no title)”. I did a comparison between wp-mail.php in 5.4 and 5.3. At line 216 changed:

if ( '' === $post_title ) {
	$post_title = $subject;
}

To:

if ( '' == $post_title ) {
	$post_title = $subject;
}

Now, the posts get the title from the email’s subject once again. Not sure if this is a bug or emails need to be structured differently now.

Introduced in [47054] / #48965.

Attachments (2)

49853.diff (337 bytes) - added by whyisjake 12 months ago.
49853.2.diff (334 bytes) - added by whyisjake 12 months ago.

Download all attachments as: .zip

Change History (14)

#1 @MarkRH
12 months ago

Using the suggestion here: https://wordpress.org/support/topic/5-4-sending-post-via-email-results-in-no-title-for-the-post-title/#post-12646149 this also works in getting the post title from the email subject.

if ( true === empty($post_title) ) {

#2 @afragen
12 months ago

There really isn't a need to use true === empty( $post_title ) when empty( $post_title ) evaluates to true if it is empty. A second evaluation isn't necessary.

#3 @MarkRH
12 months ago

Yes, if ( empty($post_title) ) { works as well in my case.

#4 @Otto42
12 months ago

Somebody pointed out (and I apologize, I don't know where I saw this, which is why I prefer to keep these types of discussions in ticket, for the records), that the use of if ( empty($post_title) ) { or similar would preclude the use of the string '0' for a post title. The number zero is empty according to PHP. Ah, the fun we have with loose typing.

https://www.php.net/manual/en/function.empty.php

The following values are considered to be empty:

"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)

So, my suggestion is to basically use trim (do your own yoda conditioning, I refuse):

if ( trim( $post_title ) == '' )

Test it with identity and equality comparisons to see if that provides the needed results.

Reason I suggest this is because trim() returns an empty string for a null/false/unset value. So, should give what we want over empty, which doesn't.

If we find that too has issues, then it looks like we should manually check for things that shouldn't be, because simply casting to whatever isn't going to work.

#5 @MattyRob
12 months ago

@Otto42

I think it was me that highlighted to post title of 'zero' issue, following from conversation about this with JoyR.

In my testing if ( trim( $post_title ) === '' ) works as expected and maintains the strict comparison:

<?php
$tests = array( 0, NULL, 'foo', '', '0' );
foreach ( $tests as $test ) {
        if ( '' === trim( $test ) ) {
                var_dump( $test );
                echo ' is empty!';
        }
}
Last edited 12 months ago by MattyRob (previous) (diff)

#6 @mukesh27
12 months ago

Hey there!

Result for the above code.

<?PHP
   NULL is empty!
   string(0) "" is empty!

@whyisjake
12 months ago

#7 @afragen
12 months ago

@whyisjake Yoda conditional must it be?

@whyisjake
12 months ago

#8 @whyisjake
12 months ago

  • Owner set to whyisjake
  • Status changed from new to accepted

Good catch @afragen.

#9 @mukesh27
12 months ago

  • Keywords has-patch added

Hi there!

Patch 49853.2.diff working fine for me.

#10 @SergeyBiryukov
12 months ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 47580:

Mail: Make the check for empty post title in wp-mail.php more resilient.

This addresses a regression in [47054], which caused posts sent via email to published with an empty title.

Props whyisjake, Otto42, MarkRH, MattyRob, mukesh27, afragen, pikamander2.
Fixes #49853.

#11 @SergeyBiryukov
12 months ago

In 47581:

Mail: Make the check for empty post title in wp-mail.php more resilient.

This addresses a regression in [47054], which caused posts sent via email to published with an empty title.

Props whyisjake, Otto42, MarkRH, MattyRob, mukesh27, afragen, pikamander2.
Merges [47580] to the 5.4 branch.
Fixes #49853.

Note: See TracTickets for help on using tickets.