1 | <?php |
||
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() { |
|
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 ) { |
||
113 | |||
114 | /** |
||
115 | * Return all currently active containers |
||
116 | * |
||
117 | * @return array |
||
118 | **/ |
||
119 | public function get_active_containers() { |
||
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 | } |
||
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 ) { |
||
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 ) { |
||
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 ) { |
||
173 | } |
||
174 |