Test Failed
Push — master ( f5256c...25a383 )
by Devin
07:02
created

frontend-donor-functions.php ➔ give_validate_gravatar()   C

Complexity

Conditions 12
Paths 43

Size

Total Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
nc 43
nop 1
dl 0
loc 50
rs 6.9666
c 0
b 0
f 0

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
/**
3
 * Donors
4
 *
5
 * @package    Give
6
 * @subpackage Donors
7
 * @copyright  Copyright (c) 2018, WordImpress
8
 * @license    https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since      2.2.0
10
 */
11
12
/**
13
 * Get the donor's Gravatar with a nice fallback.
14
 *
15
 * The fallback uses the donor's
16
 *
17
 * @since 2.2.0
18
 *
19
 * @param Give_Donor $donor
20
 * @param int        $size
21
 *
22
 * @return string HTML output.
23
 */
24
function give_get_donor_avatar( $donor, $size = 60 ) {
25
	ob_start();
26
	?>
27
	<div class="give-donor__image">
28
		<?php
29
		// Check if gravatar exists.
30
		if ( give_validate_gravatar( $donor->email ) ) {
31
			// Return avatar.
32
			echo get_avatar( $donor->email, $size );
33
		} else {
34
			// No gravatar = output initials.
35
			echo $donor->get_donor_initals();
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$donor'
Loading history...
36
		} ?>
37
	</div>
38
	<?php
39
40
	return apply_filters( 'give_get_donor_avatar', ob_get_clean() );
41
42
}
43
44
/**
45
 * Determine whether a Gravatar exists for a donor or not.
46
 *
47
 * @since 2.2.0
48
 *
49
 * @param string|int $id_or_email
50
 *
51
 * @return bool
52
 */
53
function give_validate_gravatar( $id_or_email ) {
54
55
	//id or email code borrowed from wp-includes/pluggable.php
56
	$email = '';
57
	if ( is_numeric( $id_or_email ) ) {
58
		$id   = (int) $id_or_email;
59
		$user = get_userdata( $id );
60
		if ( $user ) {
61
			$email = $user->user_email;
62
		}
63
	} elseif ( is_object( $id_or_email ) ) {
64
		// No avatar for pingbacks or trackbacks
65
		$allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
66
		if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) {
67
			return false;
68
		}
69
70
		if ( ! empty( $id_or_email->user_id ) ) {
71
			$id   = (int) $id_or_email->user_id;
72
			$user = get_userdata( $id );
73
			if ( $user ) {
74
				$email = $user->user_email;
75
			}
76
		} elseif ( ! empty( $id_or_email->comment_author_email ) ) {
77
			$email = $id_or_email->comment_author_email;
78
		}
79
	} else {
80
		$email = $id_or_email;
81
	}
82
83
	$hashkey = md5( strtolower( trim( $email ) ) );
84
	$uri     = 'http://www.gravatar.com/avatar/' . $hashkey . '?d=404';
85
86
	$data = wp_cache_get( $hashkey );
87
	if ( false === $data ) {
88
		$response = wp_remote_head( $uri );
89
		if ( is_wp_error( $response ) ) {
90
			$data = 'not200';
91
		} else {
92
			$data = $response['response']['code'];
93
		}
94
		wp_cache_set( $hashkey, $data, $group = '', $expire = 60 * 5 );
95
96
	}
97
	if ( $data == '200' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
98
		return true;
99
	} else {
100
		return false;
101
	}
102
}
103
104
105
/**
106
 * Add a donor comment to a donation
107
 *
108
 * @param int    $donation_id  The donation ID to store a note for.
109
 * @param int    $donor        The donor ID to store a note for.
110
 * @param string $note         The note to store.
111
 * @param array  $comment_args Comment arguments.
112
 *
113
 * @since 2.2.0
114
 *
115
 * @return int The new note ID
116
 */
117
function give_insert_donor_donation_comment( $donation_id, $donor, $note, $comment_args = array() ) {
118
	$comment_args = wp_parse_args(
119
		$comment_args,
120
		array(
121
			'comment_approved' => 0,
122
			'comment_parent'   => give_get_payment_form_id( $donation_id )
123
		)
124
	);
125
126
	$comment_id = Give_Comment::add( $donation_id, $note, 'payment', $comment_args );
127
128
	update_comment_meta( $comment_id, '_give_donor_id', $donor );
129
130
	return $comment_id;
131
}
132
133
134
/**
135
 * Retrieve all donor comment attached to a donation
136
 *
137
 * Note: currently donor can only add one comment per donation
138
 *
139
 * @param int    $donation_id The donation ID to retrieve comment for.
140
 * @param int    $donor_id    The donor ID to retrieve comment for.
141
 * @param string $search      Search for comment that contain a search term.
142
 *
143
 * @since 2.2.0
144
 *
145
 * @return WP_Comment|array
146
 */
