Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active February 10, 2023 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/d569bddd49a34f6453a6f69cfee05aa4 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/d569bddd49a34f6453a6f69cfee05aa4 to your computer and use it in GitHub Desktop.
[Forminator] - Attach upload to email
<?php
/**
* Plugin Name: [Forminator] - Attach upload file to email
* Plugin URI: https://premium.wpmudev.org/
* Description: Adds two new options in Uplaod's Edit Field popup under the Advanced tab. One option is for adding the uploaded file as an attachment. Second option is for deleting the file after upload
* Author: Panos Lyrakis @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WPMUDEV_Frominator_Upload_Email_Attachment' ) ) {
class WPMUDEV_Frominator_Upload_Email_Attachment {
public $attachments = array();
public $attachments_to_delete = array();
private static $_instance = null;
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_Frominator_Upload_Email_Attachment();
}
return self::$_instance;
}
private function __construct() {
add_filter( 'wp_mail', array( $this, 'filter_email_args' ) );
add_filter( 'forminator_field_upload_general_settings', array( $this, 'add_attachment_settings' ) );
add_filter( 'forminator_custom_form_submit_errors', array( $this, 'manage_field_data' ), 20, 3 );
add_action( 'forminator_custom_form_mail_after_send_mail', array( $this, 'delete_uploaded_files' ) );
}
public function add_attachment_settings( $settings ) {
$attachment_option = array(
'type' => 'Toggle',
'label' => 'Attach to email',
'name' => 'use_as_attachment'
);
$delete_upload_option = array(
'type' => 'Toggle',
'label' => 'Delete file after upload',
'name' => 'delete_uploaded_file'
);
array_push( $settings, $attachment_option, $delete_upload_option );
return $settings;
}
public function manage_field_data( $submit_errors, $form_id, $data ) {
if ( empty( $submit_errors ) ) {
foreach ( $data as $key => $field_data ) {
if ( ! isset( $field_data['name'] ) ) {
continue;
}
if ( isset( $field_data['field_type'] ) ) {
if ( 'upload' != $field_data['field_type'] ) {
continue;
}
}
$field = Forminator_API::get_form_field( $form_id, $field_data['name'] );
if ( is_wp_error( $field ) ) {
continue;
}
if ( ! isset( $field_data[ 'value' ][ 'file' ][ 'file_path' ] ) ) {
continue;
}
if ( !is_array( $field_data[ 'value' ][ 'file' ][ 'file_path' ] ) ) {
if ( $file_path = $field_data[ 'value' ][ 'file' ][ 'file_path' ] ) {
if ( Forminator_Field::get_property( 'use_as_attachment', $field ) ) {
$this->attachments[] = $file_path;
}
if ( Forminator_Field::get_property( 'delete_uploaded_file', $field ) ) {
$this->attachments_to_delete[] = $file_path;
}
}
} else {
foreach( $field_data[ 'value' ][ 'file' ][ 'file_path' ] as $fi_k => $fi_val ){
if ( Forminator_Field::get_property( 'use_as_attachment', $field ) ) {
$this->attachments[] = $fi_val;
}
if ( Forminator_Field::get_property( 'delete_uploaded_file', $field ) ) {
$this->attachments_to_delete[] = $fi_val;
}
}
}
}
}
return $submit_errors;
}
public function filter_email_args( $mail_args ) {
if ( ! empty( $this->attachments ) ) {
$mail_args[ 'attachments' ] = $this->attachments;
}
return $mail_args;
}
public function delete_uploaded_files() {
if ( ! empty( $this->attachments_to_delete ) ) {
foreach ( $this->attachments_to_delete as $file_path ) {
unlink( $file_path );
}
}
if ( ! empty( $this->attachments ) ) {
unset( $this->attachments );
}
}
}
if ( ! function_exists( 'wpmudev_forminator_upload_email_attachment' ) ) {
function wpmudev_forminator_upload_email_attachment() {
return WPMUDEV_Frominator_Upload_Email_Attachment::get_instance();
};
add_action( 'plugins_loaded', 'wpmudev_forminator_upload_email_attachment', 10 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment