Completed
Push — milestone/2_0/container-condit... ( 446b23...9b81fb )
by
unknown
04:50
created

Helper::get_the_post_meta()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Carbon_Fields\Helper;
4
5
use Carbon_Fields\App;
6
use Carbon_Fields\Datastore\Datastore;
7
use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
8
9
/**
10
 * Helper functions and main initialization class.
11
 */
12
class Helper {
13
14
	/**
15
	 * Get a value formatted for end-users
16
	 *
17
	 * @param int $object_id Object id to get value for (e.g. post_id, term_id etc.)
18
	 * @param string $container_type Container type to search in
19
	 * @param string $field_name Field name
20
	 * @return mixed
21
	 */
22
	public static function get_value( $object_id, $container_type, $field_name ) {
23
		$repository = App::resolve( 'container_repository' );
24
		$field = $repository->get_field_in_containers( $field_name, $container_type );
25
26
		if ( ! $field ) {
27
			Incorrect_Syntax_Exception::raise( 'Could not find a field which satisfies the supplied pattern: ' . $field_name );
28
		}
29
30
		$clone = clone $field;
31
		if ( $object_id !== null ) {
32
			$clone->get_datastore()->set_id( $object_id );
33
		}
34
35
		$clone->load();
36
		return $clone->get_formatted_value();
37
	}
38
39
	/**
40
	 * Set value for a field
41
	 *
42
	 * @param int $object_id Object id to get value for (e.g. post_id, term_id etc.)
43
	 * @param string $container_type Container type to search in
44
	 * @param string $field_name Field name
45
	 * @param array $value Field expects a `value_set`; Complex_Field expects a `value_tree` - refer to DEVELOPMENT.md
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 );
50
51
		if ( ! $field ) {
52
			Incorrect_Syntax_Exception::raise( 'Could not find a field which satisfies the supplied pattern: ' . $field_name );
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 type string representing an object type
239
	 * 
240
	 * @param  string $type
241
	 * @return string
242
	 */
243
	public static function normalize_type( $type ) {
244
		$normalized_type = str_replace( ' ', '_', $type );
245
		$normalized_type = preg_replace( '/[_\s]+/', '_', $normalized_type );
246
		$normalized_type = preg_replace( '/^_|_$/', '', $normalized_type );
247
		$normalized_type = strtolower( $normalized_type );
248
		return $normalized_type;
249
	}
250
251
	/**
252
	 * Convert a string representing an object type to a fully qualified class name
253
	 * 
254
	 * @param  string $type
255
	 * @param  string $namespace
256
	 * @param  string $class_suffix
257
	 * @return string
258
	 */
259
	public static function type_to_class( $type, $namespace = '', $class_suffix = '' ) {
260
		$classlike_type = static::normalize_type( $type );
261
		$classlike_type = str_replace( '_', ' ', $classlike_type );
262
		$classlike_type = ucwords( $classlike_type );
263
		$classlike_type = str_replace( ' ', '_', $classlike_type );
264
265
		$class = $classlike_type . $class_suffix;
266
		if ( $namespace ) {
267
			$class = $namespace . '\\' . $class;
268
		}
269
270
		return $class;
271
	}
272
273
	/**
274
	 * Convert a string representing an object type to a fully qualified class name
275
	 * 
276
	 * @param  string $class
277
	 * @param  string $class_suffix
278
	 * @return string
279
	 */
280
	public static function class_to_type( $class, $class_suffix = '' ) {
281
		$type = (new \ReflectionClass( $class ))->getShortName();
282
283
		if ( $class_suffix ) {
284
			$type = preg_replace( '/(' . preg_quote( $class_suffix, '/' ) . ')$/i', '', $type );
285
		}
286
287
		$type = static::normalize_type( $type );
288
289
		return $type;
290
	}
291
	
292
	/**
293
	 * Get an array of sanitized html classes
294
	 * 
295
	 * @param  string|array<strnig> $classes
296
	 * @return array<string>
297
	 */
298
	public static function sanitize_classes( $classes ) {
299
		if ( ! is_array( $classes ) ) {
300
			$classes = array_values( array_filter( explode( ' ', $classes ) ) );
301
		}
302
		$classes = array_map( 'sanitize_html_class', $classes );
303
		return $classes;
304
	}
305
}
306