147
function give_get_donor_donation_comment( $donation_id, $donor_id, $search = '' ) {
148
	$comments = Give_Comment::get(
149
		$donation_id,
150
		'payment',
151
		array(
152
			'number'     => 1,
153
			'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
154
				array(
155
					'key'   => '_give_donor_id',
156
					'value' => $donor_id
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
157
				)
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
158
			)
159
		),
160
		$search
161
	);
162
163
	return ( ! empty( $comments ) ? current( $comments ) : array() );
164
}
165
166
/**
167
 * Retrieve donor comment id attached to a donation
168
 *
169
 * Note: currently donor can only add one comment per donation
170
 *
171
 * @param int    $donation_id The donation ID to retrieve comment for.
172
 * @param int    $donor_id    The donor ID to retrieve comment for.
173
 * @param string $search      Search for comment that contain a search term.
174
 *
175
 * @since 2.2.0
176
 *
177
 * @return int
178
 */
179
function give_get_donor_donation_comment_id( $donation_id, $donor_id, $search = '' ) {
180
	/* @var WP_Comment|array $comment */
181
	$comment    = give_get_donor_donation_comment( $donation_id, $donor_id, $search );
182
	$comment_id = $comment instanceof WP_Comment ? $comment->comment_ID : 0;
0 ignored issues
show
Bug introduced by
The class WP_Comment does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
183
184
	return $comment_id;
185
}
186
187
/**
188
 * Retrieve all donor comment attached to a donation
189
 *
190
 * Note: currently donor can only add one comment per donation
191
 *
192
 * @param int    $donor_id The donor ID to retrieve comment for.
193
 * @param array  $comment_args
194
 * @param string $search   Search for comment that contain a search term.
195
 *
196
 * @since 2.2.0
197
 *
198
 * @return array
199
 */
200
function give_get_donor_donation_comments( $donor_id, $comment_args = array(), $search = '' ) {
201
	$comments = Give_Comment::get(
202
		$donor_id,
203
		'payment',
204
		$comment_args,
205
		$search
206
	);
207
208
	return ( ! empty( $comments ) ? $comments : array() );
209
}
210
211
212
/**
213
 * Gets the donor donation comment HTML
214
 *
215
 * @param WP_Comment|int $comment    The comment object or ID.
216
 * @param int            $payment_id The payment ID the note is connected to.
217
 *
218
 * @since 2.2.0
219
 *
220
 * @return string
221
 */
222
function give_get_donor_donation_comment_html( $comment, $payment_id = 0 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $payment_id 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...
223
224
	if ( is_numeric( $comment ) ) {
225
		$comment = get_comment( $comment );
226
	}
227
228
	$date_format = give_date_format() . ', ' . get_option( 'time_format' );
229
230
	$comment_html = sprintf(
231
		'<div class="give-payment-note" id="give-payment-note-%s"><p><strong>%s</strong>&nbsp;&ndash;&nbsp;<span style="color:#aaa;font-style:italic;">%s</span><br/>%s</p></div>',
232
		$comment->comment_ID,
233
		get_comment_author( $comment->comment_ID ),
234
		date_i18n( $date_format, strtotime( $comment->comment_date ) ),
235
		$comment->comment_content
236
	);
237
238
	return $comment_html;
239
240
}
241
242
243
/**
244
 * Get donor latest comment
245
 *
246
 * @since 2.2.0
247
 *
248
 * @param int $donor_id
249
 * @param int $form_id
250
 *
251
 * @return WP_Comment/array
0 ignored issues
show
Documentation introduced by
The doc-type WP_Comment/array could not be parsed: Unknown type name "WP_Comment/array" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
252
 */
253
function give_get_donor_latest_comment( $donor_id, $form_id = 0 ) {
254
	$comment_args = array(
255
		'post_id'    => 0,
256
		'orderby'    => 'comment_ID',
257
		'order'      => 'DESC',
258
		'number'     => 1,
259
		'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
260
			'related' => 'AND',
261
			array(
262
				'key'   => '_give_donor_id',
263
				'value' => $donor_id
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
264
			),
265
			array(
266
				'key'   => '_give_anonymous_donation',
267
				'value' => 0
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
268
			)
269
		)
270
	);
271
272
	// Get donor donation comment for specific form.
273
	if ( $form_id ) {
274
		$comment_args['parent'] = $form_id;
275
	}
276
277
	$comment = current( give_get_donor_donation_comments( $donor_id, $comment_args ) );
278
279
	return $comment;
280
}
281