Passed
Push — master ( 81fb7c...e448bf )
by Paul
05:17 queued 02:36
created

PostMetaManager::get()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 5
nop 2
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\Pollux\MetaBox;
4
5
use GeminiLabs\Pollux\Application;
6
7
class PostMetaManager
8
{
9
	/**
10
	 * @param string $metaKey
11
	 * @return mixed
12
	 */
13
	public function get( $metaKey, array $args = [] )
14
	{
15
		if( empty( $metaKey ))return;
16
17
		$args = $this->normalize( $args );
18
		$metaKey = $this->buildMetaKey( $metaKey, $args['prefix'] );
19
		$metaValue = get_post_meta( $args['id'], $metaKey, $args['single'] );
20
21
		if( is_string( $metaValue )) {
22
			$metaValue = trim( $metaValue );
23
		}
24
		return empty( $metaValue )
25
			? $args['fallback']
26
			: $metaValue;
27
	}
28
29
	/**
30
	 * @param string $metaKey
31
	 * @param string $prefix
32
	 * @return string
33
	 */
34
	protected function buildMetaKey( $metaKey, $prefix )
35
	{
36
		return ( substr( $metaKey, 0, 1 ) == '_' && !empty( $prefix ))
37
			? sprintf( '_%s%s', rtrim( $prefix, '_' ), $metaKey )
38
			: $prefix . $metaKey;
39
	}
40
41
	/**
42
	 * @return array
43
	 */
44
	protected function normalize( array $args )
45
	{
46
		$defaults = [
47
			'id'       => get_the_ID(),
48
			'fallback' => '',
49
			'single'   => true,
50
			'prefix'   => apply_filters( 'pollux/prefix', Application::PREFIX ),
51
		];
52
		return shortcode_atts( $defaults, array_change_key_case( $args ));
53
	}
54
}
55