Skip to content

Commit 1aeeceb

Browse files
author
Tom Mitchelmore
committed
Update uses of new Post
1 parent 4b3c490 commit 1aeeceb

File tree

12 files changed

+75
-69
lines changed

12 files changed

+75
-69
lines changed

docs/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ class IndexController
2626
{
2727
public function handle()
2828
{
29-
$context = Timber::get_context();
30-
$context['posts'] = Post::whereStatus('publish')
29+
$context = Timber::context();
30+
$context['posts'] = Post::builder()
31+
->whereStatus('publish')
3132
->limit(5)
3233
->get();
3334

docs/the-basics/post-types.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,23 @@ sidebar_position: 4
99
Typically in WordPress when you're querying posts you get [`WP_Post`](https://codex.wordpress.org/Class_Reference/WP_Post) objects back. Timber have taken this a step further and return a `Timber/Post` object instead. This [has a ton of great helper methods and properties](https://timber.github.io/docs/reference/timber-post/) which makes it easier and more expressive to use.
1010

1111
```php
12-
use Timber\Post;
13-
14-
$post = new Post(1);
15-
$posts = Timber::get_posts($wpQueryArray);
12+
$post = Timber::get_post(1); // \Rareloop\Lumberjack\Post
13+
$posts = Timber::get_posts($wpQueryArray); // \Timber\PostQuery
1614
```
1715

18-
Lumberjack has its own Post object which makes it easier and more expressive to run queries.
16+
:::info Note on Timber v2
17+
Timber v2 deprecated direct instantiation in favour of the `Timber::get_post()` factory implementation. When you call `get_post()`, Timber uses the Class Map to return the correct class for that post.
18+
19+
**Lumberjack automatically registers your custom post types with the Timber Class Map**
20+
21+
See: [Timber: Class Maps](https://timber.github.io/docs/v2/guides/class-maps/#the-post-class-map)
22+
:::
23+
24+
Lumberjack post types provide a static `query` method to make this simpler.
1925

2026
```php
2127
use Rareloop\Lumberjack\Post;
2228

23-
$post = new Post(1);
2429
$collection = Post::query($wpQueryArray);
2530
```
2631

@@ -29,7 +34,6 @@ This becomes especially powerful when you start registering **Custom Post Types*
2934
```php
3035
use App\PostTypes\Product;
3136

32-
$post = new Product(1);
3337
$collection = Product::query($wpQueryArray);
3438
```
3539

docs/the-basics/view-models.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class SingleController
2323
{
2424
public function handle()
2525
{
26-
$context = Timber::get_context();
27-
$post = new Post;
26+
$context = Timber::context();
27+
$post = Timber::get_post();
2828

2929
$date = new \DateTime($post->post_date);
3030

@@ -133,7 +133,6 @@ Now we can refactor our controller to use our view model instead:
133133

134134
namespace App;
135135

136-
use Rareloop\Lumberjack\Post;
137136
use Timber\Timber;
138137
use App\ViewModels\MediaCardViewModel;
139138
use Rareloop\Lumberjack\Http\Responses\TimberResponse;
@@ -142,8 +141,8 @@ class SingleController
142141
{
143142
public function handle()
144143
{
145-
$context = Timber::get_context();
146-
$post = new Post;
144+
$context = Timber::context();
145+
$post = Timber::get_post();
147146

148147
$context['card'] = new MediaCardViewModel($post);
149148

@@ -287,7 +286,6 @@ If your view model does not know about how to get data, then you will have to fe
287286

288287
namespace App;
289288

290-
use Rareloop\Lumberjack\Post;
291289
use Timber\Timber;
292290
use App\ViewModels\TestimonialViewModel;
293291
use Rareloop\Lumberjack\Http\Responses\TimberResponse;
@@ -296,8 +294,8 @@ class SingleController
296294
{
297295
public function handle()
298296
{
299-
$context = Timber::get_context();
300-
$post = new Post;
297+
$context = Timber::context();
298+
$post = Timber::get_post();
301299

302300
// Get the data from somewhere, for example from ACF
303301
// You would have to duplicate these two lines in each controller
@@ -366,7 +364,6 @@ And we can now refactor our controller to use the named constructor like so:
366364

367365
namespace App;
368366

369-
use Rareloop\Lumberjack\Post;
370367
use Timber\Timber;
371368
use App\ViewModels\TestimonialViewModel;
372369
use Rareloop\Lumberjack\Http\Responses\TimberResponse;
@@ -375,8 +372,8 @@ class SingleController
375372
{
376373
public function handle()
377374
{
378-
$context = Timber::get_context();
379-
$post = new Post;
375+
$context = Timber::context();
376+
$post = Timber::get_post();
380377

381378
$context['testimonial'] = TestimonialViewModel::forPost($post);
382379

@@ -391,6 +388,7 @@ And you could have the following named constructor:
391388

392389
- `latestPosts($limit = 3)`- which knows how to get the latest _n_ posts.
393390
- `relatedPosts(Post $post)`- which knows how to get posts related to given post
391+
394392
:::
395393

396394
## Using Hatchet

docs/the-basics/wordpress-controllers.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,15 @@ In the files (i.e. controllers) that WordPress uses when it matches a route (e.g
1818
namespace App;
1919

2020
use Timber\Timber;
21-
use Rareloop\Lumberjack\Post;
2221
use Rareloop\Lumberjack\Http\Responses\TimberResponse;
2322

2423
class PageHomeController
2524
{
2625
public function handle()
2726
{
28-
$context = Timber::get_context();
27+
$context = Timber::context();
2928

30-
$context['post'] = new Post;
29+
$context['post'] = Timber::get_post();
3130

3231
return new TimberResponse('home', $context);
3332
}

versioned_docs/version-v7/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ class IndexController
2626
{
2727
public function handle()
2828
{
29-
$context = Timber::get_context();
30-
$context['posts'] = Post::whereStatus('publish')
29+
$context = Timber::context();
30+
$context['posts'] = Post::builder()
31+
->whereStatus('publish')
3132
->limit(5)
3233
->get();
3334

versioned_docs/version-v7/the-basics/post-types.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,23 @@ sidebar_position: 4
99
Typically in WordPress when you're querying posts you get [`WP_Post`](https://codex.wordpress.org/Class_Reference/WP_Post) objects back. Timber have taken this a step further and return a `Timber/Post` object instead. This [has a ton of great helper methods and properties](https://timber.github.io/docs/reference/timber-post/) which makes it easier and more expressive to use.
1010

1111
```php
12-
use Timber\Post;
13-
14-
$post = new Post(1);
15-
$posts = Timber::get_posts($wpQueryArray);
12+
$post = Timber::get_post(1); // \Rareloop\Lumberjack\Post
13+
$posts = Timber::get_posts($wpQueryArray); // \Timber\PostQuery
1614
```
1715

18-
Lumberjack has its own Post object which makes it easier and more expressive to run queries.
16+
:::info Note on Timber v2
17+
Timber v2 deprecated direct instantiation in favour of the `Timber::get_post()` factory implementation. When you call `get_post()`, Timber uses the Class Map to return the correct class for that post.
18+
19+
**Lumberjack automatically registers your custom post types with the Timber Class Map**
20+
21+
See: [Timber: Class Maps](https://timber.github.io/docs/v2/guides/class-maps/#the-post-class-map)
22+
:::
23+
24+
Lumberjack post types provide a static `query` method to make this simpler.
1925

2026
```php
2127
use Rareloop\Lumberjack\Post;
2228

23-
$post = new Post(1);
2429
$collection = Post::query($wpQueryArray);
2530
```
2631

@@ -29,7 +34,6 @@ This becomes especially powerful when you start registering **Custom Post Types*
2934
```php
3035
use App\PostTypes\Product;
3136

32-
$post = new Product(1);
3337
$collection = Product::query($wpQueryArray);
3438
```
3539

versioned_docs/version-v7/the-basics/view-models.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class SingleController
2323
{
2424
public function handle()
2525
{
26-
$context = Timber::get_context();
27-
$post = new Post;
26+
$context = Timber::context();
27+
$post = Timber::get_post();
2828

2929
$date = new \DateTime($post->post_date);
3030

@@ -133,7 +133,6 @@ Now we can refactor our controller to use our view model instead:
133133

134134
namespace App;
135135

136-
use Rareloop\Lumberjack\Post;
137136
use Timber\Timber;
138137
use App\ViewModels\MediaCardViewModel;
139138
use Rareloop\Lumberjack\Http\Responses\TimberResponse;
@@ -142,8 +141,8 @@ class SingleController
142141
{
143142
public function handle()
144143
{
145-
$context = Timber::get_context();
146-
$post = new Post;
144+
$context = Timber::context();
145+
$post = Timber::get_post();
147146

148147
$context['card'] = new MediaCardViewModel($post);
149148

@@ -287,7 +286,6 @@ If your view model does not know about how to get data, then you will have to fe
287286

288287
namespace App;
289288

290-
use Rareloop\Lumberjack\Post;
291289
use Timber\Timber;
292290
use App\ViewModels\TestimonialViewModel;
293291
use Rareloop\Lumberjack\Http\Responses\TimberResponse;
@@ -296,8 +294,8 @@ class SingleController
296294
{
297295
public function handle()
298296
{
299-
$context = Timber::get_context();
300-
$post = new Post;
297+
$context = Timber::context();
298+
$post = Timber::get_post();
301299

302300
// Get the data from somewhere, for example from ACF
303301
// You would have to duplicate these two lines in each controller
@@ -366,7 +364,6 @@ And we can now refactor our controller to use the named constructor like so:
366364

367365
namespace App;
368366

369-
use Rareloop\Lumberjack\Post;
370367
use Timber\Timber;
371368
use App\ViewModels\TestimonialViewModel;
372369
use Rareloop\Lumberjack\Http\Responses\TimberResponse;
@@ -375,8 +372,8 @@ class SingleController
375372
{
376373
public function handle()
377374
{
378-
$context = Timber::get_context();
379-
$post = new Post;
375+
$context = Timber::context();
376+
$post = Timber::get_post();
380377

381378
$context['testimonial'] = TestimonialViewModel::forPost($post);
382379

@@ -391,6 +388,7 @@ And you could have the following named constructor:
391388

392389
- `latestPosts($limit = 3)`- which knows how to get the latest _n_ posts.
393390
- `relatedPosts(Post $post)`- which knows how to get posts related to given post
391+
394392
:::
395393

396394
## Using Hatchet

versioned_docs/version-v7/the-basics/wordpress-controllers.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,15 @@ In the files (i.e. controllers) that WordPress uses when it matches a route (e.g
1818
namespace App;
1919

2020
use Timber\Timber;
21-
use Rareloop\Lumberjack\Post;
2221
use Rareloop\Lumberjack\Http\Responses\TimberResponse;
2322

2423
class PageHomeController
2524
{
2625
public function handle()
2726
{
28-
$context = Timber::get_context();
27+
$context = Timber::context();
2928

30-
$context['post'] = new Post;
29+
$context['post'] = Timber::get_post();
3130

3231
return new TimberResponse('home', $context);
3332
}

versioned_docs/version-v8/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ class IndexController
2626
{
2727
public function handle()
2828
{
29-
$context = Timber::get_context();
30-
$context['posts'] = Post::whereStatus('publish')
29+
$context = Timber::context();
30+
$context['posts'] = Post::builder()
31+
->whereStatus('publish')
3132
->limit(5)
3233
->get();
3334

versioned_docs/version-v8/the-basics/post-types.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,23 @@ sidebar_position: 4
99
Typically in WordPress when you're querying posts you get [`WP_Post`](https://codex.wordpress.org/Class_Reference/WP_Post) objects back. Timber have taken this a step further and return a `Timber/Post` object instead. This [has a ton of great helper methods and properties](https://timber.github.io/docs/reference/timber-post/) which makes it easier and more expressive to use.
1010

1111
```php
12-
use Timber\Post;
13-
14-
$post = new Post(1);
15-
$posts = Timber::get_posts($wpQueryArray);
12+
$post = Timber::get_post(1); // \Rareloop\Lumberjack\Post
13+
$posts = Timber::get_posts($wpQueryArray); // \Timber\PostQuery
1614
```
1715

18-
Lumberjack has its own Post object which makes it easier and more expressive to run queries.
16+
:::info Note on Timber v2
17+
Timber v2 deprecated direct instantiation in favour of the `Timber::get_post()` factory implementation. When you call `get_post()`, Timber uses the Class Map to return the correct class for that post.
18+
19+
**Lumberjack automatically registers your custom post types with the Timber Class Map**
20+
21+
See: [Timber: Class Maps](https://timber.github.io/docs/v2/guides/class-maps/#the-post-class-map)
22+
:::
23+
24+
Lumberjack post types provide a static `query` method to make this simpler.
1925

2026
```php
2127
use Rareloop\Lumberjack\Post;
2228

23-
$post = new Post(1);
2429
$collection = Post::query($wpQueryArray);
2530
```
2631

@@ -29,7 +34,6 @@ This becomes especially powerful when you start registering **Custom Post Types*
2934
```php
3035
use App\PostTypes\Product;
3136

32-
$post = new Product(1);
3337
$collection = Product::query($wpQueryArray);
3438
```
3539

0 commit comments

Comments
 (0)