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 $field_name Field name |
19
|
|
|
* @return mixed |
20
|
|
|
*/ |
21
|
|
View Code Duplication |
public static function get_value( $object_id, $container_type, $field_name ) { |
|
|
|
|
22
|
|
|
$repository = \Carbon_Fields\Carbon_Fields::resolve( 'container_repository' ); |
23
|
|
|
$field = $repository->get_field_in_containers( $field_name, $container_type ); |
24
|
|
|
|
25
|
|
|
if ( ! $field ) { |
26
|
|
|
return ''; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$clone = clone $field; |
30
|
|
|
if ( $object_id !== null ) { |
31
|
|
|
$clone->get_datastore()->set_object_id( $object_id ); |
32
|
|
|
} |
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 Field expects a `value_set`; Complex_Field expects a `value_tree` - refer to DEVELOPMENT.md |
45
|
|
|
*/ |
46
|
|
View Code Duplication |
public static function set_value( $object_id, $container_type, $field_name, $value ) { |
|
|
|
|
47
|
|
|
$repository = \Carbon_Fields\Carbon_Fields::resolve( 'container_repository' ); |
48
|
|
|
$field = $repository->get_field_in_containers( $field_name, $container_type ); |
49
|
|
|
|
50
|
|
|
if ( ! $field ) { |
51
|
|
|
Incorrect_Syntax_Exception::raise( 'Could not find a field which satisfies the supplied pattern: ' . $field_name ); |
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$clone = clone $field; |
56
|
|
|
if ( $object_id !== null ) { |
57
|
|
|
$clone->get_datastore()->set_object_id( $object_id ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$clone->set_value( $value ); |
61
|
|
|
$clone->save(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Shorthand for get_post_meta(). |
66
|
|
|
* Uses the ID of the current post in the loop. |
67
|
|
|
* |
68
|
|
|
* @param string $name Custom field name. |
69
|
|
|
* @return mixed Meta value. |
70
|
|
|
*/ |
71
|
|
|
public static function get_the_post_meta( $name ) { |
72
|
|
|
return static::get_post_meta( get_the_ID(), $name ); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get post meta field for a post. |
77
|
|
|
* |
78
|
|
|
* @param int $id Post ID. |
79
|
|
|
* @param string $name Custom field name. |
80
|
|
|
* @return mixed Meta value. |
81
|
|
|
*/ |
82
|
|
|
public static function get_post_meta( $id, $name ) { |
83
|
|
|
return static::get_value( $id, 'post_meta', $name ); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Set post meta field for a post. |
88
|
|
|
* |
89
|
|
|
* @param int $id Post ID |
90
|
|
|
* @param string $name Custom field name |
91
|
|
|
* @param array $value |
92
|
|
|
* @return bool Success |
93
|
|
|
*/ |
94
|
|
|
public static function set_post_meta( $id, $name, $value ) { |
95
|
|
|
return static::set_value( $id, 'post_meta', $name, $value ); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get theme option field value. |
100
|
|
|
* |
101
|
|
|
* @param string $name Custom field name |
102
|
|
|
* @return mixed Option value |
103
|
|
|
*/ |
104
|
|
|
public static function get_theme_option( $name ) { |
105
|
|
|
return static::get_value( null, 'theme_options', $name ); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Set theme option field value. |
110
|
|
|
* |
111
|
|
|
* @param string $name Field name |
112
|
|
|
* @param array $value |
113
|
|
|
* @return bool Success |
114
|
|
|
*/ |
115
|
|
|
public static function set_theme_option( $name, $value ) { |
116
|
|
|
return static::set_value( null, 'theme_options', $name, $value ); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get term meta field for a term. |
121
|
|
|
* |
122
|
|
|
* @param int $id Term ID. |
123
|
|
|
* @param string $name Custom field name. |
124
|
|
|
* @return mixed Meta value. |
125
|
|
|
*/ |
126
|
|
|
public static function get_term_meta( $id, $name ) { |
127
|
|
|
return static::get_value( $id, 'term_meta', $name ); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Set term meta field for a term. |
132
|
|
|
* |
133
|
|
|
* @param int $id Term ID |
134
|
|
|
* @param string $name Field name |
135
|
|
|
* @param array $value |
136
|
|
|
* @return bool Success |
137
|
|
|
*/ |
138
|
|
|
public static function set_term_meta( $id, $name, $value ) { |
139
|
|
|
return static::set_value( $id, 'term_meta', $name, $value ); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get user meta field for a user. |
144
|
|
|
* |
145
|
|
|
* @param int $id User ID. |
146
|
|
|
* @param string $name Custom field name. |
147
|
|
|
* @return mixed Meta value. |
148
|
|
|
*/ |
149
|
|
|
public static function get_user_meta( $id, $name ) { |
150
|
|
|
return static::get_value( $id, 'user_meta', $name ); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Set user meta field for a user. |
155
|
|
|
* |
156
|
|
|
* @param int $id User ID |
157
|
|
|
* @param string $name Field name |
158
|
|
|
* @param array $value |
159
|
|
|
* @return bool Success |
160
|
|
|
*/ |
161
|
|
|
public static function set_user_meta( $id, $name, $value ) { |
162
|
|
|
return static::set_value( $id, 'user_meta', $name, $value ); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Get comment meta field for a comment. |
167
|
|
|
* |
168
|
|
|
* @param int $id Comment ID. |
169
|
|
|
* @param string $name Custom field name. |
170
|
|
|
* @return mixed Meta value. |
171
|
|
|
*/ |
172
|
|
|
public static function get_comment_meta( $id, $name ) { |
173
|
|
|
return static::get_value( $id, 'comment_meta', $name ); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Set comment meta field for a comment. |
178
|
|
|
* |
179
|
|
|
* @param int $id Comment ID |
180
|
|
|
* @param string $name Field name |
181
|
|
|
* @param array $value |
182
|
|
|
* @return bool Success |
183
|
|
|
*/ |
184
|
|
|
public static function set_comment_meta( $id, $name, $value ) { |
185
|
|
|
return static::set_value( $id, 'comment_meta', $name, $value ); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Recursive sorting function by array key. |
190
|
|
|
* |
191
|
|
|
* @param array &$array The input array. |
192
|
|
|
* @param int $sort_flags Flags for controlling sorting behavior. |
193
|
|
|
* @return array Sorted array. |
194
|
|
|
*/ |
195
|
|
|
public static function ksort_recursive( &$array, $sort_flags = SORT_REGULAR ) { |
196
|
|
|
if ( ! is_array( $array ) ) { |
197
|
|
|
return false; |
198
|
|
|
} |
199
|
|
|
ksort( $array, $sort_flags ); |
200
|
|
|
foreach ( $array as $key => $value ) { |
201
|
|
|
self::ksort_recursive( $array[ $key ], $sort_flags ); |
202
|
|
|
} |
203
|
|
|
return true; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Get the relation type from an array similar to how meta_query works in WP_Query |
208
|
|
|
* |
209
|
|
|
* @param array $array |
210
|
|
|
* @param array<string> $allowed_relations |
211
|
|
|
* @param string $relation_key |
212
|
|
|
* @return string |
213
|
|
|
*/ |
214
|
|
|
public static function get_relation_type_from_array( $array, $allowed_relations = array( 'AND', 'OR' ), $relation_key = 'relation' ) { |
215
|
|
|
$allowed_relations = array_values( $allowed_relations ); |
216
|
|
|
$allowed_relations = array_map( 'strtoupper', $allowed_relations ); |
217
|
|
|
$relation = isset( $allowed_relations[0] ) ? $allowed_relations[0] : ''; |
218
|
|
|
|
219
|
|
|
if ( isset( $array[ $relation_key ] ) ) { |
220
|
|
|
$relation = strtoupper( $array[ $relation_key ] ); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
View Code Duplication |
if ( ! in_array( $relation, $allowed_relations ) ) { |
224
|
|
|
Incorrect_Syntax_Exception::raise( 'Invalid relation type ' . $relation . '. ' . |
225
|
|
|
'The rule should be one of the following: "' . implode( '", "', $allowed_relations ) . '"' ); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
return $relation; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Normalize a label by updating case, stripping common prefixes etc. |
233
|
|
|
* |
234
|
|
|
* @param string $label |
235
|
|
|
* @return string |
236
|
|
|
*/ |
237
|
|
View Code Duplication |
public static function normalize_label( $label ) { |
238
|
|
|
// remove the leading underscore(if it's there) |
239
|
|
|
$label = preg_replace( '~^_~', '', $label ); |
240
|
|
|
|
241
|
|
|
// remove the leading "crb_"(if it's there) |
242
|
|
|
$label = preg_replace( '~^crb_~', '', $label ); |
243
|
|
|
|
244
|
|
|
// split the name into words and make them capitalized |
245
|
|
|
$label = mb_convert_case( str_replace( '_', ' ', $label ), MB_CASE_TITLE ); |
246
|
|
|
|
247
|
|
|
return $label; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Normalize a type string representing an object type |
252
|
|
|
* |
253
|
|
|
* @param string $type |
254
|
|
|
* @return string |
255
|
|
|
*/ |
256
|
|
View Code Duplication |
public static function normalize_type( $type ) { |
257
|
|
|
$normalized_type = str_replace( ' ', '_', $type ); |
258
|
|
|
$normalized_type = preg_replace( '/[_\s]+/', '_', $normalized_type ); |
259
|
|
|
$normalized_type = preg_replace( '/^_|_$/', '', $normalized_type ); |
260
|
|
|
$normalized_type = strtolower( $normalized_type ); |
261
|
|
|
return $normalized_type; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Convert a string representing an object type to a fully qualified class name |
266
|
|
|
* |
267
|
|
|
* @param string $type |
268
|
|
|
* @param string $namespace |
269
|
|
|
* @param string $class_suffix |
270
|
|
|
* @return string |
271
|
|
|
*/ |
272
|
|
|
public static function type_to_class( $type, $namespace = '', $class_suffix = '' ) { |
273
|
|
|
$classlike_type = static::normalize_type( $type ); |
274
|
|
|
$classlike_type = str_replace( '_', ' ', $classlike_type ); |
275
|
|
|
$classlike_type = ucwords( $classlike_type ); |
276
|
|
|
$classlike_type = str_replace( ' ', '_', $classlike_type ); |
277
|
|
|
|
278
|
|
|
$class = $classlike_type . $class_suffix; |
279
|
|
|
if ( $namespace ) { |
280
|
|
|
$class = $namespace . '\\' . $class; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
return $class; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Convert a string representing an object type to a fully qualified class name |
288
|
|
|
* |
289
|
|
|
* @param string $class |
290
|
|
|
* @param string $class_suffix |
291
|
|
|
* @return string |
292
|
|
|
*/ |
293
|
|
|
public static function class_to_type( $class, $class_suffix = '' ) { |
294
|
|
|
$reflection = new \ReflectionClass( $class ); |
295
|
|
|
$type = $reflection->getShortName(); |
296
|
|
|
|
297
|
|
|
if ( $class_suffix ) { |
298
|
|
|
$type = preg_replace( '/(' . preg_quote( $class_suffix, '/' ) . ')$/i', '', $type ); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
$type = static::normalize_type( $type ); |
302
|
|
|
|
303
|
|
|
return $type; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Get an array of sanitized html classes |
308
|
|
|
* |
309
|
|
|
* @param string|array<string> $classes |
310
|
|
|
* @return array<string> |
311
|
|
|
*/ |
312
|
|
|
public static function sanitize_classes( $classes ) { |
313
|
|
|
if ( ! is_array( $classes ) ) { |
314
|
|
|
$classes = array_values( array_filter( explode( ' ', $classes ) ) ); |
315
|
|
|
} |
316
|
|
|
$classes = array_map( 'sanitize_html_class', $classes ); |
317
|
|
|
return $classes; |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.