PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0.2
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0.2
1.8.13.1 1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 All 212 releases
charitable / includes / admin / donations / class-charitable-donation-list-table.php

class-charitable-donation-list-table.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.7.0.2, at includes/admin/donations/class-charitable-donation-list-table.php

810 lines 22.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Sets up the donations list table in the admin.
4 *
5 * @package Charitable/Classes/Charitable_Donation_List_Table
6 * @author David Bisset
7 * @copyright Copyright (c) 2022, WP Charitable LLC
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.5.0
10 * @version 1.6.55
11 */
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 if ( ! class_exists( 'Charitable_Donation_List_Table' ) ) :
18
19 /**
20 * Charitable_Donation_List_Table class.
21 *
22 * @final
23 * @since 1.5.0
24 */
25 final class Charitable_Donation_List_Table {
26
27 /**
28 * The single instance of this class.
29 *
30 * @var Charitable_Donation_List_Table|null
31 */
32 private static $instance = null;
33
34 /**
35 * @var array
36 */
37 private $status_counts;
38
39 /**
40 * Create object instance.
41 *
42 * @since 1.5.0
43 */
44 public function __construct() {
45 do_action( 'charitable_admin_donation_post_type_start', $this );
46 }
47
48 /**
49 * Returns and/or create the single instance of this class.
50 *
51 * @since 1.5.0
52 *
53 * @return Charitable_Donation_List_Table
54 */
55 public static function get_instance() {
56 if ( is_null( self::$instance ) ) {
57 self::$instance = new self();
58 }
59
60 return self::$instance;
61 }
62
63 /**
64 * Customize donations columns.
65 *
66 * @see get_column_headers
67 *
68 * @since 1.5.0
69 *
70 * @return array
71 */
72 public function dashboard_columns() {
73 /**
74 * Filter the columns shown in the donations list table.
75 *
76 * @since 1.0.0
77 *
78 * @param array $columns The list of columns.
79 */
80 return apply_filters(
81 'charitable_donation_dashboard_column_names',
82 array(
83 'cb' => '<input type="checkbox"/>',
84 'id' => __( 'Donation', 'charitable' ),
85 'amount' => __( 'Amount Donated', 'charitable' ),
86 'campaigns' => __( 'Campaign(s)', 'charitable' ),
87 'donation_date' => __( 'Date', 'charitable' ),
88 'post_status' => __( 'Status', 'charitable' ),
89 )
90 );
91 }
92
93 /**
94 * Add information to the dashboard donations table listing.
95 *
96 * @see WP_Posts_List_Table::single_row()
97 *
98 * @since 1.5.0
99 *
100 * @param string $column_name The name of the column to display.
101 * @param int $post_id The current post ID.
102 * @return void
103 */
104 public function dashboard_column_item( $column_name, $post_id ) {
105 $donation = charitable_get_donation( $post_id );
106
107 switch ( $column_name ) {
108
109 case 'id':
110 $title = esc_attr__( 'View Donation Details', 'charitable' );
111 $name = $donation->get_donor()->get_name();
112
113 if ( $name ) {
114 $text = sprintf( _x( '#%d by %s', 'number symbol', 'charitable' ),
115 $post_id,
116 $name
117 );
118 } else {
119 $text = sprintf( _x( '#%d', 'number symbol', 'charitable' ),
120 $post_id
121 );
122 }
123
124 $url = esc_url( add_query_arg( array(
125 'post' => $post_id,
126 'action' => 'edit',
127 ), admin_url( 'post.php' ) ) );
128
129 $display = sprintf( '<a href="%s" aria-label="%s">%s</a>', $url, $title, $text );
130 break;
131
132 case 'post_status':
133 $display = sprintf( '<mark class="status %s">%s</mark>',
134 esc_attr( $donation->get_status() ),
135 strtolower( $donation->get_status_label() )
136 );
137 break;
138
139 case 'amount':
140 $display = charitable_format_money( $donation->get_total_donation_amount(), false, false, $donation->get_currency() );
141 $display .= '<span class="meta">' . sprintf( _x( 'via %s', 'charitable' ), $donation->get_gateway_label() ). '</span>';
142 break;
143
144 case 'campaigns':
145 $campaigns = array();
146
147 foreach ( $donation->get_campaign_donations() as $cd ) {
148 $campaigns[] = sprintf( '<a href="edit.php?post_type=%s&campaign_id=%s">%s</a>',
149 Charitable::DONATION_POST_TYPE,
150 $cd->campaign_id,
151 $cd->campaign_name
152 );
153 }
154
155 $display = implode( ', ', $campaigns );
156 break;
157
158 case 'donation_date':
159 $display = $donation->get_date();
160 break;
161
162 default:
163 $display = '';
164 break;
165
166 }
167
168 /**
169 * Filter the output of the cell.
170 *
171 * @since 1.0.0
172 *
173 * @param string $display The content that will be displayed.
174 * @param string $column_name The name of the column.
175 * @param int $post_id The ID of the donation being shown.
176 * @param Charitable_Donation $donation The Charitable_Donation object.
177 */
178 echo apply_filters( 'charitable_donation_column_display', $display, $column_name, $post_id, $donation );
179 }
180
181 /**
182 * Make columns sortable.
183 *
184 * @since 1.5.0
185 *
186 * @param array $columns List of columns that are sortable by default.
187 * @return array
188 */
189 public function sortable_columns( $columns ) {
190 $sortable_columns = array(
191 'id' => 'ID',
192 'amount' => 'amount',
193 'donation_date' => 'date',
194 );
195
196 return wp_parse_args( $sortable_columns, $columns );
197 }
198
199 /**
200 * Set list table primary column for donations.
201 *
202 * Support for WordPress 4.3.
203 *
204 * @since 1.5.0
205 *
206 * @param string $default
207 * @param string $screen_id
208 * @return string
209 */
210 public function primary_column( $default, $screen_id ) {
211 if ( 'edit-donation' === $screen_id ) {
212 return 'id';
213 }
214
215 return $default;
216 }
217
218 /**
219 * Set row actions for donations.
220 *
221 * @since 1.5.0
222 *
223 * @param array $actions
224 * @param WP_Post $post
225 * @return array
226 */
227 public function row_actions( $actions, $post ) {
228 if ( Charitable::DONATION_POST_TYPE !== $post->post_type ) {
229 return $actions;
230 }
231
232 if ( isset( $actions['inline hide-if-no-js'] ) ) {
233 unset( $actions['inline hide-if-no-js'] );
234 }
235
236 $actions['edit'] = sprintf( '<a href="%s" aria-label="%s">%s</a>',
237 esc_url( add_query_arg( array(
238 'post' => $post->ID,
239 'action' => 'edit',
240 'show_form' => true,
241 ), admin_url( 'post.php' ) ) ),
242 esc_attr__( 'Edit Donation', 'charitable' ),
243 __( 'Edit', 'charitable' )
244 );
245
246 $actions = array_merge( array(
247 'view' => sprintf( '<a href="%s" aria-label="%s">%s</a>',
248 esc_url( add_query_arg( array(
249 'post' => $post->ID,
250 'action' => 'edit',
251 ), admin_url( 'post.php' ) ) ),
252 esc_attr__( 'View Details', 'charitable' ),
253 __( 'View', 'charitable' )
254 )
255 ), $actions );
256
257 return $actions;
258 }
259
260 /**
261 * Customize the output of the status views.
262 *
263 * @since 1.5.0
264 *
265 * @param string[] $views The default list of status views.
266 * @return string[]
267 */
268 public function set_status_views( $views ) {
269 $counts = $this->get_status_counts();
270 $current = array_key_exists( 'post_status', $_GET ) ? $_GET['post_status'] : '';
271
272 foreach ( charitable_get_valid_donation_statuses() as $key => $label ) {
273 $views[ $key ] = sprintf( '<a href="%s"%s>%s <span class="count">(%d)</span></a>',
274 add_query_arg( array(
275 'post_status' => $key,
276 'paged' => false,
277 ) ),
278 $current === $key ? ' class="current"' : '',
279 $label,
280 array_key_exists( $key, $counts ) ? $counts[ $key ] : '0'
281 );
282 }
283
284 $views['all'] = sprintf( '<a href="%s"%s>%s <span class="count">(%d)</span></a>',
285 remove_query_arg( array( 'post_status', 'paged' ) ),
286 'all' === $current || '' === $current ? ' class="current"' : '',
287 __( 'All', 'charitable' ),
288 array_sum( $counts )
289 );
290
291 unset( $views['mine'] );
292
293 return $views;
294 }
295
296 /**
297 * Add Custom bulk actions
298 *
299 * @param array $actions
300 * @return array
301 * @since 1.5.0
302 */
303 public function custom_bulk_actions( $actions ) {
304 if ( isset( $actions['edit'] ) ) {
305 unset( $actions['edit'] );
306 }
307
308 return array_merge( $actions, $this->get_bulk_actions() );
309 }
310
311 /**
312 * Process bulk actions
313 *
314 * @param int $redirect_to
315 * @param string $action
316 * @param int[] $post_ids
317 * @return string
318 * @since 1.5.0
319 */
320 public function bulk_action_handler( $redirect_to, $action, $post_ids ) {
321 /* Bail out if this is not a status-changing action. */
322 if ( strpos( $action, 'set-' ) === false ) {
323 $sendback = remove_query_arg( array( 'trashed', 'untrashed', 'deleted', 'locked', 'ids' ), wp_get_referer() );
324 wp_redirect( esc_url_raw( $sendback ) );
325
326 exit();
327 }
328
329 $donation_statuses = charitable_get_valid_donation_statuses();
330
331 $new_status = str_replace( 'set-', '', $action ); // get the status name from action
332 $report_action = 'bulk_' . Charitable::DONATION_POST_TYPE . '_status_update';
333
334 /**
335 * Sanity check: bail out if this is actually not a status, or is
336 * not a registered status.
337 */
338 if ( ! isset( $donation_statuses[ $new_status ] ) ) {
339 return $redirect_to;
340 }
341
342 foreach ( $post_ids as $post_id ) {
343 $donation = charitable_get_donation( $post_id );
344 $donation->update_status( $new_status );
345
346 do_action( 'charitable_donations_table_do_bulk_action', $post_id, $new_status );
347 }
348
349 $redirect_to = add_query_arg( $report_action, count( $post_ids ), $redirect_to );
350
351 return $redirect_to;
352 }
353
354
355 /**
356 * Remove edit from the bulk actions.
357 *
358 * @since 1.5.0
359 *
360 * @param array $actions List of bulk actions.
361 * @return array
362 */
363 public function remove_bulk_actions( $actions ) {
364 if ( isset( $actions['edit'] ) ) {
365 unset( $actions['edit'] );
366 }
367
368 return $actions;
369 }
370
371 /**
372 * Retrieve the bulk actions.
373 *
374 * @since 1.5.0
375 *
376 * @return array $actions Array of the bulk actions
377 */
378 public function get_bulk_actions() {
379 $actions = array();
380
381 foreach ( charitable_get_valid_donation_statuses() as $status_key => $label ) {
382 $actions[ 'set-' . $status_key ] = sprintf( _x( 'Set to %s', 'set donation status to x', 'charitable' ), $label );
383 }
384
385 /**
386 * Filter the list of bulk actions for donations.
387 *
388 * @since 1.4.0
389 *
390 * @param array $actions The list of bulk actions.
391 */
392 return apply_filters( 'charitable_donations_table_bulk_actions', $actions );
393 }
394
395 /**
396 * Add extra bulk action options to mark orders as complete or processing.
397 *
398 * Using Javascript until WordPress core fixes: https://core.trac.wordpress.org/ticket/16031
399 *
400 * @since 1.5.0
401 *
402 * @global string $post_type
403 *
404 * @return void
405 */
406 public function bulk_admin_footer() {
407 global $post_type;
408
409 if ( Charitable::DONATION_POST_TYPE == $post_type ) {
410
411 $js = '<script type="text/javascript">';
412 $js .= '(function($) {';
413 foreach ( $this->get_bulk_actions() as $status_key => $label ) {
414 $js .= sprintf( "jQuery('<option>').val('%s').text('%s').appendTo( [ '#bulk-action-selector-top', '#bulk-action-selector-bottom' ] );", $status_key, $label );
415 }
416 $js .= '})(jQuery);';
417 $js .= '</script>';
418
419 echo $js;
420 }
421 }
422
423 /**
424 * Process the new bulk actions for changing order status.
425 *
426 * @since 1.5.0
427 *
428 * @return void
429 */
430 public function process_bulk_action() {
431 /* We only want to deal with donations. In case any other CPTs have an 'active' action. */
432 if ( ! isset( $_REQUEST['post_type'] ) || Charitable::DONATION_POST_TYPE !== $_REQUEST['post_type'] || ! isset( $_REQUEST['post'] ) ) {
433 return;
434 }
435
436 check_admin_referer( 'bulk-posts' );
437
438 /* Get the action. */
439 $action = '';
440
441 if ( isset( $_REQUEST['action'] ) && -1 != $_REQUEST['action'] ) {
442 $action = $_REQUEST['action'];
443 } else if ( isset( $_REQUEST['action2'] ) && -1 != $_REQUEST['action2'] ) {
444 $action = $_REQUEST['action2'];
445 }
446
447 $post_ids = array_map( 'absint', (array) $_REQUEST['post'] );
448 $redirect_to = add_query_arg( array(
449 'post_type' => Charitable::DONATION_POST_TYPE
450 ), admin_url( 'edit.php' ) );
451 $redirect_to = $this->bulk_action_handler( $redirect_to, $action, $post_ids );
452
453 wp_redirect( esc_url_raw( $redirect_to ) );
454
455 exit();
456 }
457
458 /**
459 * Show confirmation message that order status changed for number of orders.
460 *
461 * @since 1.5.0
462 *
463 * @global string $post_type
464 * @global string $pagenow
465 *
466 * @return void
467 */
468 public function bulk_admin_notices() {
469 global $post_type, $pagenow;
470
471 /* Bail out if not on shop order list page. */
472 if ( 'edit.php' !== $pagenow || Charitable::DONATION_POST_TYPE !== $post_type ) {
473 return;
474 }
475
476 /* Check if any status changes happened. */
477 $report_action = 'bulk_' . Charitable::DONATION_POST_TYPE . '_status_update';
478
479 if ( ! empty( $_REQUEST[ $report_action ] ) ) {
480 $number = absint( $_REQUEST[ $report_action ] );
481 $message = sprintf( _n( 'Donation status changed.', '%s donation statuses changed.', $number, 'charitable' ), number_format_i18n( $number ) );
482 echo '<div class="updated"><p>' . $message . '</p></div>';
483 }
484 }
485
486 /**
487 * Change messages when a post type is updated.
488 *
489 * @since 1.5.0
490 *
491 * @global WP_Post $post
492 * @global int $post_ID
493 * @param array $messages The default list of messages.
494 * @return array
495 */
496 public function post_messages( $messages ) {
497 global $post, $post_ID;
498
499 $messages[ Charitable::DONATION_POST_TYPE ] = array(
500 0 => '', // Unused. Messages start at index 1.
501 1 => sprintf( __( 'Donation updated. <a href="%s">View Donation</a>', 'charitable' ), esc_url( get_permalink( $post_ID ) ) ),
502 2 => __( 'Custom field updated.', 'charitable' ),
503 3 => __( 'Custom field deleted.', 'charitable' ),
504 4 => __( 'Donation updated.', 'charitable' ),
505 5 => isset( $_GET['revision'] ) ? sprintf( __( 'Donation restored to revision from %s', 'charitable' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
506 6 => sprintf( __( 'Donation published. <a href="%s">View Donation</a>', 'charitable' ), esc_url( get_permalink( $post_ID ) ) ),
507 7 => __( 'Donation saved.', 'charitable' ),
508 8 => sprintf(
509 __( 'Donation submitted. <a target="_blank" href="%s">Preview Donation</a>', 'charitable' ),
510 esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) )
511 ),
512 9 => sprintf(
513 __( 'Donation scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview Donation</a>', 'charitable' ),
514 date_i18n( __( 'M j, Y @ G:i', 'charitable' ), strtotime( $post->post_date ) ),
515 esc_url( get_permalink( $post_ID ) )
516 ),
517 10 => sprintf(
518 __( 'Donation draft updated. <a target="_blank" href="%s">Preview Donation</a>', 'charitable' ),
519 esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) )
520 ),
521 11 => __( 'Donation updated and email sent.', 'charitable' ),
522 12 => __( 'Email could not be sent.', 'charitable' ),
523 );
524
525 return $messages;
526 }
527
528 /**
529 * Modify bulk messages
530 *
531 * @since 1.5.0
532 *
533 * @param array $bulk_messages
534 * @param array $bulk_counts
535 * @return array
536 */
537 public function bulk_messages( $bulk_messages, $bulk_counts ) {
538 $bulk_messages[ Charitable::DONATION_POST_TYPE ] = array(
539 'updated' => _n( '%d donation updated.', '%d donations updated.', $bulk_counts['updated'], 'charitable' ),
540 'locked' => ( 1 == $bulk_counts['locked'] ) ? __( '1 donation not updated, somebody is editing it.' ) :
541 _n( '%s donation not updated, somebody is editing it.', '%s donations not updated, somebody is editing them.', $bulk_counts['locked'], 'charitable' ),
542 'deleted' => _n( '%s donation permanently deleted.', '%s donations permanently deleted.', $bulk_counts['deleted'], 'charitable' ),
543 'trashed' => _n( '%s donation moved to the Trash.', '%s donations moved to the Trash.', $bulk_counts['trashed'], 'charitable' ),
544 'untrashed' => _n( '%s donation restored from the Trash.', '%s donations restored from the Trash.', $bulk_counts['untrashed'], 'charitable' ),
545 );
546
547 return $bulk_messages;
548 }
549
550 /**
551 * Disable the month's dropdown (will replace with custom range search).
552 *
553 * @since 1.5.0
554 *
555 * @param mixed $public_query_vars Public query vars. Unused.
556 * @param string $post_type The current post type.
557 * @return boolean
558 */
559 public function disable_months_dropdown( $disable, $post_type ) {
560 if ( Charitable::DONATION_POST_TYPE == $post_type ) {
561 $disable = true;
562 }
563
564 return $disable;
565 }
566
567 /**
568 * Add date-based filters above the donations table.
569 *
570 * @since 1.5.0
571 *
572 * @global string $typenow The current post type.
573 *
574 * @return void
575 */
576 public function add_filters() {
577 global $typenow;
578
579 /* Show custom filters to filter orders by donor. */
580 if ( in_array( $typenow, array( Charitable::DONATION_POST_TYPE ) ) ) {
581 charitable_admin_view( 'donations-page/filters' );
582 }
583 }
584
585 /**
586 * Add extra buttons after filters
587 *
588 * @since 1.5.0
589 *
590 * @global string $typenow The current post type.
591 *
592 * @param string $which The context where this is being called.
593 * @return void
594 */
595 public function add_export( $which ) {
596 global $typenow;
597
598 if ( ! current_user_can( 'export_charitable_reports' ) ) {
599 return;
600 }
601
602 /* Add the export button. */
603 if ( 'top' == $which && in_array( $typenow, array( Charitable::DONATION_POST_TYPE ) ) ) {
604 charitable_admin_view( 'donations-page/export' );
605 }
606 }
607
608 /**
609 * Add modal template to footer.
610 *
611 * @since 1.5.0
612 *
613 * @global string $typenow The current post type.
614 * @global string $pagenow The current page.
615 *
616 * @return void
617 */
618 public function modal_forms() {
619 global $typenow, $pagenow;
620
621 /* Add the modal form. */
622 if ( 'edit.php' === $pagenow && in_array( $typenow, array( Charitable::DONATION_POST_TYPE ), true ) ) {
623 charitable_admin_view( 'donations-page/export-form' );
624 charitable_admin_view( 'donations-page/filter-form' );
625 }
626 }
627
628 /**
629 * Admin scripts and styles.
630 *
631 * Set up the scripts & styles used for the modal.
632 *
633 * @since 1.5.0
634 *
635 * @global string $typenow The current post type.
636 *
637 * @param string $hook The current page hook.
638 * @return void
639 */
640 public function load_scripts( $hook ) {
641 if ( 'edit.php' !== $hook ) {
642 return;
643 }
644
645 global $typenow;
646
647 /* Enqueue the scripts for donation page */
648 if ( in_array( $typenow, array( Charitable::DONATION_POST_TYPE ), true ) ) {
649 wp_enqueue_style( 'lean-modal-css' );
650 wp_enqueue_script( 'jquery' );
651 wp_enqueue_script( 'lean-modal' );
652 wp_enqueue_script( 'charitable-admin-tables' );
653 }
654 }
655
656 /**
657 * Add custom filters to the query that returns the donations to be displayed.
658 *
659 * @since 1.5.0
660 *
661 * @global string $typenow The current post type.
662 * @global string $pagenow The current admin page.
663 *
664 * @param array $vars The array of args to pass to WP_Query.
665 * @return array
666 */
667 public function filter_request_query( $vars ) {
668 global $typenow, $pagenow;
669
670 if ( 'edit.php' !== $pagenow || Charitable::DONATION_POST_TYPE !== $typenow ) {
671 return $vars;
672 }
673
674 /* No Status: fix WP's crappy handling of "all" post status. */
675 if ( ! isset( $vars['post_status'] ) || empty( $vars['post_status'] ) ) {
676 $vars['post_status'] = array_keys( charitable_get_valid_donation_statuses() );
677 }
678
679 /* Set up date query */
680 if ( isset( $_GET['start_date'] ) && ! empty( $_GET['start_date'] ) ) {
681 $start_date = $this->get_parsed_date( $_GET['start_date'] );
682 $vars['date_query']['after'] = array(
683 'year' => $start_date['year'],
684 'month' => $start_date['month'],
685 'day' => $start_date['day'],
686 );
687 }
688
689 if ( isset( $_GET['end_date'] ) && ! empty( $_GET['end_date'] ) ) {
690 $end_date = $this->get_parsed_date( $_GET['end_date'] );
691 $vars['date_query']['before'] = array(
692 'year' => $end_date['year'],
693 'month' => $end_date['month'],
694 'day' => $end_date['day'],
695 );
696 }
697
698 if ( isset( $vars['date_query'] ) ) {
699 $vars['date_query']['inclusive'] = true;
700 }
701
702 /* Filter by campaign. */
703 if ( isset( $_GET['campaign_id'] ) && 'all' != $_GET['campaign_id'] ) {
704 $vars['post__in'] = charitable_get_table( 'campaign_donations' )->get_donation_ids_for_campaign( $_GET['campaign_id'] );
705 }
706
707 /* If the user cannot view/edit others donations, filter by author. */
708 if ( ! current_user_can( 'edit_others_donations' ) ) {
709 $vars['author'] = get_current_user_id();
710 }
711
712 return $vars;
713 }
714
715 /**
716 * Column sorting handler.
717 *
718 * @since 1.5.0
719 *
720 * @global string $typenow The current post type.
721 * @global WPDB $wpdb The WPDB object.
722 * @param array $clauses Array of SQL query clauses.
723 * @return array
724 */
725 public function sort_donations( $clauses ) {
726 global $typenow, $wpdb;
727
728 if ( Charitable::DONATION_POST_TYPE != $typenow ) {
729 return $clauses;
730 }
731
732 if ( ! isset( $_GET['orderby'] ) ) {
733 return $clauses;
734 }
735
736 /* Sorting */
737 $order = isset( $_GET['order'] ) && strtoupper( $_GET['order'] ) == 'ASC' ? 'ASC' : 'DESC';
738
739 switch ( $_GET['orderby'] ) {
740 case 'amount' :
741 $clauses['join'] = "JOIN {$wpdb->prefix}charitable_campaign_donations cd ON cd.donation_id = $wpdb->posts.ID ";
742 $clauses['orderby'] = 'cd.amount ' . $order;
743 break;
744 }
745
746 return $clauses;
747 }
748
749 /**
750 * Return the status counts, taking into account any current filters.
751 *
752 * @since 1.5.0
753 *
754 * @return array
755 */
756 protected function get_status_counts() {
757 if ( ! isset( $this->status_counts ) ) {
758
759 $args = array();
760
761 if ( isset( $_GET['s'] ) && strlen( $_GET['s'] ) ) {
762 $args['s'] = $_GET['s'];
763 }
764
765 if ( isset( $_GET['start_date'] ) && strlen( $_GET['start_date'] ) ) {
766 $args['start_date'] = $this->get_parsed_date( $_GET['start_date'] );
767 }
768
769 if ( isset( $_GET['end_date'] ) && strlen( $_GET['end_date'] ) ) {
770 $args['end_date'] = $this->get_parsed_date( $_GET['end_date'] );
771 }
772
773 /* If the user cannot view/edit others donations, filter by author. */
774 if ( ! current_user_can( 'edit_others_donations' ) ) {
775 $args['author'] = get_current_user_id();
776 }
777
778 $status_counts = Charitable_Donations::count_by_status( $args );
779
780 foreach ( charitable_get_valid_donation_statuses() as $key => $label ) {
781 $this->status_counts[ $key ] = array_key_exists( $key, $status_counts )
782 ? $status_counts[ $key ]->num_donations
783 : 0;
784 }
785 }
786
787 return $this->status_counts;
788 }
789
790 /**
791 * Given a date, returns an array containing the date, month and year.
792 *
793 * @since 1.5.0
794 *
795 * @param string $date A date as a string that can be parsed by strtotime.
796 * @return string[]
797 */
798 protected function get_parsed_date( $date ) {
799 $time = charitable_sanitize_date( $date );
800
801 return array(
802 'year' => date( 'Y', $time ),
803 'month' => date( 'm', $time ),
804 'day' => date( 'd', $time ),
805 );
806 }
807 }
808
809 endif;
810