Home / Disable / Send Google Drive link to Google Sheets and purge entries
Duplicate Snippet

Embed Snippet on Your Site

Send Google Drive link to Google Sheets and purge entries

Delays the Google Sheets task until Google Drive has finished uploading, then swaps the local/download URL for the direct Google Drive URL. Entries will be purged one day after the event.

Ralden Souza PRO
<10
Code Preview
php
<?php
/**
 * Send Google Drive URLs to Google Sheets.
 *
 * Setup requirements:
 * - Entry storage ON (Disable storing entry information in WordPress = OFF)
 * - Purge Entries Automatically = ON, set to 1 day
 * - Google Drive > Delete Local Files After Upload = ON
 * - Google Sheets field mapping includes the File Upload field
 */
/**
 * Skip the default Sheets processor for connections mapping a file field
 * that has files in this submission. Google Drive will send this row instead.
 */
add_action( 'wpforms_google_sheets_process_action', 'wpf_dev_skip_sheets_for_file_connections', 1 );
function wpf_dev_skip_sheets_for_file_connections( $meta_id ) {
    $meta      = new \WPForms\Tasks\Meta();
    $meta_data = $meta->get( (int) $meta_id );
    if ( empty( $meta_data->data ) || count( $meta_data->data ) !== 4 ) {
        return;
    }
    [ $connection_data, $fields, $form_data, $entry_id ] = $meta_data->data;
    $has_files = false;
    if ( ! empty( $connection_data['custom_fields'] ) ) {
        foreach ( $connection_data['custom_fields'] as $mapped_field_id ) {
            if (
                is_int( $mapped_field_id ) &&
                isset( $fields[ $mapped_field_id ]['type'] ) &&
                in_array( $fields[ $mapped_field_id ]['type'], [ 'file-upload', 'camera' ], true ) &&
                ! empty( $fields[ $mapped_field_id ]['value_raw'] )
            ) {
                $has_files = true;
                break;
            }
        }
    }
    if ( ! $has_files ) {
        return;
    }
    $process_task = wpforms_google_sheets()->get( 'process_task' );
    if ( $process_task ) {
        remove_action( 'wpforms_google_sheets_process_action', [ $process_task, 'process' ] );
    }
    wpforms_log(
        'Google Sheets task skipped for connection with file field. Google Drive will send this row directly.',
        [
            'meta_id'    => $meta_id,
            'entry_id'   => $entry_id,
            'form_id'    => $form_data['id'] ?? 0,
            'connection' => $connection_data['name'] ?? '',
        ],
        [ 'type' => [ 'log' ] ]
    );
}
/**
 * Once Google Drive finishes uploading, send the row to Google Sheets directly,
 * overriding the file field value with the correct Google Drive URL.
 * Only runs if THIS submission actually has files.
 */
add_action( 'wpforms_google_drive_process_upload', 'wpf_dev_send_to_sheets_with_drive_urls', 999 );
function wpf_dev_send_to_sheets_with_drive_urls( $meta_id ) {
    $task_meta = new \WPForms\Tasks\Meta();
    $meta      = $task_meta->get( (int) $meta_id );
    if ( empty( $meta->data ) || count( $meta->data ) !== 4 ) {
        return;
    }
    [ , $fields, $form_data, $entry_id ] = $meta->data;
    $form_id = $form_data['id'] ?? 0;
    if ( empty( $form_id ) || empty( $form_data['providers']['google-sheets'] ) ) {
        return;
    }
    // Bail immediately if THIS submission has no files in any file/camera field.
    $submission_has_files = false;
    foreach ( $fields as $field ) {
        if (
            isset( $field['type'] ) &&
            in_array( $field['type'], [ 'file-upload', 'camera' ], true ) &&
            ! empty( $field['value_raw'] )
        ) {
            $submission_has_files = true;
            break;
        }
    }
    if ( ! $submission_has_files ) {
        return;
    }
    $entry_meta = wpforms()->obj( 'entry_meta' );
    if ( ! $entry_meta ) {
        return;
    }
    // Avoid sending this entry to Sheets more than once.
    $sent_flag = $entry_meta->get_meta( [
        'entry_id' => $entry_id,
        'form_id'  => $form_id,
        'type'     => 'wpf_gdrive_sheets_sent',
        'number'   => 1,
    ] );
    if ( ! empty( $sent_flag[0]->data ) ) {
        return;
    }
    // Fetch the Google Drive attachments saved for this form.
    $records = $entry_meta->get_meta( [
        'entry_id' => 0,
        'form_id'  => $form_id,
        'type'     => 'google_drive_attachments',
        'number'   => 1,
    ] );
    if ( empty( $records[0]->data ) ) {
        return;
    }
    $attachments = json_decode( $records[0]->data, true );
    if ( empty( $attachments ) ) {
        return;
    }
    // Build a map of field_id => Google Drive URLs.
    $urls_by_field = [];
    foreach ( $attachments as $attachment ) {
        if ( ! empty( $attachment['field_id'] ) && ! empty( $attachment['google_drive_id'] ) ) {
            $fid                     = (int) $attachment['field_id'];
            $urls_by_field[ $fid ][] = 'https://drive.google.com/file/d/' . $attachment['google_drive_id'] . '/view';
        }
    }
    if ( empty( $urls_by_field ) ) {
        return;
    }
    $process_task = wpforms_google_sheets()->get( 'process_task' );
    if ( ! $process_task ) {
        return;
    }
    $swap_filter = function( $value, $field_id, $swap_form_data, $swap_fields, $swap_entry_id ) use ( $urls_by_field ) {
        if ( isset( $urls_by_field[ (int) $field_id ] ) ) {
            return implode( "\n", $urls_by_field[ (int) $field_id ] );
        }
        return $value;
    };
    add_filter( 'wpforms_google_sheets_provider_field_mapper_field_value', $swap_filter, 10, 5 );
    $sent_any = false;
    foreach ( $form_data['providers']['google-sheets'] as $key => $sheets_connection ) {
        if ( $key === '__lock__' ) {
            continue;
        }
        $maps_file_field = false;
        if ( ! empty( $sheets_connection['custom_fields'] ) ) {
            foreach ( $sheets_connection['custom_fields'] as $mapped_field_id ) {
                if ( is_int( $mapped_field_id ) && isset( $urls_by_field[ $mapped_field_id ] ) ) {
                    $maps_file_field = true;
                    break;
                }
            }
        }
        if ( ! $maps_file_field ) {
            continue;
        }
        $process_task->add_row( (array) $sheets_connection, $fields, (array) $form_data, (int) $entry_id );
        $sent_any = true;
        wpforms_log(
            'Sent row to Google Sheets directly with Google Drive URLs.',
            [
                'entry_id'   => $entry_id,
                'form_id'    => $form_id,
                'connection' => $sheets_connection['name'] ?? '',
            ],
            [ 'type' => [ 'log' ] ]
        );
    }
    remove_filter( 'wpforms_google_sheets_provider_field_mapper_field_value', $swap_filter, 10 );
    if ( $sent_any ) {
        $entry_meta->add(
            [
                'entry_id' => $entry_id,
                'form_id'  => absint( $form_id ),
                'user_id'  => 0,
                'type'     => 'wpf_gdrive_sheets_sent',
                'data'     => '1',
            ],
            'entry_meta'
        );
    }
}

Comments

Add a Comment