From 0cf97dc3bf3753a29a7620b534fdcd01c748a498 Mon Sep 17 00:00:00 2001 From: Brandon Date: Mon, 7 Oct 2024 17:50:10 -0500 Subject: [PATCH 1/5] =?UTF-8?q?=E2=9E=95=20Add=20`laravel/prompts`=20to=20?= =?UTF-8?q?the=20project?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 3d3b8076..e1290a64 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,8 @@ }, "require": { "php": "^8.0", - "stoutlogic/acf-builder": "^1.11" + "stoutlogic/acf-builder": "^1.11", + "laravel/prompts": "*" }, "require-dev": { "laravel/pint": "^1.14", From 99df36325dfce7d870545d52614a862cc2a11a63 Mon Sep 17 00:00:00 2001 From: Brandon Date: Mon, 7 Oct 2024 17:51:30 -0500 Subject: [PATCH 2/5] =?UTF-8?q?=E2=9C=A8=20Add=20a=20`acf:usage`=20command?= =?UTF-8?q?=20to=20search/show=20field=20type=20usage=20examples?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resources/views/field-usage.blade.php | 13 ++ src/Console/UsageCommand.php | 121 +++++++++++++++++++ src/Providers/AcfComposerServiceProvider.php | 5 + 3 files changed, 139 insertions(+) create mode 100644 resources/views/field-usage.blade.php create mode 100644 src/Console/UsageCommand.php diff --git a/resources/views/field-usage.blade.php b/resources/views/field-usage.blade.php new file mode 100644 index 00000000..cd416c9a --- /dev/null +++ b/resources/views/field-usage.blade.php @@ -0,0 +1,13 @@ +@props([ + 'name', + 'label', + 'options' => '', + 'native' => null, +]) + +@php($field = $native ? "{$native}('{$label}'" : "addField('{$name}', '{$label}'") + +$fields + ->{!! $field !!}, [ +{!! $options !!} + ]); diff --git a/src/Console/UsageCommand.php b/src/Console/UsageCommand.php new file mode 100644 index 00000000..8d1b506e --- /dev/null +++ b/src/Console/UsageCommand.php @@ -0,0 +1,121 @@ +components->error('Advanced Custom Fields must be installed and activated to use this command.'); + } + + $this->types = collect( + acf_get_field_types() + )->sortBy(fn ($type) => $type->label); + + $field = $this->argument('field') ?: search( + label: 'Search for a registered field type', + options: fn (string $value) => $this->search($value)->all(), + hint: "Found {$this->types->count()} registered field types." + ); + + $type = $this->type($field); + + if (! $type) { + return $this->components->error("The field type [{$field}] could not be found."); + } + + $options = collect([ + ...$type->defaults, + ...$type->supports, + ])->except('escaping_html'); + + table(['Label', 'Name', 'Category', 'Options #'], [[ + $type->label, + $type->name, + Str::title($type->category), + $options->count(), + ]]); + + $method = Str::of($type->name) + ->studly() + ->start('add') + ->toString(); + + $native = method_exists('Log1x\AcfComposer\Builder', $method) ? $method : null; + + $options = collect($options)->map(fn ($value) => match (true) { + is_string($value) => "'{$value}'", + is_array($value) => '[]', + is_bool($value) => $value ? 'true' : 'false', + default => $value, + })->map(fn ($value, $key) => "\t '{$key}' => {$value},"); + + $usage = view('acf-composer::field-usage', [ + 'name' => $type->name, + 'label' => "{$type->label} Example", + 'options' => $options->implode("\n"), + 'native' => $native, + ]); + + $usage = collect(explode("\n", $usage)) + ->map(fn ($line) => rtrim(" {$line}")) + ->filter() + ->implode("\n"); + + $this->line($usage); + } + + /** + * Search for a field type. + */ + protected function search(?string $search = null): Collection + { + if (filled($search)) { + return $this->types + ->filter(fn ($type) => Str::contains($type->label, $search, ignoreCase: true) || Str::contains($type->name, $search, ignoreCase: true)) + ->map(fn ($type) => $type->label) + ->values(); + } + + return $this->types->map(fn ($type) => $type->label); + } + + /** + * Retrieve a field type by label or name. + */ + protected function type(string $field): ?object + { + return $this->types->first(fn ($type) => Str::contains($type->label, $field, ignoreCase: true) || Str::contains($type->name, $field, ignoreCase: true)); + } +} diff --git a/src/Providers/AcfComposerServiceProvider.php b/src/Providers/AcfComposerServiceProvider.php index 5861f087..296ea638 100644 --- a/src/Providers/AcfComposerServiceProvider.php +++ b/src/Providers/AcfComposerServiceProvider.php @@ -18,6 +18,10 @@ class AcfComposerServiceProvider extends ServiceProvider public function register() { $this->app->singleton('AcfComposer', fn () => AcfComposer::make($this->app)); + + if (! defined('PWP_NAME')) { + define('PWP_NAME', 'ACF Composer'); + } } /** @@ -49,6 +53,7 @@ public function boot() Console\PartialMakeCommand::class, Console\StubPublishCommand::class, Console\UpgradeCommand::class, + Console\UsageCommand::class, Console\WidgetMakeCommand::class, ]); From ba0d0bc498e171afe100c3128e74455841f0a79c Mon Sep 17 00:00:00 2001 From: Brandon Date: Mon, 7 Oct 2024 17:51:53 -0500 Subject: [PATCH 3/5] =?UTF-8?q?=F0=9F=93=9D=20Add=20a=20section=20on=20fie?= =?UTF-8?q?ld=20builder=20usage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d20eb4d3..4be6d44a 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,17 @@ class ExampleField extends Field } ``` -Proceed by checking the `Add Post` for the field to ensure things are working as intended – and then [get to work](https://github.com/Log1x/acf-builder-cheatsheet). +Proceed by checking the `Add Post` for the field to ensure things are working as intended. + +### Field Builder Usage + +To assist with development, ACF Composer comes with a `usage` command that will let you search for registered field types. This will provide basic information about the field type as well as a usage example containing all possible options. + +```sh +$ wp acorn acf:usage +``` + +For additional usage, you may consult the [ACF Builder Cheatsheet](https://github.com/Log1x/acf-builder-cheatsheet). ### Generating a Field Partial From 5804594ac3eafb68752c67ef08c2e4159e994b30 Mon Sep 17 00:00:00 2001 From: Brandon Date: Thu, 24 Oct 2024 03:14:57 -0500 Subject: [PATCH 4/5] =?UTF-8?q?=F0=9F=92=84=20Add=20the=20field=20type=20s?= =?UTF-8?q?ource=20to=20the=20usage=20table=20=F0=9F=92=84=20Add=20an=20as?= =?UTF-8?q?terisk=20next=20to=20custom=20field=20types?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Console/UsageCommand.php | 56 ++++++++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/src/Console/UsageCommand.php b/src/Console/UsageCommand.php index 8d1b506e..e0b78a30 100644 --- a/src/Console/UsageCommand.php +++ b/src/Console/UsageCommand.php @@ -39,14 +39,13 @@ public function handle() return $this->components->error('Advanced Custom Fields must be installed and activated to use this command.'); } - $this->types = collect( - acf_get_field_types() - )->sortBy(fn ($type) => $type->label); + $this->types = $this->types(); $field = $this->argument('field') ?: search( - label: 'Search for a registered field type', + label: "Search {$this->types->count()} registered field types", options: fn (string $value) => $this->search($value)->all(), - hint: "Found {$this->types->count()} registered field types." + hint: '* indicates a custom field type', + scroll: 8, ); $type = $this->type($field); @@ -60,11 +59,12 @@ public function handle() ...$type->supports, ])->except('escaping_html'); - table(['Label', 'Name', 'Category', 'Options #'], [[ + table(['Label', 'Name', 'Category', 'Options #', 'Source'], [[ $type->label, $type->name, Str::title($type->category), $options->count(), + $type->source, ]]); $method = Str::of($type->name) @@ -118,4 +118,48 @@ protected function type(string $field): ?object { return $this->types->first(fn ($type) => Str::contains($type->label, $field, ignoreCase: true) || Str::contains($type->name, $field, ignoreCase: true)); } + + /** + * Retrieve all registered field types. + */ + protected function types(): Collection + { + return collect( + acf_get_field_types() + )->map(function ($type) { + if (Str::startsWith($type->doc_url, 'https://www.advancedcustomfields.com')) { + $type->source = 'Official'; + + return $type; + } + + $type->label = "{$type->label} *"; + $type->source = $this->source($type); + + return $type; + })->sortBy('label'); + } + + /** + * Attempt to retrieve the field type source. + */ + protected function source(object $type): string + { + $paths = [WPMU_PLUGIN_DIR, WP_PLUGIN_DIR]; + + $plugin = (new \ReflectionClass($type))->getFileName(); + + if (! Str::contains($plugin, $paths)) { + return 'Unknown'; + } + + $source = Str::of($plugin)->replace($paths, '') + ->trim('/') + ->explode('/') + ->first(); + + return Str::of($source) + ->headline() + ->replace('Acf', 'ACF'); + } } From d4db4254cdda42d2453d74b3cb850fae8a6b8b14 Mon Sep 17 00:00:00 2001 From: Brandon Date: Thu, 24 Oct 2024 07:07:47 -0500 Subject: [PATCH 5/5] =?UTF-8?q?=F0=9F=8E=A8=20Ensure=20the=20options=20are?= =?UTF-8?q?=20arrays?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Console/UsageCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Console/UsageCommand.php b/src/Console/UsageCommand.php index e0b78a30..603a0fd1 100644 --- a/src/Console/UsageCommand.php +++ b/src/Console/UsageCommand.php @@ -55,8 +55,8 @@ public function handle() } $options = collect([ - ...$type->defaults, - ...$type->supports, + ...$type->defaults ?? [], + ...$type->supports ?? [], ])->except('escaping_html'); table(['Label', 'Name', 'Category', 'Options #', 'Source'], [[