-
Notifications
You must be signed in to change notification settings - Fork 0
/
audit-trail.php
152 lines (120 loc) · 3.7 KB
/
audit-trail.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/*
Plugin Name: WP Document Revisions - Audit Trail Code Sample
Plugin URI: http://
Description: Code sample to demonstrate check-in/check-out audit trail functionality
Version: 1.0
Author: Benjamin J. Balter
Author URI: http://ben.balter.com
License: GPL2
*/
/**
* Helper function to return audit trail as an array
* array is the form array( 'timestamp' = YYYY-MM-DD H:i:s, 'user' => {userID}, 'action' => {Check in|Check Out}
*/
function wpdr_get_audit_trail( $postID ) {
$downloads = wpdr_get_downloads( $postID );
$uploads = wpdr_get_uploads( $postID );
//merge uploads and downloads into a single trail
$trail = array_merge( $downloads, $uploads );
//sort by timestamp
wpdr_sort( $trail, 'timestamp' );
return $trail;
}
/**
* Returns array of downloads from post meta
*/
function wpdr_get_downloads( $postID ) {
$downloads = get_post_meta( $postID, 'document_audit' );
//get_post_meta returns false if there are no results, but we use array_merge later, so force array
if ( !is_array( $downloads ) )
return array();
//sort by timestamp
wpdr_sort( $downloads, 'timestamp' );
return $downloads;
}
/**
* Parses standard WP revision history into an array for the audit trail
*/
function wpdr_get_uploads( $postID ) {
global $post;
$uplaods = array();
//get revisions using our internal function
$wpdr = Document_Revisions::$instance;
$revisions = $wpdr->get_revisions( $post->ID );
//loop through and build an array
foreach ( $revisions as $revision )
$uploads[] = array( 'timestamp' => $revision->post_date,
'user' => $revision->post_author,
'action' => 'Check In'
);
//sort by timestamp
wpdr_sort( $uploads, 'timestamp' );
return $uploads;
}
/**
* Logs file downloads by user and time
*/
function wpdr_log_download( $postID ) {
//make sure we have the post parent not the revision
if ( $parent = wp_is_post_revision( $postID ) )
$postID = $parent;
//get the current user
$user = wp_get_current_user();
//format data array
$data = array( 'timestamp' => current_time( 'mysql' ),
'user' => $user->ID,
'action' => 'Check Out'
);
//store the meta
add_post_meta( $postID, 'document_audit', $data );
}
add_action( 'serve_document', 'wpdr_log_download', 10, 1 );
/**
* Sorts an array of arrays by a specific key
* From: http://stackoverflow.com/questions/2699086/php-sort-multidimensional-array-by-value
*/
function wpdr_sort(&$arr, $col, $dir = SORT_ASC) {
$sort_col = array();
foreach ($arr as $key=> $row) {
$sort_col[$key] = $row[$col];
}
array_multisort($sort_col, $dir, $arr);
}
/**
* Formats and outputs audit trail metabox
*/
function wpdr_audit_metabox( $post ) {
//get the trail
$trail = wpdr_get_audit_trail( $post->ID );
//if there is no trail, kick
if ( sizeof( $trail ) == 0 )
return;
?>
<table width="100%">
<tr>
<th style="text-align: left;">Time</th>
<th style="text-align: left;">User</th>
<th style="text-align: left;">Event</th>
</tr>
<?php
foreach ( $trail as $event ) {
$user = get_user_by( 'id', $event['user'] );
?>
<tr>
<td><abbr class="timestamp" title="<?php echo $event['timestamp']; ?>" id="<?php echo strtotime( $event['timestamp'] ); ?>"><?php echo human_time_diff( strtotime( $event['timestamp'] ), current_time( 'timestamp' ) ); ?></abbr> ago</td>
<td><?php echo $user->display_name; ?></td>
<td><?php echo $event['action']; ?></td>
</tr>
<?php } ?>
</table>
<?php
}
/**
* Registers the audit trail metabox with the wordpress metabox API
*/
function wpdr_add_audit_metabox() {
add_meta_box( 'document_audit_trail', 'Audit Trail', 'wpdr_audit_metabox', 'document', 'normal', 'low' );
}
add_action( 'add_meta_boxes', 'wpdr_add_audit_metabox');
?>