1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\Pollux\MetaBox; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\Pollux\Helper; |
6
|
|
|
|
7
|
|
|
trait Condition |
8
|
|
|
{ |
9
|
|
|
public static $conditions = [ |
10
|
|
|
'class_exists', 'defined', 'function_exists', 'hook', 'is_front_page', 'is_home', |
11
|
|
|
'is_page_template', 'is_plugin_active', 'is_plugin_inactive', |
12
|
|
|
]; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var Application |
16
|
|
|
*/ |
17
|
|
|
protected $app; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @return string |
21
|
|
|
*/ |
22
|
|
|
public static function conditions() |
23
|
|
|
{ |
24
|
|
|
return apply_filters( 'pollux/conditions', static::$conditions, strtolower(( new Helper )->getClassname( static::class ))); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return bool |
29
|
|
|
*/ |
30
|
|
|
public function validate( array $conditions ) |
31
|
|
|
{ |
32
|
|
|
array_walk( $conditions, function( &$value, $key ) { |
33
|
|
|
$method = ( new Helper )->buildMethodName( $key, 'validate' ); |
34
|
|
|
$value = method_exists( $this, $method ) |
35
|
|
|
? $this->$method( $value ) |
36
|
|
|
: $this->validateUnknown( $key, $value ); |
37
|
|
|
}); |
38
|
|
|
return !in_array( false, $conditions ); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param mixed $conditions |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
protected function normalizeCondition( $conditions ) |
46
|
|
|
{ |
47
|
|
|
$conditions = ( new Helper )->toArray( $conditions ); |
48
|
|
|
if( count( array_filter( array_keys( $conditions ), 'is_string' )) == 0 ) { |
49
|
|
|
foreach( $conditions as $key ) { |
50
|
|
|
$conditions[str_replace( '!', '', $key )] = substr( $key, 0, 1 ) == '!' ? 0 : 1; |
51
|
|
|
} |
52
|
|
|
$conditions = array_filter( $conditions, function( $key ) { |
53
|
|
|
return !is_numeric( $key ); |
54
|
|
|
}, ARRAY_FILTER_USE_KEY ); |
55
|
|
|
} |
56
|
|
|
$hook = sprintf( 'pollux/%s/conditions', strtolower(( new Helper )->getClassname( $this ))); |
57
|
|
|
return array_intersect_key( |
58
|
|
|
$conditions, |
59
|
|
|
array_flip( apply_filters( $hook, static::conditions() )) |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $value |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
protected function validateClassExists( $value ) |
68
|
|
|
{ |
69
|
|
|
return class_exists( $value ); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $value |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
protected function validateDefined( $value ) |
77
|
|
|
{ |
78
|
|
|
return defined( $value ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $value |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
|
|
protected function validateFunctionExists( $value ) |
86
|
|
|
{ |
87
|
|
|
return function_exists( $value ); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $value |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
protected function validateHook( $value ) |
95
|
|
|
{ |
96
|
|
|
return apply_filters( $value, true ); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param bool $value |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
protected function validateIsFrontPage( $value ) |
104
|
|
|
{ |
105
|
|
|
return $value == ( $this->getPostId() == get_option( 'page_on_front' )); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param bool $value |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
protected function validateIsHome( $value ) |
113
|
|
|
{ |
114
|
|
|
return $value == ( $this->getPostId() == get_option( 'page_for_posts' )); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $value |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
protected function validateIsPageTemplate( $value ) |
122
|
|
|
{ |
123
|
|
|
return basename( get_page_template_slug( $this->getPostId() )) == $value; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $value |
128
|
|
|
* @return bool |
129
|
|
|
*/ |
130
|
|
|
protected function validateIsPluginActive( $value ) |
131
|
|
|
{ |
132
|
|
|
return is_plugin_active( $value ); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param string $value |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
|
|
protected function validateIsPluginInactive( $value ) |
140
|
|
|
{ |
141
|
|
|
return is_plugin_inactive( $value ); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param string $key |
146
|
|
|
* @param mixed $value |
147
|
|
|
* @return bool |
148
|
|
|
*/ |
149
|
|
|
protected function validateUnknown( $key, $value ) |
150
|
|
|
{ |
151
|
|
|
return apply_filters( 'pollux/metabox/condition', true, $key, $value ); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return int |
156
|
|
|
*/ |
157
|
|
|
abstract protected function getPostId(); |
158
|
|
|
} |
159
|
|
|
|