| |
| <?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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;
|
| }
|
|
|
|
|
| $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;
|
| }
|
|
|
|
|
|
|
| $records = $entry_meta->get_meta( [
|
| 'entry_id' => 0,
|
| 'form_id' => $form_id,
|
| 'type' => 'google_drive_attachments',
|
| 'number' => 1,
|
| ] );
|
|
|
| if ( ! empty( $records[0]->data ) ) {
|
|
|
| return;
|
| }
|
|
|
|
|
| as_schedule_single_action(
|
| time() + 120,
|
| 'wpforms_google_sheets_process_action',
|
| [ 'tasks_meta_id' => $meta_id ],
|
| 'wpforms'
|
| );
|
|
|
|
|
| $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;
|
| }
|
|
|
|
|
|
|
| $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