forked from folbert/fewbricks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
field-group.php
454 lines (308 loc) · 12.7 KB
/
field-group.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<?php
namespace fewbricks\acf;
// If we are in dev mode...
if(\fewbricks\helpers\is_fewbricks_in_developer_mode()) {
// Create place to keep keys of all fields/bricks etc.
global $debug_keys;
$debug_keys = [];
}
/**
* Class field_group
* @package fewbricks\acf
*/
class field_group
{
/**
* @var array
*/
private $settings;
/**
* @param string $title The text to be displayed as title to the administrator.
* @param string $key This must be unique across the site. A god idea is to use the current time and then add a
* random character. Like so: 1509011031t for September 1st, 2015 @ 10:31.
* @param array $location Where the field group should be displayed.
* @param int $menu_order Where the field group should be positioned.
* @param array $settings This allows us to set any setting that a field group can have. See $base_settins for
* available options.
*/
public function __construct($title, $key, $location, $menu_order = 1, $settings = [])
{
// Since the result of using this class will be an array,
// we might as well store everything in an array right away.
$base_settings = [
'key' => $key,
'title' => $title,
'fields' => [],
'location' => $location,
'menu_order' => $menu_order,
'position' => 'normal',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => []
];
$this->settings = array_merge($base_settings, $settings);
}
/**
* @param $key
* @param $value
* @return $this
*/
public function set_setting($key, $value)
{
$this->settings[$key] = $value;
return $this;
}
/**
* @param $key
* @param string $default_value
* @return string
*/
public function get_setting($key, $default_value = '')
{
$value = $default_value;
if (isset($this->settings[$key])) {
$value = $this->settings[$key];
}
return $value;
}
/**
* @return array
*/
public function get_settings()
{
return $this->settings;
}
/**
* @param \fewbricks\acf\fields\field $field
* @return $this
*/
public function add_field($field)
{
$this->settings['fields'][] = $field->get_settings($this);
return $this;
}
/**
* @param \fewbricks\bricks\brick $brick
* @return $this
*/
public function add_brick($brick)
{
// Add the fields of the brick to the fields of the brick
$this->set_setting('fields', array_merge($this->get_setting('fields'), $brick->get_settings($this)['fields']));
return $this;
}
/**
* @param string $common_field_array_key A key corresponding to an item in the fewbricks_common_fields array
* @param string $key A site wide unique key for the field
* @param array $settings Anye extra settings to set on the field. Can be used to override existing settings as well.
*/
public function add_common_field($common_field_array_key, $key, $settings = [])
{
global $fewbricks_common_fields;
if (isset($fewbricks_common_fields[$common_field_array_key])) {
$field = clone $fewbricks_common_fields[$common_field_array_key];
/** @noinspection PhpUndefinedMethodInspection */
$field->set_setting('key', $key);
/** @noinspection PhpUndefinedMethodInspection */
$field->set_settings($settings);
$this->add_field($field);
}
}
/**
* @param \fewbricks\acf\fields\flexible_content $flexible_content
* @return fields\flexible_content
*/
public function add_flexible_content($flexible_content)
{
$this->settings['fields'][] = $flexible_content->get_settings($this);
return $flexible_content;
}
/**
* Register a field group.
*/
public function register()
{
global $fewbricks_save_json;
if (!isset($this->settings['names_of_items_to_hide_on_screen']) && !isset($this->settings['names_of_items_to_show_on_screen'])) {
$this->settings['hide_on_screen'] = $this->get_hide_on_screen_settings();
} elseif (isset($this->settings['names_of_items_to_show_on_screen'])) {
$this->settings['hide_on_screen'] = $this->get_hide_on_screen_settings(false,
$this->settings['names_of_items_to_show_on_screen']);
} else {
$this->settings['hide_on_screen'] = $this->get_hide_on_screen_settings(
$this->settings['names_of_items_to_hide_on_screen'], false);
}
$this->settings['fields'] = $this->set_unique_keys($this->settings['fields'], $this->settings['key']);
if(\fewbricks\helpers\is_fewbricks_in_developer_mode()) {
if($fewbricks_save_json === true) {
$this->save_json();
} elseif (isset($_GET['dumpfewbricksfields'])) {
$this->print_settings();
} else {
$this->check_keys($this->settings['fields']);
}
}
register_field_group($this->settings);
}
/**
* In order to create unique keys, we prepend teh key of a parent to its kids and so on down the field settings tree.
* @param $fields
* @param $base_key
* @return mixed
*/
private function set_unique_keys($fields, $base_key)
{
foreach ($fields as $field_key => $field_settings) {
// Store the original key to ease debugging
$fields[$field_key]['original_key'] = $fields[$field_key]['key'];
// Start off new key with base key
$new_key = $base_key;
// Lets add the brick key if we have one. This will only be set if
// the field is part of a brick (which is a concept added by Fewbricks and of which ACF has no idea)
if(isset($field_settings['brick_key'])) {
$new_key .= '_' . $field_settings['brick_key'];
}
// Lets store the new key we have built so far for future needs
$new_base_key = $new_key;
// Add the current fields key to complete the new key
$new_key .= '_' . $fields[$field_key]['key'];
// Give the field the brand new key
$fields[$field_key]['key'] = $new_key;
// Lets keep the array key of any item that we should traverse down.
$field_settings_rabbit_hole_array_key = $this->get_rabbit_hole_array_key($field_settings);
// Do we have a key to go down?
if($field_settings_rabbit_hole_array_key !== false) {
// Recursion!
$fields[$field_key][$field_settings_rabbit_hole_array_key] =
$this->set_unique_keys($field_settings[$field_settings_rabbit_hole_array_key], $new_key);
}
// Lets fix the conditional logic
if(isset($field_settings['conditional_logic']) && !empty($field_settings['conditional_logic'])) {
foreach($field_settings['conditional_logic'] AS $lvl_1_key => $lvl_1_value) {
foreach($field_settings['conditional_logic'][$lvl_1_key] AS $lvl_2_key => $lvl_2_value) {
$fields[$field_key]['conditional_logic'][$lvl_1_key][$lvl_2_key]['field'] =
$new_base_key . '_' .
$field_settings['conditional_logic'][$lvl_1_key][$lvl_2_key]['field'];
}
}
}
}
return $fields;
}
/**
* @param array $field_settings
* @return bool|string
*/
private function get_rabbit_hole_array_key($field_settings)
{
// Lets keep the array key of any item that we should traverse down.
$field_settings_rabbit_hole_array_key = false;
if (isset($field_settings['fields']) && is_array($field_settings['fields'])) {
$field_settings_rabbit_hole_array_key = 'fields';
} elseif (isset($field_settings['sub_fields']) && is_array($field_settings['sub_fields'])) {
$field_settings_rabbit_hole_array_key = 'sub_fields';
} elseif (isset($field_settings['layouts']) && is_array($field_settings['layouts'])) {
$field_settings_rabbit_hole_array_key = 'layouts';
}
return $field_settings_rabbit_hole_array_key;
}
/**
* Checks for duplicate keys. This functions hould not be called in a production environment since
* it will cause wp_die if a duplicate key is found and will also slow down performance by looping lots
* of multidimensional arrays.
* @param $fields
*/
private function check_keys($fields)
{
global $debug_keys;
$error_string = false;
$name_of_looped_field = false;
foreach ($fields as $array_key => $value) {
if (!$error_string && is_array($value)) {
// If we have not already run into an error and we are dealting with an array
$this->check_keys($value);
} elseif ($array_key === 'key') {
if (array_search($value, $debug_keys) !== false) {
// If key already exists
$error_string = 'The key <b>' . $value . '</b> already exists.';
} elseif (substr($value, 0, 1) == '_' || substr($value, -1) == '_') {
// If the key starts or ends with an underscore
$error_string = 'A key must not start or end with an underscore. You have probably forgotten to set a key somewhere. Key with errors: <b>' . $value . '</b>';
} else {
$debug_keys[] = $value;
}
} elseif ($array_key === 'name') {
$name_of_looped_field = $value;
}
if ($error_string !== false && $name_of_looped_field !== false) {
// If we have an error and have found the name
$die_string = 'Message from Fewbricks: ';
$die_string .= 'Fatal error when adding field group "' . $this->settings['title'] . '". ';
$die_string .= $error_string . ' ';
$die_string .= 'Please use another key for field "' . $name_of_looped_field . '"';
wp_die($die_string);
}
}
}
/**
* This function hides and/or removes items on the edit screen for where the field group is present.
* Note that hide and showare mutually exclusive. If you pass anything but false to hide, show will be ignored.
* Pass (false, []) to apply the values in show.
* @param bool|array $names_of_items_to_hide
* @param bool|array $names_of_items_to_show
* @return array
*/
private function get_hide_on_screen_settings($names_of_items_to_hide = false, $names_of_items_to_show = false)
{
$default_items_to_hide = [
0 => 'the_content',
1 => 'excerpt',
2 => 'custom_fields',
3 => 'discussion',
4 => 'comments',
5 => 'revisions',
6 => 'slug',
7 => 'author',
8 => 'format',
//9 => 'page_attributes', // Since page attributes are so often shown
10 => 'featured_image',
11 => 'categories',
12 => 'tags',
13 => 'send-trackbacks',
];
if ($names_of_items_to_hide !== false) {
foreach ($default_items_to_hide AS $default_item_key => $default_item_name) {
if (!in_array($default_item_name, $names_of_items_to_hide)) {
unset($default_items_to_hide[$default_item_key]);
}
}
} elseif (is_array($names_of_items_to_show)) {
foreach ($names_of_items_to_show AS $name_of_item_to_show) {
// Find the key of the item in settings having the value of the key we want to show...
if (false !== ($item_key = array_search($name_of_item_to_show, $default_items_to_hide))) {
// ...and remove that item from our settings array.
unset($default_items_to_hide[$item_key]);
}
}
}
return $default_items_to_hide;
}
/**
* Write settings to json file
*/
private function save_json()
{
acf_write_json_field_group($this->settings);
}
/**
*
*/
public function print_settings()
{
echo '<h3>Field group: ' . $this->settings['title'] . '</h3>';
echo '<pre>';
print_r($this->settings);
echo '</pre>';
}
}