class-donor-table.php
7 years ago
donor-actions.php
7 years ago
donor-functions.php
7 years ago
donors.php
7 years ago
class-donor-table.php
526 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Donor List Table Class. |
| 4 | * |
| 5 | * The list view under WP-Admin > Donations > Donors. |
| 6 | * |
| 7 | * @package Give |
| 8 | * @subpackage Admin/Reports |
| 9 | * @copyright Copyright (c) 2016, WordImpress |
| 10 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
| 11 | * @since 1.0 |
| 12 | */ |
| 13 | |
| 14 | // Exit if accessed directly. |
| 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | exit; |
| 17 | } |
| 18 | |
| 19 | // Load WP_List_Table if not loaded. |
| 20 | if ( ! class_exists( 'WP_List_Table' ) ) { |
| 21 | require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Give_Donor_List_Table Class. |
| 26 | * |
| 27 | * @since 1.0 |
| 28 | */ |
| 29 | class Give_Donor_List_Table extends WP_List_Table { |
| 30 | |
| 31 | /** |
| 32 | * Number of items per page. |
| 33 | * |
| 34 | * @var int |
| 35 | * @since 1.0 |
| 36 | */ |
| 37 | public $per_page = 30; |
| 38 | |
| 39 | /** |
| 40 | * Number of donors found. |
| 41 | * |
| 42 | * @var int |
| 43 | * @since 1.0 |
| 44 | */ |
| 45 | public $count = 0; |
| 46 | |
| 47 | /** |
| 48 | * Total donors. |
| 49 | * |
| 50 | * @var int |
| 51 | * @since 1.0 |
| 52 | */ |
| 53 | public $total = 0; |
| 54 | |
| 55 | /** |
| 56 | * Get things started. |
| 57 | * |
| 58 | * @since 1.0 |
| 59 | * @see WP_List_Table::__construct() |
| 60 | */ |
| 61 | public function __construct() { |
| 62 | |
| 63 | // Set parent defaults. |
| 64 | parent::__construct( array( |
| 65 | 'singular' => __( 'Donor', 'give' ), // Singular name of the listed records. |
| 66 | 'plural' => __( 'Donors', 'give' ), // Plural name of the listed records. |
| 67 | 'ajax' => false, // Does this table support ajax?. |
| 68 | ) ); |
| 69 | |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Show the search field. |
| 74 | * |
| 75 | * @param string $text Label for the search box. |
| 76 | * @param string $input_id ID of the search box. |
| 77 | * |
| 78 | * @since 1.0 |
| 79 | * @access public |
| 80 | * |
| 81 | * @return void |
| 82 | */ |
| 83 | public function search_box( $text, $input_id ) { |
| 84 | $input_id = $input_id . '-search-input'; |
| 85 | |
| 86 | if ( ! empty( $_REQUEST['orderby'] ) ) { |
| 87 | echo sprintf( '<input type="hidden" name="orderby" value="%1$s" />', esc_attr( $_REQUEST['orderby'] ) ); |
| 88 | } |
| 89 | |
| 90 | if ( ! empty( $_REQUEST['order'] ) ) { |
| 91 | echo sprintf( '<input type="hidden" name="order" value="%1$s" />', esc_attr( $_REQUEST['order'] ) ); |
| 92 | } |
| 93 | ?> |
| 94 | <p class="search-box" role="search"> |
| 95 | <label class="screen-reader-text" for="<?php echo $input_id ?>"><?php echo $text; ?>:</label> |
| 96 | <input type="search" id="<?php echo $input_id ?>" name="s" value="<?php _admin_search_query(); ?>"/> |
| 97 | <?php submit_button( $text, 'button', false, false, array( |
| 98 | 'ID' => 'search-submit', |
| 99 | ) ); ?> |
| 100 | </p> |
| 101 | <?php |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * This function renders most of the columns in the list table. |
| 106 | * |
| 107 | * @param array $donor Contains all the data of the donors. |
| 108 | * @param string $column_name The name of the column. |
| 109 | * |
| 110 | * @access public |
| 111 | * @since 1.0 |
| 112 | * |
| 113 | * @return string Column Name. |
| 114 | */ |
| 115 | public function column_default( $donor, $column_name ) { |
| 116 | |
| 117 | switch ( $column_name ) { |
| 118 | |
| 119 | case 'num_donations' : |
| 120 | $value = sprintf( |
| 121 | '<a href="%s">%s</a>', |
| 122 | admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&donor=' . absint( $donor['id'] ) ), |
| 123 | esc_html( $donor['num_donations'] ) |
| 124 | ); |
| 125 | break; |
| 126 | |
| 127 | case 'amount_spent' : |
| 128 | $value = give_currency_filter( give_format_amount( $donor[ $column_name ], array( 'sanitize' => false ) ) ); |
| 129 | break; |
| 130 | |
| 131 | case 'date_created' : |
| 132 | $value = date_i18n( give_date_format(), strtotime( $donor['date_created'] ) ); |
| 133 | break; |
| 134 | |
| 135 | default: |
| 136 | $value = isset( $donor[ $column_name ] ) ? $donor[ $column_name ] : null; |
| 137 | break; |
| 138 | } |
| 139 | |
| 140 | return apply_filters( "give_donors_column_{$column_name}", $value, $donor['id'] ); |
| 141 | |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * For CheckBox Column |
| 146 | * |
| 147 | * @param array $donor Donor Data. |
| 148 | * |
| 149 | * @access public |
| 150 | * @since 1.8.16 |
| 151 | * |
| 152 | * @return string |
| 153 | */ |
| 154 | public function column_cb( $donor ) { |
| 155 | return sprintf( |
| 156 | '<input class="donor-selector" type="checkbox" name="%1$s[]" value="%2$d" data-name="%3$s" />', |
| 157 | $this->_args['singular'], |
| 158 | $donor['id'], |
| 159 | $donor['name'] |
| 160 | ); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Column name. |
| 165 | * |
| 166 | * @param array $donor Donor Data. |
| 167 | * |
| 168 | * @access public |
| 169 | * @since 1.0 |
| 170 | * |
| 171 | * @return string |
| 172 | */ |
| 173 | public function column_name( $donor ) { |
| 174 | $name = ! empty( $donor['name'] ) ? $donor['name'] : '<em>' . __( 'Unnamed Donor', 'give' ) . '</em>'; |
| 175 | $view_url = admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor['id'] ); |
| 176 | $actions = $this->get_row_actions( $donor ); |
| 177 | |
| 178 | return '<a href="' . esc_url( $view_url ) . '">' . $name . '</a>' . $this->row_actions( $actions ); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Retrieve the table columns. |
| 183 | * |
| 184 | * @access public |
| 185 | * @since 1.0 |
| 186 | * |
| 187 | * @return array $columns Array of all the list table columns. |
| 188 | */ |
| 189 | public function get_columns() { |
| 190 | $columns = array( |
| 191 | 'cb' => '<input type="checkbox" />', // Render a checkbox instead of text. |
| 192 | 'name' => __( 'Name', 'give' ), |
| 193 | 'email' => __( 'Email', 'give' ), |
| 194 | 'num_donations' => __( 'Donations', 'give' ), |
| 195 | 'amount_spent' => __( 'Total Donated', 'give' ), |
| 196 | 'date_created' => __( 'Date Created', 'give' ), |
| 197 | ); |
| 198 | |
| 199 | return apply_filters( 'give_list_donors_columns', $columns ); |
| 200 | |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Get the sortable columns. |
| 205 | * |
| 206 | * @access public |
| 207 | * @since 2.1 |
| 208 | * @return array Array of all the sortable columns. |
| 209 | */ |
| 210 | public function get_sortable_columns() { |
| 211 | |
| 212 | $columns = array( |
| 213 | 'date_created' => array( 'date_created', true ), |
| 214 | 'name' => array( 'name', true ), |
| 215 | 'num_donations' => array( 'purchase_count', false ), |
| 216 | 'amount_spent' => array( 'purchase_value', false ), |
| 217 | ); |
| 218 | |
| 219 | return apply_filters( 'give_list_donors_sortable_columns', $columns ); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Retrieve row actions. |
| 224 | * |
| 225 | * @param array $donor Donor Data. |
| 226 | * |
| 227 | * @since 1.7 |
| 228 | * @access public |
| 229 | * |
| 230 | * @return array An array of action links. |
| 231 | */ |
| 232 | public function get_row_actions( $donor ) { |
| 233 | |
| 234 | $actions = array( |
| 235 | 'view' => sprintf( '<a href="%1$s" aria-label="%2$s">%3$s</a>', admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=overview&id=' . $donor['id'] ), sprintf( esc_attr__( 'View "%s"', 'give' ), $donor['name'] ), __( 'View Donor', 'give' ) ), |
| 236 | 'notes' => sprintf( '<a href="%1$s" aria-label="%2$s">%3$s</a>', admin_url( 'edit.php?post_type=give_forms&page=give-donors&view=notes&id=' . $donor['id'] ), sprintf( esc_attr__( 'Notes for "%s"', 'give' ), $donor['name'] ), __( 'Notes', 'give' ) ), |
| 237 | 'delete' => sprintf( '<a class="%1$s" data-id="%2$s" href="#" aria-label="%3$s">%4$s</a>', 'give-single-donor-delete', $donor['id'],sprintf( esc_attr__( 'Delete "%s"', 'give' ), $donor['name'] ), __( 'Delete', 'give' ) ), |
| 238 | ); |
| 239 | |
| 240 | return apply_filters( 'give_donor_row_actions', $actions, $donor ); |
| 241 | |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Retrieve the current page number. |
| 246 | * |
| 247 | * @access public |
| 248 | * @since 1.0 |
| 249 | * |
| 250 | * @return int Current page number. |
| 251 | */ |
| 252 | public function get_paged() { |
| 253 | return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Retrieves the search query string. |
| 258 | * |
| 259 | * @access public |
| 260 | * @since 1.0 |
| 261 | * |
| 262 | * @return mixed string If search is present, false otherwise. |
| 263 | */ |
| 264 | public function get_search() { |
| 265 | return ! empty( $_GET['s'] ) ? urldecode( trim( $_GET['s'] ) ) : false; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Get the Bulk Actions. |
| 270 | * |
| 271 | * @access public |
| 272 | * @since 1.8.16 |
| 273 | * |
| 274 | * @return array |
| 275 | */ |
| 276 | public function get_bulk_actions() { |
| 277 | $actions = array( |
| 278 | 'delete' => __( 'Delete', 'give' ), |
| 279 | ); |
| 280 | return $actions; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Generate the table navigation above or below the table |
| 285 | * |
| 286 | * @param string $which Position to trigger i.e. Top/Bottom. |
| 287 | * |
| 288 | * @access protected |
| 289 | * @since 1.8.16 |
| 290 | */ |
| 291 | protected function display_tablenav( $which ) { |
| 292 | if ( 'top' === $which ) { |
| 293 | wp_nonce_field( 'bulk-' . $this->_args['plural'], '_wpnonce', false ); |
| 294 | } |
| 295 | ?> |
| 296 | <div class="tablenav <?php echo esc_attr( $which ); ?>"> |
| 297 | <?php if ( $this->has_items() ) : ?> |
| 298 | <div class="alignleft actions bulkactions"> |
| 299 | <?php $this->bulk_actions( $which ); ?> |
| 300 | </div> |
| 301 | <?php endif; |
| 302 | $this->extra_tablenav( $which ); |
| 303 | $this->pagination( $which ); |
| 304 | ?> |
| 305 | <br class="clear" /> |
| 306 | </div> |
| 307 | <?php |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Retrieves the donor data from db. |
| 312 | * |
| 313 | * @access public |
| 314 | * @since 1.0 |
| 315 | * |
| 316 | * @return array $data The Donor data. |
| 317 | */ |
| 318 | public function donor_data() { |
| 319 | |
| 320 | $data = array(); |
| 321 | |
| 322 | // Get donor query. |
| 323 | $args = $this->get_donor_query(); |
| 324 | $donors = Give()->donors->get_donors( $args ); |
| 325 | |
| 326 | if ( $donors ) { |
| 327 | |
| 328 | foreach ( $donors as $donor ) { |
| 329 | |
| 330 | $user_id = ! empty( $donor->user_id ) ? intval( $donor->user_id ) : 0; |
| 331 | $title_prefix = Give()->donor_meta->get_meta( $donor->id, '_give_donor_title_prefix', true ); |
| 332 | |
| 333 | // If title prefix is set, then update the donor name. |
| 334 | $donor->name = give_get_donor_name_with_title_prefixes( $title_prefix, $donor->name ); |
| 335 | |
| 336 | $data[] = array( |
| 337 | 'id' => $donor->id, |
| 338 | 'user_id' => $user_id, |
| 339 | 'name' => $donor->name, |
| 340 | 'email' => $donor->email, |
| 341 | 'num_donations' => $donor->purchase_count, |
| 342 | 'amount_spent' => $donor->purchase_value, |
| 343 | 'date_created' => $donor->date_created, |
| 344 | ); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | return apply_filters( 'give_donors_column_query_data', $data ); |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Get donor count. |
| 353 | * |
| 354 | * @since 1.8.1 |
| 355 | * @access private |
| 356 | */ |
| 357 | private function get_donor_count() { |
| 358 | // Get donor query. |
| 359 | $_donor_query = $this->get_donor_query(); |
| 360 | |
| 361 | $_donor_query['number'] = - 1; |
| 362 | $_donor_query['offset'] = 0; |
| 363 | $donors = Give()->donors->get_donors( $_donor_query ); |
| 364 | |
| 365 | return count( $donors ); |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * Get donor query. |
| 370 | * |
| 371 | * @since 1.8.1 |
| 372 | * @access public |
| 373 | * |
| 374 | * @return array |
| 375 | */ |
| 376 | public function get_donor_query() { |
| 377 | $paged = $this->get_paged(); |
| 378 | $offset = $this->per_page * ( $paged - 1 ); |
| 379 | $search = $this->get_search(); |
| 380 | $order = isset( $_GET['order'] ) ? sanitize_text_field( $_GET['order'] ) : 'DESC'; |
| 381 | $orderby = isset( $_GET['orderby'] ) ? sanitize_text_field( $_GET['orderby'] ) : 'id'; |
| 382 | |
| 383 | $args = array( |
| 384 | 'number' => $this->per_page, |
| 385 | 'offset' => $offset, |
| 386 | 'order' => $order, |
| 387 | 'orderby' => $orderby, |
| 388 | ); |
| 389 | |
| 390 | if ( $search ) { |
| 391 | if ( is_email( $search ) ) { |
| 392 | $args['email'] = $search; |
| 393 | } elseif ( is_numeric( $search ) ) { |
| 394 | $args['id'] = $search; |
| 395 | } else { |
| 396 | $args['name'] = $search; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | return $args; |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * Generates content for a single row of the table |
| 405 | * |
| 406 | * @param object $item The current item. |
| 407 | * |
| 408 | * @since 1.8.17 |
| 409 | * @access public |
| 410 | */ |
| 411 | public function single_row( $item ) { |
| 412 | echo sprintf( '<tr id="donor-%1$d" data-id="%2$d" data-name="%3$s">', $item['id'], $item['id'], $item['name'] ); |
| 413 | $this->single_row_columns( $item ); |
| 414 | echo '</tr>'; |
| 415 | } |
| 416 | |
| 417 | /** |
| 418 | * Display the final donor table |
| 419 | * |
| 420 | * @since 1.8.17 |
| 421 | * @access public |
| 422 | */ |
| 423 | public function display() { |
| 424 | $singular = $this->_args['singular']; |
| 425 | |
| 426 | $this->display_tablenav( 'top' ); |
| 427 | |
| 428 | $this->screen->render_screen_reader_content( 'heading_list' ); |
| 429 | |
| 430 | $get_data = give_clean( $_GET ); // WPCS: input var ok, sanitization ok, CSRF ok. |
| 431 | |
| 432 | $search_keyword = ! empty( $get_data['s'] ) ? $get_data['s'] : ''; |
| 433 | $order = ! empty( $get_data['order'] ) ? $get_data['order'] : 'DESC'; |
| 434 | $order_by = ! empty( $get_data['orderby'] ) ? $get_data['orderby'] : 'ID'; |
| 435 | ?> |
| 436 | <table class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>"> |
| 437 | <thead> |
| 438 | <tr> |
| 439 | <?php $this->print_column_headers(); ?> |
| 440 | </tr> |
| 441 | </thead> |
| 442 | |
| 443 | <tbody id="the-list"<?php |
| 444 | if ( $singular ) { |
| 445 | echo " data-wp-lists='list:$singular'"; |
| 446 | } ?>> |
| 447 | <tr class="hidden"></tr> |
| 448 | <tr id="give-bulk-delete" class="inline-edit-row inline-edit-row-page inline-edit-page bulk-edit-row bulk-edit-row-page bulk-edit-page inline-editor" style="display: none;" > |
| 449 | <td colspan="6" class="colspanchange"> |
| 450 | |
| 451 | <fieldset class="inline-edit-col-left"> |
| 452 | <legend class="inline-edit-legend"><?php esc_attr_e( 'BULK DELETE', 'give' ); ?></legend> |
| 453 | <div class="inline-edit-col"> |
| 454 | <div id="bulk-titles"> |
| 455 | <div id="give-bulk-donors" class="give-bulk-donors"> |
| 456 | |
| 457 | </div> |
| 458 | </div> |
| 459 | </fieldset> |
| 460 | |
| 461 | <fieldset class="inline-edit-col-right"> |
| 462 | <div class="inline-edit-col"> |
| 463 | <label> |
| 464 | <input class="give-donor-delete-confirm" type="checkbox" name="give-donor-delete-confirm"/> |
| 465 | <?php esc_attr_e( 'Are you sure you want to delete the selected donor(s)?', 'give' ); ?> |
| 466 | </label> |
| 467 | <label> |
| 468 | <input class="give-donor-delete-records" type="checkbox" name="give-donor-delete-records"/> |
| 469 | <?php esc_attr_e( 'Delete all associated donations and records?', 'give' ); ?> |
| 470 | </label> |
| 471 | </div> |
| 472 | </fieldset> |
| 473 | |
| 474 | <p class="submit inline-edit-save"> |
| 475 | <input type="hidden" name="give_action" value="delete_bulk_donor"/> |
| 476 | <input type="hidden" name="s" value="<?php echo esc_html( $search_keyword ); ?>"/> |
| 477 | <input type="hidden" name="orderby" value="<?php echo esc_html( $order_by ); ?>"/> |
| 478 | <input type="hidden" name="order" value="<?php echo esc_html( $order ); ?>"/> |
| 479 | <button type="button" id="give-bulk-delete-cancel" class="button cancel alignleft"><?php esc_attr_e( 'Cancel', 'give' ); ?></button> |
| 480 | <input type="submit" id="give-bulk-delete-button" disabled class="button button-primary alignright" value="<?php esc_attr_e( 'Delete', 'give' ); ?>"> |
| 481 | <br class="clear"> |
| 482 | </p> |
| 483 | </td> |
| 484 | </tr> |
| 485 | <?php $this->display_rows_or_placeholder(); ?> |
| 486 | </tbody> |
| 487 | |
| 488 | <tfoot> |
| 489 | <tr> |
| 490 | <?php $this->print_column_headers( false ); ?> |
| 491 | </tr> |
| 492 | </tfoot> |
| 493 | |
| 494 | </table> |
| 495 | <?php |
| 496 | $this->display_tablenav( 'bottom' ); |
| 497 | } |
| 498 | |
| 499 | /** |
| 500 | * Setup the final data for the table. |
| 501 | * |
| 502 | * @access public |
| 503 | * @since 1.0 |
| 504 | * |
| 505 | * @return void |
| 506 | */ |
| 507 | public function prepare_items() { |
| 508 | |
| 509 | $columns = $this->get_columns(); |
| 510 | $hidden = array(); // No hidden columns. |
| 511 | $sortable = $this->get_sortable_columns(); |
| 512 | |
| 513 | $this->_column_headers = array( $columns, $hidden, $sortable ); |
| 514 | |
| 515 | $this->items = $this->donor_data(); |
| 516 | |
| 517 | $this->total = $this->get_donor_count(); |
| 518 | |
| 519 | $this->set_pagination_args( array( |
| 520 | 'total_items' => $this->total, |
| 521 | 'per_page' => $this->per_page, |
| 522 | 'total_pages' => ceil( $this->total / $this->per_page ), |
| 523 | ) ); |
| 524 | } |
| 525 | } |
| 526 |