summaryrefslogtreecommitdiff
path: root/src/MetaboxField.php
blob: 8112d9bd12cbe207da7bb24c5cf2d5d3142fbf49 (plain)
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
<?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);
    }
}