path_csv = $upload_dir['basedir'] . "/export-users.csv"; add_action( 'init', array( $this, 'download_export_file' ), 11 ); add_action( 'admin_init', array( $this, 'download_export_file' ) ); add_action( 'wp_ajax_acui_export_users_csv', array( $this, 'export_users_csv' ) ); add_action( 'wp_ajax_acui_export_save_settings', array( $this, 'ajax_save_settings' ) ); } static function enqueue(){ wp_enqueue_script( 'acui_export_js', plugins_url( 'assets/export.js', dirname( __FILE__ ) ), false, ACUI_VERSION, true ); wp_localize_script( 'acui_export_js', 'acui_export_js_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'starting_process' => __( 'Starting process', 'import-users-from-csv-with-meta' ), 'step' => __( 'Step', 'import-users-from-csv-with-meta' ), 'of_approximately' => __( 'of approximately', 'import-users-from-csv-with-meta' ), 'steps' => __( 'steps', 'import-users-from-csv-with-meta' ), 'error_thrown' => __( 'Error thrown on the server, cannot continue. Please check console to see full details about the error.', 'import-users-from-csv-with-meta' ), ) ); } static function styles(){ ?>
select( array( 'options' => ACUI_Helper::get_editable_roles( false ), 'name' => 'role[]', 'show_option_all' => false, 'show_option_none' => false, 'multiple' => true, 'selected' => is_array( $settings->get( 'role' ) ) ? $settings->get( 'role' ) : array( $settings->get( 'role' ) ), 'style' => 'width:100%;' )); ?>
textarea( array( 'name' => 'columns', 'value' => $settings->get( 'columns' ), 'style' => 'width:100%;' ) ); ?> : user_email,first_name,last_name
: user_email=>Email,first_name=>First name,last_name=>Last name
select( array( 'options' => ACUI_Helper::get_csv_delimiters_titles(), 'name' => 'delimiter', 'show_option_all' => false, 'show_option_none' => false, 'selected' => $settings->get( 'delimiter' ) )); ?>
checkbox( array( 'name' => 'convert_timestamp', 'current' => 'yes', 'compare_value' => $settings->get( 'convert_timestamp' ) ) ); ?> text( array( 'name' => 'datetime_format', 'value' => 'Y-m-d H:i:s', 'class' => '', 'value' => $settings->get( 'datetime_format' ) ) ); ?>
checkbox( array( 'name' => 'order_fields_alphabetically', 'current' => 'yes', 'compare_value' => $settings->get( 'order_fields_alphabetically' ) ) ); ?>
checkbox( array( 'name' => 'double_encapsulate_serialized_values', 'current' => 'yes', 'compare_value' => $settings->get( 'double_encapsulate_serialized_values' ) ) ); ?>
checkbox( array( 'name' => 'display_arrays_as_comma_separated_list_of_values', 'current' => 'yes', 'compare_value' => $settings->get( 'display_arrays_as_comma_separated_list_of_values' ) ) ); ?>
0%

set_filename( wp_unslash( $_GET['filename'] ) ); } $exporter->export(); } } function get_results(){ $bad_character_formulas_values_cleaned = get_transient( 'acui_export_bad_character_formulas_values_cleaned' ); delete_transient( 'acui_export_bad_character_formulas_values_cleaned' ); if( empty( $bad_character_formulas_values_cleaned ) ){ return ''; } $results = array(); foreach( $bad_character_formulas_values_cleaned as $info ){ $results[] = sprintf( __( 'User with id: %s. The cell in column %s has been edited because its content may contain formulas that are automatically run in certain spreadsheet apps. The new value is: %s', 'import-users-from-csv-with-meta' ), $info['user_id'], $info['key'], $info['value'] ); } $ret = '

' . __( 'Export results','import-users-from-csv-with-meta' ) . '

'; $ret .= '

' . __( 'Some values have been altered','import-users-from-csv-with-meta' ) . '

'; $ret .= ''; return $ret; } function export_users_csv(){ check_ajax_referer( 'codection-security', 'security' ); if( !current_user_can( apply_filters( 'acui_capability', 'create_users' ) ) ) wp_die( __( 'Only users who are allowed to create users can export them.', 'import-users-from-csv-with-meta' ) ); $step = isset( $_POST['step'] ) ? absint( $_POST['step'] ) : 1; $exporter = new ACUI_Batch_Exporter(); if( isset( $_POST['role'] ) ){ if( !is_array( $_POST['role'] ) ) $_POST['role'] = explode( ',', $_POST['role'] ); if( !array_filter( $_POST['role'] ) ) unset( $_POST['role'] ); } $role = isset($_POST['role']) ? $_POST['role'] : array(); if( !is_array( $role ) ){ $role = array( $role ); } $exporter->set_role( isset( $_POST['role'] ) ? array_map( 'sanitize_text_field', $role ) : '' ); $exporter->set_from( isset( $_POST['from'] ) ? sanitize_text_field( $_POST['from'] ) : '' ); $exporter->set_to( isset( $_POST['to'] ) ? sanitize_text_field( $_POST['to'] ) : '' ); $exporter->fill_total_rows( $step == 1 ); if( $step == 1 ){ delete_transient( 'acui_export_bad_character_formulas_values_cleaned' ); $this->save_settings(); } if( isset( $_POST['filename'] ) && !empty( $_POST['filename'] ) ) $exporter->set_filename( sanitize_file_name( $_POST['filename'] ) ); $exporter->set_page( $step ); $exporter->set_delimiter( isset( $_POST['delimiter'] ) ? sanitize_text_field( $_POST['delimiter'] ) : '' ); $exporter->set_convert_timestamp( isset( $_POST['convert_timestamp'] ) ? $_POST['convert_timestamp'] : '' ); $exporter->set_datetime_format( isset( $_POST['datetime_format'] ) ? sanitize_text_field( $_POST['datetime_format'] ) : '' ); $exporter->set_order_fields_alphabetically( isset( $_POST['order_fields_alphabetically'] ) ? $_POST['order_fields_alphabetically'] : '' ); $exporter->set_double_encapsulate_serialized_values( isset( $_POST['double_encapsulate_serialized_values'] ) ? $_POST['double_encapsulate_serialized_values'] : '' ); $exporter->set_display_arrays_as_comma_separated_list_of_values( isset( $_POST['display_arrays_as_comma_separated_list_of_values'] ) ? $_POST['display_arrays_as_comma_separated_list_of_values'] : '' ); $exporter->set_filtered_columns( ( isset( $_POST['columns'] ) && !empty( $_POST['columns'] ) ) ? $_POST['columns'] : array() ); $exporter->set_orderby( ( isset( $_POST['orderby'] ) && !empty( $_POST['orderby'] ) ) ? sanitize_text_field( $_POST['orderby'] ) : '' ); $exporter->set_order( ( isset( $_POST['order'] ) && !empty( $_POST['order'] ) ) ? sanitize_text_field( $_POST['order'] ) : 'ASC' ); $exporter->load_columns(); $exporter->generate_file(); if ( 100 <= $exporter->get_percent_complete() ) { $query_args = array( 'nonce' => wp_create_nonce( 'codection-security' ), 'action' => 'download_user_csv', 'filename' => $exporter->get_filename() ); $current_url = isset( $_POST['current_url'] ) ? sanitize_text_field( $_POST['current_url'] ) : admin_url( 'tools.php?page=acui&tab=export' ); wp_send_json_success( array( 'step' => 'done', 'percentage' => 100, 'url' => add_query_arg( $query_args, $current_url ), 'results' => $this->get_results(), 'filename' => $exporter->get_filename() ) ); } else { wp_send_json_success( array( 'step' => ++$step, 'total_steps' => $exporter->get_total_steps(), 'percentage' => $exporter->get_percent_complete(), 'filename' => $exporter->get_filename() ) ); } } function ajax_save_settings(){ check_ajax_referer( 'codection-security', 'security' ); if( !current_user_can( apply_filters( 'acui_capability', 'create_users' ) ) ) wp_die( __( 'Only users who are able to create users can save settings about exporting them.', 'import-users-from-csv-with-meta' ) ); $this->save_settings(); wp_send_json_success( array( 'message' => __( 'Settings saved', 'import-users-from-csv-with-meta' ), ) ); } function save_settings(){ $settings = array(); isset( $_POST['settings'] ) ? parse_str( $_POST['settings'], $settings) : parse_str( $_POST['form'], $settings ); ACUISettings()->save_multiple( 'export_backend', $settings ); } } global $acui_exporter; $acui_exporter = new ACUI_Exporter();