Posts: Fix back-compat for filters in get the modified time and date functions after [37738].

When no valid post exists for `get_the_modified_time` or `get_the_modified_date` calls, the result (`false`) is passed through the functions respective filters to maintain back-compat.

Introduces unit tests to ensure filters are applied and for the `get_the_modified_date` function.

Fixes #37059.


git-svn-id: https://develop.svn.wordpress.org/trunk@37866 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Peter Wilson 2016-06-26 11:29:13 +00:00
parent 9853789382
commit 9ea1a7eb4f
2 changed files with 106 additions and 8 deletions

View File

@ -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 );

View File

@ -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.
*