Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement - Send file directly as an attachment. #1326

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -134,21 +134,21 @@
wp_send_json_error( $default_error );
}

if ( empty( $_FILES['file'] ) ) {

Check failure on line 137 in includes/abstracts/class-evf-form-fields-upload.php

View workflow job for this annotation

GitHub Actions / Code sniff (PHP 7.4 , WP Latest)

Processing form data without nonce verification.
wp_send_json_error( esc_html__( 'No file was uploaded', 'everest-forms' ) );
}

if ( isset( $_FILES['file']['error'] ) && UPLOAD_ERR_OK !== $_FILES['file']['error'] ) {

Check failure on line 141 in includes/abstracts/class-evf-form-fields-upload.php

View workflow job for this annotation

GitHub Actions / Code sniff (PHP 7.4 , WP Latest)

Processing form data without nonce verification.

Check failure on line 141 in includes/abstracts/class-evf-form-fields-upload.php

View workflow job for this annotation

GitHub Actions / Code sniff (PHP 7.4 , WP Latest)

Processing form data without nonce verification.
$error = $_FILES['file']['error']; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized

Check failure on line 142 in includes/abstracts/class-evf-form-fields-upload.php

View workflow job for this annotation

GitHub Actions / Code sniff (PHP 7.4 , WP Latest)

Processing form data without nonce verification.
$error_message = $this->get_upload_validation_errors( $error );
wp_send_json_error( $error_message );
}

// Make sure we have required values from $_FILES.
if ( empty( $_FILES['file']['name'] ) ) {

Check failure on line 148 in includes/abstracts/class-evf-form-fields-upload.php

View workflow job for this annotation

GitHub Actions / Code sniff (PHP 7.4 , WP Latest)

Processing form data without nonce verification.
wp_send_json_error( $default_error );
}
if ( empty( $_FILES['file']['tmp_name'] ) ) {

Check failure on line 151 in includes/abstracts/class-evf-form-fields-upload.php

View workflow job for this annotation

GitHub Actions / Code sniff (PHP 7.4 , WP Latest)

Processing form data without nonce verification.
wp_send_json_error( $default_error );
}

Expand All @@ -158,9 +158,9 @@
$this->field_id = $validated_form_field['field_id'];
$this->field_data = $this->form_data['form_fields'][ $this->field_id ];

$error = empty( $_FILES['file']['error'] ) ? UPLOAD_ERR_OK : intval( $_FILES['file']['error'] );

Check failure on line 161 in includes/abstracts/class-evf-form-fields-upload.php

View workflow job for this annotation

GitHub Actions / Code sniff (PHP 7.4 , WP Latest)

Processing form data without nonce verification.

Check failure on line 161 in includes/abstracts/class-evf-form-fields-upload.php

View workflow job for this annotation

GitHub Actions / Code sniff (PHP 7.4 , WP Latest)

Processing form data without nonce verification.
$path = $_FILES['file']['tmp_name']; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.InputNotValidated

Check failure on line 162 in includes/abstracts/class-evf-form-fields-upload.php

View workflow job for this annotation

GitHub Actions / Code sniff (PHP 7.4 , WP Latest)

Processing form data without nonce verification.
$name = sanitize_file_name( wp_unslash( $_FILES['file']['name'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated

Check failure on line 163 in includes/abstracts/class-evf-form-fields-upload.php

View workflow job for this annotation

GitHub Actions / Code sniff (PHP 7.4 , WP Latest)

Processing form data without nonce verification.
$extension = strtolower( pathinfo( $name, PATHINFO_EXTENSION ) );
$errors = $this->ajax_validate( $error, $extension, $path, $name );
$name_of_file = isset( $this->field_data['custom_file_name'] ) ? sanitize_file_name( $this->field_data['custom_file_name'] ) . '_' . uniqid( '', true ) . '.' . $extension : sanitize_file_name( wp_unslash( $_FILES['file']['name'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated
Expand Down Expand Up @@ -1287,10 +1287,15 @@
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 @@
* @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 @@
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
Loading