setup_hooks();
}
/**
* Setup Hooks.
*
* @since 1.8.14
*
* @return void
*/
private function setup_hooks() {
if ( ! $this->is_donations_import_page() ) {
return;
}
// Do not render main import tools page.
remove_action( 'give_admin_field_tools_import', array( 'Give_Settings_Import', 'render_import_field', ) );
// Render donation import page
add_action( 'give_admin_field_tools_import', array( $this, 'render_page' ) );
// Print the HTML.
add_action( 'give_tools_import_donations_form_start', array( $this, 'html' ), 10 );
// Run when form submit.
add_action( 'give-tools_save_import', array( $this, 'save' ) );
add_action( 'give-tools_update_notices', array( $this, 'update_notices' ), 11, 1 );
// Used to add submit button.
add_action( 'give_tools_import_donations_form_end', array( $this, 'submit' ), 10 );
}
/**
* Update notice
*
* @since 1.8.14
*
* @param $messages
*
* @return mixed
*/
public function update_notices( $messages ) {
if ( ! empty( $_GET['tab'] ) && 'import' === give_clean( $_GET['tab'] ) ) {
unset( $messages['give-setting-updated'] );
}
return $messages;
}
/**
* Print submit and nonce button.
*
* @since 1.8.14
*/
public function submit() {
wp_nonce_field( 'give-save-settings', '_give-save-settings' );
?>
get_step();
// Show progress.
$this->render_progress();
?>
array(
__( '%s duplicate %s detected', 'give' ),
__( '%s duplicate %s detected', 'give' ),
__( 'donor', 'give' ),
__( 'donors', 'give' ),
),
'create_donor' => array(
__( '%s %s created', 'give' ),
__( '%s %s will be created', 'give' ),
__( 'donor', 'give' ),
__( 'donors', 'give' ),
),
'create_form' => array(
__( '%s donation %s created', 'give' ),
__( '%s donation %s will be created', 'give' ),
__( 'form', 'give' ),
__( 'forms', 'give' ),
),
'duplicate_donation' => array(
__( '%s duplicate %s detected', 'give' ),
__( '%s duplicate %s detected', 'give' ),
__( 'donation', 'give' ),
__( 'donations', 'give' ),
),
'create_donation' => array(
__( '%s %s imported', 'give' ),
__( '%s %s will be imported', 'give' ),
__( 'donation', 'give' ),
__( 'donations', 'give' ),
),
);
$total = (int) $_GET['total'];
-- $total;
$success = (bool) $_GET['success'];
$dry_run = empty( $_GET['dry_run'] ) ? 0 : absint( $_GET['dry_run'] );
?>
{$total}"
);
} else {
printf(
_n( 'Import complete! %s donation processed', 'Import complete! %s donations processed', $total, 'give' ),
"{$total}"
);
}
} else {
printf(
_n( 'Failed to import %s donation', 'Failed to import %s donations', $total, 'give' ),
"{$total}"
);
}
?>
'give_forms',
'page' => 'give-tools',
'tab' => 'import',
);
if ( $success ) {
if ( $dry_run ) {
$query_arg = array(
'post_type' => 'give_forms',
'page' => 'give-tools',
'tab' => 'import',
'importer-type' => 'import_donations',
);
$text = __( 'Start Import', 'give' );
} else {
$query_arg = array(
'post_type' => 'give_forms',
'page' => 'give-payment-history',
);
$text = __( 'View Donations', 'give' );
}
}
foreach ( $report as $key => $value ) {
if ( array_key_exists( $key, $report_html ) && ! empty( $value ) ) {
$key_name = $report_html[ $key ][2];
if ( $value > 1 ) {
$key_name = $report_html[ $key ][3];
}
?>
|
|
|
|
' name="mapto"
class="mapto">
|
is_valid_csv( $csv ) ) {
$url = give_import_page_url();
?>
|
|
|
|
get_importer( $csv, 0, $delimiter );
$mapto = (array) ( isset( $_REQUEST['mapto'] ) ? $_REQUEST['mapto'] : array() );
foreach ( $raw_key as $index => $value ) {
?>
|
get_columns( $index, $value, $mapto );
?>
|
$option_value ) {
$ignore = array();
if ( isset( $option_value['ignore'] ) && is_array( $option_value['ignore'] ) ) {
$ignore = $option_value['ignore'];
unset( $option_value['ignore'] );
}
$option_value_texts = (array) $option_value;
$option_text = $option_value_texts[0];
$checked = ( ( $current_mapto === $option ) ? 'selected' : false );
if ( empty( $checked ) && ! in_array( $value, $ignore ) ) {
foreach ( $option_value_texts as $option_value_text ) {
$checked = $this->selected( $option_value_text, $value );
if ( $checked ) {
break;
}
}
}
echo sprintf(
'',
$option,
$checked,
$option_text
);
}
}
/**
* Get column count of csv file.
*
* @since 1.8.14
*
* @param $file_id
*
* @return bool|int
*/
public function get_csv_total( $file_id ) {
$total = false;
if ( $file_id ) {
$file_dir = get_attached_file( $file_id );
if ( $file_dir ) {
$total = $this->get_csv_data_from_file_dir( $file_dir );
}
}
return $total;
}
/**
* Get data from File
*
* @since 2.1
*
* @param $file_dir
*
* @return bool|int
*/
public function get_csv_data_from_file_dir( $file_dir ) {
$total = false;
if ( $file_dir ) {
$file = new SplFileObject( $file_dir, 'r' );
$file->seek( PHP_INT_MAX );
$total = $file->key() + 1;
}
return $total;
}
/**
* Get the CSV fields title from the CSV.
*
* @since 1.8.14
*
* @param (int) $file_id
* @param int $index
* @param string $delimiter
*
* @return array|bool $raw_data title of the CSV file fields
*/
public function get_importer( $file_id, $index = 0, $delimiter = 'csv' ) {
/**
* Filter to modify delimiter of Import.
*
* @since 1.8.14
*
* Return string $delimiter.
*/
$delimiter = (string) apply_filters( 'give_import_delimiter_set', $delimiter );
$raw_data = false;
$file_dir = get_attached_file( $file_id );
if ( $file_dir ) {
if ( false !== ( $handle = fopen( $file_dir, 'r' ) ) ) {
$raw_data = fgetcsv( $handle, $index, $delimiter );
// Remove BOM signature from the first item.
if ( isset( $raw_data[0] ) ) {
$raw_data[0] = $this->remove_utf8_bom( $raw_data[0] );
}
}
}
return $raw_data;
}
/**
* Remove UTF-8 BOM signature.
*
* @since 1.8.14
*
* @param string $string String to handle.
*
* @return string
*/
public function remove_utf8_bom( $string ) {
if ( 'efbbbf' === substr( bin2hex( $string ), 0, 6 ) ) {
$string = substr( $string, 3 );
}
return $string;
}
/**
* Is used to show the process when user upload the donor form.
*
* @since 1.8.14
*/
public function render_progress() {
$step = $this->get_step();
?>
-
-
-
-
check_for_dropdown_or_import() ) {
$on_step = 3;
} elseif ( 2 === $step ) {
$on_step = 2;
} elseif ( 4 === $step ) {
$on_step = 4;
}
return $on_step;
}
/**
* Render donations import page
*
* @since 1.8.14
*/
public function render_page() {
include_once GIVE_PLUGIN_DIR . 'includes/admin/tools/views/html-admin-page-import-donations.php';
}
/**
* Print Dry Run HTML on donation import page
*
* @since 2.1
*/
public function give_import_donation_submit_button_render_media_csv() {
$dry_run = isset( $_POST['dry_run'] ) ? absint( $_POST['dry_run'] ) : 1;
?>
|
|
is_valid_csv( $csv_id, $csv ) ) {
$csv_id = $csv = '';
}
$per_page = isset( $_POST['per_page'] ) ? absint( $_POST['per_page'] ) : self::$per_page;
$sample_file_text = sprintf(
'%s %s.',
__( 'Download the sample file', 'give' ),
esc_url( GIVE_PLUGIN_URL . 'sample-data/sample-data.csv' ),
__( 'here', 'give' )
);
$csv_description = sprintf(
'%1$s %2$s',
__( 'The file must be a Comma Seperated Version (CSV) file type only.', 'give' ),
$sample_file_text
);
$settings = array(
array(
'id' => 'csv',
'name' => __( 'Choose a CSV file:', 'give' ),
'type' => 'file',
'attributes' => array( 'editing' => 'false', 'library' => 'text' ),
'description' => $csv_description,
'fvalue' => 'url',
'default' => $csv,
),
array(
'id' => 'csv_id',
'type' => 'hidden',
'value' => $csv_id,
),
array(
'id' => 'delimiter',
'name' => __( 'CSV Delimiter:', 'give' ),
'description' => __( 'In case your CSV file supports a different type of separator (or delimiter) -- like a tab or space -- you can set that here.', 'give' ),
'default' => $delimiter,
'type' => 'select',
'options' => array(
'csv' => __( 'Comma', 'give' ),
'tab-separated-values' => __( 'Tab', 'give' ),
),
),
array(
'id' => 'mode',
'name' => __( 'Test Mode:', 'give' ),
'description' => __( 'Select whether you would like these donations to be marked as "test" donations within the database. By default, they will be marked as live donations.', 'give' ),
'default' => $mode,
'type' => 'radio_inline',
'options' => array(
'enabled' => __( 'Enabled', 'give' ),
'disabled' => __( 'Disabled', 'give' ),
),
),
array(
'id' => 'create_user',
'name' => __( 'Create WP users for new donors:', 'give' ),
'description' => __( 'The importer can create WordPress user accounts based on the names and email addresses of the donations in your CSV file. Enable this option if you\'d like the importer to do that.', 'give' ),
'default' => $create_user,
'type' => 'radio_inline',
'options' => array(
'enabled' => __( 'Enabled', 'give' ),
'disabled' => __( 'Disabled', 'give' ),
),
),
array(
'id' => 'delete_csv',
'name' => __( 'Delete CSV after import:', 'give' ),
'description' => __( 'Your CSV file will be uploaded via the WordPress Media Library. It\'s a good idea to delete it after the import is finished so that your sensitive data is not accessible on the web. Disable this only if you plan to delete the file manually later.', 'give' ),
'default' => $delete_csv,
'type' => 'radio_inline',
'options' => array(
'enabled' => __( 'Enabled', 'give' ),
'disabled' => __( 'Disabled', 'give' ),
),
),
array(
'id' => 'per_page',
'name' => __( 'Process Rows Per Batch:', 'give' ),
'type' => 'number',
'description' => __( 'Determine how many rows you would like to import per cycle.', 'give' ),
'default' => $per_page,
'class' => 'give-text-small',
),
);
$settings = apply_filters( 'give_import_file_upload_html', $settings );
if ( empty( $this->is_csv_valid ) ) {
Give_Admin_Settings::output_fields( $settings, 'give_settings' );
} else {
?>
get_step();
// Validation for first step.
if ( 1 === $step ) {
$csv_id = absint( $_POST['csv_id'] );
if ( $this->is_valid_csv( $csv_id, esc_url( $_POST['csv'] ) ) ) {
$url = give_import_page_url( (array) apply_filters( 'give_import_step_two_url', array(
'step' => '2',
'importer-type' => $this->importer_type,
'csv' => $csv_id,
'delimiter' => isset( $_REQUEST['delimiter'] ) ? give_clean( $_REQUEST['delimiter'] ) : 'csv',
'mode' => empty( $_POST['mode'] ) ?
'0' :
( give_is_setting_enabled( give_clean( $_POST['mode'] ) ) ? '1' : '0' ),
'create_user' => empty( $_POST['create_user'] ) ?
'0' :
( give_is_setting_enabled( give_clean( $_POST['create_user'] ) ) ? '1' : '0' ),
'delete_csv' => empty( $_POST['delete_csv'] ) ?
'1' :
( give_is_setting_enabled( give_clean( $_POST['delete_csv'] ) ) ? '1' : '0' ),
'per_page' => isset( $_POST['per_page'] ) ? absint( $_POST['per_page'] ) : self::$per_page,
'dry_run' => isset( $_POST['dry_run'] ) ? absint( $_POST['dry_run'] ) : 0,
) ) );
$this->is_csv_valid = $url;
}
}
}
/**
* Check if user uploaded csv is valid or not.
*
* @since 1.8.14
* @access public
*
* @param mixed $csv ID of the CSV files.
* @param string $match_url ID of the CSV files.
*
* @return bool $has_error CSV is valid or not.
*/
private function is_valid_csv( $csv = false, $match_url = '' ) {
$is_valid_csv = true;
if ( $csv ) {
$csv_url = wp_get_attachment_url( $csv );
$delimiter = ( ! empty( $_REQUEST['delimiter'] ) ? give_clean( $_REQUEST['delimiter'] ) : 'csv' );
if (
! $csv_url ||
( ! empty( $match_url ) && ( $csv_url !== $match_url ) ) ||
( ( $mime_type = get_post_mime_type( $csv ) ) && ! strpos( $mime_type, $delimiter ) )
) {
$is_valid_csv = false;
Give_Admin_Settings::add_error( 'give-import-csv', __( 'Please upload or provide a valid CSV file.', 'give' ) );
}
} else {
$is_valid_csv = false;
Give_Admin_Settings::add_error( 'give-import-csv', __( 'Please upload or provide a valid CSV file.', 'give' ) );
}
return $is_valid_csv;
}
/**
* Render report import field
*
* @since 1.8.14
* @access public
*
* @param $field
* @param $option_value
*/
public function render_import_field( $field, $option_value ) {
include_once GIVE_PLUGIN_DIR . 'includes/admin/tools/views/html-admin-page-imports.php';
}
/**
* Get if current page import donations page or not
*
* @since 1.8.14
* @return bool
*/
private function is_donations_import_page() {
return 'import' === give_get_current_setting_tab() &&
isset( $_GET['importer-type'] ) &&
$this->importer_type === give_clean( $_GET['importer-type'] );
}
}
Give_Import_Donations::get_instance()->setup();
}