Created
April 23, 2020 10:38
-
-
Save ledovej/34a0b824a85984d43000dd92995f875f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Class Elementor_Form_Email_Attachments | |
* | |
* Send Elementor Form upload field as attachments to email | |
*/ | |
class Elementor_Form_Email_Attachments { | |
// Set to true if you want the files to be removed from | |
// the server after they are sent by email | |
const DELETE_ATTACHMENT_FROM_SERVER = true; | |
public $attachments_array = []; | |
public function __construct() { | |
add_action( 'elementor_pro/forms/process', [ $this, 'init_form_email_attachments' ], 11, 2 ); | |
} | |
/** | |
* @param \ElementorPro\Modules\Forms\Classes\Form_Record $record | |
* @param \ElementorPro\Modules\Forms\Classes\Ajax_Handler $ajax_handler | |
*/ | |
public function init_form_email_attachments( $record, $ajax_handler ) { | |
// check if we have attachments | |
$files = $record->get( 'files' ); | |
if ( empty( $files ) ) { | |
return; | |
} | |
// Store attachment in local var | |
foreach ( $files as $id => $files_array ) { | |
$this->attachments_array[] = $files_array['path'][0]; | |
} | |
// if local var has attachments setup filter hook | |
if ( 0 < count( $this->attachments_array ) ) { | |
add_filter( 'wp_mail', [ $this, 'wp_mail' ] ); | |
add_action( 'elementor_pro/forms/new_record', [ $this, 'remove_wp_mail_filter' ], 5 ); | |
} | |
} | |
public function remove_wp_mail_filter() { | |
if ( self::DELETE_ATTACHMENT_FROM_SERVER ) { | |
foreach ( $this->attachments_array as $uploaded_file ) { | |
unlink( $uploaded_file ); | |
} | |
} | |
$this->attachments_array = []; | |
remove_filter( 'wp_mail', [ $this, 'wp_mail' ] ); | |
} | |
public function wp_mail( $args ) { | |
// check for no attachment flag | |
$no_attachments = strpos($args['message'], '[elementor_form_no_attachment]'); | |
if ( FALSE !== $no_attachments ) { | |
// remove the flag from the message | |
$args['message'] = str_replace('[elementor_form_no_attachment]', '', $args['message']); | |
} else { | |
// attach files instead | |
$args['attachments'] = $this->attachments_array; | |
} | |
return $args; | |
} | |
} | |
new Elementor_Form_Email_Attachments(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment