From de23701d8c4a4d9823a6785b9158ef8876bfb6c7 Mon Sep 17 00:00:00 2001 From: Brandon Date: Mon, 12 Aug 2024 06:09:33 -0500 Subject: [PATCH 1/4] =?UTF-8?q?=F0=9F=8E=A8=20Make=20generated=20stub=20fi?= =?UTF-8?q?elds=20variable=20generic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Console/stubs/block.construct.stub | 6 +++--- src/Console/stubs/block.stub | 6 +++--- src/Console/stubs/field.stub | 8 ++++---- src/Console/stubs/options.full.stub | 6 +++--- src/Console/stubs/options.stub | 6 +++--- src/Console/stubs/partial.stub | 6 +++--- src/Console/stubs/widget.stub | 8 ++++---- 7 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/Console/stubs/block.construct.stub b/src/Console/stubs/block.construct.stub index 0f77b761..1a22fd62 100644 --- a/src/Console/stubs/block.construct.stub +++ b/src/Console/stubs/block.construct.stub @@ -78,14 +78,14 @@ class DummyClass extends Block */ public function fields(): array { - $DummyCamel = Builder::make('DummySnake'); + $fields = Builder::make('DummySnake'); - $DummyCamel + $fields ->addRepeater('items') ->addText('item') ->endRepeater(); - return $DummyCamel->build(); + return $fields->build(); } /** diff --git a/src/Console/stubs/block.stub b/src/Console/stubs/block.stub index 38d96c4f..4c4002c9 100644 --- a/src/Console/stubs/block.stub +++ b/src/Console/stubs/block.stub @@ -157,14 +157,14 @@ class DummyClass extends Block */ public function fields(): array { - $DummyCamel = Builder::make('DummySnake'); + $fields = Builder::make('DummySnake'); - $DummyCamel + $fields ->addRepeater('items') ->addText('item') ->endRepeater(); - return $DummyCamel->build(); + return $fields->build(); } /** diff --git a/src/Console/stubs/field.stub b/src/Console/stubs/field.stub index ccea053b..6c793ecf 100644 --- a/src/Console/stubs/field.stub +++ b/src/Console/stubs/field.stub @@ -12,16 +12,16 @@ class DummyClass extends Field */ public function fields(): array { - $DummyCamel = Builder::make('DummySnake'); + $fields = Builder::make('DummySnake'); - $DummyCamel + $fields ->setLocation('post_type', '==', 'post'); - $DummyCamel + $fields ->addRepeater('items') ->addText('item') ->endRepeater(); - return $DummyCamel->build(); + return $fields->build(); } } diff --git a/src/Console/stubs/options.full.stub b/src/Console/stubs/options.full.stub index fe5bc96e..9c9b860d 100644 --- a/src/Console/stubs/options.full.stub +++ b/src/Console/stubs/options.full.stub @@ -112,13 +112,13 @@ class DummyClass extends Field */ public function fields(): array { - $DummyCamel = Builder::make('DummySnake'); + $fields = Builder::make('DummySnake'); - $DummyCamel + $fields ->addRepeater('items') ->addText('item') ->endRepeater(); - return $DummyCamel->build(); + return $fields->build(); } } diff --git a/src/Console/stubs/options.stub b/src/Console/stubs/options.stub index da5d0141..e80f53cb 100644 --- a/src/Console/stubs/options.stub +++ b/src/Console/stubs/options.stub @@ -26,13 +26,13 @@ class DummyClass extends Field */ public function fields(): array { - $DummyCamel = Builder::make('DummySnake'); + $fields = Builder::make('DummySnake'); - $DummyCamel + $fields ->addRepeater('items') ->addText('item') ->endRepeater(); - return $DummyCamel->build(); + return $fields->build(); } } diff --git a/src/Console/stubs/partial.stub b/src/Console/stubs/partial.stub index 70e4ab81..8b999e30 100644 --- a/src/Console/stubs/partial.stub +++ b/src/Console/stubs/partial.stub @@ -12,13 +12,13 @@ class DummyClass extends Partial */ public function fields(): Builder { - $DummyCamel = Builder::make('DummySnake'); + $fields = Builder::make('DummySnake'); - $DummyCamel + $fields ->addRepeater('items') ->addText('item') ->endRepeater(); - return $DummyCamel; + return $fields; } } diff --git a/src/Console/stubs/widget.stub b/src/Console/stubs/widget.stub index 261304ad..947c83f4 100644 --- a/src/Console/stubs/widget.stub +++ b/src/Console/stubs/widget.stub @@ -44,17 +44,17 @@ class DummyClass extends Widget */ public function fields(): array { - $DummyCamel = Builder::make('DummySnake'); + $fields = Builder::make('DummySnake'); - $DummyCamel + $fields ->addText('title'); - $DummyCamel + $fields ->addRepeater('items') ->addText('item') ->endRepeater(); - return $DummyCamel->build(); + return $fields->build(); } /** From e167053ba1117ecc96b21eef86897d6d6f8e81d0 Mon Sep 17 00:00:00 2001 From: Brandon Date: Mon, 12 Aug 2024 06:11:45 -0500 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=A7=91=E2=80=8D=F0=9F=92=BB=20Throw?= =?UTF-8?q?=20an=20exception=20if=20field=20group=20keys=20are=20not=20uni?= =?UTF-8?q?que=20(Fixes=20#273)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AcfComposer.php | 25 ++++++++++++++++++++++++ src/Exceptions/DuplicateKeyException.php | 10 ++++++++++ 2 files changed, 35 insertions(+) create mode 100644 src/Exceptions/DuplicateKeyException.php diff --git a/src/AcfComposer.php b/src/AcfComposer.php index 237f92b9..9b1a212e 100644 --- a/src/AcfComposer.php +++ b/src/AcfComposer.php @@ -4,6 +4,7 @@ use Illuminate\Support\Facades\File; use Illuminate\Support\Str; +use Log1x\AcfComposer\Exceptions\DuplicateKeyException; use ReflectionClass; use Roots\Acorn\Application; use Symfony\Component\Finder\Finder; @@ -150,6 +151,30 @@ public function handleComposers(): void $this->deferredOptions = []; $this->pendingComposers = []; + + foreach ($this->composers as $namespace => $composers) { + $names = []; + + foreach ($composers as $composer) { + $group = $composer->getFields(); + + $key = $group['key'] ?? $group[0]['key'] ?? null; + + if (! $key) { + continue; + } + + if (isset($names[$key])) { + $class = $composer::class; + + throw new DuplicateKeyException("Duplicate ACF field group key [{$key}] found in [{$class}] and [{$names[$key]}]."); + } + + $names[$key] = $composer::class; + } + + $this->composers[$namespace] = array_values($composers); + } } /** diff --git a/src/Exceptions/DuplicateKeyException.php b/src/Exceptions/DuplicateKeyException.php new file mode 100644 index 00000000..7e6585a2 --- /dev/null +++ b/src/Exceptions/DuplicateKeyException.php @@ -0,0 +1,10 @@ + Date: Mon, 12 Aug 2024 06:13:40 -0500 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=A7=91=E2=80=8D=F0=9F=92=BB=20Throw?= =?UTF-8?q?=20a=20`InvalidFieldsException`=20when=20fields=20are=20invalid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Composer.php | 4 ++-- src/Exceptions/InvalidFieldsException.php | 15 +++++++++++++++ src/Field.php | 4 ++-- 3 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 src/Exceptions/InvalidFieldsException.php diff --git a/src/Composer.php b/src/Composer.php index a7135df7..c1ae495f 100644 --- a/src/Composer.php +++ b/src/Composer.php @@ -2,12 +2,12 @@ namespace Log1x\AcfComposer; -use Exception; use Illuminate\Support\Collection; use Illuminate\Support\Str; use Log1x\AcfComposer\Concerns\InteractsWithPartial; use Log1x\AcfComposer\Contracts\Composer as ComposerContract; use Log1x\AcfComposer\Contracts\Field as FieldContract; +use Log1x\AcfComposer\Exceptions\InvalidFieldsException; use Roots\Acorn\Application; use StoutLogic\AcfBuilder\FieldsBuilder; @@ -129,7 +129,7 @@ public function getFields(bool $cache = true): array : $fields; if (! is_array($fields)) { - throw new Exception('Fields must be an array or an instance of Builder.'); + throw new InvalidFieldsException; } if ($this->defaults->has('field_group')) { diff --git a/src/Exceptions/InvalidFieldsException.php b/src/Exceptions/InvalidFieldsException.php new file mode 100644 index 00000000..f449272e --- /dev/null +++ b/src/Exceptions/InvalidFieldsException.php @@ -0,0 +1,15 @@ + Date: Mon, 12 Aug 2024 06:14:07 -0500 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=93=9D=20Improve=20the=20README=20exa?= =?UTF-8?q?mples?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 72 +++++++++++++++++++++++++++---------------------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 98c5f245..d20eb4d3 100644 --- a/README.md +++ b/README.md @@ -47,12 +47,12 @@ $ wp acorn vendor:publish --tag="acf-composer" To create your first field group, start by running the following generator command from your theme directory: ```bash -$ wp acorn acf:field Example +$ wp acorn acf:field ExampleField ``` -This will create `src/Fields/Example.php` which is where you will create and manage your first field group. +This will create `src/Fields/ExampleField.php` which is where you will create and manage your first field group. -Taking a glance at the generated `Example.php` stub, you will notice that it has a simple list configured. +Taking a glance at the generated `ExampleField.php` stub, you will notice that it has a simple list configured. ```php setLocation('post_type', '==', 'post'); - $example + $fields ->addRepeater('items') ->addText('item') ->endRepeater(); - return $example->build(); + return $fields->build(); } } ``` @@ -115,14 +115,14 @@ class ListItems extends Partial */ public function fields() { - $listItems = Builder::make('listItems'); + $fields = Builder::make('listItems'); - $listItems + $fields ->addRepeater('items') ->addText('item') ->endRepeater(); - return $listItems; + return $fields; } } ``` @@ -151,15 +151,15 @@ class Example extends Field */ public function fields() { - $example = Builder::make('example'); + $fields = Builder::make('example'); - $example + $fields ->setLocation('post_type', '==', 'post'); - $example + $fields ->addPartial(ListItems::class); - return $example->build(); + return $fields->build(); } } ``` @@ -171,7 +171,7 @@ Generating a block is generally the same as generating a field as seen above. Start by creating the block field using Acorn: ```bash -$ wp acorn acf:block Example +$ wp acorn acf:block ExampleBlock ``` ```php @@ -182,14 +182,14 @@ namespace App\Blocks; use Log1x\AcfComposer\Block; use Log1x\AcfComposer\Builder; -class Example extends Block +class ExampleBlock extends Block { /** * The block name. * * @var string */ - public $name = 'Example'; + public $name = 'Example Block'; /** * The block description. @@ -231,14 +231,14 @@ class Example extends Block */ public function fields() { - $example = Builder::make('example'); + $fields = Builder::make('example_block'); - $example + $fields ->addRepeater('items') ->addText('item') ->endRepeater(); - return $example->build(); + return $fields->build(); } /** @@ -297,7 +297,7 @@ Creating a sidebar widget using ACF Composer is extremely easy. Widgets are auto Start by creating a widget using Acorn: ```bash -$ wp acorn acf:widget Example +$ wp acorn acf:widget ExampleWidget ``` ```php @@ -308,14 +308,14 @@ namespace App\Widgets; use Log1x\AcfComposer\Builder; use Log1x\AcfComposer\Widget; -class Example extends Widget +class ExampleWidget extends Widget { /** * The widget name. * * @var string */ - public $name = 'Example'; + public $name = 'Example Widget'; /** * The widget description. @@ -352,17 +352,17 @@ class Example extends Widget */ public function fields() { - $example = Builder::make('example'); + $fields = Builder::make('example_widget'); - $example + $fields ->addText('title'); - $example + $fields ->addRepeater('items') ->addText('item') ->endRepeater(); - return $example->build(); + return $fields->build(); } /** @@ -400,7 +400,7 @@ Creating an options page is similar to creating a regular field group in additio Start by creating an option page using Acorn: ```bash -$ wp acorn acf:options Example +$ wp acorn acf:options ExampleOptions ``` ```php @@ -411,21 +411,21 @@ namespace App\Options; use Log1x\AcfComposer\Builder; use Log1x\AcfComposer\Options as Field; -class Example extends Field +class ExampleOptions extends Field { /** * The option page menu name. * * @var string */ - public $name = 'Example'; + public $name = 'Example Options'; /** * The option page document title. * * @var string */ - public $title = 'Example | Options'; + public $title = 'Example Options | Options'; /** * The option page field group. @@ -434,14 +434,14 @@ class Example extends Field */ public function fields() { - $example = Builder::make('example'); + $fields = Builder::make('example_options'); - $example + $fields ->addRepeater('items') ->addText('item') ->endRepeater(); - return $example->build(); + return $fields->build(); } } ``` @@ -491,7 +491,7 @@ The publish command generates all available stubs by default. However, each stub ## Default Field Settings -One of my personal favorite features of ACF Composer is the ability to set field type as well as field group defaults. Any globally set default can of course be over-ridden by simply setting it on the individual field. +A useful feature unique to ACF Composer is the ability to set field type as well as field group defaults. Any globally set default can of course be over-ridden by simply setting it on the individual field. ### Global