Last active
January 13, 2025 10:37
-
-
Save adczk/fbf06b6abf7753df6b37234b13041e79 to your computer and use it in GitHub Desktop.
Forminator 1.15.2 - set autoincremented value in a field, per form, consecutive, upon submission
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Forminator] - Auto-increase field value (with prefix/suffix) | |
* Description: [Forminator] - Auto-increase field value (with prefix/suffix) | |
* Author: Adam @ WPMUDEV | |
* Author URI: https://wpmudev.com | |
* Based on: https://gist.github.com/wpmudev-sls/b607c860225b0b6bde502bcdec0184fa | |
* License: GPLv2 or later | |
* | |
* Tested with Forminator 1.15.2 - 1.29.0 (backwards/future compatibility unknown) | |
* | |
* Update Feb 14, 2024 | |
* - fix: returning form data instead of errors in pre_set_custom_value() in some cases | |
* | |
* UPDATE Feb 11, 2024 | |
* - fix: numbers continuity and unexpected reseting in case of use with multiple forms | |
* | |
* UPDATE Jul 12, 2023 | |
* - fix: numbers are no longer incremented if submission fails (to avoid gaps in continuity) | |
* | |
* UPDATE Jun 14th, 2023: now it also supports individual field in mail notifiation and adding number to e-mail subject | |
* | |
* if you use {all_fields} or {all_non_empty_fields} in e-mail notifications, generated value will be correctly used there | |
* if you want to use individual fields macros in e-mail, you need to use macro | |
* defined in $macro in config below in message body instead of the hidden field macro | |
* if you want to add generated number to mail notification subject, also use that defined $macro there | |
* | |
* for example, if $macro = '{formatted_value}' and $field_name='{hidden-1}' | |
* | |
* in subject and in e-mail notification use {formatted_value} instead of {hidden-1} | |
* | |
*/ | |
/** | |
* To reset autincrement number of a form, please login the admin | |
* and then try to access this url: yoursite.com/wp-admin?wpmudev-fm-reset-number-by-form-id=[form_id] | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} elseif ( defined( 'WP_CLI' ) && WP_CLI ) { | |
return; | |
} | |
add_action( 'after_setup_theme', 'wpmudev_forminator_autoincreased_field', 100 ); | |
function wpmudev_forminator_autoincreased_field() { | |
if ( class_exists( 'Forminator' ) ) { | |
class WPMUDEV_FM_Autoincreased_field { | |
// CONFIGURATION ############### | |
private $macro = '{formatted_value}'; // The text to replace in the Thank you message (fixed by AMIT) | |
private $field_name = 'hidden-1'; // id of the field to hold autoincrement value; must be existing field of type hidden | |
private $form_ids = array( 2688 ); // comma separated list of forms to use this with | |
private $start_number = 1; // starting number to count up from | |
private $prefix = 'CT2021_'; // field value prefix | |
private $suffix = ''; // field value suffix | |
private $leading_zeros = 2; // how many leading zeros; | |
// 0: number are like 1, 2, 3, 4, 5 and so on | |
// 1: number are like 01, 02, 03...10, 11 and so on | |
// 2: numbers are like 001, 002...011, 012, 101, 520 and so on | |
// 3: numbers are like 0001, 002... 0011... 0520, 1234 and so on... | |
// ... | |
// DO NOT EDIT BELOW ###################### | |
private $entry; | |
private $order_numbers; | |
// AMIT: Variable to hold formatted value among filters | |
private $formatted_value; | |
public function __construct() { | |
// reset count (for admin) | |
add_action( 'admin_init', array( $this, 'reset_number' ) ); | |
add_filter( 'forminator_custom_form_submit_field_data', array( $this, 'set_custom_value' ), 10, 2 ); | |
add_filter( 'forminator_custom_form_submit_errors', array( $this, 'pre_set_custom_value' ), 10, 3 ); | |
add_filter( 'forminator_custom_form_mail_data', array( $this, 'wpmudev_fm_mail_form_data' ), 10, 3 ); | |
// handle $macro in notification subject and message | |
add_filter( 'forminator_custom_form_mail_admin_subject', array( $this, 'wpmudev_fm_mail_macro_replace' ), 10, 3 ); | |
add_filter( 'forminator_custom_form_mail_admin_message', array( $this, 'wpmudev_fm_mail_macro_replace' ), 10, 3 ); | |
// AMIT: Update Formatted value macro in the Thank you response | |
add_filter( 'forminator_form_ajax_submit_response', array( $this, 'inject_formatted_value_in_response' ), 20 ); | |
add_filter( 'forminator_form_submit_response', array( $this, 'inject_formatted_value_in_response' ), 20 ); | |
} | |
// helper - format value with leading zeros, prefix and suffix | |
public function do_format_value( $value ) { | |
$formatted_value = $value; | |
// set leading zeros | |
$add_zeros = ( $this->leading_zeros + 1 ) - strlen( $value ); | |
if ( $add_zeros > 0 ) { | |
for ( $i = 1; $i <= $add_zeros; $i++ ) { | |
$formatted_value = '0' . $formatted_value; | |
} | |
} | |
// add prefix/suffix | |
$formatted_value = $this->prefix . $formatted_value . $this->suffix; | |
return $formatted_value; | |
} | |
// helper - get clean number by removing prefix, suffix and leading zeros | |
// | |
// NOT USED | |
// | |
// but shows how to "unformat"/"decode" formatted value to a raw number | |
public function do_unformat_value( $value ) { | |
$unformatted_value = (int) ltrim( str_replace( $this->suffix, '', str_replace( $this->prefix, '', $value ) ), '0' ); | |
return $unformatted_value; | |
} | |
// helper - get number from DB option | |
public function get_number( $form_id ) { | |
static $raw_number; | |
if ( ! $raw_number ) { | |
$formatted_numbers = get_option( 'wpmudev_fm_autoincremented_field', array() ); | |
if ( isset( $formatted_numbers[ $form_id ] ) ) { | |
$raw_number = $formatted_numbers[ $form_id ]; | |
} else { | |
$raw_number = $this->start_number; | |
} | |
} | |
return $raw_number; | |
} | |
// reset number function | |
public function reset_number() { | |
if ( current_user_can( 'manage_options' ) && isset( $_GET['wpmudev-fm-reset-number-by-form-id'] ) ) { | |
$form_id = (int) $_GET['wpmudev-fm-reset-number-by-form-id']; | |
if ( $form_id ) { | |
$order_numbers = get_option( 'wpmudev_fm_autoincremented_field', array() ); | |
$order_numbers[ $form_id ] = $this->start_number; | |
update_option( 'wpmudev_fm_autoincremented_field', $order_numbers ); | |
} | |
} | |
} | |
// get, increase and update value | |
public function pre_set_custom_value( $submit_errors, $form_id, $field_data_array ) { | |
//public function set_custom_value( $field_data_array, $form_id ) { | |
if ( ! in_array( $form_id, $this->form_ids ) ) { | |
return $submit_errors; | |
} | |
if ( !empty( $submit_errors) ) { | |
return $submit_errors; | |
} | |
$current_value = $this->get_number( $form_id ); | |
$order_numbers = get_option( 'wpmudev_fm_autoincremented_field', array() ); | |
// increase value for next submission | |
$current_value++; | |
$order_numbers[ $form_id ] = $current_value; | |
// and remember it in DB option | |
update_option( 'wpmudev_fm_autoincremented_field', $order_numbers ); | |
//return $field_data_array; | |
return $submit_errors; | |
} | |
public function set_custom_value( $field_data_array, $form_id ) { | |
if ( ! in_array( $form_id, $this->form_ids ) ) { | |
return $field_data_array; | |
} | |
$current_value = $this->get_number( $form_id ); | |
// AMIT: Save Formatted value in class level variable | |
$this->formatted_value = $this->do_format_value( $current_value ); | |
$custom_data_array[] = array( | |
'name' => $this->field_name, | |
'value' => $this->formatted_value, // AMIT, changed | |
); | |
// update form submission data | |
$field_data_array = array_merge( $field_data_array, $custom_data_array ); | |
return $field_data_array; | |
} | |
public function wpmudev_fm_mail_form_data( $data, $custom_form, $entry ) { | |
/* make sure that field variable in notification uses saved data instead of submitted */ | |
$data[ $this->field_name ] = $entry->meta_data[ $this->field_name ]['value']; | |
return $data; | |
} | |
public function wpmudev_fm_mail_macro_replace( $message, $custom_form, $data ) { | |
$message = str_replace( $this->macro, $this->formatted_value, $message ); | |
return $message; | |
} | |
/* | |
** AMIT: Inject the formatted value in thank you response | |
*/ | |
public function inject_formatted_value_in_response( $response ) { | |
if ( isset( $this->formatted_value ) ) { | |
$response['message'] = str_replace( $this->macro, $this->formatted_value, $response['message'] ); | |
} | |
return $response; | |
} | |
} | |
$run = new WPMUDEV_FM_Autoincreased_field(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, it would require additional development, this code won't do it. I'm not going to enhance/update this code though, I'm afraid (no time and different priorities); it's provided "as is". But feel free to use and modify it on your own.