setup_hooks(); } /** * Setup Hooks. * * @since 1.8.17 * * @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_core_settings_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_core_settings_form_end', array( $this, 'submit' ), 10 ); } /** * Update notice * * @since 1.8.17 * * @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.17 */ public function submit() { wp_nonce_field( 'give-save-settings', '_give-save-settings' ); ?> get_step(); // Show progress. $this->render_progress(); ?>
is_json_valid ) ? 'give-hidden' : '' ); ?> " id=""> render_upload_html(); break; case 2: $this->start_import(); break; case 3: $this->import_success(); } ?>
'give_forms', 'page' => 'give-settings', ); if ( $undo ) { $success = false; } $query_arg_success = array( 'post_type' => 'give_forms', 'page' => 'give-tools', 'tab' => 'import', 'importer-type' => 'import_core_setting', 'step' => '1', 'undo' => 'true', ); $title = __( 'Settings Import Complete!', 'give' ); if ( $success ) { $query_arg_success['undo'] = '1'; $query_arg_success['step'] = '3'; $query_arg_success['success'] = '1'; $text = __( 'Undo Importing', 'give' ); } else { if ( $undo ) { $host_give_options = get_option( 'give_settings_old', array() ); update_option( 'give_settings', $host_give_options, false ); $title = __( 'Successfully Reverted Settings Import', 'give' ); } else { $title = __( 'Failed to Import', 'give' ); } $text = __( 'Import Again', 'give' ); } ?>

get_step(); ?>
get_step(); ?>

'type', 'name' => __( 'Merge Type:', 'give' ), 'description' => __( 'Select "Merge" to retain existing settings, or "Replace" to overwrite with the settings from the JSON file', 'give' ), 'default' => $type, 'type' => 'radio_inline', 'options' => array( 'merge' => __( 'Merge', 'give' ), 'replace' => __( 'Replace', 'give' ), ), ), ); $settings = apply_filters( 'give_import_core_setting_html', $settings ); if ( empty( $this->is_json_valid ) ) { Give_Admin_Settings::output_fields( $settings, 'give_settings' ); ?> " id="recount-stats-submit" value=""/> get_step(); // Validation for first step. if ( 1 === $step ) { $type = ( ! empty( $_REQUEST['type'] ) ? give_clean( $_REQUEST['type'] ) : 'replace' ); $core_settings = self::upload_widget_settings_file(); if ( ! empty( $core_settings['error'] ) ) { Give_Admin_Settings::add_error( 'give-import-csv', __( 'Please upload a valid JSON settings file.', 'give' ) ); } else { $file_path = explode( '/', $core_settings['file'] ); $count = ( count( $file_path ) - 1 ); $url = give_import_page_url( (array) apply_filters( 'give_import_core_settings_importing_url', array( 'step' => '2', 'importer-type' => $this->importer_type, 'type' => $type, 'file_name' => $file_path[ $count ], ) ) ); $this->is_json_valid = $url; } } } /** * Get if current page import donations page or not * * @since 1.8.17 * @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'] ); } /** * Upload JSON file * * @return boolean */ public static function upload_widget_settings_file() { $upload = false; if ( isset( $_FILES['json'] ) ) { add_filter( 'upload_mimes', array( __CLASS__, 'json_upload_mimes' ) ); add_filter( 'wp_check_filetype_and_ext', array( __CLASS__, 'filetype_mod' ), 10, 4 ); $upload = wp_handle_upload( $_FILES['json'], array( 'test_form' => false ) ); remove_filter( 'upload_mimes', array( __CLASS__, 'json_upload_mimes' ) ); remove_filter( 'wp_check_filetype_and_ext', array( __CLASS__, 'filetype_mod' ) ); } else { Give_Admin_Settings::add_error( 'give-import-csv', __( 'Please upload or provide a valid JSON file.', 'give' ) ); } return $upload; } /** * Add mime type for JSON * * @param array $existing_mimes * * @return array */ public static function json_upload_mimes( $existing_mimes = array() ) { $existing_mimes['json'] = 'application/json'; $existing_mimes['text'] = 'text/plain'; return $existing_mimes; } /** * Allow for json file type uploads. * * https://github.com/impress-org/give/issues/3907 * * @param $check * @param $file * @param $filename * @param $mimes * * @return mixed */ public static function filetype_mod( $check, $file, $filename, $mimes ) { if ( empty( $check['ext'] ) && empty( $check['type'] ) ) { $finfo = finfo_open( FILEINFO_MIME_TYPE ); $real_mime = finfo_file( $finfo, $file ); finfo_close( $finfo ); if ( in_array( $real_mime, array( 'text/plain', 'text/html' ) ) ) { remove_filter( 'wp_check_filetype_and_ext', array( __CLASS__, 'filetype_mod' ) ); // Allow JSON uploads $secondary_mime = array( 'json' => $real_mime ); // Run another check, but only for our secondary mime and not on core mime types. $check = wp_check_filetype_and_ext( $file, $filename, $secondary_mime ); add_filter( 'wp_check_filetype_and_ext', array( __CLASS__, 'filetype_mod' ), 10, 4 ); } } return $check; } } Give_Import_Core_Settings::get_instance()->setup(); }