-
Notifications
You must be signed in to change notification settings - Fork 0
/
inesonic-history.php
385 lines (336 loc) · 14 KB
/
inesonic-history.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?php
/**
* Plugin Name: Inesonic History
* Plugin URI: http://www.inesonic.com
* Description: A small proprietary plug-in that provides history tracking for customer activities.
* Version: 1.0.0
* Author: Inesonic, LLC
* Author URI: http://www.inesonic.com
*/
/***********************************************************************************************************************
* Copyright 2020 - 2022, Inesonic, LLC.
*
* GNU Public License, Version 3:
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see
* <https://www.gnu.org/licenses/>.
***********************************************************************************************************************
*/
require_once dirname(__FILE__) . '/include/rewrite-base.php';
/**
* Inesonic WordPress plug-in class that tracks customer history.
*/
class InesonicHistory extends \Inesonic\History\RewriteBase {
const VERSION = '1.0.0';
const SLUG = 'inesonic-history';
const NAME = 'Inesonic History Tracker';
const AUTHOR = 'Inesonic, LLC';
const PREFIX = 'InesonicHistory';
/**
* The slug to use for the customer history.
*/
const CUSTOMER_HISTORY_SLUG = 'inesonic-customer-history';
/**
* The slug to return to the users page.
*/
const USERS_PAGE_SLUG = '/wp-admin/users.php';
/**
* The singleton class instance.
*/
private static $instance; /* Plug-in instance */
/**
* The plug-in directory.
*/
public static $dir = ''; /* Plug-in directory */
/**
* The plug-in URL.
*/
public static $url = ''; /* Plug-in URL */
/**
* Our Twig template loader.
*/
public static $loader = null; /* The template loader */
/**
* Our Twig template environment.
*/
public static $template_environment = null; /* The template environment */
/**
* Method that is called to initialize a single instance of the plug-in
*/
public static function instance() {
if (!isset(self::$instance) &&
!(self::$instance instanceof InesonicHistory) ) {
self::$instance = new InesonicHistory();
self::$dir = plugin_dir_path(__FILE__);
self::$url = plugin_dir_url(__FILE__);
spl_autoload_register(array(self::$instance, 'autoloader'));
}
}
/**
* Static method that is triggered when the plug-in is activated.
*/
public static function plugin_activated() {
self::rewrite_plugin_activated(self::CUSTOMER_HISTORY_SLUG);
if (defined('ABSPATH') && current_user_can('activate_plugins')) {
$plugin = isset($_REQUEST['plugin']) ? sanitize_text_field($_REQUEST['plugin']) : '';
if (check_admin_referer('activate-plugin_' . $plugin)) {
global $wpdb;
$wpdb->query(
'CREATE TABLE IF NOT EXISTS ' . $wpdb->prefix . 'inesonic_history' . ' (' .
'event_id BIGINT UNSIGNED AUTO_INCREMENT,' .
'user_id BIGINT UNSIGNED NOT NULL,' .
'event_timestamp BIGINT UNSIGNED NOT NULL,' .
'event_type VARCHAR(40) NOT NULL,' .
'additional TEXT NOT NULL,' .
'PRIMARY KEY (event_id)' .
')'
);
}
}
}
/**
* Static method that is triggered when the plug-in is deactivated.
*/
public static function plugin_deactivated() {
self::rewrite_plugin_deactivated(self::CUSTOMER_HISTORY_SLUG);
}
/**
* Static method that is triggered when the plug-in is uninstalled.
*/
public static function plugin_uninstalled() {
if (defined('ABSPATH') && current_user_can('activate_plugins')) {
$plugin = isset($_REQUEST['plugin']) ? sanitize_text_field($_REQUEST['plugin']) : '';
if (check_admin_referer('deactivate-plugin_' . $plugin)) {
global $wpdb;
$wpdb->query('DROP TABLE ' . $wpdb->prefix . 'inesonic_history');
}
}
self::rewrite_plugin_uninstalled(self::CUSTOMER_HISTORY_SLUG);
}
/**
* Constructor
*/
public function __construct() {
parent::__construct(self::CUSTOMER_HISTORY_SLUG);
add_action('init', array($this, 'customize_on_initialization'));
add_action('delete_user', array($this, 'about_to_delete_user'), 10, 3);
add_filter('user_row_actions', array($this, 'add_user_row_actions'), 100, 2);
/* Action: inesonic_add_history
*
* Adds a history record.
*
* Parameters:
*
* $user_id - The ID of the user this history record should be tied to.
*
* $event_type - A string holding the event type.
*
* $additional - Additional textual information tied to the event.
*/
add_action('inesonic_add_history', array($this, 'add_history_record'), 10, 3);
}
/**
* PHP PSR-4 autoloader.
*
* \param[in] $class_name The class to be auto-loaded.
*/
public function autoloader($class_name) {
if (!class_exists($class_name) and (FALSE !== strpos($class_name, self::PREFIX))) {
$class_name = str_replace(self::PREFIX, '', $class_name);
$classes_dir = realpath(plugin_dir_path(__FILE__)) . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR;
$class_file = str_replace('_', DIRECTORY_SEPARATOR, $class_name) . '.php';
if (file_exists($classes_dir . $class_file)) {
require_once $classes_dir . $class_file;
}
}
}
/**
* Method that performs additional initialization on WordPress initialization.
*/
function customize_on_initialization() {
}
/**
* Method that is triggered just before we delete a user from the database.
*
* \param[in] $user_id The user ID of the user that is being deleted.
*
* \param[in] $reassigned_to The user ID of the user taking over this user. A value of null indicates no
* reassignemnt.
*
* \param[in] $user_data The WP_User object for the user being deleted.
*/
public function about_to_delete_user($user_id, $reassigned_to, $user_data) {
global $wpdb;
$wpdb->delete($wpdb->prefix . 'inesonic_history', array('user_id' => $user_id), array('%d'));
$first_name = get_user_meta($user_data->ID, 'first_name', true);
$last_name = get_user_meta($user_data->ID, 'last_name', true);
$company = get_user_meta($user_data->ID, 'company', true);
$email_address = $user_data->user_email;
$additional = $first_name . ' ' . $last_name . ' / ' . $company . ' - ' . $email_address .
'(' . implode(",", $user_data->roles) . ')';
$this->add_history_record($user_id, 'USER_DELETED', $additional);
}
/**
* Function that is triggered to add additional actions to the user's menu, per user. We also remove actions that
* do not make sense for this site.
*
* \param[in] $actions An array of actions to be updated.
*
* \param[in] $user_object The user object for the specific user.
*
* \return Returns the modified user actions.
*/
public static function add_user_row_actions($actions, $user_object) {
$current_user = wp_get_current_user();
if (!is_multisite() && current_user_can('edit_user', $user_object->ID )) {
$actions['history'] = '<a class="history" href="' .
site_url(self::CUSTOMER_HISTORY_SLUG . '/?customer_id=' . $user_object->ID) .
'">' .
__("History", 'inesonic-history') .
'</a>';
}
unset($actions['resetpassword']);
unset($actions['view']);
return $actions;
}
/**
* Method that is triggered when a new history record is to be added.
*
* \param[in] $user_id The ID of the user.
*
* \param[in] $event_type A free-from string indicating the event type.
*
* \param[in] $additional Additional text to include with the history record.
*/
public function add_history_record($user_id, $event_type, $additional) {
global $wpdb;
$wpdb->insert(
$wpdb->prefix . 'inesonic_history',
array(
'user_id' => $user_id,
'event_timestamp' => time(),
'event_type' => $event_type,
'additional' => $additional
),
array(
'%d',
'%d',
'%s',
'%s'
)
);
}
/**
* Method that handles the redirect. Overload this method in derived classes.
*/
public function process_redirect() {
if (current_user_can('edit_users')) {
global $wpdb;
$query_string = 'SELECT * FROM ' . $wpdb->prefix . 'inesonic_history';
$first_condition = true;
if (array_key_exists('customer_id', $_REQUEST)) {
$customer_id = intval(sanitize_key($_REQUEST['customer_id']));
$query_string .= ' WHERE user_id=' . $customer_id;
$first_condition = false;
} else {
$customer_id = null;
}
if (array_key_exists('start_timestamp', $_REQUEST)) {
$start_timestamp = intval(sanitize_key($_REQUEST['start_timestamp']));
if ($first_condition) {
$query_string .= ' WHERE event_timestamp>=' . $start_timestamp;
$first_condition = false;
} else {
$query_string .= ' AND event_timestamp>=' . $start_timestamp;
}
}
if (array_key_exists('end_timestamp', $_REQUEST)) {
$end_timestamp = intval(sanitize_key($_REQUEST['end_timestamp']));
if ($first_condition) {
$query_string .= ' WHERE event_timestamp<=' . $end_timestamp;
$first_condition = false;
} else {
$query_string .= ' AND event_timestamp<=' . $end_timestamp;
}
} else {
$end_timestamp = null;
}
$query_string .= ' ORDER BY event_timestamp ASC';
$query_results = $wpdb->get_results($query_string);
echo __(
'<style>
table {
border-collapse: collapse;
border: solid 1px black;
}
thead {
font-weight: bold;
}
td {
padding: 5px;
border-collapse: collapse;
border: solid 1px gray;
}
.nowrap {
white-space: nowrap;
}
</style>',
'inesonic-history'
);
if ($customer_id === null || $customer_id == 0) {
echo "<h1>" . __("All Events:") . "</h1>";
} else {
echo "<h1>" . sprintf(__("Events For Customer %d:", 'inesonic-history'), $customer_id) . "</h1>";
}
echo '<table>
<thead>
<tr>
<td>#</td>
<td class="nowrap">' . __("Customer ID", 'inesonic-history') . '</td>
<td class="nowrap">' . __("Event Date/Time", 'inesonic-history') . '</td>
<td class="nowrap">' . __("Event Type", 'inesonic-history') . '</td>
<td class="nowrap">' . __("Additional", 'inesonic-history') . '</td>
</tr>
</thead>
<tbody>';
foreach($query_results as $event) {
echo '<tr>
<td>' . esc_html($event->event_id) . '</td>
<td>' . esc_html($event->user_id) . '</td>
<td>' . esc_html(date('Y-m-d H:i:s', $event->event_timestamp)) . '</td>
<td>' . esc_html($event->event_type) . '</td>
<td>' . esc_html($event->additional) . '</td>
</tr>';
}
echo ' </tbody>" .
</table>
<p align="center">';
if ($customer_id !== null && $customer_id != 0) {
echo '<a href="' . site_url($this->rewrite_slug()) . '">' .
__("Show History For All Users", 'inesonic-history') .
'</a>
';
}
echo '<a href="' . site_url(self::USERS_PAGE_SLUG) . '">' .
__("Returns To Administrative Page", 'inesonic-history') .
'</a>
</p>';
die();
} else {
// By exiting this function, the function will behave as if the slug is not valid.
}
}
}
/* Instatiate our plug-in. */
InesonicHistory::instance();
/* Define critical global hooks. */
register_activation_hook(__FILE__, array('InesonicHistory', 'plugin_activated'));
register_deactivation_hook(__FILE__, array('InesonicHistory', 'plugin_deactivated'));
register_uninstall_hook(__FILE__, array('InesonicHistory', 'plugin_uninstalled'));