← All changes
|
includes/admin/tools/import/class-give-import-donations.php
+172
-135
2.3.0
→
4.17.0
View file →
| @@ -5,9 +5,9 @@ | ||
| 5 | 5 | * This class handles donations import. |
| 6 | 6 | * |
| 7 | 7 | * @package Give |
| 8 | 8 | * @subpackage Classes/Give_Import_Donations |
| 9 | - * @copyright Copyright (c) 2017, WordImpress | |
| 9 | + * @copyright Copyright (c) 2017, GiveWP | |
| 10 | 10 | * @license https://opensource.org/licenses/gpl-license GNU Public License |
| 11 | 11 | * @since 1.8.14 |
| 12 | 12 | */ |
| 13 | 13 | |
| @@ -38,9 +38,9 @@ | ||
| 38 | 38 | * @since |
| 39 | 39 | * @access private |
| 40 | 40 | * @var |
| 41 | 41 | */ |
| 42 | - static private $instance; | |
| 42 | + private static $instance; | |
| 43 | 43 | |
| 44 | 44 | /** |
| 45 | 45 | * Importing donation per page. |
| 46 | 46 | * |
| @@ -109,11 +109,10 @@ | ||
| 109 | 109 | return; |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | // Do not render main import tools page. |
| 113 | - remove_action( 'give_admin_field_tools_import', array( 'Give_Settings_Import', 'render_import_field', ) ); | |
| 113 | + remove_action( 'give_admin_field_tools_import', array( 'Give_Settings_Import', 'render_import_field' ) ); | |
| 114 | 114 | |
| 115 | - | |
| 116 | 115 | // Render donation import page |
| 117 | 116 | add_action( 'give_admin_field_tools_import', array( $this, 'render_page' ) ); |
| 118 | 117 | |
| 119 | 118 | // Print the HTML. |
| @@ -153,10 +152,10 @@ | ||
| 153 | 152 | public function submit() { |
| 154 | 153 | wp_nonce_field( 'give-save-settings', '_give-save-settings' ); |
| 155 | 154 | ?> |
| 156 | 155 | <input type="hidden" class="import-step" id="import-step" name="step" |
| 157 | - value="<?php echo $this->get_step(); ?>"/> | |
| 158 | - <input type="hidden" class="importer-type" value="<?php echo $this->importer_type; ?>"/> | |
| 156 | + value="<?php echo esc_attr($this->get_step()); ?>"/> | |
| 157 | + <input type="hidden" class="importer-type" value="<?php echo esc_attr($this->importer_type); ?>"/> | |
| 159 | 158 | <?php |
| 160 | 159 | } |
| 161 | 160 | |
| 162 | 161 | /** |
| @@ -171,10 +170,10 @@ | ||
| 171 | 170 | $this->render_progress(); |
| 172 | 171 | ?> |
| 173 | 172 | <section> |
| 174 | 173 | <table |
| 175 | - class="widefat export-options-table give-table <?php echo "step-{$step}"; ?> <?php echo( 1 === $step && ! empty( $this->is_csv_valid ) ? 'give-hidden' : '' ); ?> " | |
| 176 | - id="<?php echo "step-{$step}"; ?>"> | |
| 174 | + class="widefat export-options-table give-table <?php echo esc_attr("step-{$step}"); ?> <?php echo esc_attr(( 1 === $step && ! empty( $this->is_csv_valid ) ? 'give-hidden' : '' )); ?> " | |
| 175 | + id="<?php echo esc_attr("step-{$step}"); ?>"> | |
| 177 | 176 | <tbody> |
| 178 | 177 | <?php |
| 179 | 178 | switch ( $step ) { |
| 180 | 179 | case 1: |
| @@ -196,19 +195,19 @@ | ||
| 196 | 195 | ?> |
| 197 | 196 | <tr valign="top"> |
| 198 | 197 | <th> |
| 199 | 198 | <input type="submit" |
| 200 | - class="button button-primary button-large button-secondary <?php echo "step-{$step}"; ?>" | |
| 199 | + class="button button-primary button-large button-secondary <?php echo esc_attr("step-{$step}"); ?>" | |
| 201 | 200 | id="recount-stats-submit" |
| 202 | 201 | value=" |
| 203 | - <?php | |
| 204 | - /** | |
| 205 | - * Filter to modify donation importer submit button text. | |
| 206 | - * | |
| 207 | - * @since 2.1 | |
| 208 | - */ | |
| 209 | - echo apply_filters( 'give_import_donation_submit_button_text', __( 'Submit', 'give' ) ); | |
| 210 | - ?> | |
| 202 | + <?php | |
| 203 | + /** | |
| 204 | + * Filter to modify donation importer submit button text. | |
| 205 | + * | |
| 206 | + * @since 2.1 | |
| 207 | + */ | |
| 208 | + echo esc_attr(apply_filters( 'give_import_donation_submit_button_text', __( 'Submit', 'give' ) )); | |
| 209 | + ?> | |
| 211 | 210 | "/> |
| 212 | 211 | </th> |
| 213 | 212 | <th> |
| 214 | 213 | <?php |
| @@ -232,11 +231,13 @@ | ||
| 232 | 231 | |
| 233 | 232 | /** |
| 234 | 233 | * Show success notice |
| 235 | 234 | * |
| 235 | + * @since 2.25.3 Add nonce check. | |
| 236 | 236 | * @since 1.8.14 |
| 237 | 237 | */ |
| 238 | 238 | public function import_success() { |
| 239 | + check_admin_referer('give_donation_import_success'); | |
| 239 | 240 | |
| 240 | 241 | $delete_csv = ( ! empty( $_GET['delete_csv'] ) ? absint( $_GET['delete_csv'] ) : false ); |
| 241 | 242 | $csv = ( ! empty( $_GET['csv'] ) ? absint( $_GET['csv'] ) : false ); |
| 242 | 243 | if ( ! empty( $delete_csv ) && ! empty( $csv ) ) { |
| @@ -246,34 +247,34 @@ | ||
| 246 | 247 | $report = give_import_donation_report(); |
| 247 | 248 | |
| 248 | 249 | $report_html = array( |
| 249 | 250 | 'duplicate_donor' => array( |
| 250 | - __( '%s duplicate %s detected', 'give' ), | |
| 251 | - __( '%s duplicate %s detected', 'give' ), | |
| 251 | + __( '%1$s duplicate %2$s detected', 'give' ), | |
| 252 | + __( '%1$s duplicate %2$s detected', 'give' ), | |
| 252 | 253 | __( 'donor', 'give' ), |
| 253 | 254 | __( 'donors', 'give' ), |
| 254 | 255 | ), |
| 255 | 256 | 'create_donor' => array( |
| 256 | - __( '%s %s created', 'give' ), | |
| 257 | - __( '%s %s will be created', 'give' ), | |
| 257 | + __( '%1$s %2$s created', 'give' ), | |
| 258 | + __( '%1$s %2$s will be created', 'give' ), | |
| 258 | 259 | __( 'donor', 'give' ), |
| 259 | 260 | __( 'donors', 'give' ), |
| 260 | 261 | ), |
| 261 | 262 | 'create_form' => array( |
| 262 | - __( '%s donation %s created', 'give' ), | |
| 263 | - __( '%s donation %s will be created', 'give' ), | |
| 263 | + __( '%1$s donation %2$s created', 'give' ), | |
| 264 | + __( '%1$s donation %2$s will be created', 'give' ), | |
| 264 | 265 | __( 'form', 'give' ), |
| 265 | 266 | __( 'forms', 'give' ), |
| 266 | 267 | ), |
| 267 | 268 | 'duplicate_donation' => array( |
| 268 | - __( '%s duplicate %s detected', 'give' ), | |
| 269 | - __( '%s duplicate %s detected', 'give' ), | |
| 269 | + __( '%1$s duplicate %2$s detected', 'give' ), | |
| 270 | + __( '%1$s duplicate %2$s detected', 'give' ), | |
| 270 | 271 | __( 'donation', 'give' ), |
| 271 | 272 | __( 'donations', 'give' ), |
| 272 | 273 | ), |
| 273 | 274 | 'create_donation' => array( |
| 274 | - __( '%s %s imported', 'give' ), | |
| 275 | - __( '%s %s will be imported', 'give' ), | |
| 275 | + __( '%1$s %2$s imported', 'give' ), | |
| 276 | + __( '%1$s %2$s will be imported', 'give' ), | |
| 276 | 277 | __( 'donation', 'give' ), |
| 277 | 278 | __( 'donations', 'give' ), |
| 278 | 279 | ), |
| 279 | 280 | ); |
| @@ -315,9 +316,8 @@ | ||
| 315 | 316 | 'tab' => 'import', |
| 316 | 317 | ); |
| 317 | 318 | if ( $success ) { |
| 318 | 319 | |
| 319 | - | |
| 320 | 320 | if ( $dry_run ) { |
| 321 | 321 | $query_arg = array( |
| 322 | 322 | 'post_type' => 'give_forms', |
| 323 | 323 | 'page' => 'give-tools', |
| @@ -351,9 +351,9 @@ | ||
| 351 | 351 | ?> |
| 352 | 352 | |
| 353 | 353 | <p> |
| 354 | 354 | <a class="button button-large button-secondary" |
| 355 | - href="<?php echo add_query_arg( $query_arg, admin_url( 'edit.php' ) ); ?>"><?php echo $text; ?></a> | |
| 355 | + href="<?php echo esc_url( add_query_arg( $query_arg, admin_url( 'edit.php' ) ) ); ?>"><?php echo $text; ?></a> | |
| 356 | 356 | </p> |
| 357 | 357 | </th> |
| 358 | 358 | </tr> |
| 359 | 359 | <?php |
| @@ -367,12 +367,11 @@ | ||
| 367 | 367 | public function start_import() { |
| 368 | 368 | // Reset the donation form report. |
| 369 | 369 | give_import_donation_report_reset(); |
| 370 | 370 | |
| 371 | - $csv = (int) $_REQUEST['csv']; | |
| 371 | + $csv = absint( $_REQUEST['csv'] ); | |
| 372 | 372 | $delimiter = ( ! empty( $_REQUEST['delimiter'] ) ? give_clean( $_REQUEST['delimiter'] ) : 'csv' ); |
| 373 | 373 | $index_start = 1; |
| 374 | - $index_end = 1; | |
| 375 | 374 | $next = true; |
| 376 | 375 | $total = self::get_csv_total( $csv ); |
| 377 | 376 | if ( self::$per_page < $total ) { |
| 378 | 377 | $total_ajax = ceil( $total / self::$per_page ); |
| @@ -386,10 +385,10 @@ | ||
| 386 | 385 | |
| 387 | 386 | ?> |
| 388 | 387 | <tr valign="top" class="give-import-dropdown"> |
| 389 | 388 | <th colspan="2"> |
| 390 | - <h2 id="give-import-title"><?php _e( 'Importing', 'give' ) ?></h2> | |
| 391 | - <p class="give-field-description"><?php _e( 'Your donations are now being imported...', 'give' ) ?></p> | |
| 389 | + <h2 id="give-import-title"><?php _e( 'Importing', 'give' ); ?></h2> | |
| 390 | + <p class="give-field-description"><?php _e( 'Your donations are now being imported...', 'give' ); ?></p> | |
| 392 | 391 | </th> |
| 393 | 392 | </tr> |
| 394 | 393 | |
| 395 | 394 | <tr valign="top" class="give-import-dropdown"> |
| @@ -396,32 +395,26 @@ | ||
| 396 | 395 | <th colspan="2"> |
| 397 | 396 | <span class="spinner is-active"></span> |
| 398 | 397 | <div class="give-progress" |
| 399 | 398 | data-current="1" |
| 400 | - data-total_ajax="<?php echo $total_ajax; ?>" | |
| 401 | - data-start="<?php echo $index_start; ?>" | |
| 402 | - data-end="<?php echo $index_end; ?>" | |
| 403 | - data-next="<?php echo $next; ?>" | |
| 404 | - data-total="<?php echo $total; ?>" | |
| 405 | - data-per_page="<?php echo self::$per_page; ?>"> | |
| 399 | + data-total_ajax="<?php echo esc_attr(absint( $total_ajax )); ?>" | |
| 400 | + data-start="<?php echo esc_attr(absint( $index_start )); ?>" | |
| 401 | + data-end="<?php echo esc_attr(absint( $index_end )); ?>" | |
| 402 | + data-next="<?php echo esc_attr(absint( $next )); ?>" | |
| 403 | + data-total="<?php echo esc_attr(absint( $total )); ?>" | |
| 404 | + data-per_page="<?php echo esc_attr(absint( self::$per_page )); ?>"> | |
| 406 | 405 | |
| 407 | - <div style="width: <?php echo $current_percentage; ?>%"></div> | |
| 406 | + <div style="width: <?php echo esc_attr((float) $current_percentage); ?>%"></div> | |
| 408 | 407 | </div> |
| 409 | 408 | <input type="hidden" value="3" name="step"> |
| 410 | - <input type="hidden" value='<?php echo maybe_serialize( $_REQUEST['mapto'] ); ?>' name="mapto" | |
| 411 | - class="mapto"> | |
| 412 | - <input type="hidden" value="<?php echo $_REQUEST['csv']; ?>" name="csv" class="csv"> | |
| 413 | - <input type="hidden" value="<?php echo $_REQUEST['mode']; ?>" name="mode" class="mode"> | |
| 414 | - <input type="hidden" value="<?php echo $_REQUEST['create_user']; ?>" name="create_user" | |
| 415 | - class="create_user"> | |
| 416 | - <input type="hidden" value="<?php echo $_REQUEST['delete_csv']; ?>" name="delete_csv" | |
| 417 | - class="delete_csv"> | |
| 418 | - <input type="hidden" value="<?php echo $delimiter; ?>" name="delimiter"> | |
| 419 | - <input type="hidden" value="<?php echo absint( $_REQUEST['dry_run'] ); ?>" name="dry_run"> | |
| 420 | - <input type="hidden" | |
| 421 | - value='<?php echo maybe_serialize( self::get_importer( $csv, 0, $delimiter ) ); ?>' | |
| 422 | - name="main_key" | |
| 423 | - class="main_key"> | |
| 409 | + <input type="hidden" value='<?php echo esc_attr( maybe_serialize( $_REQUEST['mapto'] ) ); ?>' name="mapto" class="mapto"> | |
| 410 | + <input type="hidden" value="<?php echo esc_attr($csv); ?>" name="csv" class="csv"> | |
| 411 | + <input type="hidden" value="<?php echo esc_attr( $_REQUEST['mode'] ); ?>" name="mode" class="mode"> | |
| 412 | + <input type="hidden" value="<?php echo esc_attr( $_REQUEST['create_user'] ); ?>" name="create_user" class="create_user"> | |
| 413 | + <input type="hidden" value="<?php echo esc_attr( $_REQUEST['delete_csv'] ); ?>" name="delete_csv" class="delete_csv"> | |
| 414 | + <input type="hidden" value="<?php echo esc_attr( $delimiter ); ?>" name="delimiter"> | |
| 415 | + <input type="hidden" value="<?php echo esc_attr(absint( $_REQUEST['dry_run']) ); ?>" name="dry_run"> | |
| 416 | + <input type="hidden" value='<?php echo esc_attr( maybe_serialize( self::get_importer( $csv, 0, $delimiter ) ) ); ?>' name="main_key" class="main_key"> | |
| 424 | 417 | </th> |
| 425 | 418 | </tr> |
| 426 | 419 | <?php |
| 427 | 420 | } |
| @@ -461,8 +454,16 @@ | ||
| 461 | 454 | * |
| 462 | 455 | * @since 1.8.14 |
| 463 | 456 | */ |
| 464 | 457 | public function render_dropdown() { |
| 458 | + if (!$this->is_nonce_valid()) { | |
| 459 | + Give_Admin_Settings::add_error( 'give-import-csv', __( 'Something went wrong.', 'give' ) ); | |
| 460 | + ?> | |
| 461 | + <input type="hidden" name="csv_not_valid" class="csv_not_valid" value="<?php echo esc_attr(give_import_page_url()); ?>"/> | |
| 462 | + <?php | |
| 463 | + wp_die(); | |
| 464 | + } | |
| 465 | + | |
| 465 | 466 | $csv = (int) $_GET['csv']; |
| 466 | 467 | $delimiter = ( ! empty( $_GET['delimiter'] ) ? give_clean( $_GET['delimiter'] ) : 'csv' ); |
| 467 | 468 | |
| 468 | 469 | // TO check if the CSV files that is being add is valid or not if not then redirect to first step again |
| @@ -468,19 +469,19 @@ | ||
| 468 | 469 | // TO check if the CSV files that is being add is valid or not if not then redirect to first step again |
| 469 | 470 | if ( ! $this->is_valid_csv( $csv ) ) { |
| 470 | 471 | $url = give_import_page_url(); |
| 471 | 472 | ?> |
| 472 | - <input type="hidden" name="csv_not_valid" class="csv_not_valid" value="<?php echo $url; ?>"/> | |
| 473 | + <input type="hidden" name="csv_not_valid" class="csv_not_valid" value="<?php echo esc_attr($url); ?>"/> | |
| 473 | 474 | <?php |
| 474 | 475 | } else { |
| 475 | 476 | ?> |
| 476 | 477 | <tr valign="top" class="give-import-dropdown"> |
| 477 | 478 | <th colspan="2"> |
| 478 | - <h2 id="give-import-title"><?php _e( 'Map CSV fields to donations', 'give' ) ?></h2> | |
| 479 | + <h2 id="give-import-title"><?php _e( 'Map CSV fields to donations', 'give' ); ?></h2> | |
| 479 | 480 | |
| 480 | - <p class="give-import-donation-required-fields-title"><?php _e( 'Required Fields' ); ?></p> | |
| 481 | + <p class="give-import-donation-required-fields-title"><?php _e( 'Required Fields', 'give' ); ?></p> | |
| 481 | 482 | |
| 482 | - <p class="give-field-description"><?php _e( 'These fields are required for the import to submitted' ); ?></p> | |
| 483 | + <p class="give-field-description"><?php _e( 'These fields are required for the import to submitted', 'give' ); ?></p> | |
| 483 | 484 | |
| 484 | 485 | <ul class="give-import-donation-required-fields"> |
| 485 | 486 | <li class="give-import-donation-required-email" |
| 486 | 487 | title="Please configure all required fields to start the import process."> |
| @@ -522,9 +523,9 @@ | ||
| 522 | 523 | </span> |
| 523 | 524 | </li> |
| 524 | 525 | </ul> |
| 525 | 526 | |
| 526 | - <p class="give-field-description"><?php _e( 'Select fields from your CSV file to map against donations fields or to ignore during import.', 'give' ) ?></p> | |
| 527 | + <p class="give-field-description"><?php _e( 'Select fields from your CSV file to map against donations fields or to ignore during import.', 'give' ); ?></p> | |
| 527 | 528 | </th> |
| 528 | 529 | </tr> |
| 529 | 530 | |
| 530 | 531 | <tr valign="top" class="give-import-dropdown"> |
| @@ -532,18 +533,21 @@ | ||
| 532 | 533 | <th><b><?php _e( 'Map to field', 'give' ); ?></b></th> |
| 533 | 534 | </tr> |
| 534 | 535 | |
| 535 | 536 | <?php |
| 536 | - $raw_key = $this->get_importer( $csv, 0, $delimiter ); | |
| 537 | + | |
| 538 | + $selectedOptions = []; // Initialize the tracking array for selected options | |
| 539 | + | |
| 540 | + $raw_key = $this->get_importer( $csv, 0, $delimiter ); | |
| 537 | 541 | $mapto = (array) ( isset( $_REQUEST['mapto'] ) ? $_REQUEST['mapto'] : array() ); |
| 538 | 542 | |
| 539 | 543 | foreach ( $raw_key as $index => $value ) { |
| 540 | 544 | ?> |
| 541 | - <tr valign="top" class="give-import-option"> | |
| 542 | - <th><?php echo $value; ?></th> | |
| 545 | + <tr valign="middle" class="give-import-option"> | |
| 546 | + <th><?php echo esc_html($value); ?></th> | |
| 543 | 547 | <th> |
| 544 | 548 | <?php |
| 545 | - $this->get_columns( $index, $value, $mapto ); | |
| 549 | + $this->get_columns( $index, $value, $mapto, $selectedOptions ); | |
| 546 | 550 | ?> |
| 547 | 551 | </th> |
| 548 | 552 | </tr> |
| 549 | 553 | <?php |
| @@ -579,33 +583,34 @@ | ||
| 579 | 583 | * |
| 580 | 584 | * @param string $index |
| 581 | 585 | * @param bool $value |
| 582 | 586 | * @param array $mapto |
| 587 | + * @param array $selectedOptions | |
| 583 | 588 | * |
| 584 | 589 | * @return void |
| 585 | 590 | */ |
| 586 | - private function get_columns( $index, $value = false, $mapto = array() ) { | |
| 591 | + private function get_columns( $index, $value = false, $mapto = array(), &$selectedOptions = array() ) { | |
| 587 | 592 | $default = give_import_default_options(); |
| 588 | 593 | $current_mapto = (string) ( ! empty( $mapto[ $index ] ) ? $mapto[ $index ] : '' ); |
| 589 | 594 | ?> |
| 590 | - <select name="mapto[<?php echo $index; ?>]"> | |
| 591 | - <?php $this->get_dropdown_option_html( $default, $current_mapto, $value ); ?> | |
| 595 | + <select name="mapto[<?php echo esc_attr($index); ?>]"> | |
| 596 | + <?php $this->get_dropdown_option_html( $default, $current_mapto, $value, $selectedOptions ); ?> | |
| 592 | 597 | |
| 593 | 598 | <optgroup label="<?php _e( 'Donations', 'give' ); ?>"> |
| 594 | 599 | <?php |
| 595 | - $this->get_dropdown_option_html( give_import_donations_options(), $current_mapto, $value ); | |
| 600 | + $this->get_dropdown_option_html( give_import_donations_options(), $current_mapto, $value, $selectedOptions ); | |
| 596 | 601 | ?> |
| 597 | 602 | </optgroup> |
| 598 | 603 | |
| 599 | 604 | <optgroup label="<?php _e( 'Donors', 'give' ); ?>"> |
| 600 | 605 | <?php |
| 601 | - $this->get_dropdown_option_html( give_import_donor_options(), $current_mapto, $value ); | |
| 606 | + $this->get_dropdown_option_html( give_import_donor_options(), $current_mapto, $value, $selectedOptions ); | |
| 602 | 607 | ?> |
| 603 | 608 | </optgroup> |
| 604 | 609 | |
| 605 | 610 | <optgroup label="<?php _e( 'Forms', 'give' ); ?>"> |
| 606 | 611 | <?php |
| 607 | - $this->get_dropdown_option_html( give_import_donation_form_options(), $current_mapto, $value ); | |
| 612 | + $this->get_dropdown_option_html( give_import_donation_form_options(), $current_mapto, $value, $selectedOptions ); | |
| 608 | 613 | ?> |
| 609 | 614 | </optgroup> |
| 610 | 615 | |
| 611 | 616 | <?php |
| @@ -626,46 +631,53 @@ | ||
| 626 | 631 | * |
| 627 | 632 | * @since 1.8.15 |
| 628 | 633 | * @access public |
| 629 | 634 | * |
| 630 | - * @param array $options | |
| 635 | + * @param array $options | |
| 631 | 636 | * @param string $current_mapto |
| 632 | - * @param bool $value | |
| 637 | + * @param bool $value | |
| 638 | + * @param array $selectedOptions | |
| 633 | 639 | * |
| 634 | 640 | * @return void |
| 635 | 641 | */ |
| 636 | - public function get_dropdown_option_html( $options, $current_mapto, $value = false ) { | |
| 642 | + public function get_dropdown_option_html( $options, $current_mapto, $value = false, &$selectedOptions = array() ) { | |
| 643 | + foreach ( $options as $option => $option_value ) { | |
| 644 | + $ignore = array(); | |
| 645 | + if ( isset( $option_value['ignore'] ) && is_array( $option_value['ignore'] ) ) { | |
| 646 | + $ignore = $option_value['ignore']; | |
| 647 | + unset( $option_value['ignore'] ); | |
| 648 | + } | |
| 637 | 649 | |
| 638 | - foreach ( $options as $option => $option_value ) { | |
| 639 | - $ignore = array(); | |
| 640 | - if ( isset( $option_value['ignore'] ) && is_array( $option_value['ignore'] ) ) { | |
| 641 | - $ignore = $option_value['ignore']; | |
| 642 | - unset( $option_value['ignore'] ); | |
| 643 | - } | |
| 650 | + $option_value_texts = (array) $option_value; | |
| 651 | + $option_text = $option_value_texts[0]; | |
| 644 | 652 | |
| 645 | - $option_value_texts = (array) $option_value; | |
| 646 | - $option_text = $option_value_texts[0]; | |
| 653 | + // Initialize selected as false | |
| 654 | + $selected = false; | |
| 647 | 655 | |
| 648 | - $checked = ( ( $current_mapto === $option ) ? 'selected' : false ); | |
| 649 | - if ( empty( $checked ) && ! in_array( $value, $ignore ) ) { | |
| 650 | - foreach ( $option_value_texts as $option_value_text ) { | |
| 651 | - $checked = $this->selected( $option_value_text, $value ); | |
| 652 | - if ( $checked ) { | |
| 653 | - break; | |
| 654 | - } | |
| 655 | - } | |
| 656 | - } | |
| 656 | + // If current field matches option and it's not already selected, mark as selected | |
| 657 | + if ( $current_mapto === $option && !in_array($option, $selectedOptions) ) { | |
| 658 | + $selected = 'selected'; | |
| 659 | + $selectedOptions[] = $option; // Add to selected options | |
| 660 | + } else { | |
| 661 | + // Check against option value texts if not already selected | |
| 662 | + if ( ! in_array( $value, $ignore ) && !in_array($option, $selectedOptions) ) { | |
| 663 | + foreach ( $option_value_texts as $option_value_text ) { | |
| 664 | + $selected = $this->selected( $option_value_text, $value ); | |
| 665 | + if ( $selected ) { | |
| 666 | + $selectedOptions[] = $option; // Add to selected options | |
| 667 | + break; | |
| 668 | + } | |
| 669 | + } | |
| 670 | + } | |
| 671 | + } | |
| 672 | + ?> | |
| 673 | + <option value="<?php echo esc_attr($option); ?>" <?php echo esc_html($selected); ?> ><?php echo esc_html($option_text); ?></option> | |
| 674 | + <?php | |
| 675 | + } | |
| 676 | + } | |
| 657 | 677 | |
| 658 | - echo sprintf( | |
| 659 | - '<option value="%1$s" %2$s >%3$s</option>', | |
| 660 | - $option, | |
| 661 | - $checked, | |
| 662 | - $option_text | |
| 663 | - ); | |
| 664 | - } | |
| 665 | - } | |
| 666 | 678 | |
| 667 | - /** | |
| 679 | + /** | |
| 668 | 680 | * Get column count of csv file. |
| 669 | 681 | * |
| 670 | 682 | * @since 1.8.14 |
| 671 | 683 | * |
| @@ -709,9 +721,9 @@ | ||
| 709 | 721 | * Get the CSV fields title from the CSV. |
| 710 | 722 | * |
| 711 | 723 | * @since 1.8.14 |
| 712 | 724 | * |
| 713 | - * @param (int) $file_id | |
| 725 | + * @param (int) $file_id | |
| 714 | 726 | * @param int $index |
| 715 | 727 | * @param string $delimiter |
| 716 | 728 | * |
| 717 | 729 | * @return array|bool $raw_data title of the CSV file fields |
| @@ -766,18 +778,18 @@ | ||
| 766 | 778 | public function render_progress() { |
| 767 | 779 | $step = $this->get_step(); |
| 768 | 780 | ?> |
| 769 | 781 | <ol class="give-progress-steps"> |
| 770 | - <li class="<?php echo( 1 === $step ? 'active' : '' ); ?>"> | |
| 782 | + <li class="<?php echo esc_attr( 1 === $step ? 'active' : '' ); ?>"> | |
| 771 | 783 | <?php _e( 'Upload CSV file', 'give' ); ?> |
| 772 | 784 | </li> |
| 773 | - <li class="<?php echo( 2 === $step ? 'active' : '' ); ?>"> | |
| 785 | + <li class="<?php echo esc_attr( 2 === $step ? 'active' : '' ); ?>"> | |
| 774 | 786 | <?php _e( 'Column mapping', 'give' ); ?> |
| 775 | 787 | </li> |
| 776 | - <li class="<?php echo( 3 === $step ? 'active' : '' ); ?>"> | |
| 788 | + <li class="<?php echo esc_attr( 3 === $step ? 'active' : '' ); ?>"> | |
| 777 | 789 | <?php _e( 'Import', 'give' ); ?> |
| 778 | 790 | </li> |
| 779 | - <li class="<?php echo( 4 === $step ? 'active' : '' ); ?>"> | |
| 791 | + <li class="<?php echo esc_attr( 4 === $step ? 'active' : '' ); ?>"> | |
| 780 | 792 | <?php _e( 'Done!', 'give' ); ?> |
| 781 | 793 | </li> |
| 782 | 794 | </ol> |
| 783 | 795 | <?php |
| @@ -861,21 +873,27 @@ | ||
| 861 | 873 | * @since 1.8.14 |
| 862 | 874 | * @return void |
| 863 | 875 | */ |
| 864 | 876 | public function render_media_csv() { |
| 865 | - add_filter( 'give_import_donation_submit_button_text', array( | |
| 866 | - $this, | |
| 867 | - 'give_import_donation_submit_text_render_media_csv' | |
| 868 | - ) ); | |
| 869 | - add_action( 'give_import_donation_submit_button', array( | |
| 870 | - $this, | |
| 871 | - 'give_import_donation_submit_button_render_media_csv' | |
| 872 | - ) ); | |
| 877 | + add_filter( | |
| 878 | + 'give_import_donation_submit_button_text', | |
| 879 | + array( | |
| 880 | + $this, | |
| 881 | + 'give_import_donation_submit_text_render_media_csv', | |
| 882 | + ) | |
| 883 | + ); | |
| 884 | + add_action( | |
| 885 | + 'give_import_donation_submit_button', | |
| 886 | + array( | |
| 887 | + $this, | |
| 888 | + 'give_import_donation_submit_button_render_media_csv', | |
| 889 | + ) | |
| 890 | + ); | |
| 873 | 891 | ?> |
| 874 | 892 | <tr valign="top"> |
| 875 | 893 | <th colspan="2"> |
| 876 | - <h2 id="give-import-title"><?php _e( 'Import donations from a CSV file', 'give' ) ?></h2> | |
| 877 | - <p class="give-field-description"><?php _e( 'This tool allows you to import or add donation data to your give form(s) via a CSV file.', 'give' ) ?></p> | |
| 894 | + <h2 id="give-import-title"><?php _e( 'Import donations from a CSV file', 'give' ); ?></h2> | |
| 895 | + <p class="give-field-description"><?php _e( 'This tool allows you to import or add donation data to your give form(s) via a CSV file.', 'give' ); ?></p> | |
| 878 | 896 | </th> |
| 879 | 897 | </tr> |
| 880 | 898 | <?php |
| 881 | 899 | $csv = ( isset( $_POST['csv'] ) ? give_clean( $_POST['csv'] ) : '' ); |
| @@ -914,9 +932,12 @@ | ||
| 914 | 932 | array( |
| 915 | 933 | 'id' => 'csv', |
| 916 | 934 | 'name' => __( 'Choose a CSV file:', 'give' ), |
| 917 | 935 | 'type' => 'file', |
| 918 | - 'attributes' => array( 'editing' => 'false', 'library' => 'text' ), | |
| 936 | + 'attributes' => array( | |
| 937 | + 'editing' => 'false', | |
| 938 | + 'library' => 'text', | |
| 939 | + ), | |
| 919 | 940 | 'description' => $csv_description, |
| 920 | 941 | 'fvalue' => 'url', |
| 921 | 942 | 'default' => $csv, |
| 922 | 943 | ), |
| @@ -985,9 +1006,9 @@ | ||
| 985 | 1006 | Give_Admin_Settings::output_fields( $settings, 'give_settings' ); |
| 986 | 1007 | } else { |
| 987 | 1008 | ?> |
| 988 | 1009 | <input type="hidden" name="is_csv_valid" class="is_csv_valid" |
| 989 | - value="<?php echo $this->is_csv_valid; ?>"> | |
| 1010 | + value="<?php echo esc_attr($this->is_csv_valid); ?>"> | |
| 990 | 1011 | <?php |
| 991 | 1012 | } |
| 992 | 1013 | } |
| 993 | 1014 | |
| @@ -996,8 +1017,11 @@ | ||
| 996 | 1017 | * |
| 997 | 1018 | * @since 1.8.14 |
| 998 | 1019 | */ |
| 999 | 1020 | public function save() { |
| 1021 | + if (!$this->is_nonce_valid()){ | |
| 1022 | + wp_die(); | |
| 1023 | + } | |
| 1000 | 1024 | // Get the current step. |
| 1001 | 1025 | $step = $this->get_step(); |
| 1002 | 1026 | |
| 1003 | 1027 | // Validation for first step. |
| @@ -1005,27 +1029,32 @@ | ||
| 1005 | 1029 | $csv_id = absint( $_POST['csv_id'] ); |
| 1006 | 1030 | |
| 1007 | 1031 | if ( $this->is_valid_csv( $csv_id, esc_url( $_POST['csv'] ) ) ) { |
| 1008 | 1032 | |
| 1009 | - $url = give_import_page_url( (array) apply_filters( 'give_import_step_two_url', array( | |
| 1010 | - 'step' => '2', | |
| 1011 | - 'importer-type' => $this->importer_type, | |
| 1012 | - 'csv' => $csv_id, | |
| 1013 | - 'delimiter' => isset( $_REQUEST['delimiter'] ) ? give_clean( $_REQUEST['delimiter'] ) : 'csv', | |
| 1014 | - 'mode' => empty( $_POST['mode'] ) ? | |
| 1015 | - '0' : | |
| 1016 | - ( give_is_setting_enabled( give_clean( $_POST['mode'] ) ) ? '1' : '0' ), | |
| 1017 | - 'create_user' => empty( $_POST['create_user'] ) ? | |
| 1018 | - '0' : | |
| 1019 | - ( give_is_setting_enabled( give_clean( $_POST['create_user'] ) ) ? '1' : '0' ), | |
| 1020 | - 'delete_csv' => empty( $_POST['delete_csv'] ) ? | |
| 1021 | - '1' : | |
| 1022 | - ( give_is_setting_enabled( give_clean( $_POST['delete_csv'] ) ) ? '1' : '0' ), | |
| 1023 | - 'per_page' => isset( $_POST['per_page'] ) ? absint( $_POST['per_page'] ) : self::$per_page, | |
| 1024 | - 'dry_run' => isset( $_POST['dry_run'] ) ? absint( $_POST['dry_run'] ) : 0, | |
| 1025 | - ) ) ); | |
| 1033 | + $url = give_import_page_url( | |
| 1034 | + (array) apply_filters( | |
| 1035 | + 'give_import_step_two_url', | |
| 1036 | + array( | |
| 1037 | + 'step' => '2', | |
| 1038 | + 'importer-type' => $this->importer_type, | |
| 1039 | + 'csv' => $csv_id, | |
| 1040 | + 'delimiter' => isset( $_REQUEST['delimiter'] ) ? give_clean( $_REQUEST['delimiter'] ) : 'csv', | |
| 1041 | + 'mode' => empty( $_POST['mode'] ) ? | |
| 1042 | + '0' : | |
| 1043 | + ( give_is_setting_enabled( give_clean( $_POST['mode'] ) ) ? '1' : '0' ), | |
| 1044 | + 'create_user' => empty( $_POST['create_user'] ) ? | |
| 1045 | + '0' : | |
| 1046 | + ( give_is_setting_enabled( give_clean( $_POST['create_user'] ) ) ? '1' : '0' ), | |
| 1047 | + 'delete_csv' => empty( $_POST['delete_csv'] ) ? | |
| 1048 | + '1' : | |
| 1049 | + ( give_is_setting_enabled( give_clean( $_POST['delete_csv'] ) ) ? '1' : '0' ), | |
| 1050 | + 'per_page' => isset( $_POST['per_page'] ) ? absint( $_POST['per_page'] ) : self::$per_page, | |
| 1051 | + 'dry_run' => isset( $_POST['dry_run'] ) ? absint( $_POST['dry_run'] ) : 0, | |
| 1052 | + ) | |
| 1053 | + ) | |
| 1054 | + ); | |
| 1026 | 1055 | |
| 1027 | - $this->is_csv_valid = $url; | |
| 1056 | + $this->is_csv_valid = wp_nonce_url($url, 'give-save-settings', '_give-save-settings'); | |
| 1028 | 1057 | } |
| 1029 | 1058 | } |
| 1030 | 1059 | } |
| 1031 | 1060 | |
| @@ -1085,11 +1114,19 @@ | ||
| 1085 | 1114 | * @return bool |
| 1086 | 1115 | */ |
| 1087 | 1116 | private function is_donations_import_page() { |
| 1088 | 1117 | return 'import' === give_get_current_setting_tab() && |
| 1089 | - isset( $_GET['importer-type'] ) && | |
| 1090 | - $this->importer_type === give_clean( $_GET['importer-type'] ); | |
| 1118 | + isset( $_GET['importer-type'] ) && | |
| 1119 | + $this->importer_type === give_clean( $_GET['importer-type'] ); | |
| 1091 | 1120 | } |
| 1121 | + | |
| 1122 | + /** | |
| 1123 | + * @since 2.25.2 | |
| 1124 | + */ | |
| 1125 | + private function is_nonce_valid() | |
| 1126 | + { | |
| 1127 | + return !empty($_REQUEST['_give-save-settings']) && wp_verify_nonce($_REQUEST['_give-save-settings'], 'give-save-settings'); | |
| 1128 | + } | |
| 1092 | 1129 | } |
| 1093 | 1130 | |
| 1094 | 1131 | Give_Import_Donations::get_instance()->setup(); |
| 1095 | 1132 | } |