diff --git a/src/wp-includes/general-template.php b/src/wp-includes/general-template.php index e418f7220a..db0dfafa74 100644 --- a/src/wp-includes/general-template.php +++ b/src/wp-includes/general-template.php @@ -2265,10 +2265,9 @@ function get_the_modified_date( $d = '', $post = null ) { $post = get_post( $post ); if ( ! $post ) { - return false; - } - - if ( empty( $d ) ) { + // For backward compatibility, failures go through the filter below. + $the_time = false; + } elseif ( 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 ); @@ -2421,10 +2420,9 @@ function get_the_modified_time( $d = '', $post = null ) { $post = get_post( $post ); if ( ! $post ) { - return false; - } - - if ( empty( $d ) ) { + // For backward compatibility, failures go through the filter below. + $the_time = false; + } elseif ( 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 ); diff --git a/tests/phpunit/tests/general/template.php b/tests/phpunit/tests/general/template.php index 0fa6d9c93c..d189cc9449 100644 --- a/tests/phpunit/tests/general/template.php +++ b/tests/phpunit/tests/general/template.php @@ -369,6 +369,106 @@ class Tests_General_Template extends WP_UnitTestCase { $this->assertEquals( $expected, $actual ); } + /** + * Test get_the_modified_time failures are filtered + * + * @ticket 37059 + * + * @since 4.6.0 + */ + function test_get_the_modified_time_failures_are_filtered() { + // Remove global post objet + $GLOBALS['post'] = null; + + $expected = 'filtered modified time failure result'; + add_filter( 'get_the_modified_time', array( $this, '_filter_get_the_modified_time_failure' ) ); + $actual = get_the_modified_time(); + $this->assertEquals( $expected, $actual ); + remove_filter( 'get_the_modified_time', array( $this, '_filter_get_the_modified_time_failure' ) ); + } + + function _filter_get_the_modified_time_failure( $the_time ) { + $expected = false; + $actual = $the_time; + $this->assertEquals( $expected, $actual ); + + if ( false === $the_time ) { + return 'filtered modified time failure result'; + } + return $the_time; + } + + /** + * Test get_the_modified_time with post_id parameter. + * + * @ticket 37059 + * + * @since 4.6.0 + */ + function test_get_the_modified_date_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 = 'Y-m-d'; + $expected = '2016-01-21'; + $actual = get_the_modified_date( $d, $post_id ); + $this->assertEquals( $expected, $actual ); + } + + /** + * Test get_the_modified_date + * + * @ticket 37059 + * + * @since 4.6.0 + */ + function test_get_the_modified_date_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 = '2016-01-21'; + $d = 'Y-m-d'; + $actual = get_the_modified_date( $d ); + $this->assertEquals( $expected, $actual ); + } + + /** + * Test get_the_modified_date failures are filtered + * + * @ticket 37059 + * + * @since 4.6.0 + */ + function test_get_the_modified_date_failures_are_filtered() { + // Remove global post objet + $GLOBALS['post'] = null; + + $expected = 'filtered modified date failure result'; + add_filter( 'get_the_modified_date', array( $this, '_filter_get_the_modified_date_failure' ) ); + $actual = get_the_modified_date(); + $this->assertEquals( $expected, $actual ); + remove_filter( 'get_the_modified_date', array( $this, '_filter_get_the_modified_date_failure' ) ); + } + + function _filter_get_the_modified_date_failure( $the_date ) { + $expected = false; + $actual = $the_date; + $this->assertEquals( $expected, $actual ); + + if ( false === $the_date ) { + return 'filtered modified date failure result'; + } + return $the_date; + } + /** * Test get_the_modified_time with post_id parameter. *