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

BlockGenerator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 61
ccs 0
cts 25
cp 0
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A attributes() 0 3 1
A hasVisibleFields() 0 7 1
A normalize() 0 15 4
A register() 0 9 2
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