-
Notifications
You must be signed in to change notification settings - Fork 62
/
helpers.php
774 lines (709 loc) · 15.3 KB
/
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
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
<?php
/**
* Dump Die
*
* @param mixed ...$args
*/
function tr_dd(...$args) {
\TypeRocket\Utility\Dump::die(...$args);
}
/**
* Dump
*
* @param mixed ...$args
*/
function tr_dump(...$args) {
\TypeRocket\Utility\Dump::data(...$args);
}
/**
* Dots Walk
*
* Traverse array with dot notation.
*
* @param string $dots dot notation key.next.final
* @param array|object $array an array to traverse
* @param null|mixed $default
*
* @return array|mixed|null
*/
function tr_dots_walk($dots, $array, $default = null)
{
return \TypeRocket\Utility\Data::walk(...func_get_args());
}
/**
* Dots Set
*
* Set an array value using dot notation.
*
* @param string $dots dot notation path to set
* @param array $array the original array
* @param mixed $value the value to set
*
* @return array
*/
function tr_dots_set($dots, array $array, $value)
{
return \TypeRocket\Utility\Arr::set(...func_get_args());
}
/**
* HTML class names helper
*
* @param string $defaults
* @param null|array $classes
* @param string $failed
* @return string
*/
function tr_class_names($defaults, $classes = null, $failed = '') {
return \TypeRocket\Utility\Str::classNames(...func_get_args());
}
/**
* Get Namespaced Class
*
* @param string $append
* @return string
*/
function tr_app_class($append) {
return \TypeRocket\Utility\Helper::appNamespace($append);
}
/**
* Get WordPress Root
*
* @return string
*/
function tr_wp_root() {
return \TypeRocket\Utility\Helper::wordPressRootPath();
}
/**
* Resolve Class From DI Container
*
* Get from DI container
*
* @param string $class_name class or alias
* @return mixed|null
*/
function tr_container($class_name) {
return \TypeRocket\Core\Container::resolve($class_name);
}
/**
* Resolve Class
*
* Inject all class dependencies
*
* @param string $class_name class or alias
*
* @return mixed|null
* @throws Exception
*/
function tr_resolve($class_name) {
return \TypeRocket\Core\Resolver::build($class_name);
}
/**
* Updates _site_state_changed option in database
*
* Should be called when a theme or plugin has been activated or deactivated.
* Used to facilitate tasks like flushing rewrite rules for the registration
* and de-registration of post types and taxonomies.
*
* @link https://core.trac.wordpress.org/ticket/47526
*
* @param string|array $arg single function name or list of function names
*/
function tr_update_site_state($arg)
{
\TypeRocket\Core\System::updateSiteState($arg);
}
/**
* Locate Config Setting
*
* Traverse array with dot notation.
*
* @param string $dots dot notation key.next.final
* @param null|mixed $default default value to return if null
*
* @return array|mixed|null
*/
function tr_config($dots, $default = null) {
return \TypeRocket\Core\Config::get(...func_get_args());
}
/**
* Get Main Response
*
* @return \TypeRocket\Http\Response
*/
function tr_response() {
return \TypeRocket\Http\Response::getFromContainer();
}
/**
* TypeRocket Nonce
*
* @param string $action
*
* @return false|string
*/
function tr_nonce($action = '') {
return \TypeRocket\Http\Response::new()->createNonce(...func_get_args());
}
/**
* Get Request
*
* @param null $method
* @return \TypeRocket\Http\Request
*/
function tr_request() {
return new \TypeRocket\Http\Request;
}
/**
* Get Debug
*
* @return bool
*/
function tr_debug() {
return \TypeRocket\Core\Config::get('app.debug');
}
/**
* Get Assets URL
*
* @param string $append
* @return string
*/
function tr_assets_url( $append ) {
return \TypeRocket\Utility\Helper::assetsUrl(...func_get_args());
}
/**
* Get Views Directory
*
* @param string $append
* @return string
*/
function tr_storage_path( $append ) {
return \TypeRocket\Utility\Path::storage($append);
}
/**
* Get Views Directory
*
* @param string $append
* @return string
*/
function tr_views_path( $append ) {
return \TypeRocket\Utility\Path::views($append);
}
/**
* Get controller by recourse
*
* @param string $resource use the resource name to get controller
* @param bool $instance
*
* @return null|string $controller
*/
function tr_controller($resource, $instance = true)
{
return \TypeRocket\Utility\Helper::controllerClass($resource, $instance);
}
/**
* Get model by recourse
*
* @param string $resource use the resource name to get model
* @param bool $instance
*
* @return null|string|\TypeRocket\Models\Model $object
*/
function tr_model($resource, $instance = true)
{
return \TypeRocket\Utility\Helper::modelClass($resource, $instance);
}
/**
* Register taxonomy
*
* @param string $singular
* @param null $plural
* @param array $settings
*
* @return \TypeRocket\Register\Taxonomy
*/
function tr_taxonomy($singular, $plural = null, $settings = [])
{
return \TypeRocket\Register\Taxonomy::add(...func_get_args());
}
/**
* Register post type
*
* @param string $singular Singular name for post type
* @param string|null $plural Plural name for post type
* @param array $settings The settings for the post type
* @param string|null $id post type ID
*
* @return \TypeRocket\Register\PostType
*/
function tr_post_type($singular, $plural = null, $settings = [], $id = null )
{
return \TypeRocket\Register\PostType::add(...func_get_args());
}
/**
* Register meta box
*
* @param string $name
* @param null|string|array $screen
* @param array $settings
*
* @return \TypeRocket\Register\MetaBox
*/
function tr_meta_box($name = null, $screen = null, $settings = [])
{
return \TypeRocket\Register\MetaBox::add(...func_get_args());
}
/**
* @param string $resource
* @param string $action
* @param string $title
* @param array $settings
* @param null|array|string|callable $handler
*
* @return \TypeRocket\Register\Page
*/
function tr_page($resource, $action, $title, array $settings = [], $handler = null)
{
return \TypeRocket\Register\Page::add(...func_get_args());
}
/**
* @param string $singular
* @param string|array|null $plural
* @param array $settings
* @param null $resource
* @param null $handler
*
* @return \TypeRocket\Register\Page
* @throws Exception
*/
function tr_resource_pages($singular, $plural = null, array $settings = [], $resource = null, $handler = null)
{
return \TypeRocket\Register\Page::addResourcePages(...func_get_args());
}
/**
* Create tabs
*
* @return \TypeRocket\Elements\Tabs
*/
function tr_tabs()
{
return \TypeRocket\Elements\Tabs::new();
}
/**
* Instance the From
*
* @param string|\TypeRocket\Interfaces\Formable|array|null $resource posts, users, comments, options your own
* @param string|null $action update, delete, or create
* @param null|int $item_id you can set this to null or an integer
* @param mixed|null|string $model
*
* @return \TypeRocket\Elements\BaseForm|\App\Elements\Form
*/
function tr_form($resource = null, $action = null, $item_id = null, $model = null)
{
return \TypeRocket\Utility\Helper::form(...func_get_args());
}
/**
* Modify Model Value
*
* @param \TypeRocket\Models\Model $model use dot notation
* @param mixed $args
*
* @return array|mixed|null|string
*/
function tr_model_field(\TypeRocket\Models\Model $model, $args)
{
return \TypeRocket\Utility\ModelField::model(...func_get_args());
}
/**
* Get the post's field
*
* @param string $name use dot notation
* @param null|int|WP_Post $item_id
*
* @return array|mixed|null|string
*/
function tr_post_field($name, $item_id = null)
{
return \TypeRocket\Utility\ModelField::post(...func_get_args());
}
/**
* Get the post field
*
* @param string $name use dot notation
* @param null|int|WP_Post $item_id
*
* @return array|mixed|null|string
*/
function tr_field($name, $item_id = null)
{
return \TypeRocket\Utility\ModelField::post(...func_get_args());
}
/**
* Get components
*
* Auto binding only for post types
*
* @param string $name use dot notation
* @param null $item_id
*
* @param null|string $modelClass
*
* @return array|mixed|null|string
* @throws Exception
*/
function tr_components_field($name, $item_id = null, $modelClass = null)
{
return \TypeRocket\Utility\ModelField::components(...func_get_args());
}
/**
* Loop Components
*
* @param array $builder_data
* @param array $other be sure to pass $name, $item_id, $model
* @param string $group
*/
function tr_components_loop($builder_data, $other = [], $group = 'builder')
{
\TypeRocket\Elements\Fields\Matrix::componentsLoop(...func_get_args());
}
/**
* Is TypeRocket Builder Active
*
* @param string|null $field_name
*
* @return bool
*/
function tr_show_page_builder(string $field_name = "use_builder", $item_id = null)
{
$args = func_get_args() ?: [$field_name];
$use_builder = tr_post_field(...$args);
return $use_builder === '1' || $use_builder === 1 && !post_password_required();
}
/**
* Get users field
*
* @param string $name use dot notation
* @param null $item_id
*
* @return array|mixed|null|string
*/
function tr_user_field($name, $item_id = null)
{
return \TypeRocket\Utility\ModelField::user(...func_get_args());
}
/**
* Get options
*
* @param string $name use dot notation
*
* @return array|mixed|null|string
*/
function tr_option_field($name)
{
return \TypeRocket\Utility\ModelField::option(...func_get_args());
}
/**
* Get comments field
*
* @param string $name use dot notation
* @param null|int $item_id
*
* @return array|mixed|null|string
*/
function tr_comment_field($name, $item_id = null)
{
return \TypeRocket\Utility\ModelField::comment(...func_get_args());
}
/**
* Get taxonomy field
*
* @param string|array $name use dot notation
* @param string|null $taxonomy taxonomy model class
* @param int|null $item_id taxonomy id
*
* @return array|mixed|null|string
*/
function tr_term_field($name, $taxonomy = null, $item_id = null)
{
return \TypeRocket\Utility\ModelField::term(...func_get_args());
}
/**
* Get resource
*
* @param string $name use dot notation
* @param string $resource
* @param null|int $item_id
*
* @return array|mixed|null|string
*/
function tr_resource_field($name, $resource, $item_id = null)
{
return \TypeRocket\Utility\ModelField::resource(...func_get_args());
}
/**
* Detect is JSON
*
* @param $args
*
* @return bool
*/
function tr_is_json(...$args)
{
return \TypeRocket\Utility\Data::isJson(...$args);
}
/**
* @return \TypeRocket\Http\Redirect
*/
function tr_redirect()
{
return \TypeRocket\Http\Redirect::new();
}
/**
* @param null|array $default
* @param bool $delete
*
* @return array
*/
function tr_redirect_message($default = null, $delete = true)
{
return \TypeRocket\Http\Cookie::new()->redirectMessage(...func_get_args());
}
/**
* @param null $default
*
* @return array
*/
function tr_redirect_errors($default = null)
{
return \TypeRocket\Http\Cookie::new()->redirectErrors(...func_get_args());
}
/**
* @param null $default
* @param bool $delete
*
* @return array
*/
function tr_redirect_data($default = null, $delete = true)
{
return \TypeRocket\Http\Cookie::new()->redirectData(...func_get_args());
}
/**
* TypeRocket Nonce Field
*
* @param string $action
*
* @return string
*/
function tr_field_nonce($action = '')
{
return \TypeRocket\Elements\BaseForm::nonceInput(...func_get_args());
}
/**
* TypeRocket Nonce Field
*
* @param string $method GET, POST, PUT, PATCH, DELETE
*
* @return string
*/
function tr_field_method($method = 'POST')
{
return \TypeRocket\Elements\BaseForm::methodInput(...func_get_args());
}
/**
* TypeRocket Nonce Field
*
* @param string $method GET, POST, PUT, PATCH, DELETE
* @param string $prefix prefix fields will be grouped under
*
* @return string
*/
function tr_form_hidden_fields($method = 'POST', $prefix = 'tr')
{
return \TypeRocket\Elements\BaseForm::hiddenInputs(...func_get_args());
}
/**
* Check Spam Honeypot
*
* @param null|string $name
*
* @return \TypeRocket\Html\Html
*/
function tr_honeypot_fields($name = null)
{
return \TypeRocket\Elements\BaseForm::honeypotInputs(...func_get_args());
}
/**
* @return \TypeRocket\Http\Cookie
*/
function tr_cookie()
{
return new \TypeRocket\Http\Cookie();
}
/**
* @param string $dots
* @param array $data
* @param string $ext
*
* @return \TypeRocket\Template\View
*/
function tr_view($dots, array $data = [], $ext = '.php')
{
return \TypeRocket\Template\View::new(...func_get_args());
}
/**
* Validate fields
*
* @param array $rules
* @param array|null $fields
* @param null $modelClass
* @param bool $run
*
* @return \TypeRocket\Utility\Validator
*/
function tr_validator($rules, $fields = null, $modelClass = null, $run = false)
{
return \TypeRocket\Utility\Validator::new(...func_get_args());
}
/**
* Route
*
* @return \TypeRocket\Http\Route
*/
function tr_route()
{
return \TypeRocket\Http\Route::new();
}
/**
* Get Routes Repo
*
* @return \TypeRocket\Http\RouteCollection
*/
function tr_routes_repo()
{
return \TypeRocket\Http\RouteCollection::getFromContainer();
}
/**
* Get Routes Repo
* @param string $name
* @return null|\TypeRocket\Http\Route
*/
function tr_route_find($name)
{
return \TypeRocket\Http\RouteCollection::getFromContainer()->getNamedRoute($name);
}
/**
* Get Routes Repo
* @param string $name
* @param array $values
* @param bool $site
*
* @return null|string
*/
function tr_route_url($name, $values = [], $site = true)
{
return \TypeRocket\Http\Route::buildUrl(...func_get_args());
}
/**
* Database Query
*
* @return \TypeRocket\Database\Query
*/
function tr_query()
{
return \TypeRocket\Database\Query::new();
}
/**
* File Utility
*
* @param string $file
* @return object
* @throws Exception
*/
function tr_file($file) {
return \TypeRocket\Utility\File::new($file);
}
/**
* Get Asset Version
*
* @param string $path
* @param string $namespace
* @return mixed
*/
function tr_manifest_cache($path, $namespace) {
return \TypeRocket\Utility\Manifest::cache(...func_get_args());
}
/**
* Get Asset Version
*
* @param string $namespace
* @return \TypeRocket\Utility\RuntimeCache
*/
function tr_manifest($namespace = 'typerocket') {
return \TypeRocket\Utility\Manifest::getFromRuntimeCache(...func_get_args());
}
/**
* Throw HTTP Error
*
* @param int $code
*
* @return mixed
*/
function tr_abort(int $code) {
\TypeRocket\Exceptions\HttpError::abort($code);
}
/**
* Enable Front-end
*/
function tr_frontend_enable()
{
\TypeRocket\Core\System::getFromContainer()->frontendEnable();
}
/**
* Sanitize Editor
*
* @param $content
* @param bool $force_filter
* @param bool $auto_p
*
* @return string
*/
function tr_sanitize_editor($content, $force_filter = true, $auto_p = false)
{
return \TypeRocket\Utility\Sanitize::editor($content, $force_filter, $auto_p);
}
/**
* @param array|null $data keys include message and type
* @param bool $dismissible
*
* @return false|string|null
*/
function tr_flash_message($data = null, $dismissible = false)
{
return \TypeRocket\Elements\Notice::flash(...func_get_args());
}
/**
* @param array|object|\ArrayObject $value
*
* @return \TypeRocket\Utility\Nil
*/
function tr_nils($value)
{
return \TypeRocket\Utility\Data::nil($value);
}
/**
* @param string $folder
*
* @return \TypeRocket\Utility\PersistentCache
*/
function tr_cache($folder = 'app')
{
return \TypeRocket\Utility\PersistentCache::new($folder);
}
/**
* Roles
*
* @return \TypeRocket\Auth\Roles
*/
function tr_roles()
{
return \TypeRocket\Auth\Roles::new();
}