Completed
Push — master ( bc6754...ed64c2 )
by
unknown
07:19
created

Comment::get_default_avatar()   C

Complexity

Conditions 12
Paths 42

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 12
eloc 20
c 1
b 0
f 1
nc 42
nop 4
dl 0
loc 31
rs 5.1612

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Classy;
3
4
class Comment extends Basis {
5
6
	/**
7
	 * Comment ID
8
	 * @var int
9
	 */
10
	public $comment_ID;
11
12
	/**
13
	 * Comment post ID
14
	 * @var int
15
	 */
16
	public $comment_post_ID;
17
18
	/**
19
	 * Comment Author name
20
	 * @var string
21
	 */
22
	public $comment_author;
23
24
	/**
25
	 * Comment author email
26
	 * @var string
27
	 */
28
	public $comment_author_email;
29
30
	/**
31
	 * Comment author link
32
	 * @var string
33
	 */
34
	public $comment_author_url;
35
36
	/**
37
	 * Comment
38
	 * @var string
39
	 */
40
	public $comment_content;
41
42
	/**
43
	 * Comment approved
44
	 * @var boolean
45
	 */
46
	public $comment_approved;
47
48
49
	/**
50
	 * Comment date
51
	 * @var string
52
	 */
53
	public $comment_date;
54
55
	/**
56
	 * User Id
57
	 * @var int
58
	 */
59
	public $user_id;
60
61
	/**
62
	 * Comment nested Level
63
	 * @var int
64
	 */
65
	public $level;
66
67
	/**
68
	 * Comment Parent Id
69
	 * @var int
70
	 */
71
	public $comment_parent;
72
73
	/**
74
	 * Child comments
75
	 * @var array
76
	 */
77
	protected $children = array();
78
79
	/**
80
	 * Checks if provided arg is instance of WP_Comment and inits it
81
	 *
82
	 * @param \WP_Comment $item
83
	 */
84
	public function __construct( $item ) {
85
86
		if ( is_a( $item, '\WP_Comment' ) ) {
87
88
			$this->import( $item );
89
90
		}
91
92
	}
93
94
	/**
95
	 * Returns User object of comment author
96
	 *
97
	 * @return object User
98
	 */
99
	public function author() {
100
101
		if ( $this->user_id ) {
102
103
			return new Models\User( $this->user_id );
104
105
		} else {
106
107
			$author = new Models\User( 0 );
108
109
			if ( isset( $this->comment_author ) && $this->comment_author ) {
110
111
				$author->name = $this->comment_author;
112
113
			}
114
		}
115
116
		return $author;
117
118
	}
119
120
	/**
121
	 * Returns comment content
122
	 *
123
	 * @return string
124
	 */
125
	public function content() {
126
		return apply_filters( 'get_comment_text ', $this->comment_content );
127
	}
128
129
	/**
130
	 * Return true if comment is approved
131
	 *
132
	 * @return boolean
133
	 */
134
	public function approved() {
135
		return $this->comment_approved;
136
	}
137
138
	/**
139
	 * Returns comment date
140
	 *
141
	 * @param  string $date_format
142
	 * @return string
143
	 */
144
	public function date( $date_format = '' ) {
145
		$df = $date_format ? $date_format : get_option( 'date_format' );
146
		$the_date = (string) mysql2date( $df, $this->comment_date );
147
148
		return apply_filters( 'get_comment_date ', $the_date, $df );
149
	}
150
151
	/**
152
	 * Returns true if comment has parent
153
	 *
154
	 * @return boolean
155
	 */
156
	public function has_parent() {
157
		return $this->comment_parent > 0;
158
	}
159
160
	/**
161
	 * Shows gravatar
162
	 *
163
	 * @param  integer $size    avatar image size
164
	 * @param  string $default
165
	 * @return string
166
	 */
167
	public function avatar( $size = 92, $default = '' ) {
168
169
		if ( ! get_option( 'show_avatars' ) ) {
170
			return false;
171
		}
172
173
		if ( ! is_numeric( $size ) ) { $size = '92'; }
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $size. This often makes code more readable.
Loading history...
174
175
		$email = $this->get_avatar_email();
176
		$email_hash = '';
177
178
		if ( ! empty( $email ) ) {
179
			$email_hash = md5( strtolower( trim( $email ) ) );
180
		}
181
182
		$host = $this->get_avatar_host( $email_hash );
183
		$default = $this->get_default_avatar( $default, $email, $size, $host );
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $default. This often makes code more readable.
Loading history...
184
185
		if ( ! empty( $email ) ) {
186
			$avatar = $this->get_avatar_url( $default, $host, $email_hash, $size );
187
		} else {
188
			$avatar = $default;
189
		}
190
191
		return $avatar;
192
193
	}
194
195
	/**
196
	 * Returns email address that will be used for generating avatar
197
	 *
198
	 * @return string
199
	 */
200
	protected function get_avatar_email() {
201
202
		$id = (int) $this->user_id;
203
		$user = get_userdata( $id );
204
205
		if ( $user ) {
206
			$email = $user->user_email;
207
		} else {
208
			$email = $this->comment_author_email;
209
		}
210
211
		return $email;
212
213
	}
214
215
	/**
216
	 * Returns default avatar url
217
	 *
218
	 * @param  string $default
219
	 * @param  string $email
220
	 * @param  int $size
221
	 * @param  string $host
222
	 * @return string
223
	 */
224
	protected function get_default_avatar( $default, $email, $size, $host ) {
225
226
		if ( '/' === substr( $default, 0, 1 ) ) {
227
			$default = home_url() . $default;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $default. This often makes code more readable.
Loading history...
228
		}
229
230
		if ( empty( $default ) ) {
231
			$avatar_default = get_option( 'avatar_default' );
232
			if ( empty( $avatar_default ) ) {
233
				$default = 'mystery';
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $default. This often makes code more readable.
Loading history...
234
			} else {
235
				$default = $avatar_default;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $default. This often makes code more readable.
Loading history...
236
			}
237
		}
238
239
		if ( 'mystery' === $default ) {
240
			$default = $host . '/avatar/ad516503a11cd5ca435acc9bb6523536?s=' . $size;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $default. This often makes code more readable.
Loading history...
241
			// ad516503a11cd5ca435acc9bb6523536 == md5('[email protected]')
242
		} else if ( 'blank' === $default ) {
243
			$default = $email ? 'blank' : includes_url( 'images/blank.gif' );
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $default. This often makes code more readable.
Loading history...
244
		} else if ( ! empty( $email ) && 'gravatar_default' === $default ) {
245
			$default = '';
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $default. This often makes code more readable.
Loading history...
246
		} else if ( 'gravatar_default' === $default ) {
247
			$default = $host . '/avatar/?s=' . $size;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $default. This often makes code more readable.
Loading history...
248
		} else if ( empty( $email ) && ! strstr( $default, 'http://' ) ) {
249
			$default = $host . '/avatar/?d=' . $default . '&amp;s=' . $size;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $default. This often makes code more readable.
Loading history...
250
		}
251
252
		return $default;
253
254
	}
255
256
	/**
257
	 * Returns gravatar host
258
	 *
259
	 * @param  string $email_hash
260
	 * @return string
261
	 */
262
	protected function get_avatar_host( $email_hash ) {
263
264
		if ( is_ssl() ) {
265
			$host = 'https://secure.gravatar.com';
266
		} else {
267
			if ( ! empty( $email_hash ) ) {
268
				$host = sprintf( 'http://%d.gravatar.com', (hexdec( $email_hash[0] ) % 2) );
269
			} else {
270
				$host = 'http://0.gravatar.com';
271
			}
272
		}
273
274
		return $host;
275
276
	}
277
278
	/**
279
	 * Returns avatar url
280
	 *
281
	 * @param  string $default
282
	 * @param  string $host
283
	 * @param  string $email_hash
284
	 * @param  int $size
285
	 * @return string
286
	 */
287
	protected function get_avatar_url( $default, $host, $email_hash, $size ) {
288
289
		$_return = $host . '/avatar/' . $email_hash . '?s=' . $size . '&amp;d=' . urlencode( $default );
290
		$rating = get_option( 'avatar_rating' );
291
292
		if ( ! empty( $rating ) ) {
293
			$_return .= '&amp;r=' . $rating;
294
		}
295
296
		return str_replace( '&#038;', '&amp;', esc_url( $_return ) );
297
298
	}
299
300
	/**
301
	 * Adds child to current Comment
302
	 *
303
	 * @param Comment $comment
304
	 */
305
	public function add_child( $comment ) {
306
307
		$this->children[] = $comment;
308
		$item->level = $this->level + 1;
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...
309
310
		if ( $item->children ) {
311
			$this->update_child_levels();
312
		}
313
314
	}
315
316
317
	/**
318
	 * Updates children nesting level param
319
	 *
320
	 * @return boolean
321
	 */
322 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...
323
324
		if ( is_array( $this->children ) ) {
325
326
			foreach ( $this->children as $child ) {
327
				$child->level = $this->level + 1;
328
				$child->update_child_levels();
329
			}
330
331
			return true;
332
333
		}
334
335
		return false;
336
337
	}
338
}
339