Passed
Branch master (3d135a)
by Paul
02:50
created

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