• Resolved cadrago21

    (@cadrago21)


    Hi Guys, I am using your fantastic plug.

    I have created several forms linked to E2PDF with which customers fill in their quote request and receive a response with the PDF quote attached to the email.

    The problem is this request receives an immediate response.

    Is it possible to make it so that the reply from forminator comes after like an hour, simulating that the quote was created by me and not by the system?

    thank you

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Support Nebu John – WPMU DEV Support

    (@wpmudevsupport14)

    Hi @cadrago21,

    Trust you are doing good and thank you for reaching out to us.

    Could you please check if the following code snippet helps?

    https://gist.github.com/wpmudev-nebu/8156116010f009ab3a6823f74a24ecbe

    The code can be added using a mu-plugin. I hope the following guide comes in handy: https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins

    I hope that helps.

    Kind Regards,
    Nebu John

    Thread Starter cadrago21

    (@cadrago21)

    Thank you for your reply, I have entered files and code, but I cannot understand how to handle them.

    How can I manage the delay?

    I tested after entering the file and code the delay but it doesn’t work. Is there graphically a panel for managing the delay?

    Thanks as always

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @cadrago21

    There is no panel for that as there is no such setting in Forminator and the code that my colleague provided is a custom workaround.

    However, I read your initial request and looked into the shared code and I think there was a bit of misunderstanding on our end.

    You are simply expecting e-mail notifications to be sent automatically with a 1 hour delay, right?

    The code shared is for sending massive number of notifications in batches and on a selected date only (where date is selected on the form) so that’s a bit different. I think the code would require some modification so I’ve asked my colleague who created it to give it another look.

    I’d appreciate some patience but we’ll update you here again soon.

    Kind regards,
    Adam

    Thread Starter cadrago21

    (@cadrago21)

    Thank you for your kind reply, in fact while checking the code I could not quite understand how it works.

    I would just like to be able to choose, via a simple code variation, the delay time for sending emails.

    That there is a possibility through the code to change the number to correspond to seconds or minutes to determine the delay of the reply email and make my system more realistic than automated.

    Thank you very much indeed

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @cadrago21

    I understand that and I’m surely not expecting you to modify the code.

    I’ve asked our developers to look into it to see if it would be possible to achieve such delay and if it is doable – we will provide with working code and a clear instruction on how to apply it to the site.

    I’m aware that this is taking long and I’m sorry for keeping you waiting but I would appreciate a bit more patience as it’s about custom solution and our developers need to check it first and then come up with the code while at the same time dealing with a lot of different custom tasks.

    We will update you here again once we got feedback.

    Kind regards,
    Adam

    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @cadrago21

    I got feedback from our developers on this and I got a new code that should match your needs.

    First, please completely remove the one previously shared (if you didn’t do that yet).

    Then add a new one and here is how to do this:

    1. create an empty file with a .php extensions (e.g. “forminator-delay-notificaitons.php”); use some kind of a “clean text” editor for editing this file, such as Notepad++, Sublime or similar

    2. copy and paste following code into that file:

    <?php
    if ( ! class_exists( 'WPMUDEV_Forminator_Emails_Schedule' ) ) {
        class WPMUDEV_Forminator_Emails_Schedule {
            protected $form_id 				= 6; //Please change the form ID
            protected $emails_per_batch		= 50;
            protected $resend_to_admin 		= true;
            protected $entry 				= null;
            protected $queue_option_name 	= 'forminator_scheduled_messages_queue';
            private static $_instance 		= null;
            public static function get_instance() {
                if ( is_null( self::$_instance ) ) {
                    self::$_instance = new WPMUDEV_Forminator_Emails_Schedule();
                }
                return self::$_instance;
            }
    
            private function __construct() {
                add_action( 'init', array( $this, 'wpmudev_schedule_cron_mail' ) );
                add_action( 'forminator_form_after_save_entry', array( $this, 'store_messages' ), 20, 2 );
                add_filter( 'forminator_form_get_admin_email_recipients', array( $this, 'block_default_email' ), 20, 5 );
                add_action( 'wpmudev_forminator_emails_schedule', array( $this, 'send_batch' ) );
            }
    
            public function block_default_email( $email, $notification, $data, $custom_form, $entry ) {
                if ( $this->form_id != $custom_form->id ) {
                    return $email;
                }
                // We need the $entry for replacing email vars:
                $this->entry = $entry;
                // Return an empty array so it won't send the notification on submit
                return array();
            }
    
            public function store_messages( $form_id, $response ) {
                if ( $this->form_id != $form_id || is_null( $this->entry ) ) {
                    return;
                }
                //$form 			= Forminator_API::get_form( $form_id );
                //$setting 			= $form->settings;
                $post_data 			= $_POST;
                $messages_queue		= $this->get_queue();
    
                if ( empty( $messages_queue ) ) {
                    $messages_queue = array();
                }
    
                $messages_queue[ $form_id ][] = array(
                    'form_id' 		=> $form_id,
                    'entry_id' 		=> $this->entry->entry_id,
                    'page_id' 		=> $post_data['page_id'],
                    'timestamp'     => time()
                    //'post_data' 	=> $post_data
                );
                update_option( $this->queue_option_name, json_encode( $messages_queue ) );
            }
    
            public function send_batch() {
                $messages_queue		= $this->get_queue();
                if ( isset( $messages_queue[ $this->form_id ] ) && ! empty( $messages_queue[ $this->form_id ] ) ) {
                    $queued_items = array_slice( $messages_queue[ $this->form_id ], 0, $this->emails_per_batch, true );
                    foreach ( $queued_items as $key => $queued_item ) {
                        $form_id 				= (int) $queued_item[ 'form_id' ];
                        $entry_id 				= (int) $queued_item[ 'entry_id' ];
                        $timestamp              = $queued_item[ 'timestamp' ];
                        //$post_data 			= $queued_item[ 'post_data' ];
                        $form 					= Forminator_API::get_form( $form_id );
                        $entry 					= new Forminator_Form_Entry_Model( $entry_id );
                        $post_data 				= $this->data_from_enty( $entry );
                        if ( ! $form || ! $entry ) {
                            continue;
                        }
    
                        $currentTime = time();
                        if ( $currentTime - $timestamp < 3600 ) {
                            continue;
                        }
    
                        unset( $messages_queue[ $this->form_id ][ $key ] );
    
                        // A few hacks that we need :
                        // 1. We need the page_id to avoid a warning message
                        $post_data['page_id'] 	= (int) $queued_item[ 'page_id' ];        			
                        // 2. We also need to set the $_POST too to avoid above warning
                        $_POST 					= $post_data;
                        // 3. If we don't want to resend to admin
                        if ( ! $this->resend_to_admin ) {
                            unset( $form->settings['use-admin-email'] );
                        }        			
                        // 4. We are currently blocking the email template to be sent. We need to unblock it now though
                        remove_filter( 'forminator_form_get_admin_email_recipients', array( $this, 'block_default_email' ), 20 );
                        // All should be set to go
                        $forminator_mail_sender = new Forminator_CForm_Front_Mail();
                        $forminator_mail_sender->process_mail( $form, $entry, $post_data );
                    }
    
                    if ( empty( $messages_queue[ $this->form_id ] ) ){
                        unset( $messages_queue[ $this->form_id ] );
                    }
                    update_option( $this->queue_option_name, json_encode( $messages_queue ) );
                }
            }
    
            private function data_from_enty( $entry ) {
                $post_data = array();
                foreach ( $entry->meta_data as $key => $meta ) {
                    if ( ! isset( $meta['value'] ) ) {
                        continue;
                    }
                    $post_data[ $key ] = $meta['value'];
                }
                return $post_data;
            }
    
            protected function get_queue() {
                return json_decode( get_option( $this->queue_option_name ), true );
            }
            
            public function wpmudev_schedule_cron_mail() {
                if ( ! wp_next_scheduled ( 'wpmudev_forminator_emails_schedule' ) ) {
                    wp_schedule_event( time(), 'every_minute', 'wpmudev_forminator_emails_schedule') ;
                }
            }
            
        }
    
        if ( ! function_exists( 'wpmudev_forminator_emails_schedule' ) ) {
            function wpmudev_forminator_emails_schedule(){    		
                return WPMUDEV_Forminator_Emails_Schedule::get_instance();
            };
            add_action( 'plugins_loaded', 'wpmudev_forminator_emails_schedule', 10 );
        }
    }

    3. now you need to edit one or two lines, depending on your needs

    a) in this line

    protected $form_id = 6; //Please change the form ID

    replace number 6 with your form ID; form ID is the number you see in form shortcode

    b) if you want delay different than one hour, you need to replace value 3600 in this line

    if ( $currentTime - $timestamp < 3600 ) {

    Since 3600 = 1hr, you can set it e.g. to 1800 (half an hour) or 7200 (2 hours) and so on;

    4. save the file and upload it to your server to this folder of your WordPress installation:

    /wp-content/mu-plugins

    And that’s it, it should then work out of the box.

    Kind regards,
    Adam

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @cadrago21,

    Since we haven’t heard from you for a while. I’ll mark this thread as resolved for now. Please feel free to re-open the thread if you need further assistance.

    Regards
    Nithin

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Delay Email’ is closed to new replies.