2.x Add compatibility for Twig date functionality#2135
Conversation
Codecov Report
@@ Coverage Diff @@
## 2.x #2135 +/- ##
======================================
Coverage ? 95.72%
Complexity ? 1539
======================================
Files ? 50
Lines ? 3949
Branches ? 0
======================================
Hits ? 3780
Misses ? 169
Partials ? 0
Continue to review full report at Codecov.
|
…ocs-datetime # Conflicts: # lib/DateTimeHelper.php # lib/Twig.php # tests/test-timber-dates.php
…he Travis errors in WP 4.9.8
|
@gchtr this is fantastic. I LOVE the idea of just stealing Twig's tests — makes sure we match their functionality AND lazy. Obviously, the big Q is whether to support versions prior to 5.3 in 2.x. I'm definitely partial to dropping support. Currently the WordPress.org Stats have version 5.3 at 29% after about 6 weeks of its release. I think new Timber users for 2.x (who will no longer be able to download from WP.org and have to use Composer) are highly likely to be using fresh installs with the newest version. That said, Travis flagged a ton of errors coming from a lack of the |
Riiight? It was actually quite rewarding to drop in the tests and then make them work one by one.
That were my thoughts, too 👍.
There would be more new functions to cover in #2133. And there’s However, apart from a couple of new functions, there apparently were a lot of changes to WordPress Core in other places. For example, they tried to reduce the amount of times If we wanted to add support, we would probably have to work with the existing functions in WordPress < 5.3, namely |
…kwards compatability Nice idea! It just doesn't work
|
cool, we're on the same page then. The idea of stubbing in that function sounded lovely, but in addition to the issues you flagged ( I updated Travis on this branch to not support/test WP before 5.3.2. Even though right now |
|
@gchtr found a small issue with the tests we might want to resolve. Sometimes we'll get errors on tests like: 1) TestTimberDates::testTimeAgoWithPostDateAndCurrent
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'3 years ago'
+'4 years ago'
/home/travis/build/timber/timber/tests/test-timber-dates.php:50function testTimeAgoWithPostDateAndCurrent() {
$pid = $this->factory->post->create( [
'post_date' => '2016-07-06 20:03:00',
] );
$post = Timber::get_post( $pid );
$str = DateTimeHelper::time_ago( $post->date() );
$this->assertEquals(
sprintf( '%s ago', human_time_diff( strtotime( $post->post_date ) ) ),
$str
);
}Why? In the function we create a post created on July 6, 2016 at ~8pm (or A) Set all the time stamps to midnight so there's never a diff Which of those is your favorite? |
jarednova
left a comment
There was a problem hiding this comment.
I found one issue with the tests to take a look at ref'd in this comment here: #2135 (comment)
@jarednova I guess if we check for time diffs lower than a day (e.g. "5 hours") in other tests, then we can very well go with A). Will you update the tests, or should I do it? |
|
I can take care of this one!
|
|
@jarednova When WordPress versions below 5.3 are not supported anymore, should we add a check when Timber initializes and provide a proper warning? |
|
@gchtr good call! I'll make a separate issue/PR to track that |
|
@jarednova Can you approve your requested changes and merge this? Everything seems fine from my side. |
|
Yup, looks good — great to have this one in!!!! |
Ticket: #2104, #2127
Whew, yet another journey where I learnt lots about the inner workings of WordPress and dates and time in general 🤪.
Issue
WordPress handles dates differently than most PHP applications. It’s no suprise that there a Twig compatibility issues.
Solution
As of WordPress 5.3, there we a lot of updates to the Date/Time component. This pull request builds on top of that new functionality.
Add compatibility with Twig Date functionality
Instead of creating a separate filter function for the Twig
datefilter, I figured the best way to avoid confusion would be to try to add compatibility with Twig.I first replicated the tests from Twig to ensure the
datefilter as well as thedate()function work properly. I then tried to add compatibility by:datefunction.datefilter with a custom implementation inTwig::twig_date_format_filter().The test results indicate that now everything works. And there’s the added benefit that we also have support for
DateIntervalin thedatefilter.Add new
DateTimeHelperclassI’ve figured that the new wrapper for
wp_date()could be used in other places and that the proper place isn’t in theTimber\Twigclass. That’s why I created a newDateTimeHelperclass for all date related helper functions.Twig::intl_date()and moved it toDateTimeHelper::wp_date().Twig::time_ago()and moved it toDateTimeHelper::time_ago().\DateTimeImmutableby checking for\DateTimeInterfaceinstead of\DateTime.Impact
This pull request relies on a lot of Date/Time functionality that was introduced in WordPress 5.3, which would mean, that we would have to bump the minimum required WordPress version for Timber 2.0 to 5.3. Otherwise, we’d have to add backwards compatibility.
Usage Changes
Apart from the functions that were moved to the
DateTimeHelperclass, I guess there are none.Considerations
Adding backwards compatibility for WordPress < 5.3 would require more work. I didn’t check yet how much, but it would surely require us to basically replicate some of the functionality added in WordPress 5.3. Would there be benefits for adding that backward compatibility?
When we make use of the new Date/Time functions introduced in WordPress 5.3, we can also update
{{ post.date }}and{{ post.modified }}. See separate pull request.Testing
Yes, there’s lots of testing included.
I also added a new
Timber\Dategroup to run tests that check date functionality with the--group="Timber\Date"parameter.Todo
Maybe add backwards compatibility for WordPress < 5.3.We will not add backwards compatibility.