actions.php
3 years ago
backward-compatibility.php
7 years ago
class-give-donor-stats.php
6 years ago
class-give-donor-wall.php
3 years ago
class-give-donors-query.php
5 years ago
frontend-donor-functions.php
3 years ago
class-give-donor-wall.php
662 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Donor Wall |
| 4 | * |
| 5 | * @package Give |
| 6 | * @subpackage Classes/Give_Donor_Wall |
| 7 | * @copyright Copyright (c) 2020, GiveWP |
| 8 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
| 9 | * @since 1.0 |
| 10 | */ |
| 11 | |
| 12 | // Exit if accessed directly. |
| 13 | use Give\Donations\ValueObjects\DonationMetaKeys; |
| 14 | |
| 15 | if (!defined('ABSPATH')) { |
| 16 | exit; |
| 17 | } |
| 18 | |
| 19 | |
| 20 | /** |
| 21 | * Give_Donor_Wall Class |
| 22 | * |
| 23 | * This class handles donors. |
| 24 | * |
| 25 | * @since 2.2.0 |
| 26 | */ |
| 27 | class Give_Donor_Wall { |
| 28 | |
| 29 | /** |
| 30 | * Instance. |
| 31 | * |
| 32 | * @since 2.2.0 |
| 33 | * @access private |
| 34 | * @var Give_Donor_Wall |
| 35 | */ |
| 36 | private static $instance; |
| 37 | |
| 38 | /** |
| 39 | * Singleton pattern. |
| 40 | * |
| 41 | * @since 2.2.0 |
| 42 | * @access private |
| 43 | */ |
| 44 | private function __construct() { |
| 45 | } |
| 46 | |
| 47 | |
| 48 | /** |
| 49 | * Get instance. |
| 50 | * |
| 51 | * @since 2.2.0 |
| 52 | * @access public |
| 53 | * @return Give_Donor_Wall |
| 54 | */ |
| 55 | public static function get_instance() { |
| 56 | if ( null === static::$instance ) { |
| 57 | self::$instance = new static(); |
| 58 | |
| 59 | self::$instance->setup_actions(); |
| 60 | } |
| 61 | |
| 62 | return self::$instance; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Setup the default hooks and actions |
| 67 | * |
| 68 | * @since 2.2.0 |
| 69 | * |
| 70 | * @return void |
| 71 | */ |
| 72 | public function setup_actions() { |
| 73 | |
| 74 | add_shortcode( 'give_donor_wall', [ $this, 'render_shortcode' ] ); |
| 75 | |
| 76 | add_action( 'wp_ajax_give_get_donor_comments', [ $this, 'ajax_handler' ] ); |
| 77 | add_action( 'wp_ajax_nopriv_give_get_donor_comments', [ $this, 'ajax_handler' ] ); |
| 78 | |
| 79 | } |
| 80 | |
| 81 | |
| 82 | /** |
| 83 | * Displays donors in a grid layout. |
| 84 | * |
| 85 | * @since 2.27.0 Moved AJAX nonce verification to ajax_handler method. |
| 86 | * @since 2.2.0 |
| 87 | * |
| 88 | * @param array $atts { |
| 89 | * Optional. Attributes of the donor wall shortcode. |
| 90 | * |
| 91 | * @type int $donors_per_page Number of donors per page. Default '20'. |
| 92 | * @type int $form_id The donation form to filter donors by. Default is all forms (no filter). |
| 93 | * @type bool $paged Whether to paginate donors. Default 'true'. |
| 94 | * @type string $ids A comma-separated list of donor IDs to display. Default empty. |
| 95 | * @type string $columns Maximum columns to display. Default 'best-fit'. |
| 96 | * Accepts 'best-fit', '1', '2', '3', '4'. |
| 97 | * @type bool $show_avatar Whether to display the donor's gravatar image if available. Default 'true'. |
| 98 | * @type bool $show_name Whether to display the donor's full name, first and last. Default 'true'. |
| 99 | * @type bool $show_company_name Whether to display the donor's company name. Default 'false'. |
| 100 | * @type bool $show_total Whether to display the donor's donation amount. Default 'true'. |
| 101 | * @type bool $show_comments Whether to display the donor's comment if they left one. Default 'true'. |
| 102 | * @type int $comment_length The number of words to display for the comments before a "Read more" field |
| 103 | * @type int $only_comments Whether to display the donors only with comment. Default 'false'. |
| 104 | * @type bool $show_time Whether to display date of the last donation. Default 'true'. |
| 105 | * |
| 106 | * @type string $readmore_text Link label for modal in which donor can read full comment. |
| 107 | * @type string $loadmore_text Button label which will load more donor comments. |
| 108 | * @type int $avatar_size Avatar image size in pixels without the "px". Default "75" |
| 109 | * @type string $orderby The order in which you want the donations to appear. |
| 110 | * Currently we are using this attribute internally and, it will sort donations by created date. |
| 111 | * @type string $order The order in which you want the donors to appear. Accepts "ASC". "DESC". |
| 112 | * |
| 113 | * } |
| 114 | * @return string|bool The markup of the form grid or false. |
| 115 | */ |
| 116 | public function render_shortcode( $atts ) { |
| 117 | |
| 118 | $give_settings = give_get_settings(); |
| 119 | |
| 120 | $atts = $this->parse_atts( $atts ); |
| 121 | $donations = $this->get_donation_data( $atts ); |
| 122 | $html = ''; |
| 123 | |
| 124 | if ( $donations ) { |
| 125 | |
| 126 | ob_start(); |
| 127 | |
| 128 | foreach ( $donations as $donation ) { |
| 129 | $donor = new Give_Donor($donation['_give_payment_donor_id']); |
| 130 | // Give/templates/shortcode-donor-wall.php. |
| 131 | give_get_template( |
| 132 | 'shortcode-donor-wall', |
| 133 | [ |
| 134 | $donation, |
| 135 | $give_settings, |
| 136 | $atts, |
| 137 | $donor |
| 138 | ] |
| 139 | ); |
| 140 | } |
| 141 | |
| 142 | $html = ob_get_clean(); |
| 143 | |
| 144 | // Return only donor html. |
| 145 | if ( |
| 146 | isset( $atts['only_donor_html'] ) |
| 147 | && wp_doing_ajax() |
| 148 | && $atts['only_donor_html'] |
| 149 | ) { |
| 150 | return $html; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | $temp_atts = $atts; |
| 155 | $temp_atts['paged'] = $atts['paged'] + 1; |
| 156 | |
| 157 | $more_btn_html = sprintf( |
| 158 | '<input type="hidden" class="give-donor-wall-shortcode-attrs" data-shortcode="%s" data-nonce="%s">', |
| 159 | rawurlencode( http_build_query( $atts ) ), |
| 160 | wp_create_nonce( 'givewp-donor-wall-more' ) |
| 161 | ); |
| 162 | |
| 163 | if ( $this->has_donations( $temp_atts ) ) { |
| 164 | $more_btn_html .= sprintf( |
| 165 | '<button class="give-donor__load_more give-button-with-loader"><span class="give-loading-animation"></span>%1$s</button>', |
| 166 | $atts['loadmore_text'] |
| 167 | ); |
| 168 | } |
| 169 | |
| 170 | $html = $html |
| 171 | ? sprintf( |
| 172 | '<div class="give-wrap give-grid-ie-utility"><div class="give-grid give-grid--%1$s">%2$s</div>%3$s</div>', |
| 173 | esc_attr( $atts['columns'] ), |
| 174 | $html, |
| 175 | $more_btn_html |
| 176 | ) |
| 177 | : ''; |
| 178 | |
| 179 | return $html; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Parse shortcode attributes |
| 184 | * |
| 185 | * @since 2.2.0 |
| 186 | * @access public |
| 187 | * |
| 188 | * @param array $atts Shortcode attributes. |
| 189 | * |
| 190 | * @return array |
| 191 | */ |
| 192 | public function parse_atts( $atts ) { |
| 193 | $atts = shortcode_atts( |
| 194 | [ |
| 195 | 'donors_per_page' => 12, |
| 196 | 'form_id' => 0, |
| 197 | 'paged' => 1, |
| 198 | 'ids' => '', |
| 199 | 'cats' => '', |
| 200 | 'tags' => '', |
| 201 | 'columns' => '3', |
| 202 | 'anonymous' => true, |
| 203 | 'show_avatar' => true, |
| 204 | 'show_name' => true, |
| 205 | 'show_company_name' => false, |
| 206 | 'show_form' => false, |
| 207 | 'show_total' => true, |
| 208 | 'show_comments' => true, |
| 209 | 'show_tributes' => true, |
| 210 | 'comment_length' => 140, |
| 211 | 'only_comments' => false, |
| 212 | 'readmore_text' => esc_html__( 'Read more', 'give' ), |
| 213 | 'loadmore_text' => esc_html__( 'Load more', 'give' ), |
| 214 | 'avatar_size' => 75, |
| 215 | 'color' => "#219653", |
| 216 | 'orderby' => 'post_date', |
| 217 | 'order' => 'DESC', |
| 218 | 'hide_empty' => true, // Deprecated in 2.3.0 |
| 219 | 'only_donor_html' => false, // Only for internal use., |
| 220 | 'show_time' => true, |
| 221 | ], |
| 222 | $atts |
| 223 | ); |
| 224 | |
| 225 | // Validate boolean attributes. |
| 226 | $boolean_attributes = [ |
| 227 | 'anonymous', |
| 228 | 'show_avatar', |
| 229 | 'show_name', |
| 230 | 'show_company_name', |
| 231 | 'show_total', |
| 232 | 'show_comments', |
| 233 | 'show_tributes', |
| 234 | 'hide_empty', |
| 235 | 'only_comments', |
| 236 | 'only_donor_html', |
| 237 | 'show_time' |
| 238 | ]; |
| 239 | |
| 240 | foreach ( $boolean_attributes as $att ) { |
| 241 | // Convert numeric to boolean. |
| 242 | // It will prevent condition check against boolean value. |
| 243 | if ( is_numeric( $atts[ $att ] ) ) { |
| 244 | $atts[ $att ] = (bool) $atts[ $att ]; |
| 245 | } |
| 246 | |
| 247 | $atts[ $att ] = filter_var( $atts[ $att ], FILTER_VALIDATE_BOOLEAN ); |
| 248 | } |
| 249 | |
| 250 | // Validate numeric attributes. |
| 251 | $numeric_attributes = [ |
| 252 | 'donors_per_page', |
| 253 | 'paged', |
| 254 | 'comment_length', |
| 255 | 'avatar_size', |
| 256 | ]; |
| 257 | |
| 258 | foreach ( $numeric_attributes as $att ) { |
| 259 | // It will prevent condition check against numeric value. |
| 260 | $atts[ $att ] = absint( $atts[ $att ] ); |
| 261 | } |
| 262 | |
| 263 | // Validate comma separated numeric attributes and keep original data format ( comma separated string). |
| 264 | if ( ! empty( $atts['ids'] ) ) { |
| 265 | $atts['ids'] = implode( ',', $this->split_string($atts['ids'], 'absint') ); |
| 266 | } |
| 267 | |
| 268 | // Validate Form IDs |
| 269 | if ( ! empty( $atts['form_id'] ) ) { |
| 270 | $atts['form_id'] = implode( ',', $this->split_string($atts['form_id'], 'absint') ); |
| 271 | } |
| 272 | |
| 273 | // Donation form categories |
| 274 | if ( ! empty( $atts['cats'] ) ) { |
| 275 | $atts['cats'] = $this->split_string($atts['cats']); |
| 276 | } |
| 277 | |
| 278 | // Donation form tags |
| 279 | if ( ! empty( $atts['tags'] ) ) { |
| 280 | $atts['tags'] = $this->split_string($atts['tags']); |
| 281 | } |
| 282 | |
| 283 | return $atts; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Get donors |
| 288 | * |
| 289 | * @since 2.2.0 |
| 290 | * @access public |
| 291 | * |
| 292 | * @param array $donor_query Donor query. |
| 293 | * |
| 294 | * @return array |
| 295 | */ |
| 296 | public function get_donors( $donor_query ) { |
| 297 | $donor_query = new Give_Donors_Query( $donor_query ); |
| 298 | |
| 299 | return $donor_query->get_donors(); |
| 300 | } |
| 301 | |
| 302 | |
| 303 | /** |
| 304 | * This function should return donor comment for ajax request. |
| 305 | * |
| 306 | * @since 2.27.0 Check nonce for AJAX request to prevent scrapping, see https://github.com/impress-org/givewp/issues/6374. |
| 307 | * @since 2.2.0 |
| 308 | * @access public |
| 309 | */ |
| 310 | public function ajax_handler() { |
| 311 | $shortcode_atts = array_map( 'give_clean', wp_parse_args( rawurldecode( $_POST['data'] ) ) ); // @codingStandardsIgnoreLine |
| 312 | |
| 313 | // Get next page donor comments. |
| 314 | $shortcode_atts['paged'] = $shortcode_atts['paged'] + 1; |
| 315 | $shortcode_atts['only_donor_html'] = true; |
| 316 | |
| 317 | check_ajax_referer( 'givewp-donor-wall-more', 'nonce' ); |
| 318 | |
| 319 | $donors_comment_html = $this->render_shortcode( $shortcode_atts ); |
| 320 | |
| 321 | // Check if donor comment remaining. |
| 322 | $temp_atts = $shortcode_atts; |
| 323 | $temp_atts['paged'] = $shortcode_atts['paged'] + 1; |
| 324 | $has_donors = $this->has_donations( $temp_atts ) ? 1 : 0; |
| 325 | |
| 326 | // Remove internal shortcode param. |
| 327 | unset( $shortcode_atts['only_donor_html'] ); |
| 328 | |
| 329 | wp_send_json( |
| 330 | [ |
| 331 | 'shortcode' => rawurlencode( http_build_query( $shortcode_atts ) ), |
| 332 | 'html' => $donors_comment_html, |
| 333 | 'remaining' => $has_donors, |
| 334 | ] |
| 335 | ); |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Get query params |
| 340 | * |
| 341 | * @since 2.24.1 |
| 342 | * @since 2.3.0 |
| 343 | * |
| 344 | * @param array $atts |
| 345 | * |
| 346 | * @return array |
| 347 | */ |
| 348 | private function get_query_param( $atts = [] ) { |
| 349 | $valid_order = [ 'ASC', 'DESC' ]; |
| 350 | $valid_orderby = [ 'post_date', 'donation_amount' ]; |
| 351 | |
| 352 | $query_atts = []; |
| 353 | |
| 354 | $query_atts['order'] = in_array( $atts['order'], $valid_order ) ? $atts['order'] : 'DESC'; |
| 355 | $query_atts['orderby'] = in_array( $atts['orderby'], $valid_orderby ) ? $atts['orderby'] : 'post_date'; |
| 356 | $query_atts['limit'] = absint( $atts['donors_per_page'] ); |
| 357 | $query_atts['offset'] = absint( $atts['donors_per_page'] * ( $atts['paged'] - 1 ) ); |
| 358 | $query_atts['form_id'] = implode( '\',\'', array_map( 'absint', explode( ',', $atts['form_id'] ) ) ); |
| 359 | $query_atts['ids'] = implode( '\',\'', array_map( 'absint', explode( ',', $atts['ids'] ) ) ); |
| 360 | $query_atts['cats'] = $atts['cats']; |
| 361 | $query_atts['tags'] = $atts['tags']; |
| 362 | $query_atts['only_comments'] = ( true === $atts['only_comments'] ); |
| 363 | $query_atts['anonymous'] = ( true === $atts['anonymous'] ); |
| 364 | |
| 365 | return $query_atts; |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Get donation data. |
| 370 | * |
| 371 | * @since 2.27.0 Change to read comment from donations meta table |
| 372 | * @since 2.3.0 |
| 373 | * |
| 374 | * @param array $atts |
| 375 | * |
| 376 | * @return array |
| 377 | */ |
| 378 | private function get_donation_data( $atts = [] ) { |
| 379 | global $wpdb; |
| 380 | |
| 381 | // Bailout if donation does not exist. |
| 382 | if ( ! ( $donation_ids = $this->get_donations( $atts ) ) ) { |
| 383 | return []; |
| 384 | } |
| 385 | |
| 386 | $donation_ids = ! empty( $donation_ids ) |
| 387 | ? '\'' . implode( '\',\'', $donation_ids ) . '\'' |
| 388 | : ''; |
| 389 | |
| 390 | // Backward compatibility |
| 391 | $donation_id_col = Give()->payment_meta->get_meta_type() . '_id'; |
| 392 | |
| 393 | $sql = "SELECT m1.*, p1.post_date as donation_date FROM {$wpdb->donationmeta} as m1 |
| 394 | INNER JOIN {$wpdb->posts} as p1 ON (m1.{$donation_id_col}=p1.ID) |
| 395 | WHERE m1.{$donation_id_col} IN ( {$donation_ids} ) |
| 396 | ORDER BY FIELD( p1.ID, {$donation_ids} ) |
| 397 | "; |
| 398 | |
| 399 | $results = (array) $wpdb->get_results( $sql ); |
| 400 | |
| 401 | if ( ! empty( $results ) ) { |
| 402 | $temp = []; |
| 403 | |
| 404 | /* @var stdClass $result */ |
| 405 | foreach ( $results as $result ) { |
| 406 | $temp[ $result->{$donation_id_col} ][ $result->meta_key ] = maybe_unserialize( $result->meta_value ); |
| 407 | |
| 408 | // Set donation date. |
| 409 | if ( empty( $temp[ $result->{$donation_id_col} ]['donation_date'] ) ) { |
| 410 | $temp[ $result->{$donation_id_col} ]['donation_date'] = $result->donation_date; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | if ( ! empty( $temp ) ) { |
| 415 | foreach ( $temp as $donation_id => $donation_data ) { |
| 416 | $temp[ $donation_id ]['donation_id'] = $donation_id; |
| 417 | |
| 418 | $temp[ $donation_id ]['name_initial'] = give_get_name_initial( |
| 419 | [ |
| 420 | 'firstname' => $donation_data['_give_donor_billing_first_name'], |
| 421 | 'lastname' => $donation_data['_give_donor_billing_last_name'], |
| 422 | ] |
| 423 | ); |
| 424 | |
| 425 | $temp[$donation_id]['donor_comment'] = give_get_payment_meta( |
| 426 | $donation_id, |
| 427 | DonationMetaKeys::COMMENT |
| 428 | ); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | $results = ! empty( $temp ) ? $temp : []; |
| 433 | } |
| 434 | |
| 435 | return $results; |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * Get donation list for specific query |
| 440 | * |
| 441 | * @since 2.3.0 |
| 442 | * |
| 443 | * @param array $atts |
| 444 | * |
| 445 | * @return array |
| 446 | */ |
| 447 | private function get_donations( $atts = [] ) { |
| 448 | global $wpdb; |
| 449 | |
| 450 | // Backward compatibility |
| 451 | $donation_id_col = Give()->payment_meta->get_meta_type() . '_id'; |
| 452 | |
| 453 | $query_params = $this->get_query_param( $atts ); |
| 454 | |
| 455 | $sql = "SELECT p1.ID FROM {$wpdb->posts} as p1"; |
| 456 | $where = " WHERE p1.post_status IN ('publish') AND p1.post_type = 'give_payment'"; |
| 457 | |
| 458 | // exclude donation with zero amount from result. |
| 459 | $sql .= " INNER JOIN {$wpdb->donationmeta} as m1 ON (p1.ID = m1.{$donation_id_col})"; |
| 460 | $where .= " AND m1.meta_key='_give_payment_total' AND m1.meta_value>0"; |
| 461 | |
| 462 | if ( $query_params['form_id'] ) { |
| 463 | $sql .= " INNER JOIN {$wpdb->donationmeta} as m2 ON (p1.ID = m2.{$donation_id_col})"; |
| 464 | $where .= " AND m2.meta_key='_give_payment_form_id' AND m2.meta_value IN ('{$query_params['form_id']}')"; |
| 465 | } |
| 466 | |
| 467 | // Get donations only from specific donors. |
| 468 | if ( $query_params['ids'] ) { |
| 469 | $sql .= " INNER JOIN {$wpdb->donationmeta} as m3 ON (p1.ID = m3.{$donation_id_col})"; |
| 470 | $where .= " AND m3.meta_key='_give_payment_donor_id' AND m3.meta_value IN ('{$query_params['ids']}')"; |
| 471 | } |
| 472 | |
| 473 | // exclude donations which does not has donor comment. |
| 474 | if ( $query_params['only_comments'] ) { |
| 475 | $sql .= " INNER JOIN {$wpdb->give_comments} as gc1 ON (p1.ID = gc1.comment_parent)"; |
| 476 | $where .= " AND gc1.comment_type='donor_donation'"; |
| 477 | } |
| 478 | |
| 479 | // exclude anonymous donation form query based on query parameters. |
| 480 | if ( |
| 481 | ! $query_params['anonymous'] |
| 482 | || $query_params['only_comments'] |
| 483 | ) { |
| 484 | $where .= " AND p1.ID NOT IN ( SELECT DISTINCT({$donation_id_col}) FROM {$wpdb->donationmeta} WHERE meta_key='_give_anonymous_donation' AND meta_value='1')"; |
| 485 | } |
| 486 | |
| 487 | // Handle Taxonomy |
| 488 | $args = [ |
| 489 | 'post_type' => 'give_forms', |
| 490 | 'posts_per_page' => -1, |
| 491 | 'fields' => 'ids', |
| 492 | 'tax_query' => [], |
| 493 | ]; |
| 494 | |
| 495 | // Categories |
| 496 | if ( is_array($atts['cats'])) { |
| 497 | $args['tax_query']['conditions'] = ['relation' => 'OR']; |
| 498 | |
| 499 | foreach ($atts['cats'] as $category) { |
| 500 | $args['tax_query']['conditions'][] = [ |
| 501 | 'operator' => 'IN', |
| 502 | 'taxonomy' => 'give_forms_category', |
| 503 | 'field' => 'slug', |
| 504 | 'terms' => $category, |
| 505 | ]; |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | // Tags |
| 510 | if ( is_array($atts['tags'])) { |
| 511 | if (empty($args['tax_query'])) { |
| 512 | $args['tax_query']['conditions'] = ['relation' => 'OR']; |
| 513 | } |
| 514 | |
| 515 | foreach($atts['tags'] as $tag) { |
| 516 | $args['tax_query']['conditions'][] = [ |
| 517 | 'operator' => 'IN', |
| 518 | 'taxonomy' => 'give_forms_tag', |
| 519 | 'field' => 'slug', |
| 520 | 'terms' => $tag, |
| 521 | ]; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | if ( ! empty( $args['tax_query'] ) ) { |
| 526 | $query = new WP_Query( $args ); |
| 527 | |
| 528 | if ( ! empty($query->posts) ) { |
| 529 | $form_ids = implode("','", $query->posts ); |
| 530 | $sql .= " INNER JOIN {$wpdb->donationmeta} as m4 ON (p1.ID = m4.{$donation_id_col})"; |
| 531 | $where .= " AND m4.meta_key='_give_payment_form_id' AND m4.meta_value IN ('{$form_ids}')"; |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | // order by query based on parameter. |
| 536 | if ( 'donation_amount' === $query_params['orderby'] ) { |
| 537 | $order = " ORDER BY m1.meta_value+0 {$query_params['order']}"; |
| 538 | } else { |
| 539 | $order = " ORDER BY p1.{$query_params['orderby']} {$query_params['order']}, p1.ID {$query_params['order']}"; |
| 540 | } |
| 541 | |
| 542 | $limit = " LIMIT {$query_params['limit']}"; |
| 543 | $offset = " OFFSET {$query_params['offset']}"; |
| 544 | |
| 545 | $sql .= $where . $order . $limit . $offset; |
| 546 | |
| 547 | return $wpdb->get_col( $sql ); |
| 548 | } |
| 549 | |
| 550 | /** |
| 551 | * Get donor comments |
| 552 | * |
| 553 | * @since 2.3.0 |
| 554 | * |
| 555 | * @param array $donations_data |
| 556 | * |
| 557 | * @return array |
| 558 | */ |
| 559 | private function get_donor_comments( $donations_data = [] ) { |
| 560 | global $wpdb; |
| 561 | $comments = []; |
| 562 | |
| 563 | // Bailout. |
| 564 | if ( empty( $donations_data ) ) { |
| 565 | return $comments; |
| 566 | } |
| 567 | |
| 568 | // Backward compatibility. |
| 569 | if ( |
| 570 | ! give_has_upgrade_completed( 'v230_move_donor_note' ) |
| 571 | || ! give_has_upgrade_completed( 'v230_move_donation_note' ) |
| 572 | ) { |
| 573 | foreach ( $donations_data as $id => $data ) { |
| 574 | $comment = give_get_donor_donation_comment( $id, $data['_give_payment_donor_id'] ); |
| 575 | $comments[ $id ] = ! empty( $comment ) ? $comment->comment_content : ''; |
| 576 | } |
| 577 | |
| 578 | return $comments; |
| 579 | } |
| 580 | |
| 581 | $sql = "SELECT c1.comment_parent as donation_id, c1.comment_content as comment FROM {$wpdb->give_comments} as c1"; |
| 582 | $sql .= " INNER JOIN {$wpdb->give_commentmeta} as cm1 ON (c1.comment_ID=cm1.give_comment_id)"; |
| 583 | $where = []; |
| 584 | |
| 585 | foreach ( $donations_data as $id => $data ) { |
| 586 | // Do not fetch comment for anonymous donation. |
| 587 | if ( ! empty( $data['_give_anonymous_donation'] ) ) { |
| 588 | continue; |
| 589 | } |
| 590 | |
| 591 | $where[] = "(c1.comment_parent={$id} AND cm1.meta_key='_give_donor_id' AND cm1.meta_value={$data['_give_payment_donor_id']})"; |
| 592 | } |
| 593 | |
| 594 | $where = ' WHERE ' . implode( ' OR ', $where ); |
| 595 | $where .= " AND c1.comment_type='donor_donation'"; |
| 596 | |
| 597 | $sql = $sql . $where; |
| 598 | |
| 599 | $comments = (array) $wpdb->get_results( $sql ); |
| 600 | |
| 601 | if ( ! empty( $comments ) ) { |
| 602 | $comments = array_combine( |
| 603 | wp_list_pluck( $comments, 'donation_id' ), |
| 604 | wp_list_pluck( $comments, 'comment' ) |
| 605 | ); |
| 606 | } |
| 607 | |
| 608 | return $comments; |
| 609 | } |
| 610 | |
| 611 | /** |
| 612 | * Check if donation exist or not for specific query |
| 613 | * |
| 614 | * @since 2.3.0 |
| 615 | * |
| 616 | * @param array $atts |
| 617 | * |
| 618 | * @return bool |
| 619 | */ |
| 620 | private function has_donations( $atts = [] ) { |
| 621 | return (bool) $this->get_donations( $atts ); |
| 622 | } |
| 623 | |
| 624 | /** |
| 625 | * @since 2.20.0 |
| 626 | * |
| 627 | * @param string $string |
| 628 | * @param null|callable $filter |
| 629 | * @param string $separator |
| 630 | * |
| 631 | * @return array |
| 632 | */ |
| 633 | private function split_string($string, $filter = null, $separator = ',') { |
| 634 | if ( false === strpos( $string, $separator ) ) { |
| 635 | $string = trim( $string ); |
| 636 | |
| 637 | if (is_callable($filter)) { |
| 638 | $string = $filter($string); |
| 639 | } |
| 640 | |
| 641 | return [$string]; |
| 642 | } |
| 643 | |
| 644 | return array_filter( |
| 645 | array_map( |
| 646 | static function( $value ) use ($filter) { |
| 647 | $value = trim( $value ); |
| 648 | |
| 649 | if (is_callable($filter)) { |
| 650 | return $filter($value); |
| 651 | } |
| 652 | return $value; |
| 653 | }, |
| 654 | explode( $separator, $string ) |
| 655 | ) |
| 656 | ); |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | // Initialize shortcode. |
| 661 | Give_Donor_Wall::get_instance(); |
| 662 |