Completed
Push — milestone/2_0/react-ui ( 17c6c0...6ff089 )
by
unknown
03:44
created

Helper::normalize_label()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 12
loc 12
ccs 0
cts 5
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
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_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_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
	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_id( $object_id );
58
		}
59
60
		if ( is_a( $clone, 'Carbon_Fields\\Field\\Complex_Field' ) ) {
61
			$value_tree = ( ! empty( $value ) ) ? $value : array( 'value_set' => array(), 'groups' => array() );
62
			$clone->set_value( $value_tree['value_set'] );
63
			$clone->set_value_tree( $value_tree );
64
		} else {
65
			$clone->set_value( $value );
66
		}
67
		$clone->save();
68
	}
69
70
	/**
71
	 * Shorthand for get_post_meta().
72
	 * Uses the ID of the current post in the loop.
73
	 *
74
	 * @param  string $name Custom field name.
75
	 * @return mixed        Meta value.
76
	 */
77
	public static function get_the_post_meta( $name ) {
78
		return static::get_post_meta( get_the_ID(), $name );
79
	}
80
81
	/**
82
	 * Get post meta field for a post.
83
	 *
84
	 * @param int    $id   Post ID.
85
	 * @param string $name Custom field name.
86
	 * @return mixed        Meta value.
87
	 */
88
	public static function get_post_meta( $id, $name ) {
89
		return static::get_value( $id, 'post_meta', $name );
90
	}
91
92
	/**
93
	 * Set post meta field for a post.
94
	 *
95
	 * @param int $id Post ID
96
	 * @param string $name Custom field name
97
	 * @param array $value
98
	 * @return bool Success
99
	 */
100
	public static function set_post_meta( $id, $name, $value ) {
101
		return static::set_value( $id, 'post_meta', $name, $value );
102
	}
103
104
	/**
105
	 * Get theme option field value.
106
	 *
107
	 * @param string $name Custom field name
108
	 * @return mixed Option value
109
	 */
110
	public static function get_theme_option( $name ) {
111
		return static::get_value( null, 'theme_options', $name );
112
	}
113
114
	/**
115
	 * Set theme option field value.
116
	 *
117
	 * @param string $name Field name
118
	 * @param array $value
119
	 * @return bool Success
120
	 */
121
	public static function set_theme_option( $name, $value ) {
122
		return static::set_value( null, 'theme_options', $name, $value );
123
	}
124
125
	/**
126
	 * Get term meta field for a term.
127
	 *
128
	 * @param  int    $id   Term ID.
129
	 * @param  string $name Custom field name.
130
	 * @return mixed        Meta value.
131
	 */
132
	public static function get_term_meta( $id, $name ) {
133
		return static::get_value( $id, 'term_meta', $name );
134
	}
135
136
	/**
137
	 * Set term meta field for a term.
138
	 *
139
	 * @param int $id Term ID
140
	 * @param string $name Field name
141
	 * @param array $value
142
	 * @return bool Success
143
	 */
144
	public static function set_term_meta( $id, $name, $value ) {
145
		return static::set_value( $id, 'term_meta', $name, $value );
146
	}
147
148
	/**
149
	 * Get user meta field for a user.
150
	 *
151
	 * @param  int    $id   User ID.
152
	 * @param  string $name Custom field name.
153
	 * @return mixed        Meta value.
154
	 */
155
	public static function get_user_meta( $id, $name ) {
156
		return static::get_value( $id, 'user_meta', $name );
157
	}
158
159
	/**
160
	 * Set user meta field for a user.
161
	 *
162
	 * @param int $id User ID
163
	 * @param string $name Field name
164
	 * @param array $value
165
	 * @return bool Success
166
	 */
167
	public static function set_user_meta( $id, $name, $value ) {
168
		return static::set_value( $id, 'user_meta', $name, $value );
169
	}
170
171
	/**
172
	 * Get comment meta field for a comment.
173
	 *
174
	 * @param  int    $id   Comment ID.
175
	 * @param  string $name Custom field name.
176
	 * @return mixed        Meta value.
177
	 */
178
	public static function get_comment_meta( $id, $name ) {
179
		return static::get_value( $id, 'comment_meta', $name );
180
	}
181
182
	/**
183
	 * Set comment meta field for a comment.
184
	 *
185
	 * @param int $id Comment ID
186
	 * @param string $name Field name
187
	 * @param array $value
188
	 * @return bool Success
189
	 */
190
	public static function set_comment_meta( $id, $name, $value ) {
191
		return static::set_value( $id, 'comment_meta', $name, $value );
192
	}
193
194
	/**
195
	 * Recursive sorting function by array key.
196
	 * 
197
	 * @param  array  &$array     The input array.
198
	 * @param  int    $sort_flags Flags for controlling sorting behavior.
199
	 * @return array              Sorted array.
200
	 */
