Passed
Push — master ( 723a7a...81c350 )
by Paul
04:27
created

SiteReviewsForm::buildRecaptcha()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
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\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().$this->buildRecaptcha(),
58
			],
59
			'fields' => $this->getFields(),
60
		]);
61
	}
62
63
	/**
64
	 * @return void|string
65
	 */
66
	protected function buildRecaptcha()
67
	{
68
		$integration = glsr( OptionManager::class )->get( 'settings.submissions.recaptcha.integration' );
69
		$recaptchaMethod = glsr( Helper::class )->buildMethodName( $integration, 'getRecaptcha' );
70
		if( method_exists( $this, $recaptchaMethod )) {
71
			return $this->$recaptchaMethod();
72
		}
73
	}
74
75
	/**
76
	 * @return string
77
	 */
78
	protected function buildResults()
79
	{
80
		return glsr( Partial::class )->build( 'form-results', [
81
			'errors' => $this->errors,
82
			'message' => $this->message,
83
		]);
84
	}
85
86
	/**
87
	 * @return string
88
	 */
89
	protected function buildSubmitButton()
90
	{
91
		return glsr( Builder::class )->button( '<span></span>'.__( 'Submit your review', 'site-reviews' ), [
92
			'type' => 'submit',
93
		]);
94
	}
95
96
	/**
97
	 * @return string
98
	 */
99
	protected function getClass()
100
	{
101
		$style = apply_filters( 'site-reviews/reviews-form/style', 'glsr-style' );
102
		return trim( 'glsr-form '.$style.' '.$this->args['class'] );
103
	}
104
105
	/**
106
	 * @return array
107
	 */
108
	protected function getFields()
109
	{
110
		$fields = array_merge(
111
			$this->getHiddenFields(),
112
			[$this->getHoneypotField()],
113
			$this->normalizeFields( glsr( Form::class )->getFields( 'submission-form' ))
114
		);
115
		// 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...
116
		return $fields;
117
	}
118
119
	/**
120
	 * @return array
121
	 */
122
	protected function getHiddenFields()
123
	{
124
		$fields = [[
125
			'name' => 'action',
126
			'value' => 'submit-review',
127
		],[
128
			'name' => 'assign_to',
129
			'value' => $this->args['assign_to'],
130
		],[
131
			'name' => 'category',
132
			'value' => $this->args['category'],
133
		],[
134
			'name' => 'excluded',
135
			'value' => $this->args['excluded'], // @todo should default to "[]"
136
		],[
137
			'name' => 'form_id',
138
			'value' => $this->args['id'],
139
		],[
140
			'name' => 'nonce',
141
			'value' => wp_create_nonce( 'submit-review' ),
142
		],[
143
			'id' => 'recaptcha-token',
144
			'name' => 'recaptcha-token',
145
		],[
146
			'name' => 'referer',
147
			'value' => wp_unslash( filter_input( INPUT_SERVER, 'REQUEST_URI' )),
148
		]];
149
		return array_map( function( $field ) {
150
			return new Field( wp_parse_args( $field, ['type' => 'hidden'] ));
151
		}, $fields );
152
	}
153
154
	/**
155
	 * @return Field
156
	 */
157
	protected function getHoneypotField()
158
	{
159
		return new Field([
160
			'name' => 'gotcha',
161
			'type' => 'honeypot',
162
		]);
163
	}
164
165
	/**
166
	 * @return string
167
	 */
168
	protected function getRecaptchaCustom()
169
	{
170
		return glsr( Builder::class )->div([
171
			'class' => 'glsr-recaptcha-holder',
172
			'data-badge' => sanitize_text_field( glsr( OptionManager::class )->get( 'settings.submissions.recaptcha.position' )),
173
			'data-sitekey' => sanitize_text_field( glsr( OptionManager::class )->get( 'settings.submissions.recaptcha.key' )),
174
			'data-size' => 'invisible',
175
		]);
176
	}
177
178
	/**
179
	 * @return string
180
	 */
181
	protected function getRecaptchaInvisibleRecaptcha()
182
	{
183
		ob_start();
184
		do_action( 'google_invre_render_widget_action' );
185
		$html = ob_get_clean();
186
		return glsr( Builder::class )->div( $html, [
187
			'class' => 'glsr-recaptcha-holder',
188
		]);
189
	}
190
191
	/**
192
	 * @return void
193
	 */
194
	protected function normalizeFieldErrors( Field &$field )
195
	{
196
		if( !array_key_exists( $field->field['path'], $this->errors ))return;
197
		$field->field['errors'] = $this->errors[$field->field['path']];
198
	}
199
200
	/**
201
	 * @return void
202
	 */
203
	protected function normalizeFieldRequired( Field &$field )
204
	{
205
		if( !in_array( $field->field['path'], $this->required ))return;
206
		$field->field['required'] = true;
207
	}
208
209
	/**
210
	 * @return array
211
	 */
212
	protected function normalizeFields( $fields )
213
	{
214
		foreach( $fields as &$field ) {
215
			$this->normalizeFieldErrors( $field );
216
			$this->normalizeFieldRequired( $field );
217
			$this->normalizeFieldValue( $field );
218
		}
219
		return $fields;
220
	}
221
222
	/**
223
	 * @return void
224
	 */
225
	protected function normalizeFieldValue( Field &$field )
226
	{
227
		if( !array_key_exists( $field->field['path'], $this->values ))return;
228
		if( in_array( $field->field['type'], ['radio', 'checkbox'] )) {
229
			$field->field['checked'] = $field->field['value'] == $this->values[$field->field['path']];
230
		}
231
		else {
232
			$field->field['value'] = $this->values[$field->field['path']];
233
		}
234
	}
235
}
236