Skip to content

Commit 1eca2af

Browse files
committed
Refactor so all elements of a Post can be loaded individually, not always all at once during class construction
1 parent 3a3d407 commit 1eca2af

File tree

1 file changed

+100
-34
lines changed

1 file changed

+100
-34
lines changed

src/PostTypes/Post.php

Lines changed: 100 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,55 +10,121 @@ class Post
1010

1111
protected $WP_Post;
1212

13-
protected $options = [
14-
'thumbnail_size' => 'post-thumbnail',
15-
'thumbnail_classes' => '',
16-
];
17-
1813
public string $title = '';
1914
public string $url = '';
2015
public string $slug = '';
2116
public string $content = '';
2217
public string $status = '';
2318
public string $format = '';
2419
public string $excerpt = '';
25-
public Carbon $date;
26-
public Carbon $dateModified;
20+
public Carbon $publishedDate;
21+
public Carbon $modifiedDate;
2722
public \WP_Post $parent;
28-
public string $thumbnail = '';
29-
30-
/**
31-
* Set up
32-
*
33-
* @param integer $id
34-
*/
35-
public function __construct(int $id)
23+
public string $thumbnailHtml = '';
24+
25+
public function __construct(int $id = 0)
3626
{
37-
$this->id = $id ?: 0;
3827
$this->WP_Post = \get_post($this->id);
28+
}
29+
30+
public function exists()
31+
{
32+
return $this->WP_Post ? true : false;
33+
}
34+
35+
public function withAll()
36+
{
37+
$this
38+
->withTitle()
39+
->withUrl()
40+
->withSlug()
41+
->withContent()
42+
->withStatus()
43+
->withFormat()
44+
->withExcerpt()
45+
->withPublishedDate()
46+
->withModifiedDate()
47+
->withParent();
48+
49+
return $this;
50+
}
51+
52+
public function withTitle()
53+
{
54+
$this->title = \get_the_title($this->WP_Post);
55+
56+
return $this;
57+
}
58+
59+
public function withUrl()
60+
{
61+
$this->url = \get_permalink($this->WP_Post);
62+
63+
return $this;
64+
}
65+
66+
public function withSlug()
67+
{
68+
$this->slug = $this->WP_Post->post_name;
3969

40-
if (!$this->WP_Post) {
41-
return;
42-
}
43-
44-
$this->title = \get_the_title($this->WP_Post);
45-
$this->url = \get_permalink($this->WP_Post);
46-
$this->slug = $this->WP_Post->post_name;
47-
$this->content = \apply_filters('the_content', $this->WP_Post->post_content);
48-
$this->status = $this->WP_Post->post_status;
49-
$this->format = \get_post_format($this->WP_Post) ?: 'standard';
50-
$this->excerpt = \get_the_excerpt($this->WP_Post);
51-
$this->date = new Carbon($this->WP_Post->post_date);
52-
$this->dateModified = new Carbon($this->WP_Post->post_modified);
53-
$this->parent = new self($this->WP_Post->post_parent);
70+
return $this;
71+
}
72+
73+
public function withContent()
74+
{
75+
$this->content = \apply_filters('the_content', $this->WP_Post->post_content);
76+
77+
return $this;
78+
}
79+
80+
public function withExcerpt()
81+
{
82+
$this->excerpt = \get_the_excerpt($this->WP_Post);
83+
84+
return $this;
85+
}
86+
87+
public function withStatus()
88+
{
89+
$this->status = $this->WP_Post->post_status;
90+
91+
return $this;
92+
}
93+
94+
public function withFormat()
95+
{
96+
$this->format = \get_post_format($this->WP_Post) ?: 'standard';
97+
98+
return $this;
99+
}
100+
101+
public function withPublishedDate()
102+
{
103+
$this->publishedDate = new Carbon($this->WP_Post->post_date);
104+
105+
return $this;
106+
}
107+
108+
public function withModifiedDate()
109+
{
110+
$this->modifiedDate = new Carbon($this->WP_Post->post_modified);
111+
112+
return $this;
113+
}
114+
115+
public function withParent()
116+
{
117+
$this->parent = new self($this->WP_Post->post_parent);
118+
119+
return $this;
54120
}
55121

56-
public function withThumbnail()
122+
public function withThumbnail(string $size = 'post-thumbnail', string $css_classes = '')
57123
{
58-
$this->thumbnail = \get_the_post_thumbnail(
124+
$this->thumbnailHtml = \get_the_post_thumbnail(
59125
$this->WP_Post,
60-
$this->options['thumbnail_size'],
61-
$this->options['thumbnail_classes']
126+
$size,
127+
$css_classes
62128
);
63129

64130
return $this;

0 commit comments

Comments
 (0)