Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active June 21, 2023 12:24
Show Gist options
  • Save wpmudev-sls/e3e4655e9ad74686051393a1d0ed5ffa to your computer and use it in GitHub Desktop.
Save wpmudev-sls/e3e4655e9ad74686051393a1d0ed5ffa to your computer and use it in GitHub Desktop.
[Forminator Pro] - Submission unique identifier
<?php
/**
* Plugin Name: [Forminator Pro] - Submission unique identifier
* Plugin URI: https://premium.wpmudev.org/
* Description: Generate a unique code for each form submission (as of 1.12.1)
* Author: Alessandro Kaounas @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* Task: 0/11289012348292/1175564087636585
* 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( 'WPMUDEV_Forminator_Unique_Identifier' ) ) {
class WPMUDEV_Forminator_Unique_Identifier {
// User defined settings
private $forms = array(
100 => 'hidden-1',
200 => 'hidden-2',
// ... more (form_id) - (hidden_field) pairs here
);
/**
* Unique identifier length (Default: 6)
* Keep length at least 4 to avoid any infinite loops
*/
private $length = 6;
private $generations = 0;
private static $_instance = null;
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_Forminator_Unique_Identifier();
}
return self::$_instance;
}
private function __construct() {
$this->init();
}
public function init(){
// Do some checks here
if ( ! defined( 'FORMINATOR_VERSION' ) || FORMINATOR_VERSION < '1.12' || ! class_exists( 'Forminator_API' ) ) {
return;
}
// Generate a unique identifier
add_filter( 'forminator_custom_form_submit_field_data', array( $this, 'wpmudev_randomize_hidden_input' ), 10, 2 );
// Ajax method (action)
add_action( 'forminator_form_after_save_entry', array( $this, 'wpmudev_forminator_custom_form_after_save_entry' ), 10, 2 );
// Post method (filter)
add_filter( 'forminator_form_submit_response', array( $this, 'wpmudev_forminator_custom_form_after_save_entry' ), 10, 2 );
// Ajax method (filter)
add_filter( 'forminator_form_ajax_submit_response', array( $this, 'wpmudev_forminator_custom_form_after_save_entry' ), 10, 2 );
}
public function wpmudev_forminator_custom_form_after_save_entry( $form_id, $response ){
if( ! wp_doing_ajax() ){
// Swap variables when POST method
list( $form_id, $response ) = array( $response, $form_id );
}
if( ! in_array( $form_id, array_keys( $this->forms ) ) ){
return;
}
$entries = Forminator_API::get_entries( $form_id );
if( is_array( $entries ) ){
$entry = current( $entries );
if( $entry instanceof Forminator_Form_Entry_Model ){
$code = $entry->get_meta( $this->forms[$form_id] );
}
}
if( ! isset( $code ) ){
return;
}
$form = Forminator_API::get_form( $form_id );
if( ! $form instanceof Forminator_Form_Model ) {
return;
}
$msg = sprintf( __( "Form submitted with unique code: <strong>%s</strong>", 'wpmudev' ), $code );
if( wp_doing_ajax() ){
$form->settings['thankyou-message'] .= "<br>" . $msg;
wp_send_json_success(
array(
'success' => true,
'message' => $form->settings['thankyou-message'],
'behav' => $form->settings['submission-behaviour']
)
);
}else{
$response['message'] .= "<br>" . $msg;
}
return $response;
}
public function wpmudev_randomize_hidden_input( $field_data_array, $form_id ) {
if( ! in_array( $form_id, array_keys( $this->forms ) ) ){
return $field_data_array;
}
foreach( $field_data_array as $key => $field ){
if( $field['name'] === $this->forms[$form_id] ){
unset( $field_data_array[$key] );
}
}
$field_data_array[] = array(
'name' => $this->forms[$form_id],
'value' => $this->wpmudev_randomizer( $form_id, $this->forms[$form_id] )
);
return $field_data_array;
}
public function wpmudev_is_unique( $num = '', $form_id, $form_field ) {
global $wpdb;
$query = "SELECT COUNT(*) FROM {$wpdb->prefix}frmt_form_entry_meta
WHERE meta_key LIKE '{$form_field}'
AND meta_value LIKE '{$num}'
AND entry_id IN( SELECT entry_id
FROM {$wpdb->prefix}frmt_form_entry WHERE form_id = {$form_id} );";
if( 0 === (int) $wpdb->get_var( $query ) ){
return true;
}else{
$this->generations++;
return false;
}
}
public function wpmudev_randomizer( $form_id = 0, $form_field = 'hidden-1' ) {
$num = '';
$characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
for ($i = 0; $i < $this->length; $i++) {
$num .= $characters[rand(0, strlen( $characters ) - 1)];
}
if( $this->generations > pow( strlen( $characters ), $this->length ) ){
return __( 'No unique codes available to assign.', 'wpmudev' );
}
if( (int) $num === 0 ){
return $this->wpmudev_randomizer( $form_id, $form_field );
}
if( $this->wpmudev_is_unique( $num, $form_id, $form_field ) ) {
return $num;
} else {
return $this->wpmudev_randomizer( $form_id, $form_field );
}
}
}
add_action( 'plugins_loaded', function(){
return WPMUDEV_Forminator_Unique_Identifier::get_instance();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment