• Resolved Mattmcca

    (@mattmcca)


    I hope this message finds you well. Firstly, I would like to express my gratitude for the outstanding plugin you have developed. I am thoroughly enjoying using it and greatly appreciate the support you have provided thus far.

    I am currently encountering some challenges with the submission process and would appreciate your assistance in resolving them. Specifically, I am facing difficulties with handling invalid form requests. I was wondering if there is a way to prevent the insertion of data into the database when the form submission is invalid. Currently, even when the form is not valid, an insert operation is still performed. I would greatly appreciate any guidance or code suggestions you can provide to address this issue.

    Furthermore, I have a second inquiry. Is there a method or filter available to retrieve only the latest entry made by the user through the “get_entries” function? I am interested in filtering the results to only display the most recent submission from each user.

    To provide you with more context, I have included the relevant sections of my code below:

    https://drive.google.com/drive/folders/1nTKz6dEyvPiFF0BQpAHff6ICzsz4pleJ?usp=drive_link

    Thank you in advance for your attention to these matters. I truly value your expertise and assistance in resolving these issues. If there are any additional details or clarifications needed, please let me know, and I will be more than happy to provide them.


Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Support Jair – WPMU DEV Support

    (@wpmudevsupport15)

    Hi @mattmcca,

    I hope you are doing well today!

    Thank you for your kind words and for reaching out with your queries. We are checking those with our SLS (Second Line Support) team and will inform you accordingly.

    Kind regards,
    Zafer

    Thread Starter Mattmcca

    (@mattmcca)

    Might I add, this is for a custom plugin that I have worked on, I would like to prevent a method calling a database insert when an invalid request has been made

    Plugin Support Jair – WPMU DEV Support

    (@wpmudevsupport15)

    Hi again @mattmcca,

    We have an update from our SLS team;

    <?php
    /**
    * Plugin Name: [Forminator Pro] - Forminator insert into database after form submission 
    * Description: Charge dealer dependant on the amount of booking he made 
    * Author: Matthias McCarthy
    * License: GPLv2 or later
    */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit;
    }
    
    // No need to do anything if the request is via WP-CLI.
    if ( defined( 'WP_CLI' ) && WP_CLI ) {
    	return;
    }
    
    if ( ! class_exists( 'Forminator_Post_Insert_External_table' ) ) {
        
        class Forminator_Post_Insert_External_table {
    
            // Source Form
            private $s_form_id = 2002;
            private $t_number_field = 'number-1';
    
            private static $_instance = null;
    
            public static function get_instance() {
    
                if( is_null( self::$_instance ) ){
                    self::$_instance = new Forminator_Post_Insert_External_table();
                }
    
                return self::$_instance; 
            }
    
            private function __construct() {    
                $this->init();
            }
    
            public function init(){
                add_filter( 'forminator_form_after_save_entry', array( $this, 'wpmudev_forminator_custom_form_after_save_entry' ), 10, 2 );
            }
    
            public function wpmudev_forminator_custom_form_after_save_entry( $form_id, $response ){           
                
                 if( $this->s_form_id != $form_id ){
                    return;
                 }
    
                 if ( $response && is_array( $response ) ) {
                    if ( $response['success'] ) {
                        $entries = Forminator_API::get_entries( $form_id );
                        global $wpdb;
                        $numberOfBookings = 0;
                        $current_user = get_current_user_id();
    
                        if( is_array( $entries ) ){
                            $entry = $entries[0];
                            if( $entry instanceof Forminator_Form_Entry_Model ){
                                $numberOfBookings = $entry->get_meta( $this->t_number_field );
                                $wpdb->query("INSERT INTO Credit_Log(Credits, Action, User_ID, PerformedBy) VALUES('-$numberOfBookings', 'Transaction','$current_user','$current_user' )"); 
    
                            }
                        }
    
                        if( ! isset( $numberOfBookings ) ){
                            return;
                        }
                    }
                }
    
            }
    
        }
    
        add_filter( 'plugins_loaded', function(){
            return Forminator_Post_Insert_External_table::get_instance();
        });
    
    }

    For the latest entry of that form you can use this method forminator_get_latest_entry_by_form_id

    Please note that the code is the updated version of their code. It will only insert data when the form is successfully submitted and the function forminator_get_latest_entry_by_form_id is for your second enquiry.

    Kind regards,
    Zafer

    Thread Starter Mattmcca

    (@mattmcca)

    Thank you so much, this is going to help me out so much, in regards to the second question, I would like to incorporate it with the submitted code. So do I change

    $entries = Forminator_API::get_entries( $form_id );
    to
    $entries = Forminator_API::forminator_get_latest_entry_by_form_id( $form_id );


    Plugin Support Saurabh – WPMU DEV Support

    (@wpmudev-support7)

    Hi @mattmcca

    Hope you are doing fine!

    We have shared this last question with our development team to get a precise answer. They will review the code and let know if it can be adjusted as mentioned. Keep in mind though, the team works with more complex issues and their response may take some considerable time.

    Once they have an update, a notification will be sent and a post will also be added in this thread.

    Thanks in advance for your understanding.

    Kind regards

    Luis

    Plugin Support Saurabh – WPMU DEV Support

    (@wpmudev-support7)

    Hi @mattmcca

    Thanks for waiting for our reply.

    I just verified with the development team and they confirmed it’s correct to replace this part of your code:

    $entries = Forminator_API::get_entries( $form_id );

    With this:

    $entries = Forminator_API::forminator_get_latest_entry_by_form_id( $form_id );

    Hope this information helps. Feel free to reply if you have additional questions.

    Kind regards

    Luis

    Thread Starter Mattmcca

    (@mattmcca)

    Hello whilst testing I noticed a bug, i am getting two email submissions when using the above

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @mattmcca,

    I was facing an issue with making the above code work and it seems like the following usage is incorrect:

    $entries = Forminator_API::forminator_get_latest_entry_by_form_id( $form_id );
    

    The final code should be as follows:

    	<?php
    /**
    * Plugin Name: [Forminator Pro] - Forminator insert into database after form submission 
    * Description: Charge dealer dependant on the amount of booking he made 
    * Author: Matthias McCarthy
    * License: GPLv2 or later
    */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit;
    }
    
    // No need to do anything if the request is via WP-CLI.
    if ( defined( 'WP_CLI' ) && WP_CLI ) {
    	return;
    }
    
    if ( ! class_exists( 'Forminator_Post_Insert_External_table' ) ) {
        
        class Forminator_Post_Insert_External_table {
    
            // Source Form
            private $s_form_id = 2002;
            private $t_number_field = 'number-1';
    
            private static $_instance = null;
    
            public static function get_instance() {
    
                if( is_null( self::$_instance ) ){
                    self::$_instance = new Forminator_Post_Insert_External_table();
                }
    
                return self::$_instance; 
            }
    
            private function __construct() {    
                $this->init();
            }
    
            public function init(){
                add_filter( 'forminator_form_after_save_entry', array( $this, 'wpmudev_forminator_custom_form_after_save_entry' ), 10, 2 );
            }
    
            public function wpmudev_forminator_custom_form_after_save_entry( $form_id, $response ){           
                
                 if( $this->s_form_id != $form_id ){
                    return;
                 }
    
                 if ( $response && is_array( $response ) ) {
                    if ( $response['success'] ) {
                        $entries = Forminator_API::get_entries( $form_id );
                        global $wpdb;
                        $numberOfBookings = 0;
                        $current_user = get_current_user_id();
    
                        if( is_array( $entries ) ){
                            $entry = $entries[0];
                            if( $entry instanceof Forminator_Form_Entry_Model ){
                                $numberOfBookings = $entry->get_meta( $this->t_number_field );
                                $wpdb->query("INSERT INTO Credit_Log(Credits, Action, User_ID, PerformedBy) VALUES('-$numberOfBookings', 'Transaction','$current_user','$current_user' )"); 
    
                            }
                        }
    
                        if( ! isset( $numberOfBookings ) ){
                            return;
                        }
                    }
                }
    
            }
    
        }
    
        add_filter( 'plugins_loaded', function(){
            return Forminator_Post_Insert_External_table::get_instance();
        });
    
    }

    In the above code $entries[0];  is removed and $entry variable is added for the forminator_get_latest_entry_by_form_id function.

    I wasn’t able to replicate any issue with email getting sent twice when tested with the above code. Please do make sure to edit the $s_form_id in the above code to your form ID, if testing the above code.

    Could you please check and let us know if you are able to replicate the same issue or not?

    Looking forward to your response.

    Kind Regards,

    Nithin

    Plugin Support Amin – WPMU DEV Support

    (@wpmudev-support2)

    Hello @mattmcca ,

    We haven’t heard from you for over a week now, so it looks like you no longer need our assistance.

    Feel free to re-open this topic if needed.

    Kind regards
    Kasia

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Data Inserting to database even when form is invalid’ is closed to new replies.