admin
8 years ago
api
8 years ago
deprecated
8 years ago
donors
8 years ago
emails
8 years ago
forms
8 years ago
gateways
8 years ago
libraries
8 years ago
payments
8 years ago
actions.php
8 years ago
ajax-functions.php
8 years ago
class-give-async-process.php
8 years ago
class-give-background-updater.php
8 years ago
class-give-cache.php
8 years ago
class-give-cli-commands.php
8 years ago
class-give-cron.php
8 years ago
class-give-db-donor-meta.php
8 years ago
class-give-db-donors.php
8 years ago
class-give-db-form-meta.php
8 years ago
class-give-db-logs-meta.php
8 years ago
class-give-db-logs.php
8 years ago
class-give-db-meta.php
8 years ago
class-give-db-payment-meta.php
8 years ago
class-give-db-sequential-ordering.php
8 years ago
class-give-db.php
8 years ago
class-give-donate-form.php
8 years ago
class-give-donor.php
8 years ago
class-give-email-access.php
8 years ago
class-give-gravatars.php
8 years ago
class-give-html-elements.php
8 years ago
class-give-license-handler.php
8 years ago
class-give-logging.php
8 years ago
class-give-roles.php
8 years ago
class-give-scripts.php
8 years ago
class-give-session.php
8 years ago
class-give-stats.php
8 years ago
class-give-template-loader.php
8 years ago
class-give-tooltips.php
8 years ago
class-give-translation.php
8 years ago
class-notices.php
8 years ago
country-functions.php
8 years ago
currency-functions.php
8 years ago
error-tracking.php
8 years ago
filters.php
8 years ago
formatting.php
8 years ago
import-functions.php
8 years ago
install.php
8 years ago
login-register.php
8 years ago
misc-functions.php
8 years ago
plugin-compatibility.php
8 years ago
post-types.php
8 years ago
price-functions.php
8 years ago
process-donation.php
8 years ago
shortcodes.php
8 years ago
template-functions.php
8 years ago
user-functions.php
8 years ago
class-give-gravatars.php
552 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Donors Gravatars |
| 4 | * |
| 5 | * @package Give |
| 6 | * @subpackage Classes/Give_Donors_Gravatars |
| 7 | * @copyright Copyright (c) 2016, WordImpress |
| 8 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
| 9 | * @since 1.0 |
| 10 | */ |
| 11 | |
| 12 | // Exit if accessed directly. |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Give_Donors_Gravatars Class |
| 19 | * |
| 20 | * This class handles donors gravatars. |
| 21 | * |
| 22 | * @since 1.0 |
| 23 | */ |
| 24 | class Give_Donors_Gravatars { |
| 25 | |
| 26 | /** |
| 27 | * Class Constructor |
| 28 | * |
| 29 | * Set up the Give Donors Gravatars Class. |
| 30 | * |
| 31 | * @since 1.0 |
| 32 | * @access public |
| 33 | */ |
| 34 | public function __construct() { |
| 35 | $this->setup_actions(); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Setup the default hooks and actions |
| 40 | * |
| 41 | * @since 1.0 |
| 42 | * @access private |
| 43 | * |
| 44 | * @return void |
| 45 | */ |
| 46 | private function setup_actions() { |
| 47 | // add_action( 'widgets_init', array( $this, 'register_widget' ) ); |
| 48 | // add_shortcode( 'give_donors_gravatars', array( $this, 'shortcode' ) ); |
| 49 | // add_filter( 'give_settings_display', array( $this, 'settings' ) ); |
| 50 | // do_action( 'give_donors_gravatars_setup_actions' ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Utility function to check if a gravatar exists for a given email or id |
| 55 | * |
| 56 | * @see: https://gist.github.com/justinph/5197810 |
| 57 | * |
| 58 | * @since 1.0 |
| 59 | * @access public |
| 60 | * |
| 61 | * @param int|string|object $id_or_email A user ID, email address, or comment object |
| 62 | * |
| 63 | * @return bool If the gravatar exists or not |
| 64 | */ |
| 65 | public function validate_gravatar( $id_or_email ) { |
| 66 | //id or email code borrowed from wp-includes/pluggable.php |
| 67 | $email = ''; |
| 68 | if ( is_numeric( $id_or_email ) ) { |
| 69 | $id = (int) $id_or_email; |
| 70 | $user = get_userdata( $id ); |
| 71 | if ( $user ) { |
| 72 | $email = $user->user_email; |
| 73 | } |
| 74 | } elseif ( is_object( $id_or_email ) ) { |
| 75 | // No avatar for pingbacks or trackbacks |
| 76 | $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) ); |
| 77 | if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) { |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | if ( ! empty( $id_or_email->user_id ) ) { |
| 82 | $id = (int) $id_or_email->user_id; |
| 83 | $user = get_userdata( $id ); |
| 84 | if ( $user ) { |
| 85 | $email = $user->user_email; |
| 86 | } |
| 87 | } elseif ( ! empty( $id_or_email->comment_author_email ) ) { |
| 88 | $email = $id_or_email->comment_author_email; |
| 89 | } |
| 90 | } else { |
| 91 | $email = $id_or_email; |
| 92 | } |
| 93 | |
| 94 | $hashkey = md5( strtolower( trim( $email ) ) ); |
| 95 | $uri = 'http://www.gravatar.com/avatar/' . $hashkey . '?d=404'; |
| 96 | |
| 97 | $data = Give_Cache::get_group( $hashkey ); |
| 98 | |
| 99 | if ( is_null( $data ) ) { |
| 100 | $response = wp_remote_head( $uri ); |
| 101 | if ( is_wp_error( $response ) ) { |
| 102 | $data = 'not200'; |
| 103 | } else { |
| 104 | $data = $response['response']['code']; |
| 105 | } |
| 106 | Give_Cache::set_group( $hashkey, $data, $group = '', $expire = 60 * 5 ); |
| 107 | |
| 108 | } |
| 109 | if ( $data == '200' ) { |
| 110 | return true; |
| 111 | } else { |
| 112 | return false; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Get an array of all the log IDs using the Give Logging Class |
| 118 | * |
| 119 | * @since 1.0 |
| 120 | * @access public |
| 121 | * |
| 122 | * @param int $form_id Donation form id |
| 123 | * |
| 124 | * @return array IDs if logs, false otherwise |
| 125 | */ |
| 126 | public function get_log_ids( $form_id = '' ) { |
| 127 | // get log for this form |
| 128 | $logs = Give()->logs->get_logs( $form_id ); |
| 129 | |
| 130 | if ( $logs ) { |
| 131 | $log_ids = array(); |
| 132 | |
| 133 | // make an array with all the donor IDs |
| 134 | foreach ( $logs as $log ) { |
| 135 | $log_ids[] = $log->ID; |
| 136 | } |
| 137 | |
| 138 | return $log_ids; |
| 139 | } |
| 140 | |
| 141 | return null; |
| 142 | |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Get payment ID |
| 147 | * |
| 148 | * @since 1.0 |
| 149 | * @access public |
| 150 | * |
| 151 | * @param int $form_id Donation form id |
| 152 | * |
| 153 | * @return mixed |
| 154 | */ |
| 155 | public function get_payment_ids( $form_id = '' ) { |
| 156 | |
| 157 | $give_options = give_get_settings(); |
| 158 | |
| 159 | $log_ids = $this->get_log_ids( $form_id ); |
| 160 | |
| 161 | if ( $log_ids ) { |
| 162 | |
| 163 | $payment_ids = array(); |
| 164 | |
| 165 | foreach ( $log_ids as $id ) { |
| 166 | // get the payment ID for each corresponding log ID |
| 167 | $payment_ids[] = give_get_meta( $id, '_give_log_payment_id', true ); |
| 168 | } |
| 169 | |
| 170 | // remove donors who have donated more than once so we can have unique avatars |
| 171 | $unique_emails = array(); |
| 172 | |
| 173 | foreach ( $payment_ids as $key => $id ) { |
| 174 | |
| 175 | $email = give_get_meta( $id, '_give_payment_donor_email', true ); |
| 176 | |
| 177 | if ( isset ( $give_options['give_donors_gravatars_has_gravatar_account'] ) ) { |
| 178 | if ( ! $this->validate_gravatar( $email ) ) { |
| 179 | continue; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | $unique_emails[ $id ] = give_get_meta( $id, '_give_payment_donor_email', true ); |
| 184 | |
| 185 | } |
| 186 | |
| 187 | $unique_ids = array(); |
| 188 | |
| 189 | // strip duplicate emails |
| 190 | $unique_emails = array_unique( $unique_emails ); |
| 191 | |
| 192 | // convert the unique IDs back into simple array |
| 193 | foreach ( $unique_emails as $id => $email ) { |
| 194 | $unique_ids[] = $id; |
| 195 | } |
| 196 | |
| 197 | // randomize the payment IDs if enabled |
| 198 | if ( isset( $give_options['give_donors_gravatars_random_gravatars'] ) ) { |
| 199 | shuffle( $unique_ids ); |
| 200 | } |
| 201 | |
| 202 | // return our unique IDs |
| 203 | return $unique_ids; |
| 204 | |
| 205 | } |
| 206 | |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Gravatars |
| 211 | * |
| 212 | * @since 1.0 |
| 213 | * @access public |
| 214 | * |
| 215 | * @param int $form_id Donation form id. |
| 216 | * @param string $title Donors gravatars title. |
| 217 | * |
| 218 | * @return string |
| 219 | */ |
| 220 | public function gravatars( $form_id = false, $title = '' ) { |
| 221 | |
| 222 | // unique $payment_ids |
| 223 | $payment_ids = $this->get_payment_ids( $form_id ); |
| 224 | |
| 225 | $give_options = give_get_settings(); |
| 226 | |
| 227 | // return if no ID |
| 228 | if ( ! $form_id ) { |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | // minimum amount of donations before showing gravatars |
| 233 | // if the number of items in array is not greater or equal to the number specified, then exit |
| 234 | if ( isset( $give_options['give_donors_gravatars_min_purchases_required'] ) && '' != $give_options['give_donors_gravatars_min_purchases_required'] ) { |
| 235 | if ( ! ( count( $payment_ids ) >= $give_options['give_donors_gravatars_min_purchases_required'] ) ) { |
| 236 | return; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | ob_start(); |
| 241 | |
| 242 | $output = ''; |
| 243 | echo '<div id="give-purchase-gravatars">'; |
| 244 | |
| 245 | |
| 246 | if ( isset ( $title ) ) { |
| 247 | |
| 248 | if ( $title ) { |
| 249 | echo wp_kses_post( apply_filters( 'give_donors_gravatars_title', '<h3 class="give-gravatars-title">' . esc_attr( $title ) . '</h3>' ) ); |
| 250 | } elseif ( isset( $give_options['give_donors_gravatars_heading'] ) ) { |
| 251 | echo wp_kses_post( apply_filters( 'give_donors_gravatars_title', '<h3 class="give-gravatars-title">' . esc_attr( $give_options['give_donors_gravatars_heading'] ) . '</h2>' ) ); |
| 252 | } |
| 253 | |
| 254 | } |
| 255 | echo '<ul class="give-purchase-gravatars-list">'; |
| 256 | $i = 0; |
| 257 | |
| 258 | if ( $payment_ids ) { |
| 259 | foreach ( $payment_ids as $id ) { |
| 260 | |
| 261 | // Give saves a blank option even when the control is turned off, hence the extra check |
| 262 | if ( isset( $give_options['give_donors_gravatars_maximum_number'] ) && '' != $give_options['give_donors_gravatars_maximum_number'] && $i == $give_options['give_donors_gravatars_maximum_number'] ) { |
| 263 | continue; |
| 264 | } |
| 265 | |
| 266 | // get the payment meta |
| 267 | $payment_meta = give_get_meta( $id, '_give_payment_meta', true ); |
| 268 | |
| 269 | $user_info = $payment_meta['user_info']; |
| 270 | |
| 271 | // get donor's first name |
| 272 | $name = $user_info['first_name']; |
| 273 | |
| 274 | // get donor's email |
| 275 | $email = give_get_meta( $id, '_give_payment_donor_email', true ); |
| 276 | |
| 277 | // set gravatar size and provide filter |
| 278 | $size = isset( $give_options['give_donors_gravatars_gravatar_size'] ) ? apply_filters( 'give_donors_gravatars_gravatar_size', $give_options['give_donors_gravatars_gravatar_size'] ) : ''; |
| 279 | |
| 280 | // default image |
| 281 | $default_image = apply_filters( 'give_donors_gravatars_gravatar_default_image', false ); |
| 282 | |
| 283 | // assemble output |
| 284 | $output .= '<li>'; |
| 285 | |
| 286 | $output .= get_avatar( $email, $size, $default_image, $name ); |
| 287 | $output .= '</li>'; |
| 288 | |
| 289 | $i ++; |
| 290 | |
| 291 | } // end foreach |
| 292 | } |
| 293 | |
| 294 | echo wp_kses_post( $output ); |
| 295 | echo '</ul>'; |
| 296 | echo '</div>'; |
| 297 | |
| 298 | return apply_filters( 'give_donors_gravatars', ob_get_clean() ); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Register widget |
| 303 | * |
| 304 | * @since 1.0 |
| 305 | * @access public |
| 306 | * |
| 307 | * @return void |
| 308 | */ |
| 309 | public function register_widget() { |
| 310 | register_widget( 'Give_Donors_Gravatars_Widget' ); |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Shortcode |
| 315 | * |
| 316 | * @since 1.0 |
| 317 | * @access public |
| 318 | * |
| 319 | * @param array $atts Shortcode attribures. |
| 320 | * @param string $content Shortcode content. |
| 321 | * |
| 322 | * @return string |
| 323 | */ |
| 324 | public function shortcode( $atts, $content = null ) { |
| 325 | |
| 326 | $atts = shortcode_atts( array( |
| 327 | 'id' => '', |
| 328 | 'title' => '' |
| 329 | ), $atts, 'give_donors_gravatars' ); |
| 330 | |
| 331 | // if no ID is passed on single give_forms pages, get the correct ID |
| 332 | if ( is_singular( 'give_forms' ) ) { |
| 333 | $id = get_the_ID(); |
| 334 | } |
| 335 | |
| 336 | $content = $this->gravatars( $atts['id'], $atts['title'] ); |
| 337 | |
| 338 | return $content; |
| 339 | |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Settings |
| 344 | * |
| 345 | * @since 1.0 |
| 346 | * @access public |
| 347 | * |
| 348 | * @param array $settings Gravatar settings. |
| 349 | * |
| 350 | * @return array Gravatar settings. |
| 351 | */ |
| 352 | public function settings( $settings ) { |
| 353 | |
| 354 | $give_gravatar_settings = array( |
| 355 | array( |
| 356 | 'name' => esc_html__( 'Donator Gravatars', 'give' ), |
| 357 | 'desc' => '<hr>', |
| 358 | 'id' => 'give_title', |
| 359 | 'type' => 'give_title' |
| 360 | ), |
| 361 | array( |
| 362 | 'name' => esc_html__( 'Heading', 'give' ), |
| 363 | 'desc' => esc_html__( 'The heading to display above the Gravatars.', 'give' ), |
| 364 | 'type' => 'text', |
| 365 | 'id' => 'give_donors_gravatars_heading' |
| 366 | ), |
| 367 | array( |
| 368 | 'name' => esc_html__( 'Gravatar Size', 'give' ), |
| 369 | 'desc' => esc_html__( 'The size of each Gravatar in pixels (512px maximum).', 'give' ), |
| 370 | 'type' => 'text_small', |
| 371 | 'id' => 'give_donors_gravatars_gravatar_size', |
| 372 | 'default' => '64' |
| 373 | ), |
| 374 | array( |
| 375 | 'name' => esc_html__( 'Minimum Unique Donations Required', 'give' ), |
| 376 | 'desc' => esc_html__( 'The minimum number of unique donations a form must have before the Gravatars are shown. Leave blank for no minimum.', 'give' ), |
| 377 | 'type' => 'text_small', |
| 378 | 'id' => 'give_donors_gravatars_min_purchases_required', |
| 379 | ), |
| 380 | array( |
| 381 | 'name' => esc_html__( 'Maximum Gravatars To Show', 'give' ), |
| 382 | 'desc' => esc_html__( 'The maximum number of gravatars to show. Leave blank for no limit.', 'give' ), |
| 383 | 'type' => 'text', |
| 384 | 'id' => 'give_donors_gravatars_maximum_number', |
| 385 | 'default' => '20', |
| 386 | ), |
| 387 | array( |
| 388 | 'name' => esc_html__( 'Gravatar Visibility', 'give' ), |
| 389 | 'desc' => esc_html__( 'Show only donors with a Gravatar account.', 'give' ), |
| 390 | 'id' => 'give_donors_gravatars_has_gravatar_account', |
| 391 | 'type' => 'checkbox', |
| 392 | ), |
| 393 | array( |
| 394 | 'name' => esc_html__( 'Randomize Gravatars', 'give' ), |
| 395 | 'desc' => esc_html__( 'Randomize the Gravatars.', 'give' ), |
| 396 | 'id' => 'give_donors_gravatars_random_gravatars', |
| 397 | 'type' => 'checkbox', |
| 398 | ), |
| 399 | ); |
| 400 | |
| 401 | return array_merge( $settings, $give_gravatar_settings ); |
| 402 | } |
| 403 | |
| 404 | } |
| 405 | |
| 406 | |
| 407 | /** |
| 408 | * Give_Donors_Gravatars_Widget Class |
| 409 | * |
| 410 | * This class handles donors gravatars |
| 411 | * |
| 412 | * @since 1.0 |
| 413 | */ |
| 414 | class Give_Donors_Gravatars_Widget extends WP_Widget { |
| 415 | |
| 416 | /** |
| 417 | * Widget constructor |
| 418 | * |
| 419 | * @since 1.0 |
| 420 | * @access public |
| 421 | */ |
| 422 | public function __construct() { |
| 423 | |
| 424 | // widget settings |
| 425 | $widget_ops = array( |
| 426 | 'classname' => 'give-donors-gravatars', |
| 427 | 'description' => esc_html__( 'Displays gravatars of people who have donated using your your form. Will only show on the single form page.', 'give' ), |
| 428 | ); |
| 429 | |
| 430 | // widget control settings |
| 431 | $control_ops = array( |
| 432 | 'width' => 250, |
| 433 | 'height' => 350, |
| 434 | 'id_base' => 'give_gravatars_widget' |
| 435 | ); |
| 436 | |
| 437 | // create the widget |
| 438 | parent::__construct( |
| 439 | 'give_donors_gravatars_widget', |
| 440 | esc_html__( 'Give Donors Gravatars', 'give' ), |
| 441 | $widget_ops, |
| 442 | $control_ops |
| 443 | ); |
| 444 | |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * Donors gravatars widget content |
| 449 | * |
| 450 | * Outputs the content of the widget |
| 451 | * |
| 452 | * @since 1.0 |
| 453 | * @access public |
| 454 | * |
| 455 | * @param array $args Display arguments including 'before_title', 'after_title', 'before_widget', and 'after_widget'. |
| 456 | * @param array $instance Settings for the current Links widget instance. |
| 457 | * |
| 458 | * @return void |
| 459 | */ |
| 460 | public function widget( $args, $instance ) { |
| 461 | |
| 462 | $defaults = array( |
| 463 | 'before_widget' => '', |
| 464 | 'after_widget' => '', |
| 465 | 'before_title' => '', |
| 466 | 'after_title' => '', |
| 467 | ); |
| 468 | |
| 469 | $args = wp_parse_args( $args, $defaults ); |
| 470 | |
| 471 | if ( ! is_singular( 'give_forms' ) ) { |
| 472 | return; |
| 473 | } |
| 474 | |
| 475 | // Variables from widget settings |
| 476 | $title = apply_filters( 'widget_title', $instance['title'] ); |
| 477 | |
| 478 | $output = ''; |
| 479 | |
| 480 | // Used by themes. Opens the widget |
| 481 | $output .= $args['before_widget']; |
| 482 | |
| 483 | // Display the widget title |
| 484 | if ( $title ) { |
| 485 | $output .= $args['before_title'] . $title . $args['after_title']; |
| 486 | } |
| 487 | |
| 488 | $gravatars = new Give_Donors_Gravatars(); |
| 489 | |
| 490 | $output .= $gravatars->gravatars( get_the_ID(), null ); // remove title |
| 491 | |
| 492 | // Used by themes. Closes the widget |
| 493 | $output .= $args['after_widget']; |
| 494 | |
| 495 | echo wp_kses_post( $output ); |
| 496 | } |
| 497 | |
| 498 | /** |
| 499 | * Update donors gravatars |
| 500 | * |
| 501 | * Processes widget options to be saved. |
| 502 | * |
| 503 | * @since 1.0 |
| 504 | * @access public |
| 505 | * |
| 506 | * @param array $new_instance New settings for this instance as input by the user via WP_Widget::form(). |
| 507 | * @param array $old_instance Old settings for this instance. |
| 508 | * |
| 509 | * @return array Updated settings to save. |
| 510 | */ |
| 511 | public function update( $new_instance, $old_instance ) { |
| 512 | |
| 513 | $instance = $old_instance; |
| 514 | |
| 515 | $instance['title'] = wp_strip_all_tags( $new_instance['title'] ); |
| 516 | |
| 517 | return $instance; |
| 518 | |
| 519 | } |
| 520 | |
| 521 | /** |
| 522 | * Output donors gravatars |
| 523 | * |
| 524 | * Displays the actual form on the widget page. |
| 525 | * |
| 526 | * @since 1.0 |
| 527 | * @access public |
| 528 | * |
| 529 | * @param array $instance Current settings. |
| 530 | * |
| 531 | * @return void |
| 532 | */ |
| 533 | public function form( $instance ) { |
| 534 | |
| 535 | // Set up some default widget settings. |
| 536 | $defaults = array( |
| 537 | 'title' => '', |
| 538 | ); |
| 539 | |
| 540 | $instance = wp_parse_args( (array) $instance, $defaults ); ?> |
| 541 | |
| 542 | <!-- Title --> |
| 543 | <p> |
| 544 | <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'give' ) ?></label> |
| 545 | <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" /> |
| 546 | </p> |
| 547 | |
| 548 | <?php |
| 549 | } |
| 550 | |
| 551 | } |
| 552 |