helpers.php
3.45 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
<?php
namespace Roots;
/**
* Gets the value of an environment variable.
*
* @param string $key
* @param mixed $default
* @return mixed
*
* @copyright Taylor Otwell
* @license https://github.com/laravel/framework/blob/v5.6.25/LICENSE.md MIT
* @link https://github.com/laravel/framework/blob/v5.6.25/src/Illuminate/Support/helpers.php#L597-L632 Original
*/
function env($key, $default = null)
{
$value = getenv($key);
if ($value === false) {
return value($default);
}
switch (strtolower($value)) {
case 'true':
case '(true)':
return true;
case 'false':
case '(false)':
return false;
case 'empty':
case '(empty)':
return '';
case 'null':
case '(null)':
return;
}
if (($valueLength = strlen($value)) > 1 && $value[0] === '"' && $value[($valueLength - 1)] === '"') {
return substr($value, 1, -1);
}
return $value;
}
/**
* Return the default value of the given value.
*
* @param mixed $value
* @return mixed
*
* @copyright Taylor Otwell
* @license https://github.com/laravel/framework/blob/v5.6.25/LICENSE.md MIT
* @link https://github.com/laravel/framework/blob/v5.6.25/src/Illuminate/Support/helpers.php#L1143-L1152 Original
*/
function value($value)
{
return $value instanceof Closure ? $value() : $value;
}
/**
* Bind single callback to multiple filters
*
* @param iterable $filters List of filters
* @param callable $callback
* @param integer $priority
* @param integer $args
* @return void
*/
function add_filters(iterable $filters, $callback, $priority = 10, $args = 2)
{
$count = count($filters);
array_map(
'\add_filter',
(array) $filters,
array_fill(0, $count, $callback),
array_fill(0, $count, $priority),
array_fill(0, $count, $args)
);
}
/**
* Remove single callback from multiple filters
*
* @param iterable $filters List of filters
* @param callable $callback
* @param integer $priority
* @return void
*/
function remove_filters(iterable $filters, $callback, $priority = 10)
{
$count = count($filters);
array_map(
'\remove_filter',
(array) $filters,
array_fill(0, $count, $callback),
array_fill(0, $count, $priority)
);
}
/**
* Alias of add_filters
*
* @see add_filters
* @param iterable $actions List of actions
* @param callable $callback
* @param integer $priority
* @param integer $args
* @return void
*/
function add_actions(iterable $actions, $callback, $priority = 10, $args = 2)
{
add_filters($actions, $callback, $priority, $args);
}
/**
* Alias of remove_filters
*
* @see remove_filters
* @param iterable $actions List of actions
* @param callable $callback
* @param integer $priority
* @return void
*/
function remove_actions(iterable $actions, $callback, $priority = 10)
{
remove_filters($actions, $callback, $priority);
}
/**
* Helper function for prettying up errors
*
* @param string $message
* @param string $subtitle
* @param string $title
* @param string $footer
*/
function wp_die($message, $subtitle = '', $title = '', $footer = '')
{
$title = $title ?: __('WordPress › Error', 'roots');
$footer = $footer ?: '<a href="https://discourse.roots.io/">Roots Discourse</a>';
$message = "<h1>{$title}<br><small>{$subtitle}</small></h1><p>{$message}</p><p>{$footer}</p>";
\wp_die($message, $title);
}