-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
176 lines (145 loc) · 4.37 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
require( 'vendor/autoload.php' );
/**
* Set up the config as constants.
*/
$config_json = file_get_contents( __DIR__ . '/config.json' );
$config = json_decode( $config_json, true );
foreach( $config as $setting => $value ) {
define( $config['NAMESPACE'] . "\\${setting}", $value );
}
/**
* Provide some PHP 8 functions to older versions.
*/
if( ! function_exists( 'str_contains' ) ) {
/**
* Check is string contains substring
*
* @param string $haystack
* @param string $needle
* @return bool
*/
function str_contains( $haystack, $needle ) {
if( strpos( $haystack, $needle ) !== false ) {
return true;
}
else {
return false;
}
}
}
if( ! function_exists( 'str_starts_with' ) ) {
/**
* Check is string starts with substring.
*
* @param string $haystack
* @param string $needle
* @return bool
*/
function str_starts_with( $haystack, $needle ) {
return strpos( $haystack, $needle ) === 0;
}
}
/**
* Workaround to dreaded isset() and empty() conundrums.
* There are a ton of blog posts, stackoverlow threads, and opinions about this.
* I often use flex editor previews within the admin as users are updating content,
* so there may be array or object properties that are either not set or empty at
* any given time, and it can be painstaking to check if admins have entered in values.
* This function checks if the variable, or nested index/property has a value.
* It can check nested properties 3 levels deep.
*
* @param mixed $var
* @param string|array|object $key1 Array or object property.
* @param string|array|object $key2 Array or object property.
* @param string $key3 Array or object property.
* @return bool
*/
function _check( $var, $key1 = null, $key2 = null, $key3 = null ) {
$result = isset( $var ) && $var;
if( $key1 && $result ) {
if( gettype( $var ) == 'object' ) {
$var = (array)$var;
}
$result = isset( $var[$key1] ) && $var[$key1];
if ($key2 && $result ) {
if( gettype( $key1 ) == 'object' ) {
$key1 = (array)$key1;
}
$result = isset( $var[$key1][$key2] ) && $var[$key1][$key2];
if( $key3 && $result ) {
if( gettype( $key2 ) == 'object' ) {
$key2 = (array)$key2;
}
$result = isset( $var[$key1][$key2][$key3] ) && $var[$key1][$key2][$key3];
}
}
}
return $result;
}
/**
* Shortcut to return a value if variable isset and not empty.
*
* @param mixed $var
* @param string|array|object $key1 Array or object property.
* @param string|array|object $key2 Array or object property.
* @param string $key3 Array or object property.
* @return mixed
*/
function _checkshow( $var, $key1 = null, $key2 = null, $key3 = null ) {
$result = _check( $var, $key1, $key2, $key3 );
if( ! $result ) {
return null;
}
$var_object = is_object( $var );
$output = null;
if( $var_object ) {
$var = (array) $var;
}
if( $result ) {
if( ! $key1 ) {
$output = $var;
}
elseif( ! $key2 ) {
$output = $var[$key1];
}
elseif( ! $key3 ) {
$output = $var[$key1][$key2];
}
elseif( ! $key3 ) {
$output = $var[$key1][$key2][$key3];
}
}
if( $var_object ) {
$var = (object) $var;
}
return $output;
}
/**
* Include all necessary code into the app
*/
$lib = [
'lib/dev.php', // For debugging and whatnot.
'lib/settings.php', // Global settings.
'lib/acf.php', // Custom ACF functions.
'lib/admin.php', // Admin functionality.
'lib/assets.php', // Scripts and stylesheets
'lib/block-editor.php', // Block editor stuff.
'lib/extras.php', // Custom functions.
'lib/setup.php', // Theme setup.
'lib/theme-wrapper.php', // Setup custom theme wrapper.
'lib/*', // Include all index files for subdirectories.
];
foreach( $lib as $filepath ) {
// Regular files
if( ! strpos( $filepath, '*' ) ) {
include_once( $filepath );
}
// Index files in subdirectories.
else {
foreach( glob( __DIR__ . "/{$filepath}/_*.php" ) as $index_file ) {
include_once( $index_file );
}
}
}
unset( $filepath );