Passed
Push — master ( d9e953...87ce9d )
by Paul
02:59
created

Instruction::getInstructions()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\Pollux\MetaBox;
4
5
use GeminiLabs\Pollux\Application;
6
use GeminiLabs\Pollux\Component;
7
use GeminiLabs\Pollux\Helper;
8
use GeminiLabs\Pollux\MetaBox\Instruction;
9
use GeminiLabs\Pollux\MetaBox\Validator;
10
use RecursiveArrayIterator;
11
use RecursiveIteratorIterator;
12
13
/**
14
 * @property array $metaboxes
15
 */
16
trait Instruction
17
{
18
	/**
19
	 * @return string
20
	 */
21
	protected function generateInstructions()
22
	{
23
		$instructions = array_reduce( $this->getInstructionGroups(), function( $html, $metabox ) {
24
			$fields = $this->getInstructionFields( $metabox );
25
			if( empty( $fields )) {
26
				return $html;
27
			}
28
			return $html . sprintf( '<p><strong>%s</strong></p><pre class="my-sites nav-tab-active misc-pub-section">%s</pre>',
29
				$metabox['title'],
30
				$fields
31
			);
32
		});
33
		return $this->filter( 'before/instructions', '' ) . $instructions . $this->filter( 'after/instructions', '' );
34
	}
35
36
	/**
37
	 * @return array
38
	 */
39
	protected function getInstructionFields( $metabox )
40
	{
41
		return array_reduce( $metabox['fields'], function( $html, $field ) use( $metabox ) {
42
			return $this->validate( $field['condition'] ) && !in_array( $field['type'], ['divider', 'heading'] )
43
				? $html . $this->filter( 'instruction', "PostMeta::get('{$field['slug']}');", $field, $metabox ) . PHP_EOL
44
				: $html;
45
		});
46
	}
47
48
	/**
49
	 * @return array
50
	 */
51
	protected function getInstructionGroups()
52
	{
53
		return array_filter( $this->metaboxes, function( $metabox ) {
54
			return $this->validate( $metabox['condition'] )
55
				&& $this->hasPostType( $metabox );
56
		});
57
	}
58
59
	/**
60
	 * @return null|array
61
	 */
62
	protected function initInstructions()
63
	{
64
		if( !$this->showInstructions() )return;
65
		return [
66
			'infodiv' => [
67
				'context' => 'side',
68
				'fields' => [[
69
					'slug' => '',
70
					'std' => $this->generateInstructions(),
71
					'type' => 'custom_html',
72
				]],
73
				'post_types' => $this->getPostTypes(),
74
				'priority' => 'low',
75
				'title' => __( 'How to use in your theme', 'pollux' ),
76
			],
77
		];
78
	}
79
80
	/**
81
	 * @return bool
82
	 */
83
	protected function showInstructions()
84
	{
85
		return $this->filter( 'show/instructions', count( array_filter( $this->metaboxes, function( $metabox ) {
86
			return $this->show( false, $metabox );
87
		})) > 0 );
88
	}
89
90
	/**
91
	 * @param string $name
92
	 * @param mixed ...$args
93
	 * @return mixed
94
	 */
95
	abstract public function filter( $name, ...$args );
96
97
	/**
98
	 * @return bool
99
	 * @filter rwmb_show
100
	 */
101
	abstract public function show( $bool, array $metabox );
102
103
	/**
104
	 * @return bool
105
	 */
106
	abstract public function validate( array $conditions );
107
108
	/**
109
	 * @return array
110
	 */
111
	abstract protected function getPostTypes();
112
113
	/**
114
	 * @return bool
115
	 */
116
	abstract protected function hasPostType( array $metabox );
117
}
118