1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* Donators Gravatars |
4
|
|
|
* |
5
|
|
|
* @package Give |
6
|
|
|
* @subpackage Classes/Donators |
7
|
|
|
* @copyright Copyright (c) 2015, WordImpress |
8
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
9
|
|
|
* @since 1.0 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// Exit if accessed directly |
13
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
14
|
|
|
exit; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
class Give_Donators_Gravatars { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Start your engines |
21
|
|
|
* |
22
|
|
|
* @since 1.0 |
23
|
|
|
* |
24
|
|
|
* @return void |
|
|
|
|
25
|
|
|
*/ |
26
|
|
|
public function __construct() { |
27
|
|
|
$this->setup_actions(); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Setup the default hooks and actions |
32
|
|
|
* |
33
|
|
|
* @since 1.0 |
34
|
|
|
* |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
|
|
private function setup_actions() { |
38
|
|
|
// add_action( 'widgets_init', array( $this, 'register_widget' ) ); |
|
|
|
|
39
|
|
|
// add_shortcode( 'give_donators_gravatars', array( $this, 'shortcode' ) ); |
|
|
|
|
40
|
|
|
// add_filter( 'give_settings_display', array( $this, 'settings' ) ); |
|
|
|
|
41
|
|
|
// do_action( 'give_donators_gravatars_setup_actions' ); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Utility function to check if a gravatar exists for a given email or id |
46
|
|
|
* |
47
|
|
|
* @param int|string|object $id_or_email A user ID, email address, or comment object |
48
|
|
|
* |
49
|
|
|
* @return bool if the gravatar exists or not |
50
|
|
|
*/ |
51
|
|
|
|
52
|
|
|
// https://gist.github.com/justinph/5197810 |
53
|
|
|
function validate_gravatar( $id_or_email ) { |
|
|
|
|
54
|
|
|
//id or email code borrowed from wp-includes/pluggable.php |
55
|
|
|
$email = ''; |
56
|
|
|
if ( is_numeric( $id_or_email ) ) { |
57
|
|
|
$id = (int) $id_or_email; |
58
|
|
|
$user = get_userdata( $id ); |
59
|
|
|
if ( $user ) { |
60
|
|
|
$email = $user->user_email; |
61
|
|
|
} |
62
|
|
|
} elseif ( is_object( $id_or_email ) ) { |
63
|
|
|
// No avatar for pingbacks or trackbacks |
64
|
|
|
$allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) ); |
65
|
|
|
if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) { |
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ( ! empty( $id_or_email->user_id ) ) { |
70
|
|
|
$id = (int) $id_or_email->user_id; |
71
|
|
|
$user = get_userdata( $id ); |
72
|
|
|
if ( $user ) { |
73
|
|
|
$email = $user->user_email; |
74
|
|
|
} |
75
|
|
|
} elseif ( ! empty( $id_or_email->comment_author_email ) ) { |
76
|
|
|
$email = $id_or_email->comment_author_email; |
77
|
|
|
} |
78
|
|
|
} else { |
79
|
|
|
$email = $id_or_email; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$hashkey = md5( strtolower( trim( $email ) ) ); |
83
|
|
|
$uri = 'http://www.gravatar.com/avatar/' . $hashkey . '?d=404'; |
84
|
|
|
|
85
|
|
|
$data = wp_cache_get( $hashkey ); |
86
|
|
|
if ( false === $data ) { |
87
|
|
|
$response = wp_remote_head( $uri ); |
88
|
|
|
if ( is_wp_error( $response ) ) { |
89
|
|
|
$data = 'not200'; |
90
|
|
|
} else { |
91
|
|
|
$data = $response['response']['code']; |
92
|
|
|
} |
93
|
|
|
wp_cache_set( $hashkey, $data, $group = '', $expire = 60 * 5 ); |
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
if ( $data == '200' ) { |
97
|
|
|
return true; |
98
|
|
|
} else { |
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get an array of all the log IDs using the Give Logging Class |
105
|
|
|
* |
106
|
|
|
* @since 1.0 |
107
|
|
|
* @return array if logs, false otherwise |
108
|
|
|
* |
109
|
|
|
* @param int $form_id |
110
|
|
|
*/ |
111
|
|
|
function get_log_ids( $form_id = '' ) { |
|
|
|
|
112
|
|
|
|
113
|
|
|
// get Give_Logging class |
114
|
|
|
global $give_logs; |
|
|
|
|
115
|
|
|
|
116
|
|
|
// get log for this form |
117
|
|
|
$logs = $give_logs->get_logs( $form_id ); |
118
|
|
|
|
119
|
|
|
if ( $logs ) { |
120
|
|
|
// make an array with all the donor IDs |
121
|
|
|
foreach ( $logs as $log ) { |
122
|
|
|
$log_ids[] = $log->ID; |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $log_ids; |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return null; |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get payment ID |
135
|
|
|
* |
136
|
|
|
* @since 1.0 |
137
|
|
|
*/ |
138
|
|
|
function get_payment_ids( $form_id = '' ) { |
|
|
|
|
139
|
|
|
|
140
|
|
|
global $give_options; |
|
|
|
|
141
|
|
|
|
142
|
|
|
$log_ids = $this->get_log_ids( $form_id ); |
143
|
|
|
|
144
|
|
|
if ( $log_ids ) { |
145
|
|
|
|
146
|
|
|
$payment_ids = array(); |
147
|
|
|
|
148
|
|
|
foreach ( $log_ids as $id ) { |
149
|
|
|
// get the payment ID for each corresponding log ID |
150
|
|
|
$payment_ids[] = get_post_meta( $id, '_give_log_payment_id', true ); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// remove donors who have purchased more than once so we can have unique avatars |
154
|
|
|
$unique_emails = array(); |
155
|
|
|
|
156
|
|
|
foreach ( $payment_ids as $key => $id ) { |
157
|
|
|
|
158
|
|
|
$email = get_post_meta( $id, '_give_payment_user_email', true ); |
159
|
|
|
|
160
|
|
|
if ( isset ( $give_options['give_donators_gravatars_has_gravatar_account'] ) ) { |
161
|
|
|
if ( ! $this->validate_gravatar( $email ) ) { |
162
|
|
|
continue; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$unique_emails[ $id ] = get_post_meta( $id, '_give_payment_user_email', true ); |
167
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// strip duplicate emails |
171
|
|
|
$unique_emails = array_unique( $unique_emails ); |
172
|
|
|
|
173
|
|
|
// convert the unique IDs back into simple array |
174
|
|
|
foreach ( $unique_emails as $id => $email ) { |
175
|
|
|
$unique_ids[] = $id; |
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
// randomize the payment IDs if enabled |
179
|
|
|
if ( isset( $give_options['give_donators_gravatars_random_gravatars'] ) ) { |
180
|
|
|
shuffle( $unique_ids ); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
// return our unique IDs |
184
|
|
|
return $unique_ids; |
|
|
|
|
185
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Gravatars |
193
|
|
|
* |
194
|
|
|
* @since 1.0 |
195
|
|
|
*/ |
196
|
|
|
function gravatars( $form_id = false, $title = '' ) { |
|
|
|
|
197
|
|
|
|
198
|
|
|
// unique $payment_ids |
199
|
|
|
$payment_ids = $this->get_payment_ids( $form_id ); |
200
|
|
|
|
201
|
|
|
// var_dump( $payment_ids ); |
|
|
|
|
202
|
|
|
// var_dump( $this->get_log_ids( get_the_ID() ) ); |
|
|
|
|
203
|
|
|
|
204
|
|
|
global $give_options; |
|
|
|
|
205
|
|
|
|
206
|
|
|
// return if no ID |
207
|
|
|
if ( ! $form_id ) { |
208
|
|
|
return; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
// minimum amount of purchases before showing gravatars |
212
|
|
|
// if the number of items in array is not greater or equal to the number specified, then exit |
213
|
|
|
if ( isset( $give_options['give_donators_gravatars_min_purchases_required'] ) && '' != $give_options['give_donators_gravatars_min_purchases_required'] ) { |
214
|
|
|
if ( ! ( count( $payment_ids ) >= $give_options['give_donators_gravatars_min_purchases_required'] ) ) { |
215
|
|
|
return; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
ob_start(); |
220
|
|
|
|
221
|
|
|
$output = ''; |
222
|
|
|
echo '<div id="give-purchase-gravatars">'; |
223
|
|
|
|
224
|
|
|
|
225
|
|
|
if ( isset ( $title ) ) { |
226
|
|
|
|
227
|
|
|
if ( $title ) { |
228
|
|
|
echo apply_filters( 'give_donators_gravatars_title', '<h3 class="give-gravatars-title">' . esc_attr( $title ) . '</h3>' ); |
229
|
|
|
} elseif ( isset( $give_options['give_donators_gravatars_heading'] ) ) { |
230
|
|
|
echo apply_filters( 'give_donators_gravatars_title', '<h3 class="give-gravatars-title">' . esc_attr( $give_options['give_donators_gravatars_heading'] ) . '</h2>' ); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
} |
234
|
|
|
echo '<ul class="give-purchase-gravatars-list">'; |
235
|
|
|
$i = 0; |
236
|
|
|
|
237
|
|
|
if ( $payment_ids ) { |
238
|
|
|
foreach ( $payment_ids as $id ) { |
239
|
|
|
|
240
|
|
|
// Give saves a blank option even when the control is turned off, hence the extra check |
241
|
|
|
if ( isset( $give_options['give_donators_gravatars_maximum_number'] ) && '' != $give_options['give_donators_gravatars_maximum_number'] && $i == $give_options['give_donators_gravatars_maximum_number'] ) { |
242
|
|
|
continue; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
// get the payment meta |
246
|
|
|
$payment_meta = get_post_meta( $id, '_give_payment_meta', true ); |
247
|
|
|
|
248
|
|
|
// unserialize the payment meta |
249
|
|
|
$user_info = maybe_unserialize( $payment_meta['user_info'] ); |
250
|
|
|
|
251
|
|
|
// get donor's first name |
252
|
|
|
$name = $user_info['first_name']; |
253
|
|
|
|
254
|
|
|
// get donor's email |
255
|
|
|
$email = get_post_meta( $id, '_give_payment_user_email', true ); |
256
|
|
|
|
257
|
|
|
// set gravatar size and provide filter |
258
|
|
|
$size = isset( $give_options['give_donators_gravatars_gravatar_size'] ) ? apply_filters( 'give_donators_gravatars_gravatar_size', $give_options['give_donators_gravatars_gravatar_size'] ) : ''; |
259
|
|
|
|
260
|
|
|
// default image |
261
|
|
|
$default_image = apply_filters( 'give_donators_gravatars_gravatar_default_image', false ); |
262
|
|
|
|
263
|
|
|
// assemble output |
264
|
|
|
$output .= '<li>'; |
265
|
|
|
|
266
|
|
|
$output .= get_avatar( $email, $size, $default_image, $name ); |
267
|
|
|
$output .= '</li>'; |
268
|
|
|
|
269
|
|
|
$i ++; |
270
|
|
|
|
271
|
|
|
} // end foreach |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
echo $output; |
275
|
|
|
echo '</ul>'; |
276
|
|
|
echo '</div>'; |
277
|
|
|
|
278
|
|
|
return apply_filters( 'give_donators_gravatars', ob_get_clean() ); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Register widget |
283
|
|
|
* |
284
|
|
|
* @since 1.0 |
285
|
|
|
*/ |
286
|
|
|
function register_widget() { |
|
|
|
|
287
|
|
|
register_widget( 'Give_Donators_Gravatars_Widget' ); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Shortcode |
292
|
|
|
* |
293
|
|
|
* @since 1.0 |
294
|
|
|
* @todo set the ID to get_the_ID() if ID parameter is not passed through. Otherwise it will incorrectly get other gravatars |
295
|
|
|
*/ |
296
|
|
|
function shortcode( $atts, $content = null ) { |
|
|
|
|
297
|
|
|
|
298
|
|
|
$atts = shortcode_atts( array( |
299
|
|
|
'id' => '', |
300
|
|
|
'title' => '' |
301
|
|
|
), $atts, 'give_donators_gravatars' ); |
302
|
|
|
|
303
|
|
|
// if no ID is passed on single give_forms pages, get the correct ID |
304
|
|
|
if ( is_singular( 'give_forms' ) ) { |
305
|
|
|
$id = get_the_ID(); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
$content = $this->gravatars( $atts['id'], $atts['title'] ); |
309
|
|
|
|
310
|
|
|
return $content; |
311
|
|
|
|
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Settings |
316
|
|
|
* |
317
|
|
|
* @since 1.0 |
318
|
|
|
*/ |
319
|
|
|
function settings( $settings ) { |
|
|
|
|
320
|
|
|
|
321
|
|
|
$give_gravatar_settings = array( |
322
|
|
|
array( |
323
|
|
|
'name' => __( 'Donator Gravatars', 'give' ), |
324
|
|
|
'desc' => '<hr>', |
325
|
|
|
'id' => 'give_title', |
326
|
|
|
'type' => 'give_title' |
327
|
|
|
), |
328
|
|
|
array( |
329
|
|
|
'name' => __( 'Heading', 'give' ), |
330
|
|
|
'desc' => __( 'The heading to display above the Gravatars', 'give' ), |
331
|
|
|
'type' => 'text', |
332
|
|
|
'id' => 'give_donators_gravatars_heading' |
333
|
|
|
), |
334
|
|
|
array( |
335
|
|
|
'name' => __( 'Gravatar Size', 'give' ), |
336
|
|
|
'desc' => __( 'The size of each Gravatar in pixels (512px maximum)', 'give' ), |
337
|
|
|
'type' => 'text_small', |
338
|
|
|
'id' => 'give_donators_gravatars_gravatar_size', |
339
|
|
|
'default' => '64' |
340
|
|
|
), |
341
|
|
|
array( |
342
|
|
|
'name' => __( 'Minimum Unique Purchases Required', 'give' ), |
343
|
|
|
'desc' => sprintf( __( 'The minimum number of unique purchases a %s must have before the Gravatars are shown. Leave blank for no minimum.', 'give' ), strtolower( give_get_forms_label_singular() ) ), |
344
|
|
|
'type' => 'text_small', |
345
|
|
|
'id' => 'give_donators_gravatars_min_purchases_required', |
346
|
|
|
), |
347
|
|
|
array( |
348
|
|
|
'name' => __( 'Maximum Gravatars To Show', 'give' ), |
349
|
|
|
'desc' => __( 'The maximum number of gravatars to show. Leave blank for no limit.', 'give' ), |
350
|
|
|
'type' => 'text', |
351
|
|
|
'id' => 'give_donators_gravatars_maximum_number', |
352
|
|
|
'default' => '20', |
353
|
|
|
), |
354
|
|
|
array( |
355
|
|
|
'name' => __( 'Gravatar Visibility', 'give' ), |
356
|
|
|
'desc' => __( 'Only show donators with a Gravatar account', 'give' ), |
357
|
|
|
'id' => 'give_donators_gravatars_has_gravatar_account', |
358
|
|
|
'type' => 'checkbox', |
359
|
|
|
), |
360
|
|
|
array( |
361
|
|
|
'name' => __( 'Randomize Gravatars', 'give' ), |
362
|
|
|
'desc' => __( 'Randomize the Gravatars', 'give' ), |
363
|
|
|
'id' => 'give_donators_gravatars_random_gravatars', |
364
|
|
|
'type' => 'checkbox', |
365
|
|
|
), |
366
|
|
|
); |
367
|
|
|
|
368
|
|
|
return array_merge( $settings, $give_gravatar_settings ); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Widget |
376
|
|
|
* |
377
|
|
|
* @since 1.0 |
378
|
|
|
*/ |
379
|
|
|
class Give_Donators_Gravatars_Widget extends WP_Widget { |
380
|
|
|
|
381
|
|
|
/* |
382
|
|
|
* widget constructor |
383
|
|
|
*/ |
384
|
|
|
function __construct() { |
|
|
|
|
385
|
|
|
|
386
|
|
|
$give_label_singular = function_exists( 'give_get_forms_label_singular' ) ? strtolower( give_get_forms_label_singular() ) : null; |
387
|
|
|
|
388
|
|
|
// widget settings |
389
|
|
|
$widget_ops = array( |
390
|
|
|
'classname' => 'give-donators-gravatars', |
391
|
|
|
'description' => sprintf( __( 'Displays gravatars of people who have donated using your your %s. Will only show on the single %s page.', 'give' ), $give_label_singular, $give_label_singular ) |
392
|
|
|
); |
393
|
|
|
|
394
|
|
|
// widget control settings |
395
|
|
|
$control_ops = array( |
396
|
|
|
'width' => 250, |
397
|
|
|
'height' => 350, |
398
|
|
|
'id_base' => 'give_gravatars_widget' |
399
|
|
|
); |
400
|
|
|
|
401
|
|
|
// create the widget |
402
|
|
|
$this->WP_Widget( |
403
|
|
|
'give_donators_gravatars_widget', |
404
|
|
|
__( 'Give Donators Gravatars', 'give' ), |
405
|
|
|
$widget_ops, |
406
|
|
|
$control_ops |
407
|
|
|
); |
408
|
|
|
|
409
|
|
|
} // end constructor |
410
|
|
|
|
411
|
|
|
|
412
|
|
|
/* |
413
|
|
|
* Outputs the content of the widget |
414
|
|
|
*/ |
415
|
|
|
function widget( $args, $instance ) { |
|
|
|
|
416
|
|
|
global $give_options; |
|
|
|
|
417
|
|
|
|
418
|
|
|
extract( $args ); |
419
|
|
|
|
420
|
|
|
if ( ! is_singular( 'give_forms' ) ) { |
421
|
|
|
return; |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
// Variables from widget settings |
425
|
|
|
$title = apply_filters( 'widget_title', $instance['title'] ); |
426
|
|
|
|
427
|
|
|
// Used by themes. Opens the widget |
428
|
|
|
echo $before_widget; |
429
|
|
|
|
430
|
|
|
// Display the widget title |
431
|
|
|
if ( $title ) { |
432
|
|
|
echo $before_title . $title . $after_title; |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
$gravatars = new Give_Donators_Gravatars(); |
436
|
|
|
|
437
|
|
|
echo $gravatars->gravatars( get_the_ID(), null ); // remove title |
438
|
|
|
|
439
|
|
|
// Used by themes. Closes the widget |
440
|
|
|
echo $after_widget; |
441
|
|
|
|
442
|
|
|
} // end WIDGET function |
443
|
|
|
|
444
|
|
|
/* |
445
|
|
|
* Update function. Processes widget options to be saved |
446
|
|
|
*/ |
447
|
|
|
function update( $new_instance, $old_instance ) { |
|
|
|
|
448
|
|
|
|
449
|
|
|
$instance = $old_instance; |
450
|
|
|
|
451
|
|
|
$instance['title'] = strip_tags( $new_instance['title'] ); |
452
|
|
|
|
453
|
|
|
return $instance; |
454
|
|
|
|
455
|
|
|
} // end UPDATE function |
456
|
|
|
|
457
|
|
|
/* |
458
|
|
|
* Form function. Displays the actual form on the widget page |
459
|
|
|
*/ |
460
|
|
|
function form( $instance ) { |
|
|
|
|
461
|
|
|
|
462
|
|
|
// Set up some default widget settings. |
463
|
|
|
$defaults = array( |
464
|
|
|
'title' => '', |
465
|
|
|
); |
466
|
|
|
|
467
|
|
|
$instance = wp_parse_args( (array) $instance, $defaults ); ?> |
468
|
|
|
|
469
|
|
|
<!-- Title --> |
470
|
|
|
<p> |
471
|
|
|
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'give' ) ?></label> |
472
|
|
|
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $instance['title']; ?>" /> |
473
|
|
|
</p> |
474
|
|
|
|
475
|
|
|
|
476
|
|
|
<?php |
477
|
|
|
} // end FORM function |
478
|
|
|
|
479
|
|
|
} |
480
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.