|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Helper; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon_Fields\Datastore\Datastore; |
|
6
|
|
|
use Carbon_Fields\Exception\Incorrect_Syntax_Exception; |
|
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 $container_id |
|
19
|
|
|
* @param string $field_name Field name |
|
20
|
|
|
* @return mixed |
|
21
|
|
|
*/ |
|
22
|
|
|
public static function get_field_clone( $object_id, $container_type, $container_id, $field_name ) { |
|
23
|
|
|
$repository = \Carbon_Fields\Carbon_Fields::resolve( 'container_repository' ); |
|
24
|
|
|
$field = null; |
|
25
|
|
|
if ( $container_id ) { |
|
26
|
|
|
$field = $repository->get_field_in_container( $field_name, $container_id ); |
|
27
|
|
|
} else { |
|
28
|
|
|
$field = $repository->get_field_in_containers( $field_name, $container_type ); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
if ( ! $field ) { |
|
32
|
|
|
return null; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$clone = clone $field; |
|
36
|
|
|
if ( $object_id !== null ) { |
|
37
|
|
|
$clone->get_datastore()->set_object_id( $object_id ); |
|
38
|
|
|
} |
|
39
|
|
|
return $clone; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Get a value formatted for end-users |
|
44
|
|
|
* |
|
45
|
|
|
* @param int $object_id Object id to get value for (e.g. post_id, term_id etc.) |
|
46
|
|
|
* @param string $container_type Container type to search in |
|
47
|
|
|
* @param string $container_id |
|
48
|
|
|
* @param string $field_name Field name |
|
49
|
|
|
* @return mixed |
|
50
|
|
|
*/ |
|
51
|
|
|
public static function get_value( $object_id, $container_type, $container_id, $field_name ) { |
|
52
|
|
|
$field = static::get_field_clone( $object_id, $container_type, $container_id, $field_name ); |
|
53
|
|
|
|
|
54
|
|
|
if ( ! $field ) { |
|
55
|
|
|
return ''; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$field->load(); |
|
59
|
|
|
return $field->get_formatted_value(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Set value for a field |
|
64
|
|
|
* |
|
65
|
|
|
* @param int $object_id Object id to get value for (e.g. post_id, term_id etc.) |
|
66
|
|
|
* @param string $container_type Container type to search in |
|
67
|
|
|
* @param string $container_id |
|
68
|
|
|
* @param string $field_name Field name |
|
69
|
|
|
* @param array $value Field expects a `value_set`; Complex_Field expects a `value_tree` - refer to DEVELOPMENT.md |
|
70
|
|
|
*/ |
|
71
|
|
|
public static function set_value( $object_id, $container_type, $container_id, $field_name, $value ) { |
|
72
|
|
|
$field = static::get_field_clone( $object_id, $container_type, $container_id, $field_name ); |
|
73
|
|
|
|
|
74
|
|
|
if ( ! $field ) { |
|
75
|
|
|
Incorrect_Syntax_Exception::raise( 'Could not find a field which satisfies the supplied pattern: ' . $field_name ); |
|
76
|
|
|
return; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$field->set_value( $value ); |
|
80
|
|
|
$field->save(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Shorthand for get_post_meta(). |
|
85
|
|
|
* Uses the ID of the current post in the loop. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $name Custom field name. |
|
88
|
|
|
* @param string $container_id |
|
89
|
|
|
* @return mixed Meta value. |
|
90
|
|
|
*/ |
|
91
|
|
|
public static function get_the_post_meta( $name, $container_id = '' ) { |
|
92
|
|
|
return static::get_post_meta( get_the_ID(), $name, $container_id ); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Get post meta field for a post. |
|
97
|
|
|
* |
|
98
|
|
|
* @param int $id Post ID. |
|
99
|
|
|
* @param string $name Custom field name. |
|
100
|
|
|
* @param string $container_id |
|
101
|
|
|
* @return mixed Meta value. |
|
102
|
|
|
*/ |
|
103
|
|
|
public static function get_post_meta( $id, $name, $container_id = '' ) { |
|
104
|
|
|
return static::get_value( $id, 'post_meta', $container_id, $name ); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Set post meta field for a post. |
|
109
|
|
|
* |
|
110
|
|
|
* @param int $id Post ID |
|
111
|
|
|
* @param string $name Custom field name |
|
112
|
|
|
* @param array $value |
|
113
|
|
|
* @param string $container_id |
|
114
|
|
|
*/ |
|
115
|
|
|
public static function set_post_meta( $id, $name, $value, $container_id = '' ) { |
|
116
|
|
|
return static::set_value( $id, 'post_meta', $container_id, $name, $value ); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Get theme option field value. |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $name Custom field name |
|
123
|
|
|
* @param string $container_id |
|
124
|
|
|
* @return mixed Option value |
|
125
|
|
|
*/ |
|
126
|
|
|
public static function get_theme_option( $name, $container_id = '' ) { |
|
127
|
|
|
return static::get_value( null, 'theme_options', $container_id, $name ); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Set theme option field value. |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $name Field name |
|
134
|
|
|
* @param array $value |
|
135
|
|
|
* @param string $container_id |
|
136
|
|
|
*/ |
|
137
|
|
|
public static function set_theme_option( $name, $value, $container_id = '' ) { |
|
138
|
|
|
return static::set_value( null, 'theme_options', $container_id, $name, $value ); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Get term meta field for a term. |
|
143
|
|
|
* |
|
144
|
|
|
* @param int $id Term ID. |
|
145
|
|
|
* @param string $name Custom field name. |
|
146
|
|
|
* @param string $container_id |
|
147
|
|
|
* @return mixed Meta value. |
|
148
|
|
|
*/ |
|
149
|
|
|
public static function get_term_meta( $id, $name, $container_id = '' ) { |
|
150
|
|
|
return static::get_value( $id, 'term_meta', $container_id, $name ); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Set term meta field for a term. |
|
155
|
|
|
* |
|
156
|
|
|
* @param int $id Term ID |
|
157
|
|
|
* @param string $name Field name |
|
158
|
|
|
* @param array $value |
|
159
|
|
|
* @param string $container_id |
|
160
|
|
|
*/ |
|
161
|
|
|
public static function set_term_meta( $id, $name, $value, $container_id = '' ) { |
|
162
|
|
|
return static::set_value( $id, 'term_meta', $container_id, $name, $value ); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Get user meta field for a user. |
|
167
|
|
|
* |
|
168
|
|
|
* @param int $id User ID. |
|
169
|
|
|
* @param string $name Custom field name. |
|
170
|
|
|
* @param string $container_id |
|
171
|
|
|
* @return mixed Meta value. |
|
172
|
|
|
*/ |
|
173
|
|
|
public static function get_user_meta( $id, $name, $container_id = '' ) { |
|
174
|
|
|
return static::get_value( $id, 'user_meta', $container_id, $name ); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Set user meta field for a user. |
|
179
|
|
|
* |
|
180
|
|
|
* @param int $id User ID |
|
181
|
|
|
* @param string $name Field name |
|
182
|
|
|
* @param array $value |
|
183
|
|
|
* @param string $container_id |
|
184
|
|
|
*/ |
|
185
|
|
|
public static function set_user_meta( $id, $name, $value, $container_id = '' ) { |
|
186
|
|
|
return static::set_value( $id, 'user_meta', $container_id, $name, $value ); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Get comment meta field for a comment. |
|
191
|
|
|
* |
|
192
|
|
|
* @param int $id Comment ID. |
|
193
|
|
|
* @param string $name Custom field name. |
|
194
|
|
|
* @param string $container_id |
|
195
|
|
|
* @return mixed Meta value. |
|
196
|
|
|
*/ |
|
197
|
|
|
public static function get_comment_meta( $id, $name, $container_id = '' ) { |
|
198
|
|
|
return static::get_value( $id, 'comment_meta', $container_id, $name ); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Set comment meta field for a comment. |
|
203
|
|
|
* |
|
204
|
|
|
* @param int $id Comment ID |
|
205
|
|
|
* @param string $name Field name |
|
206
|
|
|
* @param array $value |
|
207
|
|
|
* @param string $container_id |
|
208
|
|
|
*/ |
|
209
|
|
|
public static function set_comment_meta( $id, $name, $value, $container_id = '' ) { |
|
210
|
|
|
return static::set_value( $id, 'comment_meta', $container_id, $name, $value ); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Get nav menu item meta field for a nav menu item. |
|
215
|
|
|
* |
|
216
|
|
|
* @param int $id Nav menu item ID. |
|
217
|
|
|
* @param string $name Custom field name. |
|
218
|
|
|
* @param string $container_id |
|
219
|
|
|
* @return mixed Meta value. |
|
220
|
|
|
*/ |
|
221
|
|
|
public static function get_nav_menu_item_meta( $id, $name, $container_id = '' ) { |
|
222
|
|
|
return static::get_value( $id, 'nav_menu_item', $container_id, $name ); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Set nav menu item meta field for a nav menu item. |
|
227
|
|
|
* |
|
228
|
|
|
* @param int $id Nav menu item ID |
|
229
|
|
|
* @param string $name Field name |
|
230
|
|
|
* @param array $value |
|
231
|
|
|
* @param string $container_id |
|
232
|
|
|
*/ |
|
233
|
|
|
public static function set_nav_menu_item_meta( $id, $name, $value, $container_id = '' ) { |
|
234
|
|
|
return static::set_value( $id, 'nav_menu_item', $container_id, $name, $value ); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Recursive sorting function by array key. |
|
239
|
|
|
* |
|
240
|
|
|
* @param array &$array The input array. |
|
241
|
|
|
* @param int $sort_flags Flags for controlling sorting behavior. |
|
242
|
|
|
* @return boolean |
|
243
|
|
|
*/ |
|
244
|
|
|
public static function ksort_recursive( &$array, $sort_flags = SORT_REGULAR ) { |
|
245
|
|
|
if ( ! is_array( $array ) ) { |
|
246
|
|
|
return false; |
|
247
|
|
|
} |
|
248
|
|
|
ksort( $array, $sort_flags ); |
|
249
|
|
|
foreach ( $array as $key => $value ) { |
|
250
|
|
|
self::ksort_recursive( $array[ $key ], $sort_flags ); |
|
251
|
|
|
} |
|
252
|
|
|
return true; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Get the relation type from an array similar to how meta_query works in WP_Query |
|
257
|
|
|
* |
|
258
|
|
|
* @param array $array |
|
259
|
|
|
* @param array<string> $allowed_relations |
|
260
|
|
|
* @param string $relation_key |
|
261
|
|
|
* @return string |
|
262
|
|
|
*/ |
|
263
|
|
|
public static function get_relation_type_from_array( $array, $allowed_relations = array( 'AND', 'OR' ), $relation_key = 'relation' ) { |
|
264
|
|
|
$allowed_relations = array_values( $allowed_relations ); |
|
265
|
|
|
$allowed_relations = array_map( 'strtoupper', $allowed_relations ); |
|
266
|
|
|
$relation = isset( $allowed_relations[0] ) ? $allowed_relations[0] : ''; |
|
267
|
|
|
|
|
268
|
|
|
if ( isset( $array[ $relation_key ] ) ) { |
|
269
|
|
|
$relation = strtoupper( $array[ $relation_key ] ); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
View Code Duplication |
if ( ! in_array( $relation, $allowed_relations ) ) { |
|
273
|
|
|
Incorrect_Syntax_Exception::raise( 'Invalid relation type ' . $relation . '. ' . |
|
274
|
|
|
'The rule should be one of the following: "' . implode( '", "', $allowed_relations ) . '"' ); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
return $relation; |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* Normalize a label by updating case, stripping common prefixes etc. |
|
282
|
|
|
* |
|
283
|
|
|
* @param string $label |
|
284
|
|
|
* @return string |
|
285
|
|
|
*/ |
|
286
|
|
View Code Duplication |
public static function normalize_label( $label ) { |
|
287
|
|
|
// remove the leading underscore(if it's there) |
|
288
|
|
|
$label = preg_replace( '~^_~', '', $label ); |
|
289
|
|
|
|
|
290
|
|
|
// remove the leading "crb_"(if it's there) |
|
291
|
|
|
$label = preg_replace( '~^crb_~', '', $label ); |
|
292
|
|
|
|
|
293
|
|
|
// split the name into words and make them capitalized |
|
294
|
|
|
$label = mb_convert_case( str_replace( '_', ' ', $label ), MB_CASE_TITLE ); |
|
295
|
|
|
|
|
296
|
|
|
return $label; |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* Normalize a type string representing an object type |
|
301
|
|
|
* |
|
302
|
|
|
* @param string $type |
|
303
|
|
|
* @return string |
|
304
|
|
|
*/ |
|
305
|
|
View Code Duplication |
public static function normalize_type( $type ) { |
|
306
|
|
|
$normalized_type = str_replace( ' ', '_', $type ); |
|
307
|
|
|
$normalized_type = preg_replace( '/[_\s]+/', '_', $normalized_type ); |
|
308
|
|
|
$normalized_type = preg_replace( '/^_|_$/', '', $normalized_type ); |
|
309
|
|
|
$normalized_type = strtolower( $normalized_type ); |
|
310
|
|
|
return $normalized_type; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
/** |
|
314
|
|
|
* Convert a string representing an object type to a fully qualified class name |
|
315
|
|
|
* |
|
316
|
|
|
* @param string $type |
|
317
|
|
|
* @param string $namespace |
|
318
|
|
|
* @param string $class_suffix |
|
319
|
|
|
* @return string |
|
320
|
|
|
*/ |
|
321
|
|
|
public static function type_to_class( $type, $namespace = '', $class_suffix = '' ) { |
|
322
|
|
|
$classlike_type = static::normalize_type( $type ); |
|
323
|
|
|
$classlike_type = str_replace( '_', ' ', $classlike_type ); |
|
324
|
|
|
$classlike_type = ucwords( $classlike_type ); |
|
325
|
|
|
$classlike_type = str_replace( ' ', '_', $classlike_type ); |
|
326
|
|
|
|
|
327
|
|
|
$class = $classlike_type . $class_suffix; |
|
328
|
|
|
if ( $namespace ) { |
|
329
|
|
|
$class = $namespace . '\\' . $class; |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
return $class; |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
/** |
|
336
|
|
|
* Convert a string representing an object type to a fully qualified class name |
|
337
|
|
|
* |
|
338
|
|
|
* @param string $class |
|
339
|
|
|
* @param string $class_suffix |
|
340
|
|
|
* @return string |
|
341
|
|
|
*/ |
|
342
|
|
|
public static function class_to_type( $class, $class_suffix = '' ) { |
|
343
|
|
|
$reflection = new \ReflectionClass( $class ); |
|
344
|
|
|
$type = $reflection->getShortName(); |
|
345
|
|
|
|
|
346
|
|
|
if ( $class_suffix ) { |
|
347
|
|
|
$type = preg_replace( '/(' . preg_quote( $class_suffix, '/' ) . ')$/i', '', $type ); |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
$type = static::normalize_type( $type ); |
|
351
|
|
|
|
|
352
|
|
|
return $type; |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
/** |
|
356
|
|
|
* Get an array of sanitized html classes |
|
357
|
|
|
* |
|
358
|
|
|
* @param string|array<string> $classes |
|
359
|
|
|
* @return array<string> |
|
360
|
|
|
*/ |
|
361
|
|
|
public static function sanitize_classes( $classes ) { |
|
362
|
|
|
if ( ! is_array( $classes ) ) { |
|
363
|
|
|
$classes = array_values( array_filter( explode( ' ', $classes ) ) ); |
|
364
|
|
|
} |
|
365
|
|
|
$classes = array_map( 'sanitize_html_class', $classes ); |
|
366
|
|
|
return $classes; |
|
367
|
|
|
} |
|
368
|
|
|
} |
|
369
|
|
|
|