1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Datastore; |
4
|
|
|
|
5
|
|
|
use Carbon_Fields\Field\Field; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* User meta datastore class. |
9
|
|
|
*/ |
10
|
|
|
class User_Meta_Datastore extends Meta_Datastore { |
11
|
|
|
/** |
12
|
|
|
* ID of the user. |
13
|
|
|
* |
14
|
|
|
* @var int |
15
|
|
|
*/ |
16
|
|
|
protected $user_id; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Load complex field value(s) from the database. |
20
|
|
|
* |
21
|
|
|
* @param mixed $field The field to load values for. |
22
|
|
|
*/ |
23
|
|
|
public function load_values( $field ) { |
24
|
|
|
$results = parent::load_values( $field ); |
25
|
|
|
|
26
|
|
|
if ( ! $results && is_object( $field ) ) { |
27
|
|
|
$tmp_field = clone $field; |
28
|
|
|
$tmp_field->set_value_from_input(); |
29
|
|
|
|
30
|
|
|
$values = $tmp_field->get_values(); |
31
|
|
|
|
32
|
|
|
foreach ( $values as $single_value ) { |
33
|
|
|
foreach ( $single_value as $value_field ) { |
34
|
|
|
$results[] = array( |
35
|
|
|
'field_key' => $value_field->get_name(), |
36
|
|
|
'field_value' => $value_field->get_value() |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $results; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Retrieve the type of meta data. |
47
|
|
|
* |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
public function get_meta_type() { |
51
|
|
|
return 'user'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Retrieve the meta table name to query. |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function get_table_name() { |
60
|
|
|
global $wpdb; |
61
|
|
|
return $wpdb->usermeta; |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Retrieve the meta table field name to query by. |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function get_table_field_name() { |
70
|
|
|
return 'user_id'; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set the user ID of the datastore. |
75
|
|
|
* |
76
|
|
|
* @param int $user_id ID of the user. |
77
|
|
|
*/ |
78
|
|
|
public function set_id( $user_id ) { |
79
|
|
|
$this->user_id = $user_id; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Retrieve the user ID of the datastore. |
84
|
|
|
* |
85
|
|
|
* @return int ID of the user. |
86
|
|
|
*/ |
87
|
|
|
public function get_id() { |
88
|
|
|
return $this->user_id; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|