Passed
Push — master ( e448bf...51995c )
by Paul
07:24 queued 04:56
created

Instruction::addInstructions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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