Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wpmudev-sls/b607c860225b0b6bde502bcdec0184fa to your computer and use it in GitHub Desktop.
Save wpmudev-sls/b607c860225b0b6bde502bcdec0184fa to your computer and use it in GitHub Desktop.
[Forminator] - Custom submission id as order number
<?php
/**
* Plugin Name: [Forminator] - Custom submission id as order number
* Description: [Forminator] - Custom submission id as order number
* Jira: SLS-224
* Author: Thobk @ WPMUDEV
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
} elseif ( defined( 'WP_CLI' ) && WP_CLI ) {
return;
}
add_action( 'after_setup_theme', 'wpmudev_forminator_custom_submission_number_for_custom_form_func', 100 );
/**
* To reset order number of a form, please login the admin
* and then try to access this url: yoursite.com/wp-admin?wpmudev-fm-reset-order-number-by-form-id=[form_id]
*/
function wpmudev_forminator_custom_submission_number_for_custom_form_func() {
if ( class_exists( 'Forminator' ) ) {
class WPMUDEV_FM_Order_Number{
private $include_form_ids = [];
private $exclude_form_ids = [];//enter list exclude form ids, e.g: [234,456]
private $start_number = 1;
private $entry;
public function __construct(){
add_filter( 'forminator_replace_custom_form_data', array( $this, 'custom_order_number' ), 10, 4 );
add_action( 'forminator_custom_form_mail_after_send_mail', array( $this, 'increase_order_number' ), 10, 1 );
add_action( 'admin_init', array( $this, 'reset_order_number' ) );
// change number order for admin:
add_filter( 'forminator_custom_form_entries_iterator', array( $this, 'custom_order_number_for_admin' ), 10, 2 );
}
public function reset_order_number(){
if( current_user_can( 'manage_options' ) && isset( $_GET['wpmudev-fm-reset-order-number-by-form-id']) ){
$form_id = (int) $_GET['wpmudev-fm-reset-order-number-by-form-id'];
if( $form_id ) {
$order_numbers = get_option( 'wpmudev_fm_custom_submission_id', array() );
if( isset( $order_numbers[ $form_id ] ) ){
unset( $order_numbers[ $form_id ] );
update_option( 'wpmudev_fm_custom_submission_id', $order_numbers );
}
}
}
}
public function custom_order_number( $content, $custom_form, $data, $entry ){
// maybe exclude the form.
if( ! empty( $this->include_form_ids ) ){
if( ! in_array( $custom_form->id, $this->include_form_ids ) ){
return $content;
}
}elseif( ! empty( $this->exclude_form_ids ) && in_array( $custom_form->id, $this->exclude_form_ids ) ){
return $content;
}
if( version_compare( FORMINATOR_VERSION, '1.3.1', '>' ) ){
$submission_id = '#'. esc_html( $entry->entry_id );// add prefix # to avoid the conflict.
$replace = '#'. $this->get_number_order( $entry->form_id );
}else{
$submission_id = esc_html( $entry->form_id . $entry->entry_id );
$replace = $this->get_number_order( $entry->form_id );
}
if( false !== strpos( $content, $submission_id ) ){
$this->entry = $entry;
$content = str_replace( $submission_id, $replace, $content );
}
return $content;
}
public function increase_order_number(){
if( $this->entry ){
$order_number = $this->get_number_order( $this->entry->form_id );
// set to entry meta
$this->entry->set_fields(
array(
array(
'name'=> 'order_number',
'value' => $order_number
),
)
);
$order_number ++;
$order_numbers = get_option( 'wpmudev_fm_custom_submission_id', array() );
$order_numbers[ $this->entry->form_id ] = $order_number;
update_option( 'wpmudev_fm_custom_submission_id', $order_numbers );
// reset entry
$this->entry = null;
}
}
public function get_number_order( $form_id ){
static $order_number;
if( ! $order_number ){
$order_numbers = get_option( 'wpmudev_fm_custom_submission_id', array() );
if( isset( $order_numbers[ $form_id ] ) ){
$order_number = $order_numbers[ $form_id ];
}else{
$order_number = $this->start_number;
}
}
return $order_number;
}
public function custom_order_number_for_admin( $iterator, $entry ){
// maybe exclude the form.
if( ! empty( $this->include_form_ids ) ){
if( ! in_array( $entry->form_id, $this->include_form_ids ) ){
return $iterator;
}
}elseif( ! empty( $this->exclude_form_ids ) && in_array( $entry->form_id, $this->exclude_form_ids ) ){
return $iterator;
}
$order_number = $entry->get_meta('order_number');
if( $order_number ){
$iterator['entry_id'] .= '-'. $order_number;
$iterator['detail']['items'] = array( array_shift( $iterator['detail']['items'] ), array(
'type' => 'number',
'label' => 'Order',
'value' => '#'. $order_number,
'sub_entries' => array(),
) ) + $iterator['detail']['items'];
}
return $iterator;
}
}
$run = new WPMUDEV_FM_Order_Number;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment