| |
| <?php
|
|
|
|
|
|
|
|
|
|
|
|
|
| namespace WPF;
|
|
|
| class DeleteUploadedFiles {
|
|
|
|
|
|
|
|
|
|
|
| const DELETE_MEDIA_FILES = false;
|
|
|
|
|
|
|
|
|
| public function hooks() {
|
| add_action( 'wpforms_process_complete', [ $this, 'delete_attached_files' ], 10, 4 );
|
| add_filter( 'wpforms_html_field_value', [ $this, 'replace_html_field_value' ], 100, 4 );
|
| add_filter( 'wpforms_emails_notifications_plaintext_field_value', [ $this, 'replace_plaintext_field_value' ], 100, 3 );
|
| add_filter( 'wpforms_smarttags_process_field_id_value', [ $this, 'replace_field_id_smart_tags_value' ], 10, 5 );
|
| add_filter( 'wpforms_smarttags_process_field_value_id_value', [ $this, 'replace_field_value_id_smart_tags_value' ], 10, 5 );
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| public function delete_attached_files( $fields, $entry, $form_data, $entry_id ) {
|
|
|
| if ( ! $this->is_allowed_delete_files_after_sending_email( $form_data ) ) {
|
| return;
|
| }
|
|
|
| $delete_files_fields = $this->get_filtered_attached_files_fields( $form_data );
|
|
|
| if ( empty( $delete_files_fields ) ) {
|
| return;
|
| }
|
|
|
| foreach ( $delete_files_fields as $field_id ) {
|
| if ( empty( $fields[ $field_id ] ) ) {
|
| continue;
|
| }
|
|
|
| $field = $fields[ $field_id ];
|
|
|
| if ( self::DELETE_MEDIA_FILES ) {
|
| if ( ! empty( $field[ 'style' ] ) && $field[ 'style' ] === 'modern' ) {
|
| $is_media_field = false;
|
|
|
| foreach ( $field[ 'value_raw' ] as $file ) {
|
| if ( ! empty( $file[ 'attachment_id' ] ) ) {
|
| wp_delete_attachment( $file[ 'attachment_id' ], true );
|
|
|
| $is_media_field = true;
|
| }
|
| }
|
|
|
| if ( $is_media_field ) {
|
| continue;
|
| }
|
| }
|
|
|
| if ( ! empty( $field[ 'attachment_id' ] ) ) {
|
| wp_delete_attachment( $field[ 'attachment_id' ], true );
|
|
|
| continue;
|
| }
|
| }
|
|
|
| $files_paths = \WPForms_Field_File_Upload::get_entry_field_file_paths( $form_data[ 'id' ], $field );
|
|
|
| if ( empty( $files_paths ) ) {
|
| continue;
|
| }
|
|
|
| foreach ( $files_paths as $path ) {
|
|
|
| @unlink( $path );
|
| }
|
| }
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| public function replace_html_field_value( $field_val, $field, $form_data, $context ) {
|
|
|
| if ( $context !== 'email-html' ) {
|
| return $field_val;
|
| }
|
|
|
| if ( empty( $field[ 'type' ] ) || $field[ 'type' ] !== 'file-upload' ) {
|
| return $field_val;
|
| }
|
|
|
| if ( ! $this->is_allowed_delete_files_after_sending_email( $form_data ) ) {
|
| return $field_val;
|
| }
|
|
|
| $delete_files_fields = $this->get_filtered_attached_files_fields( $form_data );
|
|
|
| if ( empty( $field[ 'id' ] ) || ! in_array( $field[ 'id' ], $delete_files_fields, true ) ) {
|
| return $field_val;
|
| }
|
|
|
| if ( ! empty( $field[ 'style' ] ) && $field[ 'style' ] === 'modern' ) {
|
| $files = [];
|
|
|
| foreach ( $field['value_raw'] as $file ) {
|
| $file_name = $file['file'] ?? '';
|
|
|
| $files[] = sprintf( '%1$s (%2$s)', $file_name, __( 'attached', 'wpforms' ) );
|
| }
|
|
|
| return implode( "\n", $files );
|
| }
|
|
|
| $file_name = $field[ 'file' ] ?? '';
|
|
|
| return sprintf( '%1$s (%2$s)', $file_name, __( 'attached', 'wpforms' ) );
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| public function replace_plaintext_field_value( $field_val, $field, $form_data ) {
|
|
|
| $replaced_value = $this->replace_html_field_value( $field_val, $field, $form_data, 'email-html' );
|
|
|
| if ( $replaced_value !== $field ) {
|
| return $replaced_value . "\r\n\r\n";
|
| }
|
|
|
| return $field_val;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| public function replace_field_id_smart_tags_value( $value, $form_data, $fields, $entry_id, $smart_tag_object ) {
|
|
|
| $attributes = $smart_tag_object->get_attributes();
|
|
|
| if ( empty( $attributes[ 'field_id' ] ) ) {
|
| return $value;
|
| }
|
|
|
| if ( empty( $fields[ $attributes[ 'field_id' ] ] ) ) {
|
| return $value;
|
| }
|
|
|
| return $this->replace_html_field_value( $value, $fields[ $attributes[ 'field_id' ] ], $form_data, 'email-html' );
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| public function replace_field_value_id_smart_tags_value( $value, $form_data, $fields, $entry_id, $smart_tag_object ) {
|
|
|
| $attributes = $smart_tag_object->get_attributes();
|
|
|
| if ( empty( $attributes[ 'field_value_id' ] ) ) {
|
| return $value;
|
| }
|
|
|
| if ( empty( $fields[ $attributes[ 'field_value_id' ] ] ) ) {
|
| return $value;
|
| }
|
|
|
| return $this->replace_html_field_value( $value, $fields[ $attributes[ 'field_value_id' ] ], $form_data, 'email-html' );
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| private function is_allowed_delete_files_after_sending_email( $form_data ) {
|
|
|
| return ! empty( $form_data[ 'settings' ][ 'disable_entries' ] ) && ! empty( $form_data[ 'settings' ][ 'notifications' ] );
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| private function get_filtered_attached_files_fields( $form_data ) {
|
|
|
| if ( empty( $form_data[ 'fields' ] ) || empty( $form_data[ 'settings' ][ 'notifications' ] ) ) {
|
| return [];
|
| }
|
|
|
| $fields = $this->get_all_attached_files_fields( $form_data );
|
|
|
| foreach ( $fields as $key => $field_id ) {
|
| if ( empty( $form_data[ 'fields' ][ $field_id ][ 'type' ] ) || $form_data['fields'][ $field_id ][ 'type' ] !== 'file-upload' ) {
|
| unset( $fields[ $key ] );
|
| }
|
|
|
| if ( ! self::DELETE_MEDIA_FILES && ! empty( $form_data[ 'fields' ][ $field_id ][ 'media_library' ] ) ) {
|
| unset( $fields[ $key ] );
|
| }
|
| }
|
|
|
| return $fields;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| private function get_all_attached_files_fields( $form_data ) {
|
|
|
| $is_first = true;
|
| $fields = [];
|
|
|
| foreach ( $form_data[ 'settings' ][ 'notifications' ] as $notification ) {
|
| if ( empty( $notification[ 'file_upload_attachment_enable' ] ) || empty( $notification[ 'file_upload_attachment_fields' ] ) ) {
|
| return [];
|
| }
|
|
|
| if ( $is_first ) {
|
| $fields = $notification[ 'file_upload_attachment_fields' ];
|
| $is_first = false;
|
|
|
| continue;
|
| }
|
|
|
| $fields = array_intersect( $fields, $notification[ 'file_upload_attachment_fields' ] );
|
| }
|
|
|
| return $fields;
|
| }
|
| }
|
| |
| |
Comments