1 | <?php |
||
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() |
||
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 |