From b190a38f19ed3710f4432b97bdf0b5f1cfac5349 Mon Sep 17 00:00:00 2001 From: Jaakko Lehtonen Date: Wed, 18 Jan 2023 16:20:38 +0200 Subject: [PATCH] Polylang compatibility --- src/Field.php | 8 ++++ src/Field/Common/Translatable.php | 63 +++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 src/Field/Common/Translatable.php diff --git a/src/Field.php b/src/Field.php index 756e998..32c50dd 100644 --- a/src/Field.php +++ b/src/Field.php @@ -5,10 +5,18 @@ namespace Geniem\ACF; +use Geniem\ACF\Field\Common\Translatable; + /** * Class Field */ abstract class Field { + + /** + * Import the translatable functionalities + */ + use Translatable; + /** * Field label. * diff --git a/src/Field/Common/Translatable.php b/src/Field/Common/Translatable.php new file mode 100644 index 0000000..44573ff --- /dev/null +++ b/src/Field/Common/Translatable.php @@ -0,0 +1,63 @@ +type, 'category' ); + if ( 'layout' === $category ) { + throw new \Geniem\ACF\Exception( self::class . ': set_translations() can\'t be called on this field type.' ); + } + + $choices = [ 'ignore', 'copy_once', 'sync' ]; + switch ( $this->type ) { + case 'text': + case 'textarea': + case 'wysiwyg': + case 'oembed': + case 'url': + case 'email': + $choices[] = 'translate'; + break; + } + + if ( ! \in_array( $translations, $choices ) ) { + throw new \Geniem\ACF\Exception( self::class . ': set_translations() does not accept argument "' . $translations . '"' ); + } + + $this->translations = $translations; + + return $this; + } + + /** + * Get the field translations value + * + * @return string + */ + public function get_translations() { + return $this->translations; + } +}