Skip to content

Ref #2012 – Update skipping get_post_meta() to match behavior in 2.0#2014

Merged
jarednova merged 7 commits intomasterfrom
pr/2012
May 31, 2019
Merged

Ref #2012 – Update skipping get_post_meta() to match behavior in 2.0#2014
jarednova merged 7 commits intomasterfrom
pr/2012

Conversation

@gchtr
Copy link
Copy Markdown
Member

@gchtr gchtr commented May 30, 2019

Ticket: #2012

Issue

In #2012 @aj-adl proposed a way to skip Timber from fetching meta values through get_post_meta() via the timber_post_get_meta_pre filter. Turns out that in 2.x, I added pretty much the same behavior, just with a new filter:

timber/lib/Post.php

Lines 574 to 624 in 35279e5

protected function get_meta_values( $post_id ) {
$post_meta = array();
/**
* Filters post meta data before it is fetched from the database.
*
* Timber loads all meta values into the post object on initialization. With this filter,
* you can disable fetching the meta values through the default method, which uses
* `get_post_meta()`, by returning `false` or an non-empty array.
*
* @example
* ```php
* // Disable fetching meta values.
* add_filter( 'timber/post/pre_get_meta_values', '__return_false' );
*
* // Add your own meta data.
* add_filter( 'timber/post/pre_get_meta_values', function( $post_meta, $post_id, $post ) {
* $post_meta = array(
* 'custom_data_1' => 73,
* 'custom_data_2' => 274,
* );
*
* return $post_meta;
* }, 10, 3 );
* ```
* @since 2.0.0
*
* @param array $post_meta An array of custom meta values. Passing false or a non-empty
* array will skip fetching the values from the database and
* will use the filtered values instead. Default `array()`.
* @param int $post_id The post ID.
* @param \Timber\Post $post The post object.
*/
$post_meta = apply_filters( 'timber/post/pre_get_meta_values', $post_meta, $post_id, $this );
/**
* Filters post meta data before it is fetched from the database.
*
* @deprecated 2.0.0, use `timber/post/pre_get_meta_values`
*/
do_action_deprecated(
'timber_post_get_meta_pre',
array( $post_meta, $post_id, $this ),
'2.0.0',
'timber/post/pre_get_meta_values'
);
// Load all meta data when it wasn’t filtered before.
if ( false !== $post_meta && empty( $post_meta ) ) {
$post_meta = get_post_meta( $post_id );
}

I wasn’t sure whether this was over-optimization, but hey, here we have a use case already 😎.

Solution

This pull request builds on #2012 to replicate the behavior that we’re going to add in 2.0 in the master branch so that this feature can be used now.

Impact

None.

Usage Changes

See #2012.

Considerations

None.

Testing

None.

aj-adl and others added 3 commits May 24, 2019 11:44
- Allow the user to stop eager loading by returning `null` or `false` via timber_post_get_meta_pre filter.
- Allow the user to choose what to eager load by returning an array of k/v meta
- Test for 'short circuit' where no meta should be eager loaded
- Test for custom list of eager-loaded post meta
@gchtr gchtr requested review from jarednova and palmiak as code owners May 30, 2019 19:40
@codecov
Copy link
Copy Markdown

codecov bot commented May 30, 2019

Codecov Report

Merging #2014 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master    #2014      +/-   ##
============================================
+ Coverage     94.98%   94.99%   +<.01%     
- Complexity     1566     1567       +1     
============================================
  Files            49       49              
  Lines          3692     3694       +2     
============================================
+ Hits           3507     3509       +2     
  Misses          185      185
Impacted Files Coverage Δ Complexity Δ
lib/Post.php 95.68% <100%> (+0.01%) 246 <0> (+1) ⬆️

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 6f3ccaf...3744e6a. Read the comment docs.

jarednova
jarednova previously approved these changes May 30, 2019
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.

Yup, this seems to do it! Just 🤞 that we're not gonna see a big merge conflict here with 2.0

@jarednova jarednova merged commit 2e0418c into master May 31, 2019
@jarednova jarednova deleted the pr/2012 branch May 31, 2019 01:26
@jarednova
Copy link
Copy Markdown
Member

Thanks @aj-adl and @gchtr — I added a note to the readme so this can go out with all the info in the next version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants