Question about handling sensitive media uploads (follow-up)
-
https://www.ads-software.com/support/topic/question-about-handling-sensitive-media-uploads/
In this topic I opened on this forum awhile ago, I asked about how to auto-delete uploaded media files in forms from WordPress after sending to email, to avoid sensitive information showing up in Google images or from searching the site. This has worked perfectly for our single upload function on our forms. However, we would like to apply Multiple Upload to our forms as well. As it works now, Multiple Upload does not work when the custom/Mu Plugin that auto-deletes media is activated; it instead throws an error. Would it be a simple fix to update this code to get it to work with either single or multiple uploads? Let me know.
https://gist.github.com/wpmudev-sls/7a32dc5407324cc902f8b9ad8970ef62
The page I need help with: [log in to see the link]
-
Hi @rpksc93019,
I hope you are doing well and thank you for reaching out to us.
I have pinged our Second Level Support team regarding this and we’ll update you here once we have feedback on this as soon as possible.
Kind Regards,
Nebu JohnHi @rpksc93019,
Can you please try the following workaround?
<?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 & Prashant Singh @ 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_field_data', array( $this, 'manage_field_data' ), 20, 2 ); 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( $data, $form_id ) { foreach ( $data as $key => $field_data ) { if ( ! isset( $field_data['name'] ) ) { continue; } $field = Forminator_API::get_form_field( $form_id, $field_data['name'] ); if ( is_wp_error( $field ) ) { continue; } $field_type = Forminator_Field::get_property( 'type', $field ); if ( 'upload' != $field_type ) { 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 ) ) { unset( $this->attachments ); $this->attachments[] = $file_path; } if ( Forminator_Field::get_property( 'delete_uploaded_file', $field ) ) { $this->attachments_to_delete[] = $file_path; } } } else { unset( $this->attachments ); 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 $data; } 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 ( ! 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 ); } }
The code could be added using a mu-plugin. I hope the following guide helps: https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/
We recommend either creating a full backup initially or testing it on a staging website.
Kind Regards,
Nebu John- This reply was modified 11 months, 2 weeks ago by Nebu John - WPMU DEV Support.
Hi @rpksc93019,
We haven’t heard from you in a while, we will go ahead and mark this thread as resolved. If you have any additional questions or require further help, please let us know!
Kind regards,
ZaferSorry, I was on vacation and unable to answer my email.
/custom-quote/
This is my staging site and the page where I have a test form. I enabled Multiple Uploads on the form. I then added the work around code .php as a mu-plugin. I’m still getting a https://domain.com/wp-admin/admin-ajax.php POST 500 internal server error when I try to submit the form. I enabled debug log and it throws an error:
[27-Dec-2023 19:50:26 UTC] PHP Warning: Undefined array key “file_path” in /home/[]/public_html/domain.com/wp-content/mu-plugins/forminator-multiple-upload-workaround.php on line 80
[27-Dec-2023 19:50:26 UTC] PHP Warning: Undefined array key “file_path” in /home/[]/public_html/domain.com/wp-content/mu-plugins/forminator-multiple-upload-workaround.php on line 81
[27-Dec-2023 19:50:26 UTC] PHP Fatal error: Uncaught TypeError: preg_match(): Argument #2 ($subject) must be of type string, array given in /home/[]/public_html/domain.com/wp-includes/PHPMailer/PHPMailer.php:1869 Stack trace:
Hi @rpksc93019,
Could we know what is the PHP version on which the website runs? I checked the given snippet and it does work fine when tested.
[27-Dec-2023 19:50:26 UTC] PHP Fatal error: Uncaught TypeError: preg_match(): Argument #2 ($subject) must be of type string, array given in /home/[]/public_html/domain.com/wp-includes/PHPMailer/PHPMailer.php:1869 Stack trace:
Possible to share the full error ie including the “Stack trace” which is missing in the above? So that it would give a better idea regarding the issues noticed.
Looking forward to your response.
Kind Regards,
Nithin
It’s PHP version 8.0.
This is the whole thing from the beginning to the end of the Stack Trace:
[27-Dec-2023 19:50:26 UTC] PHP Warning: Undefined array key "file_path" in /home/[]/public_html/dev.domain.com/wp-content/mu-plugins/forminator-multiple-upload-workaround.php on line 80 [27-Dec-2023 19:50:26 UTC] PHP Warning: Undefined array key "file_path" in /home/[]/public_html/dev.domain.com/wp-content/mu-plugins/forminator-multiple-upload-workaround.php on line 81 [27-Dec-2023 19:50:26 UTC] PHP Fatal error: Uncaught TypeError: preg_match(): Argument #2 ($subject) must be of type string, array given in /home/[]/public_html/dev.domain.com/wp-includes/PHPMailer/PHPMailer.php:1869 Stack trace: #0 /home/[]/public_html/dev.domain.com/wp-includes/PHPMailer/PHPMailer.php(1869): preg_match() #1 /home/[]/public_html/dev.domain.com/wp-includes/PHPMailer/PHPMailer.php(1881): PHPMailer\PHPMailer\PHPMailer::isPermittedPath() #2 /home/[]/public_html/dev.domain.com/wp-includes/PHPMailer/PHPMailer.php(3218): PHPMailer\PHPMailer\PHPMailer::fileIsAccessible() #3 /home/[]/public_html/dev.domain.com/wp-includes/pluggable.php(526): PHPMailer\PHPMailer\PHPMailer->addAttachment() #4 /home/[]/public_html/dev.domain.com/wp-content/plugins/forminator/library/abstracts/abstract-class-mail.php(418): wp_mail() #5 /home/[]/public_html/dev.domain.com/wp-content/plugins/forminator/library/modules/custom-forms/front/front-mail.php(225): Forminator_Mail->send_multiple() #6 /home/[]/public_html/dev.domain.com/wp-content/plugins/forminator/library/modules/custom-forms/front/front-action.php(978): Forminator_CForm_Front_Mail->process_mail() #7 /home/[]/public_html/dev.domain.com/wp-content/plugins/forminator/library/modules/custom-forms/front/front-action.php(960): Forminator_CForm_Front_Action::send_email() #8 /home/[]/public_html/dev.domain.com/wp-content/plugins/forminator/library/abstracts/abstract-class-front-action.php(450): Forminator_CForm_Front_Action->handle_form() #9 /home/[]/public_html/dev.domain.com/wp-includes/class-wp-hook.php(310): Forminator_Front_Action->save_entry() #10 /home/[]/public_html/dev.domain.com/wp-includes/class-wp-hook.php(334): WP_Hook->apply_filters() #11 /home/[]/public_html/dev.domain.com/wp-includes/plugin.php(517): WP_Hook->do_action() #12 /home/[]/public_html/dev.domain.com/wp-admin/admin-ajax.php(203): do_action() #13 {main} thrown in /home/[]/public_html/dev.domain.com/wp-includes/PHPMailer/PHPMailer.php on line 1869
Hello @rpksc93019,
Thank you for the details.
The SLS techs will check testing it in order to avoid the error. We’ll be updating you as soon as there’s more info on this.
Please note, that the next reply may take longer than usual, depending on the current amount and complexity of the tasks for the SLS team.
Best Regards,
DmytroHello @rpksc93019,
Please take our apologies for the delayed reply.
Could you please test it after replacing the current code with this updated snippet:
<?php 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_action( 'forminator_custom_form_submit_before_set_fields', 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( $entry, $form_id, $data ) { foreach ( $data as $key => $field_data ) { if ( ! isset( $field_data['name'] ) ) { continue; } $field = Forminator_API::get_form_field( $form_id, $field_data['name'] ); if ( is_wp_error( $field ) ) { continue; } $field_type = Forminator_Field::get_property( 'type', $field ); if ( 'upload' != $field_type ) { 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 ) ) { unset( $this->attachments ); $this->attachments[] = $file_path; } if ( Forminator_Field::get_property( 'delete_uploaded_file', $field ) ) { $this->attachments_to_delete[] = $file_path; } } } else { unset( $this->attachments ); 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; } } } } } 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 ( ! 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 ); } }
Let us know if there’s still any issue.
Best Regards,
DmytroThank you. I will try this out and let you know how it works soon.
The mu-plugin did not work. I believe there are too many custom mu-plugins from Forminator, that there is conflict. What would be the best way to get further help from Forminator support in regards to combining their provided mu-plugins? I think last time we just signed up for WPMUDEV, is that what you recommend?
Hi @rpksc93019,
Hope this message finds you well.
I believe there are too many custom mu-plugins from Forminator, that there is conflict.
Besides the one provided by Dmytro (you should replace and temporarily disable the old one) which other Forminator snippets are you using?
About getting a membership at wpmudev.com, it is up to you, we will be more than happy if you join us, but notice that even with it, some requests might be out of our support scope.
About the last snippet, are you getting the same error? or is just not working? Let us know.
Best regards,
Laura
- The topic ‘Question about handling sensitive media uploads (follow-up)’ is closed to new replies.