From 57ea78b47aac277cbc7b28688bf0cb57da062c0b Mon Sep 17 00:00:00 2001 From: Johannes Date: Sat, 6 Sep 2025 20:03:47 +0200 Subject: [PATCH 1/3] Allow localization of options --- src/Console/OptionsMakeCommand.php | 5 +++ src/Console/StubPublishCommand.php | 1 + src/Console/stubs/options.localized.stub | 40 ++++++++++++++++++++++++ src/Options.php | 32 ++++++++++++++----- 4 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 src/Console/stubs/options.localized.stub diff --git a/src/Console/OptionsMakeCommand.php b/src/Console/OptionsMakeCommand.php index c56043a1..315b48c7 100644 --- a/src/Console/OptionsMakeCommand.php +++ b/src/Console/OptionsMakeCommand.php @@ -11,6 +11,7 @@ class OptionsMakeCommand extends MakeCommand */ protected $signature = 'acf:options {name* : The name of the options page} {--full : Scaffold an options page that contains the complete configuration.} + {--localize : Localize the options page name and title} {--force : Overwrite any existing files}'; /** @@ -45,6 +46,10 @@ protected function getStub() return $this->resolveStub('options.full'); } + if ($this->option('localize')) { + return $this->resolveStub('options.localized'); + } + return $this->resolveStub('options'); } } diff --git a/src/Console/StubPublishCommand.php b/src/Console/StubPublishCommand.php index f252ad65..33c6bfe4 100644 --- a/src/Console/StubPublishCommand.php +++ b/src/Console/StubPublishCommand.php @@ -33,6 +33,7 @@ class StubPublishCommand extends Command 'block.localized.stub', 'block.stub', 'field.stub', + 'options.localized.stub', 'options.full.stub', 'options.stub', 'partial.stub', diff --git a/src/Console/stubs/options.localized.stub b/src/Console/stubs/options.localized.stub new file mode 100644 index 00000000..0df5d7f1 --- /dev/null +++ b/src/Console/stubs/options.localized.stub @@ -0,0 +1,40 @@ +addRepeater('items') + ->addText('item') + ->endRepeater(); + + return $fields->build(); + } +} diff --git a/src/Options.php b/src/Options.php index 394049f9..2afd7fd2 100644 --- a/src/Options.php +++ b/src/Options.php @@ -91,6 +91,22 @@ abstract class Options extends Composer */ public $settings = []; + /** + * Retrieve the option page menu name. + */ + public function getName(): string + { + return $this->name; + } + + /** + * Retrieve the option page document title. + */ + public function getTitle(): string + { + return $this->title; + } + /** * Localized text displayed on the submit button. * @@ -118,16 +134,16 @@ public function updatedMessage() */ public function compose() { - if (empty($this->name)) { - return; - } + if (blank($this->getName())) { + return null; + } if (empty($this->slug)) { - $this->slug = Str::slug($this->name); + $this->slug = Str::slug($this->getName()); } - if (empty($this->title)) { - $this->title = $this->name; + if (blank($this->getTitle())) { + $this->title = $this->getName(); } if (! Arr::has($this->fields, 'location.0.0')) { @@ -145,9 +161,9 @@ public function compose() acf_add_options_page( array_merge([ - 'menu_title' => $this->name, + 'menu_title' => $this->getName(), 'menu_slug' => $this->slug, - 'page_title' => $this->title, + 'page_title' => $this->getTitle(), 'capability' => $this->capability, 'position' => $this->position, 'parent_slug' => $this->parent, From 357e8352edf59b6c3fd11b8c22a06f7d4bb6f955 Mon Sep 17 00:00:00 2001 From: Johannes Date: Sat, 6 Sep 2025 20:04:22 +0200 Subject: [PATCH 2/3] Add a reminder to change the option position value in case multiple option pages are used --- src/Console/stubs/options.full.stub | 2 +- src/Console/stubs/options.localized.stub | 7 +++++++ src/Console/stubs/options.stub | 7 +++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/Console/stubs/options.full.stub b/src/Console/stubs/options.full.stub index 9c9b860d..33bf45c0 100644 --- a/src/Console/stubs/options.full.stub +++ b/src/Console/stubs/options.full.stub @@ -40,7 +40,7 @@ class DummyClass extends Field * * @var int */ - public $position = PHP_INT_MAX; + public $position = PHP_INT_MAX; // TODO: change to another value if you have multiple option pages. /** * The option page visibility in the admin menu. diff --git a/src/Console/stubs/options.localized.stub b/src/Console/stubs/options.localized.stub index 0df5d7f1..7bca3011 100644 --- a/src/Console/stubs/options.localized.stub +++ b/src/Console/stubs/options.localized.stub @@ -23,6 +23,13 @@ class DummyClass extends Field return __('DummyTitle', 'sage'); } + /** + * The option page menu position. + * + * @var int + */ + public $position = PHP_INT_MAX; // TODO: change to another value if you have multiple option pages. + /** * The option page field group. */ diff --git a/src/Console/stubs/options.stub b/src/Console/stubs/options.stub index e80f53cb..119a54cd 100644 --- a/src/Console/stubs/options.stub +++ b/src/Console/stubs/options.stub @@ -21,6 +21,13 @@ class DummyClass extends Field */ public $title = 'DummyTitle | Options'; + /** + * The option page menu position. + * + * @var int + */ + public $position = PHP_INT_MAX; // TODO: change to another value if you have multiple option pages. + /** * The option page field group. */ From f0ed95ff9e24830e25a6fb2527f83787e876cce2 Mon Sep 17 00:00:00 2001 From: Johannes Date: Thu, 18 Sep 2025 19:23:19 +0200 Subject: [PATCH 3/3] Remove the TODO comment from the stub files --- src/Console/stubs/options.full.stub | 2 +- src/Console/stubs/options.localized.stub | 2 +- src/Console/stubs/options.stub | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Console/stubs/options.full.stub b/src/Console/stubs/options.full.stub index 33bf45c0..9c9b860d 100644 --- a/src/Console/stubs/options.full.stub +++ b/src/Console/stubs/options.full.stub @@ -40,7 +40,7 @@ class DummyClass extends Field * * @var int */ - public $position = PHP_INT_MAX; // TODO: change to another value if you have multiple option pages. + public $position = PHP_INT_MAX; /** * The option page visibility in the admin menu. diff --git a/src/Console/stubs/options.localized.stub b/src/Console/stubs/options.localized.stub index 7bca3011..15bb8537 100644 --- a/src/Console/stubs/options.localized.stub +++ b/src/Console/stubs/options.localized.stub @@ -28,7 +28,7 @@ class DummyClass extends Field * * @var int */ - public $position = PHP_INT_MAX; // TODO: change to another value if you have multiple option pages. + public $position = PHP_INT_MAX; /** * The option page field group. diff --git a/src/Console/stubs/options.stub b/src/Console/stubs/options.stub index 119a54cd..215e98a7 100644 --- a/src/Console/stubs/options.stub +++ b/src/Console/stubs/options.stub @@ -26,7 +26,7 @@ class DummyClass extends Field * * @var int */ - public $position = PHP_INT_MAX; // TODO: change to another value if you have multiple option pages. + public $position = PHP_INT_MAX; /** * The option page field group.