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

PostMeta   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 0
loc 33
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 12 3
A buildMetaKey() 0 6 3
A normalize() 0 10 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