-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
237 lines (190 loc) · 4.52 KB
/
functions.php
File metadata and controls
237 lines (190 loc) · 4.52 KB
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
<?php
if (!function_exists('sanitize_with_underscores')) :
function sanitize_with_underscores($str)
{
return preg_replace('|\W+|', '_', strtolower($str));
}
endif;
if (!function_exists('is_numeric_array')) :
function is_numeric_array(&$array)
{
return array_values($array) === $array;
}
endif;
if (!function_exists('array_merge_numeric')):
function array_merge_numeric(&$args, &$defaults)
{
foreach ($defaults as $key => $value)
if (!isset($args[$key]))
$args[$key] = $value;
ksort($args, SORT_NUMERIC);
return $args;
}
endif;
if (!function_exists('array_merge_better')) :
function array_merge_better($defaults, $r)
{
if (!empty($r)) {
foreach ($r as $key => $value)
{
if (is_array($value) && is_array($defaults[$key])) {
$defaults[$key] = array_merge_better($defaults[$key], $value);
} else {
$defaults[$key] = is_array($value) ? $value : trim($value);
}
}
}
return $defaults;
}
endif;
if (!function_exists('array_search_better')):
function array_search_better($needle, &$array)
{
foreach($array as $key => $value)
{
if (is_array($value) && ($result = array_search_better($needle, $value)) !== false)
return $result;
if ($needle == $value)
return $key;
}
return false;
}
endif;
if (!function_exists('array_unset_empty')) :
function array_unset_empty($array)
{
if (is_array($array))
{
$is_assoc = !is_numeric_array($array);
foreach ($array as $key => $value)
if ('' == trim($value))
if ($is_assoc)
unset($array[$key]);
else
array_splice($array, $key, 1);
if ($is_assoc)
$array =& array_merge($array);
}
return $array;
}
endif;
if (!function_exists('strip_whitespace')):
function strip_whitespace($html, $echo = false)
{
$stripped = preg_replace( "/(?:(?<=\>)|(?<=\/\>))(\s+)(?=\<\/?)/", "", preg_replace('|<!--[^>]*-->|', '', $html));
$stripped = str_replace(array("\r\n", "\n", "\r"), "", $stripped);
if ($echo)
echo $stripped;
else
return $stripped;
}
endif;
if (!function_exists('level2role')) :
function level2role($level) {
switch ($level) {
case 10:
case 9:
case 8:
return 'administrator';
case 7:
case 6:
case 5:
return 'editor';
case 4:
case 3:
case 2:
return 'author';
case 1:
return 'contributor';
case 0:
return 'subscriber';
}
}
endif;
if (!function_exists('role2minlevel')) :
function role2minlevel($role) {
$map = array(
'administrator' => 8,
'editor' => 5,
'author' => 2,
'contributor' => 1,
'subscriber' => 0
);
return $map[strtolower($role)];
}
endif;
if (!function_exists('role2maxlevel')) :
function role2maxlevel($role) {
$map = array(
'administrator' => 10,
'editor' => 7,
'author' => 4,
'contributor' => 1,
'subscriber' => 0
);
return $map[strtolower($role)];
}
endif;
if (!function_exists('get_current_url')) :
function get_current_url()
{
$url = 'http';
if ($_SERVER["HTTPS"] == "on") {$url .= "s";}
$url .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$url .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$url .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $url;
}
endif;
if (!function_exists('get_post_by_slug')) :
function get_post_by_slug($slug, $pages = false)
{
if (empty($slug))
return false;
if (is_array($pages) && !empty($pages))
{
foreach ($pages as $page)
if ($page->post_name == $slug)
return $page;
}
global $wpdb;
return $wpdb->get_row("SELECT * FROM $wpdb->posts WHERE post_name = '{$wpdb->escape($slug)}'");
}
endif;
if (!function_exists('is_add_page_of')) :
function is_add_page_of($post_type)
{
global $pagenow;
if (!is_array($post_type))
$post_type = array($post_type);
if ($pagenow == 'post-new.php' && in_array($_GET['post_type'], $post_type))
return true;
if ($pagenow == 'post.php' && $_GET['action'] == 'edit' && is_numeric($_GET['post']))
{
$post = get_post($_GET['post']);
if ($post && in_array($post->post_type, $post_type))
return true;
}
return false;
}
endif;
if (!function_exists('add_caps2')) :
function add_caps2($roles, $caps)
{
$r = new WP_Roles;
if (!is_array($roles))
$roles = array($roles);
# add_cap() writes to database on every call if use_db is true, don't need that
$r->use_db = false;
foreach ($roles as $role)
if ($r->is_role($role))
foreach ($caps as $cap)
$r->add_cap($role, $cap);
$r->use_db = true;
update_option( $r->role_key, $r->roles );
}
endif;
?>