forked from schavery/sdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.php
73 lines (60 loc) · 1.68 KB
/
message.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
<?php
/**
* message handler
*/
require_once WP_PLUGIN_DIR . '/sdf/types.php';
function sdf_message_handler($type, $message) {
// type = (ERROR | SUCCESS | LOG | DEBUG)
// all messages are written to sdf.log
// rotated every six months
// success and error messages are passed as an object to the waiting js
// the data structure is json, and should be very simple.
// data.type = error | success
// data.message = message
if($type > SDFLOGLEVEL) {
return;
}
switch($type) {
case \SDF\MessageTypes::ERROR: $type = 'error'; break;
case \SDF\MessageTypes::SUCCESS: $type = 'success'; break;
case \SDF\MessageTypes::LOG: $type = 'log'; break;
case \SDF\MessageTypes::DEBUG: $type = 'debug'; break;
}
$logmessage = sprintf('%s - %s - %s%s',
date('D, d M Y H:i:s'), $type, $message, PHP_EOL);
file_put_contents(WP_PLUGIN_DIR . '/sdf/sdf.log', $logmessage, FILE_APPEND);
// send data to the requestor
if($type == 'error' || $type == 'success') {
ob_clean();
$data = array(
'type' => $type,
'message' => $message
);
echo json_encode($data);
ob_flush();
die();
}
}
function sdf_clean_log() {
$file = WP_PLUGIN_DIR . '/sdf/sdf.log';
$handle = fopen($file, 'r+');
$linecount = 0;
while(!feof($handle)) {
$line = fgets($handle);
$linecount++;
}
ftruncate($handle, 0);
rewind($handle);
fwrite($handle, time() . ' - Cron run. '
. $linecount . ' lines cleared.' . PHP_EOL);
fclose($handle);
}
function sdf_add_biannual($schedules) {
$schedules['biannual'] = array(
'interval' => 10000000,
'display' => __('Every Six Months')
);
return $schedules;
}
add_filter('cron_schedules', 'sdf_add_biannual');
add_action('sdf_biannual_hook', 'sdf_clean_log'); ?>