Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save marcus-at-localhost/0c8256b6c4cc32a00210d175ac2efd11 to your computer and use it in GitHub Desktop.
Save marcus-at-localhost/0c8256b6c4cc32a00210d175ac2efd11 to your computer and use it in GitHub Desktop.
[Forminator] - Custom form submission date format
<?php
/**
* Plugin Name: [Forminator] - Custom form submission date format (Rev.1.1)
* Description: This MU Plugin allows the usage of a custom date format for the Submission page
* Author: Anderson Salas @ WPMUDEV
* Task: SLS-3998
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*/
//
// NOTE: This doesn't affect the Quizzes/Polls submission date format. That date format
// is taken from the Settings > General > Date Format section at the WP Admin.
//
add_action( 'plugins_loaded', function(){
// >>> [ Snippet settings BEGIN ] -------------------------------------------------
$new_format = 'Y-m-d\TH:i:s\Z';
// >>> [ Snippet settings END ] ---------------------------------------------------
add_filter( 'forminator_custom_form_entries_iterator', function( $iterator, $entry ) use ( $new_format ) {
$dates = array();
if ( ! empty( $iterator['entry_date'] ) ) {
$dates[] = &$iterator['entry_date'];
}
if ( ! empty( $iterator['summary']['items'][1]['value'] ) ) {
$dates[] = &$iterator['summary']['items'][1]['value'];
}
if ( ! empty( $iterator['detail']['items'][0]['value'] ) ) {
$dates[] = &$iterator['detail']['items'][0]['value'];
}
foreach ( $dates as &$date ) {
$new_date = \DateTime::createFromFormat( "M d, Y \@ g:i A", $date );
if ( $new_date instanceof \DateTimeInterface ) {
$date = $new_date->format( $new_format );
continue;
}
$timestamp = strtotime( $date );
if ( false !== $timestamp ) {
$date = date( $new_format, $timestamp );
}
}
return $iterator;
}, 10, 2 );
if ( ! is_admin() || ( ! current_user_can( 'manage_options' ) && ! current_user_can( 'manage_forminator' ) ) ) {
return;
}
$nonce = ! empty( $_POST['_forminator_nonce'] )
? sanitize_text_field( wp_unslash( $_POST['_forminator_nonce'] ) )
: '';
if ( ! wp_verify_nonce( $nonce, 'forminator_export' ) ) {
return;
}
add_filter( 'forminator_addon_slugs_from_entry_model', function( $addon_slugs, $entry_model, $addon_metadata_pattern_prefix ) {
$GLOBALS['_for_ee'][] = $entry_model;
return $addon_slugs;
}, 10, 3);
add_filter( 'forminator_custom_form_export_mappers', function( $mappers, $model_id, $model ) use ( $new_format ) {
$GLOBALS['_for_ee'] = array();
$GLOBALS['_for_ee_i'] = 0;
$cloned_meta_key = $mappers[1]['meta_key'] ?? false;
if ( false === $cloned_meta_key ) {
return $mappers;
}
if ( ! empty( $mappers[0]['property'] ) && 'time_created' === $mappers[0]['property'] ) {
$mappers[0] = array(
'meta_key' => $cloned_meta_key,
'label' => esc_html__( 'Submission Time', 'forminator' ),
'type' => '%%DATE%%',
);
add_filter( 'forminator_entry_meta_value_to_string', function( $string_value, $field_type, $meta_value, $allow_html, $truncate ) use ( $new_format ) {
if ( '%%DATE%%' === $field_type ) {
if ( isset( $GLOBALS['_for_ee'][ $GLOBALS['_for_ee_i'] ] ) ) {
$entry = $GLOBALS['_for_ee'][ $GLOBALS['_for_ee_i']++ ];
$date = date( $new_format, strtotime( $entry->date_created_sql ) );
return $date ?? $entry->time_created;
}
return $string_value;
}
return $string_value;
}, 10, 5 );
}
return $mappers;
}, 10, 3 );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment