/** * Process the forms export request. */ public static function maybe_export() { if ( isset( $_POST['export_forms'] ) ) { check_admin_referer( 'gf_export_forms', 'gf_export_forms_nonce' ); $selected_forms = rgpost( 'gf_form_id' ); if ( empty( $selected_forms ) ) { GFCommon::add_error_message( __( 'Please select the forms to be exported', 'gravityforms' ) );
return; }
self::export_forms( $selected_forms ); } }
public static function export_forms( $form_ids ) {
case 'export_entry': self::export_lead_page(); break;
case 'import_form' : self::import_form_page(); break;
case 'export_form' : self::export_form_page(); break;
default: /** * Fires when export pages are gathered * * Used to add additional export settings pages * * @param string $view Set when defining the action string. Creates the name for the new page */ do_action( "gform_export_page_{$view}" ); break;
}
}
public static function import_file( $filepath, &$forms = null ) { $file_contents = file_get_contents( $filepath );
if ( ! $forms ) { GFCommon::log_debug( __METHOD__ . '(): Import Failed. Invalid form objects.' );
return 0; } else if ( ! rgar( $forms, 'version' ) || version_compare( $forms['version'], self::$min_import_version, '<' ) ) { GFCommon::log_debug( __METHOD__ . '(): Import Failed. The JSON version is not compatible with the current Gravity Forms version.' );
return - 1; } //Error. JSON version is not compatible with current Gravity Forms version
if ( is_wp_error( $form_ids ) ) { GFCommon::log_debug( __METHOD__ . '(): Import Failed => ' . print_r( $form_ids, 1 ) ); $form_ids = array(); } else { foreach ( $form_ids as $key => $form_id ) { $forms[ $key ] = GFAPI::get_form( $form_id ); } /** * Fires after forms have been imported. * * Used to perform additional actions after import * * @param array $forms An array imported form objects. * */ do_action( 'gform_forms_post_import', $forms ); }
return sizeof( $form_ids ); }
/** * Removes any extraneous strings from the begining of the JSON file to be imported. * * @since 2.5.16 * * @param string $forms_json Exported form JSON to be sanitized. * * @return string Sanitized JSON string. */ public static function sanitize_forms_json( $forms_json ) {
// Remove any whitespace from before and after the JSON. $forms_json = trim( $forms_json );
// Remove any characters before the beginning of the JSON string. if ( preg_match( '/{\s*"\d"\s*:\s*{/', $forms_json, $matches, PREG_OFFSET_CAPTURE ) ) { $forms_json = substr( $forms_json, $matches[0][1] ); }
return $forms_json; }
// This function is not deprecated as of 1.9 because it will still be needed for a while to import legacy XML files without generating deprecation notices. // However, XML is not used to export Forms so this function will soon be deprecated. public static function import_xml( $xmlstr, &$forms = null ) {
if ( ! $forms ) { return 0; } //Error. could not unserialize XML file else if ( version_compare( $forms['version'], self::$min_import_version, '<' ) ) { return - 1; } //Error. XML version is not compatible with current Gravity Forms version
//cleaning up generated object self::cleanup( $forms );
private static function cleanup( &$forms ) { unset( $forms['version'] );
//adding checkboxes 'inputs' property based on 'choices'. (they were removed from the export //to provide a cleaner xml format foreach ( $forms as &$form ) { if ( ! is_array( $form['fields'] ) ) { continue; } $form = GFFormsModel::convert_field_objects( $form );
if ( ! empty( $_FILES['gf_import_file']['tmp_name'][0] ) ) {
// Set initial count to 0. $count = 0;
// Loop through each uploaded file. foreach ( $_FILES['gf_import_file']['tmp_name'] as $import_file ) { $count += self::import_file( $import_file, $forms ); }
if ( $count == 0 ) { $error_message = sprintf( esc_html__( 'Forms could not be imported. Please make sure your files have the .json extension, and that they were generated by the %sGravity Forms Export form%s tool.', 'gravityforms' ), '<a href="admin.php?page=gf_export&view=export_form">', '</a>' ); GFCommon::add_error_message( $error_message ); } else if ( $count == '-1' ) { GFCommon::add_error_message( esc_html__( 'Forms could not be imported. Your export file is not compatible with your current version of Gravity Forms.', 'gravityforms' ) ); } else { $form_text = $count > 1 ? esc_html__( 'forms', 'gravityforms' ) : esc_html__( 'form', 'gravityforms' ); $edit_link = $count == 1 ? "<a href='admin.php?page=gf_edit_forms&id={$forms[0]['id']}'>" . esc_html__( 'Edit Form', 'gravityforms' ) . '</a>' : ''; GFCommon::add_message( sprintf( esc_html__( 'Gravity Forms imported %d %s successfully', 'gravityforms' ), $count, $form_text ) . ". $edit_link" ); } } } self::page_header(); ?> <div class="gform-settings__content"> <form method="post" enctype="multipart/form-data" class="gform_settings_form"> <?php wp_nonce_field( 'gf_import_forms', 'gf_import_forms_nonce' ); ?> <div class="gform-settings-panel gform-settings-panel--full"> <header class="gform-settings-panel__header"><legend class="gform-settings-panel__title"><?php esc_html_e('Import Forms', 'gravityforms'); ?></legend></header> <div class="gform-settings-panel__content"> <div class="gform-settings-description"> <?php echo sprintf( esc_html__( 'Select the Gravity Forms export files you would like to import. Please make sure your files have the .json extension, and that they were generated by the %sGravity Forms Export form%s tool. When you click the import button below, Gravity Forms will import the forms.', 'gravityforms' ), '<a href="admin.php?page=gf_export&view=export_form">', '</a>' ); ?> </div> <table class="form-table"> <tr valign="top">
<div class="gform-settings__content"> <form method="post" id="gform_export" class="gform_settings_form"> <?php wp_nonce_field( 'gf_export_forms', 'gf_export_forms_nonce' ); ?> <div class="gform-settings-panel gform-settings-panel--full"> <header class="gform-settings-panel__header"><legend class="gform-settings-panel__title"><?php esc_html_e( 'Export Forms', 'gravityforms' )?></legend></header> <div class="gform-settings-panel__content"> <div class="gform-settings-description"> <?php esc_html_e( 'Select the forms you would like to export. When you click the download button below, Gravity Forms will create a JSON file for you to save to your computer. Once you\'ve saved the download file, you can use the Import tool to import the forms.', 'gravityforms' ); ?> </div> <table class="form-table"> <tr valign="top"> <th scope="row"> <label for="export_fields"><?php esc_html_e( 'Select Forms', 'gravityforms' ); ?></label> <?php gform_tooltip( 'export_select_forms' ) ?> </th> <td> <ul id="export_form_list"> <li> <input type="checkbox" id="gf_export_forms_all" /> <label for="gf_export_forms_all" data-deselect="<?php esc_attr_e( 'Deselect All', 'gravityforms' ); ?>" data-select="<?php esc_attr_e( 'Select All', 'gravityforms' ); ?>"><?php esc_html_e( 'Select All', 'gravityforms' ); ?></label> </li> <?php $forms = RGFormsModel::get_forms( null, 'title' );
/** * Modify list of forms available for export. * * @since 2.4.7 * * @param array $forms Forms to display on Export Forms page. */ $forms = apply_filters( 'gform_export_forms_forms', $forms );
/** * Checks if form ids are provided in query to be automatically exported. * * This method checks the checkboxes of the desired forms and simulates a click on the submit button. * * @since 2.6.2 * * @return void */ public static function maybe_process_automated_export() { $export_ids = rgget( 'export_form_ids' ); $automatic_submit = rgpost( 'gform_automatic_submit' ); if ( $export_ids && ! $automatic_submit ) { ?> <script> jQuery( document ).ready( function () { var export_ids = <?php echo json_encode( $export_ids ); ?>; var clickSubmit = false; export_ids.split(',').forEach( ( id ) => { var formCheckbox = jQuery( '#gf_form_id_' + id ); if( formCheckbox.length ) { formCheckbox.prop( 'checked', true ); clickSubmit = true; } });
gf_vars.filterAndAny = <?php echo json_encode( esc_html__( 'Export entries if {0} of the following match:', 'gravityforms' ) ); ?>; jQuery("#export_filters").gfFilterUI(filterSettings); }
( function( $, window, undefined ) {
$(document).ready(function() { $("#submit_button").click(function () { if ($(".gform_export_field:checked").length == 0) { alert(<?php echo json_encode( __( 'Please select the fields to be exported', 'gravityforms' ) ); ?>); return false; }
<div class="gform-settings__content"> <form method="post" id="gform_export" class="gform_settings_form" data-js="page-loader"> <?php echo wp_nonce_field( 'rg_start_export', 'rg_start_export_nonce' ); ?> <div class="gform-settings-panel gform-settings-panel--full"> <header class="gform-settings-panel__header"><legend class="gform-settings-panel__title"><?php esc_html_e( 'Export Entries', 'gravityforms' ) ;?></legend></header> <div class="gform-settings-panel__content"> <div class="gform-settings-description"> <?php esc_html_e( 'Select a form below to export entries. Once you have selected a form you may select the fields you would like to export and then define optional filters for field values and the date range. When you click the download button below, Gravity Forms will create a CSV file for you to save to your computer.', 'gravityforms' ); ?> </div> <table class="form-table"> <tr valign="top">
/** * Modify list of forms available to export entries from. * * @since 2.4.7 * * @param array $forms Forms to display on Export Entries page. */ $forms = apply_filters( 'gform_export_entries_forms', $forms );
if ( version_compare( GFFormsModel::get_database_version(), '2.3-dev-1', '<' ) ) { $sql = $wpdb->prepare( "SELECT d.field_number as field_id, d.value as value FROM {$wpdb->prefix}rg_lead_detail d WHERE d.form_id=%d AND cast(d.field_number as decimal) IN ({$field_ids}) LIMIT %d, %d", $form['id'], $offset, $page_size ); } else { $sql = $wpdb->prepare( "SELECT d.meta_key as field_id, d.meta_value as value FROM {$wpdb->prefix}gf_entry_meta d WHERE d.form_id=%d AND d.meta_key IN ({$field_ids}) LIMIT %d, %d", $form['id'], $offset, $page_size ); }
public static function start_export( $form, $offset = 0, $export_id = '' ) {
$time_start = microtime( true );
/*** * Allows the export max execution time to be changed. * * When the max execution time is reached, the export routine stop briefly and submit another AJAX request to continue exporting entries from the point it stopped. * * @since 2.0.3.10 * * @param int 20 The amount of time, in seconds, that each request should run for. Defaults to 20 seconds. * @param array $form The Form Object */ $max_execution_time = apply_filters( 'gform_export_max_execution_time', 20, $form ); // seconds $page_size = 20;
/** * Allows the search criteria to be filtered before exporting entries. * * @since 2.7 * * @param array $search_criteria The search criteria array being filtered. * @param int $form_id The current form ID. */ $search_criteria = apply_filters( 'gform_search_criteria_export_entries', $search_criteria, $form_id );
if ( $offset == 0 ) { GFCommon::log_debug( __METHOD__ . '(): Processing request for form #' . $form_id );
/** * Allows the BOM character to be excluded from the beginning of entry export files. * * @since 2.1.1.21 * * @param bool $include_bom Whether or not to include the BOM characters. Defaults to true. * @param array $form The Form Object. */ $include_bom = apply_filters( 'gform_include_bom_export_entries', true, $form );
foreach ( $leads as $lead ) { $line = self::get_entry_export_line( $lead, $form, $fields, $field_rows, $separator ); /** * Filter the current line being exported. * * @since 2.4.11.5 * * @param string $line The current line being exported. * @param array $form The current form object. * @param array $fields An array of field IDs to be exported. * @param array $field_rows An array of List fields * @param array $entry The current entry. * @param string $separator The separator */ $line = apply_filters( 'gform_export_line', $line, $form, $fields, $field_rows, $lead, $separator ); $lines .= "$line\n"; }
if ( $execution_time >= $max_execution_time ) { break; }
$lines = ''; }
$complete = $remaining_entry_count <= 0;
if ( $complete ) { /** * Fires after exporting all the entries in form * * @since 2.4.5.11 Added the $export_id param. * @since 1.9.3 * * @param array $form The Form object to get the entries from * @param string $start_date The start date for when the export of entries should take place * @param string $end_date The end date for when the export of entries should stop * @param array $fields The specified fields where the entries should be exported from * @param string $export_id A unique ID for the export. */ do_action( 'gform_post_export_entries', $form, $start_date, $end_date, $fields, $export_id ); }
/** * Returns the content to be included in the export for the supplied entry. * * @since 2.4.5.11 * * @param array $entry The entry being exported. * @param array $form The form associated with the current entry. * @param array $fields The IDs of the fields to be exported. * @param array $field_rows An array of List fields. * @param string $separator The character to be used as the column separator. * * @return string */ public static function get_entry_export_line( $entry, $form, $fields, $field_rows, $separator ) { GFCommon::log_debug( __METHOD__ . '(): Processing entry #' . $entry['id'] );
/** * Handles the export request from the export entries page. * * @since 2.0.0 */ public static function ajax_process_export() { check_admin_referer( 'rg_start_export', 'rg_start_export_nonce' );
if ( ! GFCommon::current_user_can_any( 'gravityforms_export_entries' ) ) { die(); }
/** * Appends lines to to the csv file for the given Export ID. * * @param string $lines * @param string $export_id A unique ID for the export. */ public static function write_file( $lines, $export_id ) {
GFCommon::log_debug( __METHOD__ . '(): Writing to file.' ); $result = file_put_contents( $file, $lines, FILE_APPEND ); if ( $result === false ) { GFCommon::log_error( __METHOD__ . '(): An issue occurred whilst writing to the file.' ); } else { GFCommon::log_debug( __METHOD__ . '(): Number of bytes written to the file: ' . print_r( $result, 1 ) ); }
}
/** * Creates an .htaccess file in the given path which will disable access to all files on Apache Web Servers. * * @since 2.0.0 * * @param $path */ public static function maybe_create_htaccess_file( $path ) { $htaccess_file = $path . '.htaccess'; if ( file_exists( $htaccess_file ) ) { return; } $txt = '# Disable access to files via Apache web servers. deny from all'; $rules = explode( "\n", $txt );
/** * Adds an empty index file in the given path if it doesn't exist already. * * @since 2.0.0 * * @param $path */ public static function maybe_create_index_file( $path ) { $path = untrailingslashit( $path ); $index_file = $path . '/index.html'; if ( file_exists( $index_file ) ) { return; } GFCommon::recursive_add_index_file( $path ); }
/** * Handles the download request from the export entries page. * * @since 2.0.0 */ public static function ajax_download_export() { check_admin_referer( 'gform_download_export' );
if ( ! function_exists( 'readfile' ) ) { GFCommon::log_error( __METHOD__ . '(): Aborting. The PHP readfile function is not available.' ); die( esc_html__( 'The PHP readfile function is not available, please contact the web host.', 'gravityforms' ) ); }
if ( ! GFCommon::current_user_can_any( 'gravityforms_export_entries' ) ) { die(); }
/** * Allows you to filter and modify the Export Form * * @param array $form Assign which Gravity Form to change the export form for */ $form = gf_apply_filters( array( 'gform_export_form', $form['id'] ), $form );