Passed
Push — master ( 8a0962...507d84 )
by Paul
03:52
created

SiteReviewsForm::getHoneypotField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 5
cp 0
crap 2
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
		$this->errors = glsr( Session::class )->get( $args['id'].'errors', [], true );
51
		$this->message = glsr( Session::class )->get( $args['id'].'message', '', true );
52
		$this->required = glsr( OptionManager::class )->get( 'settings.submissions.required', [] );
53
		$this->values = glsr( Session::class )->get( $args['id'].'values', [], true );
54
		$fields = array_reduce( $this->getFields(), function( $carry, $field ) {
55
			return $carry.$field;
56
		});
57
		return glsr( Template::class )->build( 'templates/reviews-form', [
58
			'context' => [
59
				'class' => $this->getClass(),
60
				'fields' => $fields,
61
				'id' => $this->args['id'],
62
				'response' => $this->buildResponse(),
63
				'submit_button' => $this->buildSubmitButton().$this->buildRecaptcha(),
64
			],
65
		]);
66
	}
67
68
	/**
69
	 * @return void|string
70
	 */
71
	protected function buildRecaptcha()
72
	{
73
		if( !glsr( OptionManager::class )->isRecaptchaEnabled() )return;
74
		return glsr( Builder::class )->div([
75
			'class' => 'glsr-recaptcha-holder',
76
			'data-badge' => glsr( OptionManager::class )->get( 'settings.submissions.recaptcha.position' ),
77
			'data-sitekey' => sanitize_text_field( glsr( OptionManager::class )->get( 'settings.submissions.recaptcha.key' )),
78
			'data-size' => 'invisible',
79
		]);
80
	}
81
82
	/**
83
	 * @return string
84
	 */
85
	protected function buildResponse()
86
	{
87
		$classes = !empty( $this->errors )
88
			? glsr( StyleValidationDefaults::class )->defaults()['message_error_class']
89
			: '';
90
		return glsr( Template::class )->build( 'templates/form/response', [
91
			'context' => [
92
				'class' => $classes,
93
				'message' => wpautop( $this->message ),
94
			],
95
			'has_errors' => !empty( $this->errors ),
96
		]);
97
	}
98
99
	/**
100
	 * @return string
101
	 */
102
	protected function buildSubmitButton()
103
	{
104
		return glsr( Template::class )->build( 'templates/form/submit-button', [
105
			'context' => [
106
				'text' => __( 'Submit your review', 'site-reviews' ),
107
			],
108
		]);
109
	}
110
111
	/**
112
	 * @return string
113
	 */
114
	protected function getClass()
115
	{
116
		return trim( 'glsr-form glsr-'.glsr( Style::class )->get().' '.$this->args['class'] );
117
	}
118
119
	/**
120
	 * @return array
121
	 */
122
	protected function getFields()
123
	{
124
		$fields = array_merge(
125
			$this->getHiddenFields(),
126
			[$this->getHoneypotField()],
127
			$this->normalizeFields( glsr( Form::class )->getFields( 'submission-form' ))
128
		);
129
		return $fields;
130
	}
131
132
	/**
133
	 * @return array
134
	 */
135
	protected function getHiddenFields()
136
	{
137
		$fields = [[
138
			'name' => 'action',
139
			'value' => 'submit-review',
140
		],[
141
			'name' => 'assign_to',
142
			'value' => $this->args['assign_to'],
143
		],[
144
			'name' => 'category',
145
			'value' => $this->args['category'],
146
		],[
147
			'name' => 'excluded',
148
			'value' => $this->args['excluded'], // @todo should default to "[]"
149
		],[
150
			'name' => 'form_id',
151
			'value' => $this->args['id'],
152
		],[
153
			'name' => 'nonce',
154
			'value' => wp_create_nonce( 'submit-review' ),
155
		],[
156
			'name' => 'referer',
157
			'value' => wp_unslash( filter_input( INPUT_SERVER, 'REQUEST_URI' )),
158
		]];
159
		return array_map( function( $field ) {
160
			return new Field( wp_parse_args( $field, ['type' => 'hidden'] ));
161
		}, $fields );
162
	}
163
164
	/**
165
	 * @return Field
166
	 */
167
	protected function getHoneypotField()
168
	{
169
		return new Field([
170
			'name' => 'gotcha',
171
			'type' => 'honeypot',
172
		]);
173
	}
174
175
	/**
176
	 * @return void
177
	 */
178
	protected function normalizeFieldErrors( Field &$field )
179
	{
180
		if( !array_key_exists( $field->field['path'], $this->errors ))return;
181
		$field->field['errors'] = $this->errors[$field->field['path']];
182
	}
183
184
	/**
185
	 * @return void
186
	 */
187
	protected function normalizeFieldRequired( Field &$field )
188
	{
189
		if( !in_array( $field->field['path'], $this->required ))return;
190
		$field->field['required'] = true;
191
	}
192
193
	/**
194
	 * @return array
195
	 */
196
	protected function normalizeFields( $fields )
197
	{
198
		foreach( $fields as &$field ) {
199
			$field->field['is_public'] = true;
200
			$this->normalizeFieldErrors( $field );
201
			$this->normalizeFieldRequired( $field );
202
			$this->normalizeFieldValue( $field );
203
		}
204
		return $fields;
205
	}
206
207
	/**
208
	 * @return void
209
	 */
210
	protected function normalizeFieldValue( Field &$field )
211
	{
212
		if( !array_key_exists( $field->field['path'], $this->values ))return;
213
		if( in_array( $field->field['type'], ['radio', 'checkbox'] )) {
214
			$field->field['checked'] = $field->field['value'] == $this->values[$field->field['path']];
215
		}
216
		else {
217
			$field->field['value'] = $this->values[$field->field['path']];
218
		}
219
	}
220
}
221