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_Import_Donations 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_Import_Donations, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | final class Give_Import_Donations { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Importer type |
||
| 29 | * |
||
| 30 | * @since 1.8.13 |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | private $importer_type = 'import_donations'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Instance. |
||
| 37 | * |
||
| 38 | * @since |
||
| 39 | * @access private |
||
| 40 | * @var |
||
| 41 | */ |
||
| 42 | static private $instance; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Importing donation per page. |
||
| 46 | * |
||
| 47 | * @since 1.8.14 |
||
| 48 | * |
||
| 49 | * @var int |
||
| 50 | */ |
||
| 51 | public static $per_page = 5; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Singleton pattern. |
||
| 55 | * |
||
| 56 | * @since |
||
| 57 | * @access private |
||
| 58 | */ |
||
| 59 | private function __construct() { |
||
| 61 | |||
| 62 | |||
| 63 | /** |
||
| 64 | * Get instance. |
||
| 65 | * |
||
| 66 | * @since |
||
| 67 | * @access public |
||
| 68 | * |
||
| 69 | * @return static |
||
| 70 | */ |
||
| 71 | public static function get_instance() { |
||
| 72 | if ( null === static::$instance ) { |
||
| 73 | self::$instance = new static(); |
||
| 74 | } |
||
| 75 | |||
| 76 | return self::$instance; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Setup |
||
| 81 | * |
||
| 82 | * @since 1.8.14 |
||
| 83 | * |
||
| 84 | * @return void |
||
| 85 | */ |
||
| 86 | public function setup() { |
||
| 87 | $this->setup_hooks(); |
||
| 88 | } |
||
| 89 | |||
| 90 | |||
| 91 | /** |
||
| 92 | * Setup Hooks. |
||
| 93 | * |
||
| 94 | * @since 1.8.14 |
||
| 95 | * |
||
| 96 | * @return void |
||
| 97 | */ |
||
| 98 | private function setup_hooks() { |
||
| 99 | if ( ! $this->is_donations_import_page() ) { |
||
| 100 | return; |
||
| 101 | } |
||
| 102 | |||
| 103 | // Do not render main import tools page. |
||
| 104 | remove_action( 'give_admin_field_tools_import', array( 'Give_Settings_Import', 'render_import_field', ) ); |
||
| 105 | |||
|
|
|||
| 106 | |||
| 107 | // Render donation import page |
||
| 108 | add_action( 'give_admin_field_tools_import', array( $this, 'render_page' ) ); |
||
| 109 | |||
| 110 | // Print the HTML. |
||
| 111 | add_action( 'give_tools_import_donations_form_start', array( $this, 'html' ), 10 ); |
||
| 112 | |||
| 113 | // Run when form submit. |
||
| 114 | add_action( 'give-tools_save_import', array( $this, 'save' ) ); |
||
| 115 | |||
| 116 | add_action( 'give-tools_update_notices', array( $this, 'update_notices' ), 11, 1 ); |
||
| 117 | |||
| 118 | // Used to add submit button. |
||
| 119 | add_action( 'give_tools_import_donations_form_end', array( $this, 'submit' ), 10 ); |
||
| 120 | } |
||
| 121 | |||
| 122 | |||
| 123 | /** |
||
| 124 | * Update notice |
||
| 125 | * |
||
| 126 | * @since 1.8.14 |
||
| 127 | * |
||
| 128 | * @param $messages |
||
| 129 | * |
||
| 130 | * @return mixed |
||
| 131 | */ |
||
| 132 | public function update_notices( $messages ) { |
||
| 133 | if ( ! empty( $_GET['tab'] ) && 'import' === give_clean( $_GET['tab'] ) ) { |
||
| 134 | unset( $messages['give-setting-updated'] ); |
||
| 135 | } |
||
| 136 | |||
| 137 | return $messages; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Print submit and nonce button. |
||
| 142 | * |
||
| 143 | * @since 1.8.14 |
||
| 144 | */ |
||
| 145 | public function submit() { |
||
| 146 | wp_nonce_field( 'give-save-settings', '_give-save-settings' ); |
||
| 147 | ?> |
||
| 148 | <input type="hidden" class="import-step" id="import-step" name="step" value="<?php echo $this->get_step(); ?>"/> |
||
| 149 | <input type="hidden" class="importer-type" value="<?php echo $this->importer_type; ?>"/> |
||
| 150 | <?php |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Print the HTML for importer. |
||
| 155 | * |
||
| 156 | * @since 1.8.14 |
||
| 157 | */ |
||
| 158 | public function html() { |
||
| 159 | $step = $this->get_step(); |
||
| 160 | |||
| 161 | // Show progress. |
||
| 162 | $this->render_progress(); |
||
| 163 | ?> |
||
| 164 | <section> |
||
| 165 | <table class="widefat export-options-table give-table <?php echo "step-{$step}"; ?>" id="<?php echo "step-{$step}"; ?>"> |
||
| 166 | <tbody> |
||
| 167 | <?php |
||
| 168 | switch ( $this->get_step() ) { |
||
| 169 | case 1: |
||
| 170 | $this->render_media_csv(); |
||
| 171 | break; |
||
| 172 | |||
| 173 | case 2: |
||
| 174 | $this->render_dropdown(); |
||
| 175 | break; |
||
| 176 | |||
| 177 | case 3: |
||
| 178 | $this->start_import(); |
||
| 179 | break; |
||
| 180 | |||
| 181 | case 4: |
||
| 182 | $this->import_success(); |
||
| 183 | } |
||
| 184 | |||
| 185 | if ( false === $this->check_for_dropdown_or_import() ) { |
||
| 186 | ?> |
||
| 187 | <tr valign="top"> |
||
| 188 | <th></th> |
||
| 189 | <th> |
||
| 190 | <input type="submit" |
||
| 191 | class="button button-primary button-large button-secondary <?php echo "step-{$step}"; ?>" |
||
| 192 | id="recount-stats-submit" |
||
| 193 | value="<?php esc_attr_e( 'Submit', 'give' ); ?>"/> |
||
| 194 | </th> |
||
| 195 | </tr> |
||
| 196 | <?php |
||
| 197 | } |
||
| 198 | ?> |
||
| 199 | </tbody> |
||
| 200 | </table> |
||
| 201 | </section> |
||
| 202 | <?php |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Show success notice |
||
| 207 | * |
||
| 208 | * @since 1.8.14 |
||
| 209 | */ |
||
| 210 | public function import_success() { |
||
| 211 | |||
| 212 | $delete_csv = ( ! empty( $_GET['delete_csv'] ) ? absint( $_GET['delete_csv'] ) : false ); |
||
| 213 | $csv = ( ! empty( $_GET['csv'] ) ? absint( $_GET['csv'] ) : false ); |
||
| 214 | if ( ! empty( $delete_csv ) && ! empty( $csv ) ) { |
||
| 215 | wp_delete_attachment( $csv, true ); |
||
| 216 | } |
||
| 217 | |||
| 218 | $report = give_import_donation_report(); |
||
| 219 | $report_html = array( |
||
| 220 | 'duplicate_donor' => array( |
||
| 221 | __( '%s duplicate %s detected', 'give' ), |
||
| 222 | __( 'donor', 'give' ), |
||
| 223 | __( 'donors', 'give' ), |
||
| 224 | ), |
||
| 225 | 'create_donor' => array( |
||
| 226 | __( '%s %s created', 'give' ), |
||
| 227 | __( 'donor', 'give' ), |
||
| 228 | __( 'donors', 'give' ), |
||
| 229 | ), |
||
| 230 | 'create_form' => array( |
||
| 231 | __( '%s donation %s created', 'give' ), |
||
| 232 | __( 'form', 'give' ), |
||
| 233 | __( 'forms', 'give' ), |
||
| 234 | ), |
||
| 235 | 'duplicate_donation' => array( |
||
| 236 | __( '%s duplicate %s detected', 'give' ), |
||
| 237 | __( 'donation', 'give' ), |
||
| 238 | __( 'donations', 'give' ), |
||
| 239 | ), |
||
| 240 | 'create_donation' => array( |
||
| 241 | __( '%s %s imported', 'give' ), |
||
| 242 | __( 'donation', 'give' ), |
||
| 243 | __( 'donations', 'give' ), |
||
| 244 | ), |
||
| 245 | ); |
||
| 246 | $total = (int) $_GET['total']; |
||
| 247 | -- $total; |
||
| 248 | $success = (bool) $_GET['success']; |
||
| 249 | ?> |
||
| 250 | <tr valign="top" class="give-import-dropdown"> |
||
| 251 | <th colspan="2"> |
||
| 252 | <h2> |
||
| 253 | <?php |
||
| 254 | if ( $success ) { |
||
| 255 | echo sprintf( |
||
| 256 | __( 'Import complete! %s donations processed', 'give' ), |
||
| 257 | "<strong>{$total}</strong>" |
||
| 258 | ); |
||
| 259 | } else { |
||
| 260 | echo sprintf( |
||
| 261 | __( 'Failed to import %s donations', 'give' ), |
||
| 262 | "<strong>{$total}</strong>" |
||
| 263 | ); |
||
| 264 | } |
||
| 265 | ?> |
||
| 266 | </h2> |
||
| 267 | |||
| 268 | <?php |
||
| 269 | $text = __( 'Import Donation', 'give' ); |
||
| 270 | $query_arg = array( |
||
| 271 | 'post_type' => 'give_forms', |
||
| 272 | 'page' => 'give-tools', |
||
| 273 | 'tab' => 'import', |
||
| 274 | ); |
||
| 275 | if ( $success ) { |
||
| 276 | $query_arg = array( |
||
| 277 | 'post_type' => 'give_forms', |
||
| 278 | 'page' => 'give-payment-history', |
||
| 279 | ); |
||
| 280 | $text = __( 'View Donations', 'give' ); |
||
| 281 | } |
||
| 282 | |||
| 283 | foreach ( $report as $key => $value ) { |
||
| 284 | if ( array_key_exists( $key, $report_html ) && ! empty( $value ) ) { |
||
| 285 | ?> |
||
| 286 | <p> |
||
| 287 | <?php echo esc_html( wp_sprintf( $report_html[ $key ][0], $value, _n( $report_html[ $key ][1], $report_html[ $key ][2], $value, 'give' ) ) ); ?> |
||
| 288 | </p> |
||
| 289 | <?php |
||
| 290 | } |
||
| 291 | } |
||
| 292 | ?> |
||
| 293 | |||
| 294 | <p> |
||
| 295 | <a class="button button-large button-secondary" href="<?php echo add_query_arg( $query_arg, admin_url( 'edit.php' ) ); ?>"><?php echo $text; ?></a> |
||
| 296 | </p> |
||
| 297 | </th> |
||
| 298 | </tr> |
||
| 299 | <?php |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Will start Import |
||
| 304 | * |
||
| 305 | * @since 1.8.14 |
||
| 306 | */ |
||
| 307 | public function start_import() { |
||
| 308 | // Reset the donation form report. |
||
| 309 | give_import_donation_report_reset(); |
||
| 310 | |||
| 311 | $csv = (int) $_REQUEST['csv']; |
||
| 312 | $index_start = 1; |
||
| 313 | $index_end = 1; |
||
| 314 | $next = true; |
||
| 315 | $total = self::get_csv_total( $csv ); |
||
| 316 | if ( self::$per_page < $total ) { |
||
| 317 | $total_ajax = ceil( $total / self::$per_page ); |
||
| 318 | $index_end = self::$per_page; |
||
| 319 | } else { |
||
| 320 | $total_ajax = 1; |
||
| 321 | $index_end = $total; |
||
| 322 | $next = false; |
||
| 323 | } |
||
| 324 | $current_percentage = 100 / ( $total_ajax + 1 ); |
||
| 325 | |||
| 326 | ?> |
||
| 327 | <tr valign="top" class="give-import-dropdown"> |
||
| 328 | <th colspan="2"> |
||
| 329 | <h2 id="give-import-title"><?php esc_html_e( 'Importing', 'give' ) ?></h2> |
||
| 330 | <p class="give-field-description"><?php esc_html_e( 'Your donations are now being imported...', 'give' ) ?></p> |
||
| 331 | </th> |
||
| 332 | </tr> |
||
| 333 | |||
| 334 | <tr valign="top" class="give-import-dropdown"> |
||
| 335 | <th colspan="2"> |
||
| 336 | <span class="spinner is-active"></span> |
||
| 337 | <div class="give-progress" |
||
| 338 | data-current="1" |
||
| 339 | data-total_ajax="<?php echo $total_ajax; ?>" |
||
| 340 | data-start="<?php echo $index_start; ?>" |
||
| 341 | data-end="<?php echo $index_end; ?>" |
||
| 342 | data-next="<?php echo $next; ?>" |
||
| 343 | data-total="<?php echo $total; ?>" |
||
| 344 | data-per_page="<?php echo self::$per_page; ?>"> |
||
| 345 | |||
| 346 | <div style="width: <?php echo $current_percentage; ?>%"></div> |
||
| 347 | </div> |
||
| 348 | <input type="hidden" value="3" name="step"> |
||
| 349 | <input type="hidden" value='<?php echo maybe_serialize( $_REQUEST['mapto'] ); ?>' name="mapto" |
||
| 350 | class="mapto"> |
||
| 351 | <input type="hidden" value="<?php echo $_REQUEST['csv']; ?>" name="csv" class="csv"> |
||
| 352 | <input type="hidden" value="<?php echo $_REQUEST['mode']; ?>" name="mode" class="mode"> |
||
| 353 | <input type="hidden" value="<?php echo $_REQUEST['create_user']; ?>" name="create_user" |
||
| 354 | class="create_user"> |
||
| 355 | <input type="hidden" value="<?php echo $_REQUEST['delete_csv']; ?>" name="delete_csv" |
||
| 356 | class="delete_csv"> |
||
| 357 | <input type="hidden" value="<?php echo $_REQUEST['delimiter']; ?>" name="delimiter"> |
||
| 358 | <input type="hidden" value='<?php echo maybe_serialize( self::get_importer( $csv ) ); ?>' |
||
| 359 | name="main_key" |
||
| 360 | class="main_key"> |
||
| 361 | </th> |
||
| 362 | </tr> |
||
| 363 | |||
| 364 | <script type="text/javascript"> |
||
| 365 | jQuery(document).ready(function () { |
||
| 366 | give_on_donation_import_start(); |
||
| 367 | }); |
||
| 368 | </script> |
||
| 369 | <?php |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Will return true if importing can be started or not else false. |
||
| 374 | * |
||
| 375 | * @since 1.8.14 |
||
| 376 | */ |
||
| 377 | public function check_for_dropdown_or_import() { |
||
| 378 | $return = true; |
||
| 379 | if ( isset( $_REQUEST['mapto'] ) ) { |
||
| 380 | $mapto = (array) $_REQUEST['mapto']; |
||
| 381 | if ( false === in_array( 'form_title', $mapto ) && false === in_array( 'form_id', $mapto ) ) { |
||
| 382 | Give_Admin_Settings::add_error( 'give-import-csv-form', __( 'Please select Form ID or Form Name options from the dropdown.', 'give' ) ); |
||
| 383 | $return = false; |
||
| 384 | } |
||
| 385 | |||
| 386 | if ( false === in_array( 'amount', $mapto ) ) { |
||
| 387 | Give_Admin_Settings::add_error( 'give-import-csv-amount', __( 'Please select Amount option from the dropdown.', 'give' ) ); |
||
| 388 | $return = false; |
||
| 389 | } |
||
| 390 | |||
| 391 | if ( false === in_array( 'email', $mapto ) && false === in_array( 'donor_id', $mapto ) ) { |
||
| 392 | Give_Admin_Settings::add_error( 'give-import-csv-donor', __( 'Please select Email id or Customer ID options from the dropdown.', 'give' ) ); |
||
| 393 | $return = false; |
||
| 394 | } |
||
| 395 | } else { |
||
| 396 | $return = false; |
||
| 397 | } |
||
| 398 | |||
| 399 | return $return; |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Print the Dropdown option for CSV. |
||
| 404 | * |
||
| 405 | * @since 1.8.14 |
||
| 406 | */ |
||
| 407 | public function render_dropdown() { |
||
| 408 | $csv = (int) $_GET['csv']; |
||
| 409 | |||
| 410 | // TO check if the CSV files that is being add is valid or not if not then redirect to first step again |
||
| 411 | if ( ! $this->is_valid_csv( $csv ) ) { |
||
| 412 | $url = give_import_page_url(); |
||
| 413 | ?> |
||
| 414 | <script type="text/javascript"> |
||
| 415 | window.location = "<?php echo $url; ?>" |
||
| 416 | </script> |
||
| 417 | <?php |
||
| 418 | } else { |
||
| 419 | ?> |
||
| 420 | <tr valign="top" class="give-import-dropdown"> |
||
| 421 | <th colspan="2"> |
||
| 422 | <h2 id="give-import-title"><?php esc_html_e( 'Map CSV fields to donations', 'give' ) ?></h2> |
||
| 423 | <p class="give-field-description"><?php esc_html_e( 'Select fields from your CSV file to map against donations fields or to ignore during import.', 'give' ) ?></p> |
||
| 424 | </th> |
||
| 425 | </tr> |
||
| 426 | |||
| 427 | <tr valign="top" class="give-import-dropdown"> |
||
| 428 | <th><b><?php esc_html_e( 'Column name', 'give' ); ?></b></th> |
||
| 429 | <th><b><?php esc_html_e( 'Map to field', 'give' ); ?></b></th> |
||
| 430 | </tr> |
||
| 431 | |||
| 432 | <?php |
||
| 433 | $raw_key = $this->get_importer( $csv ); |
||
| 434 | $donations = give_import_donations_options(); |
||
| 435 | $donors = give_import_donor_options(); |
||
| 436 | $forms = give_import_donation_form_options(); |
||
| 437 | |||
| 438 | foreach ( $raw_key as $index => $value ) { |
||
| 439 | ?> |
||
| 440 | <tr valign="top" class="give-import-option"> |
||
| 441 | <th><?php echo $value; ?></th> |
||
| 442 | <th> |
||
| 443 | <?php |
||
| 444 | $this->get_columns( $index, $donations, $donors, $forms, $value ); |
||
| 445 | ?> |
||
| 446 | </th> |
||
| 447 | </tr> |
||
| 448 | <?php |
||
| 449 | } |
||
| 450 | } |
||
| 451 | } |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @param $option_value |
||
| 455 | * @param $value |
||
| 456 | * |
||
| 457 | * @return string |
||
| 458 | */ |
||
| 459 | public function selected( $option_value, $value ) { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Print the columns from the CSV. |
||
| 472 | * |
||
| 473 | * @since 1.8.14 |
||
| 474 | */ |
||
| 475 | public function get_columns( $index, $donations, $donors, $forms, $value = false ) { |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Get column count of csv file. |
||
| 529 | * |
||
| 530 | * @since 1.8.14 |
||
| 531 | * |
||
| 532 | * @param $file_id |
||
| 533 | * |
||
| 534 | * @return bool|int |
||
| 535 | */ |
||
| 536 | public function get_csv_total( $file_id ) { |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Get the CSV fields title from the CSV. |
||
| 552 | * |
||
| 553 | * @since 1.8.14 |
||
| 554 | * |
||
| 555 | * @param (int) $file_id |
||
| 556 | * @param int $index |
||
| 557 | * @param string $delimiter |
||
| 558 | * |
||
| 559 | * @return array|bool $raw_data title of the CSV file fields |
||
| 560 | */ |
||
| 561 | public function get_importer( $file_id, $index = 0, $delimiter = ',' ) { |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Remove UTF-8 BOM signature. |
||
| 579 | * |
||
| 580 | * @since 1.8.14 |
||
| 581 | * |
||
| 582 | * @param string $string String to handle. |
||
| 583 | * |
||
| 584 | * @return string |
||
| 585 | */ |
||
| 586 | public function remove_utf8_bom( $string ) { |
||
| 593 | |||
| 594 | |||
| 595 | /** |
||
| 596 | * Is used to show the process when user upload the donor form. |
||
| 597 | * |
||
| 598 | * @since 1.8.14 |
||
| 599 | */ |
||
| 600 | public function render_progress() { |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Will return the import step. |
||
| 622 | * |
||
| 623 | * @since 1.8.14 |
||
| 624 | * |
||
| 625 | * @return int $step on which step doest the import is on. |
||
| 626 | */ |
||
| 627 | public function get_step() { |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Render donations import page |
||
| 646 | * |
||
| 647 | * @since 1.8.14 |
||
| 648 | */ |
||
| 649 | public function render_page() { |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Add CSV upload HTMl |
||
| 655 | * |
||
| 656 | * Print the html of the file upload from which CSV will be uploaded. |
||
| 657 | * |
||
| 658 | * @since 1.8.14 |
||
| 659 | * @return void |
||
| 660 | */ |
||
| 661 | public function render_media_csv() { |
||
| 662 | ?> |
||
| 732 | |||
| 733 | /** |
||
| 734 | * Run when user click on the submit button. |
||
| 735 | * |
||
| 736 | * @since 1.8.14 |
||
| 737 | */ |
||
| 738 | public function save() { |
||
| 765 | |||
| 766 | /** |
||
| 767 | * Check if user uploaded csv is valid or not. |
||
| 768 | * |
||
| 769 | * @since 1.8.14 |
||
| 770 | * @access public |
||
| 771 | * |
||
| 772 | * @param int|bool $csv ID of the CSV files. |
||
| 773 | * @param string $match_url ID of the CSV files. |
||
| 774 | * |
||
| 775 | * @return bool $has_error CSV is valid or not. |
||
| 776 | */ |
||
| 777 | private function is_valid_csv( $csv = false, $match_url = '' ) { |
||
| 798 | |||
| 799 | |||
| 800 | /** |
||
| 801 | * Render report import field |
||
| 802 | * |
||
| 803 | * @since 1.8.14 |
||
| 804 | * @access public |
||
| 805 | * |
||
| 806 | * @param $field |
||
| 807 | * @param $option_value |
||
| 808 | */ |
||
| 809 | public function render_import_field( $field, $option_value ) { |
||
| 812 | |||
| 813 | /** |
||
| 814 | * Get if current page import donations page or not |
||
| 815 | * |
||
| 816 | * @since 1.8.14 |
||
| 817 | * @return bool |
||
| 818 | */ |
||
| 819 | private function is_donations_import_page() { |
||
| 824 | } |
||
| 825 | |||
| 828 |