From d6fabbd1e2396e9fb68e7cc8a4ea23417cc7d763 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Wed, 16 Dec 2020 19:12:18 +0100 Subject: Initial commit --- src/MetaboxField.php | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 src/MetaboxField.php (limited to 'src/MetaboxField.php') diff --git a/src/MetaboxField.php b/src/MetaboxField.php new file mode 100644 index 0000000..8112d9b --- /dev/null +++ b/src/MetaboxField.php @@ -0,0 +1,121 @@ + ucwords(strtolower(str_replace(['-', '_'], ' ', $names))), + 'name' => $names, + ]; + } + + // @see https://github.com/AdvancedCustomFields/acf/blob/5.9.3/includes/local-fields.php#L365 + $this->key = 'field_' . $names['name']; + $this->names = $names; + + $this->options($options); + } + + /** + * @param string $type + * + * @return $this + */ + public function type(string $type) + { + $this->type = $type; + + return $this; + } + + /** + * @param string $parent + * + * @return $this + */ + public function parent(string $parent) + { + if (strpos($parent, 'group') !== 0) { + $parent = "group_$parent"; + } + + $this->parent = $parent; + + return $this; + } + + /** + * @param array $options + * + * @return $this + */ + public function options(array $options) + { + $this->options = $options; + + return $this; + } + + /** + * Add the acf local field + */ + public function add() + { + // merge with extra options + $field = array_merge_recursive([ + 'key' => $this->key, + 'label' => $this->names['label'], + 'name' => $this->names['name'], + 'type' => $this->type, + 'parent' => $this->parent, + ], $this->options); + + // add the field + acf_add_local_field($field); + } +} -- cgit v1.2.3