| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Util; |
| 4 |
|
| 5 |
class MetaBoxService |
| 6 |
{ |
| 7 |
public static function getMetaBoxFields($postType) |
| 8 |
{ |
| 9 |
$unsupportedTypes = [ |
| 10 |
'file_input', |
| 11 |
'tab', |
| 12 |
'osm', |
| 13 |
'heading', |
| 14 |
'key_value', |
| 15 |
'map', |
| 16 |
'custom_html', |
| 17 |
'background', |
| 18 |
'fieldset_text', |
| 19 |
'taxonomy', |
| 20 |
'taxonomy_advanced', |
| 21 |
]; |
| 22 |
|
| 23 |
$supportedFieldType = [ |
| 24 |
'select', |
| 25 |
'select_advanced', |
| 26 |
'checkbox_list', |
| 27 |
'radio_list' |
| 28 |
]; |
| 29 |
|
| 30 |
$fileTypes = [ |
| 31 |
'image', |
| 32 |
'image_upload', |
| 33 |
'file_advanced', |
| 34 |
'file_upload', |
| 35 |
'single_image', |
| 36 |
'file', |
| 37 |
'image_advanced', |
| 38 |
'video', |
| 39 |
]; |
| 40 |
|
| 41 |
$textFields = []; |
| 42 |
|
| 43 |
$fileFields = []; |
| 44 |
|
| 45 |
$metaBoxFields = rwmb_get_object_fields($postType); |
| 46 |
|
| 47 |
foreach ($metaBoxFields as $index => $field) { |
| 48 |
if (in_array($field['type'], $unsupportedTypes) && !in_array($field['field_type'], $supportedFieldType)) { |
| 49 |
continue; |
| 50 |
} |
| 51 |
|
| 52 |
if ('group' === $field['type'] && isset($field['fields'])) { |
| 53 |
$getFields = self::setMetaBoxSubFields($field, $textFields, $fileFields, $fileTypes); |
| 54 |
$textFields = $getFields['text_fields']; |
| 55 |
$fileFields = $getFields['file_fields']; |
| 56 |
continue; |
| 57 |
} |
| 58 |
|
| 59 |
if (in_array($field['type'], $fileTypes)) { |
| 60 |
$fileFields[$index]['name'] = $field['name']; |
| 61 |
$fileFields[$index]['key'] = $field['id']; |
| 62 |
$fileFields[$index]['required'] = $field['required']; |
| 63 |
} else { |
| 64 |
$textFields[$index]['name'] = $field['name']; |
| 65 |
$textFields[$index]['key'] = $field['id']; |
| 66 |
$textFields[$index]['required'] = $field['required']; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
return ['text_fields'=> $textFields, 'file_fields'=> $fileFields]; |
| 71 |
} |
| 72 |
|
| 73 |
private static function setMetaBoxSubFields($field, $textFields, $fileFields, $fileTypes) |
| 74 |
{ |
| 75 |
foreach ($field['fields'] as $childIndex => $subField) { |
| 76 |
if (in_array($subField['type'], $fileTypes)) { |
| 77 |
$fileFields[$childIndex]['name'] = $subField['name']; |
| 78 |
$fileFields[$childIndex]['type'] = $subField['type']; |
| 79 |
$fileFields[$childIndex]['key'] = $field['id'] . '.' . $subField['id']; |
| 80 |
$fileFields[$childIndex]['required'] = $subField['required']; |
| 81 |
} else { |
| 82 |
$textFields[$childIndex]['name'] = $field['name'] . '-' . $subField['name']; |
| 83 |
$textFields[$childIndex]['key'] = $field['id'] . '.' . $subField['id']; |
| 84 |
$textFields[$childIndex]['type'] = $subField['type']; |
| 85 |
$textFields[$childIndex]['required'] = $subField['required']; |
| 86 |
} |
| 87 |
} |
| 88 |
return ['text_fields'=>$textFields, 'file_fields'=> $fileFields]; |
| 89 |
} |
| 90 |
} |
| 91 |
|