Passed
Push — develop ( 87abfc...84da8f )
by Paul
02:47
created

Instruction::validate()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
nc 1
dl 0
loc 1
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 void
22
	 */
23
	protected function addInstructions()
24
	{
25
		if( !$this->showInstructions() )return;
26
		$this->normalize([
27
			'infodiv' => [
28
				'context' => 'side',
29
				'fields' => [[
30
					'slug' => '',
31
					'std' => $this->generateInstructions(),
32
					'type' => 'custom_html',
33
				]],
34
				'post_types' => $this->getPostTypes(),
35
				'priority' => 'low',
36
				'title' => __( 'How to use in your theme', 'pollux' ),
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( $metabox['fields'], function( $html, $field ) use( $metabox ) {
48
				if( !$this->validate( $field['condition'] )) {
49
					return $html;
50
				}
51
				$hook = sprintf( 'pollux/%s/instruction', strtolower(( new Helper )->getClassname( $this )));
52
				return $html . apply_filters( $hook, "PostMeta::get('{$field['slug']}');", $field['slug'], $metabox['slug'] ) . PHP_EOL;
53
			});
54
			return $html . sprintf( '<p><strong>%s</strong></p><pre class="my-sites nav-tab-active misc-pub-section">%s</pre>',
55
				$metabox['title'],
56
				$fields
57
			);
58
		});
59
	}
60
61
	/**
62
	 * @return array
63
	 */
64
	protected function getInstructions()
65
	{
66
		return array_filter( $this->metaboxes, function( $metabox ) {
67
			return $this->validate( $metabox['condition'] )
68
				&& $this->hasPostType( $metabox );
69
		});
70
	}
71
72
	/**
73
	 * @return bool
74
	 */
75
	protected function showInstructions()
76
	{
77
		return count( array_filter( $this->metaboxes, function( $metabox ) {
78
			return $this->show( false, $metabox );
79
		})) > 0;
80
	}
81
82
	/**
83
	 * @return bool
84
	 * @filter rwmb_show
85
	 */
86
	abstract public function show( $bool, array $metabox );
87
88
	/**
89
	 * @return bool
90
	 */
91
	abstract public function validate( array $conditions );
92
93
	/**
94
	 * @return array
95
	 */
96
	abstract protected function getPostTypes();
97
98
	/**
99
	 * @return bool
100
	 */
101
	abstract protected function hasPostType( array $metabox );
102
103
	/**
104
	 * @return void
105
	 */
106
	abstract protected function normalize( array $metaboxes, array $defaults = [] );
107
}
108