Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[Forminator] - Add time field to hidden input. Add time field to hidden input in format H:i:s
<?php
/**
* Plugin Name: [Forminator] - Add time field to hidden input
* Plugin URI: https://premium.wpmudev.org/
* Description: Add time field to hidden input in format H:i:s (as of 1.9.2)
* Author: Alessandro Kaounas @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WPMUDEV_Forminator_Hidden_Input_Time_Field' ) ) {
class WPMUDEV_Forminator_Hidden_Input_Time_Field {
private static $_instance = null;
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_Forminator_Hidden_Input_Time_Field();
}
return self::$_instance;
}
private function __construct() {
$this->init();
}
public function init(){
add_filter( 'forminator_custom_form_submit_field_data', array( $this, 'wpmudev_forminator_custom_form_submit_field_data' ), 10, 2 );
add_filter( 'forminator_vars_list', array( $this, 'wpmudev_forminator_vars_lists' ) );
add_filter( 'forminator_replace_variables', array( $this, 'wpmudev_forminator_replace_variables' ) );
add_filter( 'forminator_field_hidden_field_value', array( $this, 'wpmudev_forminator_field_hidden_field_value' ), 10, 3 );
}
public function wpmudev_forminator_custom_form_submit_field_data( $field_data_array, $form_id ) {
$custom_form = Forminator_Custom_Form_Model::model()->load( $form_id );
$fields = array();
if ( is_object( $custom_form ) ) {
foreach( $custom_form->fields as $field ){
if( $field->raw['type'] === 'hidden' && $field->raw['default_value'] === 'time_his' ){
$fields[] = $field->raw['element_id'];
}
}
}
if( empty( $fields ) ){
return $field_data_array;
}
// Add current timestamp
foreach( $field_data_array as &$field ){
if( in_array( $field['name'], $fields ) ){
$field['value'] = date_i18n( 'H:i:s', forminator_local_timestamp( time() ), true );
}
}
return $field_data_array;
}
public function wpmudev_forminator_vars_lists( $vars_list ){
$vars_list = array(
'user_ip' => esc_html__( 'User IP Address', Forminator::DOMAIN ),
'date_mdy' => esc_html__( 'Date (mm/dd/yyyy)', Forminator::DOMAIN ),
'date_dmy' => esc_html__( 'Date (dd/mm/yyyy)', Forminator::DOMAIN ),
'time_his' => esc_html__( 'Time (H:i:s)', Forminator::DOMAIN ),
'embed_id' => esc_html__( 'Embed Post/Page ID', Forminator::DOMAIN ),
'embed_title' => esc_html__( 'Embed Post/Page Title', Forminator::DOMAIN ),
'embed_url' => esc_html__( 'Embed URL', Forminator::DOMAIN ),
'user_agent' => esc_html__( 'HTTP User Agent', Forminator::DOMAIN ),
'refer_url' => esc_html__( 'HTTP Refer URL', Forminator::DOMAIN ),
'user_id' => esc_html__( 'User ID', Forminator::DOMAIN ),
'user_name' => esc_html__( 'User Display Name', Forminator::DOMAIN ),
'user_email' => esc_html__( 'User Email', Forminator::DOMAIN ),
'user_login' => esc_html__( 'User Login', Forminator::DOMAIN ),
'custom_value' => esc_html__( 'Custom Value', Forminator::DOMAIN ),
);
return $vars_list;
}
public function wpmudev_forminator_replace_variables( $content ) {
// If we have no variables, skip
if ( strpos( $content, '{' ) !== false ) {
$time = date_i18n( 'H:i:s', forminator_local_timestamp(), true );
$content = str_replace( '{time_his}', $time, $content );
}
return $content;
}
public function wpmudev_forminator_field_hidden_field_value( $value, $saved_value, $field ){
$value = '';
$saved_value = Forminator_Field::get_property( 'default_value', $field );
$embed_url = forminator_get_current_url();
switch( $saved_value ) {
case "user_ip":
$value = Forminator_Geo::get_user_ip();
break;
case "time_his":
$value = date_i18n( 'H:i:s', forminator_local_timestamp(), true );
break;
case "date_mdy":
$value = date_i18n( 'm/d/Y', forminator_local_timestamp(), true );
break;
case "date_dmy":
$value = date_i18n( 'd/m/Y', forminator_local_timestamp(), true );
break;
case "embed_id":
$value = forminator_get_post_data( 'ID' );
break;
case "embed_title":
$value = forminator_get_post_data( 'post_title' );
break;
case "embed_url":
$value = $embed_url;
break;
case "user_agent":
$value = $_SERVER[ 'HTTP_USER_AGENT' ];
break;
case "refer_url":
$value = isset ( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : $embed_url;
break;
case "user_id":
$value = forminator_get_user_data( 'ID' );
break;
case "user_name":
$value = forminator_get_user_data( 'display_name' );
break;
case "user_email":
$value = forminator_get_user_data( 'user_email' );
break;
case "user_login":
$value = forminator_get_user_data( 'user_login' );
break;
case "custom_value":
$value = self::get_property( 'custom_value', $field );
break;
default:
break;
}
return $value;
}
}
if ( ! function_exists( 'wpmudev_forminator_hidden_input_time_field' ) ) {
function WPMUDEV_Forminator_Hidden_Input_Time_Field() {
return WPMUDEV_Forminator_Hidden_Input_Time_Field::get_instance();
};
add_action( 'plugins_loaded', 'wpmudev_forminator_hidden_input_time_field', 10 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.