-
Notifications
You must be signed in to change notification settings - Fork 22
/
model.php
906 lines (771 loc) · 25.8 KB
/
model.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
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
<?php
/**
* DustPress Model Class
*
* Extendable class which contains basic functions for DustPress models.
*
* @class DustPress_Model
*/
namespace DustPress;
/**
* The base class for template models.
*/
class Model {
/**
* The Data
*
* @var array
*/
public $data;
/**
* Class name
*
* @var string
*/
protected $class_name;
/**
* Arguments of this instance
*
* @var array
*/
private $args = [];
/**
* Instances of all submodels initiated from this class
*
* @var object
*/
private $submodels;
/**
* Possible parent model
*
* @var mixed|null
*/
private $parent;
/**
* Possible wanted template
*
* @var mixed
*/
private $template;
/**
* Temporary hash key
*
* @var string
*/
private $hash;
/**
* Is execution terminated
*
* @var boolean
*/
private $terminated;
// Called submodels
protected $called_subs;
// Methods that are allowed to run externally
protected $api;
/**
* The TTL for model cache
*
* @var array
*/
protected $ttl = [];
/**
* Constructor for DustPress model class.
*
* @date 2015-08-10
* @since 0.2.0
*
* @param array $args Model arguments.
* @param mixed $parent Set model parent.
*/
public function __construct( $args = [], $parent = null ) {
$this->fix_deprecated();
if ( ! empty( $args ) ) {
$this->args = $args;
}
$this->submodels = (object) [];
$this->parent = $parent;
}
/**
* Get model arguments
*
* @date 2016-06-03
* @since 0.4.0
* @return array
*/
public function get_args() {
return $this->args;
}
/**
* Set the arguments for a model
*
* @date 2018-01-26
* @since 1.11.0
*
* @param array $args Arguments for a model.
*
* @return void
*/
public function set_args( $args ) {
$this->args = $args;
}
/**
* Get the data from this model after fetch_data() has been run.
*
* @date 2016-06-03
* @since 0.4.0
*/
public function get_data() {
return $this->data;
}
/**
* Get the instance of an instantiated submodel
*
* @date 2016-06-03
* @since 0.4.0
*
* @param string $name Submodel name.
*
* @return \Dustpress\Model
*/
public function get_submodel( $name ) {
return $this->submodels->{$name};
}
/**
* Get all instantiated submodels for this model as an array
*
* @date 3/6/2016-06-03
* @since 0.4.0
* @return array|object
*/
public function get_submodels() {
return $this->submodels;
}
/**
* Get the ancestor of this model.
*
* @date 2016-06-03
*
* @since 0.4.0
*
* @param null|\DustPress\Model $model DustPress Model.
*
* @return \Dustpress\Model
*/
public function get_ancestor( $model = null ) {
if ( ! isset( $model ) ) {
return $this->get_ancestor( $this );
}
if ( isset( $model->parent ) ) {
return $this->get_ancestor( $model->parent );
}
return $model;
}
/**
* This function ensures deprecated functionalities will not break the model.
*
* @date 16/02/2017
* @since 1.5.5
*/
public function fix_deprecated() {
// Reassign deprecated "allowed_functions" to "api".
if ( isset( $this->allowed_functions ) ) {
error_log( 'DustPress: Model property "allowed_functions" is deprecated, use "api" instead.' );
$this->api = $this->allowed_functions;
}
}
/**
* This function gets the data from models and binds it to the global data structure.
*
* @date 2015-10-15
* @since 0.2.0
*
* @param array|null $functions Functions to bind.
* @param bool $tidy Tidy and return the tidied data.
*
* @return mixed
* @throws \ReflectionException If class does not have method, throw exception.
*/
public function fetch_data( $functions = null, $tidy = false ) {
$this->class_name = get_class( $this );
// Create a place to store the wanted data in the global data structure.
if ( ! isset( $this->data[ $this->class_name ] ) ) {
$this->data[ $this->class_name ] = new \stdClass();
}
// Fetch all methods from given class and in its parents.
$methods = $this->get_class_methods( $this->class_name );
$method_names = [];
// If a method has been overridden, remove duplicate instances so that they won't get run twice.
foreach ( $methods as $model => $values ) {
foreach ( $values as $key => $value ) {
if ( isset( $method_names[ $value ] ) ) {
unset( $methods[ $model ][ $key ] );
continue;
}
$method_names[ $value ] = true;
}
}
unset( $method_names );
// Check that all asked functions exist
if ( is_array( $functions ) && count( $functions ) > 0 ) {
foreach ( $functions as $function ) {
if ( ! $this->in_array_r( $function, $methods ) ) {
die( json_encode( [
"error" => sprintf(
"Method '%s' is not allowed to be run via AJAX or does not exist.",
$function
)
] ) );
}
}
}
// If we are on an AJAX call, we may want to run some private or protected functions too
$private_methods = [];
// Loop through the methods
foreach ( $methods as $class => &$class_methods ) {
foreach ( $class_methods as $index => $method_item ) {
$reflection = new \ReflectionMethod( $class, $method_item );
// If we have wanted list of functions, check if we can run them and don't run
// anything else.
if ( is_array( $functions ) && count( $functions ) > 0 ) {
if ( ! $this->in_array_r( $method_item, $functions ) ) {
continue;
}
if ( ! $this->is_function_allowed( $method_item ) ) {
die( json_encode( [
"error" => sprintf(
"Method '%s' is not allowed to be run via AJAX or does not exist.",
$method_item
)
] ) );
}
if ( $reflection->isProtected() || $reflection->isPrivate() ) {
$private_methods[] = $method_item;
continue;
}
// If the method has parameters, it should be run manually
if ( $reflection->getNumberOfParameters() > 0 ) {
unset( $class_methods[ $index ] );
} else {
$class_methods[ $index ] = [ $class, $method_item ];
}
continue;
}
if ( ! $reflection->isPublic() ) {
unset( $class_methods[ $index ] );
continue;
}
// If the method has parameters, it should be run manually
if ( $reflection->getNumberOfParameters() > 0 ) {
unset( $class_methods[ $index ] );
} else {
$class_methods[ $index ] = [ $class, $method_item ];
}
}
}
// Add some filters
$methods = apply_filters( 'dustpress/methods', $methods, $this->class_name );
$private_methods = apply_filters( 'dustpress/private_methods', $private_methods, $this->class_name );
// If we want tidy output, init variable for that
if ( $tidy ) {
$tidy_data = (object) [];
}
$methods = array_reverse( $methods );
$perf_monitoring_enabled = dustpress()->performance_enabled ?? false;
// Loop through all public methods and run the ones we wanted to deliver the data to the views.
foreach ( $methods as $class => $class_methods ) {
foreach ( $class_methods as $name => $m ) {
if ( $this->terminated ) {
break 2;
}
if ( $perf_monitoring_enabled ) {
$perf_key = $this->perf_key( $m );
dustpress()->start_performance( $perf_key );
}
if ( is_array( $m ) ) {
if ( isset( $m[1] ) && is_string( $m[1] ) ) {
if ( $m[1] === '__construct' ) {
continue;
}
$method = str_replace( 'bind_', '', $m[1] );
if ( ! isset( $this->data[ $this->class_name ] ) ) {
$this->data[ $this->class_name ] = (object) [];
}
$data = $this->run_function( $m[1], $class );
if ( $tidy ) {
$tidy_data->{$m[1]} = $data;
} else {
if ( ! is_null( $data ) ) {
$content = (array) $this->data[ $this->class_name ];
$content[ $method ] = $data;
$this->data[ $this->class_name ] = (object) $content;
}
}
}
} elseif ( is_callable( $m ) ) {
if ( $m === '__construct' ) {
continue;
}
$method = str_replace( 'bind_', '', $m );
if ( ! isset( $this->data[ $this->class_name ]->{$method} ) ) {
$this->data[ $this->class_name ]->{$method} = [];
}
$data = $this->run_function( $m, $class );
if ( ! is_null( $data ) ) {
if ( $tidy ) {
$tidy_data->{$method} = $data;
} else {
$this->data[ $this->class_name ]->{$method} = $data;
}
}
}
if ( $perf_monitoring_enabled ) {
dustpress()->save_performance( $perf_key );
}
}
unset( $class_methods );
}
// If there are private methods to run, run them too.
if ( is_array( $private_methods ) && count( $private_methods ) > 0 ) {
foreach ( $private_methods as $method ) {
if ( $this->terminated ) {
break;
}
$data = $this->run_restricted( $method );
if ( is_null( $data ) ) {
continue;
}
if ( $tidy ) {
$tidy_data->{$method} = $data;
continue;
}
$content = (array) $this->data[ $this->class_name ];
$content[ $method ] = $data;
$this->data[ $this->class_name ] = (object) $content;
}
}
if ( $tidy ) {
$this->data = $tidy_data;
return $tidy_data;
}
return $this->data[ $this->class_name ];
}
/**
* This function returns all public methods from current class and it parents up to
* but not including Model.
*
* @date 2015-03-19
* @since 0.0.1
*
* @param string $class_name Class name we want methods from.
* @param array $methods If we already have methods defined.
*
* @return array Class Methods.
* @throws \ReflectionException If class can't be reflected.
*/
private function get_class_methods( $class_name, $methods = [] ) {
$rc = new \ReflectionClass( $class_name );
$rmpu = $rc->getMethods();
if ( ! isset( $methods ) ) {
$methods = [];
}
if ( ! isset( $methods[ $class_name ] ) ) {
$methods[ $class_name ] = [];
}
foreach ( $rmpu as $r ) {
if ( $r->class === $class_name ) {
$methods[ $class_name ][] = $r->name;
}
}
$class_parent = get_parent_class( $class_name );
if ( ! empty( $class_parent ) && 'DustPress\Model' !== $class_parent ) {
$methods = $this->get_class_methods( $class_parent, $methods );
}
return $methods;
}
/**
* This function checks if a bound submodel is wanted to run and if it is, runs it.
*
* @date 2015-03-17
* @since 0.0.1
*
* @param string $name Submodel name.
* @param null $args Arguments to pass to the submodel.
* @param bool $cache_sub Cache Submodel results.
*
* @throws \Exception If provided submodel name is not a string.
*/
public function bind_sub( $name, $args = null, $cache_sub = true ) {
if ( $this->terminated === true ) {
return;
}
$this->class_name = get_class( $this );
if ( ! is_string( $name ) ) {
throw new \Exception(
sprintf(
'DustPress error: bind_sub was called with invalid class name: %s',
print_r( $name, true )
)
);
}
$model = new $name( $args, $this );
$data = $model->fetch_data();
$class_name = $model->class_name;
// If the submodel is not on the root level, set it under the current submodel.
if ( $this->parent ) {
if ( isset( $this->data[ $this->class_name ]->{$class_name} ) ) {
$data = array_merge(
(array) $this->data[ $this->class_name ]->{$class_name},
(array) $data
);
}
$this->data[ $this->class_name ]->{$class_name} = $data;
} // Set submodel under the main model.
else {
if ( isset( $this->data[ $class_name ] ) ) {
$data = array_merge(
(array) $this->data[ $class_name ],
(array) $data
);
}
$this->data[ $class_name ] = $data;
}
if ( ! is_object( $this->submodels ) ) {
$this->submodels = (object) [];
}
$this->submodels->{$name} = $model;
// Store called submodels for caching purposes.
if ( $cache_sub ) {
if ( empty( $this->called_subs ) ) {
$this->called_subs = [];
}
$this->called_subs[] = [
'class_name' => $name,
'args' => $args,
];
}
if ( $model->terminated ) {
$this->terminate();
}
}
/**
* This function binds the data from the models to the global data structure.
* It takes the data key as second parameter and optional data block name as third.
*
* @date 2015-03-17
* @since 0.0.1
*
* @param mixed $data Data to bind to the model key.
* @param string $key Key to bind to.
* @param string $model Model to bind data to.
*/
public function bind( $data, $key = null, $model = null ) {
if ( ! $key ) {
die( 'DustPress error: You need to specify the key if you use bind(). Use return if you want to use the function name.' );
}
if ( ! isset( $this->class_name ) ) {
$this->class_name = get_class( $this );
}
if ( $model ) {
// Create a place to store the wanted data in the global data structure.
if ( ! isset( $this->data[ $model ] ) ) {
$this->data[ $model ] = new \stdClass();
}
if ( ! isset( $this->data[ $model ] ) ) {
$this->data[ $model ] = (object) [];
}
$this->data[ $model ]->{$key} = $data;
return;
}
// Create a place to store the wanted data in the global data structure.
if ( ! isset( $this->data[ $this->class_name ] ) ) {
$this->data[ $this->class_name ] = new \stdClass();
}
if ( $this->parent ) {
$this->data[ $this->class_name ]->{$key} = $data;
return;
}
if ( is_array( $data ) && isset( $this->data[ $this->class_name ]->{$key} ) ) {
$data = array_merge(
(array) $this->data[ $this->class_name ]->{$key},
$data
);
}
$this->data[ $this->class_name ]->{$key} = $data;
}
/**
* This function returns the desired Dust template.
* If the developer has defined one instead of default.
* Otherwise return false.
*
* @date 2015-10-15
* @since 0.2.0
*
* @return mixed
*/
public function get_template() {
$ancestor = $this->get_ancestor();
return $this === $ancestor
? $this->template
: $ancestor->get_template();
}
/**
* This function lets the developer to set the template to be used to render a page.
*
* @date 2015-10-15
* @since 0.2.0
*
* @param string $template Template name.
*/
public function set_template( $template ) {
$ancestor = $this->get_ancestor();
if ( ! $template ) {
return;
}
if ( $this === $ancestor ) {
$this->template = $template;
return;
}
$ancestor->set_template( $template );
}
/**
* This function checks whether data exists in cache (if cache is enabled)
* and returns the data or runs the function and returns its return data.
*
* @date 2016-01-29
* @since 0.3.1
*
* @param string $m Function name.
* @param null $class Class name.
*
* @return mixed
* @throws \ReflectionException If selected class does not have function.
* @throws \Exception Running get_cached() might throw an exception.
*/
private function run_function( $m, $class = null ) {
$cached = $this->get_cached( $m );
if ( is_null( $class ) ) {
$class = $this->class_name;
}
if ( $cached ) {
return $cached;
}
$reflection = new \ReflectionMethod( $class, $m );
$result = $reflection->isStatic()
? call_user_func( $class . '::' . $m )
: call_user_func( [ $this, $m ] );
$subs = null;
if ( isset( $this->called_subs ) ) {
$subs = $this->called_subs;
}
$this->maybe_cache( $m, $result, $subs );
// Unset called submodels for this run
$this->called_subs = null;
return $result;
}
/**
* This function checks if the function is defined as cacheable and returns the cache if it exists.
*
* @date 2016-01-29
* @since 0.3.1
*
* @param string $m Function name.
*
* @return mixed|bool
* @throws \Exception Running bind_sub() might throw an exception.
*/
private function get_cached( $m ) {
if ( ! dustpress()->get_setting( 'cache' ) ) {
return false;
}
$this->hash = $this->generate_cache_key(
$this->class_name,
$this->get_args(),
$m
);
$cached = get_transient( $this->hash );
if ( false === $cached ) {
return false;
}
// Run stored submodel calls
if ( isset( $cached->subs ) && is_array( $cached->subs ) ) {
foreach ( $cached->subs as $sub_data ) {
// Run submodel without caching it.
$this->bind_sub(
$sub_data['class_name'],
$sub_data['args'],
false
);
}
}
return $cached->data;
}
/**
* This function stores data sets to transient cache if it is enabled
* and indexes cache keys for model-function-pairs.
*
* @date 2016-01-29
* @since 0.3.1
*
* @param string $m Function name.
* @param mixed $data Data to cache.
* @param array $subs Submodels.
*/
private function maybe_cache( $m, $data, $subs ) {
// Check whether cache is enabled and model has ttl-settings.
if ( ! dustpress()->get_setting( 'cache' ) || ! $this->is_cacheable_function( $m ) ) {
return;
}
// If no hash key exists, bail out
if ( empty( $this->hash ) ) {
return;
}
// Extend data with submodels
$to_cache = (object) [
'data' => $data,
'subs' => $subs,
];
set_transient( $this->hash, $to_cache, $this->ttl[ $m ] );
// Index key for cache clearing
$index = $this->generate_cache_key( $this->class_name, $m );
$hash_index = get_transient( $index );
if ( ! is_array( $hash_index ) ) {
$hash_index = [];
}
// Set the data hash key to the index array of this model function
if ( ! in_array( $this->hash, $hash_index, true ) ) {
$hash_index[] = $this->hash;
}
// Store transient for 30 days
set_transient( $index, $hash_index, 30 * DAY_IN_SECONDS );
}
/**
* Checks whether the function is to be cached.
*
* @param string $m Function name.
*
* @return boolean
*/
private function is_cacheable_function( $m ) {
// TTL is not empty, it is an array and our method name is one of the keys.
return ! empty( $this->ttl ) && is_array( $this->ttl ) && array_key_exists( $m, $this->ttl );
}
/**
* This functions returns true if asked private or protected functions is
* allowed to be run via the run wrapper.
*
* @date 2015-12-17
* @since 0.3.0
*
* @param string $function Function name to check.
*
* @return boolean Is function allowed to be run.
* @throws \ReflectionException If function can't be found, throw an exception.
*/
private function is_function_allowed( $function ) {
if ( ! defined( 'DOING_AJAX' ) ) {
$reflection = new \ReflectionMethod( $this, $function );
return $reflection->isPublic();
}
return isset( $this->api ) && is_array( $this->api ) && in_array( $function, $this->api, true );
}
/**
* This functions creates a cache key hash from parameters.
*
* @date 2015-12-17
* @since 0.3.0
* @return string Cache key from serialized and hashed arguments.
*/
private function generate_cache_key() {
$seed = '';
foreach ( func_get_args() as $arg ) {
$seed .= serialize( $arg );
}
$seed = apply_filters( 'dustpress/cache/seed', $seed );
return sha1( $seed );
}
/**
* This function runs a restricted function if it exists in the allowed functions
* and returns whatever the wanted function returns.
*
* @date 2015-12-17
* @since 0.3.0
*
* @param string $function Wanted function name.
*
* @return mixed
* @throws \ReflectionException If restricted function does not exist.
*/
public function run_restricted( $function ) {
if ( $this->is_function_allowed( $function ) ) {
return $this->run_function( $function );
}
return (object) [ 'error' => 'Wanted function does not exist in the allowed functions list.' ];
}
/**
* Rename current model's data block. Probably for template changing purposes.
*
* @date 2016-09-14
* @since 1.2.0
*
* @param string $name New name for the model.
*/
protected function rename_model( $name ) {
$original = $this->class_name;
if ( $original === $name ) {
return;
}
$this->class_name = $name;
if ( isset( $this->data[ $original ] ) ) {
$this->data[ $name ] = $this->data[ $original ];
unset( $this->data[ $original ] );
}
if ( ! isset( $this->data[ $name ] ) ) {
$this->data[ $name ] = (object) [];
}
}
/**
* A recursive array search.
*
* @param mixed $needle Needle we are looking for.
* @param array $haystack Haystack we are looking from.
* @param boolean $strict Should we be strict about it.
*
* @return boolean
*/
protected function in_array_r( $needle, $haystack, $strict = true ) {
foreach ( $haystack as $item ) {
$needle_match = ( $strict ? $item === $needle : $item == $needle );
$array_match = is_array( $item ) && $this->in_array_r( $needle, $item, $strict );
if ( $needle_match || $array_match ) {
return true;
}
}
return false;
}
/**
* Set Model terminated.
*/
public function terminate() {
$this->terminated = true;
}
/**
* Returns terminated methods.
*
* @return mixed
*/
public function get_terminated() {
return $this->terminated;
}
/**
* Generate performance tracking key.
*
* @param string $key Performance key.
* @param string $prefix Prefix for performance key.
*
* @return string
*/
private function perf_key( $key = '', $prefix = 'Models.%s' ) {
return sprintf( $prefix, wp_json_encode( $key ) );
}
}