Conversation
|
Not sure why there are so many old commits in here since I based this branch off of |
That’s probably because you merged |
|
And I realized it’s hard to look at what you changed if all those commits from #2073 are in here as well. You probably have to revert that somehow. Up until here, what you write makes sense to me. But I also have to say that the exact inner workings of all the classes that are in involved here were always a bit hard to keep track of for me. So if we can reduce the complexity here, that would be great 👍.
Yes! I think I already partially worked on this in #2145. Maybe you could build on that.
Doesn’t this have big implications on caching if the actual work is only done after fetching cached posts in PHP templates during And then this is probably also related to handling posts in multisite environments (see #1434 and #2207). And from what I see in issues and StackOverflow questions, a lot of developers are relying on dumping post objects to see what’s in there. This of course wouldn’t be reliable anymore. I’m not saying I wouldn’t add lazy-loading, but we need to check what it means for other parts of Timber and probably add documentation about this in various places. A related example: we already fetch meta values lazily. A lot of developers were used to work with
I’m all in favor of simplifying this. What I figured when doing more complex stuff with iterators is that as soon as we have factories ready, we can move some logic regarding globals into One specific example is this class: https://github.com/mindkomm/timber-integration-woocommerce/blob/master/lib/ProductsIterator.php. We discussed this in mindkomm/timber-integration-woocommerce#6. With factories, we can get rid of it completley and a WooCommerce product object might solve WooCommerce globals like this: class Product extends Timber\Post {
public function setup( $loop_index = 0 ) {
parent::setup( $loop_index );
wc_setup_product_data( $this->ID );
return $this;
}
} |
Dang, OK, I'll figure out how to do that.
Awesome! I will take a look there, thanks!
I don't think I understand what you mean. :) Which code are you referring to as "fetching cached posts," and how is that affected by this change? I agree we'd want to document the effects of the change if it actually affects the behavior of the API. But as far as I can tell this will be completely transparent to the caller. That is, by the time you have a Again, maybe I'm missing something...wouldn't be the first time. :) I'm also cool with being cautious and reverting the lazy instantiation piece for now. If I'm right and it's truly transparent, that means we can roll it out after 2.0 lands.
Makes sense to me! Good idea. |
5393189 to
aa8d7ae
Compare
aa8d7ae to
23c3421
Compare
fc09fd6 to
23c3421
Compare
|
OK, |
@jarednova @gchtr I wanted to check in about this before I get too far along, because it involves a pretty deep design change that I want to validate. This is the first attempt at addressing #2308.
Summary
Right now, the logic for building and running
WP_Querys is spread aroundPostQuery,PostGetter,QueryIterator, and elsewhere. Part of the goal of Factories is to consolidate all of that logic in one place:PostFactory. To allow that,PostQueryneeds to give up some power, because right now it queries for posts itself viaPostGetter::query_posts(). But if the Factory already knows what it's dealing with, there's no need for this - the factory can simply instantiate aWP_Querydirectly and wrap it with aPostQuery. Then there's exactly one class responsible for querying for collections of posts.What this means
PostQuerynow extendsArrayObjectdirectly and implements thePostCollectionInterface.PostCollectionclass completely - just need to make sure the::maybe_set_preview()logic makes it out of there.QueryIterator. More on that below.Timber\PostQueryexcept when passing aWP_Query.WP_Queryin an array (i.e.[ 'query' => $wp_query ]) when passing to the constructor. Thepost_classoption is a precursor to the new Class Maps design and themerge_defaultseems like something that would better be implemented as an option toTimber::get_posts().WP_Postobjects they wrap, theTimber\Postinstances themselves are not actually created until they're needed. SeePostsIteratorfor how this works.PostGetter. I want to be careful here as a lot of high-level functionality expected from theTimberclass is tested at the level ofPostGetterinstead, so I just want to be extra mindful about keeping that test coverage without lifting redundant tests up to theTimberlevel.A note about
QueryIteratorAs far as I can tell, this class has four responsibilities:
WP_Querythat (mostly) lives inPostFactorynowPostFactoryare various query filter callbacks, namelyfix_number_posts_wp_quirk,fix_cat_wp_quirk, andhandle_maybe_custom_posts_page. This stuff should probably be movedWP_Queryresults, which we already have inPostQuery.Paginationobjects, which again, we can do directly inPostQuery(which is added in this PR).If I'm missing something here and we need this class after all, please let me know! But if we can move out the logic we want to keep I'm hoping we can just get rid of it.
The Plan
This PR isn't quite ready to merge but it seemed like a good stopping point to have this wider design discussion, since the high-level pieces are there. But quite a few tests are broken or skipped. I imagine I'll keep skipping many of them until I'm able to switch the Posts API altogether to using Factories, but some are probably easy fixes.
I'm leaning towards getting this to a good transitional point with Collections and merging this in then, before fully switching to Factories. Just don't want the PR to get too huge. Thoughts?