1 <?php
2
3 4 5 6
7
8 9 10 11 12 13 14 15
16
17 function var_template_include($t)
18 {
19 $GLOBALS['current_theme_template'] = basename($t, '.php');
20 return $t;
21 }
22
23 add_filter('template_include', 'var_template_include', 1000);
24
25 if (!function_exists('exceptions_error_handler')) {
26 27 28 29 30 31 32 33
34 function exceptions_error_handler($severity, $message, $filename, $lineno)
35 {
36 if (error_reporting() == 0) {
37 return;
38 }
39 if (error_reporting() & $severity) {
40 throw new ErrorException($message, 0, $severity, $filename, $lineno);
41 }
42 }
43 }
44
45 set_error_handler('exceptions_error_handler');
46
47 if (!function_exists('strip_non_numeric')) {
48 49 50 51 52
53 function strip_non_numeric($value)
54 {
55 return preg_replace("/[^0-9]/", "", $value);
56 }
57 }
58
59 if (!function_exists('plural_count')) {
60 61 62 63 64 65
66 function plural_count($word, $count)
67 {
68 $added_s = $count == 1 ? '': 's';
69 return $word.$added_s;
70 }
71 }
72
73 if (!function_exists('asset')) {
74 75 76 77 78
79 function asset($value)
80 {
81 return sprintf("%s", get_template_directory_uri() . '/' . $value);
82 }
83 }
84
85 if (!function_exists('copyright_date')) {
86 87 88 89 90
91 function copyright_date($year = '2016')
92 {
93 return (date('Y') == $year) ? null: '-' . date('Y');
94 }
95 }
96
97 if (!function_exists('str_slug')) {
98 99 100 101 102 103 104
105 function str_slug($string, $replace = array(), $delimiter = '-')
106 {
107
108 if (!extension_loaded('iconv')) {
109 throw new Exception('iconv module not loaded');
110 }
111
112
113 $oldLocale = setlocale(LC_ALL, '0');
114 setlocale(LC_ALL, 'en_US.UTF-8');
115 $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
116
117 if (!empty($replace)) {
118 $clean = str_replace((array) $replace, ' ', $clean);
119 }
120
121 $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
122 $clean = strtolower($clean);
123 $clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
124 $clean = trim($clean, $delimiter);
125
126
127 setlocale(LC_ALL, $oldLocale);
128 return $clean;
129 }
130 }
131
132 if (!function_exists('auth')) {
133 include 'inc/Auth.php';
134 135 136 137
138 function auth()
139 {
140 return new Auth();
141 }
142 }
143
144 if (!function_exists('request')) {
145 include 'inc/Request.php';
146 147 148 149
150 function request()
151 {
152 return new Request();
153 }
154 }
155
156 if (!function_exists('str_limit')) {
157 158 159 160 161 162 163
164 function str_limit($value, $limit = 100, $end = '...')
165 {
166 if (mb_strwidth($value, 'UTF-8') <= $limit) {
167 return $value;
168 }
169 return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')).$end;
170 }
171 }
172
173 if (!function_exists('word_limit')) {
174 175 176 177 178 179 180
181 function word_limit($text, $limit = 20, $end = '...')
182 {
183 if (str_word_count($text, 0) > $limit) {
184 $words = str_word_count($text, 2);
185 $pos = array_keys($words);
186 $text = trim(substr($text, 0, $pos[$limit])) . $end;
187 }
188 return $text;
189 }
190 }
191
192 if (!function_exists('dd')) {
193 194 195 196 197
198 function dd($data)
199 {
200 ini_set("highlight.comment", "#969896; font-style: italic");
201 ini_set("highlight.default", "#FFFFFF");
202 ini_set("highlight.html", "#D16568");
203 ini_set("highlight.keyword", "#7FA3BC; font-weight: bold");
204 ini_set("highlight.string", "#F2C47E");
205 $output = highlight_string("<?php\n\n" . var_export($data, true), true);
206 echo "<div style=\"text-align:left; background-color: #1C1E21; padding: 1rem\">{$output}</div>";
207 die();
208 }
209 }
210
211 if (!function_exists('is_even')) {
212 213 214 215 216
217 function is_even($value)
218 {
219 if (is_array($value)) {
220 return count($value) % 2 !== 0;
221 }
222
223 return $value % 2 !== 0;
224 }
225 }
226
227 if (!function_exists('is_odd')) {
228 229 230 231 232
233 function is_odd($value)
234 {
235 if (is_array($value)) {
236 return count($value) % 2 == 0;
237 }
238
239 return $value % 2 == 0;
240 }
241 }
242
243 if (!function_exists('url')) {
244 245 246 247 248 249
250 function url($uri, $protocol = null)
251 {
252
253 if (is_object($uri) && get_class($uri) == 'WP_Post') {
254 return get_permalink($uri);
255 }
256
257
258 return site_url($uri, $protocol);
259 }
260 }
261
262 if (!function_exists('e')) {
263 264 265 266 267
268 function e($value)
269 {
270 echo htmlentities($value, ENT_QUOTES, 'utf-8');
271 }
272 }
273
274 if (!function_exists('template_is')) {
275 276 277 278 279
280 function template_is($names)
281 {
282 $names = is_array($names) ? $names: [$names];
283 return count(array_filter($names, function ($uri) {
284 return $GLOBALS['current_theme_template'] == $uri;
285 }));
286 }
287 }
288
289 if (!function_exists('content')) {
290 291 292 293 294
295 function content($post = null)
296 {
297 $post = is_null($post) ? get_post(): $post;
298
299 return apply_filters('the_content', $post->post_content);
300 }
301 }
302
303 if (!function_exists('featured_image')) {
304 305 306 307 308
309 function featured_image($post = null)
310 {
311 $post = is_null($post) ? get_post(): $post;
312
313 $image = (object)wp_get_attachment_metadata(get_post_thumbnail_id($post));
314
315 $image->url = sprintf("/wp-content/uploads/%s", $image->file);
316 return $image;
317 }
318 }
319
320 if (!function_exists('view')) {
321 322 323 324 325 326
327 function view($filename, $vars = null)
328 {
329 try {
330 if (is_array($vars) && !empty($vars)) {
331 extract($vars);
332 }
333
334 ob_start();
335
336 include(get_template_directory() . '/' . $filename);
337
338 return ob_get_clean();
339 } catch (Exception $e) {
340 dd(sprintf("%s in %s:%s when using the `view` function.", $e->getMessage(), $filename, $e->getLine()));
341 }
342 }
343 }
344