1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12: function xu_get_model( $model, array $args = [], $dir = 'models' ) {
13: if ( method_exists( $model, 'model' ) ) {
14: return call_user_func_array( [$model, 'model'], $args );
15: }
16:
17: $file = null;
18: $names = [
19: sprintf( '%s/class-%s.php', $dir, $model ),
20: sprintf( '%s/%s.php', $dir, $model )
21: ];
22:
23: foreach ( $names as $name ) {
24: 25: 26: 27: 28:
29: if ( $file = apply_filters( 'xu_get_model', $model ) ) {
30: if ( file_exists( $file ) ) {
31: break;
32: }
33: }
34:
35:
36: if ( $file = locate_template( $name, true ) ) {
37: if ( file_exists( $file ) ) {
38: break;
39: }
40: }
41: }
42:
43: if ( empty( $file ) ) {
44: return;
45: }
46:
47: $class_name = xu_get_class_name( $file );
48: $reflection = new ReflectionClass( $class_name );
49:
50: return $reflection->newInstanceArgs( $args );
51: }
52: