Last active
May 13, 2024 03:17
-
-
Save alfredo-wpmudev/e92c5ad947aa479f26cdc22e06405dae to your computer and use it in GitHub Desktop.
Custom submission id as order number, available to display it with the tag {order}
This file contains 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] - Custom submission id as order number | |
* Description: [Forminator] - Custom submission id as order number, available to display it with the tag {order} | |
* Jira: SLS-224 | |
* Author: Thobk | Alfredo Galano Loyola @ 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 = [1199]; | |
private $exclude_form_ids = []; | |
private $start_number = 1; | |
private $order_number; | |
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); | |
//Replace {order} | |
add_filter('forminator_replace_form_data', array($this, 'wpmudev_replace_form_data'), 10, 3); | |
} | |
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; | |
} | |
$submission_id = '#' . esc_html($entry->entry_id); // add prefix # to avoid the conflict. | |
$replace = '#' . $this->format_order_number($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; | |
} | |
} | |
$this->order_number = $this->format_order_number($order_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#' . $this->format_order_number($order_number); | |
array_unshift($iterator['detail']['items'], array( | |
'type' => 'text', | |
'label' => 'Order', | |
'value' => '#' . $this->format_order_number($order_number), | |
'sub_entries' => array(), | |
)); | |
} | |
return $iterator; | |
} | |
public function format_order_number($order_number) | |
{ | |
return sprintf('%08d', $order_number); | |
} | |
//To replace the Order Number in the form where is used {order} | |
function wpmudev_replace_form_data($content, $data, $original_content) | |
{ | |
if (!empty($this->include_form_ids)) { | |
if (!in_array(intval($data['form_id']), $this->include_form_ids)) { | |
return $content; | |
} | |
} elseif (!empty($this->exclude_form_ids) && in_array(intval($data['form_id']), $this->exclude_form_ids)) { | |
return $content; | |
} | |
if (strpos($content, '{order}') !== false) { | |
$content = str_replace('{order}', $this->order_number, $content); | |
} | |
return $content; | |
} | |
} | |
$run = new WPMUDEV_FM_Order_Number; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment