#37059 closed enhancement (fixed)
Allow post to be passed to get_the_modified_time & get_the_modified_date
| Reported by: | peterwilsoncc | Owned by: | peterwilsoncc |
|---|---|---|---|
| Priority: | normal | Milestone: | 4.6 |
| Component: | Posts, Post Types | Version: | |
| Severity: | normal | Keywords: | good-first-bug has-patch has-unit-tests |
| Cc: | Focuses: |
Description (last modified by )
The signatures of get_the_time and get_the_modified_time differ. Let's make them match
The current signatures are:
get_the_time ( string $d = '', int|WP_Post $post = null )
get_the_modified_time ( string $d = '' )
get_the_date ( string $d = '', int|WP_Post $post = null )
get_the_modified_date ( string $d = '' )
~
Note: added the date functions per @Soean's comment below.
Attachments (9)
Change History (31)
#1
@
10 years ago
- Component General → Posts, Post Types
- Description modified (diff)
- Keywords good-first-bug needs-patch needs-unit-tests added
- Milestone Awaiting Review → Future Release
#4
@
10 years ago
- Keywords has-patch added; needs-patch removed
Same difference in the date functions.
get_the_date( $d = '', $post = null ) get_the_modified_date($d = '')
#5
@
10 years ago
The patch added in the documentation that was added, as well as changing the null value to be false.
#6
@
10 years ago
- Keywords has-unit-tests added; needs-unit-tests removed
- Owner set to
- Status new → reviewing
You have your patch, sir :)
#7
@
10 years ago
- Description modified (diff)
- Summary Allow post to be passed to get_the_modified_time → Allow post to be passed to get_the_modified_time & get_the_modified_date
#8
@
10 years ago
- Keywords needs-refresh added
Thanks for the patches @Soean and @lukecavanagh.
Just a few notes to get the patch commit ready. I'm basing them off the latest patch, 37059.3.diff
This looks like a lot but it's all really minor (and I'm verbose).
In the source code:
- In the docs:
- add a second
@sincetag noting the new argument (similar to thelanguage_attributesfunction in the same file). - align the
@paramvariables and descriptions.
- add a second
- Add the
get_post& check for a valid post similar to the check inget_the_time - The filters in the functions we're aiming to match also accept the post as an argument, let's match them too (noting the update in the docs).
- There's a couple of coding standards (whitespace) fixes required in
get_the_modified_date's first call toget_post_modified_time - [edit] all the code inside the if...else statements is changing, so it's a good opportunity to add braces to meet the coding standards.
In the tests:
- Coding standards (whitespace) the closing brace of the first new function onwards are incorrectly indented.
- Add some documentation for each function, including:
- explanation
- since tags
- this ticket number
...with_post_idis clearer for people scanning the code.
As I say, all quite minor.
Pete
#9
@
10 years ago
Thanks @peterwilsoncc for your great review.
This is my first patch and your feedback helps me a lot.
So I hope I get all your points in 37059.4.diff .
#11
@
10 years ago
- Milestone Future Release → 4.6
@Soean: Thanks for the update. Here are some further notes on 37059.4.diff:
- Let's add backticks around
$postin the changelog entries for both functions. The should also be structured as sentences, albeit short:* @since 4.6.0 The `$post` parameter was added.
- We also need changelog entries for the new parameters added to the two filters
- Also, at the point where
$postis passed to the filters, it's only aWP_Postobject
#12
@
10 years ago
Thanks @DrewAPicture for your notes, I updated the patch and uploaded 37059.5.diff
#14
@
10 years ago
Thanks for the refreshes @Soean, I'll commit this Friday Australian time and props you and @lukecavanagh.
This is my first patch and your feedback helps me a lot.
Looking forward to many more, I'm glad Drew and I could help :)
#16
follow-up:
↓ 20
@
10 years ago
- Resolution fixed
- Status closed → reopened
I think that in get_the_modified_date() this code
$post = get_post( $post );
if ( ! $post ) {
return false;
}
is unnecessary because in get_post_modified_time check valid $variable and function get_post_modified_time() is called always
This ticket was mentioned in Slack in #core by peterwilsoncc. View the logs.
10 years ago
#18
@
10 years ago
- Keywords commit removed
Post commit [37738] @azaozz and I had a brief chat in Slack re backcompat.
Previously, if called before the global $post object was set, the value false would be run through the filter. Now, if the post is called before the global $post object is set, the function returns false and bypasses the filter.
37059.2.diff restores the old behavior of passing a failure to get the post though the filter.
I'm genuinely undecided on how much it matters, the old behavior could only have occurred before the posts_selection action.
#19
@
10 years ago
In 37059.6.diff:
- maintains back-compat by passing failures to get a post through filters for both
get_the_modified_dateandget_the_modified_time - adds unit test to ensure failures are filtered
- added tests for
get_modified_dateto match those forget_modified_time.
#20
in reply to: ↑ 16
@
10 years ago
Replying to sebastian.pisula:
I think that in get_the_modified_date() this code
$post = get_post( $post ); if ( ! $post ) { return false; }is unnecessary because in get_post_modified_time check valid $variable and function get_post_modified_time() is called always
I'm going to keep the ! $post check in both get_the_modified_date and get_the_modified_time. You're correct, get_post_modified_time does the same check but when the outcome of a function call is easily premepted it can be bypassed.
This ticket was mentioned in PR #15473 on WordPress/wordpress-develop by @westonruter.
7 days ago
#22
get_comment_date() and get_comment_time() return the result of mysql2date(), which is an integer for the 'U' and 'G' formats and false when the date is missing or cannot be parsed. Yet both were documented as returning only a string, so callers like human_time_diff( get_comment_date( 'U', $comment ) ) in the Comment Date block had to cast the result to satisfy PHPStan (see https://github.com/WordPress/gutenberg/pull/86095#discussion_r4471957574).
This documents the actual string|int|false return type on both functions, with a @phpstan-return conditional on $format matching the ones already on mysql2date() and the post date functions (get_the_date(), get_the_time(), get_post_time(), get_the_modified_date(), get_the_modified_time(), and get_post_modified_time()):
get_comment_date():( $format is 'U'|'G' ? int|false : string|false )get_comment_time():( $format is 'U'|'G' ? int|false|'' : string|false ), since it also returns an empty string when the comment does not exist.
The get_comment_date and get_comment_time filters now document false as well. Since apply_filters() is typed from the hook's @param, leaving it out made PHPStan report return.unusedType for the new return types.
get_comment_date() also had no guard for a nonexistent comment: it read $comment->comment_date on null, emitting an "Attempt to read property on null" warning on PHP 8, before mysql2date() returned false. It now uses false as the date without touching the null comment, but still applies the get_comment_date filter, following the back-compat pattern of get_the_modified_date() and get_the_modified_time(). Those functions briefly skipped their filters on failure in r37738, which was reverted for back-compat in r37866 (Core-37059). The filter's $comment parameter is now documented as WP_Comment|null, and a test covers the filter receiving false and a null comment.
These were the only date functions found lacking a conditional return: date_i18n(), wp_date(), and get_feed_build_date() always return a string, even for 'U'.
At level 10, a few unchanged callers that pass the result straight into a string parameter (the comment feed templates' mysql2date( …, get_comment_time( 'Y-m-d H:i:s', true, false ), false )) now surface the pre-existing false case as argument.type. Nothing new is reported at the configured level 5.
Trac ticket: Core-65817
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5 (medium effort)
Used for: Applying the suggested patch, surveying the other date functions, verifying with PHPStan, and drafting this description; reviewed by me.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Let me work on a patch.