Skip to content

Instantly share code, notes, and snippets.

@apos37
Last active July 22, 2024 20:34
Show Gist options
  • Save apos37/1f820eddc5a69851de4b6b0be4fff35d to your computer and use it in GitHub Desktop.
Save apos37/1f820eddc5a69851de4b6b0be4fff35d to your computer and use it in GitHub Desktop.
Gravity Forms Track Who Created and Modified Forms
<?php
/**
* Track who created and modified Gravity Forms
* Updates the form object with 'created_by' on first form save.
* Updates the form object with 'date_modified' and 'modified_by' on form saves thereafter
* Adds a "Log" column to the forms list table
*/
// Exit if accessed directly.
if ( !defined( 'ABSPATH' ) ) {
exit;
}
/**
* Initiate the class
*/
new MY_GF_CLASS();
/**
* The class.
*/
class MY_GF_CLASS {
/**
* Constructor
*/
public function __construct() {
// Save modified on form save
add_action( 'gform_after_save_form', [ $this, 'log_form_saved' ], 10, 2 );
// Add columns to the forms list
add_filter( 'gform_form_list_columns', [ $this, 'forms_columns' ], 10, 1 );
add_action( 'gform_form_list_column_log', [ $this, 'forms_column_content' ] );
} // End __construct()
/**
* Add who and when form was saved
*
* @param array $form
* @param boolean $is_new
* @return void
*/
public function log_form_saved( $form, $is_new ) {
// Get the current user
$user_id = get_current_user_id();
// Fields to update
if ( $is_new ) {
$form[ 'created_by' ] = $user_id;
// date_created already exists in form object
} else {
$form[ 'modified_by' ] = $user_id;
$form[ 'date_modified' ] = date( 'Y-m-d H:i:s' );
}
// Gotta make it active or it will make it inactive by default
$form[ 'is_active' ] = true;
// Update the form meta
GFAPI::update_form( $form );
} // End log_form_saved()
/**
* Forms columns
*
* @param array $columns
* @return array
*/
public function forms_columns( $columns ) {
$columns = array_merge( $columns, [
'log' => esc_html__( 'Log', 'gravityforms' )
] );
return $columns;
} // End forms_columns()
/**
* Forms column content
*
* @param object $form
* @return void
*/
public function forms_column_content( $form ) {
// Actions
$actions = [];
// Get the full form object
$form = GFAPI::get_form( $form->id );
// Created by
if ( isset( $form[ 'created_by' ] ) ) {
$created_by_id = absint( $form[ 'created_by' ] );
$created_by_user = get_user_by( 'ID', $created_by_id );
$incl_created_by = ' by '.$created_by_user->display_name;
} else {
$incl_created_by = '';
}
$actions[] = 'Created on '.date( 'n/j/Y', strtotime( $form[ 'date_created' ] ) ).$incl_created_by;
// Modified by
if ( isset( $form[ 'date_modified' ] ) ) {
if ( isset( $form[ 'modified_by' ] ) ) {
$modified_by_id = absint( $form[ 'modified_by' ] );
$modified_by_user = get_user_by( 'ID', $modified_by_id );
$incl_modified_by = ' by '.$modified_by_user->display_name;
} else {
$incl_modified_by = '';
}
$actions[] = '<em>Last Modified on '.date( 'n/j/Y', strtotime( $form[ 'date_modified' ] ) ).$incl_modified_by.'</em>';
}
// Return the actions
if ( !empty( $actions ) ) {
echo wp_kses( implode( '<br>', $actions ), [ 'br' => [], 'em' => [] ] );
}
} // End forms_column_content()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment