diff --git a/experimental/gpeb-export-entries-csv.php b/experimental/gpeb-export-entries-csv.php new file mode 100644 index 000000000..90847fdc1 --- /dev/null +++ b/experimental/gpeb-export-entries-csv.php @@ -0,0 +1,131 @@ +name !== 'gp-entry-blocks/entries-table' ) { + return; + } + + global $post; + + $export_url = add_query_arg( array( + 'export_block_entries' => $block->context['gp-entry-blocks/uuid'], + 'export_block_entries_nonce' => wp_create_nonce( $block->context['gp-entry-blocks/uuid'] ), + 'block_type' => $block->context['gp-entry-blocks/uuid'], + ), \GP_Entry_Blocks\get_current_url() ); + + echo 'Download Entries as CSV'; +} ); + +add_action( 'template_redirect', function() { + $block_uuid = rgget( 'export_block_entries' ); + + if ( ! $block_uuid ) { + return; + } + + if ( ! wp_verify_nonce( rgget( 'export_block_entries_nonce' ), $block_uuid ) ) { + return; + } + + $parsed_entries_block = \GP_Entry_Blocks\get_parsed_block_by_uuid( $block_uuid, 'gp-entry-blocks/entries' ); + $parsed_entries_table_block = \GP_Entry_Blocks\get_parsed_block_by_uuid( $block_uuid, 'gp-entry-blocks/entries-table' ); + + if ( ! $parsed_entries_block || ! $parsed_entries_table_block ) { + return; + } + + $block = new WP_Block( $parsed_entries_block, $parsed_entries_block['availableContext'] ); + + // Use $block->attributes with the keys prefixed with "gp-entry-blocks/" as the context. + $context = array_combine( + array_map( function( $key ) { + return 'gp-entry-blocks/' . $key; + }, array_keys( $block->attributes ) ), + $block->attributes + ); + + // @todo Wire up to `gform_search_criteria_export_entries` + $queryer = \GP_Entry_Blocks\GF_Queryer::attach( $context ); + + require_once( GFCommon::get_base_path() . '/export.php' ); + + $form = GFAPI::get_form( $block->attributes['formId'] ); + + // Make unique to GPEB. + $export_id = wp_hash( uniqid( 'export', true ) ); + $export_id = sanitize_key( $export_id ); + + // Use the summary columns from the Entries Table block in the export. + $summary_columns = $parsed_entries_table_block['attrs']['formFields']; + + $_POST['export_field'] = array(); + + foreach ( $summary_columns as $summary_column ) { + switch ( $summary_column['type'] ) { + case 'id': + $_POST['export_field'][] = $summary_column['type']; + break; + + case 'field': + $_POST['export_field'][] = $summary_column['meta']['fieldId']; + break; + } + } + + /** + * @var array{ + * status: 'complete'|'in_progress', + * offset: int, + * exportId: string, + * progress: string, + * } $export_result + */ + // Note, the way this works is it'll only export for 20 seconds (or whatever is set in `gform_export_max_execution_time`). + // Anything after that will not be included in the export. + $export_result = GFExport::start_export( $form, 0, $export_id ); + + $filename = sanitize_title_with_dashes( $form['title'] ) . '-' . gmdate( 'Y-m-d', GFCommon::get_local_timestamp( time() ) ) . '.csv'; + $export_folder = RGFormsModel::get_upload_root() . 'export/'; + $file = $export_folder . sanitize_file_name( 'export-' . $export_id . '.csv' ); + + $charset = get_option( 'blog_charset' ); + header( 'Content-Description: File Transfer' ); + header( "Content-Disposition: attachment; filename=$filename" ); + header( 'Content-Type: text/csv; charset=' . $charset, true ); + $buffer_length = ob_get_length(); //length or false if no buffer + if ( $buffer_length > 1 ) { + ob_clean(); + } + + $result = readfile( $file ); + + if ( $result === false ) { + GFCommon::log_error( __METHOD__ . '(): An issue occurred whilst reading the file.' ); + } else { + @unlink( $file ); + GFCommon::log_debug( __METHOD__ . '(): Number of bytes read from the file: ' . print_r( $result, 1 ) ); + } + + die(); +} );