-
Notifications
You must be signed in to change notification settings - Fork 3
/
functions.php
449 lines (395 loc) · 9.59 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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<?php
/**
* Holds globally available functions.
*
* @since 0.1
*/
/**
* Transforms various types to the appropriate Rila object.
*
* @since 0.1
* @see https://github.com/RadoslavGeorgiev/rila-framework/wiki/The-rila-function
*
* @param mixed $item The item that should be wrapped by the framework.
* @return mixed
*/
function rila( $item = null ) {
static $pairs;
# An empty parameter means a posts collection
if( is_null( $item ) ) {
return new Rila\Collection\Posts;
}
# For integers, return a simple post
if( is_null( $item ) || is_int( $item ) ) {
if( is_null( $item ) ) {
$item = get_the_id();
}
return Rila\Post_Type::factory( $item );
}
# Check standard objects
if( is_object( $item ) ) {
if( is_null( $pairs ) ) {
$pairs = array(
'WP_Post' => 'Rila\\Post_Type',
'WP_Term' => 'Rila\\Taxonomy',
'WP_User' => 'Rila\\User',
'WP_Comment' => 'Rila\\Comment',
'WP_Site' => 'Rila\\Site',
'WP_Query' => 'Rila\\Query'
);
}
foreach( $pairs as $cn => $wrapper ) {
if( is_a( $item, $wrapper ) ) {
return $item;
}
if( is_a( $item, $cn ) ) {
return call_user_func( array( $wrapper, 'factory' ), $item );
}
}
}
# Check for URLs and embeds
if( is_string( $item ) && filter_var( $item, FILTER_VALIDATE_URL ) ) {
return new Rila\Embed( $item );
}
# Check string-type pairs
if( is_string( $item ) ) foreach( Rila\Meta::shortcuts() as $keyword => $target ) {
if( 0 !== strpos( $item, $keyword . '_' ) )
continue;
$value = preg_replace( '~^' . preg_quote( $keyword ) . '_~i', '', $item );
/**
* @Todo: Extract the one-value functionality of "map" and use without a hack.
*/
return Rila\Meta::map( $value, 'temp', array(
'temp' => $keyword
));
}
# Try content blocks
if( is_array( $item ) && isset( $item[ '__type' ] ) ) {
$cn = $item[ '__type' ];
$cn = str_replace( '_ns_', '\\', $cn );
if( class_exists( $cn ) && is_subclass_of( $cn, 'Rila\\Block' ) ) {
return new $cn( $item );
}
}
# Return the item itself
return $item;
}
/**
* Returns an instance of the main class.
*
* @since 0.1
*
* @return Rila\Plugin
*/
function rila_framework() {
return Rila\Plugin::instance();
}
/**
* Creates a new post object.
*
* @since 0.1
*
* @param mixed $id Either the ID of the post, a post object or a WP_Post object.
* @return Post_Type
*/
function rila_post( $id = null ) {
if( ! $id ) {
$id = get_the_id();
}
return Rila\Post_Type::factory( $id );
}
/**
* Creates a new term object.
*
* @since 0.1
*
* @param mixed $term Either a term ID, a WP_Term or a Term object.
* @return Term
*/
function rila_term( $term ) {
return Rila\Taxonomy::factory( $term );
}
/**
* Returns the instance of the current site.
*
* @since 0.1
*
* @return Site
*/
function rila_site() {
return Rila\Site::instance();
}
/**
* Creates a new view.
*
* When using the view, you need to either return it or echo it, based on the context.
*
* @since 0.1
*
* @param string $name Either the name of the view (without .twig) or an array of names.
* @param mixed[] $context The context for the view. The ->with( 'name', $value ) can be used too.
* @return Template A template that can be manipulated or rendered.
*/
function rila_view( $name, $context = array() ) {
return new Rila\Template( $name, $context );
}
/**
* Creates a new comment.
*
* @since 0.1
*
* @param mixed $comment Either a WP_Comment or a comment ID.
* @return Comment
*/
function rila_comment( $comment ) {
return Rila\Comment::factory( $comment );
}
/**
* Creates a new user object.
*
* @since 0.1
*
* @param mixed $user Either a WP_User or a user ID.
* @return User
*/
function rila_user( $user = null ) {
return Rila\User::factory( $user );
}
/**
* Creates a new image object.
*
* @since 0.1
*
* @param int $id The ID of the image.
* @return File
*/
function rila_file( $file ) {
return Rila\File::factory( $file );
}
/**
* Creates a new image object.
*
* @since 0.1
*
* @param int $id The ID of the image.
* @return Image
*/
function rila_image( $image ) {
return Rila\Image::factory( $image );
}
/**
* Creates a new date object.
*
* @since 0.1
*
* @param mixed $date THe date/time to use.
* @return Date
*/
function rila_date( $date ) {
return new Rila\Date( $date );
}
/**
* Creates a new query.
*
* @since 0.1
*
* @param mixed $request WP_Query arguments. Can also be an array of IDs for specific posts.
* @return Query
*/
function rila_query( $request = array() ) {
return new Rila\Query( $request );
}
/**
* Registers a post type or a taxonomy class by adding the neccessary hooks.
*
* @since 0.1
*
* @param string $classname The class to be registered.
*/
function rila_register_class( $classname ) {
new Rila\Class_Handler( $classname );
}
/**
* Creates a new collection of posts.
*
* @since 0.11
*
* @param mixed $args Either an array or string of arguments for the collection. (optional)
* @return Rila\Collection\Posts
*/
function rila_posts( $args = '' ) {
return new Rila\Collection\Posts( $args );
}
/**
* Creates a new collection of terms.
*
* @since 0.11
*
* @param mixed $args Either an array or string of arguments for the collection. (optional)
* @return Rila\Collection\Terms
*/
function rila_terms( $args = '' ) {
return new Rila\Collection\Terms( $args );
}
/**
* Creates a new collection of users.
*
* @since 0.11
*
* @param mixed $args Either an array or string of arguments for the collection. (optional)
* @return Rila\Collection\Users
*/
function rila_users( $args = '' ) {
return new Rila\Collection\Users( $args );
}
/**
* Creates a new collection of comments.
*
* @since 0.11
*
* @param mixed $args Either an array or string of arguments for the collection. (optional)
* @return Rila\Collection\Comments
*/
function rila_comments( $args = '' ) {
return new Rila\Collection\Comments( $args );
}
/**
* Creates a new collection of files.
*
* @since 0.11
*
* @param mixed $args Either an array or string of arguments for the collection. (optional)
* @return Rila\Collection\Files
*/
function rila_files( $args = '' ) {
return new Rila\Collection\Files( $args );
}
/**
* Creates a new collection of images.
*
* @since 0.11
*
* @param mixed $args Either an array or string of arguments for the collection. (optional)
* @return Rila\Collection\Images
*/
function rila_images( $args = '' ) {
return new Rila\Collection\Images( $args );
}
/**
* Converts a linear array with dot notations to a nested one.
*
* @since 0.1
*
* @param mixed[] $data The flat array with keys like content_blocks.text.title.
* @return mixed[] The nested array
*/
function rila_dot_to_array( $data ) {
$processed = array();
$go_deep = array();
foreach( $data as $key => $value ) {
if( false !== ( $pos = strpos( $key, '.' ) ) ) {
$gr = substr( $key, 0, $pos );
$rest = substr( $key, $pos+1 );
if( isset( $processed[ $gr ] ) ) {
$processed[ $gr ][ $rest ] = $value;
} else {
$processed[ $gr ] = array( $rest => $value );
}
$go_deep[ $gr ] = 1;
} else {
$processed[ $key ] = $value;
}
}
foreach( array_keys( $go_deep ) as $key ) {
$processed[ $key ] = rila_dot_to_array( $processed[ $key ] );
}
return $processed;
}
/**
* Processes arguments for functions that are in URL-like format.
*
* @since 0.1
*
* @param mixed $query The needed query, ex. main-menu?menu_class=the-menu
* @return moxed[]
*/
function rila_parse_args( $query ) {
$query = explode( '?', $query );
$main = $query[ 0 ];
if( count( $query ) > 1 ) {
$extra = wp_parse_args( $query[ 1 ] );
} else {
$extra = array();
}
return array(
'main' => $main,
'args' => $extra
);
}
/**
* Converts the dashes in array keys to underscores.
*
* @since 0.1
*
* @param mixed[] $arr The value to convert.
* @return mixed[]
*/
function rila_dashes_to_underscrores( $arr ) {
$result = array();
foreach( $arr as $key => $value ) {
if( is_array( $value ) ) {
$value = rila_dashes_to_underscrores( $value );
}
$result[ $key ] = $value;
if( false !== stripos( $key, '-' ) ) {
$result[ str_replace( '-', '_', $key ) ] = $value;
}
}
return $result;
}
/**
* Cleans a class name up from namespaces, prefixes and suffixes.
*
* @since 0.1
*
* @param string $class_name The name that needs to be cleaned up.
* @param string $type A type that would be used as a prefix or a suffix.
* @return string The cleaned-up class
*/
function rila_cleanup_class( $class_name, $type = '' ) {
$class_name = preg_replace( '~.*\\\\~', '', $class_name );
# Remove "Block_" from "Block_Text" and "_Widget" from "Text_Widget"
if( '' != $type ) {
$class_name = preg_replace( '~^' . preg_quote( $type ) . '_~', '', $class_name );
$class_name = preg_replace( '~_' . preg_quote( $type ) . '$~', '', $class_name );
}
return $class_name;
}
/**
* Attempts converting a Twig template to a string.
* If errors occur, they will be returned as a string.
*
* @since 0.1
*
* @param object $object The object that should be converted to a string.
* @param string $method The name of the method that can avoid __toString().
* @return string
*/
function rila_convert_to_string( $object, $method ) {
if( ! is_object( $object ) || ! method_exists( $object, $method ) ) {
return 'rila_convert_to_string() can only work with a class that has a ' . $method . '() method.';
}
try {
$result = call_user_func( array( $object, $method ) );
if( is_a( $result, 'Rila\\Template' ) ) {
$result = $result->render();
}
return $result;
} catch( Exception $e ) {
return sprintf(
"<strong>\"%s\" exception thrown, but not caught: </strong> %s",
get_class( $e ),
esc_html( $e->getMessage() )
);
}
}