1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flynt\Utils; |
4
|
|
|
|
5
|
|
|
class Log |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Outputs a message to the Web Console. |
9
|
|
|
* |
10
|
|
|
* Outputs a JavaScript `console.log`. |
11
|
|
|
* |
12
|
|
|
* @since 0.1.0 |
13
|
|
|
* |
14
|
|
|
* @param mixed $data Data to be logged. |
15
|
|
|
* @param boolean $postpone Postpone logging to the wp_footer action. |
16
|
|
|
*/ |
17
|
|
|
public static function console($data, $postpone = true) |
18
|
|
|
{ |
19
|
|
|
self::consoleDebug($data, $postpone); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Outputs an error message to the Web Console. |
24
|
|
|
* |
25
|
|
|
* Outputs a JavaScript `console.error`. |
26
|
|
|
* |
27
|
|
|
* @since 0.1.0 |
28
|
|
|
* |
29
|
|
|
* @param mixed $data Data to be logged. |
30
|
|
|
* @param boolean $postpone Postpone logging to the wp_footer action. |
31
|
|
|
*/ |
32
|
|
|
public static function error($data, $postpone = true) |
33
|
|
|
{ |
34
|
|
|
self::consoleDebug($data, $postpone, 'PHP', 'error'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Pretty prints a variable. |
39
|
|
|
* |
40
|
|
|
* Prints human-readable information about a variable. |
41
|
|
|
* |
42
|
|
|
* @since 0.1.0 |
43
|
|
|
* |
44
|
|
|
* @param mixed $data Data to be printed. |
45
|
|
|
* @param boolean $postpone Postpone printing to the wp_footer action. |
46
|
|
|
*/ |
47
|
|
|
public static function pp($data, $postpone = true) |
48
|
|
|
{ |
49
|
|
|
self::printDebug($data, $postpone); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Outputs a message to the Web Console. |
54
|
|
|
* |
55
|
|
|
* Outputs a JavaScript log. |
56
|
|
|
* Accepts log, error and trace. Can specify a title. |
57
|
|
|
* Will also output the data type, the filename and line number. |
58
|
|
|
* |
59
|
|
|
* @since 0.1.0 |
60
|
|
|
* |
61
|
|
|
* @param mixed $data Data to be logged. |
62
|
|
|
* @param boolean $postpone Postpone logging to the wp_footer action. |
63
|
|
|
* @param string $title A title. |
64
|
|
|
* @param string $logType The type of log (accepts log|error|trace). |
65
|
|
|
*/ |
66
|
|
|
public static function consoleDebug($data, $postpone = true, $title = 'PHP', $logType = 'log') |
67
|
|
|
{ |
68
|
|
|
if (in_array($logType, ['log', 'error', 'trace'])) { |
69
|
|
|
$title .= '(' . self::getCallerFile(2) . '):'; |
70
|
|
|
$type = gettype($data); |
71
|
|
|
$output = json_encode($data); |
72
|
|
|
$result = "<script>console.{$logType}('{$title}', '({$type})', {$output});</script>\n"; |
73
|
|
|
self::echoDebug($result, $postpone); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Pretty prints a variable. |
79
|
|
|
* |
80
|
|
|
* Prints human-readable information about a variable. |
81
|
|
|
* Print will be wrapped between <pre> tags. |
82
|
|
|
* Will also output the data type, the filename and line number. |
83
|
|
|
* |
84
|
|
|
* @since 0.2.0 |
85
|
|
|
* |
86
|
|
|
* @param mixed $data Data to be printed. |
87
|
|
|
* @param boolean $postpone Postpone printing to the wp_footer action. |
88
|
|
|
*/ |
89
|
|
|
public static function printDebug($data, $postpone = true) |
90
|
|
|
{ |
91
|
|
|
$type = gettype($data); |
92
|
|
|
$output = '<pre>'; |
93
|
|
|
$output .= '(' . $type . ') '; |
94
|
|
|
ob_start(); |
95
|
|
|
print_r($data); |
96
|
|
|
$output .= ob_get_clean(); |
97
|
|
|
$output .= '<br />File: <strong>' . self::getCallerFile(2) . '</strong>'; |
98
|
|
|
$output .= "</pre>\n"; |
99
|
|
|
self::echoDebug($output, $postpone); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Echoes a variable. |
104
|
|
|
* |
105
|
|
|
* @since 0.2.0 |
106
|
|
|
* |
107
|
|
|
* @param mixed $data Data to be printed. |
108
|
|
|
* @param boolean $postpone Postpone printing to the wp_footer action. |
109
|
|
|
*/ |
110
|
|
|
protected static function echoDebug($data, $postpone) |
111
|
|
|
{ |
112
|
|
|
if ($postpone) { |
113
|
|
|
add_action('wp_footer', function () use ($data) { |
114
|
|
|
echo $data; |
115
|
|
|
}, 30); |
116
|
|
|
} else { |
117
|
|
|
echo $data; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Returns a filename and line number by performing a debug_backtrace. |
123
|
|
|
* |
124
|
|
|
* @since 0.1.0 |
125
|
|
|
* |
126
|
|
|
* @param integer $depth Depth to return. |
127
|
|
|
* |
128
|
|
|
* @return string The filename and line number. |
129
|
|
|
*/ |
130
|
|
|
protected static function getCallerFile($depth = 1) |
131
|
|
|
{ |
132
|
|
|
$debug = debug_backtrace(); |
133
|
|
|
$fileName = $debug[$depth]['file']; |
134
|
|
|
$templateDir = get_template_directory() . '/'; |
135
|
|
|
return str_replace($templateDir, '', $fileName) . '#' . $debug[$depth]['line']; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|