Passed
Branch master (3d135a)
by Paul
02:50
created

Instruction::addInstructions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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