201
	public static function ksort_recursive( &$array, $sort_flags = SORT_REGULAR ) {
202
		if ( ! is_array( $array ) ) {
203
			return false;
204
		}
205
		ksort( $array, $sort_flags );
206
		foreach ( $array as $key => $value ) {
207
			self::ksort_recursive( $array[ $key ], $sort_flags );
208
		}
209
		return true;
210
	}
211
212
	/**
213
	 * Get the relation type from an array similar to how meta_query works in WP_Query
214
	 * 
215
	 * @param array $array
216
	 * @param array<string> $allowed_relations
217
	 * @param string $relation_key
218
	 * @return string
219
	 */
220
	public static function get_relation_type_from_array( $array, $allowed_relations = array( 'AND', 'OR' ), $relation_key = 'relation' ) {
221
		$allowed_relations = array_values( $allowed_relations );
222
		$allowed_relations = array_map( 'strtoupper', $allowed_relations );
223
		$relation = isset( $allowed_relations[0] ) ? $allowed_relations[0] : '';
224
225
		if ( isset( $array[ $relation_key ] ) ) {
226
			$relation = strtoupper( $array[ $relation_key ] );
227
		}
228
229 View Code Duplication
		if ( ! in_array( $relation, $allowed_relations ) ) {
230
			Incorrect_Syntax_Exception::raise( 'Invalid relation type ' . $relation . '. ' .
231
			'The rule should be one of the following: "' . implode( '", "', $allowed_relations ) . '"' );
232
		}
233
234
		return $relation;
235
	}
236
237
	/**
238
	 * Normalize a label by updating case, stripping common prefixes etc.
239
	 * 
240
	 * @param  string $label
241
	 * @return string
242
	 */
243 View Code Duplication
	public static function normalize_label( $label ) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
244
		// remove the leading underscore(if it's there)
245
		$label = preg_replace( '~^_~', '', $label );
246
247
		// remove the leading "crb_"(if it's there)
248
		$label = preg_replace( '~^crb_~', '', $label );
249
250
		// split the name into words and make them capitalized
251
		$label = mb_convert_case( str_replace( '_', ' ', $label ), MB_CASE_TITLE );
252
253
		return $label;
254
	}
255
256
	/**
257
	 * Normalize a type string representing an object type
258
	 * 
259
	 * @param  string $type
260
	 * @return string
261
	 */
262 View Code Duplication
	public static function normalize_type( $type ) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
263
		$normalized_type = str_replace( ' ', '_', $type );
264
		$normalized_type = preg_replace( '/[_\s]+/', '_', $normalized_type );
265
		$normalized_type = preg_replace( '/^_|_$/', '', $normalized_type );
266
		$normalized_type = strtolower( $normalized_type );
267
		return $normalized_type;
268
	}
269
270
	/**
271
	 * Convert a string representing an object type to a fully qualified class name
272
	 * 
273
	 * @param  string $type
274
	 * @param  string $namespace
275
	 * @param  string $class_suffix
276
	 * @return string
277
	 */
278
	public static function type_to_class( $type, $namespace = '', $class_suffix = '' ) {
279
		$classlike_type = static::normalize_type( $type );
280
		$classlike_type = str_replace( '_', ' ', $classlike_type );
281
		$classlike_type = ucwords( $classlike_type );
282
		$classlike_type = str_replace( ' ', '_', $classlike_type );
283
284
		$class = $classlike_type . $class_suffix;
285
		if ( $namespace ) {
286
			$class = $namespace . '\\' . $class;
287
		}
288
289
		return $class;
290
	}
291
292
	/**
293
	 * Convert a string representing an object type to a fully qualified class name
294
	 * 
295
	 * @param  string $class
296
	 * @param  string $class_suffix
297
	 * @return string
298
	 */
299
	public static function class_to_type( $class, $class_suffix = '' ) {
300
		$reflection = new \ReflectionClass( $class );
301
		$type = $reflection->getShortName();
302
303
		if ( $class_suffix ) {
304
			$type = preg_replace( '/(' . preg_quote( $class_suffix, '/' ) . ')$/i', '', $type );
305
		}
306
307
		$type = static::normalize_type( $type );
308
309
		return $type;
310
	}
311
	
312
	/**
313
	 * Get an array of sanitized html classes
314
	 * 
315
	 * @param  string|array<string> $classes
316
	 * @return array<string>
317
	 */
318
	public static function sanitize_classes( $classes ) {
319
		if ( ! is_array( $classes ) ) {
320
			$classes = array_values( array_filter( explode( ' ', $classes ) ) );
321
		}
322
		$classes = array_map( 'sanitize_html_class', $classes );
323
		return $classes;
324
	}
325
}
326