Passed
Push — master ( d98594...cee2ee )
by Paul
07:20 queued 03:47
created

BlockGenerator::hasVisibleFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 7
ccs 0
cts 6
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Blocks;
4
5
use GeminiLabs\SiteReviews\Application;
6
7
abstract class BlockGenerator
8
{
9
	/**
10
	 * @return array
11
	 */
12
	public function attributes()
13
	{
14
		return [];
15
	}
16
17
	/**
18
	 * @return array
19
	 */
20
	public function normalize( array $attributes )
21
	{
22
		$hide = array_flip( explode( ',', $attributes['hide'] ));
23
		unset( $hide['if_empty'] );
24
		$attributes['hide'] = implode( ',', array_keys( $hide ));
25
		if( !isset( $attributes['assigned_to'] )) {
26
			return $attributes;
27
		}
28
		if( $attributes['assigned_to'] == 'post_id' ) {
29
			$attributes['assigned_to'] = $attributes['post_id'];
30
		}
31
		else if( $attributes['assigned_to'] == 'parent_id' ) {
32
			$attributes['assigned_to'] = wp_get_post_parent_id( $attributes['post_id'] );
33
		}
34
		return $attributes;
35
	}
36
37
	/**
38
	 * @return void
39
	 */
40
	public function register( $block )
41
	{
42
		if( !function_exists( 'register_block_type' ))return;
43
		register_block_type( Application::ID.'/'.$block, [
44
			'attributes' => $this->attributes(),
45
			'editor_script' => Application::ID.'/blocks',
46
			'editor_style' => Application::ID.'/blocks',
47
			'render_callback' => [$this, 'render'],
48
			'style' => Application::ID,
49
		]);
50
	}
51
52
	/**
53
	 * @return void
54
	 */
55
	abstract public function render( array $attributes );
56
57
	/**
58
	 * @param mixed $shortcode
59
	 * @return bool
60
	 */
61
	protected function hasVisibleFields( $shortcode, array $attributes )
62
	{
63
		$args = $shortcode->normalize( $attributes );
64
		$defaults = $shortcode->getHideOptions();
65
		$hide = array_flip( $args['hide'] );
66
		unset( $defaults['if_empty'], $hide['if_empty'] );
67
		return !empty( array_diff_key( $defaults, $hide ));
68
	}
69
}
70