1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Container; |
4
|
|
|
|
5
|
|
|
use Carbon_Fields\Helper\Helper; |
6
|
|
|
use Carbon_Fields\Exception\Incorrect_Syntax_Exception; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Keeps track of all instantiated containers |
10
|
|
|
*/ |
11
|
|
|
class Repository { |
12
|
|
|
/** |
13
|
|
|
* List of registered unique panel identificator ids |
14
|
|
|
* |
15
|
|
|
* @see get_unique_panel_id() |
16
|
|
|
* @see register_unique_panel_id() |
17
|
|
|
* @see unregister_unique_panel_id() |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $registered_panel_ids = array(); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* List of registered containers that should be initialized |
24
|
|
|
* |
25
|
|
|
* @see initialize_containers() |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $pending_containers = array(); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* List of all containers |
32
|
|
|
* |
33
|
|
|
* @see _attach() |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $containers = array(); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Register a container with the repository |
40
|
|
|
* |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
5 |
|
public function register_container( Container $container ) { |
44
|
5 |
|
$this->register_unique_panel_id( $container->id ); |
45
|
5 |
|
$this->containers[] = $container; |
46
|
5 |
|
$this->pending_containers[] = $container; |
47
|
5 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Initialize registered containers |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
3 |
|
public function initialize_containers() { |
55
|
3 |
|
$initialized_containers = array(); |
56
|
3 |
|
while ( ( $container = array_shift( $this->pending_containers ) ) ) { |
57
|
3 |
|
$container->init(); |
58
|
3 |
|
$initialized_containers[] = $container; |
59
|
3 |
|
} |
60
|
|
|
|
61
|
3 |
|
return $initialized_containers; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Return all containers |
66
|
|
|
* |
67
|
|
|
* @param string $type Container type to filter for |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
public function get_containers( $type = null ) { |
71
|
|
|
$raw_containers = $this->containers; |
72
|
|
|
$containers = array(); |
73
|
|
|
|
74
|
|
|
if ( $type === null ) { |
75
|
|
|
$containers = $raw_containers; |
76
|
|
|
} else { |
77
|
|
|
$normalized_type = Helper::normalize_type( $type ); |
78
|
|
|
foreach ( $raw_containers as $container ) { |
79
|
|
|
if ( $container->type === $normalized_type ) { |
80
|
|
|
$containers[] = $container; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $containers; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Return field in containers |
90
|
|
|
* |
91
|
|
|
* @param string $field_name |
92
|
|
|
* @param string $container_type Container type to filter for |
93
|
|
|
* @param bool $include_nested_fields Search in nested fields as well |
94
|
|
|
* @return Carbon_Fields\Field\Field |
95
|
|
|
*/ |
96
|
|
|
public function get_field_in_containers( $field_name, $container_type = null, $include_nested_fields = true ) { |
97
|
|
|
$containers = $this->get_containers( $container_type ); |
98
|
|
|
$field = null; |
99
|
|
|
|
100
|
|
|
foreach ( $containers as $container ) { |
101
|
|
|
if ( $include_nested_fields ) { |
102
|
|
|
$field = $container->get_field_by_name( $field_name ); |
103
|
|
|
} else { |
104
|
|
|
$field = $container->get_root_field_by_name( $field_name ); |
105
|
|
|
} |
106
|
|
|
if ( $field ) { |
107
|
|
|
break; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $field; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Return all currently active containers |
116
|
|
|
* |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
public function get_active_containers() { |
120
|
1 |
|
return array_filter( $this->containers, function( $container ) { |
121
|
1 |
|
return $container->active(); |
122
|
1 |
|
} ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Generate a unique container identificator id based on container title |
127
|
|
|
* |
128
|
|
|
* @param string $title |
129
|
|
|
*/ |
130
|
3 |
|
public function get_unique_panel_id( $title ) { |
131
|
3 |
|
$id = preg_replace( '~[^\w\-]*~', '', remove_accents( $title ) ); |
132
|
3 |
|
$base = $id; |
133
|
3 |
|
$suffix = 0; |
134
|
|
|
|
135
|
3 |
|
while ( ! $this->is_unique_panel_id( $id ) ) { |
136
|
2 |
|
$suffix++; |
137
|
2 |
|
$id = $base . strval( $suffix ); |
138
|
2 |
|
} |
139
|
|
|
|
140
|
3 |
|
return $id; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Check if container identificator id is unique |
145
|
|
|
* |
146
|
|
|
* @param string $id |
147
|
|
|
*/ |
148
|
|
|
protected function is_unique_panel_id( $id ) { |
149
|
|
|
return ! in_array( $id, $this->registered_panel_ids ); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Add container identificator id to the list of unique container ids |
154
|
|
|
* |
155
|
|
|
* @param string $id |
156
|
|
|
*/ |
157
|
|
|
protected function register_unique_panel_id( $id ) { |
158
|
|
|
if ( $this->is_unique_panel_id( $id ) ) { |
159
|
|
|
$this->registered_panel_ids[] = $id; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Remove container identificator id from the list of unique container ids |
165
|
|
|
* |
166
|
|
|
* @param string $id |
167
|
|
|
*/ |
168
|
|
|
protected function unregister_unique_panel_id( $id ) { |
169
|
|
|
if ( ! $this->is_unique_panel_id( $id ) ) { |
170
|
|
|
unset( $this->registered_panel_ids[ array_search( $id, $this->registered_panel_ids ) ] ); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|