├── LICENSE
├── composer.json
└── src
├── ConditionalLogic.php
├── Fields
├── Accordion.php
├── ButtonGroup.php
├── Checkbox.php
├── ColorPicker.php
├── DatePicker.php
├── DateTimePicker.php
├── Email.php
├── Field.php
├── File.php
├── FlexibleContent.php
├── Gallery.php
├── GoogleMap.php
├── Group.php
├── IconPicker.php
├── Image.php
├── Layout.php
├── Link.php
├── Message.php
├── Number.php
├── Oembed.php
├── PageLink.php
├── Password.php
├── PostObject.php
├── RadioButton.php
├── Range.php
├── Relationship.php
├── Repeater.php
├── Select.php
├── Settings
│ ├── Affixable.php
│ ├── Bidirectional.php
│ ├── ButtonLabel.php
│ ├── Choices.php
│ ├── ConditionalLogic.php
│ ├── DateTimeFormat.php
│ ├── DefaultValue.php
│ ├── Dimensions.php
│ ├── DirectionLayout.php
│ ├── Disabled.php
│ ├── Endpoint.php
│ ├── Fields.php
│ ├── FileSize.php
│ ├── FileTypes.php
│ ├── FilterBy.php
│ ├── Height.php
│ ├── HelperText.php
│ ├── Immutable.php
│ ├── Layout.php
│ ├── Library.php
│ ├── MaxLength.php
│ ├── MinMax.php
│ ├── Multiple.php
│ ├── NewLines.php
│ ├── Nullable.php
│ ├── Placeholder.php
│ ├── PreviewSize.php
│ ├── Required.php
│ ├── Step.php
│ ├── WeekDay.php
│ └── Wrapper.php
├── Tab.php
├── Taxonomy.php
├── Text.php
├── Textarea.php
├── TimePicker.php
├── TrueFalse.php
├── URL.php
├── User.php
└── WYSIWYGEditor.php
├── Key.php
├── Location.php
└── helpers.php
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) Vincent Klaiber
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6 |
7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8 |
9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "vinkla/extended-acf",
3 | "description": "Register advanced custom fields with object-oriented PHP",
4 | "license": "MIT",
5 | "keywords": [
6 | "acf",
7 | "advanced",
8 | "custom",
9 | "extended",
10 | "fields",
11 | "wordpress"
12 | ],
13 | "authors": [
14 | {
15 | "name": "Vincent Klaiber",
16 | "homepage": "https://github.com/vinkla"
17 | }
18 | ],
19 | "require": {
20 | "php": "^8.2"
21 | },
22 | "require-dev": {
23 | "phpunit/phpunit": "^11.0 || ^12.0",
24 | "symfony/var-dumper": "^7.0"
25 | },
26 | "suggest": {
27 | "symfony/var-dumper": "Required to use the dump method (^7.0)."
28 | },
29 | "minimum-stability": "dev",
30 | "prefer-stable": true,
31 | "autoload": {
32 | "psr-4": {
33 | "Extended\\ACF\\": "src/"
34 | },
35 | "files": [
36 | "src/helpers.php"
37 | ]
38 | },
39 | "autoload-dev": {
40 | "psr-4": {
41 | "Extended\\ACF\\Tests\\": "tests/"
42 | },
43 | "files": [
44 | "tests/helpers.php"
45 | ]
46 | },
47 | "extra": {
48 | "branch-alias": {
49 | "dev-master": "14.4-dev"
50 | }
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/ConditionalLogic.php:
--------------------------------------------------------------------------------
1 | rules[] = [
30 | 'name' => $name,
31 | 'operator' => $operator,
32 | 'value' => $value,
33 | 'group' => $group,
34 | 'key' => $key,
35 | ];
36 | }
37 |
38 | /**
39 | * @param string $operator `==` is equal to, `!=` is not equal to, `>` is greater than, `<` is less than, `==pattern` matches pattern, `==contains` contains value, `==empty` has no value, `!=empty` has any value
40 | * @throws \InvalidArgumentException
41 | */
42 | public static function where(string $name, string $operator, mixed $value = null, ?string $group = null, ?string $key = null): static
43 | {
44 | $allowedOperators = [
45 | '>',
46 | '<',
47 | '==',
48 | '!=',
49 | '==pattern',
50 | '==contains',
51 | '==empty',
52 | '!=empty',
53 | ];
54 |
55 | if (!in_array($operator, $allowedOperators)) {
56 | throw new InvalidArgumentException("Invalid conditional logic operator [$operator].");
57 | }
58 |
59 | return new self($name, $operator, $value, $group, $key);
60 | }
61 |
62 | public function and(string|array $name, string $operator, mixed $value = null, ?string $group = null, ?string $key = null): static
63 | {
64 | $this->rules[] = [
65 | 'name' => $name,
66 | 'operator' => $operator,
67 | 'value' => $value,
68 | 'group' => $group,
69 | 'field' => $key,
70 | ];
71 |
72 | return $this;
73 | }
74 |
75 | /** @internal */
76 | public function get(string|null $parentKey = null): array
77 | {
78 | return array_map(function ($rule) use ($parentKey) {
79 | $parentKey = $rule['group'] ?: $parentKey;
80 |
81 | $resolvedParentKey = Key::resolveParentKey($parentKey, Key::sanitize($rule['name']));
82 | $key = $resolvedParentKey . '_' . Key::sanitize($rule['name']);
83 |
84 | $newRule = [
85 | 'field' => $rule['key'] ?? 'field_' . Key::hash($key),
86 | 'operator' => $rule['operator'],
87 | ];
88 |
89 | if ($rule['value']) {
90 | $newRule['value'] = $rule['value'];
91 | }
92 |
93 | return $newRule;
94 | }, $this->rules);
95 | }
96 | }
97 |
--------------------------------------------------------------------------------
/src/Fields/Accordion.php:
--------------------------------------------------------------------------------
1 | settings['multi_expand'] = true;
29 |
30 | return $this;
31 | }
32 |
33 | public function open(): static
34 | {
35 | $this->settings['open'] = true;
36 |
37 | return $this;
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/Fields/ButtonGroup.php:
--------------------------------------------------------------------------------
1 | settings['return_format'] = $format;
48 |
49 | return $this;
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/Fields/Checkbox.php:
--------------------------------------------------------------------------------
1 | settings['return_format'] = $format;
50 |
51 | return $this;
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/src/Fields/ColorPicker.php:
--------------------------------------------------------------------------------
1 | settings['enable_opacity'] = true;
36 |
37 | return $this;
38 | }
39 |
40 | /**
41 | * @param string $format array, string
42 | * @throws \InvalidArgumentException
43 | */
44 | public function format(string $format): static
45 | {
46 | if (!in_array($format, ['array', 'string'])) {
47 | throw new InvalidArgumentException("Invalid argument format [$format].");
48 | }
49 |
50 | $this->settings['return_format'] = $format;
51 |
52 | return $this;
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/Fields/DatePicker.php:
--------------------------------------------------------------------------------
1 | settings = [
28 | 'label' => $label,
29 | 'name' => $name ?? Key::sanitize($label),
30 | ];
31 | }
32 |
33 | public static function make(string $label, string|null $name = null): static
34 | {
35 | return new static($label, $name);
36 | }
37 |
38 | /** @throws \InvalidArgumentException */
39 | public function withSettings(array $settings): static
40 | {
41 | $invalidKeys = [
42 | 'collapsed',
43 | 'conditional_logic',
44 | 'key',
45 | 'label',
46 | 'layouts',
47 | 'name',
48 | 'sub_fields',
49 | 'type',
50 | ];
51 |
52 | foreach ($invalidKeys as $key) {
53 | if (array_key_exists($key, $settings)) {
54 | throw new InvalidArgumentException("Invalid settings key [$key].");
55 | }
56 | }
57 |
58 | $this->settings = array_merge($this->settings, $settings);
59 |
60 | return $this;
61 | }
62 |
63 | public function dump(...$args): static
64 | {
65 | $settings = $this->cloneRecursively()->get();
66 |
67 | dump($settings, ...$args);
68 |
69 | return $this;
70 | }
71 |
72 | public function dd(...$args): never
73 | {
74 | dd($this->get(), ...$args);
75 | }
76 |
77 | /** @internal */
78 | private function cloneRecursively(): static
79 | {
80 | $clone = clone $this;
81 |
82 | if (isset($this->settings['sub_fields'])) {
83 | $clone->settings['sub_fields'] = array_map(
84 | fn(Field $field) => $field->cloneRecursively(),
85 | $this->settings['sub_fields'],
86 | );
87 | }
88 |
89 | return $clone;
90 | }
91 |
92 | /**
93 | * Avoid using custom field keys unless you thoroughly understand them. The
94 | * field keys are automatically generated when you use the
95 | * `register_extended_field_group` function.
96 | * @throws \InvalidArgumentException
97 | */
98 | public function key(string $key): static
99 | {
100 | if (!str_starts_with($key, $this->keyPrefix . '_')) {
101 | throw new InvalidArgumentException(
102 | sprintf('The key should have the prefix [%s_].', $this->keyPrefix),
103 | );
104 | }
105 |
106 | if (Key::has($key)) {
107 | throw new InvalidArgumentException("The key [$key] is not unique.");
108 | }
109 |
110 | $this->settings['key'] = $key;
111 |
112 | Key::set($key, $key);
113 |
114 | return $this;
115 | }
116 |
117 | /** @internal */
118 | public function get(string|null $parentKey = null): array
119 | {
120 | $key =
121 | $this->settings['key'] ??
122 | $parentKey . '_' . Key::sanitize($this->settings['name']);
123 |
124 | if ($this->type !== null) {
125 | $this->settings['type'] = $this->type;
126 | }
127 |
128 | if (isset($this->settings['conditional_logic'])) {
129 | $this->settings['conditional_logic'] = array_map(
130 | fn($rules) => $rules->get($parentKey),
131 | $this->settings['conditional_logic'],
132 | );
133 | }
134 |
135 | if (isset($this->settings['layouts'])) {
136 | $this->settings['layouts'] = array_map(
137 | fn($layout) => $layout->get($key),
138 | $this->settings['layouts'],
139 | );
140 | }
141 |
142 | if (isset($this->settings['sub_fields'])) {
143 | $this->settings['sub_fields'] = array_map(
144 | fn($field) => $field->get($key),
145 | $this->settings['sub_fields'],
146 | );
147 | }
148 |
149 | if (isset($this->settings['collapsed'], $this->settings['sub_fields'])) {
150 | foreach ($this->settings['sub_fields'] as $field) {
151 | if ($field['name'] === $this->settings['collapsed']) {
152 | $this->settings['collapsed'] = $field['key'];
153 | break;
154 | }
155 | }
156 | }
157 |
158 | $this->settings['key'] ??= Key::generate($key, $this->keyPrefix);
159 |
160 | return $this->settings;
161 | }
162 | }
163 |
--------------------------------------------------------------------------------
/src/Fields/File.php:
--------------------------------------------------------------------------------
1 | settings['return_format'] = $format;
48 |
49 | return $this;
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/Fields/FlexibleContent.php:
--------------------------------------------------------------------------------
1 | settings['layouts'] = $layouts;
35 |
36 | return $this;
37 | }
38 |
39 | public function maxLayouts(int $count): static
40 | {
41 | $this->settings['max'] = $count;
42 |
43 | return $this;
44 | }
45 |
46 | public function minLayouts(int $count): static
47 | {
48 | $this->settings['min'] = $count;
49 |
50 | return $this;
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/Fields/Gallery.php:
--------------------------------------------------------------------------------
1 | settings['return_format'] = $format;
52 |
53 | return $this;
54 | }
55 |
56 | public function maxFiles(int $count): static
57 | {
58 | $this->settings['max'] = $count;
59 |
60 | return $this;
61 | }
62 |
63 | public function minFiles(int $count): static
64 | {
65 | $this->settings['min'] = $count;
66 |
67 | return $this;
68 | }
69 |
70 | public function prependFiles(): static
71 | {
72 | $this->settings['insert'] = 'prepend';
73 |
74 | return $this;
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/src/Fields/GoogleMap.php:
--------------------------------------------------------------------------------
1 | settings['center_lat'] = $latitude;
35 | $this->settings['center_lng'] = $longitude;
36 |
37 | return $this;
38 | }
39 |
40 | public function zoom(int $zoom): static
41 | {
42 | $this->settings['zoom'] = $zoom;
43 |
44 | return $this;
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/Fields/Group.php:
--------------------------------------------------------------------------------
1 | settings['return_format'] = $format;
44 |
45 | return $this;
46 | }
47 |
48 | /**
49 | * @param array $tabs dashicons, media_library, url
50 | * @throws \InvalidArgumentException
51 | */
52 | public function tabs(array $tabs): static
53 | {
54 | foreach ($tabs as $tab) {
55 | if (!in_array($tab, ['dashicons', 'media_library', 'url'])) {
56 | throw new InvalidArgumentException("Invalid argument tab [$tab].");
57 | }
58 | }
59 |
60 | $this->settings['tabs'] = $tabs;
61 |
62 | return $this;
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/src/Fields/Image.php:
--------------------------------------------------------------------------------
1 | settings['return_format'] = $format;
52 |
53 | return $this;
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/src/Fields/Layout.php:
--------------------------------------------------------------------------------
1 | settings['display'] = $layout;
36 |
37 | return $this;
38 | }
39 |
40 | public function maxInstances(int $count): static
41 | {
42 | $this->settings['max'] = $count;
43 |
44 | return $this;
45 | }
46 |
47 | public function minInstances(int $count): static
48 | {
49 | $this->settings['min'] = $count;
50 |
51 | return $this;
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/src/Fields/Link.php:
--------------------------------------------------------------------------------
1 | settings['return_format'] = $format;
42 |
43 | return $this;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/Fields/Message.php:
--------------------------------------------------------------------------------
1 | text
29 | $text = preg_replace('/\*\*(.*?)\*\*|__(.*?)__/', '$1$2', $text);
30 |
31 | // Replace strong formatting: **text** or __text__ => text
32 | $text = preg_replace('/\*(.*?)\*|_(.*?)_/', '$1$2', $text);
33 |
34 | // Replace formatting: `code` => code
35 | $text = preg_replace('/\`(.*?)\`/', '$1
', $text);
36 |
37 | // Replace link formatting: [text](url) => text
38 | $text = preg_replace('/\[(.*?)\]\((.*?)\)/', '$1', $text);
39 |
40 | $this->settings['message'] = $text;
41 |
42 | return $this;
43 | }
44 |
45 | public function escapeHtml(): static
46 | {
47 | $this->settings['esc_html'] = true;
48 |
49 | return $this;
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/Fields/Number.php:
--------------------------------------------------------------------------------
1 | settings['width'] = $width;
35 |
36 | return $this;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/Fields/PageLink.php:
--------------------------------------------------------------------------------
1 | settings['allow_archives'] = false;
39 |
40 | return $this;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/Fields/Password.php:
--------------------------------------------------------------------------------
1 | settings['return_format'] = $format;
50 |
51 | return $this;
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/src/Fields/RadioButton.php:
--------------------------------------------------------------------------------
1 | settings['return_format'] = $format;
52 |
53 | return $this;
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/src/Fields/Range.php:
--------------------------------------------------------------------------------
1 | settings['elements'] = $elements;
38 |
39 | return $this;
40 | }
41 |
42 | public function filters(array $filters): static
43 | {
44 | $this->settings['filters'] = $filters;
45 |
46 | return $this;
47 | }
48 |
49 | /**
50 | * @param string $format id, object
51 | * @throws \InvalidArgumentException
52 | */
53 | public function format(string $format): static
54 | {
55 | if (!in_array($format, ['id', 'object'])) {
56 | throw new InvalidArgumentException("Invalid argument format [$format].");
57 | }
58 |
59 | $this->settings['return_format'] = $format;
60 |
61 | return $this;
62 | }
63 |
64 | public function maxPosts(int $count): static
65 | {
66 | $this->settings['max'] = $count;
67 |
68 | return $this;
69 | }
70 |
71 | public function minPosts(int $count): static
72 | {
73 | $this->settings['min'] = $count;
74 |
75 | return $this;
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/src/Fields/Repeater.php:
--------------------------------------------------------------------------------
1 | settings['collapsed'] = $name;
39 |
40 | return $this;
41 | }
42 |
43 | public function paginated(int $perPage = 20): static
44 | {
45 | $this->settings['pagination'] = true;
46 | $this->settings['rows_per_page'] = $perPage;
47 |
48 | return $this;
49 | }
50 |
51 | public function maxRows(int $count): static
52 | {
53 | $this->settings['max'] = $count;
54 |
55 | return $this;
56 | }
57 |
58 | public function minRows(int $count): static
59 | {
60 | $this->settings['min'] = $count;
61 |
62 | return $this;
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/src/Fields/Select.php:
--------------------------------------------------------------------------------
1 | settings['ui'] = true;
46 |
47 | return $this;
48 | }
49 |
50 | public function lazyLoad(): static
51 | {
52 | $this->settings['ui'] = true;
53 | $this->settings['ajax'] = true;
54 |
55 | return $this;
56 | }
57 |
58 | public function create(): static
59 | {
60 | $this->settings['ui'] = true;
61 | $this->settings['multiple'] = true;
62 | $this->settings['create_options'] = true;
63 |
64 | return $this;
65 | }
66 |
67 | public function save(): static
68 | {
69 | $this->settings['ui'] = true;
70 | $this->settings['multiple'] = true;
71 | $this->settings['create_options'] = true;
72 | $this->settings['save_options'] = true;
73 |
74 | return $this;
75 | }
76 |
77 | /**
78 | * @param string $format array, label, value
79 | * @throws \InvalidArgumentException
80 | */
81 | public function format(string $format): static
82 | {
83 | if (!in_array($format, ['array', 'label', 'value'])) {
84 | throw new InvalidArgumentException("Invalid argument format [$format].");
85 | }
86 |
87 | $this->settings['return_format'] = $format;
88 |
89 | return $this;
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/src/Fields/Settings/Affixable.php:
--------------------------------------------------------------------------------
1 | settings['prepend'] = $value;
21 |
22 | return $this;
23 | }
24 |
25 | public function suffix(string $value): static
26 | {
27 | $this->settings['append'] = $value;
28 |
29 | return $this;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/Fields/Settings/Bidirectional.php:
--------------------------------------------------------------------------------
1 | settings['bidirectional'] = 1;
26 | $this->settings['bidirectional_target'] = is_array($keys) ? $keys : [$keys];
27 |
28 | return $this;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/src/Fields/Settings/ButtonLabel.php:
--------------------------------------------------------------------------------
1 | settings['button_label'] = $label;
21 |
22 | return $this;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/Fields/Settings/Choices.php:
--------------------------------------------------------------------------------
1 | 'Forest Green', 'sky_blue' => 'Sky Blue']`.
22 | */
23 | public function choices(array $choices): static
24 | {
25 | if (array_is_list($choices)) {
26 | $choices = array_combine(array_map(fn($key) => Key::sanitize($key), $choices), $choices);
27 | }
28 |
29 | $this->settings['choices'] = $choices;
30 |
31 | return $this;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/Fields/Settings/ConditionalLogic.php:
--------------------------------------------------------------------------------
1 | settings['conditional_logic'] = array_merge(
21 | $this->settings['conditional_logic'] ?? [],
22 | $rules,
23 | );
24 |
25 | return $this;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/Fields/Settings/DateTimeFormat.php:
--------------------------------------------------------------------------------
1 | settings['display_format'] = $format;
22 |
23 | return $this;
24 | }
25 |
26 | /** @see https://wordpress.org/support/article/formatting-date-and-time/ */
27 | public function format(string $format): static
28 | {
29 | $this->settings['return_format'] = $format;
30 |
31 | return $this;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/Fields/Settings/DefaultValue.php:
--------------------------------------------------------------------------------
1 | settings['default_value'] = $value;
21 |
22 | return $this;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/Fields/Settings/Dimensions.php:
--------------------------------------------------------------------------------
1 | settings['max_height'] = $max;
21 |
22 | return $this;
23 | }
24 |
25 | public function minHeight(int $min): static
26 | {
27 | $this->settings['min_height'] = $min;
28 |
29 | return $this;
30 | }
31 |
32 | public function maxWidth(int $max): static
33 | {
34 | $this->settings['max_width'] = $max;
35 |
36 | return $this;
37 | }
38 |
39 | public function minWidth(int $min): static
40 | {
41 | $this->settings['min_width'] = $min;
42 |
43 | return $this;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/Fields/Settings/DirectionLayout.php:
--------------------------------------------------------------------------------
1 | settings['layout'] = $layout;
31 |
32 | return $this;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/Fields/Settings/Disabled.php:
--------------------------------------------------------------------------------
1 | settings['disabled'] = true;
21 |
22 | return $this;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/Fields/Settings/Endpoint.php:
--------------------------------------------------------------------------------
1 | settings['endpoint'] = true;
21 |
22 | return $this;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/Fields/Settings/Fields.php:
--------------------------------------------------------------------------------
1 | settings['sub_fields'] = $fields;
21 |
22 | return $this;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/Fields/Settings/FileSize.php:
--------------------------------------------------------------------------------
1 | settings['min_size'] = $size;
21 |
22 | return $this;
23 | }
24 |
25 | public function maxSize(int|string $size): static
26 | {
27 | $this->settings['max_size'] = $size;
28 |
29 | return $this;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/Fields/Settings/FileTypes.php:
--------------------------------------------------------------------------------
1 | settings['mime_types'] = implode(',', $types);
21 |
22 | return $this;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/Fields/Settings/FilterBy.php:
--------------------------------------------------------------------------------
1 | 0) {
27 | throw new InvalidArgumentException('Invalid argument post status.');
28 | }
29 |
30 | $this->settings['post_status'] = $postStatus;
31 |
32 | return $this;
33 | }
34 |
35 | public function postTypes(array $postTypes): static
36 | {
37 | $this->settings['post_type'] = $postTypes;
38 |
39 | return $this;
40 | }
41 |
42 | public function taxonomies(array $taxonomies): static
43 | {
44 | $this->settings['taxonomy'] = $taxonomies;
45 |
46 | return $this;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/Fields/Settings/Height.php:
--------------------------------------------------------------------------------
1 | settings['height'] = $height;
21 |
22 | return $this;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/Fields/Settings/HelperText.php:
--------------------------------------------------------------------------------
1 | `, ``, ``, and ``.
20 | * @see https://wordpress.com/support/markdown-quick-reference/
21 | */
22 | public function helperText(string $text): static
23 | {
24 | // Replace emphasis formatting: *text* or _text_ => text
25 | $text = preg_replace('/\*\*(.*?)\*\*|__(.*?)__/', '$1$2', $text);
26 |
27 | // Replace strong formatting: **text** or __text__ => text
28 | $text = preg_replace('/\*(.*?)\*|_(.*?)_/', '$1$2', $text);
29 |
30 | // Replace formatting: `code` => code
31 | $text = preg_replace('/\`(.*?)\`/', '$1
', $text);
32 |
33 | // Replace link formatting: [text](url) => text
34 | $text = preg_replace('/\[(.*?)\]\((.*?)\)/', '$1', $text);
35 |
36 | $this->settings['instructions'] = $text;
37 |
38 | return $this;
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/Fields/Settings/Immutable.php:
--------------------------------------------------------------------------------
1 | settings['readonly'] = true;
21 |
22 | return $this;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/Fields/Settings/Layout.php:
--------------------------------------------------------------------------------
1 | settings['layout'] = $layout;
31 |
32 | return $this;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/Fields/Settings/Library.php:
--------------------------------------------------------------------------------
1 | settings['library'] = $library;
31 |
32 | return $this;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/Fields/Settings/MaxLength.php:
--------------------------------------------------------------------------------
1 | settings['maxlength'] = $limit;
21 |
22 | return $this;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/Fields/Settings/MinMax.php:
--------------------------------------------------------------------------------
1 | settings['max'] = $max;
21 |
22 | return $this;
23 | }
24 |
25 | public function min(float $min): static
26 | {
27 | $this->settings['min'] = $min;
28 |
29 | return $this;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/Fields/Settings/Multiple.php:
--------------------------------------------------------------------------------
1 | settings['multiple'] = true;
21 |
22 | return $this;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/Fields/Settings/NewLines.php:
--------------------------------------------------------------------------------
1 | settings['new_lines'] = $newLines;
31 |
32 | return $this;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/Fields/Settings/Nullable.php:
--------------------------------------------------------------------------------
1 | settings['allow_null'] = true;
21 |
22 | return $this;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/Fields/Settings/Placeholder.php:
--------------------------------------------------------------------------------
1 | settings['placeholder'] = $placeholder;
21 |
22 | return $this;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/Fields/Settings/PreviewSize.php:
--------------------------------------------------------------------------------
1 | settings['preview_size'] = $size;
22 |
23 | return $this;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/Fields/Settings/Required.php:
--------------------------------------------------------------------------------
1 | settings['required'] = true;
21 |
22 | return $this;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/Fields/Settings/Step.php:
--------------------------------------------------------------------------------
1 | settings['step'] = $step;
21 |
22 | return $this;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/Fields/Settings/WeekDay.php:
--------------------------------------------------------------------------------
1 | settings['first_day'] = $day;
21 |
22 | return $this;
23 | }
24 |
25 | public function weekStartsOnMonday(): static
26 | {
27 | return $this->firstDayOfWeek(1);
28 | }
29 |
30 | public function weekStartsOnSunday(): static
31 | {
32 | return $this->firstDayOfWeek(0);
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/Fields/Settings/Wrapper.php:
--------------------------------------------------------------------------------
1 | settings['wrapper'] = array_merge(
21 | $this->settings['wrapper'] ?? [],
22 | $wrapper,
23 | );
24 |
25 | return $this;
26 | }
27 |
28 | public function column(int|float $width): static
29 | {
30 | $this->settings['wrapper']['width'] = $width;
31 |
32 | return $this;
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/Fields/Tab.php:
--------------------------------------------------------------------------------
1 | settings['placement'] = $placement;
38 |
39 | return $this;
40 | }
41 |
42 | public function selected(): static
43 | {
44 | $this->settings['selected'] = true;
45 |
46 | return $this;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/Fields/Taxonomy.php:
--------------------------------------------------------------------------------
1 | settings['field_type'] = $type;
46 |
47 | return $this;
48 | }
49 |
50 | /**
51 | * @param string $format id, object
52 | * @throws \InvalidArgumentException
53 | */
54 | public function format(string $format): static
55 | {
56 | if (!in_array($format, ['id', 'object'])) {
57 | throw new InvalidArgumentException("Invalid argument format [$format].");
58 | }
59 |
60 | $this->settings['return_format'] = $format;
61 |
62 | return $this;
63 | }
64 |
65 | public function create(bool $create = true): static
66 | {
67 | $this->settings['add_term'] = $create;
68 |
69 | return $this;
70 | }
71 |
72 | public function load(bool $load = false): static
73 | {
74 | $this->settings['load_terms'] = $load;
75 |
76 | return $this;
77 | }
78 |
79 | public function save(bool $save = false): static
80 | {
81 | $this->settings['save_terms'] = $save;
82 |
83 | return $this;
84 | }
85 |
86 | public function taxonomy(string $taxonomy): static
87 | {
88 | $this->settings['taxonomy'] = $taxonomy;
89 |
90 | return $this;
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/src/Fields/Text.php:
--------------------------------------------------------------------------------
1 | settings['rows'] = $rows;
45 |
46 | return $this;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/Fields/TimePicker.php:
--------------------------------------------------------------------------------
1 | settings['message'] = $text;
35 |
36 | return $this;
37 | }
38 |
39 | public function stylized(?string $on = null, ?string $off = null): static
40 | {
41 | $this->settings['ui'] = true;
42 |
43 | if ($on) {
44 | $this->settings['ui_on_text'] = $on;
45 | }
46 |
47 | if ($off) {
48 | $this->settings['ui_off_text'] = $off;
49 | }
50 |
51 | return $this;
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/src/Fields/URL.php:
--------------------------------------------------------------------------------
1 | settings['return_format'] = $format;
48 |
49 | return $this;
50 | }
51 |
52 | public function roles(array $roles): static
53 | {
54 | $this->settings['role'] = $roles;
55 |
56 | return $this;
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/Fields/WYSIWYGEditor.php:
--------------------------------------------------------------------------------
1 | settings['delay'] = true;
36 |
37 | return $this;
38 | }
39 |
40 | public function disableMediaUpload(): static
41 | {
42 | $this->settings['media_upload'] = false;
43 |
44 | return $this;
45 | }
46 |
47 | /**
48 | * @param string $tabs all, visual, text
49 | * @throws \InvalidArgumentException
50 | */
51 | public function tabs(string $tabs): static
52 | {
53 | if (!in_array($tabs, ['all', 'visual', 'text'])) {
54 | throw new InvalidArgumentException("Invalid argument tabs [$tabs].");
55 | }
56 |
57 | $this->settings['tabs'] = $tabs;
58 |
59 | return $this;
60 | }
61 |
62 | /**
63 | * If `string` is passed, it will be used as the toolbar name. If `array` is passed, it will be used as the toolbar buttons.
64 | * @param string|array $toolbar aligncenter, alignleft, alignright, blockquote, bold, bullist, charmap, forecolor, formatselect, fullscreen, hr, indent, italic, link, numlist, outdent, pastetext, redo, removeformat, spellchecker, strikethrough, underline, undo, wp_adv, wp_help, wp_more
65 | */
66 | public function toolbar(string|array $toolbar): static
67 | {
68 | if (is_string($toolbar)) {
69 | $this->settings['toolbar'] = $toolbar;
70 | }
71 |
72 | if (is_array($toolbar)) {
73 | $this->settings['toolbar'] = implode('_', $toolbar);
74 |
75 | add_filter('acf/fields/wysiwyg/toolbars', function (
76 | array $toolbars,
77 | ) use ($toolbar) {
78 | $toolbars[$this->settings['toolbar']] = [
79 | 1 => $toolbar,
80 | ];
81 |
82 | return $toolbars;
83 | });
84 | }
85 |
86 | return $this;
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/src/Key.php:
--------------------------------------------------------------------------------
1 | 1) {
61 | $potentialParentKey = implode('_', $parentKeyPieces);
62 | $potentialKey = $potentialParentKey . '_' . $key;
63 |
64 | if (array_key_exists($potentialKey, self::$keys)) {
65 | return $potentialParentKey;
66 | }
67 |
68 | array_pop($parentKeyPieces);
69 | }
70 |
71 | return $parentKey;
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/src/Location.php:
--------------------------------------------------------------------------------
1 | rules[] = ['param' => $param, 'operator' => $operator, 'value' => $value];
27 | }
28 |
29 | /**
30 | * @param string $param post_type, post_template, post_status, post_format, post_category, post_taxonomy, post, page_template, page_type, page_parent, page, current_user, current_user_role, user_form, user_role, taxonomy, attachment, comment, widget, nav_menu, nav_menu, nav_menu_item, block, options_page
31 | * @param string $operator `==` is equal to, `!=` is not equal to
32 | */
33 | public static function where(string $param, string $operator, string|null $value = null): static
34 | {
35 | if (func_num_args() === 2) {
36 | $value = $operator;
37 | $operator = '==';
38 | }
39 |
40 | return new self($param, $operator, $value);
41 | }
42 |
43 | /**
44 | * @param string $param post_type, post_template, post_status, post_format, post_category, post_taxonomy, post, page_template, page_type, page_parent, page, current_user, current_user_role, user_form, user_role, taxonomy, attachment, comment, widget, nav_menu, nav_menu, nav_menu_item, block, options_page
45 | * @param string $operator `==` is equal to, `!=` is not equal to
46 | */
47 | public function and(string $param, string $operator, string|null $value = null): static
48 | {
49 | if (func_num_args() === 2) {
50 | $value = $operator;
51 | $operator = '==';
52 | }
53 |
54 | $this->rules[] = ['param' => $param, 'operator' => $operator, 'value' => $value];
55 |
56 | return $this;
57 | }
58 |
59 | /** @internal */
60 | public function get(): array
61 | {
62 | return $this->rules;
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/src/helpers.php:
--------------------------------------------------------------------------------
1 | $field->get($key), $settings['fields']);
37 |
38 | $settings['location'] = array_map(fn($location) => $location->get(), $settings['location']);
39 |
40 | $settings['key'] = Key::generate($key, 'group');
41 |
42 | register_field_group($settings);
43 |
44 | return $settings;
45 | }
46 | }
47 |
--------------------------------------------------------------------------------