summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaniel Weipert <code@drogueronin.de>2020-12-16 19:12:18 +0100
committerDaniel Weipert <code@drogueronin.de>2020-12-16 19:12:18 +0100
commitd6fabbd1e2396e9fb68e7cc8a4ea23417cc7d763 (patch)
tree975d883c853461c2102465ace8cb64938237878f /src
Initial commitv1.0.0
Diffstat (limited to 'src')
-rw-r--r--src/Metabox.php137
-rw-r--r--src/MetaboxField.php121
2 files changed, 258 insertions, 0 deletions
diff --git a/src/Metabox.php b/src/Metabox.php
new file mode 100644
index 0000000..0f4c5d7
--- /dev/null
+++ b/src/Metabox.php
@@ -0,0 +1,137 @@
+<?php
+
+namespace PostTypes;
+
+/**
+ * A Metabox in ACF is called a "Field Group" or just "Group".
+ * So all instances of "Metabox" or "Box" can be understood as an "ACF Field Group"
+ *
+ * @see https://github.com/AdvancedCustomFields/acf
+ */
+class Metabox
+{
+ /**
+ * The Box's key
+ */
+ public string $key;
+
+ /**
+ * The Box's title
+ * they $key is derived from that
+ */
+ public string $title;
+
+ /**
+ * The PostType/s to add the Metabox to
+ */
+ public array $posttypes = [];
+
+ /**
+ * The Box's fields to add
+ *
+ * @var MetaboxField[]
+ */
+ public array $fields = [];
+
+ /**
+ * The "ACF Group Settings"
+ * @see https://www.advancedcustomfields.com/resources/register-fields-via-php/#group-settings
+ */
+ public array $options = [];
+
+ /**
+ * Metabox constructor.
+ *
+ * @param string $title
+ * @param array $options
+ */
+ public function __construct(string $title, array $options = [])
+ {
+ // @see https://github.com/AdvancedCustomFields/acf/blob/5.9.3/includes/local-fields.php#L163
+ $this->key = 'group_' . str_replace(['_', '-', '/', ' '], '_', strtolower($title));
+ $this->title = $title;
+
+ $this->options($options);
+ }
+
+ /**
+ * Assign a PostType to add the Metabox to
+ *
+ * @param string|array $posttypes
+ *
+ * @return $this
+ */
+ public function posttype($posttypes)
+ {
+ $posttypes = (array)$posttypes;
+
+ foreach ($posttypes as $posttype) {
+ $this->posttypes[$posttype] = $posttype;
+ }
+
+ return $this;
+ }
+
+ /**
+ * @param MetaboxField|array $fields
+ *
+ * @return $this
+ */
+ public function field($fields)
+ {
+ if ($fields instanceof MetaboxField) {
+ $fields = [$fields];
+ }
+
+ foreach ($fields as $field) {
+ // set the field's parent
+ $field->parent($this->key);
+
+ $this->fields[$field->key] = &$field;
+ }
+
+ return $this;
+ }
+
+ /**
+ * @param array $options
+ *
+ * @return $this
+ */
+ public function options(array $options)
+ {
+ $this->options = $options;
+
+ return $this;
+ }
+
+ /**
+ * Add the acf local field group
+ */
+ public function add()
+ {
+ // build the rule groups for the location
+ $postTypeRuleGroups = array_map(function ($posttype) {
+ return [[
+ 'param' => 'post_type',
+ 'operator' => '==',
+ 'value' => $posttype,
+ ]];
+ }, $this->posttypes);
+
+ // merge with extra options
+ $fieldGroup = array_merge_recursive([
+ 'key' => $this->key,
+ 'title' => $this->title,
+ 'location' => $postTypeRuleGroups,
+ ], $this->options);
+
+ // add field group
+ acf_add_local_field_group($fieldGroup);
+
+ // add fields
+ foreach ($this->fields as $field) {
+ $field->add();
+ }
+ }
+}
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 @@
+<?php
+
+namespace PostTypes;
+
+class MetaboxField
+{
+ /**
+ * The field's key
+ */
+ public string $key;
+
+ /**
+ * The field's names
+ * 'label' is the title
+ * 'name' is the meta_key
+ * The $key is derived from the label
+ */
+ public array $names;
+
+ /**
+ * The field's type
+ * Can be one of the following
+ * @see https://www.advancedcustomfields.com/resources/register-fields-via-php/#field-type%20settings
+ */
+ public string $type;
+
+ /**
+ * The Metabox's $key
+ */
+ public string $parent;
+
+ /**
+ * The "ACF Field Settings"
+ * @see https://www.advancedcustomfields.com/resources/register-fields-via-php/#field-settings
+ * There are extra settings available based on the field's type
+ * @see https://www.advancedcustomfields.com/resources/register-fields-via-php/#field-type%20settings
+ */
+ public array $options = [];
+
+ /**
+ * MetaboxField constructor.
+ *
+ * @param string|array $names
+ * @param array $options
+ */
+ public function __construct($names, array $options = [])
+ {
+ // only the name is passed
+ if (is_string($names)) {
+ $names = [
+ // create a human friendly name
+ 'label' => 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);
+ }
+}