Skip to content

Commit e4f566e

Browse files
authored
gpeb-entry-edit-timestamp.php: Added snippet to display edit entry timestamp.
1 parent 5a38c4d commit e4f566e

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
/**
3+
* Gravity Perks // Entry Blocks // Shows a log of edits that have been made to an entry.
4+
* http://gravitywiz.com/
5+
*
6+
* * Adds `{entry_edit_log}` merge tag to display Edit Entry Timestamp.
7+
* * Adds `{last_edited_by}` merge tag to display the User who last edited the entry.
8+
*
9+
* Instruction Video: https://www.loom.com/share/af4e456263324971b3463ff344c77830
10+
*
11+
*/
12+
class GPEB_Entry_Edit_Log {
13+
const META_KEY = 'gw_entry_edit_log';
14+
15+
public function __construct() {
16+
add_action( 'init', array( $this, 'init' ) );
17+
}
18+
19+
public function init() {
20+
21+
add_action( 'gform_after_update_entry', array( $this, 'update_meta_after_update_entry' ), 10, 2 );
22+
add_action( 'gform_post_update_entry', array( $this, 'update_meta_post_update_entry' ), 10, 2 );
23+
add_filter( 'gform_replace_merge_tags', array( $this, 'replace_last_edited_by' ), 10, 7 );
24+
add_filter( 'gform_replace_merge_tags', array( $this, 'replace_entry_edit_log' ), 10, 7 );
25+
26+
}
27+
28+
public function update_meta_after_update_entry( $form, $entry_id ) {
29+
$this->update_entry_meta( $entry_id );
30+
}
31+
32+
public function update_meta_post_update_entry( $entry, $original_entry ) {
33+
$this->update_entry_meta( rgar( $entry, 'id' ) );
34+
}
35+
36+
function update_entry_meta( $entry_id ) {
37+
$log = $this->get_log( $entry_id );
38+
39+
// Use timestamp as key to allow for sorting
40+
$time = current_time( 'mysql' );
41+
42+
$log[ $time ] = array(
43+
'time' => $time,
44+
'user' => get_current_user_id(),
45+
);
46+
47+
gform_update_meta( $entry_id, self::META_KEY, $log );
48+
}
49+
50+
public function get_log( $entry_id ) {
51+
$log = gform_get_meta( $entry_id, self::META_KEY );
52+
53+
if ( empty( $log ) || ! is_array( $log ) ) {
54+
$log = array();
55+
}
56+
57+
return $log;
58+
}
59+
60+
public function get_last_edit( $entry_id ) {
61+
$log = $this->get_log( $entry_id );
62+
63+
if ( empty( $log ) ) {
64+
return null;
65+
}
66+
67+
// Get the last edit, which is the last item in the array
68+
$last_edit = end( $log );
69+
70+
return $last_edit;
71+
}
72+
73+
public function replace_last_edited_by( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) {
74+
$custom_merge_tag = '{last_edited_by}';
75+
76+
if ( strpos( $text, $custom_merge_tag ) === false ) {
77+
return $text;
78+
}
79+
80+
$last_edit = $this->get_last_edit( $entry['id'] );
81+
82+
if ( empty( $last_edit ) ) {
83+
return str_replace( $custom_merge_tag, '', $text );
84+
}
85+
86+
$last_edit_by = get_user_by( 'id', $last_edit['user'] )->display_name;
87+
88+
return str_replace( $custom_merge_tag, $last_edit_by, $text );
89+
}
90+
91+
public function replace_entry_edit_log( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) {
92+
$custom_merge_tag = '{entry_edit_log}';
93+
94+
if ( strpos( $text, $custom_merge_tag ) === false ) {
95+
return $text;
96+
}
97+
98+
$log = $this->get_log( $entry['id'] );
99+
100+
if ( empty( $log ) ) {
101+
return str_replace( $custom_merge_tag, '', $text );
102+
}
103+
104+
$log_formatted = '<ul>';
105+
106+
foreach ( $log as $edit ) {
107+
$user = get_user_by( 'id', $edit['user'] );
108+
$log_formatted .= sprintf( '<li>Entry updated at %s by %s.</li>', mysql2date( 'Y-m-d H:i:s', $edit['time'] ), $user->display_name );
109+
}
110+
111+
$log_formatted .= '</ul>';
112+
113+
return str_replace( $custom_merge_tag, $log_formatted, $text );
114+
}
115+
}
116+
117+
# Configuration
118+
119+
new GPEB_Entry_Edit_Log();

0 commit comments

Comments
 (0)