Instruction   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 30
c 4
b 1
f 0
dl 0
loc 102
ccs 0
cts 32
cp 0
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A initInstructions() 0 14 2
A showInstructions() 0 5 1
A getInstructionFields() 0 7 3
A getInstructionGroups() 0 5 2
A generateInstructions() 0 13 2
1
<?php
2
3
namespace GeminiLabs\Pollux\MetaBox;
4
5
use GeminiLabs\Pollux\Application;
6
use GeminiLabs\Pollux\Component;
7
use GeminiLabs\Pollux\MetaBox\Instruction;
8
use RecursiveArrayIterator;
9
use RecursiveIteratorIterator;
10
11
/**
12
 * @property array $metaboxes
13
 */
14
trait Instruction
15
{
16
	/**
17
	 * @return string
18
	 */
19
	protected function generateInstructions()
20
	{
21
		$instructions = array_reduce( $this->getInstructionGroups(), function( $html, $metabox ) {
22
			$fields = $this->getInstructionFields( $metabox );
23
			if( empty( $fields )) {
24
				return $html;
25
			}
26
			return $html . sprintf( '<p><strong>%s</strong></p><pre class="my-sites nav-tab-active misc-pub-section">%s</pre>',
27
				$metabox['title'],
28
				$fields
0 ignored issues
show
Bug introduced by
$fields of type array is incompatible with the type string expected by parameter $args of sprintf(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

28
				/** @scrutinizer ignore-type */ $fields
Loading history...
29
			);
30
		});
31
		return $this->filter( 'before/instructions', '' ) . $instructions . $this->filter( 'after/instructions', '' );
32
	}
33
34
	/**
35
	 * @return array
36
	 */
37
	protected function getInstructionFields( $metabox )
38
	{
39
		$skipFields = ['custom_html', 'divider', 'heading', 'taxonomy'];
40
		return array_reduce( $metabox['fields'], function( $html, $field ) use( $metabox, $skipFields ) {
41
			return $this->validate( $field['condition'] ) && !in_array( $field['type'], $skipFields )
42
				? $html . $this->filter( 'instruction', "PostMeta::get('{$field['slug']}');", $field, $metabox ) . PHP_EOL
43
				: $html;
44
		});
45
	}
46
47
	/**
48
	 * @return array
49
	 */
50
	protected function getInstructionGroups()
51
	{
52
		return array_filter( $this->metaboxes, function( $metabox ) {
53
			return $this->validate( $metabox['condition'] )
54
				&& $this->hasPostType( $metabox );
55
		});
56
	}
57
58
	/**
59
	 * @return null|array
60
	 */
61
	protected function initInstructions()
62
	{
63
		if( !$this->showInstructions() )return;
64
		return [
65
			'infodiv' => [
66
				'context' => 'side',
67
				'fields' => [[
68
					'slug' => '',
69
					'std' => $this->generateInstructions(),
70
					'type' => 'custom_html',
71
				]],
72
				'post_types' => $this->getPostTypes(),
73
				'priority' => 'low',
74
				'title' => __( 'How to use in your theme', 'pollux' ),
75
			],
76
		];
77
	}
78
79
	/**
80
	 * @return bool
81
	 */
82
	protected function showInstructions()
83
	{
84
		return $this->filter( 'show/instructions', count( array_filter( $this->metaboxes, function( $metabox ) {
85
			return $this->show( false, $metabox );
86
		})) > 0 );
87
	}
88
89
	/**
90
	 * @param string $name
91
	 * @param mixed ...$args
92
	 * @return mixed
93
	 */
94
	abstract public function filter( $name, ...$args );
95
96
	/**
97
	 * @return bool
98
	 * @filter rwmb_show
99
	 */
100
	abstract public function show( $bool, array $metabox );
101
102
	/**
103
	 * @return bool
104
	 */
105
	abstract public function validate( array $conditions );
106
107
	/**
108
	 * @return array
109
	 */
110
	abstract protected function getPostTypes();
111
112
	/**
113
	 * @return bool
114
	 */
115
	abstract protected function hasPostType( array $metabox );
116
}
117