Skip to content

Commit

Permalink
Added - Custom file name succesfll
Browse files Browse the repository at this point in the history
  • Loading branch information
NirajChaudhary143 committed Jul 30, 2024
1 parent 6d39a60 commit 1bf8c8b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 42 deletions.
48 changes: 8 additions & 40 deletions includes/abstracts/class-evf-form-fields-upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,12 @@ public function upload_file() {
$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'] );
$path = $_FILES['file']['tmp_name']; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.InputNotValidated
$name = sanitize_file_name( wp_unslash( $_FILES['file']['name'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated
$extension = strtolower( pathinfo( $name, PATHINFO_EXTENSION ) );
$errors = $this->ajax_validate( $error, $extension, $path, $name );
$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

if ( count( $errors ) ) {
wp_send_json_error( implode( ',', $errors ), 400 );
Expand All @@ -182,7 +183,7 @@ public function upload_file() {
wp_send_json_success(
array(
'file' => pathinfo( $tmp, PATHINFO_FILENAME ) . '.' . pathinfo( $tmp, PATHINFO_EXTENSION ),
'name' => $name,
'name' => $name_of_file,
)
);
}
Expand Down Expand Up @@ -525,7 +526,7 @@ public function custom_file_name( $field ) {
$field,
array(
'slug' => 'custom_file_name',
'value' => esc_html__( 'File name', 'everest-forms' ),
'value' => esc_html__( 'Custom File Name', 'everest-forms' ),
'tooltip' => esc_html__( 'Enter text to be displayed as file name.', 'everest-forms' ),
),
false
Expand All @@ -547,39 +548,6 @@ public function custom_file_name( $field ) {
$this->field_element( 'row', $field, $args );
}

/**
* Creates file name by using fields of the pdf file
*
* @param array $field Field data.
*/
public function custom_tag_file_name( $field ) {
$lbl = $this->field_element(
'label',
$field,
array(
'slug' => 'custom_tag_file_name',
'value' => esc_html__( 'File name based on field tags', 'everest-forms' ),
'tooltip' => esc_html__( 'Enter the tags which represents the fields of the form and uses the field value as file name.', 'everest-forms' ),
)
);

$fld = $this->field_element(
'text',
$field,
array(
'slug' => 'custom_tag_file_name',
'value' => ! empty( $field['custom_tag_file_name'] ) ? $field['custom_tag_file_name'] : '', // probably need to put the first tag present
)
);

$args = array(
'slug' => 'custom_tag_file_name',
'content' => $lbl . $fld,
);

$this->field_element( 'row', $field, $args );
}

/**
* Limit message field option.
*
Expand Down
3 changes: 1 addition & 2 deletions includes/fields/class-evf-field-file-upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public function __construct() {
'description',
'upload_message',
'limit_message',
'custom_file_name', //user provided file name
'custom_tag_file_name', //file name with field tags
'custom_file_name',
'extensions',
'max_size',
'max_file_number',
Expand Down

0 comments on commit 1bf8c8b

Please sign in to comment.