Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polylang compatibility #113

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
63 changes: 63 additions & 0 deletions src/Field/Common/Translatable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* ACF Codifier Translatable trait
*/

namespace Geniem\ACF\Field\Common;

/**
* Translatable trait
*/
trait Translatable {
/**
* The field translations value
*
* @var string
*/
protected $translations;

/**
* Set field translations value
*
* @see https://polylang.pro/doc/working-with-acf-pro/#customize-acf-fields
*
* @throws \Geniem\ACF\Exception Throws error if translations is not valid.
* @param string $translations The translations value to set.
* @return self
*/
public function set_translations( string $translations ) {
$category = \acf_get_field_type_prop( $this->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;
}
}