1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Field; |
4
|
|
|
|
5
|
|
|
use Carbon_Fields\Datastore\Datastore_Interface; |
6
|
|
|
use Carbon_Fields\Exception\Incorrect_Syntax_Exception; |
7
|
|
|
|
8
|
|
|
class Group_Field { |
9
|
|
|
/** |
10
|
|
|
* Unique group identificator. Generated randomly. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $group_id; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Sanitized group name. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $name; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Group label, used during rendering. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $label; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Group label underscore template. |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $label_template; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Group fields. |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $fields = array(); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* List of registered unique field names |
46
|
|
|
* |
47
|
|
|
* @see verify_unique_field_name() |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $registered_field_names = array(); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Create a group field with the specified name and label. |
54
|
|
|
* |
55
|
|
|
* @param string $name |
56
|
|
|
* @param string $label |
57
|
|
|
* @param array $fields |
58
|
|
|
*/ |
59
|
|
|
public function __construct($name, $label, $fields) { |
60
|
|
|
$this->set_name( $name ); |
61
|
|
|
$this->set_label( $label ); |
62
|
|
|
$this->add_fields( $fields ); |
63
|
|
|
|
64
|
|
|
// Pick random ID |
65
|
|
|
$random_string = md5( mt_rand() . $this->get_name() . $this->get_label() ); |
66
|
|
|
$random_string = substr( $random_string, 0, 5 ); // 5 chars should be enough |
67
|
|
|
$this->group_id = 'carbon-group-' . $random_string; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Add a group of fields. |
72
|
|
|
* |
73
|
|
|
* @param array $fields |
74
|
|
|
*/ |
75
|
|
|
public function add_fields( $fields ) { |
76
|
|
|
foreach ( $fields as $field ) { |
77
|
|
|
if ( ! is_a( $field, __NAMESPACE__ . '\\Field' ) ) { |
78
|
|
|
Incorrect_Syntax_Exception::raise( 'Object must be of type ' . __NAMESPACE__ . '\\Field' ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// verify name validity |
82
|
|
|
if ( preg_match( '~_\d+~', $field->get_name() ) ) { |
83
|
|
|
Incorrect_Syntax_Exception::raise( 'Subfield names cannot contain underscore followed by a digit(s). Replace "' . ltrim( $field->get_name(), '_' ) . '" with "' . ltrim( preg_replace( '~_+(\d+)~', '$1', $field->get_name() ), '_' ) . '"' ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$this->verify_unique_field_name( $field->get_name() ); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->fields = array_merge( $this->fields, $fields ); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Fields attribute getter. |
94
|
|
|
* |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
public function get_fields() { |
98
|
|
|
return $this->fields; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Return the names of all fields. |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
public function get_field_names() { |
107
|
|
|
$names = array(); |
108
|
|
|
|
109
|
|
|
foreach ( $this->fields as $field ) { |
110
|
|
|
$names[] = $field->get_name(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $names; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Returns an array that holds the field data, suitable for JSON representation. |
118
|
|
|
* This data will be available in the Underscore template and the Backbone Model. |
119
|
|
|
* |
120
|
|
|
* @param bool $load Should the value be loaded from the database or use the value from the current instance. |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
|
|
public function to_json( $load ) { |
124
|
|
|
$fields_data = array(); |
125
|
|
|
|
126
|
|
|
foreach ( $this->get_fields() as $field ) { |
127
|
|
|
// The field default value should be set manually if the field is not loaded |
128
|
|
|
if ( ! $load ) { |
129
|
|
|
if ( $field->get_value() === null ) { |
130
|
|
|
$field->set_value( $field->get_default_value() ); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$fields_data[] = $field->to_json( $load ); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$group_data = array( |
138
|
|
|
'group_id' => $this->get_group_id(), |
139
|
|
|
'name' => $this->get_name(), |
140
|
|
|
'label' => $this->get_label(), |
141
|
|
|
'fields' => $fields_data, |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
return $group_data; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Group ID attribute getter. |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
public function get_group_id() { |
153
|
|
|
return $this->group_id; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Set the group label. |
158
|
|
|
* |
159
|
|
|
* @param string $label If null, the label will be generated from the group name |
160
|
|
|
*/ |
161
|
|
|
public function set_label( $label ) { |
162
|
|
|
// Try to guess field label from it's name |
163
|
|
View Code Duplication |
if ( is_null( $label ) ) { |
|
|
|
|
164
|
|
|
// remove the leading underscore(if it's there) |
165
|
|
|
$label = preg_replace( '~^_~', '', $this->name ); |
166
|
|
|
|
167
|
|
|
// remove the leading "crb_"(if it's there) |
168
|
|
|
$label = preg_replace( '~^crb_~', '', $label ); |
169
|
|
|
|
170
|
|
|
// split the name into words and make them capitalized |
171
|
|
|
$label = ucwords( str_replace( '_', ' ', $label ) ); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$this->label = $label; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Label attribute getter. |
179
|
|
|
* |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
|
|
public function get_label() { |
183
|
|
|
return $this->label; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Set the Underscore label template. |
188
|
|
|
* |
189
|
|
|
* @param string $template |
190
|
|
|
*/ |
191
|
|
|
public function set_label_template($template) { |
192
|
|
|
$this->label_template = $template; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Set the Underscore label template. |
197
|
|
|
* |
198
|
|
|
* @return string |
199
|
|
|
*/ |
200
|
|
|
public function get_label_template() { |
201
|
|
|
return $this->label_template; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Print the label Underscore template. |
206
|
|
|
*/ |
207
|
|
|
public function template_label() { |
208
|
|
|
echo $this->label_template; |
|
|
|
|
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Set the group name. |
213
|
|
|
* |
214
|
|
|
* @param string $name Group name, either sanitized or not |
215
|
|
|
*/ |
216
|
|
|
public function set_name( $name ) { |
217
|
|
|
$name = preg_replace( '~\s+~', '_', strtolower( $name ) ); |
218
|
|
|
if ( substr( $name, 0, 1 ) != '_' ) { |
219
|
|
|
// add underscore to custom field name -- this will remove it from |
220
|
|
|
// custom fields list in administration |
221
|
|
|
$name = '_' . $name; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
$this->name = $name; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Return the group name. |
229
|
|
|
* |
230
|
|
|
* @return string |
231
|
|
|
*/ |
232
|
|
|
public function get_name() { |
233
|
|
|
return $this->name; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Assign a DataStore instance for all group fields. |
238
|
|
|
* |
239
|
|
|
* @param object $store |
240
|
|
|
*/ |
241
|
|
|
public function set_datastore( Datastore_Interface $store ) { |
242
|
|
|
foreach ( $this->fields as $field ) { |
243
|
|
|
if ( ! $field->get_datastore() ) { |
244
|
|
|
$field->set_datastore( $store ); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Set a prefix for all group fields. |
251
|
|
|
* |
252
|
|
|
* @param string $prefix |
253
|
|
|
*/ |
254
|
|
|
public function set_prefix( $prefix ) { |
255
|
|
|
foreach ( $this->fields as $field ) { |
256
|
|
|
$field->set_prefix( $prefix ); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Perform checks whether there is a field registered with the name $name. |
262
|
|
|
* If not, the field name is recorded. |
263
|
|
|
* |
264
|
|
|
* @param string $name |
265
|
|
|
*/ |
266
|
|
|
public function verify_unique_field_name( $name ) { |
267
|
|
|
if ( in_array( $name, $this->registered_field_names ) ) { |
268
|
|
|
Incorrect_Syntax_Exception::raise( 'Field name "' . $name . '" already registered' ); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
$this->registered_field_names[] = $name; |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.