Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save goodyis/cae717e8db3abbb6f3f19b41ea445fd4 to your computer and use it in GitHub Desktop.
Save goodyis/cae717e8db3abbb6f3f19b41ea445fd4 to your computer and use it in GitHub Desktop.
Snippet for programmatically creating entries in WPForms.
<?php
/*
Plugin Name: CSV to WPForms Entry
Description: Process a CSV file from the plugin directory to create WPForms entries.
Version: 2.0
Author: Your Name
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
// Register the admin menu page
add_action( 'admin_menu', 'csv_to_wpforms_menu' );
function csv_to_wpforms_menu() {
add_menu_page(
'CSV to WPForms Entry',
'CSV to WPForms',
'manage_options',
'csv-to-wpforms',
'csv_to_wpforms_page',
'dashicons-upload',
76
);
}
// Display the form and handle CSV processing
function csv_to_wpforms_page() {
// Fetch available WPForms forms
$forms = get_wpforms_forms();
?>
<div class="wrap">
<h1>Process CSV to Create WPForms Entries</h1>
<form method="post">
<input type="text" name="csv_file_name" placeholder="Enter CSV file name" required />
<select name="form_id" required>
<option value="">Select a Form</option>
<?php foreach ( $forms as $form ) : ?>
<option value="<?php echo esc_attr( $form['id'] ); ?>"><?php echo esc_html( $form['name'] ); ?></option>
<?php endforeach; ?>
</select>
<button type="submit" name="submit_csv" class="button button-primary">Process Import</button>
</form>
<div id="log" style="margin-top: 20px; padding: 10px; border: 1px solid #ccc; height: 200px; overflow-y: scroll;">
<strong>Status Log:</strong>
<ul>
<?php handle_csv_processing(); ?>
</ul>
</div>
</div>
<?php
}
// Fetch available WPForms forms
function get_wpforms_forms() {
if ( ! function_exists( 'wpforms' ) ) {
return array();
}
$forms = wpforms()->form->get( '', array(
'orderby' => 'ID',
'order' => 'ASC',
) );
$form_list = array();
if ( $forms ) {
foreach ( $forms as $form ) {
$form_list[] = array(
'id' => $form->ID,
'name' => $form->post_title,
);
}
}
return $form_list;
}
// Handle the CSV processing and WPForms entry creation
function handle_csv_processing() {
if ( isset( $_POST['submit_csv'] ) && isset( $_POST['csv_file_name'] ) && isset( $_POST['form_id'] ) ) {
// Start logging
echo '<li>Processing started...</li>';
// Check if WPForms is active
if ( ! function_exists( 'wpforms' ) ) {
echo '<li style="color: red;">Error: WPForms is not active or installed.</li>';
error_log("Error: WPForms is not active or installed.");
return;
}
$csv_file_name = sanitize_text_field( $_POST['csv_file_name'] );
$csv_file_path = plugin_dir_path( __FILE__ ) . $csv_file_name;
$form_id = absint( $_POST['form_id'] );
$user_id = get_current_user_id();
$user_ip = $_SERVER['REMOTE_ADDR'];
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$date = current_time('mysql');
$user_uuid = wp_generate_uuid4();
// Log CSV file details
echo '<li>CSV File Name: ' . esc_html( $csv_file_name ) . '</li>';
echo '<li>Form ID: ' . esc_html( $form_id ) . '</li>';
// Check if the file exists
if ( ! file_exists( $csv_file_path ) ) {
echo '<li style="color: red;">Error: CSV file not found in the plugin directory.</li>';
error_log("Error: CSV file not found - " . $csv_file_path);
return;
}
else {
echo '<li style="color: blue;">FOUND: CSV file found in the plugin directory.' . $csv_file_path . '</li>';
error_log("Found: CSV file found - " . $csv_file_path);
}
// Parse the CSV file
$fields_data = parse_csv_to_fields( $csv_file_path );
if ( $fields_data && is_array( $fields_data ) ) {
echo '<li">Fields Data exists and is an array</li><br/>';
error_log("Fields Data exists and is an array");
foreach ( $fields_data as $fields ) {
var_dump($fields_data);
error_log("Fields Data: " . $fields_data);
echo '<li style="color: green;">' . esc_html( $form_id ) . '</li>';
echo '<li style="color: green;">' . esc_html( $user_id ) . '</li>';
echo '<li style="color: green;">' . esc_html( $user_ip ) . '</li>';
echo '<li style="color: green;">' . esc_html( $user_agent ) . '</li>';
echo '<li style="color: green;">' . esc_html( $date ) . '</li>';
echo '<li style="color: green;">' . esc_html( $user_uuid ) . '</li>';
$entry_id = wpforms()->entry->add( array(
'form_id' => $form_id,
'user_id' => $user_id,
'fields' => wp_json_encode( $fields ),
'ip_address' => sanitize_text_field( $user_ip ),
'user_agent' => sanitize_text_field( $user_agent ),
'date' => $date,
'user_uuid' => sanitize_text_field( $user_uuid ),
));
if ( $entry_id ) {
echo '<li style="color: green;">Entry created successfully: Entry ID ' . esc_html( $entry_id ) . '</li>';
error_log("Entry created successfully: Entry ID $entry_id");
} else {
echo '<li style="color: red;">Error creating entry for form ID ' . esc_html( $form_id ) . '</li>';
error_log("Error creating entry for form ID $form_id");
}
}
} else {
echo '<li style="color: red;">Error processing the CSV file.</li>';
error_log("Error processing the CSV file.");
}
echo '<li>Processing finished.</li>';
error_log("CSV Processing finished.");
}
}
// Function to parse CSV to WPForms fields array
function parse_csv_to_fields( $csv_file_path ) {
echo '<li">Starting To Parse CSV</li>';
if ( ( $handle = fopen( $csv_file_path, "r" ) ) !== false ) {
echo '<li"Opening Handle</li>';
error_log("Opening Handle");
$fields = array();
$row_id = 1;
// Read the CSV file line by line
while ( ( $data = fgetcsv( $handle, 1000, "," ) ) !== false ) {
echo '<li"Starting to Read</li>';
error_log("Starting to Read");
// Skip the header row
if ( $row_id === 1 ) {
$row_id++;
continue;
}
// Assuming the CSV columns are in this order: Name, Email, Message
$fields[] = array(
1 => array(
'id' => '1',
'type' => 'text',
'label' => 'Name',
'value' => sanitize_text_field( $data[0] ),
),
2 => array(
'id' => '2',
'type' => 'email',
'label' => 'Email',
'value' => sanitize_email( $data[1] ),
),
3 => array(
'id' => '3',
'type' => 'textarea',
'label' => 'Message',
'value' => sanitize_textarea_field( $data[2] ),
),
);
echo '<li>Row== '.$row_id.'</li>';
error_log("Opening Handle");
$row_id++;
}
fclose( $handle );
return $fields;
} else {
error_log("Error opening the CSV file.");
return false; // Error opening the file
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment