-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
227 lines (195 loc) · 4.68 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
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
<?php
/**
* Contains all the Global common functions used throughout the plugin
*/
use FooPlugins\Generator\Admin\BoilerplateLoader;
/**
* Custom Autoloader used throughout plugin
*
* @param $class
*/
function foogen_autoloader( $class ) {
/* Only autoload classes from this namespace */
if ( false === strpos( $class, FOOGEN_NAMESPACE ) ) {
return;
}
/* Remove namespace from class name */
$class_file = str_replace( FOOGEN_NAMESPACE . '\\', '', $class );
/* Convert sub-namespaces into directories */
$class_path = explode( '\\', $class_file );
$class_file = array_pop( $class_path );
$class_path = strtolower( implode( '/', $class_path ) );
/* Convert class name format to file name format */
$class_file = foogen_uncamelize( $class_file );
$class_file = str_replace( '_', '-', $class_file );
$class_file = str_replace( '--', '-', $class_file );
/* Load the class */
require_once FOOGEN_DIR . '/includes/' . $class_path . '/class-' . $class_file . '.php';
}
/**
* Convert a CamelCase string to camel_case
*
* @param $str
*
* @return string
*/
function foogen_uncamelize( $str ) {
$str = lcfirst( $str );
$lc = strtolower( $str );
$result = '';
$length = strlen( $str );
for ( $i = 0; $i < $length; $i ++ ) {
$result .= ( $str[ $i ] == $lc[ $i ] ? '' : '_' ) . $lc[ $i ];
}
return $result;
}
/**
* Safe way to get value from an array
*
* @param $key
* @param $array
* @param $default
*
* @return mixed
*/
function foogen_safe_get_from_array( $key, $array, $default ) {
if ( is_array( $array ) && array_key_exists( $key, $array ) ) {
return $array[ $key ];
} else if ( is_object( $array ) && property_exists( $array, $key ) ) {
return $array->{$key};
}
return $default;
}
/**
* Safe way to get value from the request object
*
* @param $key
*
* @return mixed
*/
function foogen_safe_get_from_request( $key ) {
return foogen_safe_get_from_array( $key, $_REQUEST, null );
}
/**
* Gets all boilerplates
*
* @return mixed|void
*/
function foogen_get_all_boilerplates() {
global $foogen_boilerplates;
if ( isset( $foogen_boilerplates ) ) {
return $foogen_boilerplates;
}
//get the default boilerplates built into the plugin
$loader = new BoilerplateLoader();
$foogen_boilerplates = $loader->load_plugin_boilderplates();
//TODO : get the boilerplates saved in the current theme location
$foogen_boilerplates = apply_filters( 'FooPlugins\Generator\Admin\GetAllBoilerplates', $foogen_boilerplates );
return $foogen_boilerplates;
}
/**
* Converts a string to something that can be used safely for a function name
*
* @param $value
*
* @return string
*/
function __foogen_function( $value ) {
return strtolower( trim( preg_replace( '/[^A-Za-z0-9-]+/', '_', $value ) ) );
}
/**
* Converts a string to something that can be used safely for a file name
*
* @param $value
*
* @return string
*/
function __foogen_filename( $value ) {
return strtolower( trim( preg_replace( '/[^A-Za-z0-9-]+/', '-', $value ) ) );
}
/**
* Converts a string to something that can be used safely for a PHP constant name
*
* @param $value
*
* @return string
*/
function __foogen_constant( $value ) {
return strtoupper( trim( preg_replace( '/[^A-Za-z0-9-]+/', '_', $value ) ) );
}
/**
* Converts a string to something that can be used safely for a PHP class name
*
* @param $value
*
* @return string
*/
function __foogen_class( $value ) {
return str_replace( ' ', '_', ucwords( str_replace( array('-', '_'), ' ', $value ) ) );
}
/**
* Slugify a string
*
* @param $value
*
* @return string
*/
function __foogen_slugify( $value ) {
return sanitize_title_with_dashes( $value );
}
/**
* Uppercase a string
*
* @param $value
*
* @return string
*/
function __foogen_uppercase( $value ) {
return strtoupper( $value );
}
/**
* Lowercase a string
*
* @param $value
*
* @return string
*/
function __foogen_lowercase( $value ) {
return strtolower( $value );
}
/**
* Converts a string into a function name that can be used for the Freemius global function
*
* @param $value
*
* @return string
*/
function __foogen_freemius_function( $value ) {
return __foogen_function( $value ) . '_fs';
}
/**
* Adds a global message used on the generator form
*
* @param $message
* @param bool $is_error
*/
function foogen_add_global_message( $message, $is_error = false ) {
global $foogen_messages;
if ( !isset( $foogen_messages ) ) {
$foogen_messages = array();
}
$foogen_messages[] = array(
'message' => $message,
'error' => $is_error
);
}
/**
* Gets all global message used on the generator form
*/
function foogen_get_global_messages() {
global $foogen_messages;
if ( !isset( $foogen_messages ) ) {
$foogen_messages = array();
}
return $foogen_messages;
}