1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Helper; |
4
|
|
|
|
5
|
|
|
use \Carbon_Fields\App; |
6
|
|
|
use \Carbon_Fields\Datastore\Datastore; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Helper functions and main initialization class. |
10
|
|
|
*/ |
11
|
|
|
class Helper { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Get a value formatted for end-users |
15
|
|
|
* |
16
|
|
|
* @param int $object_id Object id to get value for (e.g. post_id, term_id etc.) |
17
|
|
|
* @param string $container_type Container type to search in |
18
|
|
|
* @param string $field_name Field name |
19
|
|
|
* @return mixed |
20
|
|
|
*/ |
21
|
|
|
public static function get_value( $object_id, $container_type, $field_name ) { |
22
|
|
|
$repository = App::resolve( 'container_repository' ); |
23
|
|
|
$field = $repository->get_field_in_containers( $field_name, $container_type, false ); |
24
|
|
|
$default_value = ''; // for consistency - get_post_meta returns an empty string when a meta key does not exist |
25
|
|
|
|
26
|
|
|
if ( ! $field ) { |
27
|
|
|
return $default_value; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$clone = clone $field; |
31
|
|
|
if ( $object_id !== null ) { |
32
|
|
|
$clone->get_datastore()->set_id( $object_id ); |
33
|
|
|
} |
34
|
|
|
$clone->load(); |
35
|
|
|
return $clone->get_formatted_value(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Set value for a field |
40
|
|
|
* |
41
|
|
|
* @param int $object_id Object id to get value for (e.g. post_id, term_id etc.) |
42
|
|
|
* @param string $container_type Container type to search in |
43
|
|
|
* @param string $field_name Field name |
44
|
|
|
* @param array $value Refer to Complex_Field::set_value_tree() in case you wish to update a complex field |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
|
|
public static function set_value( $object_id, $container_type, $field_name, $value ) { |
48
|
|
|
$repository = App::resolve( 'container_repository' ); |
49
|
|
|
$field = $repository->get_field_in_containers( $field_name, $container_type, false ); |
50
|
|
|
|
51
|
|
|
if ( ! $field ) { |
52
|
|
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$clone = clone $field; |
56
|
|
|
if ( $object_id !== null ) { |
57
|
|
|
$clone->get_datastore()->set_id( $object_id ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ( is_a( $clone, '\\Carbon_Fields\\Field\\Complex_Field' ) ) { |
61
|
|
|
$value = ( ! empty( $value ) ) ? $value : array( 'value_set' => array() ); |
62
|
|
|
$clone->set_value_tree( $value ); |
63
|
|
|
$clone->set_value( $value['value_set'] ); |
64
|
|
|
} else { |
65
|
|
|
$clone->set_value( $value ); |
66
|
|
|
} |
67
|
|
|
$clone->save(); |
68
|
|
|
|
69
|
|
|
return true; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Shorthand for get_post_meta(). |
74
|
|
|
* Uses the ID of the current post in the loop. |
75
|
|
|
* |
76
|
|
|
* @param string $name Custom field name. |
77
|
|
|
* @return mixed Meta value. |
78
|
|
|
*/ |
79
|
|
|
public static function get_the_post_meta( $name ) { |
80
|
|
|
return static::get_post_meta( get_the_ID(), $name ); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get post meta field for a post. |
85
|
|
|
* |
86
|
|
|
* @param int $id Post ID. |
87
|
|
|
* @param string $name Custom field name. |
88
|
|
|
* @return mixed Meta value. |
89
|
|
|
*/ |
90
|
|
|
public static function get_post_meta( $id, $name ) { |
91
|
|
|
return static::get_value( $id, 'Post_Meta', $name ); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Set post meta field for a post. |
96
|
|
|
* |
97
|
|
|
* @param int $id Post ID |
98
|
|
|
* @param string $name Custom field name |
99
|
|
|
* @param array $value |
100
|
|
|
* @return bool Success |
101
|
|
|
*/ |
102
|
|
|
public static function set_post_meta( $id, $name, $value ) { |
103
|
|
|
return static::set_value( $id, 'Post_Meta', $name, $value ); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get theme option field value. |
108
|
|
|
* |
109
|
|
|
* @param string $name Custom field name |
110
|
|
|
* @return mixed Option value |
111
|
|
|
*/ |
112
|
|
|
public static function get_theme_option( $name ) { |
113
|
|
|
return static::get_value( null, 'Theme_Options', $name ); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Set theme option field value. |
118
|
|
|
* |
119
|
|
|
* @param string $name Field name |
120
|
|
|
* @param array $value |
121
|
|
|
* @return bool Success |
122
|
|
|
*/ |
123
|
|
|
public static function set_theme_option( $name, $value ) { |
124
|
|
|
return static::set_value( null, 'Theme_Options', $name, $value ); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get term meta field for a term. |
129
|
|
|
* |
130
|
|
|
* @param int $id Term ID. |
131
|
|
|
* @param string $name Custom field name. |
132
|
|
|
* @return mixed Meta value. |
133
|
|
|
*/ |
134
|
|
|
public static function get_term_meta( $id, $name ) { |
135
|
|
|
return static::get_value( $id, 'Term_Meta', $name ); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Set term meta field for a term. |
140
|
|
|
* |
141
|
|
|
* @param int $id Term ID |
142
|
|
|
* @param string $name Field name |
143
|
|
|
* @param array $value |
144
|
|
|
* @return bool Success |
145
|
|
|
*/ |
146
|
|
|
public static function set_term_meta( $id, $name, $value ) { |
147
|
|
|
return static::set_value( $id, 'Term_Meta', $name, $value ); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Get user meta field for a user. |
152
|
|
|
* |
153
|
|
|
* @param int $id User ID. |
154
|
|
|
* @param string $name Custom field name. |
155
|
|
|
* @return mixed Meta value. |
156
|
|
|
*/ |
157
|
|
|
public static function get_user_meta( $id, $name ) { |
158
|
|
|
return static::get_value( $id, 'user_meta', $name ); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Set user meta field for a user. |
163
|
|
|
* |
164
|
|
|
* @param int $id User ID |
165
|
|
|
* @param string $name Field name |
166
|
|
|
* @param array $value |
167
|
|
|
* @return bool Success |
168
|
|
|
*/ |
169
|
|
|
public static function set_user_meta( $id, $name, $value ) { |
170
|
|
|
return static::set_value( $id, 'user_meta', $name, $value ); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Get comment meta field for a comment. |
175
|
|
|
* |
176
|
|
|
* @param int $id Comment ID. |
177
|
|
|
* @param string $name Custom field name. |
178
|
|
|
* @return mixed Meta value. |
179
|
|
|
*/ |
180
|
|
|
public static function get_comment_meta( $id, $name ) { |
181
|
|
|
return static::get_value( $id, 'comment_meta', $name ); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Set comment meta field for a comment. |
186
|
|
|
* |
187
|
|
|
* @param int $id Comment ID |
188
|
|
|
* @param string $name Field name |
189
|
|
|
* @param array $value |
190
|
|
|
* @return bool Success |
191
|
|
|
*/ |
192
|
|
|
public static function set_comment_meta( $id, $name, $value ) { |
193
|
|
|
return static::set_value( $id, 'comment_meta', $name, $value ); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Recursive sorting function by array key. |
198
|
|
|
* |
199
|
|
|
* @param array &$array The input array. |
200
|
|
|
* @param int $sort_flags Flags for controlling sorting behavior. |
201
|
|
|
* @return array Sorted array. |
202
|
|
|
*/ |
203
|
|
|
public static function ksort_recursive( &$array, $sort_flags = SORT_REGULAR ) { |
204
|
|
|
if ( ! is_array( $array ) ) { |
205
|
|
|
return false; |
206
|
|
|
} |
207
|
|
|
ksort( $array, $sort_flags ); |
208
|
|
|
foreach ( $array as $key => $value ) { |
209
|
|
|
self::ksort_recursive( $array[ $key ], $sort_flags ); |
210
|
|
|
} |
211
|
|
|
return true; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|