Skip to content

Commit

Permalink
Enhancement - Send file directly as an attachment. (#1326)
Browse files Browse the repository at this point in the history
* Enhancement- send File directly to cloud

* Added - Support for image upload

---------

Co-authored-by: MILAN88888 <chaudharymilan996@gmail.com>
  • Loading branch information
deepench and MILAN88888 authored Sep 6, 2024
1 parent 0495f1f commit 2b89746
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 4 deletions.
80 changes: 76 additions & 4 deletions includes/abstracts/class-evf-form-fields-upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -1287,10 +1287,15 @@ public function max_file_size() {
public function send_file_as_email_attachment( $attachment, $entry, $form_data, $context, $connection_id, $entry_id ) {

$file_email_attachments = isset( $form_data['settings']['email'][ $connection_id ]['file-email-attachments'] ) ? $form_data['settings']['email'][ $connection_id ]['file-email-attachments'] : 0;
if ( '1' !== $file_email_attachments ) {
if ( isset( $form_data['settings']['disabled_entries'] ) && '1' === $form_data['settings']['disabled_entries'] ) {
$attachment = $this->attach_entry_files_upload( $entry );
}

if ( '1' === $file_email_attachments ) {
$attachment = array_unique( array_merge( (array) $attachment, $this->attach_entry_files( $entry_id ) ) );
return $attachment;
}
$attachment = array_unique( array_merge( (array) $attachment, $this->attach_entry_files( $entry_id ) ) );

return $attachment;
}

Expand Down Expand Up @@ -1325,22 +1330,52 @@ public function send_csv_file_as_email_attachment( $attachment, $entry, $form_da
* @param integer $entry_id Entry id for the form.
*/
public function remove_csv_file_after_email_send( $attachment, $entry, $form_data, $context, $connection_id, $entry_id ) {

if ( isset( $form_data['settings']['disabled_entries'] ) && '1' === $form_data['settings']['disabled_entries'] ) {
if ( ! empty( $entry ) && is_array( $entry ) ) {
foreach ( $entry as $meta_key => $meta_value ) {
if ( empty( $meta_value ) ) {
continue;
}

if ( preg_match( '/signature_/', $meta_key ) && file_exists( $meta_value ) ) {
unlink( $meta_value );
}

if ( isset( $meta_value['type'] ) && ( 'file-upload' === $meta_value['type'] && isset( $meta_value['value_raw'] ) || 'image-upload' === $meta_value['type'] && isset( $meta_value['value_raw'] ) ) ) {
foreach ( $meta_value['value_raw'] as $file_data ) {
if ( isset( $file_data['value'] ) ) {
$file_url = $file_data['value'];

$uploaded_file = ABSPATH . preg_replace( '/.*wp-content/', 'wp-content', wp_parse_url( $file_url, PHP_URL_PATH ) );
if ( file_exists( $uploaded_file ) ) {
unlink( $uploaded_file );
}
}
}
}
}
}
}

if ( ! $entry_id ) {
return;
}

$file_email_attachments = isset( $form_data['settings']['email'][ $connection_id ]['file-email-attachments'] ) ? $form_data['settings']['email'][ $connection_id ]['file-email-attachments'] : 0;
$csv_file_email_attachments = isset( $form_data['settings']['email'][ $connection_id ]['csv-file-email-attachments'] ) ? $form_data['settings']['email'][ $connection_id ]['csv-file-email-attachments'] : 0;
$csv_file_email_attachments = apply_filters( 'everest_forms_change_csv_attachments', $csv_file_email_attachments );

if ( '1' === $csv_file_email_attachments ) {
$get_entry = evf_get_entry( $entry_id, 'meta' );
$upload_dir = WP_CONTENT_DIR . '/uploads/Everes-Froms-Entries-CSV-file/';
$csv_path = $upload_dir . 'Entry data-' . $get_entry->entry_id . '.csv';
$csv_path = $upload_dir . 'Entry data-' . $entry_id . '.csv';
if ( file_exists( $csv_path ) ) {
unlink( $csv_path );
}
}

}

/**
* Attach the entry file.
*
Expand Down Expand Up @@ -1377,6 +1412,43 @@ public function attach_entry_files( $entry_id ) {
return $entry_files;
}

/**
* Attach the entry file.
*
* @param int $entry Entry for which file should be attached.
*/
public function attach_entry_files_upload( $entry ) {
$entry_files = array();

if ( ! empty( $entry ) && is_array( $entry ) ) {
foreach ( $entry as $meta_key => $meta_value ) {
if ( empty( $meta_value ) ) {
continue;
}

if ( preg_match( '/signature_/', $meta_key ) ) {
if ( file_exists( $meta_value ) ) {
$entry_files[] = $meta_value;
}
} elseif ( isset( $meta_value['type'] ) && ( 'file-upload' === $meta_value['type'] && isset( $meta_value['value_raw'] ) || 'image-upload' === $meta_value['type'] && isset( $meta_value['value_raw'] ) ) ) {
foreach ( $meta_value['value_raw'] as $file_data ) {
if ( isset( $file_data['value'] ) ) {
$file_url = $file_data['value'];
$uploaded_file = ABSPATH . preg_replace( '/.*wp-content/', 'wp-content', wp_parse_url( $file_url, PHP_URL_PATH ) );

if ( ! in_array( $uploaded_file, $entry_files ) && file_exists( $uploaded_file ) ) {
$entry_files[] = $uploaded_file;
}
}
}
}
}
}

return $entry_files;
}


/**
* Attach the csv file.
*
Expand Down
16 changes: 16 additions & 0 deletions includes/class-evf-emails.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ public function everest_forms_html_field_value( $html = true ) {
$message = '';

if ( $html ) {

/*
* HTML emails.
*/
Expand All @@ -477,6 +478,13 @@ public function everest_forms_html_field_value( $html = true ) {

$field_iterator = 1;
foreach ( $this->fields as $meta_id => $field ) {

if ( isset( $this->form_data['settings']['disabled_entries'] ) && '1' === $this->form_data['settings']['disabled_entries'] ) {
$types_to_remove = array( 'image-upload', 'file-upload', 'signature' );
if ( isset( $field['type'] ) && in_array( $field['type'], $types_to_remove, true ) ) {
continue;
}
}
if (
! apply_filters( 'everest_forms_email_display_empty_fields', false ) &&
( empty( $field['value'] ) && '0' !== $field['value'] )
Expand Down Expand Up @@ -585,6 +593,14 @@ public function everest_forms_html_field_value( $html = true ) {
* Plain Text emails.
*/
foreach ( $this->fields as $field ) {

if ( isset( $this->form_data['settings']['disabled_entries'] ) && '1' === $this->form_data['settings']['disabled_entries'] ) {
$types_to_remove = array( 'image-upload', 'file-upload', 'signature' );
if ( isset( $field['type'] ) && in_array( $field['type'], $types_to_remove, true ) ) {
continue;
}
}

if ( ! apply_filters( 'everest_forms_email_display_empty_fields', false ) && ( empty( $field['value'] ) && '0' !== $field['value'] ) ) {
continue;
}
Expand Down

0 comments on commit 2b89746

Please sign in to comment.