PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 2.3.1
GiveWP – Donation Plugin and Fundraising Platform v2.3.1
4.18.0 4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 All 257 releases
give / includes / donors / frontend-donor-functions.php

frontend-donor-functions.php in GiveWP – Donation Plugin and Fundraising Platform 2.3.1, at includes/donors/frontend-donor-functions.php

282 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Donors
4 *
5 * @package Give
6 * @subpackage Donors
7 * @copyright Copyright (c) 2018, GiveWP
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();
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
84 $hashkey = md5( strtolower( trim( $email ) ) );
85 $cache_key = 'give_valid_gravatars';
86 $data = get_transient( $cache_key );
87 $data = ! empty( $data ) ? $data : array();
88
89
90 if ( ! array_key_exists( $hashkey, $data ) ) {
91 $uri = 'http://www.gravatar.com/avatar/' . $hashkey . '?d=404';
92
93 $response = wp_remote_head( $uri );
94
95 $data[ $hashkey ] = 0;
96
97 if ( ! is_wp_error( $response ) ) {
98 $data[ $hashkey ] = absint( '200' == $response['response']['code'] );
99 }
100
101 set_transient( $cache_key, $data, DAY_IN_SECONDS );
102 }
103
104 return (bool) $data[ $hashkey ];
105 }
106
107
108 /**
109 * Add a donor comment to a donation
110 *
111 * @param int $donation_id The donation ID to store a note for.
112 * @param int $donor The donor ID to store a note for.
113 * @param string $note The note to store.
114 * @param array $comment_args Comment arguments.
115 *
116 * @since 2.2.0
117 *
118 * @return int The new note ID
119 */
120 function give_insert_donor_donation_comment( $donation_id, $donor, $note, $comment_args = array() ) {
121 // Backward compatibility.
122 if( ! give_has_upgrade_completed('v230_move_donation_note' ) ) {
123 $comment_args = wp_parse_args(
124 $comment_args,
125 array(
126 'comment_approved' => 0,
127 'comment_parent' => give_get_payment_form_id( $donation_id )
128 )
129 );
130
131 $comment_id = Give_Comment::add( $donation_id, $note, 'payment', $comment_args );
132
133 update_comment_meta( $comment_id, '_give_donor_id', $donor );
134
135 return $comment_id;
136 }
137
138 $comment_id = Give_Comment::add(
139 array(
140 'comment_ID' => isset( $comment_args['comment_ID'] ) ? absint( $comment_args['comment_ID'] ) : 0,
141 'comment_parent' => $donation_id,
142 'comment_content' => $note,
143 'comment_type' => 'donor_donation',
144 )
145 );
146
147 Give()->comment->db_meta->update_meta( $comment_id, '_give_donor_id', $donor );
148 Give()->comment->db_meta->update_meta( $comment_id, '_give_form_id', give_get_payment_form_id( $donation_id ) );
149
150 return $comment_id;
151 }
152
153
154 /**
155 * Retrieve all donor comment attached to a donation
156 *
157 * Note: currently donor can only add one comment per donation
158 *
159 * @param int $donation_id The donation ID to retrieve comment for.
160 * @param int $donor_id The donor ID to retrieve comment for.
161 * @param string $search Search for comment that contain a search term.
162 *
163 * @since 2.2.0
164 *
165 * @return WP_Comment|array
166 */
167 function give_get_donor_donation_comment( $donation_id, $donor_id, $search = '' ) {
168 // Backward compatibility.
169 if( ! give_has_upgrade_completed('v230_move_donation_note' ) ) {
170
171 $comments = Give_Comment::get(
172 $donation_id,
173 'payment',
174 array(
175 'number' => 1,
176 'meta_query' => array(
177 array(
178 'key' => '_give_donor_id',
179 'value' => $donor_id
180 )
181 )
182 ),
183 $search
184 );
185
186 $comment = ! empty( $comments ) ? current( $comments ) : array();
187
188 return $comment;
189 }
190
191 $comments = Give()->comment->db->get_comments( array(
192 'number' => 1,
193 'comment_parent' => $donation_id,
194 'comment_type' => 'donor_donation',
195 ) );
196
197 return ( ! empty( $comments ) ? current( $comments ) : array() );
198 }
199
200 /**
201 * Retrieve donor comment id attached to a donation
202 *
203 * Note: currently donor can only add one comment per donation
204 *
205 * @param int $donation_id The donation ID to retrieve comment for.
206 * @param int $donor_id The donor ID to retrieve comment for.
207 * @param string $search Search for comment that contain a search term.
208 *
209 * @since 2.2.0
210 *
211 * @return int
212 */
213 function give_get_donor_donation_comment_id( $donation_id, $donor_id, $search = '' ) {
214 /* @var WP_Comment|array $comment */
215 $comment = give_get_donor_donation_comment( $donation_id, $donor_id, $search );
216 $comment_id = $comment instanceof WP_Comment ? $comment->comment_ID : 0;
217
218 return $comment_id;
219 }
220
221
222 /**
223 * Gets the donor donation comment HTML
224 *
225 * @param WP_Comment|int $comment The comment object or ID.
226 * @param int $payment_id The payment ID the note is connected to.
227 *
228 * @since 2.2.0
229 *
230 * @return string
231 */
232 function give_get_donor_donation_comment_html( $comment, $payment_id = 0 ) {
233
234 if ( is_numeric( $comment ) ) {
235 $comment = get_comment( $comment );
236 }
237
238 $date_format = give_date_format() . ', ' . get_option( 'time_format' );
239
240 $comment_html = sprintf(
241 '<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>',
242 $comment->comment_ID,
243 get_comment_author( $comment->comment_ID ),
244 date_i18n( $date_format, strtotime( $comment->comment_date ) ),
245 $comment->comment_content
246 );
247
248 return $comment_html;
249
250 }
251
252 /**
253 * Retrieves a name initials (first name and last name).
254 *
255 * @since 2.3.0
256 *
257 * @param array $args
258 *
259 * @return string
260 */
261 function give_get_name_initial( $args ) {
262 $args = wp_parse_args(
263 $args,
264 array(
265 'firstname' => '',
266 'lastname' => '',
267 )
268 );
269
270 $first_name_initial = mb_substr( $args['firstname'], 0, 1, 'utf-8' );
271 $last_name_initial = mb_substr( $args['lastname'], 0, 1, 'utf-8' );
272
273 $name_initial = trim( $first_name_initial . $last_name_initial );
274
275 /**
276 * Filter the name initial
277 *
278 * @since 2.3.0
279 */
280 return apply_filters( 'give_get_name_initial', $name_initial, $args );
281 }
282