Completed
Push — develop ( 652775...251fde )
by Paul
02:25
created

PostMeta::normalize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
3
namespace GeminiLabs\Castor\Helpers;
4
5
class PostMeta
6
{
7
	public function get( $metaKey, array $args = [] )
8
	{
9
		if( empty( $metaKey ))return;
10
11
		$args = $this->normalize( $args );
12
		$metaKey = $this->buildMetaKey( $metaKey, $args['prefix'] );
13
		$metaValue = get_post_meta( $args['ID'], $metaKey, $args['single'] );
14
15
		return empty( $metaValue )
16
			? $args['fallback']
17
			: $metaValue;
18
	}
19
20
	protected function buildMetaKey( $metaKey, $prefix )
21
	{
22
		return ( substr( $metaKey, 0, 1 ) == '_' && !empty( $prefix ))
23
			? sprintf( '_%s%s', rtrim( $prefix, '_' ), $metaKey )
24
			: $prefix . $metaKey;
25
	}
26
27
	protected function normalize( array $args )
28
	{
29
		$defaults = [
30
			'ID'       => get_the_ID(),
31
			'fallback' => '',
32
			'single'   => true,
33
			'prefix'   => apply_filters( 'castor/postmeta/prefix', 'pollux_' ),
34
		];
35
		return shortcode_atts( $defaults, $args );
36
	}
37
}
38