Completed
Push — master ( f191a4...75c234 )
by Andrew
02:27
created

ClassyComment::content()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php 
2
3
class ClassyComment extends ClassyBasis {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
5
	/**
6
	 * Comment ID
7
	 * @var int
8
	 */
9
	public $comment_ID;
10
11
	/**
12
	 * Comment post ID
13
	 * @var int
14
	 */
15
	public $comment_post_ID;
16
17
	/**
18
	 * Comment Author name
19
	 * @var string
20
	 */
21
	public $comment_author;
22
23
	/**
24
	 * Comment author email
25
	 * @var string
26
	 */
27
	public $comment_author_email;
28
29
	/**
30
	 * Comment author link
31
	 * @var string
32
	 */
33
	public $comment_author_url;
34
35
	/**
36
	 * Comment
37
	 * @var string
38
	 */
39
	public $comment_content;
40
41
	/**
42
	 * Child comments
43
	 * @var array
44
	 */
45
	protected $children = array();
46
47
	/**
48
	 * Checks if provided arg is instance of WP_Comment and inits it
49
	 * 
50
	 * @param WP_Comment $item
51
	 */
52
	public function __construct($item) {
53
54
		if (is_a($item, 'WP_Comment')) {
55
56
			$this->import($item);
57
			
58
		}
59
60
	}
61
62
	/**
63
	 * Returns ClassyUser object of comment author
64
	 * 
65
	 * @return object ClassyUser
66
	 */
67
	public function author() {
68
		
69
		if ($this->user_id) {
70
			
71
			return new ClassyUser($this->user_id);
0 ignored issues
show
Bug introduced by
The property user_id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
72
		
73
		} else {
74
75
			$author = new ClassyUser(0);
76
			
77
			if (isset($this->comment_author) && $this->comment_author) {
78
			
79
				$author->name = $this->comment_author;
80
			
81
			}
82
83
		}
84
85
		return $author;
86
87
	}
88
89
	/**
90
	 * Returns comment content
91
	 * 
92
	 * @return string
93
	 */
94
	public function content() {
95
		return apply_filters('get_comment_text ', $this->comment_content);
96
	}
97
98
	/**
99
	 * Return true if comment is approved
100
	 * 
101
	 * @return boolean
102
	 */
103
	public function approved() {
104
		return $this->comment_approved;
0 ignored issues
show
Bug introduced by
The property comment_approved does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
105
	}
106
107
	/**
108
	 * Returns comment date
109
	 * 
110
	 * @param  string $date_format
111
	 * @return string
112
	 */
113
	public function date( $date_format = '' ) {
114
		$df = $date_format ? $date_format : get_option('date_format');
115
		$the_date = (string)mysql2date($df, $this->comment_date);
0 ignored issues
show
Bug introduced by
The property comment_date does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
116
117
		return apply_filters('get_comment_date ', $the_date, $df);
118
	}
119
120
	/**
121
	 * Returns true if comment has parent
122
	 * 
123
	 * @return boolean
124
	 */
125
	public function has_parent() {
126
		return $this->comment_parent > 0;
0 ignored issues
show
Bug introduced by
The property comment_parent does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
127
	}
128
129
	/**
130
	 * Shows gravatar
131
	 * 
132
	 * @param  integer $size    avatar image size
133
	 * @param  string $default
134
	 * @return string           
135
	 */
136
	public function avatar($size = 92, $default = '') {
137
		
138
		if (!get_option('show_avatars')) {
139
			return false;
140
		}
141
		
142
		if (!is_numeric($size)) $size = '92';
143
144
145
		$email = $this->get_avatar_email();
146
		$email_hash = '';
147
		
148
		if (!empty($email)) {
149
			$email_hash = md5(strtolower(trim($email)));
150
		}
151
152
		$host = $this->get_avatar_host($email_hash);
153
		$default = $this->get_default_avatar($default, $email, $size, $host);
154
155
		if (!empty($email)) {
156
			$avatar = $this->get_avatar_url($default, $host, $email_hash, $size);
157
		} else {
158
			$avatar = $default;
159
		}
160
161
		return $avatar;
162
163
	}
164
165
	/**
166
	 * Returns email address that will be used for generating avatar
167
	 * 
168
	 * @return string
169
	 */
170
	protected function get_avatar_email() {
171
172
		$id = (int)$this->user_id;
173
		$user = get_userdata($id);
174
175
		if ($user) {
176
			$email = $user->user_email;
177
		} else {
178
			$email = $this->comment_author_email;
179
		}
180
		
181
		return $email;
182
183
	}
184
185
	/**
186
	 * Returns default avatar url
187
	 * 
188
	 * @param  string $default 
189
	 * @param  string $email   
190
	 * @param  int $size    
191
	 * @param  string $host    
192
	 * @return string
193
	 */
194
	protected function get_default_avatar($default, $email, $size, $host) {
195
		
196
		if (substr($default, 0, 1) == '/') {
197
			$default = home_url() . $default;
198
		}
199
200
		if (empty($default)) {
201
			$avatar_default = get_option('avatar_default');
202
			if (empty($avatar_default)) {
203
				$default = 'mystery';
204
			} else {
205
				$default = $avatar_default;
206
			}
207
		}
208
209
		if ('mystery' == $default) {
210
			$default = $host . '/avatar/ad516503a11cd5ca435acc9bb6523536?s=' . $size;
211
			// ad516503a11cd5ca435acc9bb6523536 == md5('[email protected]')
212
		} else if ('blank' == $default) {
213
			$default = $email ? 'blank' : includes_url('images/blank.gif');
214
		} else if (!empty($email) && 'gravatar_default' == $default) {
215
			$default = '';
216
		} else if ('gravatar_default' == $default) {
217
			$default = $host . '/avatar/?s=' . $size;
218
		} else if (empty($email) && !strstr($default, 'http://')) {
219
			$default = $host . '/avatar/?d=' . $default . '&amp;s=' . $size;
220
		}
221
222
		return $default;
223
224
	}
225
226
	/**
227
	 * Returns gravatar host
228
	 * 
229
	 * @param  string $email_hash 
230
	 * @return string
231
	 */
232
	protected function get_avatar_host($email_hash) {
233
		
234
		if (is_ssl()) {
235
			$host = 'https://secure.gravatar.com';
236
		} else {
237
			if (!empty($email_hash)) {
238
				$host = sprintf("http://%d.gravatar.com", (hexdec($email_hash[0]) % 2));
239
			} else {
240
				$host = 'http://0.gravatar.com';
241
			}
242
		}
243
244
		return $host;
245
246
	}
247
248
	/**
249
	 * Returns avatar url
250
	 * 
251
	 * @param  string $default    
252
	 * @param  string $host       
253
	 * @param  string $email_hash 
254
	 * @param  int $size       
255
	 * @return string
256
	 */
257
	protected function get_avatar_url($default, $host, $email_hash, $size) {
258
		
259
		$_return = $host . '/avatar/' . $email_hash . '?s=' . $size . '&amp;d=' . urlencode($default);
260
		$rating = get_option('avatar_rating');
261
		
262
		if (!empty($rating)) {
263
			$_return .= '&amp;r=' . $rating;
264
		}
265
		
266
		return str_replace('&#038;', '&amp;', esc_url($_return));
267
	
268
	}
269
270
	/**
271
	 * Adds child to current ClassyComment
272
	 * 
273
	 * @param ClassyComment $comment 
274
	 */
275
	public function add_child($comment) {
0 ignored issues
show
Unused Code introduced by
The parameter $comment is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
276
277
		$this->children[] = $item;
0 ignored issues
show
Bug introduced by
The variable $item does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
278
		$item->level = $this->level + 1;
0 ignored issues
show
Bug introduced by
The property level does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
279
280
		if ($item->children) {
281
			$this->update_child_levels();
282
		}
283
284
	}
285
286
287
	/**
288
	 * Updates children nesting level param
289
	 * 
290
	 * @return boolean
291
	 */
292 View Code Duplication
	protected function update_child_levels() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
293
294
		if (is_array($this->children)) {
295
		
296
			foreach( $this->children as $child ) {
297
				$child->level = $this->level + 1;
298
				$child->update_child_levels();
299
			}
300
		
301
			return true;
302
		
303
		}
304
305
		return false;
306
307
	}
308
309
310
311
}