Skip to content

Commit f8dbf48

Browse files
committed
Add User::WP_User and Post::WP_Post methods for accessing the WP objects
1 parent b8db4fe commit f8dbf48

2 files changed

Lines changed: 36 additions & 22 deletions

File tree

src/PostTypes/Post.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ class Post
66
{
77
protected $id;
88

9+
protected $WP_Post;
10+
911
/**
1012
* @var string
1113
*/
@@ -60,30 +62,35 @@ class Post
6062
*/
6163
public function __construct(int $id)
6264
{
63-
$this->id = $id ?: -1;
64-
$WP_Post = \get_post($this->id());
65+
$this->id = $id ?: -1;
66+
$this->WP_Post = \get_post($this->id());
6567

66-
if (!$WP_Post) {
68+
if (!$this->WP_Post) {
6769
return;
6870
}
6971

70-
$this->title = \get_the_title($WP_Post);
71-
$this->url = \get_permalink($WP_Post);
72-
$this->slug = $WP_Post->post_name;
73-
$this->content = $WP_Post->post_content;
74-
$this->status = $WP_Post->post_status;
75-
$this->format = \get_post_format($WP_Post) ?: 'standard';
76-
$this->excerpt = \get_the_excerpt($WP_Post);
77-
$this->date = $WP_Post->post_date;
78-
$this->dateModified = $WP_Post->post_modified;
79-
$this->parent = new self($WP_Post->post_parent);
72+
$this->title = \get_the_title($this->WP_Post);
73+
$this->url = \get_permalink($this->WP_Post);
74+
$this->slug = $this->WP_Post->post_name;
75+
$this->content = $this->WP_Post->post_content;
76+
$this->status = $this->WP_Post->post_status;
77+
$this->format = \get_post_format($this->WP_Post) ?: 'standard';
78+
$this->excerpt = \get_the_excerpt($this->WP_Post);
79+
$this->date = $this->WP_Post->post_date;
80+
$this->dateModified = $this->WP_Post->post_modified;
81+
$this->parent = new self($this->WP_Post->post_parent);
8082
}
8183

8284
public function id()
8385
{
8486
return $this->id;
8587
}
8688

89+
public function WP_Post()
90+
{
91+
return $this->WP_Post;
92+
}
93+
8794
public function url()
8895
{
8996
return $this->url;

src/User.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ class User
66
{
77
protected $id;
88

9+
protected $WP_User;
10+
911
/**
1012
* @var string
1113
*/
@@ -63,20 +65,20 @@ class User
6365
*/
6466
public function __construct(int $id)
6567
{
66-
$this->id = $id ?: -1;
67-
$WP_User = \get_user_by('id', $this->id());
68+
$this->id = $id ?: -1;
69+
$this->WP_User = \get_user_by('id', $this->id());
6870

69-
if (!$WP_User) {
71+
if (!$this->WP_User) {
7072
return;
7173
}
7274

7375
//---- Data
74-
$this->username = $WP_User->data->user_login;
75-
$this->nicename = $WP_User->data->user_nicename;
76-
$this->displayName = $WP_User->data->display_name;
77-
$this->email = $WP_User->data->user_email;
78-
$this->url = $WP_User->data->user_url;
79-
$this->registeredDate = $WP_User->data->user_registered;
76+
$this->username = $this->WP_User->data->user_login;
77+
$this->nicename = $this->WP_User->data->user_nicename;
78+
$this->displayName = $this->WP_User->data->display_name;
79+
$this->email = $this->WP_User->data->user_email;
80+
$this->url = $this->WP_User->data->user_url;
81+
$this->registeredDate = $this->WP_User->data->user_registered;
8082

8183
//---- Meta
8284
$this->first_name = $this->meta('first_name');
@@ -90,6 +92,11 @@ public function id()
9092
return $this->id;
9193
}
9294

95+
public function WP_User()
96+
{
97+
return $this->WP_User;
98+
}
99+
93100
/**
94101
* @param string $key Meta field key
95102
* @param bool $single Whether to return a single value or array

0 commit comments

Comments
 (0)