Passed
Push — master ( fe5b90...baa8e1 )
by Paul
05:38
created

SiteReviewsForm::normalizeFieldClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
ccs 0
cts 6
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Html\Partials;
4
5
use GeminiLabs\SiteReviews\Database\OptionManager;
6
use GeminiLabs\SiteReviews\Defaults\StyleValidationDefaults;
7
use GeminiLabs\SiteReviews\Helper;
8
use GeminiLabs\SiteReviews\Modules\Html;
9
use GeminiLabs\SiteReviews\Modules\Html\Builder;
10
use GeminiLabs\SiteReviews\Modules\Html\Field;
11
use GeminiLabs\SiteReviews\Modules\Html\Form;
12
use GeminiLabs\SiteReviews\Modules\Html\Partial;
13
use GeminiLabs\SiteReviews\Modules\Html\Template;
14
use GeminiLabs\SiteReviews\Modules\Session;
15
use GeminiLabs\SiteReviews\Modules\Style;
16
17
class SiteReviewsForm
18
{
19
	/**
20
	 * @var array
21
	 */
22
	protected $args;
23
24
	/**
25
	 * @var array
26
	 */
27
	protected $errors;
28
29
	/**
30
	 * @var string
31
	 */
32
	protected $message;
33
34
	/**
35
	 * @var array
36
	 */
37
	protected $required;
38
39
	/**
40
	 * @var array
41
	 */
42
	protected $values;
43
44
	/**
45
	 * @return void|string
46
	 */
47
	public function build( array $args = [] )
48
	{
49
		$this->args = $args;
50
		if( !is_user_logged_in() && glsr( OptionManager::class )->get( 'settings.general.require.login' ) != 'yes' ) {
51
			return $this->buildLoginRegister();
52
		}
53
		$this->errors = glsr( Session::class )->get( $args['id'].'errors', [], true );
54
		$this->message = glsr( Session::class )->get( $args['id'].'message', '', true );
55
		$this->required = glsr( OptionManager::class )->get( 'settings.submissions.required', [] );
56
		$this->values = glsr( Session::class )->get( $args['id'].'values', [], true );
57
		$fields = array_reduce( $this->getFields(), function( $carry, $field ) {
58
			return $carry.$field;
59
		});
60
		return glsr( Template::class )->build( 'templates/reviews-form', [
61
			'context' => [
62
				'class' => $this->getClass(),
63
				'fields' => $fields,
64
				'id' => $this->args['id'],
65
				'response' => $this->buildResponse(),
66
				'submit_button' => $this->buildSubmitButton().$this->buildRecaptcha(),
67
			],
68
		]);
69
	}
70
71
	/**
72
	 * @return string
73
	 */
74
	protected function buildLoginRegister()
75
	{
76
		return glsr( Template::class )->build( 'templates/login-register', [
77
			'context' => [
78
				'text' => trim( $this->getLoginText().' '.$this->getRegisterText() ),
79
			],
80
		]);
81
	}
82
83
	/**
84
	 * @return void|string
85
	 */
86
	protected function buildRecaptcha()
87
	{
88
		if( !glsr( OptionManager::class )->isRecaptchaEnabled() )return;
89
		return glsr( Builder::class )->div([
90
			'class' => 'glsr-recaptcha-holder',
91
			'data-badge' => glsr( OptionManager::class )->get( 'settings.submissions.recaptcha.position' ),
92
			'data-sitekey' => sanitize_text_field( glsr( OptionManager::class )->get( 'settings.submissions.recaptcha.key' )),
93
			'data-size' => 'invisible',
94
		]);
95
	}
96
97
	/**
98
	 * @return string
99
	 */
100
	protected function buildResponse()
101
	{
102
		$classes = !empty( $this->errors )
103
			? glsr( StyleValidationDefaults::class )->defaults()['message_error_class']
104
			: '';
105
		return glsr( Template::class )->build( 'templates/form/response', [
106
			'context' => [
107
				'class' => $classes,
108
				'message' => wpautop( $this->message ),
109
			],
110
			'has_errors' => !empty( $this->errors ),
111
		]);
112
	}
113
114
	/**
115
	 * @return string
116
	 */
117
	protected function buildSubmitButton()
118
	{
119
		return glsr( Template::class )->build( 'templates/form/submit-button', [
120
			'context' => [
121
				'text' => __( 'Submit your review', 'site-reviews' ),
122
			],
123
		]);
124
	}
125
126
	/**
127
	 * @return string
128
	 */
129
	protected function getClass()
130
	{
131
		return trim( 'glsr-form glsr-'.glsr( Style::class )->get().' '.$this->args['class'] );
132
	}
133
134
	/**
135
	 * @return array
136
	 */
137
	protected function getFields()
138
	{
139
		$fields = array_merge(
140
			$this->getHiddenFields(),
141
			[$this->getHoneypotField()],
142
			$this->normalizeFields( glsr( Form::class )->getFields( 'submission-form' ))
143
		);
144
		return $fields;
145
	}
146
147
	/**
148
	 * @return string
149
	 */
150
	protected function getLoginText()
151
	{
152
		$loginLink = glsr( Builder::class )->a([
153
			'href' => wp_login_url( strval( get_permalink() )),
154
			'text' => __( 'logged in', 'site-reviews' ),
155
		]);
156
		return sprintf( __( 'You must be %s to submit a review.', 'site-reviews' ), $loginLink );
157
	}
158
159
	/**
160
	 * @return void|string
161
	 */
162
	protected function getRegisterText()
163
	{
164
		if( !get_option( 'users_can_register' ) || glsr( OptionManager::class )->get( 'settings.general.require.login' ) != 'yes' )return;
165
		$registerLink = glsr( Builder::class )->a([
166
			'href' => wp_registration_url(),
167
			'text' => __( 'register', 'site-reviews' ),
168
		]);
169
		return sprintf( __( 'You may also %s for an account.', 'site-reviews' ), $registerLink );
170
	}
171
172
	/**
173
	 * @return array
174
	 */
175
	protected function getHiddenFields()
176
	{
177
		$fields = [[
178
			'name' => 'action',
179
			'value' => 'submit-review',
180
		],[
181
			'name' => 'assign_to',
182
			'value' => $this->args['assign_to'],
183
		],[
184
			'name' => 'category',
185
			'value' => $this->args['category'],
186
		],[
187
			'name' => 'excluded',
188
			'value' => $this->args['excluded'], // @todo should default to "[]"
189
		],[
190
			'name' => 'form_id',
191
			'value' => $this->args['id'],
192
		],[
193
			'name' => 'nonce',
194
			'value' => wp_create_nonce( 'submit-review' ),
195
		],[
196
			'name' => 'post_id',
197
			'value' => get_the_ID(),
198
		],[
199
			'name' => 'referer',
200
			'value' => wp_unslash( filter_input( INPUT_SERVER, 'REQUEST_URI' )),
201
		]];
202
		return array_map( function( $field ) {
203
			return new Field( wp_parse_args( $field, ['type' => 'hidden'] ));
204
		}, $fields );
205
	}
206
207
	/**
208
	 * @return Field
209
	 */
210
	protected function getHoneypotField()
211
	{
212
		return new Field([
213
			'name' => 'gotcha',
214
			'type' => 'honeypot',
215
		]);
216
	}
217
218
	/**
219
	 * @return void
220
	 */
221
	protected function normalizeFieldClass( Field &$field )
222
	{
223
		if( !isset( $field->field['class'] )) {
224
			$field->field['class'] = '';
225
		}
226
		$field->field['class'] = trim( $field->field['class'].' glsr-field-control' );
227
	}
228
229
	/**
230
	 * @return void
231
	 */
232
	protected function normalizeFieldErrors( Field &$field )
233
	{
234
		if( !array_key_exists( $field->field['path'], $this->errors ))return;
235
		$field->field['errors'] = $this->errors[$field->field['path']];
236
	}
237
238
	/**
239
	 * @return void
240
	 */
241
	protected function normalizeFieldRequired( Field &$field )
242
	{
243
		if( !in_array( $field->field['path'], $this->required ))return;
244
		$field->field['required'] = true;
245
	}
246
247
	/**
248
	 * @return array
249
	 */
250
	protected function normalizeFields( $fields )
251
	{
252
		foreach( $fields as &$field ) {
253
			$field->field['is_public'] = true;
254
			$this->normalizeFieldClass( $field );
255
			$this->normalizeFieldErrors( $field );
256
			$this->normalizeFieldRequired( $field );
257
			$this->normalizeFieldValue( $field );
258
		}
259
		return $fields;
260
	}
261
262
	/**
263
	 * @return void
264
	 */
265
	protected function normalizeFieldValue( Field &$field )
266
	{
267
		if( !array_key_exists( $field->field['path'], $this->values ))return;
268
		if( in_array( $field->field['type'], ['radio', 'checkbox'] )) {
269
			$field->field['checked'] = $field->field['value'] == $this->values[$field->field['path']];
270
		}
271
		else {
272
			$field->field['value'] = $this->values[$field->field['path']];
273
		}
274
	}
275
}
276