-
Notifications
You must be signed in to change notification settings - Fork 1
/
invoke-helpers.php
352 lines (313 loc) · 8.8 KB
/
invoke-helpers.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
/**
* @package Invoke_Helpers
* @version 1.0
*/
/*
Plugin Name: Invoke Helpers
Plugin URI: http://wordpress.org/plugins/invoke-helpers/
Description: Theme and function helpers that make using WordPress more pleasant
Author: Invoke Media
Version: 1.0
Author URI: http://www.invokemedia.com
*/
function var_template_include($t)
{
$GLOBALS['current_theme_template'] = basename($t, '.php');
return $t;
}
add_filter('template_include', 'var_template_include', 1000);
if (!function_exists('exceptions_error_handler')) {
/**
* Convert errors into Exceptions
* @param int $severity
* @param string $message
* @param string $filename
* @param int $lineno
* @return Exception
*/
function exceptions_error_handler($severity, $message, $filename, $lineno)
{
if (error_reporting() == 0) {
return;
}
if (error_reporting() & $severity) {
throw new ErrorException($message, 0, $severity, $filename, $lineno);
}
}
}
set_error_handler('exceptions_error_handler');
if (!function_exists('strip_non_numeric')) {
/**
* Strip characters that are not numbers
* @param string $value
* @return string
*/
function strip_non_numeric($value)
{
return preg_replace("/[^0-9]/", "", $value);
}
}
if (!function_exists('plural_count')) {
/**
* Returns a 's' given a number. Works well for plural and singular words
* @param string $word
* @param int $count
* @return string
*/
function plural_count($word, $count)
{
$added_s = $count == 1 ? '': 's';
return $word.$added_s;
}
}
if (!function_exists('asset')) {
/**
* Returns an url for an asset in the current theme
* @param string $value
* @return string
*/
function asset($value)
{
return sprintf("%s", get_template_directory_uri() . '/' . $value);
}
}
if (!function_exists('copyright_date')) {
/**
* Returns a string for copyright for the difference in year started and current year
* @param string $year
* @return string
*/
function copyright_date($year = '2016')
{
return (date('Y') == $year) ? $year : $year . ' - ' . date('Y');
}
}
if (!function_exists('str_slug')) {
/**
* Slugify a string with certain rules
* @param string $string
* @param array $replace
* @param string $delimiter
* @return string
*/
function str_slug($string, $replace = array(), $delimiter = '-')
{
// https://github.com/phalcon/incubator/blob/master/Library/Phalcon/Utils/Slug.php
if (!extension_loaded('iconv')) {
throw new Exception('iconv module not loaded');
}
// Save the old locale and set the new locale to UTF-8
$oldLocale = setlocale(LC_ALL, '0');
setlocale(LC_ALL, 'en_US.UTF-8');
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
if (!empty($replace)) {
$clean = str_replace((array) $replace, ' ', $clean);
}
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
$clean = strtolower($clean);
$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
$clean = trim($clean, $delimiter);
// Revert back to the old locale
setlocale(LC_ALL, $oldLocale);
return $clean;
}
}
if (!function_exists('auth')) {
include 'inc/Auth.php';
/**
* A helper for the autenticated user
* @return Auth
*/
function auth()
{
return new Auth();
}
}
if (!function_exists('request')) {
include 'inc/Request.php';
/**
* A helper for POST and GET requests
* @return Request
*/
function request()
{
return new Request();
}
}
if (!function_exists('str_limit')) {
/**
* Limit a string to a certain amount of characters
* @param string $value
* @param int $limit
* @param string $end
* @return string
*/
function str_limit($value, $limit = 100, $end = '...')
{
if (mb_strwidth($value, 'UTF-8') <= $limit) {
return $value;
}
return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')).$end;
}
}
if (!function_exists('word_limit')) {
/**
* Limit a sentence to a specific number of words
* @param string $text
* @param int $limit
* @param string $end
* @return string
*/
function word_limit($text, $limit = 20, $end = '...')
{
if (str_word_count($text, 0) > $limit) {
$words = str_word_count($text, 2);
$pos = array_keys($words);
$text = trim(substr($text, 0, $pos[$limit])) . $end;
}
return $text;
}
}
if (!function_exists('dd')) {
/**
* Dump and die with nice formatting and styling
* @param mixed $data
* @return string
*/
function dd($data)
{
ini_set("highlight.comment", "#969896; font-style: italic");
ini_set("highlight.default", "#FFFFFF");
ini_set("highlight.html", "#D16568");
ini_set("highlight.keyword", "#7FA3BC; font-weight: bold");
ini_set("highlight.string", "#F2C47E");
$output = highlight_string("<?php\n\n" . var_export($data, true), true);
echo "<div style=\"text-align:left; background-color: #1C1E21; padding: 1rem; word-break: break-word\">{$output}</div>";
die();
}
}
if (!function_exists('is_even')) {
/**
* Tell if an array length or integer is even
* @param mixed $value
* @return int
*/
function is_even($value)
{
if (is_array($value)) {
return count($value) % 2 !== 0;
}
return $value % 2 !== 0;
}
}
if (!function_exists('is_odd')) {
/**
* Tell if an array length or integer is odd
* @param mixed $value
* @return int
*/
function is_odd($value)
{
return !is_even($value);
}
}
if (!function_exists('url')) {
/**
* A wrapper around get_permalink and site_url
* @param WP_Post|string $uri
* @param string|null $protocol
* @return string
*/
function url($uri, $protocol = null)
{
// calls the correct function is the object is a post
if (is_object($uri) && get_class($uri) == 'WP_Post') {
return get_permalink($uri);
}
// just a general site URL from a URI
return site_url($uri, $protocol);
}
}
if (!function_exists('e')) {
/**
* Escape html entities
* @param string $value
* @return string
*/
function e($value)
{
return htmlentities($value, ENT_QUOTES, 'utf-8');
}
}
if (!function_exists('template_is')) {
/**
* Figure out if a template is the current template of the page
* @param array|string $names
* @return bool
*/
function template_is($names)
{
$names = is_array($names) ? $names: [$names];
return count(array_filter($names, function ($uri) {
return $GLOBALS['current_theme_template'] == $uri;
}));
}
}
if (!function_exists('content')) {
/**
* Get the content for the current page or a given post
* @param WP_POST|null $post
* @return string
*/
function content($post = null)
{
$post = is_null($post) ? get_post(): $post;
return apply_filters('the_content', $post->post_content);
}
}
if (!function_exists('featured_image')) {
/**
* Get the featured image of the current post or a given post
* @param WP_POST|null $post
* @return object
*/
function featured_image($post = null)
{
$post = is_null($post) ? get_post(): $post;
$image = (object)wp_get_attachment_metadata(get_post_thumbnail_id($post));
if (!property_exists($image, 'file')) {
return null;
}
$image->url = sprintf("%s/wp-content/uploads/%s", defined('WP_HOME') ? WP_HOME : get_site_url() , $image->file);
return $image;
}
}
if (!function_exists('view')) {
/**
* Render a partial template with a given array of data
* @param string $filename
* @param array $vars
* @return string
*/
function view($filename, $vars = null, $comment = '<!-- %s -->')
{
try {
if (is_array($vars) && !empty($vars)) {
extract($vars);
}
ob_start();
// adds a comment at the top of included file
if (!empty($comment)) {
printf($comment . PHP_EOL, $filename);
}
include(get_template_directory() . '/' . $filename);
return ob_get_clean();
} catch (Exception $e) {
dd([
'message' => sprintf("%s in %s:%s when using the `view` function.", $e->getMessage(), $filename, $e->getLine()),
'exception' => $e
]);
}
}
}