Prevent fatal error when previewing unpublished liveblog posts#921
Merged
Conversation
Previewing an unpublished liveblog post (draft, pending or auto-draft) triggered a fatal: get_post_datetime() returns false for the 0000-00-00 00:00:00 GMT date such posts carry, and the metadata builder called format() on that false value directly. Guard both the published and modified datetime lookups so the date-derived schema properties are simply omitted when no valid date is available, leaving behaviour unchanged for published, private and scheduled posts. Add integration coverage for the draft case and a companion assertion that published posts still expose the dates. Fixes VIPPLUG-24.
b535c0f to
98a3c90
Compare
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Viewing an unpublished liveblog post could take the whole page down with a fatal error:
The schema metadata builder derived
datePublishedanddateModifiedby callingget_post_datetime( $post, …, 'gmt' )->format( 'c' ). WordPress stores a0000-00-00 00:00:00GMT date for drafts, pending and auto-draft posts, and for that valueget_post_datetime()returnsfalse. Callingformat()onfalsethen threw, and because the metadata is printed duringwp_head, the error surfaced for anyone previewing such a post.This change looks up each datetime first and only sets the corresponding schema properties (
datePublished/coverageStartTimeanddateModified/coverageEndTime) when a valid object is returned. The date-derived fields are simply omitted when no published date exists yet, which is the correct outcome for a post that has not been published. Behaviour is unchanged for published, private and scheduled posts, all of which carry real GMT dates.The accompanying integration tests lock in both halves of the contract: a draft post now generates its metadata without fatalling and without the date keys, while a published post continues to expose them.
Fixes #919. Resolves VIPPLUG-24.
Test plan
composer test:integration(or the equivalent wp-env run) passes, including the two newSchemaMetadataTestcases