Skip to content

Commit a3fdcc1

Browse files
committed
Created base Post class
1 parent b8c7570 commit a3fdcc1

File tree

2 files changed

+114
-1
lines changed

2 files changed

+114
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"type": "project",
1111
"autoload": {
1212
"psr-4": {
13-
"WP-OOP\\": "src/"
13+
"OOPWP\\": "src/"
1414
}
1515
},
1616
"license": "MIT",

src/Posts/Post.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
namespace OOPWP;
4+
5+
class Post
6+
{
7+
protected $id;
8+
protected $date_format;
9+
10+
/**
11+
* Set up
12+
*
13+
* @param integer $id
14+
*/
15+
public function __construct(int $id = 0)
16+
{
17+
$this->id = $id;
18+
19+
if (!$this->getPost()) {
20+
throw new \Exception('Post not found.');
21+
}
22+
23+
$this->date_format = \get_option('date_format');
24+
}
25+
26+
/**
27+
* Get WP_Post object
28+
*
29+
* @return WP_Post
30+
*/
31+
protected function getPost()
32+
{
33+
return \get_post($this->id);
34+
}
35+
36+
/**
37+
* The slug
38+
*
39+
* @return string
40+
*/
41+
public function slug()
42+
{
43+
return ($this->getPost()->post_name ?? null);
44+
}
45+
46+
/**
47+
* The title
48+
*
49+
* @return string
50+
*/
51+
public function title()
52+
{
53+
return \get_the_title($this->id);
54+
}
55+
56+
/**
57+
* The exceprt
58+
*
59+
* @return string
60+
*/
61+
public function excerpt()
62+
{
63+
return \get_the_excerpt($this->id);
64+
}
65+
66+
/**
67+
* The publish date
68+
*
69+
* @param string $format
70+
* @return string
71+
*/
72+
public function publishDate($format = $this->date_format)
73+
{
74+
return \get_the_date($format, $this->id);
75+
}
76+
77+
/**
78+
* The modified date
79+
*
80+
* @param string $format
81+
* @return string
82+
*/
83+
public function modifiedDate($format = $this->date_format)
84+
{
85+
return \get_the_modified_time($format, $this->id);
86+
}
87+
88+
/**
89+
* The content
90+
*
91+
* @return string
92+
*/
93+
public function content()
94+
{
95+
$post_content = ($this->getPost()->post_content ?? null);
96+
97+
if (!$post_content) {
98+
return;
99+
}
100+
101+
return \apply_filters('the_content', $post_content);
102+
}
103+
104+
/**
105+
* Parent Post object
106+
*
107+
* @return Post
108+
*/
109+
public function parent()
110+
{
111+
return new Post(($this->getPost()->post_parent ?? 0));
112+
}
113+
}

0 commit comments

Comments
 (0)