From 54f1c097a64e4e6f34c793d6eb2e07a4414880a1 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Fri, 17 Jun 2016 05:11:29 +0000 Subject: [PATCH] Posts: Add `$post` parameter to modified date and time functions. Unifies the APIs for getting a post's modified date or time with getting a post's date or time. Adds the `$post` parameter to the functions `get_the_modified_date` and `get_the_modified_time`. Adds the `$post` parameter to the filters `get_the_modified_date` and `get_the_modified_time`. Props Soean, lukecavanagh. Fixes #37059. git-svn-id: https://develop.svn.wordpress.org/trunk@37738 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/general-template.php | 62 ++++++++++++++++-------- tests/phpunit/tests/general/template.php | 42 ++++++++++++++++ 2 files changed, 85 insertions(+), 19 deletions(-) diff --git a/src/wp-includes/general-template.php b/src/wp-includes/general-template.php index c7139c52db..99ae18b044 100644 --- a/src/wp-includes/general-template.php +++ b/src/wp-includes/general-template.php @@ -2254,26 +2254,37 @@ function the_modified_date( $d = '', $before = '', $after = '', $echo = true ) { * Retrieve the date on which the post was last modified. * * @since 2.1.0 + * @since 4.6.0 The `$post` parameter was added. * - * @param string $d Optional. PHP date format. Defaults to the "date_format" option - * @return string + * @param string $d Optional. PHP date format defaults to the date_format option if not specified. + * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default current post. + * @return false|string Date the current post was modified. False on failure. */ -function get_the_modified_date($d = '') { - if ( '' == $d ) - $the_time = get_post_modified_time(get_option('date_format'), null, null, true); - else - $the_time = get_post_modified_time($d, null, null, true); +function get_the_modified_date( $d = '', $post = null ) { + $post = get_post( $post ); + + if ( ! $post ) { + return false; + } + + if ( empty( $d ) ) { + $the_time = get_post_modified_time( get_option( 'date_format' ), false, $post, true ); + } else { + $the_time = get_post_modified_time( $d, false, $post, true ); + } /** * Filters the date a post was last modified. * * @since 2.1.0 + * @since 4.6.0 The `$post` parameter was added. * - * @param string $the_time The formatted date. - * @param string $d PHP date format. Defaults to value specified in - * 'date_format' option. + * @param string $the_time The formatted date. + * @param string $d PHP date format. Defaults to value specified in + * 'date_format' option. + * @param WP_Post $post WP_Post object. */ - return apply_filters( 'get_the_modified_date', $the_time, $d ); + return apply_filters( 'get_the_modified_date', $the_time, $d, $post ); } /** @@ -2397,27 +2408,40 @@ function the_modified_time($d = '') { * Retrieve the time at which the post was last modified. * * @since 2.0.0 + * @since 4.6.0 The `$post` parameter was added. * - * @param string $d Optional Either 'G', 'U', or php date format defaults to the value specified in the time_format option. - * @return string + * @param string $d Optional. Format to use for retrieving the time the post + * was modified. Either 'G', 'U', or php date format defaults + * to the value specified in the time_format option. Default empty. + * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default current post. + * @return false|string Formatted date string or Unix timestamp. False on failure. */ -function get_the_modified_time($d = '') { - if ( '' == $d ) - $the_time = get_post_modified_time(get_option('time_format'), null, null, true); - else - $the_time = get_post_modified_time($d, null, null, true); +function get_the_modified_time( $d = '', $post = null ) { + $post = get_post( $post ); + + if ( ! $post ) { + return false; + } + + if ( empty( $d ) ) { + $the_time = get_post_modified_time( get_option( 'time_format' ), false, $post, true ); + } else { + $the_time = get_post_modified_time( $d, false, $post, true ); + } /** * Filters the localized time a post was last modified. * * @since 2.0.0 + * @since 4.6.0 The `$post` parameter was added. * * @param string $the_time The formatted time. * @param string $d Format to use for retrieving the time the post was * written. Accepts 'G', 'U', or php date format. Defaults * to value specified in 'time_format' option. + * @param WP_Post $post WP_Post object. */ - return apply_filters( 'get_the_modified_time', $the_time, $d ); + return apply_filters( 'get_the_modified_time', $the_time, $d, $post ); } /** diff --git a/tests/phpunit/tests/general/template.php b/tests/phpunit/tests/general/template.php index 5d4b92135f..0fa6d9c93c 100644 --- a/tests/phpunit/tests/general/template.php +++ b/tests/phpunit/tests/general/template.php @@ -345,4 +345,46 @@ class Tests_General_Template extends WP_UnitTestCase { $this->custom_logo_id = $this->_make_attachment( $upload ); return $this->custom_logo_id; } + + /** + * Test get_the_modified_time + * + * @ticket 37059 + * + * @since 4.6.0 + */ + function test_get_the_modified_time_default() { + $details = array( + 'post_date' => '2016-01-21 15:34:36', + 'post_date_gmt' => '2016-01-21 15:34:36', + ); + $post_id = $this->factory->post->create( $details ); + $post = get_post( $post_id ); + + $GLOBALS['post'] = $post; + + $expected = '1453390476'; + $d = 'G'; + $actual = get_the_modified_time( $d ); + $this->assertEquals( $expected, $actual ); + } + + /** + * Test get_the_modified_time with post_id parameter. + * + * @ticket 37059 + * + * @since 4.6.0 + */ + function test_get_the_modified_time_with_post_id() { + $details = array( + 'post_date' => '2016-01-21 15:34:36', + 'post_date_gmt' => '2016-01-21 15:34:36', + ); + $post_id = $this->factory->post->create( $details ); + $d = 'G'; + $expected = '1453390476'; + $actual = get_the_modified_time( $d, $post_id ); + $this->assertEquals( $expected, $actual ); + } }