Group.php
1.13 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
<?php
namespace ACA\ACF\Editing\Storage;
use ACP;
final class Group implements ACP\Editing\Storage {
/**
* @var string
*/
private $group_key;
/**
* @var string
*/
private $sub_key;
/**
* @var string
*/
private $id_prefix;
/**
* @var ReadStorage
*/
private $read_storage;
public function __construct( $group_key, $sub_key, $id_prefix, ReadStorage $read_storage ) {
$this->group_key = (string) $group_key;
$this->sub_key = (string) $sub_key;
$this->id_prefix = (string) $id_prefix;
$this->read_storage = $read_storage;
}
public function get( int $id ) {
return $this->read_storage->get( $id );
}
public function update( int $id, $data ): bool {
$raw_group_value = get_field( $this->group_key, $this->id_prefix . $id, false );
$group_value = [];
foreach ( $raw_group_value as $field_key => $field_value ) {
$field = acf_get_field( $field_key );
if ( ! $field || ! isset( $field['name'] ) ) {
exit;
}
$group_value[ $field['name'] ] = $field_value;
}
$group_value[ $this->sub_key ] = $data;
return false !== update_field( $this->group_key, $group_value, $this->id_prefix . $id );
}
}