Passed
Push — master ( 042452...c272a3 )
by Paul
05:20
created

SiteReviewsForm   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 177
ccs 0
cts 106
cp 0
rs 10
c 0
b 0
f 0
wmc 16

11 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 15 1
A normalizeFields() 0 8 2
A normalizeFieldErrors() 0 4 2
A normalizeFieldRequired() 0 4 2
A getFields() 0 9 1
A buildResults() 0 5 1
A normalizeFieldValue() 0 8 3
A getClass() 0 4 1
A getHoneypotField() 0 5 1
A buildSubmitButton() 0 4 1
B getHiddenFields() 0 27 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Html\Partials;
4
5
use GeminiLabs\SiteReviews\Database\OptionManager;
6
use GeminiLabs\SiteReviews\Helper;
7
use GeminiLabs\SiteReviews\Modules\Html;
8
use GeminiLabs\SiteReviews\Modules\Html\Builder;
9
use GeminiLabs\SiteReviews\Modules\Html\Field;
10
use GeminiLabs\SiteReviews\Modules\Html\Form;
11
use GeminiLabs\SiteReviews\Modules\Html\Partial;
12
use GeminiLabs\SiteReviews\Modules\Html\Template;
13
use GeminiLabs\SiteReviews\Modules\Session;
14
15
class SiteReviewsForm
16
{
17
	/**
18
	 * @var array
19
	 */
20
	protected $args;
21
22
	/**
23
	 * @var array
24
	 */
25
	protected $errors;
26
27
	/**
28
	 * @var array
29
	 */
30
	protected $message;
31
32
	/**
33
	 * @var array
34
	 */
35
	protected $required;
36
37
	/**
38
	 * @var array
39
	 */
40
	protected $values;
41
42
	/**
43
	 * @return void|string
44
	 */
45
	public function build( array $args = [] )
46
	{
47
		$this->args = $args;
48
		$this->errors = glsr( Session::class )->get( $args['id'].'errors', [], true );
49
		$this->message = glsr( Session::class )->get( $args['id'].'message', [], true );
50
		$this->required = glsr( OptionManager::class )->get( 'settings.submissions.required', [] );
51
		$this->values = glsr( Session::class )->get( $args['id'].'values', [], true );
52
		return glsr( Template::class )->build( 'templates/reviews-form', [
53
			'context' => [
54
				'class' => $this->getClass(),
55
				'id' => $this->args['id'],
56
				'results' => $this->buildResults(),
57
				'submit_button' => $this->buildSubmitButton(),
58
			],
59
			'fields' => $this->getFields(),
60
		]);
61
	}
62
63
	/**
64
	 * @return string
65
	 */
66
	public function buildResults()
67
	{
68
		return glsr( Partial::class )->build( 'form-results', [
69
			'errors' => $this->errors,
70
			'message' => $this->message,
71
		]);
72
	}
73
74
	/**
75
	 * @return string
76
	 */
77
	public function buildSubmitButton()
78
	{
79
		return glsr( Builder::class )->button( '<span></span>'.__( 'Submit your review', 'site-reviews' ), [
80
			'type' => 'submit',
81
		]);
82
	}
83
84
	/**
85
	 * @return string
86
	 */
87
	protected function getClass()
88
	{
89
		$style = apply_filters( 'site-reviews/reviews-form/style', 'glsr-style' );
90
		return trim( 'glsr-form '.$style.' '.$this->args['class'] );
91
	}
92
93
	/**
94
	 * @return array
95
	 */
96
	protected function getFields()
97
	{
98
		$fields = array_merge(
99
			$this->getHiddenFields(),
100
			[$this->getHoneypotField()],
101
			$this->normalizeFields( glsr( Form::class )->getFields( 'submission-form' ))
102
		);
103
		// glsr_debug( $fields );
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
104
		return $fields;
105
	}
106
107
	/**
108
	 * @return array
109
	 */
110
	protected function getHiddenFields()
111
	{
112
		$fields = [[
113
			'name' => 'action',
114
			'value' => 'submit-review',
115
		],[
116
			'name' => 'assign_to',
117
			'value' => $this->args['assign_to'],
118
		],[
119
			'name' => 'category',
120
			'value' => $this->args['category'],
121
		],[
122
			'name' => 'excluded',
123
			'value' => $this->args['excluded'], // @todo should default to "[]"
124
		],[
125
			'name' => 'form_id',
126
			'value' => $this->args['id'],
127
		],[
128
			'name' => '_wp_http_referer',
129
			'value' => wp_unslash( filter_input( INPUT_SERVER, 'REQUEST_URI' )), // @todo this doesn't work, maybe need to get this on submit
130
		],[
131
			'name' => '_wpnonce',
132
			'value' => wp_create_nonce( 'submit-review' ),
133
		]];
134
		return array_map( function( $field ) {
135
			return new Field( wp_parse_args( $field, ['type' => 'hidden'] ));
136
		}, $fields );
137
	}
138
139
	/**
140
	 * @return Field
141
	 */
142
	protected function getHoneypotField()
143
	{
144
		return new Field([
145
			'name' => 'gotcha',
146
			'type' => 'honeypot',
147
		]);
148
	}
149
150
	/**
151
	 * @return void
152
	 */
153
	protected function normalizeFieldErrors( Field &$field )
154
	{
155
		if( !array_key_exists( $field->field['path'], $this->errors ))return;
156
		$field->field['errors'] = $this->errors[$field->field['path']];
157
	}
158
159
	/**
160
	 * @return void
161
	 */
162
	protected function normalizeFieldRequired( Field &$field )
163
	{
164
		if( !in_array( $field->field['path'], $this->required ))return;
165
		$field->field['required'] = true;
166
	}
167
168
	/**
169
	 * @return array
170
	 */
171
	protected function normalizeFields( $fields )
172
	{
173
		foreach( $fields as &$field ) {
174
			$this->normalizeFieldErrors( $field );
175
			$this->normalizeFieldRequired( $field );
176
			$this->normalizeFieldValue( $field );
177
		}
178
		return $fields;
179
	}
180
181
	/**
182
	 * @return void
183
	 */
184
	protected function normalizeFieldValue( Field &$field )
185
	{
186
		if( !array_key_exists( $field->field['path'], $this->values ))return;
187
		if( in_array( $field->field['type'], ['radio', 'checkbox'] )) {
188
			$field->field['checked'] = $field->field['value'] == $this->values[$field->field['path']];
189
		}
190
		else {
191
			$field->field['value'] = $this->values[$field->field['path']];
192
		}
193
	}
194
}
195