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
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
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
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 );
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
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
Hello whilst testing I noticed a bug, i am getting two email submissions when using the above
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
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