-
Notifications
You must be signed in to change notification settings - Fork 1
/
bea-wpf-upload-attachment.php
62 lines (50 loc) · 1.77 KB
/
bea-wpf-upload-attachment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php class BEA_WPForms_Upload_Attachment {
function __construct() {
add_filter( 'wpforms_email_attachments', [ $this, 'add_files_as_attachment_to_mail' ], 20, 2 );
add_filter( 'wpforms_email_send_after', [ $this, 'delete_files_from_upload' ], 20 );
add_filter( 'wpforms_html_field_value', [ $this, 'render_upload_file_into_mail' ], 20, 2 );
}
public function add_files_as_attachment_to_mail( $attachments, $wpf_obj ) {
if ( empty( $wpf_obj->fields ) ) {
return $attachments;
}
foreach ( $wpf_obj->fields as $field ) {
if ( 'file-upload' !== $field['type'] || empty( $field['value'] ) ) {
continue;
}
$attachment_path = $this->transform_upload_url_into_path( $field['value'] );
if ( empty( $attachment_path ) ) {
continue;
}
$attachments[] = $attachment_path;
}
return $attachments;
}
public function delete_files_from_upload( $wpf_obj ) {
if ( empty( $wpf_obj->fields ) ) {
return;
}
foreach ( $wpf_obj->fields as $field ) {
if ( 'file-upload' !== $field['type'] || empty( $field['value'] ) ) {
continue;
}
$attachment_path = $this->transform_upload_url_into_path( $field['value'] );
if ( empty( $attachment_path ) ) {
continue;
}
unlink( $attachment_path );
}
}
// Customize value format for HTML emails.
public function render_upload_file_into_mail( $val, $field ) {
if ( ! empty( $field['value'] ) && 'file-upload' === $field['type'] ) {
return __( sprintf( 'Cf. pièce jointe : %s.', $field['file_original'] ), '' );
}
return $val;
}
private function transform_upload_url_into_path( $url ) {
$found = preg_match( '/\/uploads\/(.*)/', $url, $file_path );
return empty( $found ) ? '' : sprintf( '%s/%s', wp_get_upload_dir()['basedir'], $file_path[1] );
}
}
new BEA_WPForms_Upload_Attachment();