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
/**
 * Delays the Google Sheets task until Google Drive has finished uploading,
 * then swaps the local/download URL for the direct Google Drive URL.
 *
 * 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
 */
add_action( 'wpforms_google_sheets_process_action', 'wpf_dev_delay_sheets_until_drive_ready', 1 );
function wpf_dev_delay_sheets_until_drive_ready( $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;
    $form_id = $form_data['id'] ?? 0;
    if ( empty( $form_id ) ) {
        return;
    }
    // Only intervene if this connection maps at least one file-upload or camera field.
    $has_file_field = 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 )
            ) {
                $has_file_field = true;
                break;
            }
        }
    }
    if ( ! $has_file_field ) {
        return;
    }
    $entry_meta = wpforms()->obj( 'entry_meta' );
    if ( ! $entry_meta ) {
        return;
    }
    // Drive always saves attachment meta under entry_id = 0 when entry storage
    // is off at task registration time. Query by entry_id = 0 + form_id.
    $records = $entry_meta->get_meta( [
        'entry_id' => 0,
        'form_id'  => $form_id,
        'type'     => 'google_drive_attachments',
        'number'   => 1,
    ] );
    if ( ! empty( $records[0]->data ) ) {
        // Drive meta is present — let Sheets proceed normally.
        return;
    }
    // Drive hasn't run yet. Reschedule this Sheets task 2 minutes from now.
    as_schedule_single_action(
        time() + 120,
        'wpforms_google_sheets_process_action',
        [ 'tasks_meta_id' => $meta_id ],
        'wpforms'
    );
    // Prevent the real processor from running in this current execution.
    $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 delayed: waiting for Google Drive upload to complete.',
        [
            'meta_id'  => $meta_id,
            'entry_id' => $entry_id,
            'form_id'  => $form_id,
        ],
        [ 'type' => [ 'log' ] ]
    );
}
add_filter( 'wpforms_google_sheets_provider_field_mapper_field_value', 'wpf_dev_swap_gdrive_url_in_sheets', 10, 5 );
function wpf_dev_swap_gdrive_url_in_sheets( $value, $field_id, $form_data, $fields, $entry_id ) {
    if (
        ! isset( $fields[ $field_id ]['type'] ) ||
        ! in_array( $fields[ $field_id ]['type'], [ 'file-upload', 'camera' ], true )
    ) {
        return $value;
    }
    $form_id = $form_data['id'] ?? 0;
    if ( empty( $form_id ) ) {
        return $value;
    }
    $entry_meta = wpforms()->obj( 'entry_meta' );
    if ( ! $entry_meta ) {
        return $value;
    }
    // Drive saves attachment meta under entry_id = 0. Match by form_id to avoid
    // collisions between concurrent submissions.
    $records = $entry_meta->get_meta( [
        'entry_id' => 0,
        'form_id'  => $form_id,
        'type'     => 'google_drive_attachments',
        'number'   => 1,
    ] );
    if ( empty( $records[0]->data ) ) {
        return $value;
    }
    $attachments = json_decode( $records[0]->data, true );
    if ( empty( $attachments ) ) {
        return $value;
    }
    $urls = [];
    foreach ( $attachments as $attachment ) {
        if (
            isset( $attachment['field_id'] ) &&
            (int) $attachment['field_id'] === (int) $field_id &&
            ! empty( $attachment['google_drive_id'] )
        ) {
            $urls[] = 'https://drive.google.com/file/d/' . $attachment['google_drive_id'] . '/view';
        }
    }
    if ( empty( $urls ) ) {
        return $value;
    }
    return implode( "\n", $urls );
}

Comments

Add a Comment