← Back
Editing: class-give-import-donations.php
<?php /** * Donations Import Class * * This class handles donations import. * * @package Give * @subpackage Classes/Give_Import_Donations * @copyright Copyright (c) 2017, GiveWP * @license https://opensource.org/licenses/gpl-license GNU Public License * @since 1.8.14 */ if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly } if ( ! class_exists( 'Give_Import_Donations' ) ) { /** * Give_Import_Donations. * * @since 1.8.14 */ final class Give_Import_Donations { /** * Importer type * * @since 1.8.13 * @var string */ private $importer_type = 'import_donations'; /** * Instance. * * @since * @access private * @var */ private static $instance; /** * Importing donation per page. * * @since 1.8.14 * * @var int */ public static $per_page = 25; /** * Importing donation per page. * * @since 2.1 * * @var int */ public $is_csv_valid = false; /** * Singleton pattern. * * @since * @access private */ private function __construct() { self::$per_page = ! empty( $_GET['per_page'] ) ? absint( $_GET['per_page'] ) : self::$per_page; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only import step param; it only changes what is shown and saves nothing. } /** * Get instance. * * @since * @access public * * @return static */ public static function get_instance() { if ( null === static::$instance ) { self::$instance = new static(); } return self::$instance; } /** * Setup * * @since 1.8.14 * * @return void */ public function setup() { $this->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'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- read-only import step param; it only changes what is shown and saves nothing. give_clean() unslashes and sanitizes with sanitize_text_field(). 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' ); ?> <input type="hidden" class="import-step" id="import-step" name="step" value="<?php echo esc_attr($this->get_step()); ?>"/> <input type="hidden" class="importer-type" value="<?php echo esc_attr($this->importer_type); ?>"/> <?php } /** * Print the HTML for importer. * * @since 1.8.14 */ public function html() { $step = $this->get_step(); // Show progress. $this->render_progress(); ?> <section> <table 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' : '' )); ?> " id="<?php echo esc_attr("step-{$step}"); ?>"> <tbody> <?php switch ( $step ) { case 1: $this->render_media_csv(); break; case 2: $this->render_dropdown(); break; case 3: $this->start_import(); break; case 4: $this->import_success(); } if ( false === $this->check_for_dropdown_or_import() ) { ?> <tr valign="top"> <th> <input type="submit" class="button button-primary button-large button-secondary <?php echo esc_attr("step-{$step}"); ?>" id="recount-stats-submit" value=" <?php /** * Filter to modify donation importer submit button text. * * @since 2.1 */ echo esc_attr(apply_filters( 'give_import_donation_submit_button_text', __( 'Submit', 'give' ) )); ?> "/> </th> <th> <?php /** * Action to add submit button description. * * @since 2.1 */ do_action( 'give_import_donation_submit_button' ); ?> </th> </tr> <?php } ?> </tbody> </table> </section> <?php } /** * Show success notice * * @since 4.19.1 Escape output. Add translators comments. Check that the total and success values are set. * @since 2.25.3 Add nonce check. * @since 1.8.14 */ public function import_success() { check_admin_referer('give_donation_import_success'); $delete_csv = ( ! empty( $_GET['delete_csv'] ) ? absint( $_GET['delete_csv'] ) : false ); $csv = ( ! empty( $_GET['csv'] ) ? absint( $_GET['csv'] ) : false ); if ( ! empty( $delete_csv ) && ! empty( $csv ) ) { wp_delete_attachment( $csv, true ); } $report = give_import_donation_report(); $report_html = array( 'duplicate_donor' => array( /* translators: 1: Number of items, 2: Name of the item type, for example donor or donors */ __( '%1$s duplicate %2$s detected', 'give' ), /* translators: 1: Number of items, 2: Name of the item type, for example donor or donors */ __( '%1$s duplicate %2$s detected', 'give' ), __( 'donor', 'give' ), __( 'donors', 'give' ), ), 'create_donor' => array( /* translators: 1: Number of items, 2: Name of the item type, for example donor or donors */ __( '%1$s %2$s created', 'give' ), /* translators: 1: Number of items, 2: Name of the item type, for example donor or donors */ __( '%1$s %2$s will be created', 'give' ), __( 'donor', 'give' ), __( 'donors', 'give' ), ), 'create_form' => array( /* translators: 1: Number of items, 2: Name of the item type, for example donor or donors */ __( '%1$s donation %2$s created', 'give' ), /* translators: 1: Number of items, 2: Name of the item type, for example donor or donors */ __( '%1$s donation %2$s will be created', 'give' ), __( 'form', 'give' ), __( 'forms', 'give' ), ), 'duplicate_donation' => array( /* translators: 1: Number of items, 2: Name of the item type, for example donor or donors */ __( '%1$s duplicate %2$s detected', 'give' ), /* translators: 1: Number of items, 2: Name of the item type, for example donor or donors */ __( '%1$s duplicate %2$s detected', 'give' ), __( 'donation', 'give' ), __( 'donations', 'give' ), ), 'create_donation' => array( /* translators: 1: Number of items, 2: Name of the item type, for example donor or donors */ __( '%1$s %2$s imported', 'give' ), /* translators: 1: Number of items, 2: Name of the item type, for example donor or donors */ __( '%1$s %2$s will be imported', 'give' ), __( 'donation', 'give' ), __( 'donations', 'give' ), ), ); $total = isset( $_GET['total'] ) ? (int) $_GET['total'] : 0; -- $total; $success = isset( $_GET['success'] ) && (bool) $_GET['success']; $dry_run = empty( $_GET['dry_run'] ) ? 0 : absint( $_GET['dry_run'] ); ?> <tr valign="top" class="give-import-dropdown"> <th colspan="2"> <h2> <?php if ( $success ) { if ( $dry_run ) { echo wp_kses_post( sprintf( /* translators: %s: Number of donations, wrapped in bold tags */ _n( 'Dry run import complete! %s donation processed', 'Dry run import complete! %s donations processed', $total, 'give' ), '<strong>' . esc_html( $total ) . '</strong>' ) ); } else { echo wp_kses_post( sprintf( /* translators: %s: Number of donations, wrapped in bold tags */ _n( 'Import complete! %s donation processed', 'Import complete! %s donations processed', $total, 'give' ), '<strong>' . esc_html( $total ) . '</strong>' ) ); } } else { echo wp_kses_post( sprintf( /* translators: %s: Number of donations, wrapped in bold tags */ _n( 'Failed to import %s donation', 'Failed to import %s donations', $total, 'give' ), '<strong>' . esc_html( $total ) . '</strong>' ) ); } ?> </h2> <?php $text = __( 'Import Donation', 'give' ); $query_arg = array( 'post_type' => '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]; } ?> <p> <?php echo esc_html( sprintf( $report_html[ $key ][ $dry_run ], $value, $key_name ) ); ?> </p> <?php } } ?> <p> <a class="button button-large button-secondary" href="<?php echo esc_url( add_query_arg( $query_arg, admin_url( 'edit.php' ) ) ); ?>"><?php echo esc_html( $text ); ?></a> </p> </th> </tr> <?php } /** * Will start Import * * @since 4.19.1 Escape translated output. Unslash and sanitize the request input. * @since 1.8.14 */ public function start_import() { // Reset the donation form report. give_import_donation_report_reset(); // phpcs:disable WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing -- read-only import step params; they are printed back into the form and save nothing. $csv = isset( $_REQUEST['csv'] ) ? absint( $_REQUEST['csv'] ) : 0; $mapto = isset( $_REQUEST['mapto'] ) ? map_deep( wp_unslash( $_REQUEST['mapto'] ), 'sanitize_text_field' ) : ''; $mode = isset( $_REQUEST['mode'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['mode'] ) ) : ''; $create_user = isset( $_REQUEST['create_user'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['create_user'] ) ) : ''; $delete_csv = isset( $_REQUEST['delete_csv'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['delete_csv'] ) ) : ''; $dry_run = isset( $_REQUEST['dry_run'] ) ? absint( $_REQUEST['dry_run'] ) : 0; $delimiter = ( ! empty( $_REQUEST['delimiter'] ) ? give_clean( $_REQUEST['delimiter'] ) : 'csv' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- give_clean() unslashes and sanitizes with sanitize_text_field(). // phpcs:enable WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing $index_start = 1; $next = true; $total = self::get_csv_total( $csv ); if ( self::$per_page < $total ) { $total_ajax = ceil( $total / self::$per_page ); $index_end = self::$per_page; } else { $total_ajax = 1; $index_end = $total; $next = false; } $current_percentage = 100 / ( $total_ajax + 1 ); ?> <tr valign="top" class="give-import-dropdown"> <th colspan="2"> <h2 id="give-import-title"><?php esc_html_e( 'Importing', 'give' ); ?></h2> <p class="give-field-description"><?php esc_html_e( 'Your donations are now being imported...', 'give' ); ?></p> </th> </tr> <tr valign="top" class="give-import-dropdown"> <th colspan="2"> <span class="spinner is-active"></span> <div class="give-progress" data-current="1" data-total_ajax="<?php echo esc_attr(absint( $total_ajax )); ?>" data-start="<?php echo esc_attr(absint( $index_start )); ?>" data-end="<?php echo esc_attr(absint( $index_end )); ?>" data-next="<?php echo esc_attr(absint( $next )); ?>" data-total="<?php echo esc_attr(absint( $total )); ?>" data-per_page="<?php echo esc_attr(absint( self::$per_page )); ?>"> <div style="width: <?php echo esc_attr((float) $current_percentage); ?>%"></div> </div> <input type="hidden" value="3" name="step"> <input type="hidden" value='<?php echo esc_attr( maybe_serialize( $mapto ) ); ?>' name="mapto" class="mapto"> <input type="hidden" value="<?php echo esc_attr($csv); ?>" name="csv" class="csv"> <input type="hidden" value="<?php echo esc_attr( $mode ); ?>" name="mode" class="mode"> <input type="hidden" value="<?php echo esc_attr( $create_user ); ?>" name="create_user" class="create_user"> <input type="hidden" value="<?php echo esc_attr( $delete_csv ); ?>" name="delete_csv" class="delete_csv"> <input type="hidden" value="<?php echo esc_attr( $delimiter ); ?>" name="delimiter"> <input type="hidden" value="<?php echo esc_attr( $dry_run ); ?>" name="dry_run"> <input type="hidden" value='<?php echo esc_attr( maybe_serialize( self::get_importer( $csv, 0, $delimiter ) ) ); ?>' name="main_key" class="main_key"> </th> </tr> <?php } /** * Will return true if importing can be started or not else false. * * @since 4.19.1 Unslash and sanitize the mapped fields. * @since 1.8.14 */ public function check_for_dropdown_or_import() { $return = true; if ( isset( $_REQUEST['mapto'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only import step param; it only changes what is shown and saves nothing. $mapto = (array) map_deep( wp_unslash( $_REQUEST['mapto'] ), 'sanitize_text_field' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only import step param; it is only checked for required fields and saves nothing. if ( false === in_array( 'form_title', $mapto ) && false === in_array( 'form_id', $mapto ) ) { Give_Admin_Settings::add_error( 'give-import-csv-form', __( 'In order to import donations, a column must be mapped to either the "Donation Form Title" or "Donation Form ID" field. Please map a column to one of those fields.', 'give' ) ); $return = false; } if ( false === in_array( 'amount', $mapto ) ) { Give_Admin_Settings::add_error( 'give-import-csv-amount', __( 'In order to import donations, a column must be mapped to the "Amount" field. Please map a column to that field.', 'give' ) ); $return = false; } if ( false === in_array( 'email', $mapto ) && false === in_array( 'donor_id', $mapto ) ) { Give_Admin_Settings::add_error( 'give-import-csv-donor', __( 'In order to import donations, a column must be mapped to either the "Donor Email" or "Donor ID" field. Please map a column to that field.', 'give' ) ); $return = false; } } else { $return = false; } return $return; } /** * Print the Dropdown option for CSV. * * @since 4.19.1 Escape translated output. Unslash and sanitize the request input. * @since 1.8.14 */ public function render_dropdown() { if (!$this->is_nonce_valid()) { Give_Admin_Settings::add_error( 'give-import-csv', __( 'Something went wrong.', 'give' ) ); ?> <input type="hidden" name="csv_not_valid" class="csv_not_valid" value="<?php echo esc_attr(give_import_page_url()); ?>"/> <?php wp_die(); } $csv = isset( $_GET['csv'] ) ? (int) $_GET['csv'] : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- nonce verified by $this->is_nonce_valid() at the top of this method. $delimiter = ( ! empty( $_GET['delimiter'] ) ? give_clean( $_GET['delimiter'] ) : 'csv' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- nonce verified by $this->is_nonce_valid() at the top of this method. give_clean() unslashes and sanitizes with sanitize_text_field(). // TO check if the CSV files that is being add is valid or not if not then redirect to first step again if ( ! $this->is_valid_csv( $csv ) ) { $url = give_import_page_url(); ?> <input type="hidden" name="csv_not_valid" class="csv_not_valid" value="<?php echo esc_attr($url); ?>"/> <?php } else { ?> <tr valign="top" class="give-import-dropdown"> <th colspan="2"> <h2 id="give-import-title"><?php esc_html_e( 'Map CSV fields to donations', 'give' ); ?></h2> <p class="give-import-donation-required-fields-title"><?php esc_html_e( 'Required Fields', 'give' ); ?></p> <p class="give-field-description"><?php esc_html_e( 'These fields are required for the import to submitted', 'give' ); ?></p> <ul class="give-import-donation-required-fields"> <li class="give-import-donation-required-email" title="Please configure all required fields to start the import process."> <span class="give-import-donation-required-symbol dashicons dashicons-no-alt"></span> <span class="give-import-donation-required-text"> <?php esc_html_e( 'Email Address', 'give' ); ?> </span> </li> <li class="give-import-donation-required-first" title="Please configure all required fields to start the import process."> <span class="give-import-donation-required-symbol dashicons dashicons-no-alt"></span> <span class="give-import-donation-required-text"> <?php esc_html_e( 'First Name', 'give' ); ?> </span> </li> <li class="give-import-donation-required-amount" title="Please configure all required fields to start the import process."> <span class="give-import-donation-required-symbol dashicons dashicons-no-alt"></span> <span class="give-import-donation-required-text"> <?php esc_html_e( 'Donation Amount', 'give' ); ?> </span> </li> <li class="give-import-donation-required-form" title="Please configure all required fields to start the import process."> <span class="give-import-donation-required-symbol dashicons dashicons-no-alt"></span> <span class="give-import-donation-required-text"> <?php esc_html_e( 'Form Title or ID', 'give' ); ?> </span> </li> </ul> <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> </th> </tr> <tr valign="top" class="give-import-dropdown"> <th><b><?php esc_html_e( 'Column name', 'give' ); ?></b></th> <th><b><?php esc_html_e( 'Map to field', 'give' ); ?></b></th> </tr> <?php $selectedOptions = []; // Initialize the tracking array for selected options $raw_key = $this->get_importer( $csv, 0, $delimiter ); $mapto = isset( $_REQUEST['mapto'] ) ? (array) map_deep( wp_unslash( $_REQUEST['mapto'] ), 'sanitize_text_field' ) : array(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- nonce verified by $this->is_nonce_valid() at the top of this method. foreach ( $raw_key as $index => $value ) { ?> <tr valign="middle" class="give-import-option"> <th><?php echo esc_html($value); ?></th> <th> <?php $this->get_columns( $index, $value, $mapto, $selectedOptions ); ?> </th> </tr> <?php } } } /** * @param $option_value * @param $value * * @return string */ public function selected( $option_value, $value ) { $option_value = strtolower( $option_value ); $value = strtolower( $value ); $selected = ''; if ( stristr( $value, $option_value ) ) { $selected = 'selected'; } elseif ( strrpos( $value, 'give_' ) && stristr( $option_value, __( 'Import as Meta', 'give' ) ) ) { $selected = 'selected'; } return $selected; } /** * Print the columns from the CSV. * * @since 4.19.1 Escape translated output. * @since 1.8.14 * @access private * * @param string $index * @param bool $value * @param array $mapto * @param array $selectedOptions * * @return void */ private function get_columns( $index, $value = false, $mapto = array(), &$selectedOptions = array() ) { $default = give_import_default_options(); $current_mapto = (string) ( ! empty( $mapto[ $index ] ) ? $mapto[ $index ] : '' ); ?> <select name="mapto[<?php echo esc_attr($index); ?>]"> <?php $this->get_dropdown_option_html( $default, $current_mapto, $value, $selectedOptions ); ?> <optgroup label="<?php esc_attr_e( 'Donations', 'give' ); ?>"> <?php $this->get_dropdown_option_html( give_import_donations_options(), $current_mapto, $value, $selectedOptions ); ?> </optgroup> <optgroup label="<?php esc_attr_e( 'Donors', 'give' ); ?>"> <?php $this->get_dropdown_option_html( give_import_donor_options(), $current_mapto, $value, $selectedOptions ); ?> </optgroup> <optgroup label="<?php esc_attr_e( 'Forms', 'give' ); ?>"> <?php $this->get_dropdown_option_html( give_import_donation_form_options(), $current_mapto, $value, $selectedOptions ); ?> </optgroup> <?php /** * Fire the action * You can use this filter to add new options. * * @since 1.8.15 */ do_action( 'give_import_dropdown_option', $index, $value, $mapto, $current_mapto ); ?> </select> <?php } /** * Print the option html for select in importer * * @since 1.8.15 * @access public * * @param array $options * @param string $current_mapto * @param bool $value * @param array $selectedOptions * * @return void */ public function get_dropdown_option_html( $options, $current_mapto, $value = false, &$selectedOptions = array() ) { foreach ( $options as $option => $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]; // Initialize selected as false $selected = false; // If current field matches option and it's not already selected, mark as selected if ( $current_mapto === $option && !in_array($option, $selectedOptions) ) { $selected = 'selected'; $selectedOptions[] = $option; // Add to selected options } else { // Check against option value texts if not already selected if ( ! in_array( $value, $ignore ) && !in_array($option, $selectedOptions) ) { foreach ( $option_value_texts as $option_value_text ) { $selected = $this->selected( $option_value_text, $value ); if ( $selected ) { $selectedOptions[] = $option; // Add to selected options break; } } } } ?> <option value="<?php echo esc_attr($option); ?>" <?php echo esc_html($selected); ?> ><?php echo esc_html($option_text); ?></option> <?php } } /** * 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 ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- reads only the header row of the uploaded CSV with fgetcsv(); WP_Filesystem has no file handle API. 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 4.19.1 Escape translated output. * @since 1.8.14 */ public function render_progress() { $step = $this->get_step(); ?> <ol class="give-progress-steps"> <li class="<?php echo esc_attr( 1 === $step ? 'active' : '' ); ?>"> <?php esc_html_e( 'Upload CSV file', 'give' ); ?> </li> <li class="<?php echo esc_attr( 2 === $step ? 'active' : '' ); ?>"> <?php esc_html_e( 'Column mapping', 'give' ); ?> </li> <li class="<?php echo esc_attr( 3 === $step ? 'active' : '' ); ?>"> <?php esc_html_e( 'Import', 'give' ); ?> </li> <li class="<?php echo esc_attr( 4 === $step ? 'active' : '' ); ?>"> <?php esc_html_e( 'Done!', 'give' ); ?> </li> </ol> <?php } /** * Will return the import step. * * @since 1.8.14 * * @return int $step on which step doest the import is on. */ public function get_step() { $step = (int) ( isset( $_REQUEST['step'] ) ? give_clean( $_REQUEST['step'] ) : 0 ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- read-only import step param; it only changes what is shown and saves nothing. give_clean() unslashes and sanitizes with sanitize_text_field(). $on_step = 1; if ( empty( $step ) || 1 === $step ) { $on_step = 1; } elseif ( $this->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 4.19.1 Escape translated output. * @since 2.1 */ public function give_import_donation_submit_button_render_media_csv() { $dry_run = isset( $_POST['dry_run'] ) ? absint( $_POST['dry_run'] ) : 1; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- read-only import step param; it only changes what is shown and saves nothing. ?> <div> <label for="dry_run"> <input type="hidden" name="dry_run" value="0"/> <input type="checkbox" name="dry_run" id="dry_run" class="dry_run" value="1" <?php checked( 1, $dry_run ); ?> > <strong><?php esc_html_e( 'Dry Run', 'give' ); ?></strong> </label> <p class="give-field-description"> <?php esc_html_e( 'Preview what the import would look like without making any default changes to your site or your database.', 'give' ); ?> </p> </div> <?php } /** * Change submit button text on first step of importing donation. * * @since 2.1 * * @param $text * * @return string */ function give_import_donation_submit_text_render_media_csv( $text ) { return __( 'Begin Import', 'give' ); } /** * Add CSV upload HTMl * * Print the html of the file upload from which CSV will be uploaded. * * @since 4.19.1 Escape translated output. * @since 1.8.14 * @return void */ public function render_media_csv() { add_filter( 'give_import_donation_submit_button_text', array( $this, 'give_import_donation_submit_text_render_media_csv', ) ); add_action( 'give_import_donation_submit_button', array( $this, 'give_import_donation_submit_button_render_media_csv', ) ); ?> <tr valign="top"> <th colspan="2"> <h2 id="give-import-title"><?php esc_html_e( 'Import donations from a CSV file', 'give' ); ?></h2> <p class="give-field-description"><?php esc_html_e( 'This tool allows you to import or add donation data to your give form(s) via a CSV file.', 'give' ); ?></p> </th> </tr> <?php $csv = ( isset( $_POST['csv'] ) ? give_clean( $_POST['csv'] ) : '' ); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- read-only import step param; it only changes what is shown and saves nothing. give_clean() unslashes and sanitizes with sanitize_text_field(). $csv_id = ( isset( $_POST['csv_id'] ) ? give_clean( $_POST['csv_id'] ) : '' ); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- read-only import step param; it only changes what is shown and saves nothing. give_clean() unslashes and sanitizes with sanitize_text_field(). $delimiter = ( isset( $_POST['delimiter'] ) ? give_clean( $_POST['delimiter'] ) : 'csv' ); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- read-only import step param; it only changes what is shown and saves nothing. give_clean() unslashes and sanitizes with sanitize_text_field(). $mode = empty( $_POST['mode'] ) ? // phpcs:ignore WordPress.Security.NonceVerification.Missing -- read-only import step param; it only changes what is shown and saves nothing. 'disabled' : ( give_is_setting_enabled( give_clean( $_POST['mode'] ) ) ? 'enabled' : 'disabled' ); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- read-only import step param; it only changes what is shown and saves nothing. give_clean() unslashes and sanitizes with sanitize_text_field(). $create_user = empty( $_POST['create_user'] ) ? // phpcs:ignore WordPress.Security.NonceVerification.Missing -- read-only import step param; it only changes what is shown and saves nothing. 'disabled' : ( give_is_setting_enabled( give_clean( $_POST['create_user'] ) ) ? 'enabled' : 'disabled' ); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- read-only import step param; it only changes what is shown and saves nothing. give_clean() unslashes and sanitizes with sanitize_text_field(). $delete_csv = empty( $_POST['delete_csv'] ) ? // phpcs:ignore WordPress.Security.NonceVerification.Missing -- read-only import step param; it only changes what is shown and saves nothing. 'enabled' : ( give_is_setting_enabled( give_clean( $_POST['delete_csv'] ) ) ? 'enabled' : 'disabled' ); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- read-only import step param; it only changes what is shown and saves nothing. give_clean() unslashes and sanitizes with sanitize_text_field(). // Reset csv and csv_id if csv if ( empty( $csv_id ) || ! $this->is_valid_csv( $csv_id, $csv ) ) { $csv_id = $csv = ''; } $per_page = isset( $_POST['per_page'] ) ? absint( $_POST['per_page'] ) : self::$per_page; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- read-only import step param; it only changes what is shown and saves nothing. $sample_file_text = sprintf( '%s <a href="%s">%s</a>.', __( '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 { ?> <input type="hidden" name="is_csv_valid" class="is_csv_valid" value="<?php echo esc_attr($this->is_csv_valid); ?>"> <?php } } /** * Run when user click on the submit button. * * @since 4.19.1 Unslash and sanitize the request input. * @since 1.8.14 */ public function save() { if (!$this->is_nonce_valid()){ wp_die(); } // Get the current step. $step = $this->get_step(); // Validation for first step. if ( 1 === $step ) { $csv_id = isset( $_POST['csv_id'] ) ? absint( $_POST['csv_id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce verified by $this->is_nonce_valid() at the top of this method. if ( $this->is_valid_csv( $csv_id, isset( $_POST['csv'] ) ? esc_url_raw( wp_unslash( $_POST['csv'] ) ) : '' ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce verified by $this->is_nonce_valid() at the top of this method. $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', // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- nonce verified by $this->is_nonce_valid() at the top of this method. give_clean() unslashes and sanitizes with sanitize_text_field(). 'mode' => empty( $_POST['mode'] ) ? // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce verified by $this->is_nonce_valid() at the top of this method. '0' : ( give_is_setting_enabled( give_clean( $_POST['mode'] ) ) ? '1' : '0' ), // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- nonce verified by $this->is_nonce_valid() at the top of this method. give_clean() unslashes and sanitizes with sanitize_text_field(). 'create_user' => empty( $_POST['create_user'] ) ? // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce verified by $this->is_nonce_valid() at the top of this method. '0' : ( give_is_setting_enabled( give_clean( $_POST['create_user'] ) ) ? '1' : '0' ), // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- nonce verified by $this->is_nonce_valid() at the top of this method. give_clean() unslashes and sanitizes with sanitize_text_field(). 'delete_csv' => empty( $_POST['delete_csv'] ) ? // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce verified by $this->is_nonce_valid() at the top of this method. '1' : ( give_is_setting_enabled( give_clean( $_POST['delete_csv'] ) ) ? '1' : '0' ), // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- nonce verified by $this->is_nonce_valid() at the top of this method. give_clean() unslashes and sanitizes with sanitize_text_field(). 'per_page' => isset( $_POST['per_page'] ) ? absint( $_POST['per_page'] ) : self::$per_page, // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce verified by $this->is_nonce_valid() at the top of this method. 'dry_run' => isset( $_POST['dry_run'] ) ? absint( $_POST['dry_run'] ) : 0, // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce verified by $this->is_nonce_valid() at the top of this method. ) ) ); $this->is_csv_valid = wp_nonce_url($url, 'give-save-settings', '_give-save-settings'); } } } /** * 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' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- read-only import step param; it only changes what is shown and saves nothing. give_clean() unslashes and sanitizes with sanitize_text_field(). 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'] ) && // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only import step param; it only changes what is shown and saves nothing. $this->importer_type === give_clean( $_GET['importer-type'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- read-only import step param; it only changes what is shown and saves nothing. give_clean() unslashes and sanitizes with sanitize_text_field(). } /** * @since 4.19.1 Unslash and sanitize the nonce before checking it. * @since 2.25.2 */ private function is_nonce_valid() { return !empty($_REQUEST['_give-save-settings']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_give-save-settings'])), 'give-save-settings'); } } Give_Import_Donations::get_instance()->setup(); }
Save File
Cancel