Passed
Push — master ( 4a454f...b92003 )
by Paul
03:35
created

TinymcePopupGenerator::normalizeField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Shortcodes;
4
5
use GeminiLabs\SiteReviews\Database;
6
use GeminiLabs\SiteReviews\Helper;
7
use ReflectionClass;
8
9
abstract class TinymcePopupGenerator
10
{
11
	/**
12
	 * @var array
13
	 */
14
	public $properties;
15
16
	/**
17
	 * @var string
18
	 */
19
	public $tag;
20
21
	/**
22
	 * @var array
23
	 */
24
	protected $errors = [];
25
26
	/**
27
	 * @var array
28
	 */
29
	protected $required = [];
30
31
	/**
32
	 * @return array
33
	 */
34
	abstract public function fields();
35
36
	/**
37
	 * @param string $tag
38
	 * @return static
39
	 */
40 1
	public function register( $tag, array $args )
41
	{
42 1
		$this->tag = $tag;
43 1
		$this->properties = wp_parse_args( $args, [
44 1
			'btn_close' => esc_html__( 'Close', 'site-reviews' ),
45 1
			'btn_okay' => esc_html__( 'Insert Shortcode', 'site-reviews' ),
46 1
			'errors' => $this->errors,
47 1
			'fields' => $this->getFields(),
48 1
			'label' => '['.$tag.']',
49 1
			'required' => $this->required,
50 1
			'title' => esc_html__( 'Shortcode', 'site-reviews' ),
51
		]);
52 1
		return $this;
53
	}
54
55
	/**
56
	 * @return array
57
	 */
58
	protected function generateFields( array $fields )
59
	{
60 1
		$generatedFields = array_map( function( $field ) {
61 1
			if( empty( $field ))return;
62 1
			$field = $this->normalize( $field );
63 1
			if( !method_exists( $this, $method = 'normalize'.ucfirst( $field['type'] )))return;
64 1
			return $this->$method( $field );
65 1
		}, $fields );
66 1
		return array_values( array_filter( $generatedFields ));
67
	}
68
69
	/**
70
	 * @param string $tooltip
71
	 * @return array
72
	 */
73 1
	protected function getCategories( $tooltip = '' )
74
	{
75 1
		$terms = glsr( Database::class )->getTerms();
76 1
		if( empty( $terms )) {
77 1
			return [];
78
		}
79
		return [
80
			'label' => esc_html__( 'Category', 'site-reviews' ),
81
			'name' => 'category',
82
			'options' => $terms,
83
			'tooltip' => $tooltip,
84
			'type' => 'listbox',
85
		];
86
	}
87
88
	/**
89
	 * @return array
90
	 */
91 1
	protected function getFields()
92
	{
93 1
		$fields = $this->generateFields( $this->fields() );
94 1
		if( !empty( $this->errors )) {
95
			$errors = [];
96
			foreach( $this->required as $name => $alert ) {
97
				if( false !== array_search( $name, glsr_array_column( $fields, 'name' )))continue;
98
				$errors[] = $this->errors[$name];
99
			}
100
			$this->errors = $errors;
101
		}
102 1
		return empty( $this->errors )
103 1
			? $fields
104 1
			: $this->errors;
105
	}
106
107
	/**
108
	 * @return array
109
	 */
110 1
	protected function getHideOptions()
111
	{
112 1
		$classname = str_replace( 'Popup', 'Shortcode', get_class( $this ));
113 1
		$hideOptions = glsr( $classname )->getHideOptions();
114 1
		$options = [];
115 1
		foreach( $hideOptions as $name => $tooltip ) {
116 1
			$options[] = [
117 1
				'name' => 'hide_'.$name,
118 1
				'text' => $name,
119 1
				'tooltip' => $tooltip,
120 1
				'type' => 'checkbox',
121
			];
122
		}
123 1
		return $options;
124
	}
125
126
	/**
127
	 * @param string $tooltip
128
	 * @return array
129
	 */
130 1
	protected function getTypes( $tooltip = '' )
131
	{
132 1
		if( count( glsr()->reviewTypes ) < 2 ) {
133 1
			return [];
134
		}
135
		return [
136
			'label' => esc_html__( 'Type', 'site-reviews' ),
137
			'name' => 'type',
138
			'options' => glsr()->reviewTypes,
139
			'tooltip' => $tooltip,
140
			'type' => 'listbox',
141
		];
142
	}
143
144
	/**
145
	 * @return array
146
	 */
147 1
	protected function normalize( array $field )
148
	{
149 1
		return wp_parse_args( $field, [
150 1
			'items' => [],
151
			'type' => '',
152
		]);
153
	}
154
155
	/**
156
	 * @return void|array
157
	 */
158 1
	protected function normalizeCheckbox( array $field )
159
	{
160 1
		return $this->normalizeField( $field, [
161 1
			'checked' => false,
162
			'label' => '',
163
			'minHeight' => '',
164
			'minWidth' => '',
165
			'name' => false,
166
			'text' => '',
167
			'tooltip' => '',
168
			'type' => '',
169
			'value' => '',
170
		]);
171
	}
172
173
	/**
174
	 * @return void|array
175
	 */
176 1
	protected function normalizeContainer( array $field )
177
	{
178 1
		if( !array_key_exists( 'html', $field ) && !array_key_exists( 'items', $field ))return;
179 1
		$field['items'] = $this->generateFields( $field['items'] );
180 1
		return $field;
181
	}
182
183
	/**
184
	 * @return void|array
185
	 */
186 1
	protected function normalizeField( array $field, array $defaults )
187
	{
188 1
		if( !$this->validate( $field ))return;
189 1
		return array_filter( shortcode_atts( $defaults, $field ), function( $value ) {
190 1
			return $value !== '';
191 1
		});
192
	}
193
194
	/**
195
	 * @return void|array
196
	 */
197 1
	protected function normalizeListbox( array $field )
198
	{
199 1
		$listbox = $this->normalizeField( $field, [
200 1
			'label' => '',
201 1
			'minWidth' => '',
202
			'name' => false,
203
			'options' => [],
204 1
			'placeholder' => esc_attr__( '- Select -', 'site-reviews' ),
205 1
			'tooltip' => '',
206 1
			'type' => '',
207 1
			'value' => '',
208
		]);
209 1
		if( !is_array( $listbox ))return;
210 1
		if( !array_key_exists( '', $listbox['options'] )) {
211 1
			$listbox['options'] = ['' => $listbox['placeholder']] + $listbox['options'];
212
		}
213 1
		foreach( $listbox['options'] as $value => $text ) {
214 1
			$listbox['values'][] = [
215 1
				'text' => $text,
216 1
				'value' => $value,
217
			];
218
		}
219 1
		return $listbox;
220
	}
221
222
	/**
223
	 * @return void|array
224
	 */
225
	protected function normalizePost( array $field )
226
	{
227
		if( !is_array( $field['query_args'] )) {
228
			$field['query_args'] = [];
229
		}
230
		$posts = get_posts( wp_parse_args( $field['query_args'], [
231
			'order' => 'ASC',
232
			'orderby' => 'title',
233
			'post_type' => 'post',
234
			'posts_per_page' => 30,
235
		]));
236
		if( !empty( $posts )) {
237
			$options = [];
238
			foreach( $posts as $post ) {
239
				$options[$post->ID] = esc_html( $post->post_title );
240
			}
241
			$field['options'] = $options;
242
			$field['type'] = 'listbox';
243
			return $this->normalizeListbox( $field );
244
		}
245
		$this->validate( $field );
246
	}
247
248
	/**
249
	 * @return void|array
250
	 */
251 1
	protected function normalizeTextbox( array $field )
252
	{
253 1
		return $this->normalizeField( $field, [
254 1
			'hidden' => false,
255
			'label' => '',
256
			'maxLength' => '',
257
			'minHeight' => '',
258
			'minWidth' => '',
259
			'multiline' => false,
260
			'name' => false,
261
			'size' => '',
262
			'text' => '',
263
			'tooltip' => '',
264
			'type' => '',
265
			'value' => '',
266
		]);
267
	}
268
269
	/**
270
	 * @return bool
271
	 */
272 1
	protected function validate( array $field )
273
	{
274 1
		$args = shortcode_atts([
275 1
			'label' => '',
276
			'name' => false,
277
			'required' => false,
278 1
		], $field );
279 1
		if( !$args['name'] ) {
280
			return false;
281
		}
282 1
		return $this->validateErrors( $args ) && $this->validateRequired( $args );
283
	}
284
285
	/**
286
	 * @return bool
287
	 */
288 1
	protected function validateErrors( array $args )
289
	{
290 1
		if( !isset( $args['required']['error'] )) {
291 1
			return true;
292
		}
293
		$this->errors[$args['name']] = $this->normalizeContainer([
294
			'html' => $args['required']['error'],
295
			'type' => 'container',
296
		]);
297
		return false;
298
	}
299
300
	/**
301
	 * @return bool
302
	 */
303 1
	protected function validateRequired( array $args )
304
	{
305 1
		if( $args['required'] == false ) {
306 1
			return true;
307
		}
308
		$alert = esc_html__( 'Some of the shortcode options are required.', 'site-reviews' );
309
		if( isset( $args['required']['alert'] )) {
310
			$alert = $args['required']['alert'];
311
		}
312
		else if( !empty( $args['label'] )) {
313
			$alert = sprintf(
314
				esc_html_x( 'The "%s" option is required.', 'the option label', 'site-reviews' ),
315
				str_replace( ':', '', $args['label'] )
316
			);
317
		}
318
		$this->required[$args['name']] = $alert;
319
		return false;
320
	}
321
}
322