1 | <?php |
||
10 | trait Condition |
||
11 | { |
||
12 | /** |
||
13 | * @return string |
||
14 | */ |
||
15 | public static function conditions() |
||
16 | { |
||
17 | $defaults = [ |
||
18 | 'class_exists', 'defined', 'function_exists', 'hook', 'is_front_page', 'is_home', |
||
19 | 'is_page_template', 'is_plugin_active', 'is_plugin_inactive', |
||
20 | ]; |
||
21 | return defined( 'static::CONDITIONS' ) |
||
22 | ? static::CONDITIONS |
||
23 | : $defaults; |
||
24 | } |
||
25 | |||
26 | /** |
||
27 | * @return bool |
||
28 | */ |
||
29 | public function validate( array $conditions ) |
||
30 | { |
||
31 | array_walk( $conditions, function( &$value, $key ) { |
||
32 | $method = ( new Helper )->buildMethodName( $key, 'validate' ); |
||
33 | $value = method_exists( $this, $method ) |
||
34 | ? $this->$method( $value ) |
||
35 | : $this->validateUnknown( $key, $value ); |
||
36 | }); |
||
37 | return !in_array( false, $conditions ); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @param mixed $conditions |
||
42 | * @return array |
||
43 | */ |
||
44 | protected function normalizeCondition( $conditions ) |
||
45 | { |
||
46 | if( !is_array( $conditions )) { |
||
47 | $conditions = []; |
||
48 | } |
||
49 | if( count( array_filter( array_keys( $conditions ), 'is_string' )) == 0 ) { |
||
50 | foreach( $conditions as $key ) { |
||
51 | $conditions[str_replace( '!', '', $key )] = substr( $key, 0, 1 ) == '!' ? 0 : 1; |
||
52 | } |
||
53 | $conditions = array_filter( $conditions, function( $key ) { |
||
54 | return !is_numeric( $key ); |
||
55 | }, ARRAY_FILTER_USE_KEY ); |
||
56 | } |
||
57 | return array_intersect_key( |
||
58 | $conditions, |
||
59 | array_flip( $this->filter( 'conditions', static::conditions() )) |
||
60 | ); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @param string $value |
||
65 | * @return bool |
||
66 | */ |
||
67 | protected function validateClassExists( $value ) |
||
71 | |||
72 | /** |
||
73 | * @param string $value |
||
74 | * @return bool |
||
75 | */ |
||
76 | protected function validateDefined( $value ) |
||
80 | |||
81 | /** |
||
82 | * @param string $value |
||
83 | * @return bool |
||
84 | */ |
||
85 | protected function validateFunctionExists( $value ) |
||
89 | |||
90 | /** |
||
91 | * @param string $value |
||
92 | * @return bool |
||
93 | */ |
||
94 | protected function validateHook( $value ) |
||
98 | |||
99 | /** |
||
100 | * @param bool $value |
||
101 | * @return bool |
||
102 | */ |
||
103 | protected function validateIsFrontPage( $value ) |
||
107 | |||
108 | /** |
||
109 | * @param bool $value |
||
110 | * @return bool |
||
111 | */ |
||
112 | protected function validateIsHome( $value ) |
||
116 | |||
117 | /** |
||
118 | * @param string $value |
||
119 | * @return bool |
||
120 | */ |
||
121 | protected function validateIsPageTemplate( $value ) |
||
128 | |||
129 | /** |
||
130 | * @param string $value |
||
131 | * @return bool |
||
132 | */ |
||
133 | protected function validateIsPluginActive( $value ) |
||
137 | |||
138 | /** |
||
139 | * @param string $value |
||
140 | * @return bool |
||
141 | */ |
||
142 | protected function validateIsPluginInactive( $value ) |
||
146 | |||
147 | /** |
||
148 | * @param string $key |
||
149 | * @param mixed $value |
||
150 | * @return bool |
||
151 | */ |
||
152 | protected function validateUnknown( $key, $value ) |
||
156 | |||
157 | /** |
||
158 | * @param string $name |
||
159 | * @param mixed ...$args |
||
160 | * @return mixed |
||
161 | */ |
||
162 | abstract public function filter( $name, ...$args ); |
||
163 | |||
164 | /** |
||
165 | * @return int |
||
166 | */ |
||
167 | abstract protected function getPostId(); |
||
168 | } |
||
169 |