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 container ids |
14
|
|
|
* |
15
|
|
|
* @see get_unique_container_id() |
16
|
|
|
* @see register_unique_container_id() |
17
|
|
|
* @see unregister_unique_container_id() |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $registered_container_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
|
|
|
* Container id prefix |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $container_id_prefix = 'carbon_fields_container_'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Container id prefix |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $widget_id_wildcard_suffix = '-__i__'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Register a container with the repository |
54
|
|
|
* |
55
|
|
|
* @param Container $container |
56
|
|
|
*/ |
57
|
5 |
|
public function register_container( Container $container ) { |
58
|
5 |
|
$this->register_unique_container_id( $container->get_id() ); |
59
|
5 |
|
$this->containers[] = $container; |
60
|
5 |
|
$this->pending_containers[] = $container; |
61
|
5 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Initialize registered containers |
65
|
|
|
* |
66
|
|
|
* @return Container[] |
67
|
|
|
*/ |
68
|
3 |
|
public function initialize_containers() { |
69
|
3 |
|
$initialized_containers = array(); |
70
|
3 |
|
while ( ( $container = array_shift( $this->pending_containers ) ) ) { |
71
|
3 |
|
$container->init(); |
72
|
3 |
|
$initialized_containers[] = $container; |
73
|
|
|
} |
74
|
|
|
|
75
|
3 |
|
return $initialized_containers; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Return all containers |
80
|
|
|
* |
81
|
|
|
* @param string $type Container type to filter for |
82
|
|
|
* @return Container[] |
83
|
|
|
*/ |
84
|
|
|
public function get_containers( $type = null ) { |
85
|
|
|
$raw_containers = $this->containers; |
86
|
|
|
$containers = array(); |
87
|
|
|
|
88
|
|
|
if ( $type === null ) { |
89
|
|
|
$containers = $raw_containers; |
90
|
|
|
} else { |
91
|
|
|
$normalized_type = Helper::normalize_type( $type ); |
92
|
|
|
foreach ( $raw_containers as $container ) { |
93
|
|
|
if ( $container->type === $normalized_type ) { |
94
|
|
|
$containers[] = $container; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $containers; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Return field in a container with supplied id |
104
|
|
|
* |
105
|
|
|
* @param string $field_name |
106
|
|
|
* @param string $container_id |
107
|
|
|
* @param bool $include_nested_fields |
108
|
|
|
* @return \Carbon_Fields\Field\Field |
109
|
|
|
*/ |
110
|
|
View Code Duplication |
public function get_field_in_container( $field_name, $container_id, $include_nested_fields = true ) { |
111
|
|
|
$containers = $this->get_containers(); |
112
|
|
|
$field = null; |
113
|
|
|
|
114
|
|
|
foreach ( $containers as $container ) { |
115
|
|
|
if ( $container->get_id() !== $container_id ) { |
116
|
|
|
continue; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if ( $include_nested_fields ) { |
120
|
|
|
$field = $container->get_field_by_name( $field_name ); |
121
|
|
|
} else { |
122
|
|
|
$field = $container->get_root_field_by_name( $field_name ); |
123
|
|
|
} |
124
|
|
|
break; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $field; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Return field in containers |
132
|
|
|
* |
133
|
|
|
* @param string $field_name |
134
|
|
|
* @param string $container_type |
135
|
|
|
* @param bool $include_nested_fields |
136
|
|
|
* @return \Carbon_Fields\Field\Field |
137
|
|
|
*/ |
138
|
|
View Code Duplication |
public function get_field_in_containers( $field_name, $container_type = null, $include_nested_fields = true ) { |
139
|
|
|
$containers = $this->get_containers( $container_type ); |
140
|
|
|
$field = null; |
141
|
|
|
|
142
|
|
|
foreach ( $containers as $container ) { |
143
|
|
|
if ( $include_nested_fields ) { |
144
|
|
|
$field = $container->get_field_by_name( $field_name ); |
145
|
|
|
} else { |
146
|
|
|
$field = $container->get_root_field_by_name( $field_name ); |
147
|
|
|
} |
148
|
|
|
if ( $field ) { |
149
|
|
|
break; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $field; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Return all currently active containers |
158
|
|
|
* |
159
|
|
|
* @return Container[] |
160
|
|
|
*/ |
161
|
|
|
public function get_active_containers() { |
162
|
1 |
|
return array_filter( $this->containers, function( $container ) { |
163
|
|
|
/** @var Container $container */ |
164
|
1 |
|
return $container->is_active(); |
165
|
1 |
|
} ); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Check if container identificator id is unique |
170
|
|
|
* |
171
|
|
|
* @param string $id |
172
|
|
|
* @return bool |
173
|
|
|
*/ |
174
|
|
|
public function is_unique_container_id( $id ) { |
175
|
|
|
return ! in_array( $id, $this->registered_container_ids ); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Generate a unique container identificator id based on container title |
180
|
|
|
* |
181
|
|
|
* @param string $title |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
3 |
|
public function get_unique_container_id( $title ) { |
185
|
3 |
|
$id = remove_accents( $title ); |
186
|
3 |
|
$id = strtolower( $id ); |
187
|
|
|
|
188
|
3 |
|
$id_prefix = $this->container_id_prefix; |
189
|
3 |
|
if ( substr( $id, 0, strlen( $id_prefix ) ) === $id_prefix ) { |
190
|
|
|
$id_prefix = ''; |
191
|
|
|
} |
192
|
|
|
|
193
|
3 |
|
$wids = $this->widget_id_wildcard_suffix; |
194
|
3 |
|
$id_suffix = ''; |
195
|
3 |
|
if ( substr( $id, -strlen( $wids ) ) === $wids ) { |
196
|
|
|
$id_suffix = $wids; |
197
|
|
|
$id = substr( $id, 0, -strlen( $wids ) ); |
198
|
|
|
} |
199
|
|
|
|
200
|
3 |
|
$id = preg_replace( '~[\s]+~', '_', $id ); |
201
|
3 |
|
$id = preg_replace( '~[^\w\-\_]+~', '', $id ); |
202
|
|
|
|
203
|
3 |
|
$id = $id_prefix . $id . $id_suffix; |
204
|
|
|
|
205
|
3 |
|
$base = $id; |
206
|
3 |
|
$suffix = 0; |
207
|
|
|
|
208
|
3 |
|
while ( ! $this->is_unique_container_id( $id ) ) { |
209
|
1 |
|
$suffix++; |
210
|
1 |
|
$id = $base . strval( $suffix ); |
211
|
|
|
} |
212
|
|
|
|
213
|
3 |
|
return $id; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Add container identificator id to the list of unique container ids |
218
|
|
|
* |
219
|
|
|
* @param string $id |
220
|
|
|
*/ |
221
|
|
|
protected function register_unique_container_id( $id ) { |
222
|
|
|
if ( $this->is_unique_container_id( $id ) ) { |
223
|
|
|
$this->registered_container_ids[] = $id; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Remove container identificator id from the list of unique container ids |
229
|
|
|
* |
230
|
|
|
* @param string $id |
231
|
|
|
*/ |
232
|
|
|
protected function unregister_unique_container_id( $id ) { |
233
|
|
|
if ( ! $this->is_unique_container_id( $id ) ) { |
234
|
|
|
unset( $this->registered_container_ids[ array_search( $id, $this->registered_container_ids ) ] ); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|