/
functions.php
155 lines (132 loc) · 3.43 KB
/
functions.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
<?php
namespace WP_Dbug;
/**
* create the log directory if it does not exist
* default to /logs/ in wordpress root
* @TODO find a better way to make sure the path is writeable and valid
* @TODO fix error when log path is not on same server.
* @param string
* @return string absolute path to directory or FALSE
*/
function check_log_dir($dir)
{
if (!is_dir($dir)) {
$wp_upload_dir = wp_upload_dir();
$dir = $wp_upload_dir['basedir'].'/logs/';
}
$pathinfo = pathinfo( $dir );
$dirname = isset( $pathinfo['dirname'] ) ? $pathinfo['dirname'] : null;
if (!is_dir($dirname)) {
return false;
}
// force trailing slash!
if (strrpos($dir, '/') != (strlen($dir)-1)) {
$dir .= '/';
}
// make directory if it doesnt exist
if (!is_dir($dir)) {
wp_mkdir_p( $dir, 0755 );
}
// change permissions if we cant write to it
if (!is_writable($dir)) {
@chmod( $dir, 0755 );
}
// test and make sure we can write to it
if (!is_dir($dir) || !is_writable($dir)) {
return false;
}
// make sure htaccess is in place to protect log files
if (!file_exists($dir.'.htaccess') && file_exists(__DIR__.'/_htaccess.php')) {
copy( __DIR__.'/_htaccess.php',
$dir.'.htaccess' );
}
return $dir;
}
/**
* get the type of method or property. is there a better way to do this?
* @param ReflectionMethod | ReflectionProperty
* @return string
*/
function get_type($r)
{
if ($r->isPublic()) {
$type = 'public';
} elseif ($r->isPrivate()) {
$type = 'private';
} elseif ($r->isProtected()) {
$type = 'protected';
}
if ($r instanceof \ReflectionProperty) {
return $type;
}
// ReflectionMethod only below
if ($r->isStatic()) {
$type = "static $type";
}
if ($r->isAbstract()) {
$type = "abstract $type";
}
if ($r->isFinal()) {
$type = "final $type";
}
return $type;
}
/**
* catch all php errors to log file rather than screen
* usually only enabled on production
* @param
* @param
* @param
* @param
* @return bool
*/
function handle_error_log($err_no, $err_str, $err_file, $err_line)
{
dlog( $err_str, // php error
"PHP ERROR ($err_no) $err_file $err_line", // file name, line
'php_errors' );
return true;
}
/**
* catch all php errors to screen rather than log file
* usually only enabled on development
* @param
* @param
* @param
* @param
* @return bool
*/
function handle_error_screen($err_no, $err_str, $err_file, $err_line)
{
dbug( $err_str, // php error
"PHP ERROR ($err_no) ", 2, 1 );
return true;
}
/**
* render a page into wherever
* @param string
* @param object|array
*/
function render($_template, $vars = [])
{
if (file_exists(__DIR__.'/views/'.$_template.'.php')) {
$_template_file = __DIR__.'/views/'.$_template.'.php';
} else {
return "<div>template missing: $_template</div>";
}
extract( (array) $vars, EXTR_SKIP );
ob_start();
require $_template_file;
$html = ob_get_contents();
ob_end_clean();
return $html;
}
/**
*
* @return string
*/
function version()
{
$data = get_plugin_data( __DIR__.'/_plugin.php' );
return $data['Version'];
}