Skip to content

2.x Improve Timber::get_post_by()#2195

Merged
jarednova merged 6 commits into2.x-get-post-byfrom
2.x-get-post-by-oldest
Feb 28, 2020
Merged

2.x Improve Timber::get_post_by()#2195
jarednova merged 6 commits into2.x-get-post-byfrom
2.x-get-post-by-oldest

Conversation

@gchtr
Copy link
Copy Markdown
Member

@gchtr gchtr commented Feb 16, 2020

Ticket: #2169

This pull request adds on top of #2169. But because I had to change more than I thought, this comes in a separate pull request so we can discuss it first.

In 302e08b, you changed the order the posts were created. This go me thinking. If we want to check for the oldest post and make sure we ignore the post ID, which is an indicator of the order the posts were created in, then the post expect to be returned shouldn’t be the first to be created.

In 58d24dd, you changed the parameter in the test to any, which is not the same as an array of post types. Because of this, a different function to search for posts was used, making the tests pass. I think we should check for both.

I figured out that post_exists() and get_page_by_title() won’t work if we go for the oldest post and don’t want to sort by ID. That’s why I replaced post_exists() and get_page_by_title() with our own query in d6f95d3. I usually try to avoid creating direct queries, but I think there’s no easier way to search for a post title.

But by doing that, we open a new possibility to make the function more useful. We could add order arguments:

public static function get_post_by( $type, $search_value, $args = [] ) {
    $args = wp_parse_args( $args, [
        'post_type' => 'any',
        'order_by'  => 'post_date',
        'order'     => 'ASC'
    ]

    // ...
}

This way, developers could decide on their own to change the logic a post is selected when there are multiple posts available.

When we check for the date, the ID of the post we expect to be returned needs be higher than the ones that should be ignored in order for the test to run correctly
This makes it possible to order by anything we want
@codecov
Copy link
Copy Markdown

codecov bot commented Feb 16, 2020

Codecov Report

Merging #2195 into 2.x-get-post-by will increase coverage by 1.23%.
The diff coverage is 100%.

Impacted file tree graph

@@                  Coverage Diff                  @@
##             2.x-get-post-by    #2195      +/-   ##
=====================================================
+ Coverage              94.83%   96.07%   +1.23%     
- Complexity              1536     1537       +1     
=====================================================
  Files                     50       50              
  Lines                   3912     3921       +9     
=====================================================
+ Hits                    3710     3767      +57     
+ Misses                   202      154      -48
Impacted Files Coverage Δ Complexity Δ
lib/Timber.php 97.2% <100%> (+6.15%) 39 <7> (+1) ⬆️
lib/Loader.php 96.61% <0%> (+0.48%) 75% <0%> (ø) ⬇️
lib/Cache/Cleaner.php 100% <0%> (+33.33%) 6% <0%> (ø) ⬇️
lib/Site.php 100% <0%> (+45.71%) 18% <0%> (ø) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 402e3c9...8e49829. Read the comment docs.

@jarednova
Copy link
Copy Markdown
Member

Looks good! You sure caught my play-by-play as I was trying to get this to a good place last week. Seems like a crazy little bit of WordPress weirdness left in by the WordPress gods that d6f95d3 solves.

I totally agree with the last option (I was already feeling a bit unsure about "assuming" that a user would want the oldest) — so this makes it a pure default/option. I'm pulling down now to add that last piece ...

Copy link
Copy Markdown
Member

@jarednova jarednova left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I implemented your suggestion and added a test to go along with — back to you or @acobster to merge!

@jarednova jarednova merged commit 7603c3a into 2.x-get-post-by Feb 28, 2020
@jarednova jarednova deleted the 2.x-get-post-by-oldest branch February 28, 2020 00:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants