Passed
Push — master ( d9e953...87ce9d )
by Paul
02:59
created

Condition::validateClassExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\Pollux\MetaBox;
4
5
use GeminiLabs\Pollux\Helper;
6
7
/**
8
 * @property Application $app
9
 */
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 )
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 ( new Helper )->endsWith(
124
			$value,
125
			basename( get_page_template_slug( $this->getPostId() ))
126
		);
127
	}
128
129
	/**
130
	 * @param string $value
131
	 * @return bool
132
	 */
133
	protected function validateIsPluginActive( $value )
134
	{
135
		return $this->app->gatekeeper->isPluginActive( $value );
136
	}
137
138
	/**
139
	 * @param string $value
140
	 * @return bool
141
	 */
142
	protected function validateIsPluginInactive( $value )
143
	{
144
		return !$this->validateIsPluginActive( $value );
145
	}
146
147
	/**
148
	 * @param string $key
149
	 * @param mixed $value
150
	 * @return bool
151
	 */
152
	protected function validateUnknown( $key, $value )
153
	{
154
		return apply_filters( 'pollux/metabox/condition', true, $key, $value );
155
	}
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