Test Failed
Push — issue/2900 ( f2122b )
by Ravinder
07:32
created

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

Complexity

Conditions 12
Paths 43

Size

Total Lines 50
Code Lines 34

Duplication

Lines 50
Ratio 100 %

Importance

Changes 0
Metric Value
cc 12
eloc 34
nc 43
nop 1
dl 50
loc 50
rs 5.3904
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.1
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.1
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.1
48
 *
49
 * @param string|int $id_or_email
50
 *
51
 * @return bool
52
 */
53 View Code Duplication
function give_validate_gravatar( $id_or_email ) {
0 ignored issues
show
Duplication introduced by
This function 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...
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.1.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.1.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
		$search,
151
		'payment',
152
		array(
153
			'number'     => 1,
154
			'meta_query' => array(
0 ignored issues
show
introduced by
Detected usage of meta_query, possible slow query.
Loading history...
155
				array(
156
					'key'   => '_give_donor_id',
157
					'value' => $donor_id
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
158
				)
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
159
			)
160
		)
161
	);
162
163
	return ( ! empty( $comments ) ? current( $comments ) : array() );
164
}
165
166
/**
167
 * Retrieve all donor comment attached to a donation
168
 *
169
 * Note: currently donor can only add one comment per donation
170
 *
171
 * @param int    $donor_id The donor ID to retrieve comment for.
172
 * @param array  $comment_args
173
 * @param string $search   Search for comment that contain a search term.
174
 *
175
 * @since 2.1.0
176
 *
177
 * @return array
178
 */
179
function give_get_donor_comments( $donor_id, $comment_args = array(), $search = '' ) {
180
	$comments = Give_Comment::get(
181
		$donor_id,
182
		$search,
183
		'donor',
184
		$comment_args
185
	);
186
187
	return ( ! empty( $comments ) ? $comments : array() );
188
}
189
190
191
/**
192
 * Gets the donor donation comment HTML
193
 *
194
 * @param WP_Comment|int $comment    The comment object or ID.
195
 * @param int            $payment_id The payment ID the note is connected to.
196
 *
197
 * @since 2.1.0
198
 *
199
 * @return string
200
 */
201
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...
202
203
	if ( is_numeric( $comment ) ) {
204
		$comment = get_comment( $comment );
205
	}
206
207
	$date_format = give_date_format() . ', ' . get_option( 'time_format' );
208
209
	$comment_html = sprintf(
210
		'<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>',
211
		$comment->comment_ID,
212
		get_comment_author( $comment->comment_ID ),
213
		date_i18n( $date_format, strtotime( $comment->comment_date ) ),
214
		$comment->comment_content
215
	);
216
217
	return $comment_html;
218
219
}
220
221
222
/**
223
 * Get donor latest comment
224
 *
225
 * @since 2.1.0
226
 *
227
 * @param int $donor_id
228
 * @param int $form_id
229
 *
230
 * @return string
231
 */
232
function get_donor_latest_comment( $donor_id, $form_id = 0 ) {
233
	$comment_content = '';
234
235
	$comment_args = array(
236
		'orderby' => 'comment_ID',
237
		'order'   => 'DESC',
238
		'number'  => 1,
239
	);
240
241
	// Get donor donation comment for specific form.
242
	if ( $form_id ) {
243
		$comment_args['comment_parent'] = $form_id;
244
	}
245
246
	$comment = current( give_get_donor_comments( $donor_id, $comment_args ) );
247
248
	if ( $comment instanceof WP_Comment ) {
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...
249
		$comment_content = esc_attr( $comment->comment_content );
250
	}
251
252
	return $comment_content;
253
}
254