ClonePrefixedField.php
1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace ACA\ACF\Configurable;
use ACA\ACF\CloneColumnFactory;
use ACA\ACF\Configurable;
use ACA\ACF\FieldFactory;
final class ClonePrefixedField implements Configurable {
/**
* @var FieldFactory
*/
private $field_factory;
public function __construct( FieldFactory $field_factory ) {
$this->field_factory = $field_factory;
}
private function remove_prefix( $column_type ) {
return str_replace( CloneColumnFactory::CLONE_PREFIX, '', $column_type );
}
private function get_clone_hash( $column_type ) {
$column_type = $this->remove_prefix( $column_type );
$key_parts = explode( '_', $column_type );
return sprintf( '%s_%s', $key_parts[0], $key_parts[1] );
}
private function get_field_hash( $column_type ) {
$column_type = $this->remove_prefix( $column_type );
$key_parts = explode( '_', $column_type );
return sprintf( '%s_%s', $key_parts[2], $key_parts[3] );
}
public function create( $column_type ) {
$clone_hash = $this->get_clone_hash( $column_type );
$field_hash = $this->get_field_hash( $column_type );
$clone_settings = acf_get_field( $clone_hash );
$field_settings = acf_get_field( $field_hash );
if ( ! $clone_settings || ! $field_settings ) {
return null;
}
$clone_field = $this->field_factory->create( $clone_settings );
$field = $this->field_factory->create( $field_settings );
$meta_key = sprintf( '%s_%s', $clone_field->get_meta_key(), $field->get_meta_key() );
return [
self::FIELD => $field,
self::FIELD_TYPE => $field->get_type(),
self::META_KEY => $meta_key,
self::FIELD_HASH => $field_hash,
];
}
}