Passed
Push — master ( d8e403...d1c323 )
by Paul
05:05
created

SiteReviewsForm::getHiddenFields()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 0
dl 0
loc 24
ccs 0
cts 24
cp 0
crap 2
rs 8.9713
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Html\Partials;
4
5
use GeminiLabs\SiteReviews\Modules\Html;
6
use GeminiLabs\SiteReviews\Modules\Html\Builder;
7
use GeminiLabs\SiteReviews\Modules\Html\Field;
8
use GeminiLabs\SiteReviews\Modules\Html\Form;
9
use GeminiLabs\SiteReviews\Modules\Html\Partial;
10
use GeminiLabs\SiteReviews\Modules\Html\Template;
11
12
class SiteReviewsForm
13
{
14
	/**
15
	 * @var array
16
	 */
17
	protected $args;
18
19
	/**
20
	 * @return void|string
21
	 */
22
	public function build( array $args = [] )
23
	{
24
		$this->args = $args;
25
		return glsr( Template::class )->build( 'templates/reviews-form', [
26
			'context' => [
27
				'class' => $this->getClass(),
28
				'id' => $this->args['id'],
29
				'results' => $this->buildResults(),
30
				'submit_button' => $this->buildSubmitButton(),
31
			],
32
			'fields' => $this->getFields(),
33
		]);
34
	}
35
36
	/**
37
	 * @return string
38
	 */
39
	public function buildResults()
40
	{
41
		return glsr( Partial::class )->build( 'form-results' );
42
	}
43
44
	/**
45
	 * @return string
46
	 */
47
	public function buildSubmitButton()
48
	{
49
		return glsr( Builder::class )->button( '<span></span>'.__( 'Submit your review', 'site-reviews' ), [
50
			'type' => 'submit',
51
		]);
52
	}
53
54
	/**
55
	 * @return string
56
	 */
57
	protected function getClass()
58
	{
59
		$style = apply_filters( 'site-reviews/style', 'glsr-style' );
60
		return trim( 'glsr-form '.$style.' '.$this->args['class'] );
61
	}
62
63
	/**
64
	 * @return array
65
	 */
66
	protected function getFields()
67
	{
68
		$fields = array_merge(
69
			[$this->getHoneypotField()],
70
			glsr( Form::class )->getFields( 'submission-form' ),
71
			$this->getHiddenFields()
72
		);
73
		glsr_debug( $fields );
74
		return $fields;
75
	}
76
77
	/**
78
	 * @return array
79
	 */
80
	protected function getHiddenFields()
81
	{
82
		$fields = [[
83
			'name' => 'assign_to',
84
			'value' => $this->args['assign_to'],
85
		],[
86
			'name' => 'category',
87
			'value' => $this->args['category'],
88
		],[
89
			'name' => 'excluded',
90
			'value' => $this->args['excluded'],
91
		],[
92
			'name' => 'id',
93
			'value' => $this->args['id'],
94
		],[
95
			'name' => '_wp_http_referer',
96
			'value' => wp_get_referer(),
97
		],[
98
			'name' => '_wpnonce',
99
			'value' => wp_create_nonce( 'post-review' ),
100
		]];
101
		return array_values( array_map( function( $field ) {
102
			return new Field( wp_parse_args( $field, ['type' => 'hidden'] ));
103
		}, $fields ));
104
	}
105
106
	/**
107
	 * @return Field
108
	 */
109
	protected function getHoneypotField()
110
	{
111
		return new Field([
112
			'name' => 'gotcha',
113
			'type' => 'honeypot',
114
		]);
115
	}
116
}
117