Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Give_Payment_History_Table often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Give_Payment_History_Table, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class Give_Payment_History_Table extends WP_List_Table { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Number of results to show per page |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | * @since 1.0 |
||
| 36 | */ |
||
| 37 | public $per_page = 30; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * URL of this page |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | * @since 1.0.1 |
||
| 44 | */ |
||
| 45 | public $base_url; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Total number of payments |
||
| 49 | * |
||
| 50 | * @var int |
||
| 51 | * @since 1.0 |
||
| 52 | */ |
||
| 53 | public $total_count; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Total number of complete payments |
||
| 57 | * |
||
| 58 | * @var int |
||
| 59 | * @since 1.0 |
||
| 60 | */ |
||
| 61 | public $complete_count; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Total number of pending payments |
||
| 65 | * |
||
| 66 | * @var int |
||
| 67 | * @since 1.0 |
||
| 68 | */ |
||
| 69 | public $pending_count; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Total number of processing payments |
||
| 73 | * |
||
| 74 | * @var int |
||
| 75 | * @since 1.8.9 |
||
| 76 | */ |
||
| 77 | public $processing_count; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Total number of refunded payments |
||
| 81 | * |
||
| 82 | * @var int |
||
| 83 | * @since 1.0 |
||
| 84 | */ |
||
| 85 | public $refunded_count; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Total number of failed payments |
||
| 89 | * |
||
| 90 | * @var int |
||
| 91 | * @since 1.0 |
||
| 92 | */ |
||
| 93 | public $failed_count; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Total number of revoked payments |
||
| 97 | * |
||
| 98 | * @var int |
||
| 99 | * @since 1.0 |
||
| 100 | */ |
||
| 101 | public $revoked_count; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Total number of cancelled payments |
||
| 105 | * |
||
| 106 | * @var int |
||
| 107 | * @since 1.4 |
||
| 108 | */ |
||
| 109 | public $cancelled_count; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Total number of abandoned payments |
||
| 113 | * |
||
| 114 | * @var int |
||
| 115 | * @since 1.6 |
||
| 116 | */ |
||
| 117 | public $abandoned_count; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Total number of pre-approved payments |
||
| 121 | * |
||
| 122 | * @var int |
||
| 123 | * @since 1.8.13 |
||
| 124 | */ |
||
| 125 | public $preapproval_count; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Get things started. |
||
| 129 | * |
||
| 130 | * @since 1.0 |
||
| 131 | * @uses Give_Payment_History_Table::get_payment_counts() |
||
| 132 | * @see WP_List_Table::__construct() |
||
| 133 | */ |
||
| 134 | public function __construct() { |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Add donation search filter. |
||
| 152 | * |
||
| 153 | * @return void |
||
| 154 | */ |
||
| 155 | public function advanced_filters() { |
||
| 156 | $start_date = isset( $_GET['start-date'] ) ? give_clean( $_GET['start-date'] ) : null; |
||
| 157 | $end_date = isset( $_GET['end-date'] ) ? give_clean( $_GET['end-date'] ) : null; |
||
| 158 | $status = isset( $_GET['status'] ) ? give_clean( $_GET['status'] ) : ''; |
||
| 159 | $donor = isset( $_GET['donor'] ) ? absint( $_GET['donor'] ) : ''; |
||
| 160 | $search = isset( $_GET['s'] ) ? give_clean( $_GET['s'] ) : ''; |
||
| 161 | $form_id = ! empty( $_GET['form_id'] ) ? absint( $_GET['form_id'] ) : 0; |
||
| 162 | $date_format = give_date_format(); |
||
| 163 | ?> |
||
| 164 | <div id="give-payment-filters" class="give-filters"> |
||
| 165 | <?php $this->search_box( __( 'Search', 'give' ), 'give-payments' ); ?> |
||
| 166 | <div id="give-payment-date-filters"> |
||
| 167 | <div class="give-filter give-filter-half"> |
||
| 168 | <label for="start-date" |
||
| 169 | class="give-start-date-label"><?php _e( 'Start Date', 'give' ); ?></label> |
||
| 170 | <input type="text" id="start-date" name="start-date" class="give_datepicker" autocomplete="off" |
||
| 171 | value="<?php printf( esc_attr( $start_date ) ); ?>" placeholder="<?php _e( 'Start Date', 'give' ); ?>" /> |
||
| 172 | </div> |
||
| 173 | <div class="give-filter give-filter-half"> |
||
| 174 | <label for="end-date" class="give-end-date-label"><?php _e( 'End Date', 'give' ); ?></label> |
||
| 175 | <input type="text" id="end-date" name="end-date" class="give_datepicker" autocomplete="off" |
||
| 176 | value="<?php printf( esc_attr( $end_date ) ); ?>" placeholder="<?php _e( 'End Date', 'give' ); ?>" /> |
||
| 177 | </div> |
||
| 178 | </div> |
||
| 179 | <div id="give-payment-form-filter" class="give-filter"> |
||
| 180 | <label for="give-donation-forms-filter" |
||
| 181 | class="give-donation-forms-filter-label"><?php _e( 'Form', 'give' ); ?></label> |
||
| 182 | <?php |
||
| 183 | // Filter Donations by Donation Forms. |
||
| 184 | echo Give()->html->forms_dropdown( |
||
| 185 | array( |
||
| 186 | 'name' => 'form_id', |
||
| 187 | 'id' => 'give-donation-forms-filter', |
||
| 188 | 'class' => 'give-donation-forms-filter', |
||
| 189 | 'selected' => $form_id, // Make sure to have $form_id set to 0, if there is no selection. |
||
| 190 | 'chosen' => true, |
||
| 191 | 'number' => 30, |
||
| 192 | ) |
||
| 193 | ); |
||
| 194 | ?> |
||
| 195 | </div> |
||
| 196 | |||
| 197 | <?php |
||
| 198 | /** |
||
| 199 | * Action to add hidden fields and HTML in Payment search. |
||
| 200 | * |
||
| 201 | * @since 1.8.18 |
||
| 202 | */ |
||
| 203 | do_action( 'give_payment_table_advanced_filters' ); |
||
| 204 | |||
| 205 | |||
| 206 | if ( ! empty( $status ) ) { |
||
| 207 | echo sprintf( '<input type="hidden" name="status" value="%s"/>', esc_attr( $status ) ); |
||
| 208 | } |
||
| 209 | |||
| 210 | if ( ! empty( $donor ) ) { |
||
| 211 | echo sprintf( '<input type="hidden" name="donor" value="%s"/>', absint( $donor ) ); |
||
| 212 | } |
||
| 213 | ?> |
||
| 214 | |||
| 215 | <div class="give-filter"> |
||
| 216 | <?php submit_button( __( 'Apply', 'give' ), 'secondary', '', false ); ?> |
||
| 217 | <?php |
||
| 218 | // Clear active filters button. |
||
| 219 | View Code Duplication | if ( ! empty( $start_date ) || ! empty( $end_date ) || ! empty( $donor ) || ! empty( $search ) || ! empty( $status ) || ! empty( $form_id ) ) : |
|
| 220 | ?> |
||
| 221 | <a href="<?php echo admin_url( 'edit.php?post_type=give_forms&page=give-payment-history' ); ?>" |
||
| 222 | class="button give-clear-filters-button"><?php _e( 'Clear Filters', 'give' ); ?></a> |
||
| 223 | <?php endif; ?> |
||
| 224 | </div> |
||
| 225 | </div> |
||
| 226 | |||
| 227 | <?php |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Show the search field |
||
| 232 | * |
||
| 233 | * @param string $text Label for the search box. |
||
| 234 | * @param string $input_id ID of the search box. |
||
| 235 | * |
||
| 236 | * @since 1.0 |
||
| 237 | * @access public |
||
| 238 | * |
||
| 239 | * @return void |
||
| 240 | */ |
||
| 241 | public function search_box( $text, $input_id ) { |
||
| 242 | $input_id = $input_id . '-search-input'; |
||
| 243 | |||
| 244 | if ( ! empty( $_REQUEST['orderby'] ) ) { |
||
| 245 | echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />'; |
||
| 246 | } |
||
| 247 | if ( ! empty( $_REQUEST['order'] ) ) { |
||
| 248 | echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />'; |
||
| 249 | } |
||
| 250 | ?> |
||
| 251 | <div class="give-filter give-filter-search" role="search"> |
||
| 252 | <?php |
||
| 253 | /** |
||
| 254 | * Fires in the payment history search box. |
||
| 255 | * |
||
| 256 | * Allows you to add new elements before the search box. |
||
| 257 | * |
||
| 258 | * @since 1.7 |
||
| 259 | */ |
||
| 260 | do_action( 'give_payment_history_search' ); |
||
| 261 | ?> |
||
| 262 | <label class="screen-reader-text" for="<?php echo $input_id ?>"><?php echo $text; ?>:</label> |
||
| 263 | <input type="search" id="<?php echo $input_id ?>" name="s" |
||
| 264 | value="<?php _admin_search_query(); ?>" |
||
| 265 | placeholder="<?php _e( 'Name, Email, or Donation ID', 'give' ); ?>" /> |
||
| 266 | <?php submit_button( $text, 'button', false, false, array( |
||
| 267 | 'ID' => 'search-submit', |
||
| 268 | ) ); ?><br /> |
||
| 269 | </div> |
||
| 270 | <?php |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Retrieve the view types |
||
| 275 | * |
||
| 276 | * @access public |
||
| 277 | * @since 1.0 |
||
| 278 | * |
||
| 279 | * @return array $views All the views available |
||
| 280 | */ |
||
| 281 | public function get_views() { |
||
| 282 | |||
| 283 | $current = isset( $_GET['status'] ) ? $_GET['status'] : ''; |
||
| 284 | $views = array(); |
||
| 285 | $tabs = array( |
||
| 286 | 'all' => array( |
||
| 287 | 'total_count', |
||
| 288 | __( 'All', 'give' ), |
||
| 289 | ), |
||
| 290 | 'publish' => array( |
||
| 291 | 'complete_count', |
||
| 292 | __( 'Completed', 'give' ), |
||
| 293 | ), |
||
| 294 | 'pending' => array( |
||
| 295 | 'pending_count', |
||
| 296 | __( 'Pending', 'give' ), |
||
| 297 | ), |
||
| 298 | 'processing' => array( |
||
| 299 | 'processing_count', |
||
| 300 | __( 'Processing', 'give' ), |
||
| 301 | ), |
||
| 302 | 'refunded' => array( |
||
| 303 | 'refunded_count', |
||
| 304 | __( 'Refunded', 'give' ), |
||
| 305 | ), |
||
| 306 | 'revoked' => array( |
||
| 307 | 'revoked_count', |
||
| 308 | __( 'Revoked', 'give' ), |
||
| 309 | ), |
||
| 310 | 'failed' => array( |
||
| 311 | 'failed_count', |
||
| 312 | __( 'Failed', 'give' ), |
||
| 313 | ), |
||
| 314 | 'cancelled' => array( |
||
| 315 | 'cancelled_count', |
||
| 316 | __( 'Cancelled', 'give' ), |
||
| 317 | ), |
||
| 318 | 'abandoned' => array( |
||
| 319 | 'abandoned_count', |
||
| 320 | __( 'Abandoned', 'give' ), |
||
| 321 | ), |
||
| 322 | 'preapproval' => array( |
||
| 323 | 'preapproval_count', |
||
| 324 | __( 'Preapproval Pending', 'give' ), |
||
| 325 | ), |
||
| 326 | ); |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Remove Query from Args of the URL that are being pass to Donation Status. |
||
| 330 | * |
||
| 331 | * @since 1.8.18 |
||
| 332 | */ |
||
| 333 | $args = (array) apply_filters( 'give_payments_table_status_remove_query_arg', array( 'paged', '_wpnonce', '_wp_http_referer' ) ); |
||
| 334 | |||
| 335 | // Build URL. |
||
| 336 | $staus_url = remove_query_arg( $args ); |
||
| 337 | |||
| 338 | foreach ( $tabs as $key => $tab ) { |
||
| 339 | $count_key = $tab[0]; |
||
| 340 | $name = $tab[1]; |
||
| 341 | $count = $this->$count_key; |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Filter can be used to show all the status inside the donation tabs. |
||
| 345 | * |
||
| 346 | * Filter can be used to show all the status inside the donation submenu tabs return true to show all the tab. |
||
| 347 | * |
||
| 348 | * @param string $key Current view tab value. |
||
| 349 | * @param int $count Number of donation inside the tab. |
||
| 350 | * |
||
| 351 | * @since 1.8.12 |
||
| 352 | */ |
||
| 353 | if ( 'all' === $key || $key === $current || apply_filters( 'give_payments_table_show_all_status', 0 < $count, $key, $count ) ) { |
||
| 354 | |||
| 355 | $staus_url = 'all' === $key ? |
||
| 356 | add_query_arg( array( 'status' => false ), $staus_url ) : |
||
| 357 | add_query_arg( array( 'status' => $key ), $staus_url ); |
||
| 358 | |||
| 359 | $views[ $key ] = sprintf( |
||
| 360 | '<a href="%s"%s>%s <span class="count">(%s)</span></a>', |
||
| 361 | esc_url( $staus_url ), |
||
| 362 | ( ( 'all' === $key && empty( $current ) ) ) ? ' class="current"' : ( $current == $key ? 'class="current"' : '' ), |
||
| 363 | $name, |
||
| 364 | $count |
||
| 365 | ); |
||
| 366 | } |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Filter the donation listing page views. |
||
| 371 | * |
||
| 372 | * @since 1.0 |
||
| 373 | * |
||
| 374 | * @param array $views |
||
| 375 | * @param Give_Payment_History_Table |
||
| 376 | */ |
||
| 377 | return apply_filters( 'give_payments_table_views', $views, $this ); |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Retrieve the table columns |
||
| 382 | * |
||
| 383 | * @access public |
||
| 384 | * @since 1.0 |
||
| 385 | * |
||
| 386 | * @return array $columns Array of all the list table columns |
||
| 387 | */ |
||
| 388 | public function get_columns() { |
||
| 389 | $columns = array( |
||
| 390 | 'cb' => '<input type="checkbox" />', // Render a checkbox instead of text. |
||
| 391 | 'donation' => __( 'Donation', 'give' ), |
||
| 392 | 'donation_form' => __( 'Donation Form', 'give' ), |
||
| 393 | 'status' => __( 'Status', 'give' ), |
||
| 394 | 'date' => __( 'Date', 'give' ), |
||
| 395 | 'amount' => __( 'Amount', 'give' ), |
||
| 396 | ); |
||
| 397 | |||
| 398 | if ( current_user_can( 'view_give_payments' ) ) { |
||
| 399 | $columns['details'] = __( 'Details', 'give' ); |
||
| 400 | } |
||
| 401 | |||
| 402 | return apply_filters( 'give_payments_table_columns', $columns ); |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Retrieve the table's sortable columns |
||
| 407 | * |
||
| 408 | * @access public |
||
| 409 | * @since 1.0 |
||
| 410 | * |
||
| 411 | * @return array Array of all the sortable columns |
||
| 412 | */ |
||
| 413 | public function get_sortable_columns() { |
||
| 414 | $columns = array( |
||
| 415 | 'donation' => array( 'ID', true ), |
||
| 416 | 'donation_form' => array( 'donation_form', false ), |
||
| 417 | 'status' => array( 'status', false ), |
||
| 418 | 'amount' => array( 'amount', false ), |
||
| 419 | 'date' => array( 'date', false ), |
||
| 420 | ); |
||
| 421 | |||
| 422 | return apply_filters( 'give_payments_table_sortable_columns', $columns ); |
||
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Gets the name of the primary column. |
||
| 427 | * |
||
| 428 | * @since 1.5 |
||
| 429 | * @access protected |
||
| 430 | * |
||
| 431 | * @return string Name of the primary column. |
||
| 432 | */ |
||
| 433 | protected function get_primary_column_name() { |
||
| 434 | return 'donation'; |
||
| 435 | } |
||
| 436 | |||
| 437 | /** |
||
| 438 | * This function renders most of the columns in the list table. |
||
| 439 | * |
||
| 440 | * @param Give_Payment $payment Payment ID. |
||
| 441 | * @param string $column_name The name of the column. |
||
| 442 | * |
||
| 443 | * @access public |
||
| 444 | * @since 1.0 |
||
| 445 | * |
||
| 446 | * @return string Column Name |
||
| 447 | */ |
||
| 448 | public function column_default( $payment, $column_name ) { |
||
| 449 | |||
| 450 | $single_donation_url = esc_url( add_query_arg( 'id', $payment->ID, admin_url( 'edit.php?post_type=give_forms&page=give-payment-history&view=view-payment-details' ) ) ); |
||
| 451 | $row_actions = $this->get_row_actions( $payment ); |
||
| 452 | $value = ''; |
||
| 453 | |||
| 454 | switch ( $column_name ) { |
||
| 455 | case 'donation' : |
||
| 456 | $serial_code = Give()->seq_donation_number->get_serial_code( $payment ); |
||
| 457 | if ( current_user_can( 'view_give_payments' ) ) { |
||
| 458 | $value = Give()->tooltips->render_link( array( |
||
| 459 | 'label' => sprintf( __( 'View Donation %s', 'give' ), $serial_code ), |
||
| 460 | 'tag_content' => $serial_code, |
||
| 461 | 'link' => $single_donation_url, |
||
| 462 | ) ); |
||
| 463 | } else { |
||
| 464 | $value = $serial_code; |
||
| 465 | } |
||
| 466 | |||
| 467 | $value .= sprintf( |
||
| 468 | ' %1$s %2$s<br>', |
||
| 469 | __( 'by', 'give' ), |
||
| 470 | $this->get_donor( $payment ) |
||
| 471 | ); |
||
| 472 | |||
| 473 | $value .= $this->get_donor_email( $payment ); |
||
| 474 | $value .= $this->row_actions( $row_actions ); |
||
| 475 | break; |
||
| 476 | |||
| 477 | case 'amount': |
||
| 478 | $value = give_donation_amount( $payment, true ); |
||
| 479 | $value .= sprintf( '<br><small>%1$s %2$s</small>', __( 'via', 'give' ), give_get_gateway_admin_label( $payment->gateway ) ); |
||
| 480 | break; |
||
| 481 | |||
| 482 | case 'donation_form': |
||
| 483 | $form_title = empty( $payment->form_title ) ? sprintf( __( 'Untitled (#%s)', 'give' ), $payment->form_id ) : $payment->form_title; |
||
| 484 | $value = '<a href="' . admin_url( 'post.php?post=' . $payment->form_id . '&action=edit' ) . '">' . $form_title . '</a>'; |
||
| 485 | $level = give_get_donation_form_title( |
||
| 486 | $payment, |
||
| 487 | array( |
||
| 488 | 'only_level' => true, |
||
| 489 | ) |
||
| 490 | ); |
||
| 491 | |||
| 492 | if ( ! empty( $level ) ) { |
||
| 493 | $value .= $level; |
||
| 494 | } |
||
| 495 | |||
| 496 | break; |
||
| 497 | |||
| 498 | case 'date': |
||
| 499 | $date = strtotime( $payment->date ); |
||
| 500 | $value = date_i18n( give_date_format(), $date ); |
||
| 501 | break; |
||
| 502 | |||
| 503 | case 'status': |
||
| 504 | $value = $this->get_payment_status( $payment ); |
||
| 505 | break; |
||
| 506 | |||
| 507 | |||
| 508 | case 'details' : |
||
| 509 | if ( current_user_can( 'view_give_payments' ) ) { |
||
| 510 | $value = Give()->tooltips->render_link( array( |
||
| 511 | 'label' => sprintf( __( 'View Donation #%s', 'give' ), $payment->ID ), |
||
| 512 | 'tag_content' => '<span class="dashicons dashicons-visibility"></span>', |
||
| 513 | 'link' => $single_donation_url, |
||
| 514 | 'attributes' => array( |
||
| 515 | 'class' => 'give-payment-details-link button button-small', |
||
| 516 | ), |
||
| 517 | ) ); |
||
| 518 | |||
| 519 | $value = "<div class=\"give-payment-details-link-wrap\">{$value}</div>"; |
||
| 520 | } |
||
| 521 | break; |
||
| 522 | |||
| 523 | default: |
||
| 524 | $value = isset( $payment->$column_name ) ? $payment->$column_name : ''; |
||
| 525 | break; |
||
| 526 | |||
| 527 | }// End switch(). |
||
| 528 | |||
| 529 | return apply_filters( 'give_payments_table_column', $value, $payment->ID, $column_name ); |
||
| 530 | } |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Get donor email html. |
||
| 534 | * |
||
| 535 | * @param object $payment Contains all the data of the payment. |
||
| 536 | * |
||
| 537 | * @access public |
||
| 538 | * @since 1.0 |
||
| 539 | * |
||
| 540 | * @return string Data shown in the Email column |
||
| 541 | */ |
||
| 542 | public function get_donor_email( $payment ) { |
||
| 543 | |||
| 544 | $email = give_get_payment_user_email( $payment->ID ); |
||
| 545 | |||
| 546 | if ( empty( $email ) ) { |
||
| 547 | $email = __( '(unknown)', 'give' ); |
||
| 548 | } |
||
| 549 | |||
| 550 | |||
| 551 | $value = Give()->tooltips->render_link( array( |
||
| 552 | 'link' => "mailto:{$email}", |
||
| 553 | 'label' => __( 'Email donor', 'give' ), |
||
| 554 | 'tag_content' => $email, |
||
| 555 | ) ); |
||
| 556 | |||
| 557 | return apply_filters( 'give_payments_table_column', $value, $payment->ID, 'email' ); |
||
| 558 | } |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Get Row Actions |
||
| 562 | * |
||
| 563 | * @param object $payment Payment Data. |
||
| 564 | * |
||
| 565 | * @since 1.6 |
||
| 566 | * |
||
| 567 | * @return array $actions |
||
| 568 | */ |
||
| 569 | function get_row_actions( $payment ) { |
||
| 570 | |||
| 571 | $actions = array(); |
||
| 572 | $email = give_get_payment_user_email( $payment->ID ); |
||
| 573 | |||
| 574 | // Add search term string back to base URL. |
||
| 575 | $search_terms = ( isset( $_GET['s'] ) ? trim( $_GET['s'] ) : '' ); |
||
| 576 | if ( ! empty( $search_terms ) ) { |
||
| 577 | $this->base_url = add_query_arg( 's', $search_terms, $this->base_url ); |
||
| 578 | } |
||
| 579 | |||
| 580 | View Code Duplication | if ( give_is_payment_complete( $payment->ID ) && ! empty( $email ) ) { |
|
| 581 | |||
| 582 | $actions['email_links'] = sprintf( |
||
| 583 | '<a class="resend-single-donation-receipt" href="%1$s" aria-label="%2$s">%3$s</a>', wp_nonce_url( |
||
| 584 | add_query_arg( |
||
| 585 | array( |
||
| 586 | 'give-action' => 'email_links', |
||
| 587 | 'purchase_id' => $payment->ID, |
||
| 588 | ), $this->base_url |
||
| 589 | ), 'give_payment_nonce' |
||
| 590 | ), sprintf( __( 'Resend Donation %s Receipt', 'give' ), $payment->ID ), __( 'Resend Receipt', 'give' ) |
||
| 591 | ); |
||
| 592 | |||
| 593 | } |
||
| 594 | |||
| 595 | View Code Duplication | if ( current_user_can( 'view_give_payments' ) ) { |
|
| 596 | $actions['delete'] = sprintf( |
||
| 597 | '<a class="delete-single-donation" href="%1$s" aria-label="%2$s">%3$s</a>', |
||
| 598 | wp_nonce_url( |
||
| 599 | add_query_arg( |
||
| 600 | array( |
||
| 601 | 'give-action' => 'delete_payment', |
||
| 602 | 'purchase_id' => $payment->ID, |
||
| 603 | ), $this->base_url |
||
| 604 | ), 'give_donation_nonce' |
||
| 605 | ), sprintf( __( 'Delete Donation %s', 'give' ), $payment->ID ), __( 'Delete', 'give' ) |
||
| 606 | ); |
||
| 607 | } |
||
| 608 | |||
| 609 | return apply_filters( 'give_payment_row_actions', $actions, $payment ); |
||
| 610 | } |
||
| 611 | |||
| 612 | |||
| 613 | /** |
||
| 614 | * Get payment status html. |
||
| 615 | * |
||
| 616 | * @since 1.0 |
||
| 617 | * @access public |
||
| 618 | * |
||
| 619 | * @param Give_Payment $payment Contains all the data of the payment. |
||
| 620 | * |
||
| 621 | * @return string Data shown in the Email column |
||
| 622 | */ |
||
| 623 | function get_payment_status( $payment ) { |
||
| 624 | $value = sprintf( |
||
| 625 | '<div class="give-donation-status status-%1$s"><span class="give-donation-status-icon"></span> %2$s</div>', |
||
| 626 | $payment->status, |
||
| 627 | give_get_payment_status( $payment, true ) |
||
| 628 | ); |
||
| 629 | |||
| 630 | if ( $payment->mode == 'test' ) { |
||
| 631 | $value .= Give()->tooltips->render_span( array( |
||
| 632 | 'label' => __( 'This donation was made in test mode.', 'give' ), |
||
| 633 | 'tag_content' => __( 'Test', 'give' ), |
||
| 634 | 'attributes' => array( |
||
| 635 | 'class' => 'give-item-label give-item-label-orange give-test-mode-transactions-label', |
||
| 636 | ), |
||
| 637 | |||
| 638 | |||
| 639 | ) ); |
||
| 640 | } |
||
| 641 | |||
| 642 | if ( true === $payment->import && true === (bool) apply_filters( 'give_payment_show_importer_label', false ) ) { |
||
| 643 | $value .= sprintf( |
||
| 644 | ' <span class="give-item-label give-item-label-orange give-test-mode-transactions-label" data-tooltip="%1$s">%2$s</span>', |
||
| 645 | __( 'This donation was imported.', 'give' ), |
||
| 646 | __( 'Import', 'give' ) |
||
| 647 | ); |
||
| 648 | } |
||
| 649 | |||
| 650 | return $value; |
||
| 651 | } |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Get checkbox html. |
||
| 655 | * |
||
| 656 | * @param object $payment Contains all the data for the checkbox column. |
||
| 657 | * |
||
| 658 | * @access public |
||
| 659 | * @since 1.0 |
||
| 660 | * |
||
| 661 | * @return string Displays a checkbox. |
||
| 662 | */ |
||
| 663 | public function column_cb( $payment ) { |
||
| 664 | return sprintf( '<input type="checkbox" name="%1$s[]" value="%2$s" />', 'payment', $payment->ID ); |
||
| 665 | } |
||
| 666 | |||
| 667 | /** |
||
| 668 | * Get payment ID html. |
||
| 669 | * |
||
| 670 | * @param object $payment Contains all the data for the checkbox column. |
||
| 671 | * |
||
| 672 | * @access public |
||
| 673 | * @since 1.0 |
||
| 674 | * |
||
| 675 | * @return string Displays a checkbox. |
||
| 676 | */ |
||
| 677 | public function get_payment_id( $payment ) { |
||
| 678 | return '<span class="give-payment-id">' . give_get_payment_number( $payment->ID ) . '</span>'; |
||
| 679 | } |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Get donor html. |
||
| 683 | * |
||
| 684 | * @param object $payment Contains all the data of the payment. |
||
| 685 | * |
||
| 686 | * @access public |
||
| 687 | * @since 1.0 |
||
| 688 | * |
||
| 689 | * @return string Data shown in the User column |
||
| 690 | */ |
||
| 691 | public function get_donor( $payment ) { |
||
| 692 | |||
| 693 | $donor_id = give_get_payment_donor_id( $payment->ID ); |
||
| 694 | $donor_billing_name = give_get_donor_name_by( $payment->ID, 'donation' ); |
||
| 695 | $donor_name = give_get_donor_name_by( $donor_id, 'donor' ); |
||
| 696 | |||
| 697 | $value = ''; |
||
| 698 | if ( ! empty( $donor_id ) ) { |
||
| 699 | |||
| 700 | // Check whether the donor name and WP_User name is same or not. |
||
| 701 | if ( sanitize_title( $donor_billing_name ) !== sanitize_title( $donor_name ) ) { |
||
| 702 | $value .= $donor_billing_name . ' ('; |
||
| 703 | } |
||
| 704 | |||
| 705 | $value .= '<a href="' . esc_url( admin_url( "edit.php?post_type=give_forms&page=give-donors&view=overview&id=$donor_id" ) ) . '">' . $donor_name . '</a>'; |
||
| 706 | |||
| 707 | // Check whether the donor name and WP_User name is same or not. |
||
| 708 | if ( sanitize_title( $donor_billing_name ) != sanitize_title( $donor_name ) ) { |
||
| 709 | $value .= ')'; |
||
| 710 | } |
||
| 711 | } else { |
||
| 712 | $email = give_get_payment_user_email( $payment->ID ); |
||
| 713 | $value .= '<a href="' . esc_url( admin_url( "edit.php?post_type=give_forms&page=give-payment-history&s=$email" ) ) . '">' . __( '(donor missing)', 'give' ) . '</a>'; |
||
| 714 | } |
||
| 715 | |||
| 716 | return apply_filters( 'give_payments_table_column', $value, $payment->ID, 'donor' ); |
||
| 717 | } |
||
| 718 | |||
| 719 | /** |
||
| 720 | * Retrieve the bulk actions |
||
| 721 | * |
||
| 722 | * @access public |
||
| 723 | * @since 1.0 |
||
| 724 | * |
||
| 725 | * @return array $actions Array of the bulk actions |
||
| 726 | */ |
||
| 727 | public function get_bulk_actions() { |
||
| 728 | $actions = array( |
||
| 729 | 'delete' => __( 'Delete', 'give' ), |
||
| 730 | 'set-status-publish' => __( 'Set To Completed', 'give' ), |
||
| 731 | 'set-status-pending' => __( 'Set To Pending', 'give' ), |
||
| 732 | 'set-status-processing' => __( 'Set To Processing', 'give' ), |
||
| 733 | 'set-status-refunded' => __( 'Set To Refunded', 'give' ), |
||
| 734 | 'set-status-revoked' => __( 'Set To Revoked', 'give' ), |
||
| 735 | 'set-status-failed' => __( 'Set To Failed', 'give' ), |
||
| 736 | 'set-status-cancelled' => __( 'Set To Cancelled', 'give' ), |
||
| 737 | 'set-status-abandoned' => __( 'Set To Abandoned', 'give' ), |
||
| 738 | 'set-status-preapproval' => __( 'Set To Preapproval', 'give' ), |
||
| 739 | 'resend-receipt' => __( 'Resend Email Receipts', 'give' ), |
||
| 740 | ); |
||
| 741 | |||
| 742 | return apply_filters( 'give_payments_table_bulk_actions', $actions ); |
||
| 743 | } |
||
| 744 | |||
| 745 | /** |
||
| 746 | * Process the bulk actions |
||
| 747 | * |
||
| 748 | * @access public |
||
| 749 | * @since 1.0 |
||
| 750 | * |
||
| 751 | * @return void |
||
| 752 | */ |
||
| 753 | public function process_bulk_action() { |
||
| 754 | $ids = isset( $_GET['payment'] ) ? $_GET['payment'] : false; |
||
| 755 | $action = $this->current_action(); |
||
| 756 | |||
| 757 | if ( ! is_array( $ids ) ) { |
||
| 758 | $ids = array( $ids ); |
||
| 759 | } |
||
| 760 | |||
| 761 | if ( empty( $action ) ) { |
||
| 762 | return; |
||
| 763 | } |
||
| 764 | |||
| 765 | foreach ( $ids as $id ) { |
||
| 766 | |||
| 767 | // Detect when a bulk action is being triggered. |
||
| 768 | switch ( $this->current_action() ) { |
||
| 769 | |||
| 770 | case 'delete': |
||
| 771 | give_delete_donation( $id ); |
||
| 772 | break; |
||
| 773 | |||
| 774 | case 'set-status-publish': |
||
| 775 | give_update_payment_status( $id, 'publish' ); |
||
| 776 | break; |
||
| 777 | |||
| 778 | case 'set-status-pending': |
||
| 779 | give_update_payment_status( $id, 'pending' ); |
||
| 780 | break; |
||
| 781 | |||
| 782 | case 'set-status-processing': |
||
| 783 | give_update_payment_status( $id, 'processing' ); |
||
| 784 | break; |
||
| 785 | |||
| 786 | case 'set-status-refunded': |
||
| 787 | give_update_payment_status( $id, 'refunded' ); |
||
| 788 | break; |
||
| 789 | case 'set-status-revoked': |
||
| 790 | give_update_payment_status( $id, 'revoked' ); |
||
| 791 | break; |
||
| 792 | |||
| 793 | case 'set-status-failed': |
||
| 794 | give_update_payment_status( $id, 'failed' ); |
||
| 795 | break; |
||
| 796 | |||
| 797 | case 'set-status-cancelled': |
||
| 798 | give_update_payment_status( $id, 'cancelled' ); |
||
| 799 | break; |
||
| 800 | |||
| 801 | case 'set-status-abandoned': |
||
| 802 | give_update_payment_status( $id, 'abandoned' ); |
||
| 803 | break; |
||
| 804 | |||
| 805 | case 'set-status-preapproval': |
||
| 806 | give_update_payment_status( $id, 'preapproval' ); |
||
| 807 | break; |
||
| 808 | |||
| 809 | case 'resend-receipt': |
||
| 810 | /** |
||
| 811 | * Fire the action |
||
| 812 | * |
||
| 813 | * @since 2.0 |
||
| 814 | */ |
||
| 815 | do_action( 'give_donation-receipt_email_notification', $id ); |
||
| 816 | break; |
||
| 817 | }// End switch(). |
||
| 818 | |||
| 819 | /** |
||
| 820 | * Fires after triggering bulk action on payments table. |
||
| 821 | * |
||
| 822 | * @param int $id The ID of the payment. |
||
| 823 | * @param string $current_action The action that is being triggered. |
||
| 824 | * |
||
| 825 | * @since 1.7 |
||
| 826 | */ |
||
| 827 | do_action( 'give_payments_table_do_bulk_action', $id, $this->current_action() ); |
||
| 828 | }// End foreach(). |
||
| 829 | |||
| 830 | } |
||
| 831 | |||
| 832 | /** |
||
| 833 | * Retrieve the payment counts |
||
| 834 | * |
||
| 835 | * @access public |
||
| 836 | * @since 1.0 |
||
| 837 | * |
||
| 838 | * @return object |
||
| 839 | */ |
||
| 840 | public function get_payment_counts() { |
||
| 841 | |||
| 842 | $args = array(); |
||
| 843 | |||
| 844 | if ( isset( $_GET['user'] ) ) { |
||
| 845 | $args['user'] = urldecode( $_GET['user'] ); |
||
| 846 | } elseif ( isset( $_GET['donor'] ) ) { |
||
| 847 | $args['donor'] = absint( $_GET['donor'] ); |
||
| 848 | } elseif ( isset( $_GET['s'] ) ) { |
||
| 849 | $is_user = strpos( $_GET['s'], strtolower( 'user:' ) ) !== false; |
||
| 850 | if ( $is_user ) { |
||
| 851 | $args['user'] = absint( trim( str_replace( 'user:', '', strtolower( $_GET['s'] ) ) ) ); |
||
| 852 | unset( $args['s'] ); |
||
| 853 | } else { |
||
| 854 | $args['s'] = sanitize_text_field( $_GET['s'] ); |
||
| 855 | } |
||
| 856 | } |
||
| 857 | |||
| 858 | if ( ! empty( $_GET['start-date'] ) ) { |
||
| 859 | $args['start-date'] = urldecode( $_GET['start-date'] ); |
||
| 860 | } |
||
| 861 | |||
| 862 | if ( ! empty( $_GET['end-date'] ) ) { |
||
| 863 | $args['end-date'] = urldecode( $_GET['end-date'] ); |
||
| 864 | } |
||
| 865 | |||
| 866 | $args['form_id'] = ! empty( $_GET['form_id'] ) ? absint( $_GET['form_id'] ) : null; |
||
| 867 | $args['gateway'] = ! empty( $_GET['gateway'] ) ? give_clean( $_GET['gateway'] ) : null; |
||
| 868 | |||
| 869 | $payment_count = give_count_payments( $args ); |
||
| 870 | $this->complete_count = $payment_count->publish; |
||
| 871 | $this->pending_count = $payment_count->pending; |
||
| 872 | $this->processing_count = $payment_count->processing; |
||
| 873 | $this->refunded_count = $payment_count->refunded; |
||
| 874 | $this->failed_count = $payment_count->failed; |
||
| 875 | $this->revoked_count = $payment_count->revoked; |
||
| 876 | $this->cancelled_count = $payment_count->cancelled; |
||
| 877 | $this->abandoned_count = $payment_count->abandoned; |
||
| 878 | $this->preapproval_count = $payment_count->preapproval; |
||
| 879 | |||
| 880 | foreach ( $payment_count as $count ) { |
||
| 881 | $this->total_count += $count; |
||
| 882 | } |
||
| 883 | |||
| 884 | return $payment_count; |
||
| 885 | } |
||
| 886 | |||
| 887 | /** |
||
| 888 | * Retrieve all the data for all the payments. |
||
| 889 | * |
||
| 890 | * @access public |
||
| 891 | * @since 1.0 |
||
| 892 | * |
||
| 893 | * @return array objects in array containing all the data for the payments |
||
| 894 | */ |
||
| 895 | public function payments_data() { |
||
| 896 | $per_page = $this->per_page; |
||
| 897 | $orderby = isset( $_GET['orderby'] ) ? urldecode( $_GET['orderby'] ) : 'ID'; |
||
| 898 | $order = isset( $_GET['order'] ) ? $_GET['order'] : 'DESC'; |
||
| 899 | $user = isset( $_GET['user'] ) ? $_GET['user'] : null; |
||
| 900 | $donor = isset( $_GET['donor'] ) ? $_GET['donor'] : null; |
||
| 901 | $status = isset( $_GET['status'] ) ? $_GET['status'] : give_get_payment_status_keys(); |
||
| 902 | $meta_key = isset( $_GET['meta_key'] ) ? $_GET['meta_key'] : null; |
||
| 903 | $year = isset( $_GET['year'] ) ? $_GET['year'] : null; |
||
| 904 | $month = isset( $_GET['m'] ) ? $_GET['m'] : null; |
||
| 905 | $day = isset( $_GET['day'] ) ? $_GET['day'] : null; |
||
| 906 | $search = isset( $_GET['s'] ) ? sanitize_text_field( $_GET['s'] ) : null; |
||
| 907 | $start_date = ! empty ( $_GET['start-date'] ) ? sanitize_text_field( $_GET['start-date'] ) : date( give_date_format(), 0 ); |
||
| 908 | $end_date = ! empty( $_GET['end-date'] ) |
||
| 909 | ? sanitize_text_field( $_GET['end-date'] ) |
||
| 910 | : date( give_date_format(), current_time( 'timestamp' ) ); |
||
| 911 | $form_id = ! empty( $_GET['form_id'] ) ? absint( $_GET['form_id'] ) : null; |
||
| 912 | $gateway = ! empty( $_GET['gateway'] ) ? give_clean( $_GET['gateway'] ) : null; |
||
| 913 | |||
| 914 | $args = array( |
||
| 915 | 'output' => 'payments', |
||
| 916 | 'number' => $per_page, |
||
| 917 | 'page' => isset( $_GET['paged'] ) ? $_GET['paged'] : null, |
||
| 918 | 'orderby' => $orderby, |
||
| 919 | 'order' => $order, |
||
| 920 | 'user' => $user, |
||
| 921 | 'donor' => $donor, |
||
| 922 | 'status' => $status, |
||
| 923 | 'meta_key' => $meta_key, |
||
| 924 | 'year' => $year, |
||
| 925 | 'month' => $month, |
||
| 926 | 'day' => $day, |
||
| 927 | 's' => $search, |
||
| 928 | 'start_date' => $start_date, |
||
| 929 | 'gateway' => $gateway, |
||
| 930 | 'end_date' => $end_date, |
||
| 931 | 'give_forms' => $form_id, |
||
| 932 | ); |
||
| 933 | |||
| 934 | if ( is_string( $search ) && false !== strpos( $search, 'txn:' ) ) { |
||
| 935 | $args['search_in_notes'] = true; |
||
| 936 | $args['s'] = trim( str_replace( 'txn:', '', $args['s'] ) ); |
||
| 937 | } |
||
| 938 | |||
| 939 | /** |
||
| 940 | * Filter to modify payment table argument. |
||
| 941 | * |
||
| 942 | * @since 1.8.18 |
||
| 943 | */ |
||
| 944 | $args = (array) apply_filters( 'give_payment_table_payments_query', $args ); |
||
| 945 | |||
| 946 | $p_query = new Give_Payments_Query( $args ); |
||
| 947 | |||
| 948 | return $p_query->get_payments(); |
||
| 949 | |||
| 950 | } |
||
| 951 | |||
| 952 | /** |
||
| 953 | * Setup the final data for the table |
||
| 954 | * |
||
| 955 | * @access public |
||
| 956 | * @since 1.0 |
||
| 957 | * @uses Give_Payment_History_Table::get_columns() |
||
| 958 | * @uses Give_Payment_History_Table::get_sortable_columns() |
||
| 959 | * @uses Give_Payment_History_Table::payments_data() |
||
| 960 | * @uses WP_List_Table::get_pagenum() |
||
| 961 | * @uses WP_List_Table::set_pagination_args() |
||
| 962 | * |
||
| 963 | * @return void |
||
| 964 | */ |
||
| 965 | public function prepare_items() { |
||
| 1035 | } |
||
| 1036 |