| 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 |
$fileTypes = [ |
| 24 |
'image', |
| 25 |
'image_upload', |
| 26 |
'file_advanced', |
| 27 |
'file_upload', |
| 28 |
'single_image', |
| 29 |
'file', |
| 30 |
'image_advanced', |
| 31 |
'video', |
| 32 |
]; |
| 33 |
|
| 34 |
$textFields = []; |
| 35 |
|
| 36 |
$fileFields = []; |
| 37 |
|
| 38 |
$metaBoxFields = rwmb_get_object_fields($postType); |
| 39 |
|
| 40 |
foreach ($metaBoxFields as $index => $field) { |
| 41 |
if (in_array($field['type'], $unsupportedTypes)) { |
| 42 |
continue; |
| 43 |
} |
| 44 |
|
| 45 |
if ('group' === $field['type'] && isset($field['fields'])) { |
| 46 |
$getFields = self::setMetaBoxSubFields($field, $textFields, $fileFields, $fileTypes); |
| 47 |
$textFields = $getFields['text_fields']; |
| 48 |
$fileFields = $getFields['file_fields']; |
| 49 |
continue; |
| 50 |
} |
| 51 |
|
| 52 |
if (in_array($field['type'], $fileTypes)) { |
| 53 |
$fileFields[$index]['name'] = $field['name']; |
| 54 |
$fileFields[$index]['key'] = $field['id']; |
| 55 |
$fileFields[$index]['required'] = $field['required']; |
| 56 |
} else { |
| 57 |
$textFields[$index]['name'] = $field['name']; |
| 58 |
$textFields[$index]['key'] = $field['id']; |
| 59 |
$textFields[$index]['required'] = $field['required']; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
return ['text_fields'=> $textFields, 'file_fields'=> $fileFields]; |
| 64 |
} |
| 65 |
|
| 66 |
private static function setMetaBoxSubFields($field, $textFields, $fileFields, $fileTypes) |
| 67 |
{ |
| 68 |
foreach ($field['fields'] as $childIndex => $subField) { |
| 69 |
if (in_array($subField['type'], $fileTypes)) { |
| 70 |
$fileFields[$childIndex]['name'] = $subField['name']; |
| 71 |
$fileFields[$childIndex]['type'] = $subField['type']; |
| 72 |
$fileFields[$childIndex]['key'] = $field['id'] . '.' . $subField['id']; |
| 73 |
$fileFields[$childIndex]['required'] = $subField['required']; |
| 74 |
} else { |
| 75 |
$textFields[$childIndex]['name'] = $field['name'] . '-' . $subField['name']; |
| 76 |
$textFields[$childIndex]['key'] = $field['id'] . '.' . $subField['id']; |
| 77 |
$textFields[$childIndex]['type'] = $subField['type']; |
| 78 |
$textFields[$childIndex]['required'] = $subField['required']; |
| 79 |
} |
| 80 |
} |
| 81 |
return ['text_fields'=>$textFields, 'file_fields'=> $fileFields]; |
| 82 |
} |
| 83 |
} |
| 84 |
|