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 Transactions_Admin_Page 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 Transactions_Admin_Page, and based on these observations, apply Extract Interface, too.
| 1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) exit('No direct script access allowed'); |
||
| 24 | class Transactions_Admin_Page extends EE_Admin_Page { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var EE_Transaction |
||
| 28 | */ |
||
| 29 | private $_transaction; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var EE_Session |
||
| 33 | */ |
||
| 34 | private $_session; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array $_txn_status |
||
| 38 | */ |
||
| 39 | private static $_txn_status; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var array $_pay_status |
||
| 43 | */ |
||
| 44 | private static $_pay_status; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array $_existing_reg_payment_REG_IDs |
||
| 48 | */ |
||
| 49 | protected $_existing_reg_payment_REG_IDs = null; |
||
| 50 | |||
| 51 | |||
| 52 | |||
| 53 | /** |
||
| 54 | * @Constructor |
||
| 55 | * @access public |
||
| 56 | * @param bool $routing |
||
| 57 | * @return Transactions_Admin_Page |
||
|
|
|||
| 58 | */ |
||
| 59 | public function __construct( $routing = TRUE ) { |
||
| 62 | |||
| 63 | |||
| 64 | |||
| 65 | /** |
||
| 66 | * _init_page_props |
||
| 67 | * @return void |
||
| 68 | */ |
||
| 69 | protected function _init_page_props() { |
||
| 70 | $this->page_slug = TXN_PG_SLUG; |
||
| 71 | $this->page_label = __('Transactions', 'event_espresso'); |
||
| 72 | $this->_admin_base_url = TXN_ADMIN_URL; |
||
| 73 | $this->_admin_base_path = TXN_ADMIN; |
||
| 74 | } |
||
| 75 | |||
| 76 | |||
| 77 | |||
| 78 | /** |
||
| 79 | * _ajax_hooks |
||
| 80 | * @return void |
||
| 81 | */ |
||
| 82 | protected function _ajax_hooks() { |
||
| 83 | add_action('wp_ajax_espresso_apply_payment', array( $this, 'apply_payments_or_refunds')); |
||
| 84 | add_action('wp_ajax_espresso_apply_refund', array( $this, 'apply_payments_or_refunds')); |
||
| 85 | add_action('wp_ajax_espresso_delete_payment', array( $this, 'delete_payment')); |
||
| 86 | } |
||
| 87 | |||
| 88 | |||
| 89 | |||
| 90 | /** |
||
| 91 | * _define_page_props |
||
| 92 | * @return void |
||
| 93 | */ |
||
| 94 | View Code Duplication | protected function _define_page_props() { |
|
| 95 | $this->_admin_page_title = $this->page_label; |
||
| 96 | $this->_labels = array( |
||
| 97 | 'buttons' => array( |
||
| 98 | 'add' => __('Add New Transaction', 'event_espresso'), |
||
| 99 | 'edit' => __('Edit Transaction', 'event_espresso'), |
||
| 100 | 'delete' => __('Delete Transaction','event_espresso'), |
||
| 101 | ) |
||
| 102 | ); |
||
| 103 | } |
||
| 104 | |||
| 105 | |||
| 106 | |||
| 107 | /** |
||
| 108 | * grab url requests and route them |
||
| 109 | * @access private |
||
| 110 | * @return void |
||
| 111 | */ |
||
| 112 | public function _set_page_routes() { |
||
| 113 | |||
| 114 | $this->_set_transaction_status_array(); |
||
| 115 | |||
| 116 | $txn_id = ! empty( $this->_req_data['TXN_ID'] ) && ! is_array( $this->_req_data['TXN_ID'] ) ? $this->_req_data['TXN_ID'] : 0; |
||
| 117 | |||
| 118 | $this->_page_routes = array( |
||
| 119 | |||
| 120 | 'default' => array( |
||
| 121 | 'func' => '_transactions_overview_list_table', |
||
| 122 | 'capability' => 'ee_read_transactions' |
||
| 123 | ), |
||
| 124 | |||
| 125 | 'view_transaction' => array( |
||
| 126 | 'func' => '_transaction_details', |
||
| 127 | 'capability' => 'ee_read_transaction', |
||
| 128 | 'obj_id' => $txn_id |
||
| 129 | ), |
||
| 130 | |||
| 131 | 'send_payment_reminder' => array( |
||
| 132 | 'func' => '_send_payment_reminder', |
||
| 133 | 'noheader' => TRUE, |
||
| 134 | 'capability' => 'ee_send_message' |
||
| 135 | ), |
||
| 136 | |||
| 137 | 'espresso_apply_payment' => array( |
||
| 138 | 'func' => 'apply_payments_or_refunds', |
||
| 139 | 'noheader' => TRUE, |
||
| 140 | 'capability' => 'ee_edit_payments' |
||
| 141 | ), |
||
| 142 | |||
| 143 | 'espresso_apply_refund' => array( |
||
| 144 | 'func' => 'apply_payments_or_refunds', |
||
| 145 | 'noheader' => TRUE, |
||
| 146 | 'capability' => 'ee_edit_payments' |
||
| 147 | ), |
||
| 148 | |||
| 149 | 'espresso_delete_payment' => array( |
||
| 150 | 'func' => 'delete_payment', |
||
| 151 | 'noheader' => TRUE, |
||
| 152 | 'capability' => 'ee_delete_payments' |
||
| 153 | ), |
||
| 154 | |||
| 155 | ); |
||
| 156 | |||
| 157 | } |
||
| 158 | |||
| 159 | |||
| 160 | |||
| 161 | |||
| 162 | |||
| 163 | |||
| 164 | |||
| 165 | |||
| 166 | protected function _set_page_config() { |
||
| 167 | $this->_page_config = array( |
||
| 168 | 'default' => array( |
||
| 169 | 'nav' => array( |
||
| 170 | 'label' => __('Overview', 'event_espresso'), |
||
| 171 | 'order' => 10 |
||
| 172 | ), |
||
| 173 | 'list_table' => 'EE_Admin_Transactions_List_Table', |
||
| 174 | 'help_tabs' => array( |
||
| 175 | 'transactions_overview_help_tab' => array( |
||
| 176 | 'title' => __('Transactions Overview', 'event_espresso'), |
||
| 177 | 'filename' => 'transactions_overview' |
||
| 178 | ), |
||
| 179 | 'transactions_overview_table_column_headings_help_tab' => array( |
||
| 180 | 'title' => __('Transactions Table Column Headings', 'event_espresso'), |
||
| 181 | 'filename' => 'transactions_overview_table_column_headings' |
||
| 182 | ), |
||
| 183 | 'transactions_overview_views_filters_help_tab' => array( |
||
| 184 | 'title' => __('Transaction Views & Filters & Search', 'event_espresso'), |
||
| 185 | 'filename' => 'transactions_overview_views_filters_search' |
||
| 186 | ), |
||
| 187 | ), |
||
| 188 | 'help_tour' => array( 'Transactions_Overview_Help_Tour' ), |
||
| 189 | /** |
||
| 190 | * commented out because currently we are not displaying tips for transaction list table status but this |
||
| 191 | * may change in a later iteration so want to keep the code for then. |
||
| 192 | */ |
||
| 193 | //'qtips' => array( 'Transactions_List_Table_Tips' ), |
||
| 194 | 'require_nonce' => FALSE |
||
| 195 | ), |
||
| 196 | 'view_transaction' => array( |
||
| 197 | 'nav' => array( |
||
| 198 | 'label' => __('View Transaction', 'event_espresso'), |
||
| 199 | 'order' => 5, |
||
| 200 | 'url' => isset($this->_req_data['TXN_ID']) ? add_query_arg(array('TXN_ID' => $this->_req_data['TXN_ID'] ), $this->_current_page_view_url ) : $this->_admin_base_url, |
||
| 201 | 'persistent' => FALSE |
||
| 202 | ), |
||
| 203 | 'help_tabs' => array( |
||
| 204 | 'transactions_view_transaction_help_tab' => array( |
||
| 205 | 'title' => __('View Transaction', 'event_espresso'), |
||
| 206 | 'filename' => 'transactions_view_transaction' |
||
| 207 | ), |
||
| 208 | 'transactions_view_transaction_transaction_details_table_help_tab' => array( |
||
| 209 | 'title' => __('Transaction Details Table', 'event_espresso'), |
||
| 210 | 'filename' => 'transactions_view_transaction_transaction_details_table' |
||
| 211 | ), |
||
| 212 | 'transactions_view_transaction_attendees_registered_help_tab' => array( |
||
| 213 | 'title' => __('Attendees Registered', 'event_espresso'), |
||
| 214 | 'filename' => 'transactions_view_transaction_attendees_registered' |
||
| 215 | ), |
||
| 216 | 'transactions_view_transaction_views_primary_registrant_billing_information_help_tab' => array( |
||
| 217 | 'title' => __('Primary Registrant & Billing Information', 'event_espresso'), |
||
| 218 | 'filename' => 'transactions_view_transaction_primary_registrant_billing_information' |
||
| 219 | ), |
||
| 220 | ), |
||
| 221 | 'qtips' => array( 'Transaction_Details_Tips' ), |
||
| 222 | 'help_tour' => array( 'Transaction_Details_Help_Tour' ), |
||
| 223 | 'metaboxes' => array('_transaction_details_metaboxes'), |
||
| 224 | |||
| 225 | 'require_nonce' => FALSE |
||
| 226 | ) |
||
| 227 | ); |
||
| 228 | } |
||
| 229 | |||
| 230 | |||
| 231 | /** |
||
| 232 | * The below methods aren't used by this class currently |
||
| 233 | */ |
||
| 234 | protected function _add_screen_options() {} |
||
| 236 | public function admin_init() { |
||
| 237 | // IF a registration was JUST added via the admin... |
||
| 238 | if ( |
||
| 239 | isset( |
||
| 240 | $this->_req_data[ 'redirect_from' ], |
||
| 241 | $this->_req_data[ 'EVT_ID' ], |
||
| 242 | $this->_req_data[ 'event_name' ] |
||
| 243 | ) |
||
| 244 | ) { |
||
| 245 | // then set a cookie so that we can block any attempts to use |
||
| 246 | // the back button as a way to enter another registration. |
||
| 247 | setcookie( 'ee_registration_added', $this->_req_data[ 'EVT_ID' ], time() + WEEK_IN_SECONDS, '/' ); |
||
| 248 | // and update the global |
||
| 249 | $_COOKIE[ 'ee_registration_added' ] = $this->_req_data[ 'EVT_ID' ]; |
||
| 250 | } |
||
| 251 | EE_Registry::$i18n_js_strings[ 'invalid_server_response' ] = __( 'An error occurred! Your request may have been processed, but a valid response from the server was not received. Please refresh the page and try again.', 'event_espresso' ); |
||
| 252 | EE_Registry::$i18n_js_strings[ 'error_occurred' ] = __( 'An error occurred! Please refresh the page and try again.', 'event_espresso' ); |
||
| 253 | EE_Registry::$i18n_js_strings[ 'txn_status_array' ] = self::$_txn_status; |
||
| 254 | EE_Registry::$i18n_js_strings[ 'pay_status_array' ] = self::$_pay_status; |
||
| 255 | } |
||
| 256 | public function admin_notices() {} |
||
| 258 | |||
| 259 | |||
| 260 | |||
| 261 | /** |
||
| 262 | * _set_transaction_status_array |
||
| 263 | * sets list of transaction statuses |
||
| 264 | * |
||
| 265 | * @access private |
||
| 266 | * @return void |
||
| 267 | */ |
||
| 268 | private function _set_transaction_status_array() { |
||
| 271 | |||
| 272 | |||
| 273 | |||
| 274 | /** |
||
| 275 | * get_transaction_status_array |
||
| 276 | * return the transaction status array for wp_list_table |
||
| 277 | * |
||
| 278 | * @access public |
||
| 279 | * @return array |
||
| 280 | */ |
||
| 281 | public function get_transaction_status_array() { |
||
| 284 | |||
| 285 | |||
| 286 | |||
| 287 | /** |
||
| 288 | * get list of payment statuses |
||
| 289 | * |
||
| 290 | * @access private |
||
| 291 | * @return void |
||
| 292 | */ |
||
| 293 | private function _get_payment_status_array() { |
||
| 298 | |||
| 299 | |||
| 300 | |||
| 301 | /** |
||
| 302 | * _add_screen_options_default |
||
| 303 | * |
||
| 304 | * @access protected |
||
| 305 | * @return void |
||
| 306 | */ |
||
| 307 | protected function _add_screen_options_default() { |
||
| 310 | |||
| 311 | |||
| 312 | |||
| 313 | /** |
||
| 314 | * load_scripts_styles |
||
| 315 | * |
||
| 316 | * @access public |
||
| 317 | * @return void |
||
| 318 | */ |
||
| 319 | public function load_scripts_styles() { |
||
| 332 | |||
| 333 | |||
| 334 | |||
| 335 | /** |
||
| 336 | * load_scripts_styles_view_transaction |
||
| 337 | * |
||
| 338 | * @access public |
||
| 339 | * @return void |
||
| 340 | */ |
||
| 341 | public function load_scripts_styles_view_transaction() { |
||
| 345 | |||
| 346 | |||
| 347 | |||
| 348 | /** |
||
| 349 | * load_scripts_styles_default |
||
| 350 | * |
||
| 351 | * @access public |
||
| 352 | * @return void |
||
| 353 | */ |
||
| 354 | public function load_scripts_styles_default() { |
||
| 358 | |||
| 359 | |||
| 360 | |||
| 361 | /** |
||
| 362 | * _set_list_table_views_default |
||
| 363 | * |
||
| 364 | * @access protected |
||
| 365 | * @return void |
||
| 366 | */ |
||
| 367 | protected function _set_list_table_views_default() { |
||
| 386 | |||
| 387 | |||
| 388 | |||
| 389 | /** |
||
| 390 | * _set_transaction_object |
||
| 391 | * This sets the _transaction property for the transaction details screen |
||
| 392 | * |
||
| 393 | * @access private |
||
| 394 | * @return void |
||
| 395 | */ |
||
| 396 | private function _set_transaction_object() { |
||
| 413 | |||
| 414 | |||
| 415 | |||
| 416 | /** |
||
| 417 | * _transaction_legend_items |
||
| 418 | * |
||
| 419 | * @access protected |
||
| 420 | * @return array |
||
| 421 | */ |
||
| 422 | protected function _transaction_legend_items() { |
||
| 493 | |||
| 494 | |||
| 495 | |||
| 496 | /** |
||
| 497 | * _transactions_overview_list_table |
||
| 498 | * |
||
| 499 | * @access protected |
||
| 500 | * @return void |
||
| 501 | */ |
||
| 502 | protected function _transactions_overview_list_table() { |
||
| 509 | |||
| 510 | |||
| 511 | |||
| 512 | /** |
||
| 513 | * _transaction_details |
||
| 514 | * generates HTML for the View Transaction Details Admin page |
||
| 515 | * |
||
| 516 | * @access protected |
||
| 517 | * @return void |
||
| 518 | */ |
||
| 519 | protected function _transaction_details() { |
||
| 520 | do_action( 'AHEE__Transactions_Admin_Page__transaction_details__start', $this->_transaction ); |
||
| 521 | EE_Registry::instance()->load_helper( 'MSG_Template' ); |
||
| 522 | |||
| 523 | $this->_set_transaction_status_array(); |
||
| 524 | |||
| 525 | $this->_template_args = array(); |
||
| 526 | $this->_template_args['transactions_page'] = $this->_wp_page_slug; |
||
| 527 | |||
| 528 | $this->_set_transaction_object(); |
||
| 529 | |||
| 530 | $primary_registration = $this->_transaction->primary_registration(); |
||
| 531 | $attendee = $primary_registration instanceof EE_Registration ? $primary_registration->attendee() : NULL; |
||
| 532 | |||
| 533 | $this->_template_args['txn_nmbr']['value'] = $this->_transaction->ID(); |
||
| 534 | $this->_template_args['txn_nmbr']['label'] = __( 'Transaction Number', 'event_espresso' ); |
||
| 535 | |||
| 536 | $this->_template_args['txn_datetime']['value'] = $this->_transaction->get_i18n_datetime('TXN_timestamp'); |
||
| 537 | $this->_template_args['txn_datetime']['label'] = __( 'Date', 'event_espresso' ); |
||
| 538 | |||
| 539 | $this->_template_args['txn_status']['value'] = self::$_txn_status[ $this->_transaction->get('STS_ID') ]; |
||
| 540 | $this->_template_args['txn_status']['label'] = __( 'Transaction Status', 'event_espresso' ); |
||
| 541 | $this->_template_args['txn_status']['class'] = 'status-' . $this->_transaction->get('STS_ID'); |
||
| 542 | |||
| 543 | $this->_template_args['grand_total'] = $this->_transaction->get('TXN_total'); |
||
| 544 | $this->_template_args['total_paid'] = $this->_transaction->get('TXN_paid'); |
||
| 545 | |||
| 546 | if ( |
||
| 547 | $attendee instanceof EE_Attendee |
||
| 548 | && EE_Registry::instance()->CAP->current_user_can( |
||
| 549 | 'ee_send_message', |
||
| 550 | 'espresso_transactions_send_payment_reminder' |
||
| 551 | ) |
||
| 552 | ) { |
||
| 553 | EE_Registry::instance()->load_helper( 'MSG_Template' ); |
||
| 554 | $this->_template_args['send_payment_reminder_button'] = |
||
| 555 | EEH_MSG_Template::is_mt_active( 'payment_reminder' ) |
||
| 556 | && $this->_transaction->get('STS_ID') != EEM_Transaction::complete_status_code |
||
| 557 | && $this->_transaction->get('STS_ID') != EEM_Transaction::overpaid_status_code |
||
| 558 | ? EEH_Template::get_button_or_link( |
||
| 559 | EE_Admin_Page::add_query_args_and_nonce( |
||
| 560 | array( |
||
| 561 | 'action'=>'send_payment_reminder', |
||
| 562 | 'TXN_ID'=>$this->_transaction->ID(), |
||
| 563 | 'redirect_to' => 'view_transaction' |
||
| 564 | ), |
||
| 565 | TXN_ADMIN_URL |
||
| 566 | ), |
||
| 567 | __(' Send Payment Reminder', 'event_espresso'), |
||
| 568 | 'button secondary-button right', |
||
| 569 | 'dashicons dashicons-email-alt' |
||
| 570 | ) |
||
| 571 | : ''; |
||
| 572 | } else { |
||
| 573 | $this->_template_args['send_payment_reminder_button'] = ''; |
||
| 574 | } |
||
| 575 | |||
| 576 | $amount_due = $this->_transaction->get('TXN_total') - $this->_transaction->get('TXN_paid'); |
||
| 577 | $this->_template_args['amount_due'] = EEH_Template::format_currency( $amount_due, TRUE ); |
||
| 578 | if ( EE_Registry::instance()->CFG->currency->sign_b4 ) { |
||
| 579 | $this->_template_args['amount_due'] = EE_Registry::instance()->CFG->currency->sign . $this->_template_args['amount_due']; |
||
| 580 | } else { |
||
| 581 | $this->_template_args['amount_due'] = $this->_template_args['amount_due'] . EE_Registry::instance()->CFG->currency->sign; |
||
| 582 | } |
||
| 583 | $this->_template_args['amount_due_class'] = ''; |
||
| 584 | |||
| 585 | if ( $this->_transaction->get('TXN_paid') == $this->_transaction->get('TXN_total') ) { |
||
| 586 | // paid in full |
||
| 587 | $this->_template_args['amount_due'] = FALSE; |
||
| 588 | View Code Duplication | } elseif ( $this->_transaction->get('TXN_paid') > $this->_transaction->get('TXN_total') ) { |
|
| 589 | // overpaid |
||
| 590 | $this->_template_args['amount_due_class'] = 'txn-overview-no-payment-spn'; |
||
| 591 | } elseif (( $this->_transaction->get('TXN_total') > 0 ) && ( $this->_transaction->get('TXN_paid') > 0 )) { |
||
| 592 | // monies owing |
||
| 593 | $this->_template_args['amount_due_class'] = 'txn-overview-part-payment-spn'; |
||
| 594 | View Code Duplication | } elseif (( $this->_transaction->get('TXN_total') > 0 ) && ( $this->_transaction->get('TXN_paid') == 0 )) { |
|
| 595 | // no payments made yet |
||
| 596 | $this->_template_args['amount_due_class'] = 'txn-overview-no-payment-spn'; |
||
| 597 | } elseif ( $this->_transaction->get('TXN_total') == 0 ) { |
||
| 598 | // free event |
||
| 599 | $this->_template_args['amount_due'] = FALSE; |
||
| 600 | } |
||
| 601 | |||
| 602 | $payment_method = $this->_transaction->payment_method(); |
||
| 603 | |||
| 604 | $this->_template_args['method_of_payment_name'] = $payment_method instanceof EE_Payment_Method |
||
| 605 | ? $payment_method->admin_name() |
||
| 606 | : __( 'Unknown', 'event_espresso' ); |
||
| 607 | |||
| 608 | $this->_template_args['currency_sign'] = EE_Registry::instance()->CFG->currency->sign; |
||
| 609 | // link back to overview |
||
| 610 | $this->_template_args['txn_overview_url'] = ! empty ( $_SERVER['HTTP_REFERER'] ) |
||
| 611 | ? $_SERVER['HTTP_REFERER'] |
||
| 612 | : TXN_ADMIN_URL; |
||
| 613 | |||
| 614 | |||
| 615 | // next link |
||
| 616 | $next_txn = $this->_transaction->next( |
||
| 617 | null, |
||
| 618 | array( array( 'STS_ID' => array( '!=', EEM_Transaction::failed_status_code ) ) ), |
||
| 619 | 'TXN_ID' |
||
| 620 | ); |
||
| 621 | $this->_template_args['next_transaction'] = $next_txn |
||
| 622 | ? $this->_next_link( |
||
| 623 | EE_Admin_Page::add_query_args_and_nonce( |
||
| 624 | array( 'action' => 'view_transaction', 'TXN_ID' => $next_txn['TXN_ID'] ), |
||
| 625 | TXN_ADMIN_URL |
||
| 626 | ), |
||
| 627 | 'dashicons dashicons-arrow-right ee-icon-size-22' |
||
| 628 | ) |
||
| 629 | : ''; |
||
| 630 | // previous link |
||
| 631 | $previous_txn = $this->_transaction->previous( |
||
| 632 | null, |
||
| 633 | array( array( 'STS_ID' => array( '!=', EEM_Transaction::failed_status_code ) ) ), |
||
| 634 | 'TXN_ID' |
||
| 635 | ); |
||
| 636 | $this->_template_args['previous_transaction'] = $previous_txn |
||
| 637 | ? $this->_previous_link( |
||
| 638 | EE_Admin_Page::add_query_args_and_nonce( |
||
| 639 | array( 'action' => 'view_transaction', 'TXN_ID' => $previous_txn['TXN_ID'] ), |
||
| 640 | TXN_ADMIN_URL |
||
| 641 | ), |
||
| 642 | 'dashicons dashicons-arrow-left ee-icon-size-22' |
||
| 643 | ) |
||
| 644 | : ''; |
||
| 645 | |||
| 646 | // were we just redirected here after adding a new registration ??? |
||
| 647 | if ( |
||
| 648 | isset( |
||
| 649 | $this->_req_data[ 'redirect_from' ], |
||
| 650 | $this->_req_data[ 'EVT_ID' ], |
||
| 651 | $this->_req_data[ 'event_name' ] |
||
| 652 | ) |
||
| 653 | ) { |
||
| 654 | if ( |
||
| 655 | EE_Registry::instance()->CAP->current_user_can( |
||
| 656 | 'ee_edit_registrations', |
||
| 657 | 'espresso_registrations_new_registration', |
||
| 658 | $this->_req_data[ 'EVT_ID' ] |
||
| 659 | ) |
||
| 660 | ) { |
||
| 661 | $this->_admin_page_title .= '<a id="add-new-registration" class="add-new-h2 button-primary" href="'; |
||
| 662 | $this->_admin_page_title .= EE_Admin_Page::add_query_args_and_nonce( |
||
| 663 | array( |
||
| 664 | 'page' => 'espresso_registrations', |
||
| 665 | 'action' => 'new_registration', |
||
| 666 | 'return' => 'default', |
||
| 667 | 'TXN_ID' => $this->_transaction->ID(), |
||
| 668 | 'event_id' => $this->_req_data[ 'EVT_ID' ], |
||
| 669 | ), |
||
| 670 | REG_ADMIN_URL |
||
| 671 | ); |
||
| 672 | $this->_admin_page_title .= '">'; |
||
| 673 | |||
| 674 | $this->_admin_page_title .= sprintf( |
||
| 675 | __('Add Another New Registration to Event: "%1$s" ?'), |
||
| 676 | htmlentities( urldecode( $this->_req_data[ 'event_name' ] ), ENT_QUOTES, 'UTF-8' ) |
||
| 677 | ); |
||
| 678 | $this->_admin_page_title .= '</a>'; |
||
| 679 | } |
||
| 680 | EE_Registry::instance()->SSN->clear_session( __CLASS__, __FUNCTION__ ); |
||
| 681 | } |
||
| 682 | // grab messages at the last second |
||
| 683 | $this->_template_args['notices'] = EE_Error::get_notices(); |
||
| 684 | // path to template |
||
| 685 | $template_path = TXN_TEMPLATE_PATH . 'txn_admin_details_header.template.php'; |
||
| 686 | $this->_template_args['admin_page_header'] = EEH_Template::display_template( $template_path, $this->_template_args, TRUE ); |
||
| 687 | |||
| 688 | // the details template wrapper |
||
| 689 | $this->display_admin_page_with_sidebar(); |
||
| 690 | |||
| 691 | } |
||
| 692 | |||
| 693 | |||
| 694 | |||
| 695 | /** |
||
| 696 | * _transaction_details_metaboxes |
||
| 697 | * |
||
| 698 | * @access protected |
||
| 699 | * @return void |
||
| 700 | */ |
||
| 701 | protected function _transaction_details_metaboxes() { |
||
| 719 | |||
| 720 | |||
| 721 | |||
| 722 | /** |
||
| 723 | * txn_details_meta_box |
||
| 724 | * generates HTML for the Transaction main meta box |
||
| 725 | * |
||
| 726 | * @access public |
||
| 727 | * @return void |
||
| 728 | */ |
||
| 729 | public function txn_details_meta_box() { |
||
| 812 | |||
| 813 | |||
| 814 | |||
| 815 | /** |
||
| 816 | * _get_registration_payment_IDs |
||
| 817 | * |
||
| 818 | * generates an array of Payment IDs and their corresponding Registration IDs |
||
| 819 | * |
||
| 820 | * @access protected |
||
| 821 | * @param EE_Payment[] $payments |
||
| 822 | * @return array |
||
| 823 | */ |
||
| 824 | protected function _get_registration_payment_IDs( $payments = array() ) { |
||
| 851 | |||
| 852 | |||
| 853 | |||
| 854 | /** |
||
| 855 | * _get_registrations_to_apply_payment_to |
||
| 856 | * |
||
| 857 | * generates HTML for displaying a series of checkboxes in the admin payment modal window |
||
| 858 | * which allows the admin to only apply the payment to the specific registrations |
||
| 859 | * |
||
| 860 | * @access protected |
||
| 861 | * @return void |
||
| 862 | */ |
||
| 863 | protected function _get_registrations_to_apply_payment_to() { |
||
| 915 | |||
| 916 | |||
| 917 | |||
| 918 | /** |
||
| 919 | * _get_reg_status_selection |
||
| 920 | * |
||
| 921 | * @todo this will need to be adjusted either once MER comes along OR we move default reg status to tickets instead of events. |
||
| 922 | * @access protected |
||
| 923 | * @return void |
||
| 924 | */ |
||
| 925 | protected function _get_reg_status_selection() { |
||
| 935 | |||
| 936 | |||
| 937 | |||
| 938 | /** |
||
| 939 | * _get_payment_methods |
||
| 940 | * Gets all the payment methods available generally, or the ones that are already |
||
| 941 | * selected on these payments (in case their payment methods are no longer active). |
||
| 942 | * Has the side-effect of updating the template args' payment_methods item |
||
| 943 | * @access private |
||
| 944 | * @param EE_Payment[] to show on this page |
||
| 945 | * @return void |
||
| 946 | */ |
||
| 947 | private function _get_payment_methods( $payments = array() ) { |
||
| 963 | |||
| 964 | |||
| 965 | |||
| 966 | /** |
||
| 967 | * txn_attendees_meta_box |
||
| 968 | * generates HTML for the Attendees Transaction main meta box |
||
| 969 | * |
||
| 970 | * @access public |
||
| 971 | * @param WP_Post $post |
||
| 972 | * @param array $metabox |
||
| 973 | * @return void |
||
| 974 | */ |
||
| 975 | public function txn_attendees_meta_box( $post, $metabox = array( 'args' => array() )) { |
||
| 1035 | |||
| 1036 | |||
| 1037 | |||
| 1038 | /** |
||
| 1039 | * txn_registrant_side_meta_box |
||
| 1040 | * generates HTML for the Edit Transaction side meta box |
||
| 1041 | * |
||
| 1042 | * @access public |
||
| 1043 | * @throws \EE_Error |
||
| 1044 | * @return void |
||
| 1045 | */ |
||
| 1046 | public function txn_registrant_side_meta_box() { |
||
| 1063 | |||
| 1064 | |||
| 1065 | |||
| 1066 | /** |
||
| 1067 | * txn_billing_info_side_meta_box |
||
| 1068 | * generates HTML for the Edit Transaction side meta box |
||
| 1069 | * |
||
| 1070 | * @access public |
||
| 1071 | * @return void |
||
| 1072 | */ |
||
| 1073 | public function txn_billing_info_side_meta_box() { |
||
| 1084 | |||
| 1085 | |||
| 1086 | |||
| 1087 | /** |
||
| 1088 | * apply_payments_or_refunds |
||
| 1089 | * registers a payment or refund made towards a transaction |
||
| 1090 | * |
||
| 1091 | * @access public |
||
| 1092 | * @return void |
||
| 1093 | */ |
||
| 1094 | public function apply_payments_or_refunds() { |
||
| 1095 | $json_response_data = array( 'return_data' => FALSE ); |
||
| 1096 | $valid_data = $this->_validate_payment_request_data(); |
||
| 1097 | if ( ! empty( $valid_data ) ) { |
||
| 1098 | $PAY_ID = $valid_data[ 'PAY_ID' ]; |
||
| 1099 | //save the new payment |
||
| 1100 | $payment = $this->_create_payment_from_request_data( $valid_data ); |
||
| 1101 | // get the TXN for this payment |
||
| 1102 | $transaction = $payment->transaction(); |
||
| 1103 | // verify transaction |
||
| 1104 | if ( $transaction instanceof EE_Transaction ) { |
||
| 1105 | // calculate_total_payments_and_update_status |
||
| 1106 | $this->_process_transaction_payments( $transaction ); |
||
| 1107 | $REG_IDs = $this->_get_REG_IDs_to_apply_payment_to( $payment ); |
||
| 1108 | $this->_remove_existing_registration_payments( $payment, $PAY_ID ); |
||
| 1109 | // apply payment to registrations (if applicable) |
||
| 1110 | if ( ! empty( $REG_IDs ) ) { |
||
| 1111 | $this->_update_registration_payments( $transaction, $payment, $REG_IDs ); |
||
| 1112 | $this->_maybe_send_notifications(); |
||
| 1113 | // now process status changes for the same registrations |
||
| 1114 | $this->_process_registration_status_change( $transaction, $REG_IDs ); |
||
| 1115 | } |
||
| 1116 | $this->_maybe_send_notifications( $payment ); |
||
| 1117 | //prepare to render page |
||
| 1118 | $json_response_data[ 'return_data' ] = $this->_build_payment_json_response( $payment, $REG_IDs ); |
||
| 1119 | do_action( 'AHEE__Transactions_Admin_Page__apply_payments_or_refund__after_recording', $transaction, $payment ); |
||
| 1120 | } else { |
||
| 1121 | EE_Error::add_error( |
||
| 1122 | __( 'A valid Transaction for this payment could not be retrieved.', 'event_espresso' ), |
||
| 1123 | __FILE__, __FUNCTION__, __LINE__ |
||
| 1124 | ); |
||
| 1125 | } |
||
| 1126 | } else { |
||
| 1127 | EE_Error::add_error( __( 'The payment form data could not be processed. Please try again.', 'event_espresso' ), __FILE__, __FUNCTION__, __LINE__ ); |
||
| 1128 | } |
||
| 1129 | |||
| 1130 | $notices = EE_Error::get_notices( false, false, false ); |
||
| 1131 | $this->_template_args = array( |
||
| 1132 | 'data' => $json_response_data, |
||
| 1133 | 'error' => $notices['errors'], |
||
| 1134 | 'success' => $notices['success'] |
||
| 1135 | ); |
||
| 1136 | $this->_return_json(); |
||
| 1137 | } |
||
| 1138 | |||
| 1139 | |||
| 1140 | |||
| 1141 | /** |
||
| 1142 | * _validate_payment_request_data |
||
| 1143 | * |
||
| 1144 | * @return array |
||
| 1145 | */ |
||
| 1146 | protected function _validate_payment_request_data() { |
||
| 1175 | |||
| 1176 | |||
| 1177 | |||
| 1178 | /** |
||
| 1179 | * _generate_payment_form_section |
||
| 1180 | * |
||
| 1181 | * @return EE_Form_Section_Proper |
||
| 1182 | */ |
||
| 1183 | protected function _generate_payment_form_section() { |
||
| 1276 | |||
| 1277 | |||
| 1278 | |||
| 1279 | /** |
||
| 1280 | * _create_payment_from_request_data |
||
| 1281 | * |
||
| 1282 | * @param array $valid_data |
||
| 1283 | * @return EE_Payment |
||
| 1284 | */ |
||
| 1285 | protected function _create_payment_from_request_data( $valid_data ) { |
||
| 1286 | $PAY_ID = $valid_data[ 'PAY_ID' ]; |
||
| 1287 | // get payment amount |
||
| 1288 | $amount = $valid_data[ 'amount' ] ? abs( $valid_data[ 'amount' ] ) : 0; |
||
| 1289 | // payments have a type value of 1 and refunds have a type value of -1 |
||
| 1290 | // so multiplying amount by type will give a positive value for payments, and negative values for refunds |
||
| 1291 | $amount = $valid_data[ 'type' ] < 0 ? $amount * -1 : $amount; |
||
| 1292 | // for some reason the date string coming in has extra spaces between the date and time. This fixes that. |
||
| 1293 | $date = $valid_data['date'] ? preg_replace( '/\s+/', ' ', $valid_data['date'] ) : date( 'Y-m-d g:i a', current_time( 'timestamp' ) ); |
||
| 1294 | $payment = EE_Payment::new_instance( |
||
| 1295 | array( |
||
| 1296 | 'TXN_ID' => $valid_data[ 'TXN_ID' ], |
||
| 1297 | 'STS_ID' => $valid_data[ 'status' ], |
||
| 1298 | 'PAY_timestamp' => $date, |
||
| 1299 | 'PAY_source' => EEM_Payment_Method::scope_admin, |
||
| 1300 | 'PMD_ID' => $valid_data[ 'PMD_ID' ], |
||
| 1301 | 'PAY_amount' => $amount, |
||
| 1302 | 'PAY_txn_id_chq_nmbr' => $valid_data[ 'txn_id_chq_nmbr' ], |
||
| 1303 | 'PAY_po_number' => $valid_data[ 'po_number' ], |
||
| 1304 | 'PAY_extra_accntng' => $valid_data[ 'accounting' ], |
||
| 1305 | 'PAY_details' => $valid_data, |
||
| 1306 | 'PAY_ID' => $PAY_ID |
||
| 1307 | ), |
||
| 1308 | '', |
||
| 1309 | array( 'Y-m-d', 'g:i a' ) |
||
| 1310 | ); |
||
| 1311 | |||
| 1312 | if ( ! $payment->save() ) { |
||
| 1313 | EE_Error::add_error( |
||
| 1314 | sprintf( |
||
| 1315 | __( 'Payment %1$d has not been successfully saved to the database.', 'event_espresso' ), |
||
| 1316 | $payment->ID() |
||
| 1317 | ), |
||
| 1318 | __FILE__, __FUNCTION__, __LINE__ |
||
| 1319 | ); |
||
| 1320 | } |
||
| 1321 | return $payment; |
||
| 1322 | } |
||
| 1323 | |||
| 1324 | |||
| 1325 | |||
| 1326 | /** |
||
| 1327 | * _process_transaction_payments |
||
| 1328 | * |
||
| 1329 | * @param \EE_Transaction $transaction |
||
| 1330 | * @return array |
||
| 1331 | */ |
||
| 1332 | protected function _process_transaction_payments( EE_Transaction $transaction ) { |
||
| 1345 | |||
| 1346 | |||
| 1347 | |||
| 1348 | /** |
||
| 1349 | * _get_REG_IDs_to_apply_payment_to |
||
| 1350 | * |
||
| 1351 | * returns a list of registration IDs that the payment will apply to |
||
| 1352 | * |
||
| 1353 | * @param \EE_Payment $payment |
||
| 1354 | * @return array |
||
| 1355 | */ |
||
| 1356 | protected function _get_REG_IDs_to_apply_payment_to( EE_Payment $payment ) { |
||
| 1370 | |||
| 1371 | |||
| 1372 | |||
| 1373 | /** |
||
| 1374 | * @return array |
||
| 1375 | */ |
||
| 1376 | public function existing_reg_payment_REG_IDs() { |
||
| 1379 | |||
| 1380 | |||
| 1381 | |||
| 1382 | /** |
||
| 1383 | * @param array $existing_reg_payment_REG_IDs |
||
| 1384 | */ |
||
| 1385 | public function set_existing_reg_payment_REG_IDs( $existing_reg_payment_REG_IDs = null ) { |
||
| 1388 | |||
| 1389 | |||
| 1390 | |||
| 1391 | /** |
||
| 1392 | * _get_existing_reg_payment_REG_IDs |
||
| 1393 | * |
||
| 1394 | * returns a list of registration IDs that the payment is currently related to |
||
| 1395 | * as recorded in the database |
||
| 1396 | * |
||
| 1397 | * @param \EE_Payment $payment |
||
| 1398 | * @return array |
||
| 1399 | */ |
||
| 1400 | protected function _get_existing_reg_payment_REG_IDs( EE_Payment $payment ) { |
||
| 1410 | |||
| 1411 | |||
| 1412 | |||
| 1413 | /** |
||
| 1414 | * _remove_existing_registration_payments |
||
| 1415 | * |
||
| 1416 | * this calculates the difference between existing relations |
||
| 1417 | * to the supplied payment and the new list registration IDs, |
||
| 1418 | * removes any related registrations that no longer apply, |
||
| 1419 | * and then updates the registration paid fields |
||
| 1420 | * |
||
| 1421 | * @param \EE_Payment $payment |
||
| 1422 | * @param int $PAY_ID |
||
| 1423 | * @return bool; |
||
| 1424 | */ |
||
| 1425 | protected function _remove_existing_registration_payments( EE_Payment $payment, $PAY_ID = 0 ) { |
||
| 1446 | |||
| 1447 | |||
| 1448 | |||
| 1449 | /** |
||
| 1450 | * _update_registration_payments |
||
| 1451 | * |
||
| 1452 | * this applies the payments to the selected registrations |
||
| 1453 | * but only if they have not already been paid for |
||
| 1454 | * |
||
| 1455 | * @param EE_Transaction $transaction |
||
| 1456 | * @param \EE_Payment $payment |
||
| 1457 | * @param array $REG_IDs |
||
| 1458 | * @return bool |
||
| 1459 | */ |
||
| 1460 | protected function _update_registration_payments( EE_Transaction $transaction, EE_Payment $payment, $REG_IDs = array() ) { |
||
| 1481 | |||
| 1482 | |||
| 1483 | |||
| 1484 | /** |
||
| 1485 | * _process_registration_status_change |
||
| 1486 | * |
||
| 1487 | * This processes requested registration status changes for all the registrations |
||
| 1488 | * on a given transaction and (optionally) sends out notifications for the changes. |
||
| 1489 | * |
||
| 1490 | * @param EE_Transaction $transaction |
||
| 1491 | * @param array $REG_IDs |
||
| 1492 | * @return bool |
||
| 1493 | */ |
||
| 1494 | protected function _process_registration_status_change( EE_Transaction $transaction, $REG_IDs = array() ) { |
||
| 1512 | |||
| 1513 | |||
| 1514 | |||
| 1515 | /** |
||
| 1516 | * _build_payment_json_response |
||
| 1517 | * |
||
| 1518 | * @access public |
||
| 1519 | * @param \EE_Payment $payment |
||
| 1520 | * @param array $REG_IDs |
||
| 1521 | * @param bool | null $delete_txn_reg_status_change |
||
| 1522 | * @return array |
||
| 1523 | */ |
||
| 1524 | protected function _build_payment_json_response( EE_Payment $payment, $REG_IDs = array(), $delete_txn_reg_status_change = null ) { |
||
| 1558 | |||
| 1559 | |||
| 1560 | |||
| 1561 | /** |
||
| 1562 | * delete_payment |
||
| 1563 | * delete a payment or refund made towards a transaction |
||
| 1564 | * |
||
| 1565 | * @access public |
||
| 1566 | * @return void |
||
| 1567 | */ |
||
| 1568 | public function delete_payment() { |
||
| 1569 | $json_response_data = array( 'return_data' => FALSE ); |
||
| 1570 | $PAY_ID = isset( $this->_req_data['delete_txn_admin_payment'], $this->_req_data['delete_txn_admin_payment']['PAY_ID'] ) ? absint( $this->_req_data['delete_txn_admin_payment']['PAY_ID'] ) : 0; |
||
| 1571 | if ( $PAY_ID ) { |
||
| 1572 | $delete_txn_reg_status_change = isset( $this->_req_data[ 'delete_txn_reg_status_change' ] ) ? $this->_req_data[ 'delete_txn_reg_status_change' ] : false; |
||
| 1573 | $payment = EEM_Payment::instance()->get_one_by_ID( $PAY_ID ); |
||
| 1574 | if ( $payment instanceof EE_Payment ) { |
||
| 1575 | $REG_IDs = $this->_get_existing_reg_payment_REG_IDs( $payment ); |
||
| 1576 | /** @type EE_Transaction_Payments $transaction_payments */ |
||
| 1577 | $transaction_payments = EE_Registry::instance()->load_class( 'Transaction_Payments' ); |
||
| 1578 | if ( $transaction_payments->delete_payment_and_update_transaction( $payment )) { |
||
| 1579 | $json_response_data['return_data'] = $this->_build_payment_json_response( $payment, $REG_IDs, $delete_txn_reg_status_change ); |
||
| 1580 | if ( $delete_txn_reg_status_change ) { |
||
| 1581 | $this->_req_data['txn_reg_status_change'] = $delete_txn_reg_status_change; |
||
| 1582 | //MAKE sure we also add the delete_txn_req_status_change to the |
||
| 1583 | //$_REQUEST global because that's how messages will be looking for it. |
||
| 1584 | $_REQUEST['txn_reg_status_change'] = $delete_txn_reg_status_change; |
||
| 1585 | $this->_maybe_send_notifications(); |
||
| 1586 | $this->_process_registration_status_change( $payment->transaction(), $REG_IDs ); |
||
| 1587 | } |
||
| 1588 | } |
||
| 1589 | } else { |
||
| 1590 | EE_Error::add_error( |
||
| 1591 | __( 'Valid Payment data could not be retrieved from the database.', 'event_espresso' ), |
||
| 1592 | __FILE__, __FUNCTION__, __LINE__ |
||
| 1593 | ); |
||
| 1594 | } |
||
| 1595 | } else { |
||
| 1596 | EE_Error::add_error( |
||
| 1597 | __( 'A valid Payment ID was not received, therefore payment form data could not be loaded.', 'event_espresso' ), |
||
| 1598 | __FILE__, __FUNCTION__, __LINE__ |
||
| 1599 | ); |
||
| 1600 | } |
||
| 1601 | $notices = EE_Error::get_notices( false, false, false); |
||
| 1602 | $this->_template_args = array( |
||
| 1603 | 'data' => $json_response_data, |
||
| 1604 | 'success' => $notices['success'], |
||
| 1605 | 'error' => $notices['errors'], |
||
| 1606 | 'attention' => $notices['attention'] |
||
| 1607 | ); |
||
| 1608 | $this->_return_json(); |
||
| 1609 | } |
||
| 1610 | |||
| 1611 | |||
| 1612 | |||
| 1613 | /** |
||
| 1614 | * _registration_payment_data_array |
||
| 1615 | * adds info for 'owing' and 'paid' for each registration to the json response |
||
| 1616 | * |
||
| 1617 | * @access protected |
||
| 1618 | * @param array $REG_IDs |
||
| 1619 | * @return array |
||
| 1620 | */ |
||
| 1621 | protected function _registration_payment_data_array( $REG_IDs ) { |
||
| 1638 | |||
| 1639 | |||
| 1640 | |||
| 1641 | /** |
||
| 1642 | * _maybe_send_notifications |
||
| 1643 | * |
||
| 1644 | * determines whether or not the admin has indicated that notifications should be sent. |
||
| 1645 | * If so, will toggle a filter switch for delivering registration notices. |
||
| 1646 | * If passed an EE_Payment object, then it will trigger payment notifications instead. |
||
| 1647 | * |
||
| 1648 | * @access protected |
||
| 1649 | * @param \EE_Payment | null $payment |
||
| 1650 | */ |
||
| 1651 | protected function _maybe_send_notifications( $payment = null ) { |
||
| 1679 | |||
| 1680 | |||
| 1681 | |||
| 1682 | /** |
||
| 1683 | * _send_payment_reminder |
||
| 1684 | * generates HTML for the View Transaction Details Admin page |
||
| 1685 | * |
||
| 1686 | * @access protected |
||
| 1687 | * @return void |
||
| 1688 | */ |
||
| 1689 | protected function _send_payment_reminder() { |
||
| 1696 | |||
| 1697 | |||
| 1698 | |||
| 1699 | /** |
||
| 1700 | * get_transactions |
||
| 1701 | * get transactions for given parameters (used by list table) |
||
| 1702 | * |
||
| 1703 | * @param int $perpage how many transactions displayed per page |
||
| 1704 | * @param boolean $count return the count or objects |
||
| 1705 | * @param string $view |
||
| 1706 | * @return mixed int = count || array of transaction objects |
||
| 1707 | */ |
||
| 1708 | public function get_transactions( $perpage, $count = FALSE, $view = '' ) { |
||
| 1816 | |||
| 1817 | |||
| 1818 | |||
| 1819 | } |
||
| 1820 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.