User::get_object()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 3
b 0
f 1
1
<?php
2
/**
3
 * Wrapper for \WP_User.
4
 *
5
 * @package Classy\Models
6
 */
7
8
namespace Classy\Models;
9
10
use Classy\Basis;
11
12
/**
13
 * Class User.
14
 */
15
class User extends Basis {
16
17
	/**
18
	 * Current user id.
19
	 *
20
	 * @var int
21
	 */
22
	public $ID;
23
24
	/**
25
	 * User posts url.
26
	 *
27
	 * @var string
28
	 */
29
	public $link;
30
31
	/**
32
	 * User login (example: anrw).
33
	 *
34
	 * @var string
35
	 */
36
	public $user_login;
37
38
	/**
39
	 * User full name.
40
	 *
41
	 * @var string
42
	 */
43
	public $name;
44
45
	/**
46
	 * It's basically sanitized version of user login, used for permalinks.
47
	 *
48
	 * @var string
49
	 */
50
	public $user_nicename;
51
52
	/**
53
	 * User email.
54
	 *
55
	 * @var string
56
	 */
57
	public $user_email;
58
59
	/**
60
	 * Human-Friendly name, like: Andrew Tolochka.
61
	 *
62
	 * @var string
63
	 */
64
	public $display_name;
65
66
	/**
67
	 * Stores current user object.
68
	 *
69
	 * @var object
70
	 */
71
	private $object;
72
73
	/**
74
	 * Main constructor function. Requires user id.
75
	 *
76
	 * @param int $uid User id.
77
	 */
78
	public function __construct( $uid = null ) {
79
		$this->ID = $this->verify_id( $uid );
80
81
		$this->init();
82
	}
83
84
	/**
85
	 * Verify user id.
86
	 *
87
	 * @param int $uid User id.
88
	 *
89
	 * @return int
90
	 */
91
	private function verify_id( $uid ) {
92
		// @todo: Realize this method.
93
		return $uid;
94
	}
95
96
	/**
97
	 * Initialises User Instance.
98
	 */
99
	private function init() {
100
		$this->object = $this->get_object();
101
102
		$this->import( $this->object );
103
104
		$this->setup_user_name();
105
	}
106
107
	/**
108
	 * Returns user object.
109
	 *
110
	 * @return object
111
	 */
112
	private function get_object() {
113
		return get_user_by( 'id', $this->ID );
114
	}
115
116
	/**
117
	 * Returns user first name.
118
	 *
119
	 * @return string
120
	 */
121
	public function first_name() {
122
		return $this->object->first_name;
123
	}
124
125
	/**
126
	 * Returns user display name.
127
	 *
128
	 * @return string
129
	 */
130
	public function display_name() {
131
		return $this->display_name;
132
	}
133
134
	/**
135
	 * Returns user full name.
136
	 *
137
	 * @return string
138
	 */
139
	public function name() {
140
		return $this->name;
141
	}
142
143
	/**
144
	 * Returns user user_login.
145
	 *
146
	 * @return string
147
	 */
148
	public function user_login() {
149
		return $this->user_login;
150
	}
151
152
	/**
153
	 * Returns user email.
154
	 *
155
	 * @return string
156
	 */
157
	public function email() {
158
		return $this->user_email;
159
	}
160
161
	/**
162
	 * Returns author posts url.
163
	 *
164
	 * @return string
165
	 */
166
	public function link() {
167
		if ( ! $this->link ) {
168
			$this->link = get_author_posts_url( $this->ID );
169
		}
170
171
		return $this->link;
172
	}
173
174
	/**
175
	 * Setup user name and display name.
176
	 */
177
	private function setup_user_name() {
178
		$this->name = 'Anonymous';
179
		if ( isset( $this->object->first_name ) && isset( $this->object->last_name ) ) {
180
			$this->name = $this->object->first_name . ' ' . $this->object->last_name;
181
		}
182
	}
183
}
184