Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save patrickfreitasdev/4078ef5c277036b9d875ba1092095572 to your computer and use it in GitHub Desktop.
Save patrickfreitasdev/4078ef5c277036b9d875ba1092095572 to your computer and use it in GitHub Desktop.
[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_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' ),
'date_mdy' => esc_html__( 'Date (mm/dd/yyyy)', 'forminator' ),
'date_dmy' => esc_html__( 'Date (dd/mm/yyyy)', 'forminator' ),
'time_his' => esc_html__( 'Time (H:i:s)', 'forminator' ),
'embed_id' => esc_html__( 'Embed Post/Page ID', 'forminator' ),
'embed_title' => esc_html__( 'Embed Post/Page Title', 'forminator' ),
'embed_url' => esc_html__( 'Embed URL', 'forminator' ),
'user_agent' => esc_html__( 'HTTP User Agent', 'forminator' ),
'refer_url' => esc_html__( 'HTTP Refer URL', 'forminator' ),
'user_id' => esc_html__( 'User ID', 'forminator' ),
'user_name' => esc_html__( 'User Display Name', 'forminator' ),
'user_email' => esc_html__( 'User Email', 'forminator' ),
'user_login' => esc_html__( 'User Login', 'forminator' ),
'custom_value' => esc_html__( 'Custom Value', 'forminator' ),
);
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