|
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
|
|
|
* @return void |
|
16
|
|
|
*/ |
|
17
|
|
|
protected function addInstructions() |
|
18
|
|
|
{ |
|
19
|
|
|
if( !count( array_filter( $this->metaboxes, function( $metabox ) { |
|
|
|
|
|
|
20
|
|
|
return $this->show( false, $metabox ); |
|
|
|
|
|
|
21
|
|
|
})))return; |
|
22
|
|
|
$this->metaboxes[] = [ |
|
23
|
|
|
'id' => 'infodiv', |
|
24
|
|
|
'post_types' => $this->getPostTypes(), |
|
|
|
|
|
|
25
|
|
|
'title' => __( 'How to use in your theme', 'pollux' ), |
|
26
|
|
|
'context' => 'side', |
|
27
|
|
|
'priority' => 'low', |
|
28
|
|
|
'fields' => [[ |
|
29
|
|
|
'slug' => '', |
|
30
|
|
|
'std' => $this->generateInstructions(), |
|
31
|
|
|
'type' => 'custom_html', |
|
32
|
|
|
]], |
|
33
|
|
|
]; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return string |
|
38
|
|
|
*/ |
|
39
|
|
|
protected function generateInstructions() |
|
40
|
|
|
{ |
|
41
|
|
|
return array_reduce( $this->getInstructions(), function( $html, $metabox ) { |
|
42
|
|
|
$fields = array_reduce( array_column( $metabox['fields'], 'slug' ), function( $html, $slug ) use( $metabox ) { |
|
43
|
|
|
$hook = sprintf( 'pollux/%s/instruction', $this->getClassname() ); |
|
|
|
|
|
|
44
|
|
|
return $html . apply_filters( $hook, "PostMeta::get('{$slug}');", $slug, $metabox['slug'] ) . PHP_EOL; |
|
45
|
|
|
}); |
|
46
|
|
|
return $html . sprintf( '<p><strong>%s</strong></p><pre class="my-sites nav-tab-active misc-pub-section">%s</pre>', |
|
47
|
|
|
$metabox['title'], |
|
48
|
|
|
$fields |
|
49
|
|
|
); |
|
50
|
|
|
}); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return array |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function getInstructions() |
|
57
|
|
|
{ |
|
58
|
|
|
return array_filter( $this->metaboxes, function( $metabox ) { |
|
59
|
|
|
return $this->validate( $metabox['condition'] ) |
|
|
|
|
|
|
60
|
|
|
&& $this->hasPostType( $metabox ); |
|
|
|
|
|
|
61
|
|
|
}); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: