From 9ea1a7eb4fcb359a9c78599c9fa27cdce9f7b5d9 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Sun, 26 Jun 2016 11:29:13 +0000 Subject: [PATCH] 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 --- src/wp-includes/general-template.php | 14 ++-- tests/phpunit/tests/general/template.php | 100 +++++++++++++++++++++++ 2 files changed, 106 insertions(+), 8 deletions(-) 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. *