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