PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.7
GiveWP – Donation Plugin and Fundraising Platform v4.16.7
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 2.3.2 2.30.0 2.31.0 2.31.1 All 253 releases
give / includes / donors / frontend-donor-functions.php
frontend-donor-functions.php
284 lines 7.2 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 use Give\Donations\ValueObjects\DonationMetaKeys;
13
14 /**
15 * Get the donor's Gravatar with a nice fallback.
16 *
17 * The fallback uses the donor's
18 *
19 * @since 2.2.0
20 *
21 * @param Give_Donor $donor
22 * @param int $size
23 *
24 * @return string HTML output.
25 */
26 function give_get_donor_avatar( $donor, $size = 60 ) {
27 ob_start();
28 ?>
29 <div class="give-donor__image">
30 <?php
31 // Check if gravatar exists.
32 if ( give_validate_gravatar( $donor->email ) ) {
33 // Return avatar.
34 echo get_avatar( $donor->email, $size );
35 } else {
36 // No gravatar = output initials.
37 echo $donor->get_donor_initals();
38 }
39 ?>
40 </div>
41 <?php
42
43 return apply_filters( 'give_get_donor_avatar', ob_get_clean() );
44
45 }
46
47 /**
48 * Determine whether a Gravatar exists for a donor or not.
49 *
50 * @since 2.2.0
51 *
52 * @param string|int $id_or_email
53 *
54 * @return bool
55 */
56 function give_validate_gravatar( $id_or_email ) {
57
58 // id or email code borrowed from wp-includes/pluggable.php
59 $email = '';
60 if ( is_numeric( $id_or_email ) ) {
61 $id = (int) $id_or_email;
62 $user = get_userdata( $id );
63 if ( $user ) {
64 $email = $user->user_email;
65 }
66 } elseif ( is_object( $id_or_email ) ) {
67 // No avatar for pingbacks or trackbacks
68 $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
69 if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) {
70 return false;
71 }
72
73 if ( ! empty( $id_or_email->user_id ) ) {
74 $id = (int) $id_or_email->user_id;
75 $user = get_userdata( $id );
76 if ( $user ) {
77 $email = $user->user_email;
78 }
79 } elseif ( ! empty( $id_or_email->comment_author_email ) ) {
80 $email = $id_or_email->comment_author_email;
81 }
82 } else {
83 $email = $id_or_email;
84 }
85
86 $hashkey = md5( strtolower( trim( $email ) ) );
87 $cache_key = Give_Cache::get_key( 'give_valid_gravatars' );
88 $data = Give_Cache::get( $cache_key );
89 $data = ! empty( $data ) ? $data : array();
90
91 if ( ! array_key_exists( $hashkey, $data ) ) {
92 $uri = "http://www.gravatar.com/avatar/{$hashkey}?d=404";
93
94 $response = wp_remote_head( $uri );
95
96 $data[ $hashkey ] = 0;
97
98 if ( ! is_wp_error( $response ) ) {
99 $data[ $hashkey ] = absint( '200' == $response['response']['code'] );
100 }
101
102 Give_Cache::set( $cache_key, $data, DAY_IN_SECONDS );
103 }
104
105 return (bool) $data[ $hashkey ];
106 }
107
108
109 /**
110 * Add a donor comment to a donation
111 *
112 * @param int $donation_id The donation ID to store a note for.
113 * @param int $donor The donor ID to store a note for.
114 * @param string $note The note to store.
115 * @param array $comment_args Comment arguments.
116 *
117 * @since 2.2.0
118 *
119 * @return int The new note ID
120 */
121 function give_insert_donor_donation_comment( $donation_id, $donor, $note, $comment_args = array() ) {
122 // Backward compatibility.
123 if ( ! give_has_upgrade_completed( 'v230_move_donation_note' ) ) {
124 $comment_args = wp_parse_args(
125 $comment_args,
126 array(
127 'comment_approved' => 0,
128 'comment_parent' => give_get_payment_form_id( $donation_id ),
129 )
130 );
131
132 $comment_id = Give_Comment::add( $donation_id, $note, 'payment', $comment_args );
133
134 update_comment_meta( $comment_id, '_give_donor_id', $donor );
135
136 return $comment_id;
137 }
138
139 $comment_id = Give_Comment::add(
140 array(
141 'comment_ID' => isset( $comment_args['comment_ID'] ) ? absint( $comment_args['comment_ID'] ) : 0,
142 'comment_parent' => $donation_id,
143 'comment_content' => $note,
144 'comment_type' => 'donor_donation',
145 )
146 );
147
148 Give()->comment->db_meta->update_meta( $comment_id, '_give_donor_id', $donor );
149 Give()->comment->db_meta->update_meta( $comment_id, '_give_form_id', give_get_payment_form_id( $donation_id ) );
150
151 return $comment_id;
152 }
153
154
155 /**
156 * Retrieve all donor comment attached to a donation
157 *
158 * Note: currently donor can only add one comment per donation
159 *
160 * @since 2.27.0 Change to read comment from donations meta table
161 * @since 2.2.0
162 *
163 * @param int $donation_id The donation ID to retrieve comment for.
164 * @param int $donor_id The donor ID to retrieve comment for.
165 * @param string $search Search for comment that contain a search term.
166 *
167 * @return array|stdClass|WP_Comment
168 */
169 function give_get_donor_donation_comment( $donation_id, $donor_id, $search = '' ) {
170 // Backward compatibility.
171 if ( ! give_has_upgrade_completed( 'v230_move_donation_note' ) ) {
172
173 $comments = Give_Comment::get(
174 $donation_id,
175 'payment',
176 array(
177 'number' => 1,
178 'meta_query' => array(
179 array(
180 'key' => '_give_donor_id',
181 'value' => $donor_id,
182 ),
183 ),
184 ),
185 $search
186 );
187
188 $comment = !empty($comments) ? current($comments) : array();
189
190 return $comment;
191 }
192
193 $comment = new stdClass();
194 $comment->comment_ID = 0;
195 $comment->comment_content = give_get_payment_meta($donation_id, DonationMetaKeys::COMMENT, true);
196 $comment->comment_parent = $donation_id;
197 $comment->comment_type = 'donor_donation';
198
199 return $comment;
200 }
201
202 /**
203 * Retrieve donor comment id attached to a donation
204 *
205 * Note: currently donor can only add one comment per donation
206 *
207 * @param int $donation_id The donation ID to retrieve comment for.
208 * @param int $donor_id The donor ID to retrieve comment for.
209 * @param string $search Search for comment that contain a search term.
210 *
211 * @since 2.2.0
212 *
213 * @return int
214 */
215 function give_get_donor_donation_comment_id( $donation_id, $donor_id, $search = '' ) {
216 /* @var WP_Comment|array $comment */
217 $comment = give_get_donor_donation_comment( $donation_id, $donor_id, $search );
218 $comment_id = $comment instanceof WP_Comment ? $comment->comment_ID : 0;
219
220 return $comment_id;
221 }
222
223
224 /**
225 * Gets the donor donation comment HTML
226 *
227 * @param WP_Comment|int $comment The comment object or ID.
228 * @param int $payment_id The payment ID the note is connected to.
229 *
230 * @since 2.2.0
231 *
232 * @return string
233 */
234 function give_get_donor_donation_comment_html( $comment, $payment_id = 0 ) {
235
236 if ( is_numeric( $comment ) ) {
237 $comment = get_comment( $comment );
238 }
239
240 $date_format = give_date_format() . ', ' . get_option( 'time_format' );
241
242 $comment_html = sprintf(
243 '<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>',
244 $comment->comment_ID,
245 get_comment_author( $comment->comment_ID ),
246 date_i18n( $date_format, strtotime( $comment->comment_date ) ),
247 $comment->comment_content
248 );
249
250 return $comment_html;
251
252 }
253
254 /**
255 * Retrieves a name initials (first name and last name).
256 *
257 * @since 2.3.0
258 *
259 * @param array $args
260 *
261 * @return string
262 */
263 function give_get_name_initial( $args ) {
264 $args = wp_parse_args(
265 $args,
266 array(
267 'firstname' => '',
268 'lastname' => '',
269 )
270 );
271
272 $first_name_initial = mb_substr( $args['firstname'], 0, 1, 'utf-8' );
273 $last_name_initial = mb_substr( $args['lastname'], 0, 1, 'utf-8' );
274
275 $name_initial = trim( $first_name_initial . $last_name_initial );
276
277 /**
278 * Filter the name initial
279 *
280 * @since 2.3.0
281 */
282 return apply_filters( 'give_get_name_initial', $name_initial, $args );
283 }
284