Passed
Push — master ( 9b46a7...ed8579 )
by Paul
03:56
created

Settings::isMultiDependency()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 4
nop 1
dl 0
loc 8
ccs 0
cts 8
cp 0
crap 20
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Html;
4
5
use GeminiLabs\SiteReviews\Database\DefaultsManager;
6
use GeminiLabs\SiteReviews\Database\OptionManager;
7
use GeminiLabs\SiteReviews\Helper;
8
use GeminiLabs\SiteReviews\Modules\Html\Field;
9
use GeminiLabs\SiteReviews\Modules\Html\Template;
10
use GeminiLabs\SiteReviews\Modules\Translation;
11
12
class Settings
13
{
14
	/**
15
	 * @var array
16
	 */
17
	public $settings;
18
19
	/**
20
	 * @param string $id
21
	 * @return string
22
	 */
23
	public function buildFields( $id )
24
	{
25
		$this->settings = glsr( DefaultsManager::class )->settings();
26
		$method = glsr( Helper::class )->buildMethodName( $id, 'getTemplateDataFor' );
27
		$data = !method_exists( $this, $method )
28
			? $this->getTemplateData( $id )
29
			: $this->$method( $id );
30
		return glsr( Template::class )->build( 'pages/settings/'.$id, $data );
31
	}
32
33
	/**
34
	 * @return string
35
	 */
36
	protected function getFieldDefault( array $field )
37
	{
38
		return isset( $field['default'] )
39
			? $field['default']
40
			: '';
41
	}
42
43
	/**
44
	 * @return array
45
	 */
46
	protected function getSettingFields( $path )
47
	{
48
		return array_filter( $this->settings, function( $key ) use( $path ) {
49
			return glsr( Helper::class )->startsWith( $path, $key );
50
		}, ARRAY_FILTER_USE_KEY );
51
	}
52
53
	/**
54
	 * @return string
55
	 */
56
	protected function getSettingRows( array $fields )
57
	{
58
		$rows = '';
59
		foreach( $fields as $name => $field ) {
60
			$field = wp_parse_args( $field, [
61
				'is_setting' => true,
62
				'name' => $name,
63
			]);
64
			$rows.= new Field( $this->normalize( $field ));
65
		}
66
		return $rows;
67
	}
68
69
	/**
70
	 * @param string $id
71
	 * @return array
72
	 */
73
	protected function getTemplateData( $id )
74
	{
75
		$fields = $this->getSettingFields( $this->normalizeSettingPath( $id ));
76
		return [
77
			'context' => [
78
				'rows' => $this->getSettingRows( $fields ),
79
			],
80
		];
81
	}
82
83
	/**
84
	 * @param string $id
85
	 * @return array
86
	 */
87
	protected function getTemplateDataForAddons( $id )
88
	{
89
		$fields = $this->getSettingFields( $this->normalizeSettingPath( $id ));
90
		$settings = glsr( Helper::class )->convertDotNotationArray( $fields );
91
		$settingKeys = array_keys( $settings['settings']['addons'] );
92
		$results = [];
93
		foreach( $settingKeys as $key ) {
94
			$addonFields = array_filter( $fields, function( $path ) use( $key ) {
95
				return glsr( Helper::class )->startsWith( 'settings.addons.'.$key, $path );
96
			}, ARRAY_FILTER_USE_KEY );
97
			$results[$key] = $this->getSettingRows( $addonFields );
98
		}
99
		return [
100
			'settings' => $results,
101
		];
102
	}
103
104
	/**
105
	 * @return array
106
	 */
107
	protected function getTemplateDataForTranslations()
108
	{
109
		$translations = glsr( Translation::class )->renderAll();
110
		$class = empty( $translations )
111
			? 'glsr-hidden'
112
			: '';
113
		return [
114
			'context' => [
115
				'class' => $class,
116
				'database_key' => OptionManager::databaseKey(),
117
				'translations' => $translations,
118
			],
119
		];
120
	}
121
122
	/**
123
	 * @param string $path
124
	 * @param string|array $expectedValue
125
	 * @return bool
126
	 */
127
	protected function isFieldHidden( $path, $expectedValue )
128
	{
129
		$optionValue = glsr( OptionManager::class )->get(
130
			$path,
131
			glsr( Helper::class )->getPathValue( $path, glsr()->defaults )
132
		);
133
		if( is_array( $expectedValue )) {
134
			return is_array( $optionValue )
135
				? count( array_intersect( $optionValue, $expectedValue )) === 0
136
				: !in_array( $optionValue, $expectedValue );
137
		}
138
		return $optionValue != $expectedValue;
139
	}
140
141
	/**
142
	 * @return bool
143
	 */
144
	protected function isMultiDependency( $path )
145
	{
146
		if( isset( $this->settings[$path] )) {
147
			$field = $this->settings[$path];
148
			return ( $field['type'] == 'checkbox' && !empty( $field['options'] ))
149
				|| !empty( $field['multiple'] );
150
		}
151
		return false;
152
	}
153
154
	/**
155
	 * @return array
156
	 */
157
	protected function normalize( array $field )
158
	{
159
		$field = $this->normalizeDependsOn( $field );
160
		$field = $this->normalizeLabelAndLegend( $field );
161
		$field = $this->normalizeValue( $field );
162
		return $field;
163
	}
164
165
	/**
166
	 * @return array
167
	 */
168
	protected function normalizeDependsOn( array $field )
169
	{
170
		if( !empty( $field['depends_on'] ) && is_array( $field['depends_on'] )) {
171
			$path = key( $field['depends_on'] );
172
			$expectedValue = $field['depends_on'][$path];
173
			$fieldName = glsr( Helper::class )->convertPathToName( $path, OptionManager::databaseKey() );
174
			if( $this->isMultiDependency( $path )) {
175
				$fieldName.= '[]';
176
			}
177
			$field['data-depends'] = json_encode([
178
				'name' => $fieldName,
179
				'value' => $expectedValue,
180
			], JSON_HEX_APOS|JSON_HEX_QUOT );
181
			$field['is_hidden'] = $this->isFieldHidden( $path, $expectedValue );
182
		}
183
		return $field;
184
	}
185
186
	/**
187
	 * @return array
188
	 */
189
	protected function normalizeLabelAndLegend( array $field )
190
	{
191
		if( !empty( $field['label'] )) {
192
			$field['legend'] = $field['label'];
193
			unset( $field['label'] );
194
		}
195
		else {
196
			$field['is_valid'] = false;
197
			glsr_log()->warning( 'Setting field is missing a label' )->info( $field );
198
		}
199
		return $field;
200
	}
201
202
	/**
203
	 * @return array
204
	 */
205
	protected function normalizeValue( array $field )
206
	{
207
		if( !isset( $field['value'] )) {
208
			$field['value'] = glsr( OptionManager::class )->get(
209
				$field['name'],
210
				$this->getFieldDefault( $field )
211
			);
212
		}
213
		return $field;
214
	}
215
216
	/**
217
	 * @return string
218
	 */
219
	protected function normalizeSettingPath( $path )
220
	{
221
		return glsr( Helper::class )->prefixString( rtrim( $path, '.' ), 'settings.' );
222
	}
223
}
224