Skip to content

Commit 2f7722d

Browse files
committed
Refactor User class to get properties individually, instead of always all in class construction
1 parent 1eca2af commit 2f7722d

File tree

2 files changed

+77
-116
lines changed

2 files changed

+77
-116
lines changed

src/PostTypes/Post.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Post
88
{
99
public $id;
1010

11-
protected $WP_Post;
11+
protected \WP_Post $WP_Post;
1212

1313
public string $title = '';
1414
public string $url = '';
@@ -24,6 +24,8 @@ class Post
2424

2525
public function __construct(int $id = 0)
2626
{
27+
$this->id = $id;
28+
2729
$this->WP_Post = \get_post($this->id);
2830
}
2931

src/User.php

Lines changed: 74 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -6,164 +6,123 @@
66

77
class User
88
{
9-
protected $id;
10-
11-
protected $WP_User;
12-
13-
/**
14-
* @var string
15-
*/
16-
protected $username;
17-
18-
/**
19-
* @var string
20-
*/
21-
protected $nicename;
22-
23-
/**
24-
* @var string
25-
*/
26-
protected $displayName;
27-
28-
/**
29-
* @var string
30-
*/
31-
protected $email;
32-
33-
/**
34-
* @var string
35-
*/
36-
protected $url;
37-
38-
/**
39-
* @var string
40-
*/
41-
protected $registeredDate;
42-
43-
/**
44-
* @var string
45-
*/
46-
protected $first_name;
47-
48-
/**
49-
* @var string
50-
*/
51-
protected $last_name;
52-
53-
/**
54-
* @var string
55-
*/
56-
protected $nickname;
57-
58-
/**
59-
* @var string
60-
*/
61-
protected $description;
62-
63-
/**
64-
* Set up
65-
*
66-
* @param integer $id
67-
*/
68-
public function __construct(int $id)
9+
public int $id;
10+
11+
protected \WP_User $WP_User;
12+
13+
public string $username = '';
14+
public string $nicename = '';
15+
public string $displayName = '';
16+
public string $email = '';
17+
public string $url = '';
18+
public Carbon $registeredDate;
19+
public string $first_name = '';
20+
public string $last_name = '';
21+
public string $full_name = '';
22+
public string $nickname = '';
23+
public string $description = '';
24+
25+
public function __construct(int $id = 0)
6926
{
70-
$this->id = $id ?: -1;
71-
$this->WP_User = \get_user_by('id', $this->id());
72-
73-
if (!$this->WP_User) {
74-
return;
75-
}
76-
77-
//---- Data
78-
$this->username = $this->WPUser()->data->user_login;
79-
$this->nicename = $this->WPUser()->data->user_nicename;
80-
$this->displayName = $this->WPUser()->data->display_name;
81-
$this->email = $this->WPUser()->data->user_email;
82-
$this->url = $this->WPUser()->data->user_url;
83-
$this->registeredDate = $this->WPUser()->data->user_registered;
84-
85-
//---- Meta
86-
$this->first_name = $this->meta('first_name');
87-
$this->last_name = $this->meta('last_name');
88-
$this->nickname = $this->meta('nickname');
89-
$this->description = $this->meta('description');
90-
}
27+
$this->id = $id;
9128

92-
public function id()
93-
{
94-
return $this->id;
29+
$this->WP_User = \get_user_by('id', $this->id);
30+
31+
$this->full_name = \trim("{$this->first_name} {$this->last_name}");
9532
}
9633

97-
public function WPUser()
34+
public function exists()
9835
{
99-
return $this->WP_User;
36+
return $this->WP_User->exists();
10037
}
10138

10239
/**
10340
* @param string $key Meta field key
10441
* @param bool $single Whether to return a single value or array
42+
*
10543
* @return mixed
10644
*/
107-
public function meta(string $key, bool $single = true)
45+
public function getMeta(string $key, bool $single = true)
10846
{
109-
return \get_user_meta($this->id(), $key, $single);
47+
return \get_user_meta($this->id, $key, $single);
11048
}
11149

112-
public function firstName()
50+
public function withAll()
11351
{
114-
return $this->first_name;
52+
$this
53+
->withUsername()
54+
->withNicename()
55+
->withEmail()
56+
->withUrl()
57+
->withRegisteredDate()
58+
->withFirstName()
59+
->withLastName()
60+
->withNickname()
61+
->withDescription();
62+
63+
return $this;
11564
}
11665

117-
public function lastName()
66+
public function withUsername()
11867
{
119-
return $this->last_name;
120-
}
68+
$this->username = $this->WP_User->data->user_login;
12169

122-
public function fullName()
123-
{
124-
return \trim("{$this->firstName()} {$this->lastName()}");
70+
return $this;
12571
}
12672

127-
public function nickname()
73+
public function withNicename()
12874
{
129-
return $this->nickname;
75+
$this->displayName = $this->WP_User->data->display_name;
76+
77+
return $this;
13078
}
13179

132-
public function description()
80+
public function withEmail()
13381
{
134-
return \wpautop($this->description);
82+
$this->email = $this->WP_User->data->user_email;
83+
84+
return $this;
13585
}
13686

137-
public function username()
87+
public function withUrl()
13888
{
139-
return $this->username;
89+
$this->url = $this->WP_User->data->user_url;
90+
91+
return $this;
14092
}
14193

142-
/**
143-
* * A URL friendly version of username()
144-
*/
145-
public function nicename()
94+
public function withRegisteredDate()
14695
{
147-
return $this->nicename;
96+
$this->registeredDate = new Carbon($this->WP_User->data->user_registered);
97+
98+
return $this;
14899
}
149100

150-
public function displayName()
101+
public function withFirstName()
151102
{
152-
return $this->displayName;
103+
$this->first_name = $this->getMeta('first_name');
104+
105+
return $this;
153106
}
154107

155-
public function email()
108+
public function withLastName()
156109
{
157-
return $this->email;
110+
$this->last_name = $this->getMeta('last_name');
111+
112+
return $this;
158113
}
159114

160-
public function url()
115+
public function withNickname()
161116
{
162-
return $this->url;
117+
$this->nickname = $this->getMeta('nickname');
118+
119+
return $this;
163120
}
164121

165-
public function registredDate()
122+
public function withDescription()
166123
{
167-
return new Carbon($this->registeredDate);
124+
$this->description = \wpautop($this->getMeta('description'));
125+
126+
return $this;
168127
}
169128
}

0 commit comments

Comments
 (0)