Skip to content

Commit 9669b00

Browse files
authored
Merge pull request #1 from SebKay/posts
Posts
2 parents 0e4c22f + 2dad423 commit 9669b00

File tree

4 files changed

+158
-4
lines changed

4 files changed

+158
-4
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# WP OOP
1+
# OOP WP
22
A simple library of OOP style helpers for WordPress theme and plugin development.
33

44
## What is this?
5-
WP OOP is a package that give you a load of handy classes and methods to use when building things in WordPress.
5+
OOP WP is a package that give you a load of handy classes and methods to use when building things in WordPress.
66

77
It can give you well-formatted classes like User and Post which can then be extended into sub-classes in your own projects.

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "sebkay/oop-wp",
3+
"version" : "1.0.0",
34
"authors": [
45
{
56
"name": "Seb Kay",
@@ -10,7 +11,7 @@
1011
"type": "project",
1112
"autoload": {
1213
"psr-4": {
13-
"WP-OOP\\": "src/"
14+
"OOPWP\\": "src/"
1415
}
1516
},
1617
"license": "MIT",

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Posts/Post.php

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
3+
namespace OOPWP\Posts;
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)
16+
{
17+
$this->id = ($id ?: -1);
18+
19+
$this->date_format = \get_option('date_format');
20+
}
21+
22+
/**
23+
* The ID
24+
*
25+
* @return int
26+
*/
27+
public function id()
28+
{
29+
return $this->id;
30+
}
31+
32+
/**
33+
* Get WP_Post object
34+
*
35+
* @return WP_Post
36+
*/
37+
protected function getPost()
38+
{
39+
return \get_post($this->id());
40+
}
41+
42+
/**
43+
* The url
44+
*
45+
* @return string
46+
*/
47+
public function url()
48+
{
49+
return \get_permalink($this->id());
50+
}
51+
52+
/**
53+
* The slug
54+
*
55+
* @return string
56+
*/
57+
public function slug()
58+
{
59+
return ($this->getPost()->post_name ?? null);
60+
}
61+
62+
/**
63+
* The status
64+
*
65+
* @return string
66+
*/
67+
public function status()
68+
{
69+
return \get_post_status($this->id());
70+
}
71+
72+
/**
73+
* The format
74+
*
75+
* @return string
76+
*/
77+
public function format()
78+
{
79+
return (\get_post_format($this->id()) ?: 'standard');
80+
}
81+
82+
/**
83+
* The title
84+
*
85+
* @return string
86+
*/
87+
public function title()
88+
{
89+
return \get_the_title($this->id());
90+
}
91+
92+
/**
93+
* The excerpt
94+
*
95+
* @return string
96+
*/
97+
public function excerpt()
98+
{
99+
return \get_the_excerpt($this->id());
100+
}
101+
102+
/**
103+
* The publish date
104+
*
105+
* @param string $format
106+
* @return string
107+
*/
108+
public function publishDate($format = null)
109+
{
110+
$format = ($format ?: $this->date_format);
111+
112+
return \get_the_date($format, $this->id());
113+
}
114+
115+
/**
116+
* The modified date
117+
*
118+
* @param string $format
119+
* @return string
120+
*/
121+
public function modifiedDate($format = null)
122+
{
123+
$format = ($format ?: $this->date_format);
124+
125+
return \get_the_modified_time($format, $this->id());
126+
}
127+
128+
/**
129+
* The content
130+
*
131+
* @return string
132+
*/
133+
public function content()
134+
{
135+
$post_content = ($this->getPost()->post_content ?? null);
136+
137+
if (!$post_content) {
138+
return;
139+
}
140+
141+
return \apply_filters('the_content', $post_content);
142+
}
143+
144+
/**
145+
* Parent Post object
146+
*
147+
* @return Post
148+
*/
149+
public function parent()
150+
{
151+
return new Post($this->getPost()->post_parent);
152+
}
153+
}

0 commit comments

Comments
 (0)