From b2c1c693c79f0ad9432fb1b3442044fdbc230959 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Rei=C3=9Fner?= Date: Fri, 18 Nov 2022 17:42:18 +0100 Subject: [PATCH] fix(field): Properly use `acf_register_field_type()` for registering field type (#62) enhance(field): Format field value in Rest API --- plugin.php | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/plugin.php b/plugin.php index bcf926f..e638ea9 100644 --- a/plugin.php +++ b/plugin.php @@ -4,7 +4,7 @@ * Plugin Name: Advanced Custom Fields: Phone Number * Plugin URI: https://github.com/log1x/acf-phone-number * Description: A real ACF phone number field. - * Version: 1.2.1 + * Version: 1.2.2 * Author: Brandon Nifong * Author URI: https://github.com/log1x */ @@ -45,14 +45,16 @@ public function __invoke() */ protected function register() { - foreach (['acf/include_field_types', 'acf/register_fields'] as $hook) { - add_filter($hook, function () { - return new PhoneNumberField( - plugin_dir_url(__FILE__) . $this->assetPath, - plugin_dir_path(__FILE__) . $this->assetPath - ); - }); - } + $field = new PhoneNumberField( + plugin_dir_url(__FILE__) . $this->assetPath, + plugin_dir_path(__FILE__) . $this->assetPath + ); + + acf_register_field_type($field); + + add_filter("acf/rest/format_value_for_rest/type={$field->name}", function ($formatted, $post, $field, $value) { + return (new PhoneNumber($value))->toArray(); + }, 10, 4); } /**