1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\Pollux\MetaBox; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\Pollux\Helper; |
6
|
|
|
use WP_Post; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @property Application $app |
10
|
|
|
*/ |
11
|
|
|
trait Condition |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @return array |
15
|
|
|
*/ |
16
|
|
|
public static function absolutes() |
17
|
|
|
{ |
18
|
|
|
$defaults = [ |
19
|
|
|
'class_exists', 'defined', 'function_exists', 'hook', 'is_front_page', 'is_home', |
20
|
|
|
'is_plugin_active', 'is_plugin_inactive', |
21
|
|
|
]; |
22
|
|
|
return defined( 'static::ABSOLUTE_CONDITIONS' ) |
23
|
|
|
? Helper::toArray( static::ABSOLUTE_CONDITIONS ) |
|
|
|
|
24
|
|
|
: $defaults; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
|
|
public static function conditions() |
31
|
|
|
{ |
32
|
|
|
$defaults = [ |
33
|
|
|
'class_exists', 'defined', 'function_exists', 'hook', 'is_front_page', 'is_home', |
34
|
|
|
'is_page_template', 'is_plugin_active', 'is_plugin_inactive', |
35
|
|
|
]; |
36
|
|
|
return defined( 'static::CONDITIONS' ) |
37
|
|
|
? Helper::toArray( static::CONDITIONS ) |
|
|
|
|
38
|
|
|
: $defaults; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $name |
43
|
|
|
* @param mixed ...$args |
44
|
|
|
* @return mixed |
45
|
|
|
*/ |
46
|
|
|
abstract public function filter( $name, ...$args ); |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return bool |
50
|
|
|
*/ |
51
|
|
|
public function validate( array $conditions ) |
52
|
|
|
{ |
53
|
|
|
array_walk( $conditions, function( &$value, $key ) { |
54
|
|
|
$value = $this->isConditionValid( $key, $value ); |
55
|
|
|
}); |
56
|
|
|
return !in_array( false, $conditions ); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return int |
61
|
|
|
*/ |
62
|
|
|
abstract protected function getPostId(); |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $method |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
|
protected function isAbsoluteConditionValid( $method, array $values ) |
69
|
|
|
{ |
70
|
|
|
foreach( $values as $value ) { |
71
|
|
|
if( $this->$method( $value ))continue; |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
return true; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $key |
79
|
|
|
* @param string|array $values |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
protected function isConditionValid( $key, $values ) |
83
|
|
|
{ |
84
|
|
|
$method = Helper::buildMethodName( $key, 'validate' ); |
85
|
|
|
if( !method_exists( $this, $method )) { |
86
|
|
|
return $this->validateUnknown( $key, $values ); |
87
|
|
|
} |
88
|
|
|
$values = Helper::toArray( $values ); |
89
|
|
|
return in_array( $key, $this->absolutes() ) |
90
|
|
|
? $this->isAbsoluteConditionValid( $method, $values ) |
91
|
|
|
: $this->isLooseConditionValid( $method, $values ); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $method |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
|
protected function isLooseConditionValid( $method, array $values ) |
99
|
|
|
{ |
100
|
|
|
foreach( $values as $value ) { |
101
|
|
|
if( !$this->$method( $value ))continue; |
102
|
|
|
return true; |
103
|
|
|
} |
104
|
|
|
return false; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param mixed $conditions |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
protected function normalizeCondition( $conditions ) |
112
|
|
|
{ |
113
|
|
|
if( !is_array( $conditions )) { |
114
|
|
|
$conditions = []; |
115
|
|
|
} |
116
|
|
|
if( count( array_filter( array_keys( $conditions ), 'is_string' )) == 0 ) { |
117
|
|
|
foreach( $conditions as $key ) { |
118
|
|
|
$conditions[str_replace( '!', '', $key )] = substr( $key, 0, 1 ) == '!' ? 0 : 1; |
119
|
|
|
} |
120
|
|
|
$conditions = array_filter( $conditions, function( $key ) { |
121
|
|
|
return !is_numeric( $key ); |
122
|
|
|
}, ARRAY_FILTER_USE_KEY ); |
123
|
|
|
} |
124
|
|
|
return array_intersect_key( |
125
|
|
|
$conditions, |
126
|
|
|
array_flip( $this->filter( 'conditions', static::conditions() )) |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $value |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
|
|
protected function validateClassExists( $value ) |
135
|
|
|
{ |
136
|
|
|
return class_exists( $value ); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $value |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
|
|
protected function validateDefined( $value ) |
144
|
|
|
{ |
145
|
|
|
return defined( $value ); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $value |
150
|
|
|
* @return bool |
151
|
|
|
*/ |
152
|
|
|
protected function validateFunctionExists( $value ) |
153
|
|
|
{ |
154
|
|
|
return function_exists( $value ); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param string $value |
159
|
|
|
* @return bool |
160
|
|
|
*/ |
161
|
|
|
protected function validateHook( $value ) |
162
|
|
|
{ |
163
|
|
|
return apply_filters( $value, true ); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param bool $value |
168
|
|
|
* @return bool |
169
|
|
|
*/ |
170
|
|
|
protected function validateIsFrontPage( $value ) |
171
|
|
|
{ |
172
|
|
|
return $value == ( $this->getPostId() == get_option( 'page_on_front' )); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param bool $value |
177
|
|
|
* @return bool |
178
|
|
|
*/ |
179
|
|
|
protected function validateIsHome( $value ) |
180
|
|
|
{ |
181
|
|
|
return $value == ( $this->getPostId() == get_option( 'page_for_posts' )); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param string $value |
186
|
|
|
* @return bool |
187
|
|
|
*/ |
188
|
|
|
protected function validateIsPageTemplate( $value ) |
189
|
|
|
{ |
190
|
|
|
return Helper::endsWith( $value, basename( get_page_template_slug( $this->getPostId() ))); |
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param string $value |
195
|
|
|
* @return bool |
196
|
|
|
*/ |
197
|
|
|
protected function validateIsPluginActive( $value ) |
198
|
|
|
{ |
199
|
|
|
return $this->app->gatekeeper->isPluginActive( $value ); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param string $value |
204
|
|
|
* @return bool |
205
|
|
|
*/ |
206
|
|
|
protected function validateIsPluginInactive( $value ) |
207
|
|
|
{ |
208
|
|
|
return !$this->validateIsPluginActive( $value ); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param string $key |
213
|
|
|
* @param mixed $value |
214
|
|
|
* @return bool |
215
|
|
|
*/ |
216
|
|
|
protected function validateUnknown( $key, $value ) |
217
|
|
|
{ |
218
|
|
|
return apply_filters( 'pollux/metabox/condition', true, $key, $value ); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|